:root {
  --bg: #04080f;
  --panel: rgba(10, 26, 42, 0.72);
  --line: rgba(70, 212, 255, 0.22);
  --line-strong: rgba(70, 212, 255, 0.55);
  --cyan: #46d4ff;
  --cyan-dim: #2a8fb3;
  --listen: #7cf7c4;
  --text: #d8f3ff;
  --muted: #7fa6ba;
  --user: #0d2a3f;
  --ok: #4be3a0;
  --warn: #ffc857;
  --bad: #ff6b6b;
  --radius: 12px;
  --mono: "Cascadia Mono", "Consolas", "SF Mono", ui-monospace, monospace;
  --sans: "Segoe UI Variable", "Segoe UI", -apple-system, system-ui, sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --header-h: calc(56px + var(--safe-top));
  --dock-h: calc(116px + var(--safe-bottom));
  --avail: calc(100dvh - var(--header-h) - var(--dock-h));
  --rx-size: clamp(150px, 36vmin, 280px);       /* reactor size in voice view */
  --rx-y: calc(var(--avail) * 0.42);              /* its centre, measured from below the top bar */
  --ease: cubic-bezier(.2, .8, .2, 1);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }
body {
  margin: 0;
  background:
    radial-gradient(900px 700px at 50% 38%, rgba(70, 212, 255, 0.09), transparent 65%),
    radial-gradient(900px 600px at 110% 110%, rgba(70, 212, 255, 0.05), transparent 60%),
    var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--sans);
  -webkit-tap-highlight-color: transparent;
}
.scanlines {
  pointer-events: none; position: fixed; inset: 0; z-index: 0; opacity: .25;
  background: repeating-linear-gradient(0deg, rgba(255,255,255,.025) 0 1px, transparent 1px 3px);
}
.mono { font-family: var(--mono); font-size: 12px; word-break: break-all; }
.err { color: var(--bad); }

/* ---------- top bar ---------- */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 2; height: var(--header-h);
  display: flex; align-items: center; gap: 12px;
  padding: var(--safe-top) 14px 0 18px; border-bottom: 1px solid transparent;
  transition: border-color .4s, background .4s;
}
body.mode-chat .topbar { border-bottom-color: var(--line); background: rgba(5, 12, 21, .85); backdrop-filter: blur(8px); }
.brand { display: flex; align-items: center; transition: padding-left .45s var(--ease); }
body.mode-chat .brand { padding-left: 46px; }
.brand-name { font: 600 15px/1 var(--mono); letter-spacing: .32em; color: var(--cyan); text-shadow: 0 0 10px rgba(70,212,255,.6); }
.status-pill {
  margin-left: auto; display: inline-flex; align-items: center; gap: 8px; padding: 4px 12px; min-width: 0;
  border: 1px solid var(--line); border-radius: 999px; font: 12px var(--mono); color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.status-pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); box-shadow: 0 0 8px currentColor; flex: none; }
