:root {
  color-scheme: light;
  --bg: #f2f3f5;
  --surface: #ffffff;
  --ink: #161719;
  --muted: #676b72;
  --line: #d7dae0;
  --line-strong: #b8bdc6;
  --accent: #0878d1;
  --accent-dark: #0563ad;
  --good: #168a48;
  --warn: #a56600;
  --bad: #c7362c;
  --radius: 8px;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  min-height: 100vh;
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  padding-bottom: env(safe-area-inset-bottom);
}

button,
input,
select,
textarea { font: inherit; }

button {
  min-height: 46px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  font-weight: 750;
  padding: 0 14px;
  cursor: pointer;
  touch-action: manipulation;
}

button:hover { border-color: var(--line-strong); }
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible { outline: 3px solid #0878d133; outline-offset: 1px; }
button:disabled { cursor: wait; opacity: .58; }
.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.primary:hover { background: var(--accent-dark); border-color: var(--accent-dark); }
.danger { color: var(--bad); }
.wide { width: 100%; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  min-height: calc(60px + env(safe-area-inset-top));
  background: #111214;
  color: #fff;
  padding: calc(9px + env(safe-area-inset-top)) max(14px, env(safe-area-inset-left)) 9px max(14px, env(safe-area-inset-right));
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.topbar div { display: grid; }
.topbar span { color: #aeb1b7; font-size: .72rem; }
.icon-button { width: 46px; padding: 0; background: #292b2f; border-color: #404248; color: #fff; font-size: 1.35rem; }

main { width: min(920px, 100%); margin: auto; padding: 14px; }
.display-section { display: grid; gap: 10px; }

.display-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 2 / 1;
  overflow: hidden;
  border: 1px solid #323438;
  border-radius: 6px;
  background: #000;
  box-shadow: 0 8px 24px #0002;
}

.display-frame canvas { display: block; width: 100%; height: 100%; image-rendering: pixelated; }
.pixel-grid,
.safe-area { position: absolute; inset: 0; pointer-events: none; }
.pixel-grid {
  background-image: linear-gradient(#fff0 78%, #fff2 78%), linear-gradient(90deg, #fff0 78%, #fff2 78%);
  background-size: calc(100% / 128) calc(100% / 64);
}
.safe-area { inset: 3.125% 1.5625%; border: 1px dashed #ffd60acc; }
.display-meta { display: flex; justify-content: space-between; color: var(--muted); font: 700 .76rem ui-monospace, monospace; }
.transport { display: grid; grid-template-columns: 50px 1fr 50px; gap: 8px; }
.transport button { min-height: 50px; font-size: 1.15rem; }
.transport .primary { font-size: 1rem; }

.command-state {
  min-height: 38px;
  border: 1px solid var(--line);
  border-left: 4px solid var(--accent);
  border-radius: 6px;
  background: var(--surface);
  padding: 9px 10px;
  color: var(--muted);
  font-size: .82rem;
  font-weight: 700;
}
.command-state.success { border-left-color: var(--good); color: var(--good); }
.command-state.warning { border-left-color: var(--warn); color: var(--warn); }
.command-state.error { border-left-color: var(--bad); color: var(--bad); }

.device-strip {
  margin: 14px 0;
  display: grid;
  grid-template-columns: 14px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 12px;
}
.device-strip div { min-width: 0; display: grid; }
.device-strip strong,
.device-strip small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device-strip small { color: var(--muted); }
.dot { width: 12px; height: 12px; border-radius: 50%; background: #92959a; }
.dot.online { background: var(--good); box-shadow: 0 0 0 4px #168a4820; }
.dot.offline { background: var(--bad); }
.dot.pending { background: var(--warn); box-shadow: 0 0 0 4px #a566001c; }

.tabs { display: grid; grid-template-columns: repeat(3, 1fr); border-bottom: 1px solid var(--line); margin: 0 -14px; }
.tabs button { min-width: 0; border: 0; border-radius: 0; background: transparent; color: var(--muted); }
.tabs button.active { color: var(--ink); box-shadow: inset 0 -3px var(--accent); }
.tab-panel { display: none; padding: 16px 0; }
.tab-panel.active { display: block; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
h1,
h2 { margin: 0; font-size: 1.1rem; letter-spacing: 0; }
#sceneTypeBadge { color: var(--muted); font-size: .78rem; font-weight: 750; }

.scene-list { display: grid; gap: 8px; margin-bottom: 12px; }
.scene-empty { border: 1px dashed var(--line-strong); border-radius: var(--radius); padding: 22px 14px; color: var(--muted); text-align: center; }
.scene-item {
  display: grid;
  grid-template-columns: 24px 64px minmax(0, 1fr) auto;
  gap: 9px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 8px;
}
.scene-item.active { border-color: var(--accent); box-shadow: 0 0 0 2px #0878d118; }
.scene-item.dragging { opacity: .55; }
.drag-handle { min-height: 42px; padding: 0; border: 0; background: transparent; color: var(--muted); cursor: grab; font-size: 1.1rem; }
.mini-oled { width: 64px; height: 32px; border-radius: 3px; background: #000; image-rendering: pixelated; }
.scene-copy { min-width: 0; display: grid; gap: 2px; }
.scene-copy strong,
.scene-copy small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scene-copy small { color: var(--muted); }
.scene-actions { display: grid; grid-template-columns: repeat(3, 38px); gap: 4px; }
.scene-actions button { width: 38px; min-height: 38px; padding: 0; }
.scene-actions .enabled { color: var(--good); }
.scene-actions .paused { color: var(--warn); }

.control-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
label { display: grid; gap: 6px; margin-bottom: 10px; color: var(--muted); font-size: .82rem; font-weight: 700; }
input,
select,
textarea { width: 100%; min-height: 46px; border: 1px solid var(--line); border-radius: var(--radius); background: #fff; color: var(--ink); padding: 9px 10px; }
input:disabled { color: var(--muted); background: #eaecf0; }
textarea { min-height: 110px; resize: vertical; }
.toggle { display: flex; grid-template-columns: none; align-items: center; gap: 9px; min-height: 46px; color: var(--ink); }
.toggle input { width: 22px; min-height: 22px; accent-color: var(--accent); }
.editor { padding: 12px 0; border-top: 1px solid var(--line); }
.editor-subhead { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 8px 0 12px; padding-top: 14px; border-top: 1px solid var(--line); }
.editor-subhead h2 { font-size: .92rem; }
.editor-subhead select { width: auto; min-width: 112px; margin: 0; }
.live-filters { min-width: 0; margin: 4px 0 14px; padding: 12px 0 2px; border: 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.live-filters:disabled { opacity: .5; }
.live-filters legend { padding: 0 8px 0 0; color: var(--ink); font-size: .92rem; font-weight: 800; }
.filter-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 2px 10px; }
.filter-grid .toggle { min-width: 0; margin: 0; }
.filter-grid .toggle span { line-height: 1.2; overflow-wrap: anywhere; }
.compact-note { color: var(--muted); line-height: 1.5; }

.connection-state { margin-bottom: 12px; border: 1px solid var(--line); border-left: 4px solid var(--warn); border-radius: 6px; background: var(--surface); padding: 11px; font-weight: 750; }
.connection-state.online { border-left-color: var(--good); color: var(--good); }
.connection-state.error { border-left-color: var(--bad); color: var(--bad); }
.status-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin: 12px 0; }
.status-grid div { min-width: 0; display: grid; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); padding: 10px; }
.status-grid span { color: var(--muted); font-size: .75rem; }
.status-grid strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.scene-types { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin: 14px 0; }
.scene-types button { min-height: 60px; }
dialog { width: min(440px, calc(100% - 28px)); border: 0; border-radius: var(--radius); padding: 18px; }
dialog::backdrop { background: #0008; }
.toast { position: fixed; z-index: 20; left: 50%; bottom: calc(18px + env(safe-area-inset-bottom)); transform: translateX(-50%); max-width: calc(100% - 28px); border-radius: var(--radius); background: #111; color: #fff; padding: 11px 14px; font-weight: 700; }

@media (min-width: 760px) {
  main { display: grid; grid-template-columns: 1.05fr .95fr; gap: 18px; }
  .display-section,
  .device-strip,
  .tabs { grid-column: 1; }
  .tab-panel { grid-column: 2; grid-row: 1 / 5; }
  .tabs { align-self: end; margin: 0; }
  .display-frame { margin-top: 2px; }
}

@media (max-width: 520px) {
  main { padding: 12px; }
  .tabs { margin: 0 -12px; }
  .control-grid { grid-template-columns: 1fr; }
  .scene-item { grid-template-columns: 24px 56px minmax(0, 1fr); }
  .mini-oled { width: 56px; height: 28px; }
  .scene-actions { grid-column: 2 / 4; display: flex; justify-content: flex-end; }
  .scene-actions button { width: 42px; min-height: 42px; }
  .device-strip button { padding-inline: 10px; }
}

@media (max-width: 360px) {
  .display-meta span:last-child { display: none; }
  .scene-actions { justify-content: space-between; }
  .filter-grid { grid-template-columns: 1fr; }
}
