:root {
  --bg: #f4f1ea;
  --sheet: #fffdf8;
  --ink: #1d2026;
  --muted: #6b6f78;
  --line: #ddd6c8;
  --accent: #c9821a;
  --accent-ink: #1d1404;
  --own: #1f6f5c;
  --own-ink: #f2fbf7;
  --bubble: #ffffff;
  --danger: #b8402f;
  --ok: #2f9e57;
  --shadow: 0 1px 2px #0000000f, 0 6px 18px #0000000d;
  --radius: 16px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --sheet: #1c2027;
    --ink: #eef0f3;
    --muted: #9aa1ad;
    --line: #2d333d;
    --accent: #f2b544;
    --accent-ink: #1d1404;
    --own: #2f8f76;
    --own-ink: #f2fbf7;
    --bubble: #232831;
    --danger: #e0604a;
    --ok: #5fd17f;
    --shadow: 0 1px 2px #00000040, 0 8px 24px #00000030;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

main { max-width: 34rem; margin: 0 auto; min-height: 100dvh; }
.screen { padding: max(16px, env(safe-area-inset-top)) 16px max(20px, env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 14px; }

/* ------------------------------------------------------------------ home */
.hero { text-align: center; padding: 6vh 0 2vh; }
.hero h1 { margin: 12px 0 6px; font-size: 2rem; letter-spacing: -0.02em; }
.lede { margin: 0 auto; max-width: 24rem; color: var(--muted); }
.logo { display: inline-flex; gap: 6px; align-items: center; height: 56px; padding: 0 18px; border-radius: 18px; background: var(--accent); box-shadow: var(--shadow); }
.logo i { display: block; width: 7px; border-radius: 4px; background: var(--accent-ink); animation: wave 1.2s ease-in-out infinite; }
.logo i:nth-child(1) { height: 14px; }
.logo i:nth-child(2) { height: 30px; animation-delay: .15s; }
.logo i:nth-child(3) { height: 20px; animation-delay: .3s; }
@keyframes wave { 50% { transform: scaleY(.55); } }

h2 { margin: 4px 0 0; font-size: 1.45rem; letter-spacing: -0.01em; }
.field { display: grid; gap: 6px; }
.field > span { font-weight: 600; font-size: .92rem; }
.field small { color: var(--muted); }
.field.inline { grid-template-columns: 1fr auto; align-items: center; }
input:not([type]), input[type="text"] {
  width: 100%; min-height: 48px; padding: 10px 14px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--sheet); outline: none; transition: border-color .15s, box-shadow .15s;
}
input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 30%, transparent); }
.actions { display: grid; gap: 10px; margin-top: 6px; }
.big { min-height: 54px; border-radius: 14px; font-weight: 650; font-size: 1.05rem; }
.primary { border: 0; background: var(--accent); color: var(--accent-ink); box-shadow: var(--shadow); }
.primary:disabled { opacity: .45; cursor: default; box-shadow: none; }
.secondary { border: 1px solid var(--line); background: var(--sheet); }
.primary:not(:disabled):active, .secondary:active { transform: translateY(1px); }
.hint { color: var(--muted); font-size: .9rem; margin: 0; }
.error { color: var(--danger); font-weight: 600; margin: 0; }
.back, .leave { align-self: flex-start; border: 0; background: none; color: var(--muted); padding: 6px 0; font-weight: 600; }
.back::before { content: "‹ "; }

.stepper { display: inline-grid; grid-template-columns: 48px 56px 48px; align-items: center; border: 1px solid var(--line); border-radius: 12px; background: var(--sheet); overflow: hidden; width: max-content; }
.stepper button { height: 46px; border: 0; background: none; font-size: 1.3rem; }
.stepper button:disabled { opacity: .3; }
.stepper output { text-align: center; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------- scan / lobby */
.listening { display: flex; gap: 10px; align-items: center; color: var(--muted); margin: 0; }
.pulse { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); animation: pulse 1.4s ease-out infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 60%, transparent); } 100% { box-shadow: 0 0 0 14px transparent; } }
.rooms, .people { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.rooms li { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; align-items: center; padding: 12px 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--sheet); box-shadow: var(--shadow); animation: rise .25s ease-out; }
.rooms li.stale { opacity: .5; }
.rooms strong { font-size: 1.1rem; }
.rooms small { color: var(--muted); grid-column: 1; }
.rooms button { grid-row: 1 / span 2; grid-column: 2; min-height: 44px; padding: 0 18px; border-radius: 12px; }
.rooms .empty { border-style: dashed; box-shadow: none; color: var(--muted); display: block; text-align: center; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }

