:root {
  color-scheme: light dark;
  /* Catppuccin Latte (light) and Mocha (dark); apps use the same palette. */
  --bg: #eff1f5;
  --fg: #4c4f69;
  --muted: #6c6f85;
  --card: #e6e9ef;
  --border: #ccd0da;
  --accent: #8839ef;
  --on-accent: #eff1f5;
  --error: #d20f39;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1e1e2e;
    --fg: #cdd6f4;
    --muted: #a6adc8;
    --card: #181825;
    --border: #313244;
    --accent: #cba6f7;
    --on-accent: #11111b;
    --error: #f38ba8;
  }
}
* { box-sizing: border-box; }
/* Author display rules (form, nav) would otherwise beat the hidden attribute. */
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  padding: max(16px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
}
header, main { max-width: 760px; margin: 0 auto; }
header { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
h1 { font-size: 1.5rem; margin: 8px 0 16px; }
h2 { font-size: 1.1rem; }
nav { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; color: var(--muted); font-size: 0.9rem; }
button, input {
  font: inherit;
  border-radius: 10px;
  border: 1px solid var(--border);
  padding: 10px 14px;
  min-height: 44px;
}
button { background: var(--accent); color: var(--on-accent); border-color: transparent; cursor: pointer; white-space: nowrap; font-weight: 600; }
button:hover { filter: brightness(1.08); }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.quiet { background: transparent; color: var(--muted); border-color: var(--border); min-height: 36px; padding: 6px 10px; font-weight: 400; }
button.secondary { background: transparent; color: var(--fg); border-color: var(--border); }
input { background: var(--bg); color: var(--fg); width: 100%; }
form { display: flex; gap: 8px; margin: 8px 0; }

/* Login: a centred card, with the page header out of the way. */
body:has(#login:not([hidden]), #device-wait:not([hidden])) header { display: none; }
body:has(.card:not([hidden])) main {
  min-height: calc(100dvh - 40px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.card {
  width: 100%;
  max-width: 400px;
  padding: 32px;
  border-radius: 20px;
  background: var(--card);
  border: 1px solid var(--border);
  box-shadow: 0 12px 40px rgb(0 0 0 / 0.12);
}
#login, #device-wait, #device-approve { display: flex; flex-direction: column; }
#device-wait[hidden], #device-approve[hidden], #approve-confirm[hidden] { display: none; }
#approve-confirm:not([hidden]) { display: flex; }
.card + .card { margin-top: 16px; }
#login .brand { border-radius: 12px; margin-bottom: 16px; }
#login h2 { font-size: 1.4rem; margin: 0 0 4px; }
.lede { color: var(--muted); margin: 0 0 24px; }
.stack { flex-direction: column; gap: 8px; margin: 0; }
.stack + .stack { margin-top: 16px; }
label { font-size: 0.85rem; font-weight: 600; }
.or {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 20px 0;
  color: var(--muted);
  font-size: 0.85rem;
}
.or::before, .or::after { content: ""; flex: 1; border-top: 1px solid var(--border); }
main > .card ~ #message { text-align: center; margin-top: 16px; max-width: 400px; }
@media (max-width: 480px) {
  .card { padding: 24px 20px; border-radius: 16px; }
}
.grid {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
}
.tile {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 16px;
  min-height: 140px;
  border-radius: 16px;
  background: var(--card);
  border: 1px solid var(--border);
  color: inherit;
  text-decoration: none;
}
.tile { transition: transform 0.12s ease; }
.tile:active { transform: scale(0.98); }
.tile:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; transform: scale(1.05); }

/* TV (html.tv, hub.js): D-pad only, read from ~3 m; content inside the 5% overscan margin (spec D4).
   Always dark (Catppuccin Mocha): TV guidelines advise against white backgrounds. */
html.tv {
  --bg: #1e1e2e;
  --fg: #cdd6f4;
  --muted: #a6adc8;
  --card: #181825;
  --border: #313244;
  --accent: #cba6f7;
  --on-accent: #11111b;
  --error: #f38ba8;
}
html.tv body { font-size: 20px; padding: 5vh 5vw; }
html.tv header, html.tv main { max-width: none; }
html.tv .grid { grid-template-columns: repeat(auto-fill, minmax(196px, 1fr)); gap: 24px; }
html.tv #add-passkey { display: none; }
/* On TV, focus set by the page (first tile, Cancel) must show too, not only focus from a key press. */
html.tv :is(a, button, input):focus { outline: 3px solid var(--accent); outline-offset: 3px; }
html.tv .tile:focus { transform: scale(1.05); }
.icon { font-size: 2rem; line-height: 1; height: 40px; display: flex; align-items: center; }
.icon img { width: 40px; height: 40px; border-radius: 10px; }
.tile small { color: var(--muted); }
#message { min-height: 1.5em; color: var(--muted); }
#message.error { color: var(--error); }
details { margin-top: 24px; color: var(--muted); font-size: 0.9rem; }

.qr { align-self: center; background: #fff; padding: 12px; border-radius: 12px; margin: 8px 0 16px; }
.qr svg { display: block; width: 200px; height: 200px; }
.user-code { font: 700 2rem/1.2 ui-monospace, Menlo, monospace; letter-spacing: 0.12em; text-align: center; margin: 0 0 8px; }
#approve-back { margin-top: 16px; align-self: center; }

/* Short landscape screens (a TV is about 960x540 CSS px): QR code beside the text, nothing scrolls off. */
@media (orientation: landscape) and (max-height: 640px) {
  #device-wait:not([hidden]) {
    max-width: 760px;
    display: grid;
    grid-template-columns: auto 1fr;
    column-gap: 32px;
    align-items: center;
  }
  #device-wait > :not(.qr) { grid-column: 2; }
  #device-wait .qr { grid-column: 1; grid-row: 1 / span 5; margin: 0; }
  #device-wait .user-code { text-align: left; }
}
