:root {
  color-scheme: light;
  --page: #E4E7EB;
  --ink: #1D2127;
  --muted: #5B626D;
  --panel: #FFFFFF;
  --line: #D2D7DD;

  /* O controle é um objeto físico: mantém o grafite nos dois temas. */
  --shell: #2B2F36;
  --shell-top: #353A43;
  --key: #3B4049;
  --key-pressed: #4B515C;
  --key-ink: #F0F2F5;
  --key-sub: #A9B0BB;

  --claro: #DA291C;
  --claro-pressed: #B51E13;
  --good: #2F9E63;
  --bad: #D93F44;
  --focus: #5AA9FF;

  --font: "Figtree", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #121417;
    --ink: #E7E9EC;
    --muted: #9AA2AD;
    --panel: #1C1F24;
    --line: #2E333A;
    --shell: #22262C;
    --shell-top: #2B3037;
    --key: #333841;
    --key-pressed: #424853;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; }

body {
  background: var(--page);
  color: var(--ink);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.i {
  width: 22px;
  height: 22px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- Topo ---------- */

.app { min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding: 12px 16px;
}

.device {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  padding: 8px 16px 8px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  text-align: left;
}

.dot {
  width: 10px;
  height: 10px;
  flex: none;
  border-radius: 50%;
  background: var(--muted);
  transition: background-color .15s, box-shadow .2s;
}
.dot[data-state="ready"] { background: var(--good); }
.dot[data-state="busy"]  { background: var(--focus); }
.dot[data-state="error"] { background: var(--bad); }
.dot.flash { box-shadow: 0 0 0 5px color-mix(in srgb, var(--good) 35%, transparent); }

.device-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.device-name { font-size: .9375rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.device-sub { font-size: .75rem; color: var(--muted); }

.icon-btn {
  width: 44px;
  height: 44px;
  flex: none;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--panel);
}

/* ---------- Palco ---------- */

.stage {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  gap: 40px;
  padding: 4px 14px 28px;
}

.remote {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 22px 18px 16px;
  border-radius: 40px;
  background: linear-gradient(180deg, var(--shell-top), var(--shell) 35%);
  color: var(--key-ink);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .07), 0 28px 50px -26px rgba(0, 0, 0, .55);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

/* ---------- Teclas ---------- */

.k {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-height: 52px;
  padding: 6px 4px;
  border-radius: 16px;
  background: var(--key);
  color: var(--key-ink);
  font-size: .6875rem;
  font-weight: 500;
  line-height: 1.15;
  touch-action: manipulation;
  transition: background-color .08s, transform .08s;
}
.k span { color: var(--key-sub); }
.k:active, .k.is-pressed { background: var(--key-pressed); transform: translateY(1px) scale(.98); }

.row-top { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }

.k-power { background: var(--claro); color: #fff; }
.k-power:active, .k-power.is-pressed { background: var(--claro-pressed); }

/* D-pad com cantos */
.nav {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  grid-template-rows: 1fr 1fr;
  align-items: center;
  justify-items: center;
}
.dpad { grid-column: 2; grid-row: 1 / span 2; }
.tl { grid-column: 1; grid-row: 1; align-self: start; }
.bl { grid-column: 1; grid-row: 2; align-self: end; }
.tr { grid-column: 3; grid-row: 1; align-self: start; }
.br { grid-column: 3; grid-row: 2; align-self: end; }

.k-corner {
  width: 54px;
  height: 54px;
  min-height: 0;
  padding: 0;
  border-radius: 50%;
  font-size: .5625rem;
  gap: 1px;
}
.k-corner .i { width: 19px; height: 19px; }

.dpad {
  --size: clamp(156px, 52vw, 204px);
  position: relative;
  width: var(--size);
  height: var(--size);
  overflow: hidden;
  border-radius: 50%;
  background: var(--key);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .04), 0 2px 0 rgba(0, 0, 0, .25);
  touch-action: none;
}

.dir {
  position: absolute;
  inset: 0;
  display: flex;
  padding: calc(var(--size) * .07);
  color: var(--key-sub);
  touch-action: none;
  transition: background-color .08s, color .08s;
}
.dir.up    { clip-path: polygon(50% 50%, 0 0, 100% 0);       align-items: flex-start; justify-content: center; }
.dir.down  { clip-path: polygon(50% 50%, 100% 100%, 0 100%); align-items: flex-end;   justify-content: center; }
.dir.left  { clip-path: polygon(50% 50%, 0 100%, 0 0);       align-items: center;     justify-content: flex-start; }
.dir.right { clip-path: polygon(50% 50%, 100% 0, 100% 100%); align-items: center;     justify-content: flex-end; }
.dir .i { width: 26px; height: 26px; stroke-width: 2.2; }
.dir:active, .dir.is-pressed { background: var(--key-pressed); color: var(--key-ink); }
.dir:focus-visible { outline: none; background: var(--key-pressed); color: var(--focus); }

.ok {
  position: absolute;
  inset: 31%;
  border-radius: 50%;
  background: var(--shell);
  color: var(--key-ink);
  box-shadow: 0 0 0 3px var(--claro), 0 6px 14px -6px rgba(0, 0, 0, .6);
  font-size: 1.0625rem;
  font-weight: 700;
  letter-spacing: .02em;
  touch-action: manipulation;
  transition: transform .08s, background-color .08s;
}
.ok:active, .ok.is-pressed { transform: scale(.95); background: var(--claro); }

/* Volume e canal */
.rockers {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  justify-items: center;
}
.rocker {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 64px;
  overflow: hidden;
  border-radius: 32px;
  background: var(--key);
}
.rocker button {
  width: 100%;
  height: 58px;
  display: grid;
  place-items: center;
  touch-action: none;
  transition: background-color .08s;
}
.rocker button:active, .rocker button.is-pressed { background: var(--key-pressed); }
.rocker-label { padding: 2px 0; font-size: .6875rem; color: var(--key-sub); }

.rocker-mid { display: flex; flex-direction: column; gap: 14px; }
.k-round { width: 54px; height: 54px; min-height: 0; padding: 0; border-radius: 50%; }

/* Teclas coloridas */
.colors { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.c {
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  touch-action: manipulation;
}
.c::before {
  content: "";
  width: 100%;
  height: 12px;
  border-radius: 6px;
  background: var(--c);
  transition: transform .08s, filter .08s;
}
.c:active::before, .c.is-pressed::before { transform: scaleY(.8); filter: brightness(.85); }

/* Abas */
.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  padding: 4px;
  border-radius: 14px;
  background: rgba(0, 0, 0, .22);
}
.tab {
  height: 36px;
  border-radius: 10px;
  color: var(--key-sub);
  font-size: .8125rem;
  font-weight: 600;
}
.tab[aria-selected="true"] { background: var(--key); color: var(--key-ink); }

.panels { display: grid; }
.panel { grid-area: 1 / 1; }
.panel:not(.is-active) { visibility: hidden; }

.numpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px 14px;
}
.n { min-height: 52px; border-radius: 26px; font-size: 1.375rem; font-weight: 600; }
.n-text { font-size: .6875rem; font-weight: 500; color: var(--key-sub); }

.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.grid2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.k-wide { font-size: .8125rem; font-weight: 600; }
.k-rec .i { color: var(--claro); }

.wordmark {
  margin: 2px 0 0;
  text-align: center;
  font-size: .875rem;
  font-weight: 700;
  letter-spacing: -.01em;
  color: var(--key-sub);
  opacity: .6;
}

/* ---------- Atalhos (desktop) ---------- */

.help-card { display: none; }

@media (min-width: 900px) {
  .stage { padding-top: 24px; }
  .topbar { max-width: 760px; }
  .help-card {
    display: block;
    position: sticky;
    top: 24px;
    width: 280px;
    padding: 20px 22px;
    border: 1px solid var(--line);
    border-radius: 20px;
    background: var(--panel);
  }
  .help-card h2 { margin: 0 0 14px; font-size: 1rem; }
  .help-card dl {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 9px 14px;
    margin: 0;
    font-size: .875rem;
  }
  .help-card dt { white-space: nowrap; }
  .help-card dd { margin: 0; color: var(--muted); }
}

kbd {
  padding: 2px 7px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--page);
  font-family: inherit;
  font-size: .75rem;
  font-weight: 600;
}

/* Telas bem estreitas (320px) */
@media (max-width: 350px) {
  .remote { padding: 18px 12px 14px; border-radius: 32px; gap: 14px; }
  .row-top, .colors, .grid3, .grid2 { gap: 8px; }
  .k-corner { width: 46px; height: 46px; }
  .k-corner span { display: none; }
}

/* ---------- Diálogo ---------- */

dialog {
  width: min(440px, calc(100% - 24px));
  padding: 0;
  border: 0;
  border-radius: 24px;
  background: var(--panel);
  color: var(--ink);
  box-shadow: 0 30px 80px -20px rgba(0, 0, 0, .45);
}
dialog::backdrop { background: rgba(10, 12, 15, .5); }

@media (max-width: 599px) {
  dialog {
    width: 100%;
    max-width: 100%;
    margin: auto 0 0;
    border-radius: 24px 24px 0 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
}

.sheet { display: flex; flex-direction: column; gap: 14px; padding: 20px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.sheet h2 { margin: 0; font-size: 1.125rem; }

.hint, .note { margin: 0; font-size: .875rem; line-height: 1.45; color: var(--muted); }
.note:empty { display: none; }
.note.warn { color: var(--bad); }

.discover { display: flex; flex-direction: column; gap: 10px; }
.found { display: flex; flex-direction: column; gap: 8px; margin: 0; padding: 0; list-style: none; }
.found-item {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  text-align: left;
}
.found-item strong { font-weight: 600; }
.found-item small { color: var(--muted); }
.found-empty { font-size: .875rem; line-height: 1.45; color: var(--muted); }

.field-row { display: grid; grid-template-columns: 1fr 104px; gap: 10px; }
label { display: flex; flex-direction: column; gap: 6px; font-size: .8125rem; font-weight: 600; }
input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--page);
  color: var(--ink);
  font: inherit;
  font-size: 1rem;
}
.field-error { margin: -6px 0 0; font-size: .8125rem; color: var(--bad); }
.field-error:empty { display: none; }

.sheet-actions { display: flex; justify-content: space-between; gap: 10px; margin-top: 4px; }
.btn {
  height: 46px;
  padding: 0 18px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--page);
  font-weight: 600;
}
.btn:disabled { opacity: .6; cursor: progress; }
.btn-primary { margin-left: auto; border-color: transparent; background: var(--claro); color: #fff; }
.btn-ghost { background: none; color: var(--muted); }

/* ---------- Aviso ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  z-index: 10;
  width: max-content;
  max-width: min(420px, calc(100% - 32px));
  padding: 12px 16px;
  border-radius: 14px;
  background: var(--ink);
  color: var(--page);
  font-size: .875rem;
  line-height: 1.4;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 16px);
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.bad { background: var(--bad); color: #fff; }

@media (prefers-reduced-motion: reduce) {
  *, *::before { transition: none !important; }
}

/* ---------- PWA ---------- */

[hidden] { display: none !important; }

.topbar-actions { display: flex; gap: 8px; }

/* No app instalado (standalone), a barra de status do iOS fica por cima. */
@media (display-mode: standalone) {
  .topbar { padding-top: 8px; }
}

/* ---------- Gerenciador de decodificadores ---------- */

.sheet { max-height: calc(100dvh - 40px); overflow-y: auto; }
.sheet h3 { margin: 0; font-size: .9375rem; }
.block { display: flex; flex-direction: column; gap: 10px; padding-top: 14px; border-top: 1px solid var(--line); }
.field-row.inline { grid-template-columns: 1fr auto; align-items: end; }
progress { width: 100%; height: 6px; accent-color: var(--claro); }

.devices { display: flex; flex-direction: column; gap: 8px; margin: 0; padding: 0; list-style: none; }
.device-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
}
.device-item.is-current { border-color: var(--claro); box-shadow: inset 0 0 0 1px var(--claro); }
.device-pick { display: flex; align-items: center; justify-content: space-between; gap: 10px; width: 100%; text-align: left; }
.device-pick strong { font-weight: 600; }
.device-pick small { color: var(--muted); }
.device-pick .badge { font-size: .75rem; font-weight: 600; color: var(--claro); }
.device-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.device-actions button {
  min-height: 36px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: .8125rem;
  font-weight: 600;
}
.device-actions .danger { color: var(--bad); }

.toast { display: flex; align-items: center; gap: 12px; }
.toast.has-action { pointer-events: auto; }
.toast-action {
  flex: none;
  min-height: 32px;
  padding: 0 12px;
  border-radius: 8px;
  background: rgba(255, 255, 255, .18);
  color: inherit;
  font-weight: 700;
  font-size: .8125rem;
}
