:root {
  --bg: #0e1013;
  --surface: #171a1f;
  --surface-2: #1f232a;
  --surface-3: #272c35;
  --line: rgba(255, 255, 255, 0.08);
  --line-strong: rgba(255, 255, 255, 0.16);
  --ink: #f3f5f8;
  --muted: #9aa3b2;
  --faint: #6b7483;
  --accent: #f6b650;
  --accent-ink: #1a1304;
  --accent-soft: rgba(246, 182, 80, 0.14);
  --live: #3ddc84;
  --warn: #ff7b72;
  --radius: 14px;
  --radius-sm: 10px;
  --sans: "Inter", -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
}

body {
  height: 100dvh;
  font-family: var(--sans);
  font-size: 15px;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

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

button { -webkit-tap-highlight-color: transparent; }

/* Layout */

.wrap {
  width: 100%;
  height: 100%;
  padding:
    max(12px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}

.top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  flex: 0 0 auto;
}

h1, h2, h3 {
  margin: 0;
  font-weight: 700;
  letter-spacing: -0.02em;
}

h1 { font-size: clamp(1.25rem, 2vw, 1.6rem); line-height: 1.1; }

h2 { font-size: 1.35rem; line-height: 1.15; }

h3 {
  margin: 20px 0 8px;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.lede {
  margin: 2px 0 0;
  max-width: 40rem;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.35;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  margin: 0;
  padding: 0 12px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 600;
  white-space: nowrap;
}

.pill::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--faint);
}

.pill.live { color: var(--ink); }
.pill.live::before { background: var(--live); box-shadow: 0 0 0 3px rgba(61, 220, 132, 0.18); }
.pill.down { color: var(--warn); border-color: rgba(255, 123, 114, 0.35); }
.pill.down::before { background: var(--warn); }

.ghost, .solid {
  height: 36px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  background: transparent;
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
}

.ghost:hover { background: var(--surface-2); }

.solid {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: transparent;
}

.solid:hover { filter: brightness(1.06); }

.wide { width: 100%; margin-top: 10px; height: 44px; }

/* Presets */

.presets { margin: 0; flex: 0 0 auto; }