.status-pill[data-state="ready"] .dot { background: var(--ok); }
.status-pill[data-state="busy"] .dot { background: var(--cyan); animation: blink 1s infinite; }
.status-pill[data-state="error"] .dot { background: var(--bad); }
.menu-btn { width: 40px; height: 40px; padding: 0; display: grid; place-items: center; flex: none; }
.refresh-btn .update-label { display: none; }
.refresh-btn.has-update {
  width: auto; display: inline-flex; gap: 6px; padding: 0 12px; border-color: var(--listen); color: var(--listen);
  background: rgba(124,247,196,.1); animation: pulse 2s ease-in-out infinite;
}
.refresh-btn.has-update .update-label { display: inline; font-size: 13px; }
.voice-pick { display: flex; align-items: center; gap: 8px; font-size: 15px; }
.note { margin: 0 0 8px; font-size: 12px; color: var(--muted); }
.note:empty { display: none; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
#notifyOn { width: 100%; }
.voice-pick input[type="range"] { flex: 1; min-width: 0; accent-color: var(--cyan); }
.voice-pick select {
  flex: 1; min-width: 0; padding: 7px 8px; border-radius: 8px; border: 1px solid var(--line-strong);
  background: rgba(0,0,0,.4); color: var(--text); font: 15px var(--sans);
}

/* ---------- the arc reactor ---------- */
.reactor { position: relative; width: 64px; height: 64px; flex: none; --w: 2px; --level: 0; }
.reactor .ring { position: absolute; border-radius: 50%; border: var(--w) solid transparent; }
.reactor .r1 { inset: 0; border-top-color: var(--cyan); border-bottom-color: var(--cyan); animation: spin 7s linear infinite; }
.reactor .r2 { inset: 12%; border-left-color: var(--cyan-dim); border-right-color: var(--cyan-dim); animation: spin 5s linear infinite reverse; }
.reactor .r3 { inset: 24%; border: 1px dashed var(--line-strong); animation: spin 12s linear infinite; }
.reactor .r4 { inset: 4%; border: 1px solid rgba(70,212,255,.12); }
.reactor .core {
  position: absolute; inset: 36%; border-radius: 50%; background: var(--cyan);
  box-shadow: 0 0 calc(18px + var(--level) * 50px) var(--cyan), 0 0 calc(36px + var(--level) * 80px) rgba(70,212,255,.45);
  transform: scale(calc(1 + var(--level) * .55)); transition: transform .08s linear, background .3s;
  animation: breathe 4s ease-in-out infinite;
}
.reactor[data-state="thinking"] .r1 { animation-duration: 1.1s; }
.reactor[data-state="thinking"] .r2 { animation-duration: .8s; }
.reactor[data-state="thinking"] .r3 { animation-duration: 2s; }
.reactor[data-state="listening"] .core { background: var(--listen); animation: none;
  box-shadow: 0 0 calc(18px + var(--level) * 60px) var(--listen), 0 0 calc(40px + var(--level) * 90px) rgba(124,247,196,.4); }
.reactor[data-state="listening"] .r1 { border-top-color: var(--listen); border-bottom-color: var(--listen); }
.reactor[data-state="speaking"] .core { animation: none; }            /* glow follows the voice (--level) */
.reactor[data-state="speaking"] .r1 { animation-duration: 3s; }
.reactor[data-state="speaking-device"] .core { animation: pulse .45s ease-in-out infinite; }
.reactor[data-state="offline"] { filter: grayscale(1) opacity(.5); }

.reactor-main {
  position: fixed; z-index: 3; padding: 0; border: 0; background: none; cursor: pointer; border-radius: 50%;
  left: 50%; top: calc(var(--header-h) + var(--rx-y)); width: var(--rx-size); height: var(--rx-size);
  transform: translate(-50%, -50%); --w: 3px;
  transition: top .5s var(--ease), left .5s var(--ease), width .5s var(--ease), height .5s var(--ease);
}
.reactor-main:hover, .reactor-main:focus-visible { box-shadow: none; outline: none; background: none; }
.reactor-main:focus-visible .r4 { border-color: var(--line-strong); }
body.mode-chat .reactor-main { top: calc(var(--safe-top) + 28px); left: 36px; width: 40px; height: 40px; --w: 2px; }

/* ---------- stage: voice view / chat view + the dock ---------- */
.stage {
  position: fixed; z-index: 1; top: var(--header-h); left: 0; right: 0; bottom: 0;
  display: flex; flex-direction: column; min-height: 0;
}
.alerts {
  position: absolute; z-index: 4; top: 8px; left: 50%; transform: translateX(-50%);
  width: min(620px, calc(100% - 24px)); display: grid; gap: 8px;
}

.voice-view {
  flex: 1; min-height: 0; display: flex; flex-direction: column; align-items: center; gap: 10px;
  padding: calc(var(--rx-y) + var(--rx-size) / 2 + 22px) 20px 12px; text-align: center;
}
.voice-caption { font: 12px var(--mono); letter-spacing: .2em; text-transform: uppercase; color: var(--cyan-dim); }
.voice-heard { max-width: 640px; color: var(--muted); font-size: 15px; font-style: italic; }
.voice-heard:empty, .voice-reply:empty { display: none; }
.voice-reply {
  max-width: 680px; min-height: 0; overflow-y: auto; font-size: 18px; line-height: 1.55; padding: 0 4px;
  mask-image: linear-gradient(180deg, #000 85%, transparent);
}
.voice-reply ul { text-align: left; margin: 6px 0; padding-left: 20px; }
.voice-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; margin-top: auto; padding-top: 6px; }
.pill { border-radius: 999px; padding: 6px 14px; font-size: 13px; }
.pill[aria-pressed="true"] { border-color: var(--listen); color: var(--listen); background: rgba(124,247,196,.1); }

.chat-view { flex: 1; min-height: 0; display: flex; justify-content: center; }
body.mode-voice .chat-view, body.mode-chat .voice-view { display: none; }
body.mode-voice .voice-view, body.mode-chat .chat-view { animation: fadeIn .35s ease-out; }
.log { width: min(860px, 100%); overflow-y: auto; padding: 8px 18px 14px; scroll-behavior: smooth; -webkit-overflow-scrolling: touch; }
.msg { max-width: 78%; margin: 10px 0; padding: 10px 14px; border-radius: 12px; overflow-wrap: anywhere; }
.msg.user { margin-left: auto; background: var(--user); border: 1px solid rgba(70,212,255,.18); }
.msg.jarvis { border-left: 2px solid var(--cyan); background: rgba(70,212,255,.05); }
.msg.jarvis .who { font: 600 10px var(--mono); letter-spacing: .2em; color: var(--cyan); display: block; margin-bottom: 2px; }
.msg.notice { max-width: none; text-align: center; font-size: 12px; color: var(--muted); background: none; }
.msg .meta { display: block; margin-top: 4px; font: 11px var(--mono); color: var(--muted); }
.msg ul { margin: 6px 0; padding-left: 18px; }
.msg.pending .body::after { content: "▍"; animation: blink 1s infinite; color: var(--cyan); }
.attachment {
  display: inline-block; margin-top: 8px; padding: 6px 12px; border-radius: 8px; border: 1px solid var(--line-strong);
  color: var(--cyan); text-decoration: none; font-size: 13px; background: rgba(70,212,255,.08);
}

.dock { flex: none; width: min(880px, 100%); margin: 0 auto; padding-bottom: var(--safe-bottom); }
.chips { display: flex; gap: 6px; padding: 6px 14px 0; overflow-x: auto; scrollbar-width: none; justify-content: center; flex-wrap: wrap; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; font-size: 12px; padding: 6px 11px; border-radius: 999px; }
.composer { display: flex; gap: 10px; align-items: flex-end; padding: 10px 14px 12px; }
.composer textarea {
  flex: 1; resize: none; max-height: 160px; overflow-y: hidden; min-height: 44px; padding: 10px 12px; border-radius: 12px;
  background: rgba(0,0,0,.45); color: var(--text); border: 1px solid var(--line); font: inherit; outline: none;
}
.composer textarea:focus { border-color: var(--line-strong); box-shadow: 0 0 0 3px rgba(70,212,255,.12); }

button {
  font: inherit; color: var(--text); background: rgba(70,212,255,.12); border: 1px solid var(--line-strong);
  border-radius: 10px; padding: 8px 12px; cursor: pointer; transition: background .15s, box-shadow .15s;
}
button:hover { background: rgba(70,212,255,.22); box-shadow: 0 0 12px rgba(70,212,255,.25); }
button:disabled { opacity: .45; cursor: default; box-shadow: none; }
button.ghost { background: transparent; border-color: var(--line); color: var(--muted); }
button.small { padding: 3px 8px; font-size: 12px; }
.mic, .send { width: 44px; height: 44px; padding: 0; display: grid; place-items: center; flex: none; border-radius: 12px; }
.mic.recording { background: rgba(255,107,107,.25); border-color: var(--bad); color: #ffd0d0; animation: pulse 1s infinite; }

/* ---------- alerts ---------- */
.alert {
  display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: start;
  padding: 10px 12px; border-radius: 10px; border: 1px solid var(--bad);
  background: rgba(40, 10, 14, .92); box-shadow: 0 0 18px rgba(255,107,107,.18); animation: alertIn .3s ease-out;
}
.alert.info { border-color: var(--ok); background: rgba(6, 30, 24, .92); box-shadow: none; }
.alert .icon { color: var(--bad); font: 700 14px var(--mono); }
.alert.info .icon { color: var(--ok); }
.alert .title { font-weight: 600; }
.alert .msg { color: var(--muted); font-size: 13px; }
.alert .when { font: 11px var(--mono); color: var(--muted); }
.alert button { padding: 2px 8px; font-size: 12px; }

/* ---------- side drawer ---------- */
.side {
  position: fixed; z-index: 6; top: 0; right: 0; bottom: 0; width: min(380px, 88vw);
  display: flex; flex-direction: column; gap: 12px; overflow-y: auto;
  padding: calc(12px + var(--safe-top)) 12px calc(12px + var(--safe-bottom));
  background: #06101b; border-left: 1px solid var(--line);
  transform: translateX(105%); transition: transform .22s ease-out;
}
body.side-open .side { transform: none; }
.scrim { position: fixed; inset: 0; z-index: 5; background: rgba(0,0,0,.5); }
.scrim[hidden] { display: none; }
.side-head { display: flex; align-items: center; justify-content: space-between; font: 600 12px var(--mono); letter-spacing: .22em; text-transform: uppercase; color: var(--cyan); }
.close-side { width: 36px; height: 36px; padding: 0; }
.panel { flex: none; min-width: 0; overflow: hidden; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); padding: 12px 14px; }
.panel.grow { flex: 1 0 180px; min-height: 180px; display: flex; flex-direction: column; }
.panel h2 { margin: 0 0 10px; font: 600 11px var(--mono); letter-spacing: .22em; text-transform: uppercase; color: var(--cyan-dim); }
.toggles-m { display: grid; gap: 10px; }
.toggle { display: inline-flex; align-items: center; gap: 8px; font-size: 15px; color: var(--text); cursor: pointer; }
.toggle input { accent-color: var(--cyan); width: 18px; height: 18px; }
.systems, .devices { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.systems li, .devices li { display: flex; align-items: center; gap: 8px; font-size: 13px; min-width: 0; }
.systems .light { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); box-shadow: 0 0 6px currentColor; flex: none; }
.systems .light.ok { background: var(--ok); }
.systems .name { text-transform: capitalize; min-width: 64px; }
.devices .name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.systems .detail, .devices .detail { flex: 1; min-width: 0; color: var(--muted); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.remote-url { margin: 10px 0; font-size: 12px; color: var(--muted); }
.remote-url button { margin-top: 6px; }
#pairBtn { width: 100%; }
.quick { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.quick button { font-size: 12px; padding: 7px 8px; text-align: left; }
.activity { list-style: none; margin: 0; padding: 0; overflow-y: auto; font: 12px/1.4 var(--mono); }
.activity li { padding: 6px 0; border-bottom: 1px dashed rgba(70,212,255,.12); color: var(--muted); }
.activity li .tag { color: var(--cyan); }
.activity li.ok::before { content: "✓ "; color: var(--ok); }
.activity li.fail::before { content: "✗ "; color: var(--bad); }
.activity li.run::before { content: "◌ "; color: var(--cyan); }
.activity li .err { display: block; color: var(--bad); }

/* ---------- dialogs ---------- */
dialog.confirm {
  width: min(480px, calc(100vw - 32px)); padding: 0; border: 1px solid var(--warn); border-radius: 14px;
  background: #0b1520; color: var(--text); box-shadow: 0 0 40px rgba(255,200,87,.25);
}
dialog.confirm::backdrop { background: rgba(0,0,0,.6); backdrop-filter: blur(2px); }
dialog.confirm form { padding: 18px 20px; display: grid; gap: 12px; }
.confirm-head { display: flex; align-items: center; gap: 10px; }
.confirm-head h3 { margin: 0; font: 600 15px var(--mono); letter-spacing: .08em; }
.badge { font: 600 10px var(--mono); text-transform: uppercase; letter-spacing: .14em; padding: 3px 8px; border-radius: 999px; background: rgba(255,200,87,.15); color: var(--warn); border: 1px solid var(--warn); }
.badge.secure { color: var(--bad); border-color: var(--bad); background: rgba(255,107,107,.12); }
.confirm-summary { margin: 0; padding: 10px 12px; border-radius: 8px; background: rgba(0,0,0,.35); border: 1px solid var(--line); }
.field[hidden] { display: none; }
.field { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.field input { padding: 9px 10px; border-radius: 8px; border: 1px solid var(--line-strong); background: rgba(0,0,0,.4); color: var(--text); font: 16px var(--mono); letter-spacing: .1em; }
.confirm-actions { display: flex; justify-content: flex-end; gap: 8px; }
#confirmOk { border-color: var(--warn); background: rgba(255,200,87,.15); }
dialog.pair-dialog { border-color: var(--cyan); box-shadow: 0 0 40px rgba(70,212,255,.25); }
.pair-steps { margin: 0; padding-left: 20px; font-size: 13px; color: var(--muted); display: grid; gap: 6px; }
.pair-code { text-align: center; font: 700 40px/1.2 var(--mono); letter-spacing: .2em; color: var(--cyan); text-shadow: 0 0 14px rgba(70,212,255,.6); }
.pair-qr { justify-self: center; width: 180px; height: 180px; background: #fff; padding: 8px; border-radius: 8px; }
.pair-qr[hidden] { display: none; }
.pair-expiry { margin: 0; text-align: center; font: 12px var(--mono); color: var(--muted); }

/* ---------- pairing page (unpaired devices) ---------- */
html:has(body.pair-page), body.pair-page { overflow: auto; }
body.pair-page { display: grid; place-items: center; padding: calc(24px + var(--safe-top)) 16px calc(24px + var(--safe-bottom)); }
.pair-card {
  position: relative; z-index: 1; width: min(420px, 100%); display: grid; gap: 14px; justify-items: center; text-align: center;
  padding: 28px 22px; border: 1px solid var(--line); border-radius: 16px; background: var(--panel);
}
.pair-card .reactor { width: 96px; height: 96px; }
.pair-card h1 { margin: 0; font-size: 20px; }
.pair-card form { width: 100%; display: grid; gap: 12px; text-align: left; }
.pair-card #code { font-size: 28px; text-align: center; letter-spacing: .3em; }
.pair-lead { margin: 0; font-size: 16px; }
.pair-help, .pair-foot { margin: 0; font-size: 13px; color: var(--muted); }
.pair-error { margin: 0; min-height: 1.2em; color: var(--bad); font-size: 13px; text-align: center; }
#pairSubmit { width: 100%; padding: 12px; font-size: 16px; }

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 50% { transform: scale(.82); opacity: .75; } }
@keyframes breathe { 50% { filter: brightness(1.25); } }
@keyframes blink { 50% { opacity: 0; } }
@keyframes alertIn { from { transform: translateY(-6px); opacity: 0; } }
@keyframes fadeIn { from { opacity: 0; } }

/* ---------- phones ---------- */
@media (max-width: 700px) {
  :root { --dock-h: calc(108px + var(--safe-bottom)); }
  .brand-name { font-size: 13px; letter-spacing: .24em; }
  .status-pill { padding: 3px 10px; }
  .chips { flex-wrap: nowrap; justify-content: flex-start; padding: 6px 12px 0; }
  .composer { padding: 8px 12px 10px; }
  .composer textarea { font-size: 16px; }  /* 16px stops iPhone zooming into the field */
  .log { padding: 4px 12px 12px; }
  .msg { max-width: 92%; }
  .voice-reply { font-size: 17px; }
}
@media (prefers-reduced-motion: reduce) {
  .reactor *, .reactor-main, .side, .brand { animation: none !important; transition: none !important; }
}
