:root {
  --bg: #0d0f14;
  --surface: #161a22;
  --surface-2: #1e2430;
  --line: #2a3140;
  --text: #e9edf5;
  --muted: #8a93a6;
  --accent: #e3366b;
  --accent-soft: rgba(227, 54, 107, 0.16);
  --ok: #3ccf8e;
  --warn: #f3b33d;
  --bad: #f2555a;
  --focus: #7cb4ff;
  --radius: 18px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #eef1f6;
    --surface: #ffffff;
    --surface-2: #f1f3f8;
    --line: #d9dee8;
    --text: #141821;
    --muted: #5d6679;
    --accent-soft: rgba(227, 54, 107, 0.12);
    --focus: #1f5fbf;
    color-scheme: light;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.bar, main, .keys, .tabs { width: 100%; max-width: 980px; margin-inline: auto; }

button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, a:focus-visible, select:focus-visible, input:focus-visible, .pad:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}
.pad.locked:focus-visible { outline: none; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- top bar ---------- */
.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + var(--safe-t)) 16px 10px;
}
.status {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
  background: none;
  border: 0;
  padding: 4px 0;
  text-align: left;
}
.status-text { display: flex; flex-direction: column; min-width: 0; }
.status-text strong { font-size: 15px; }
.status-text small { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); flex: none; transition: background .2s; }
.dot.ready { background: var(--ok); box-shadow: 0 0 0 4px rgba(60, 207, 142, .18); }
.dot.busy { background: var(--warn); animation: pulse 1.2s infinite; }
.dot.bad { background: var(--bad); }
.dot.warn { background: var(--warn); }
.dot.off { background: transparent; box-shadow: inset 0 0 0 2px var(--muted); }
@keyframes pulse { 50% { opacity: .35; } }

.bar-actions { display: flex; gap: 8px; flex: none; }
.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
}
.icon-btn:active { background: var(--surface-2); }

/* One power button: "Turn on" is the loud, obvious action when the TV is off. */
.power-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 44px;
  padding: 0 14px 0 11px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-weight: 600;
  font-size: 14px;
  white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s;
}
.power-btn svg { width: 20px; height: 20px; flex: none; }
.power-btn:active { background: var(--surface-2); }
.power-btn[data-mode="on"] svg { color: var(--ok); }
.power-btn[data-mode="off"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.power-btn[data-mode="off"]:active { filter: brightness(.9); }
.power-btn[data-mode="busy"] { cursor: progress; color: var(--muted); }
.power-btn[data-mode="busy"] svg { animation: pulse 1.2s infinite; color: var(--warn); }
@media (max-width: 360px) {
  .power-btn { padding: 0 11px; }
  .power-btn[data-mode="on"] .power-label, .power-btn[data-mode="unknown"] .power-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
}

/* ---------- main ---------- */
main { flex: 1; min-height: 0; display: flex; padding: 0 16px; }
.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.view[hidden] { display: none; }

.pad {
  position: relative;
  flex: 1;
  border-radius: var(--radius);
  background:
    radial-gradient(circle at 50% 40%, var(--accent-soft), transparent 60%),
    var(--surface);
  border: 1px solid var(--line);
  touch-action: none;
  outline: none;
  overflow: hidden;
  cursor: pointer;
}
.pad.locked { border-color: var(--accent); cursor: none; }
.pad.active { background: var(--surface-2); }
.pad.dragging { border-color: var(--warn); }
/* While the TV isn't connected the state card replaces the gesture hints. */
.pad.disabled .pad-hint { opacity: 0; }
.pad.disabled .scroll-strip { opacity: .2; }

.pad-hint {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  pointer-events: none;
  transition: opacity .3s;
}
.pad-hint ul { margin: 0; padding: 0; list-style: none; line-height: 1.5; display: grid; gap: 6px; }
.pad-hint b { color: var(--text); font-weight: 600; }
.pad.locked .pad-hint, .pad.used .pad-hint { opacity: 0; }

.lock-tip {
  position: absolute;
  left: 50%; bottom: 12px;
  transform: translateX(-50%);
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 12.5px;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity .2s;
}
.pad.locked .lock-tip { opacity: 1; }
.pad-hint .hint-mouse { display: none; }
@media (hover: hover) and (pointer: fine) {
  .pad-hint .hint-touch { display: none; }
  .pad-hint .hint-mouse { display: grid; }
}
kbd { border: 1px solid var(--line); border-radius: 5px; padding: 0 5px; font: inherit; font-size: 12px; }

/* TV state on the pad: off, standby, waking, pairing... One calm card. */
.pad-state {
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  width: min(calc(100% - 48px), 340px);
  padding: 22px 20px 20px;
  border-radius: 18px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-align: center;
  cursor: default;
  user-select: text;
}
.pad-state[hidden] { display: none; }
.pad-state[data-tone="bad"] { border-color: var(--bad); }
.state-title { font-size: 17px; }
.state-detail { margin: 0; color: var(--muted); font-size: 14px; line-height: 1.45; }
.state-detail:empty { display: none; }
.state-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 6px; width: 100%; }
.state-actions:empty { display: none; }
.state-actions .pill { flex: 1 1 auto; min-width: 120px; max-width: 220px; }
.state-icon {
  width: 44px; height: 44px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--surface); border: 1px solid var(--line);
  margin-bottom: 4px;
}
.state-icon::before {
  content: ""; width: 12px; height: 12px; border-radius: 50%;
  box-shadow: inset 0 0 0 2px var(--muted);
}
.state-icon.connecting::before, .state-icon.waking::before, .state-icon.turningoff::before,
.state-icon.pairing::before, .state-icon.link::before { background: var(--warn); box-shadow: none; animation: pulse 1.2s infinite; }
.state-icon.unpaired::before { background: var(--warn); box-shadow: none; }
.state-icon.error::before { background: var(--bad); box-shadow: none; }