.preset-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.preset {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 54px;
  padding: 8px 16px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  text-align: left;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.preset:hover { background: var(--surface-2); border-color: var(--line-strong); }
.preset:active { background: var(--surface-3); }
.preset:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.preset .kicker { display: none; }

.preset-default {
  background: var(--accent-soft);
  border-color: rgba(246, 182, 80, 0.45);
  color: var(--accent);
}

.preset-default:hover { background: rgba(246, 182, 80, 0.22); }

.preset.quiet { color: var(--faint); }

.preset-name {
  font-size: 1.02rem;
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.preset-state {
  margin-left: auto;
  color: var(--faint);
  font-size: 0.62rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  white-space: nowrap;
}

/* Banner */

.banner {
  margin: 0;
  flex: 0 0 auto;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid rgba(255, 123, 114, 0.3);
  background: rgba(255, 123, 114, 0.1);
  color: var(--warn);
  font-size: 0.85rem;
}

/* Board */

.board {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.tv {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  width: 100%;
  min-height: 0;
  height: 100%;
  text-align: left;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  overflow: hidden;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

.tv:hover { background: var(--surface-2); border-color: var(--line-strong); }
.tv:active { transform: scale(0.99); }
.tv:focus-visible, .source:focus-visible, .ghost:focus-visible, .solid:focus-visible,
input:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.tv-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.kicker {
  color: var(--faint);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.lamp {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--faint);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #454c58;
}

.lamp.on { color: var(--muted); }
.lamp.on .dot { background: var(--live); }

.tv-name {
  margin: 4px 0 0;
  font-size: clamp(1.1rem, 2vw, 1.7rem);
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.also {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 500;
  line-height: 1.2;
}

.showing-label {
  margin: auto 0 0;
  padding-top: 8px;
  color: var(--faint);
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.showing {
  margin: 4px 0 0;
  align-self: flex-start;
  max-width: 100%;
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: clamp(0.95rem, 1.6vw, 1.35rem);
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.note, .audio-line {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.78rem;
}

.note.warn { color: var(--warn); }

.tv.off { opacity: 0.6; }

.foot {
  margin: 0;
  flex: 0 0 auto;
  color: var(--faint);
  font-size: 0.72rem;
}

/* Dialogs */

dialog {
  border: 0;
  padding: 0;
  background: transparent;
  width: min(520px, calc(100vw - 24px));
  color: var(--ink);
}

dialog::backdrop { background: rgba(0, 0, 0, 0.6); backdrop-filter: blur(4px); }

.sheet {
  padding: 22px 20px 18px;
  border-radius: 20px;
  border: 1px solid var(--line);
  background: var(--surface);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
  max-height: min(88vh, 900px);
  overflow: auto;
}

.sources { display: grid; gap: 6px; margin-top: 16px; }

.source {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 56px;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--surface-2);
  text-align: left;
  cursor: pointer;
}

.source:hover { background: var(--surface-3); }

.source.current {
  border-color: rgba(246, 182, 80, 0.6);
  background: var(--accent-soft);
}

.source-copy { display: grid; gap: 1px; }

.source-copy .kicker { font-size: 0.65rem; }

.source-name {
  font-size: 1.05rem;
  font-weight: 600;
  line-height: 1.15;
}

.source-state {
  color: var(--faint);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  white-space: nowrap;
}

.source.current .source-state { color: var(--accent); }

/* Forms */

.field { display: grid; gap: 6px; margin-top: 14px; }

.field span, .pair span {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

input, select {
  width: 100%;
  min-height: 42px;
  padding: 0 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong);
  background: var(--bg);
}

input::placeholder { color: var(--faint); }

.fields { display: grid; gap: 8px; }

.pair {
  display: grid;
  grid-template-columns: 40px 1fr;
  gap: 8px;
  align-items: start;
}

.pair .port {
  min-height: 42px;
  display: grid;
  place-items: center;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
}

.stack { display: grid; gap: 6px; }

.sheet-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
  position: sticky;
  bottom: 0;
  padding-top: 12px;
  background: linear-gradient(180deg, transparent, var(--surface) 30%);
}

.sheet-actions .ghost, .sheet-actions .solid { height: 42px; padding: 0 18px; }

.form-error { color: var(--warn); margin: 12px 0 0; font-size: 0.9rem; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

body.busy .tv, body.busy .source, body.busy .preset { pointer-events: none; opacity: 0.6; }

.toast {
  position: fixed;
  left: 50%;
  bottom: max(18px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 50;
  margin: 0;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  max-width: calc(100vw - 32px);
  padding: 12px 18px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  background: var(--surface-3);
  color: var(--ink);
  font-size: 0.95rem;
  font-weight: 600;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
}

.toast[hidden] { display: none; }

.toast.working::before {
  content: "";
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.25);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}

.toast.done { border-color: rgba(61, 220, 132, 0.5); color: var(--live); }
.toast.failed { border-color: rgba(255, 123, 114, 0.5); color: var(--warn); }

@keyframes spin { to { transform: rotate(360deg); } }

/* Responsive */

@media (min-width: 640px) {
  .board {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-template-rows: repeat(2, minmax(0, 1fr));
  }
}

@media (min-width: 900px) {
  .preset-row { grid-template-columns: repeat(5, minmax(0, 1fr)); }
}

@media (min-width: 1280px) {
  .preset-row { grid-template-columns: repeat(9, minmax(0, 1fr)); }
}

@media (max-height: 720px) {
  .lede { display: none; }
  .showing-label { display: none; }
  .tv { padding: 8px 12px; }
  .showing { margin-top: 6px; }
}

@media (max-height: 480px) {
  .wrap { gap: 8px; }
  .tv { padding: 6px 10px; }
  .tv-name { margin-top: 1px; }
  .also { display: none; }
  .showing { margin-top: 3px; padding: 3px 10px; font-size: 0.9rem; }
  .preset { min-height: 40px; padding: 4px 12px; }
}

@media (max-width: 700px) {
  dialog {
    width: 100%;
    max-width: 100%;
    height: 100%;
    max-height: 100%;
    margin: 0;
  }
  .sheet {
    min-height: 100%;
    border-radius: 0;
    max-height: none;
  }
}