.status { margin: 0; color: var(--muted); }
.key { display: grid; justify-items: center; gap: 2px; padding: 14px; border-radius: var(--radius); background: var(--sheet); border: 1px solid var(--line); text-align: center; }
.key span { font-size: .85rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .06em; }
.key strong { font: 700 2rem/1.1 ui-monospace, "SF Mono", Menlo, Consolas, monospace; letter-spacing: .08em; }
.key small { color: var(--muted); }
.people li { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: 12px; background: var(--sheet); border: 1px solid var(--line); animation: rise .25s ease-out; }
.people .avatar { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; font-size: .9rem; color: #fff; background: hsl(var(--hue) 55% 45%); flex: none; }
.people .badge { margin-left: auto; font-size: .78rem; color: var(--muted); }
.people li.quiet { opacity: .55; }
.people li.waiting { border-style: dashed; color: var(--muted); }
.asking { margin: 0; padding: 8px 12px; border-radius: 12px; background: color-mix(in srgb, var(--accent) 18%, transparent); font-weight: 600; }
.host-tools { display: grid; gap: 12px; }

/* ---------------------------------------------------------------- signal */
.signal-line { position: fixed; inset: 0 0 auto 0; height: 3px; z-index: 10; pointer-events: none;
  background: linear-gradient(90deg, hsl(var(--hue, 45) 70% 50%) calc(var(--level, .04) * 100%), color-mix(in srgb, hsl(var(--hue, 45) 70% 50%) 25%, transparent) 0); transition: background .2s; }
.signal-line[data-tx="true"] { box-shadow: 0 0 8px hsl(var(--hue, 45) 80% 60%); }
.signal-panel { display: grid; gap: 6px; padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--radius); background: color-mix(in srgb, var(--sheet) 70%, var(--bg)); margin-top: auto; }
.sig-row { display: flex; justify-content: space-between; gap: 8px; font-size: .9rem; }
.sig-row.small { font-size: .78rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.sig-label { font-weight: 600; }
.sig-value { font-weight: 700; }
.signal-panel[data-state="good"] .sig-value { color: var(--ok); }
.signal-panel[data-state="poor"] .sig-value, .signal-panel[data-state="silent"] .sig-value { color: var(--danger); }
.sig-tip { margin: 0; font-size: .85rem; color: var(--danger); }
.quality-strip { position: relative; display: flex; width: 100%; height: 6px; overflow: hidden; border-radius: 999px;
  background: repeating-linear-gradient(-45deg, color-mix(in srgb, var(--ink) 14%, transparent) 0 6px, color-mix(in srgb, var(--ink) 6%, transparent) 6px 12px); }
.quality-strip.is-wide { height: 9px; }
.quality-strip .q-good { display: block; height: 100%; background: linear-gradient(90deg, #2f9e57, #5fd17f); transition: width .3s ease; }
.quality-strip .q-bad { display: block; height: 100%; background: linear-gradient(90deg, #c8402f, #e0604a); transition: width .3s ease; }
.quality-strip .q-mic { position: absolute; top: 0; bottom: 0; width: 2px; margin-left: -1px; background: #fff; box-shadow: 0 0 4px #fff; transition: left .1s linear; }
.quality-strip[data-state="silent"] { background: var(--danger); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: .55; } }

/* ------------------------------------------------------------------ chat */
.screen.chat { height: 100dvh; padding: 0; gap: 0; display: flex; flex-direction: column; }
.screen.chat > * { flex: none; }
.screen.chat > .log { flex: 1 1 auto; min-height: 0; }
.chat-head { display: grid; grid-template-columns: 1fr auto; grid-template-areas: "title leave" "q q"; gap: 6px 10px; align-items: center;
  padding: max(10px, env(safe-area-inset-top)) 16px 8px; background: var(--sheet); border-bottom: 1px solid var(--line); }
.chat-title { grid-area: title; display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.chat-title h2 { font-size: 1.15rem; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.people-chip { border: 1px solid var(--line); background: var(--bg); border-radius: 999px; padding: 2px 10px; font-size: .82rem; color: var(--muted); white-space: nowrap; }
.chat-head .leave { grid-area: leave; padding: 4px 0; }
.chat-head .quality-strip { grid-area: q; }
.roster-pop { padding: 10px 16px; background: var(--sheet); border-bottom: 1px solid var(--line); grid-template-columns: repeat(auto-fill, minmax(9rem, 1fr)); }
.log { list-style: none; margin: 0; padding: 12px 12px 8px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; scroll-behavior: smooth; overscroll-behavior: contain; }
.msg { display: grid; justify-items: start; max-width: 84%; animation: rise .2s ease-out; }
.msg .who { font-size: .76rem; font-weight: 700; color: hsl(var(--hue) 55% 42%); margin: 0 0 2px 10px; }
@media (prefers-color-scheme: dark) { .msg .who { color: hsl(var(--hue) 65% 65%); } }
.msg .bubble { padding: 8px 12px; border-radius: 18px 18px 18px 6px; background: var(--bubble); box-shadow: var(--shadow); overflow-wrap: anywhere; }
.msg.own { align-self: flex-end; justify-items: end; }
.msg.own .bubble { background: var(--own); color: var(--own-ink); border-radius: 18px 18px 6px 18px; }
.msg.pending .bubble { opacity: .7; box-shadow: none; border: 1px dashed color-mix(in srgb, var(--ink) 25%, transparent); }
.msg.own.pending .bubble { background: color-mix(in srgb, var(--own) 70%, var(--bg)); }
.msg .state { font-size: .72rem; color: var(--muted); margin: 2px 8px 0; }
.msg.failed .state { color: var(--danger); font-weight: 700; }
.msg.failed .bubble { cursor: pointer; }
.sys { align-self: center; font-size: .8rem; color: var(--muted); padding: 2px 10px; border-radius: 999px; background: color-mix(in srgb, var(--ink) 6%, transparent); }
.gap { align-self: center; font-size: .8rem; color: var(--muted); letter-spacing: .3em; }
.gap.lost { letter-spacing: normal; color: var(--danger); }

.composer { display: grid; gap: 4px; padding: 8px 12px max(10px, env(safe-area-inset-bottom)); background: var(--sheet); border-top: 1px solid var(--line); }
.compose-row { display: grid; grid-template-columns: 1fr 48px; gap: 8px; }
.compose-row input { border-radius: 24px; }
.send { width: 48px; height: 48px; border-radius: 50%; border: 0; background: var(--accent); display: grid; place-items: center; transition: transform .15s, opacity .15s; }
.send svg { width: 22px; height: 22px; fill: var(--accent-ink); }
.send:disabled { opacity: .35; cursor: default; }
.send:not(:disabled):active { transform: scale(.92); }
.meter { display: flex; justify-content: space-between; font-size: .75rem; color: var(--muted); padding: 0 14px; font-variant-numeric: tabular-nums; }
.meter .full { color: var(--danger); font-weight: 700; }
.air { display: flex; align-items: center; gap: 6px; font-size: .78rem; color: var(--muted); padding: 0 6px; min-height: 1.1em; }
.air .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line); }
.air[data-state="tx"] .dot { background: var(--accent); animation: pulse 1s ease-out infinite; }
.air[data-state="rx"] .dot { background: var(--ok); animation: pulse 1s ease-out infinite; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
@media (min-width: 700px) {
  .screen.chat { border-inline: 1px solid var(--line); }
}