/* Same state on the Remote tab, as a slim strip. */
.notice {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: none;
  min-height: 52px;
  padding: 6px 6px 6px 14px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  font-weight: 600;
  font-size: 14px;
  order: -1;
}
.notice[hidden] { display: none; }
.notice-text { flex: 1; min-width: 0; }
.notice-actions { display: flex; gap: 6px; }
.notice-actions .pill { flex: none; min-width: 0; height: 40px; }
@media (min-width: 760px) { .notice { grid-column: 1 / -1; } }

.scroll-strip {
  position: absolute;
  top: 12px; bottom: 12px; right: 6px;
  width: 26px;
  border-radius: 13px;
  background: repeating-linear-gradient(to bottom, transparent 0 10px, var(--line) 10px 12px);
  opacity: .5;
}
@media (hover: hover) and (pointer: fine) { .scroll-strip { display: none; } }

/* ---------- remote view ---------- */
.dpad {
  position: relative;
  width: min(78vw, 300px, 42dvh);
  aspect-ratio: 1;
  margin: auto auto 18px;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--line);
}
.dk {
  position: absolute;
  display: grid;
  place-items: center;
  border: 0;
  background: transparent;
  color: var(--text);
  border-radius: 14px;
}
.dk:active { background: var(--surface-2); }
.dk svg { width: 30px; height: 30px; }
.dk.up    { top: 4%;  left: 33%; width: 34%; height: 29%; }
.dk.down  { bottom: 4%; left: 33%; width: 34%; height: 29%; }
.dk.left  { left: 4%; top: 33%; width: 29%; height: 34%; }
.dk.right { right: 4%; top: 33%; width: 29%; height: 34%; }
.dk.ok {
  top: 31%; left: 31%; width: 38%; height: 38%;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 17px;
}
.dk.ok:active { filter: brightness(.85); }

#view-remote { overflow-y: auto; gap: 8px; padding-bottom: 4px; scrollbar-width: none; }
#view-remote .dpad { margin: 4px auto 10px; flex: none; }
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  flex: none;
}
.grid.g2 { grid-template-columns: repeat(2, 1fr); }
.grid.g4 { grid-template-columns: repeat(4, 1fr); }
.grid.g5 { grid-template-columns: repeat(5, 1fr); }
.grid[hidden] { display: none; }
.grid button { font-size: 14px; }
.cdot { width: 26px; height: 10px; border-radius: 5px; }
.cdot.red { background: #e5484d; }
.cdot.green { background: #30a46c; }
.cdot.yellow { background: #f5d90a; }
.cdot.blue { background: #3e63dd; }
#btn-numpad[aria-expanded="true"] { border-color: var(--accent); color: var(--accent); }
.grid button {
  height: 48px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--surface);
  display: grid;
  place-items: center;
}
.grid button:active { background: var(--surface-2); }

/* ---------- key row + tabs ---------- */
.keys {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
  padding: 10px 16px 0;
}
.keys button {
  height: 48px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--surface);
  display: grid;
  place-items: center;
}
.keys button:active, .keys button.on { background: var(--surface-2); border-color: var(--accent); }
#btn-kbd.hint { border-color: var(--accent); color: var(--accent); }

.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  padding: 10px 16px calc(10px + var(--safe-b));
}
.tab {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 42px;
  border-radius: 12px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
}
.tab svg { width: 19px; height: 19px; }
.tab[aria-selected="true"] { background: var(--surface); color: var(--text); }

/* ---------- sheets ---------- */
.sheet {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, .45);
  z-index: 10;
}
.sheet[hidden] { display: none; }
.sheet-card {
  width: min(100%, 520px);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 22px 22px 0 0;
  padding: 16px 18px calc(20px + var(--safe-b));
  display: flex;
  flex-direction: column;
  gap: 14px;
  user-select: text;
}
@media (min-width: 600px) {
  .sheet { align-items: center; }
  .sheet-card { border-radius: 22px; }
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.sheet-head strong { font-size: 17px; }
.sheet-head .icon-btn { width: 40px; height: 40px; border: 0; background: var(--surface-2); flex: none; }
.sheet-body { display: flex; flex-direction: column; gap: 20px; overflow-y: auto; min-height: 0; margin: 0 -4px; padding: 0 4px 2px; }
.group { display: flex; flex-direction: column; gap: 14px; }
.group h3 {
  margin: 0; font-size: 12px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--muted);
}
.group + .group { border-top: 1px solid var(--line); padding-top: 18px; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; margin: 0; font-size: 14px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.empty-note { display: flex; flex-direction: column; align-items: flex-start; gap: 10px; padding: 8px 2px; }
.empty-note .pill { flex: none; }

#kbd-input {
  width: 100%;
  height: 50px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  font-size: 17px;
  outline: none;
}
#kbd-input:focus { border-color: var(--accent); }

.field { display: flex; flex-direction: column; gap: 6px; }
.field span { display: flex; justify-content: space-between; }
.field .hint { color: var(--muted); font-size: 12.5px; line-height: 1.35; }
.field[hidden], .check[hidden] { display: none; }
.field output { color: var(--muted); font-variant-numeric: tabular-nums; }
input[type="range"] { width: 100%; accent-color: var(--accent); }
.check { display: flex; align-items: center; gap: 12px; min-height: 36px; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); flex: none; }

.row { display: flex; flex-wrap: wrap; gap: 8px; }
.pill {
  flex: 1;
  min-width: 120px;
  height: 44px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  font-weight: 600;
  display: grid;
  place-items: center;
  text-decoration: none;
  color: var(--text);
}
.pill.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.pill.danger { color: var(--bad); }
.pill.danger.armed { background: var(--bad); border-color: var(--bad); color: #fff; }
.pill[hidden] { display: none; }

.muted { color: var(--muted); }
.small { font-size: 13px; margin: 0; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(140px + var(--safe-b));
  transform: translateX(-50%);
  width: max-content;
  max-width: min(calc(100% - 32px), 460px);
  padding: 11px 16px;
  border-radius: 12px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
  z-index: 20;
  font-size: 14px;
  line-height: 1.4;
  cursor: pointer;
}
.toast.bad { border-color: var(--bad); border-left-width: 4px; }
.toast[hidden] { display: none; }

/* ---------- login ---------- */
.login {
  margin: auto;
  width: min(100% - 32px, 380px);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 28px 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  user-select: text;
}
.login h1 { margin: 0; font-size: 22px; display: flex; gap: 10px; align-items: center; }
.login h1 svg { color: var(--accent); width: 26px; height: 26px; }
.login input {
  height: 48px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
}
.login input:focus { outline: none; border-color: var(--accent); }
.login .error { color: var(--bad); margin: 0; font-size: 14px; }
.login .error:empty { display: none; }
.login .pill { flex: none; }

/* ---------- labeled buttons, pickers ---------- */
.grid.labeled button {
  height: 62px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
}
.grid.labeled button span { font-size: 11.5px; color: var(--muted); line-height: 1; }
.grid.labeled button:active span { color: var(--text); }
.grid.row-icons button { height: 48px; flex-direction: row; gap: 8px; }
.grid.row-icons button span { font-size: 14px; color: var(--text); }
#btn-numpad[aria-expanded="true"] span { color: var(--accent); }

.sheet-card.tall { max-height: 85dvh; }
.pick-list { display: flex; flex-direction: column; gap: 8px; overflow-y: auto; min-height: 0; }
.pick-list button {
  display: flex; align-items: center; gap: 12px;
  height: 52px; padding: 0 14px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--surface-2);
  text-align: left;
}
.pick-list button small { margin-left: auto; color: var(--muted); font-size: 12.5px; }
.pick-list button.off span { color: var(--muted); }
.app-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 10px;
  overflow-y: auto;
  min-height: 0;
}
.app-grid button {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 10px 4px; border-radius: 14px; border: 0; background: transparent;
}
.app-grid button:active { background: var(--surface-2); }
.app-grid img, .app-grid .letter {
  width: 56px; height: 56px; border-radius: 14px; object-fit: cover; background: var(--surface-2);
}
.app-grid .letter { display: grid; place-items: center; font-size: 22px; font-weight: 700; color: var(--accent); }
.app-grid span {
  font-size: 12px; text-align: center; line-height: 1.2;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}

/* ---------- shortcuts ---------- */
.rcol { display: contents; }
/* A stretching grid (like .app-grid) so shortcut rows end flush with the
   button grids above instead of ragged fixed-width tiles. */
.shortcuts {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 8px;
  flex: none;
}
.shortcut {
  min-width: 0;
  display: flex; flex-direction: column; align-items: center; gap: 5px;
  padding: 8px 2px 6px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--surface);
}
.shortcut:active { background: var(--surface-2); }
.shortcut img, .shortcut .letter {
  width: 40px; height: 40px; border-radius: 10px; object-fit: cover; background: var(--surface-2);
}
.shortcut .letter { display: grid; place-items: center; font-weight: 700; color: var(--accent); }
.shortcut span { font-size: 11px; color: var(--muted); max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 4px; }
.shortcut.add { border-style: dashed; }
.shortcut.add svg { width: 40px; height: 40px; padding: 8px; color: var(--muted); }

.pill.slim { flex: none; min-width: 0; height: 32px; font-size: 13px; margin-left: auto; margin-right: 8px; }
.pill.slim[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.app-grid button { position: relative; }
.app-grid.editing button { opacity: .5; }
.app-grid.editing button.pinned { opacity: 1; }
.app-grid button .badge {
  position: absolute; top: 4px; right: 12px;
  width: 22px; height: 22px; border-radius: 50%;
  background: var(--accent); color: #fff; display: none; place-items: center;
}
.app-grid button .badge svg { width: 14px; height: 14px; stroke-width: 3; }
.app-grid.editing button.pinned .badge { display: grid; }

/* ---------- wide screens: two columns, no vertical scroll ---------- */
@media (min-width: 760px) {
  #view-remote:not([hidden]) {
    display: grid;
    grid-template-columns: minmax(280px, 1fr) minmax(360px, 1.25fr);
    gap: 20px;
    align-items: center;
  }
  .rcol { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
  #view-remote .dpad { width: min(300px, 46dvh); margin: 0 auto 8px; }
  .grid.labeled button { height: min(62px, 8.2dvh); }
}

/* Same visual order on phones (single column) and desktop (two columns). */
#view-remote .dpad { order: 0; }
#view-remote .grid.labeled:not(.row-icons) { order: 1; }
#view-remote .grid.g5 { order: 2; }
#view-remote .shortcuts { order: 3; }
#view-remote .grid.row-icons { order: 4; }
#view-remote .numpad { order: 5; }
#view-remote .colors { order: 6; }
@media (min-width: 760px) {
  #view-remote .numpad { grid-template-columns: repeat(6, 1fr); }
  #view-remote .numpad button { height: 42px; }
}

select {
  height: 44px; padding: 0 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface-2); color: var(--text); font: inherit;
}
@media (prefers-reduced-motion: reduce) {
  *, *::before { animation: none !important; transition: none !important; }
}
