/* Jarvis — founder interface. Dark, restrained, mobile-first. No inline styles (CSP).
 * Every orb animation is keyed by [data-state]; listening also reads --level (real mic
 * amplitude). Reduced motion (system preference or the in-app toggle) removes continuous
 * motion; state is always also shown as text. */

:root {
  --bg: #0b0d10;
  --bg-elev: #12151a;
  --bg-elev-2: #181c23;
  --line: #232833;
  --text: #e8eaee;
  --text-dim: #9aa3b2;
  --text-faint: #6b7381;
  --accent: #f2b34c;
  --accent-soft: rgba(242, 179, 76, .18);
  --accent-ink: #1a1305;
  --user: #1f2937;
  --danger: #ff6b6b;
  --ok: #4cd08d;
  --cool: #7fb6ff;
  --radius: 16px;
  --radius-sm: 10px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Inter, system-ui, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --level: 0;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
html { -webkit-text-size-adjust: 100%; overflow-x: hidden; background: var(--bg); }
body {
  margin: 0; background: var(--bg); color: var(--text); font-family: var(--font);
  font-size: 16px; line-height: 1.45; -webkit-font-smoothing: antialiased;
  overflow-x: hidden; overscroll-behavior: none;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
.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; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

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

/* ---------- login ---------- */
.view-login { align-items: center; justify-content: center; padding: calc(24px + var(--safe-top)) 20px calc(24px + var(--safe-bottom)); }
.login-card { width: 100%; max-width: 380px; }
.brand { text-align: center; margin-bottom: 28px; }
.brand-ring { display: inline-block; width: 44px; height: 44px; border-radius: 50%; border: 6px solid var(--accent); margin-bottom: 14px; }
.brand-name { margin: 0; font-size: 28px; font-weight: 600; letter-spacing: .2px; }
.brand-sub { margin: 4px 0 0; color: var(--text-dim); font-size: 14px; }
.login-form { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 8px; }
.field-label { font-size: 13px; color: var(--text-dim); }
.input { width: 100%; min-height: 52px; padding: 14px 16px; background: var(--bg-elev); border: 1px solid var(--line); border-radius: var(--radius-sm); outline: none; font-size: 17px; }
.input:focus { border-color: var(--accent); }
.login-foot { margin-top: 22px; text-align: center; color: var(--text-faint); font-size: 12px; }

/* ---------- buttons ---------- */
.button {
  min-height: 48px; padding: 12px 18px; border-radius: var(--radius-sm); border: 1px solid transparent;
  font-weight: 600; font-size: 16px; display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  -webkit-tap-highlight-color: transparent; transition: transform .06s ease, opacity .15s ease, background .15s ease;
}
.button:active { transform: scale(.985); }
.button:disabled { opacity: .5; cursor: default; }
.button-primary { background: var(--accent); color: var(--accent-ink); }
.button-secondary { background: var(--bg-elev-2); border-color: var(--line); color: var(--text); }
.button-ghost { background: transparent; border-color: var(--line); color: var(--text-dim); }
.button-small { min-height: 40px; padding: 8px 14px; font-size: 14px; }
.button-send { width: 48px; height: 48px; padding: 0; border-radius: 50%; background: var(--accent); color: var(--accent-ink); flex: 0 0 auto; }
.button-mic { width: 52px; height: 52px; padding: 0; border-radius: 50%; background: var(--bg-elev-2); border-color: var(--line); color: var(--text); flex: 0 0 auto; position: relative; }
.button-mic[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.button-mic[aria-pressed="true"]::after { content: ""; position: absolute; inset: -6px; border-radius: 50%; border: 2px solid var(--accent); opacity: calc(.25 + var(--level) * .75); transform: scale(calc(1 + var(--level) * .25)); }
.button-mic:disabled { opacity: .45; }

.icon-button { width: 44px; height: 44px; border-radius: 50%; border: none; background: transparent; color: var(--text); display: inline-flex; align-items: center; justify-content: center; -webkit-tap-highlight-color: transparent; }
.icon-button:active { background: var(--bg-elev-2); }
.icon-lines { width: 18px; height: 2px; background: currentColor; position: relative; border-radius: 2px; }
.icon-lines::before, .icon-lines::after { content: ""; position: absolute; left: 0; width: 18px; height: 2px; background: currentColor; border-radius: 2px; }
.icon-lines::before { top: -6px; } .icon-lines::after { top: 6px; }
.icon-plus { width: 16px; height: 16px; position: relative; }
.icon-plus::before, .icon-plus::after { content: ""; position: absolute; background: currentColor; border-radius: 2px; }
.icon-plus::before { left: 7px; top: 0; width: 2px; height: 16px; }
.icon-plus::after { top: 7px; left: 0; width: 16px; height: 2px; }
.icon-close { width: 16px; height: 16px; position: relative; }
.icon-close::before, .icon-close::after { content: ""; position: absolute; left: 7px; top: 0; width: 2px; height: 16px; background: currentColor; border-radius: 2px; }
.icon-close::before { transform: rotate(45deg); } .icon-close::after { transform: rotate(-45deg); }
.icon-arrow { width: 12px; height: 12px; border-top: 3px solid currentColor; border-right: 3px solid currentColor; transform: translateY(2px) rotate(-45deg); }
.icon-mic { width: 12px; height: 18px; border-radius: 6px; background: currentColor; position: relative; display: block; }
.icon-mic::before { content: ""; position: absolute; left: -5px; right: -5px; bottom: -6px; height: 12px; border: 2px solid currentColor; border-top: none; border-radius: 0 0 12px 12px; }
.icon-mic::after { content: ""; position: absolute; left: 4px; bottom: -11px; width: 4px; height: 5px; background: currentColor; }

/* ---------- chat ---------- */
.view-chat { height: 100vh; height: 100dvh; }
.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: calc(8px + var(--safe-top)) calc(8px + var(--safe-right)) 6px calc(8px + var(--safe-left));
  background: rgba(11, 13, 16, .92); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  position: sticky; top: 0; z-index: 5;
}
.topbar-title { flex: 1; display: flex; flex-direction: column; align-items: center; min-width: 0; }
.topbar-name { font-weight: 600; font-size: 17px; letter-spacing: .2px; }
.topbar-status { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-dim); }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); transition: background .2s ease; }
html[data-state="listening"] .status-dot, html[data-state="transcribing"] .status-dot { background: var(--accent); }
html[data-state="sending"] .status-dot, html[data-state="thinking"] .status-dot { background: var(--cool); }
html[data-state="answering"] .status-dot { background: var(--accent); }
html[data-state="error"] .status-dot { background: var(--danger); }

/* ---------- the core ---------- */
.stage { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 4px 16px 8px; }
.orb { position: relative; width: 132px; height: 132px; flex: 0 0 auto; }
.orb-svg { width: 100%; height: 100%; display: block; overflow: visible; }
.orb-glow {
  position: absolute; inset: 14%; border-radius: 50%;
  background: radial-gradient(circle, rgba(242, 179, 76, .55) 0%, rgba(242, 179, 76, .12) 45%, rgba(242, 179, 76, 0) 70%);
  opacity: .55; filter: blur(8px); transform: scale(1); will-change: transform, opacity;
  transition: opacity .3s ease;
}
.ring { fill: none; stroke: var(--accent); stroke-width: 1.5; opacity: .28; transform-origin: 100px 100px; transform-box: view-box; }
.ring-outer { stroke-dasharray: 6 10; }
.ring-mid { stroke-dasharray: 40 14; opacity: .22; }
.ring-inner { stroke-width: 2; opacity: .35; }
.core { fill: var(--accent); transform-origin: 100px 100px; transform-box: view-box; will-change: transform; transition: fill .3s ease; }

/* idle: slow breathing */
.orb[data-state="idle"] .core { animation: breathe 4.2s ease-in-out infinite; }
.orb[data-state="idle"] .orb-glow { animation: glow-breathe 4.2s ease-in-out infinite; }
.orb[data-state="idle"] .ring-outer { animation: spin 90s linear infinite; }

/* listening: real amplitude drives scale and glow; outer ring turns slowly */
.orb[data-state="listening"] .core { transform: scale(calc(1 + var(--level) * .38)); transition: transform 60ms linear; }
.orb[data-state="listening"] .orb-glow { opacity: calc(.45 + var(--level) * .55); transform: scale(calc(1 + var(--level) * .5)); transition: transform 60ms linear, opacity 60ms linear; }
.orb[data-state="listening"] .ring-inner { opacity: calc(.35 + var(--level) * .5); stroke-width: calc(2px + var(--level) * 3px); }
.orb[data-state="listening"] .ring-outer { animation: spin 30s linear infinite; }

/* transcribing: small controlled processing motion */
.orb[data-state="transcribing"] .ring-mid { animation: spin 3s linear infinite; opacity: .5; }
.orb[data-state="transcribing"] .core { transform: scale(.96); }

/* sending: one brief directional transition */
.orb[data-state="sending"] .core { animation: lift .35s ease-out 1; }
.orb[data-state="sending"] .ring-inner { animation: expand .45s ease-out 1; }

/* thinking: calm orbital motion while the request is genuinely in flight */
.orb[data-state="thinking"] .ring-outer { animation: spin 7s linear infinite; opacity: .5; }
.orb[data-state="thinking"] .ring-mid { animation: spin-reverse 11s linear infinite; opacity: .4; }
.orb[data-state="thinking"] .core { animation: think 1.8s ease-in-out infinite; }
.orb[data-state="thinking"] .orb-glow { opacity: .7; animation: glow-breathe 1.8s ease-in-out infinite; }

/* answering: reveal, or a tighter pulse while speech is actually playing */
.orb[data-state="answering"] .core { animation: reveal .7s ease-out 1; }
.orb[data-state="answering"][data-speaking="true"] .core { animation: speak .9s ease-in-out infinite; }
.orb[data-state="answering"][data-speaking="true"] .ring-inner { animation: speak-ring .9s ease-in-out infinite; }

/* error: clear, still, non-alarming */
.orb[data-state="error"] .core { fill: var(--danger); transform: scale(.94); }
.orb[data-state="error"] .ring { stroke: var(--danger); opacity: .3; }
.orb[data-state="error"] .orb-glow { opacity: .25; background: radial-gradient(circle, rgba(255, 107, 107, .5) 0%, rgba(255, 107, 107, 0) 70%); }

.orb.paused .core, .orb.paused .ring, .orb.paused .orb-glow { animation-play-state: paused; }

@keyframes breathe { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.05); } }
@keyframes glow-breathe { 0%, 100% { opacity: .45; transform: scale(.98); } 50% { opacity: .7; transform: scale(1.06); } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes spin-reverse { to { transform: rotate(-360deg); } }
@keyframes lift { 0% { transform: translateY(0) scale(1); } 50% { transform: translateY(-8px) scale(1.06); } 100% { transform: translateY(0) scale(1); } }
@keyframes expand { 0% { transform: scale(1); opacity: .35; } 100% { transform: scale(1.35); opacity: 0; } }
@keyframes think { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.1); } }
@keyframes reveal { 0% { transform: scale(.85); } 60% { transform: scale(1.08); } 100% { transform: scale(1); } }
@keyframes speak { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.12); } }
@keyframes speak-ring { 0%, 100% { opacity: .35; } 50% { opacity: .8; } }

/* reduced motion: state by colour and text only */
html[data-motion="reduced"] .orb .core, html[data-motion="reduced"] .orb .ring, html[data-motion="reduced"] .orb .orb-glow { animation: none !important; transition: none !important; }
html[data-motion="reduced"] .orb[data-state="listening"] .core { transform: scale(1.08); }
html[data-motion="reduced"] .orb[data-state="thinking"] .core { fill: var(--cool); }
html[data-motion="reduced"] .status-dot.busy, html[data-motion="reduced"] .working-pulse { animation: none; }
@media (prefers-reduced-motion: reduce) {
  html[data-motion="auto"] .orb .core, html[data-motion="auto"] .orb .ring, html[data-motion="auto"] .orb .orb-glow { animation: none !important; transition: none !important; }
}

.state-label { margin: 0; font-size: 15px; font-weight: 600; letter-spacing: .3px; color: var(--text); min-height: 22px; }
html[data-state="listening"] .state-label, html[data-state="answering"] .state-label { color: var(--accent); }
html[data-state="thinking"] .state-label, html[data-state="sending"] .state-label { color: var(--cool); }
html[data-state="error"] .state-label { color: var(--danger); }
.state-hint { margin: 0; font-size: 13px; color: var(--text-dim); text-align: center; max-width: 34ch; font-variant-numeric: tabular-nums; }

/* ---------- messages ---------- */
.messages {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  padding: 6px calc(14px + var(--safe-right)) 14px calc(14px + var(--safe-left));
  display: flex; flex-direction: column; gap: 12px;
}
.empty-state { margin: auto; text-align: center; color: var(--text-dim); padding: 16px 24px; }
.empty-title { margin: 0 0 6px; font-size: 18px; font-weight: 600; color: var(--text); }
.empty-sub { margin: 0; font-size: 14px; }

.msg { max-width: 92%; display: flex; flex-direction: column; gap: 6px; }
.msg-user { align-self: flex-end; align-items: flex-end; }
.msg-jarvis { align-self: flex-start; align-items: flex-start; max-width: 96%; }
.bubble { padding: 12px 14px; border-radius: var(--radius); font-size: 16px; line-height: 1.5; overflow-wrap: anywhere; word-break: break-word; max-width: 100%; }
.msg-user .bubble { background: var(--user); border-bottom-right-radius: 6px; white-space: pre-wrap; }
.msg-jarvis .bubble { background: var(--bg-elev); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.bubble.collapsed { max-height: 9.5em; overflow: hidden; position: relative; }
.bubble.collapsed::after { content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 3.5em; background: linear-gradient(rgba(18, 21, 26, 0), var(--bg-elev)); pointer-events: none; }
.bubble p { margin: 0 0 .7em; } .bubble p:last-child { margin-bottom: 0; }
.bubble h1, .bubble h2, .bubble h3 { margin: .9em 0 .4em; font-size: 1.02em; font-weight: 700; }
.bubble h1:first-child, .bubble h2:first-child, .bubble h3:first-child { margin-top: 0; }
.bubble ul, .bubble ol { margin: .3em 0 .8em; padding-left: 1.3em; }
.bubble li { margin: .2em 0; }
.bubble code { font-family: var(--mono); font-size: .9em; background: var(--bg-elev-2); padding: 1px 5px; border-radius: 5px; }
.bubble pre { background: var(--bg-elev-2); padding: 10px 12px; border-radius: var(--radius-sm); overflow-x: auto; max-width: 100%; }
.bubble pre code { background: transparent; padding: 0; }
.bubble blockquote { margin: .4em 0; padding: 4px 12px; border-left: 3px solid var(--accent); color: var(--text-dim); }
.msg-tools { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; padding: 0 4px; }
.msg-tools .button-small { min-height: 36px; padding: 6px 12px; font-size: 13px; }
.pill { display: inline-flex; align-items: center; padding: 3px 9px; border-radius: 999px; font-size: 12px; background: var(--bg-elev-2); border: 1px solid var(--line); color: var(--text-dim); }
.pill-warn { border-color: rgba(242, 179, 76, .5); color: var(--accent); }
.pill-alert { border-color: rgba(255, 107, 107, .5); color: var(--danger); }
.pill-note { border-color: rgba(127, 182, 255, .45); color: var(--cool); }

.details { align-self: stretch; }
.details summary { list-style: none; cursor: pointer; font-size: 12px; color: var(--text-dim); padding: 6px 8px; border-radius: 8px; display: inline-flex; align-items: center; gap: 6px; -webkit-tap-highlight-color: transparent; }
.details summary::-webkit-details-marker { display: none; }
.details summary::before { content: "▸"; font-size: 11px; transition: transform .15s ease; }
.details[open] summary::before { transform: rotate(90deg); }
.details-body { font-size: 13px; color: var(--text-dim); padding: 4px 8px 6px; display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; }
.details-body dt { color: var(--text-faint); }
.details-body dd { margin: 0; overflow-wrap: anywhere; }
.details-full { grid-column: 1 / -1; margin-top: 6px; }
.details-full summary { padding-left: 0; }
.details-full .bubble { margin-top: 6px; font-size: 14px; }

.notice { margin: 0; padding: 10px 12px; border-radius: var(--radius-sm); font-size: 14px; }
.notice-error { background: rgba(255, 107, 107, .08); border: 1px solid rgba(255, 107, 107, .35); color: var(--danger); }
.notice-info { background: rgba(242, 179, 76, .08); border: 1px solid rgba(242, 179, 76, .35); color: var(--text); }
.notice-actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }

.interim { margin: 0 calc(14px + var(--safe-right)) 4px calc(14px + var(--safe-left)); padding: 8px 12px; font-size: 14px; color: var(--text-dim); font-style: italic; border-left: 2px solid var(--accent); }

/* ---------- composer ---------- */
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px calc(10px + var(--safe-right)) calc(10px + var(--safe-bottom)) calc(10px + var(--safe-left));
  border-top: 1px solid var(--line); background: var(--bg);
}
.composer-input { flex: 1; min-height: 48px; max-height: 160px; resize: none; padding: 13px 16px; background: var(--bg-elev); border: 1px solid var(--line); border-radius: 24px; outline: none; font-size: 17px; line-height: 1.35; }
.composer-input:focus { border-color: var(--accent); }
.composer-input:disabled { opacity: .6; }

/* ---------- drawer ---------- */
.drawer { position: fixed; inset: 0; z-index: 20; }
.drawer-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .55); }
.drawer-panel { position: absolute; top: 0; bottom: 0; left: 0; width: min(86vw, 360px); background: var(--bg-elev); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: calc(8px + var(--safe-top)) 0 calc(8px + var(--safe-bottom)) var(--safe-left); animation: slide-in .22s ease-out; }
@keyframes slide-in { from { transform: translateX(-24px); opacity: .6; } to { transform: none; opacity: 1; } }
html[data-motion="reduced"] .drawer-panel { animation: none; }
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 8px 4px 18px; }
.drawer-title { font-weight: 600; font-size: 17px; }
.drawer-new { margin: 6px 14px 10px; }
.session-list { list-style: none; margin: 0; padding: 0 8px; overflow-y: auto; flex: 1; }
.session-item { width: 100%; text-align: left; background: transparent; border: none; border-left: 3px solid transparent; border-radius: var(--radius-sm); padding: 12px 10px; display: flex; flex-direction: column; gap: 3px; -webkit-tap-highlight-color: transparent; }
.session-item:active { background: var(--bg-elev-2); }
.session-item.active { background: var(--bg-elev-2); border-left-color: var(--accent); }
.session-preview { font-size: 15px; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-sub { font-size: 12px; color: var(--text-faint); }
.session-empty { color: var(--text-faint); font-size: 14px; padding: 14px 10px; }
.prefs { padding: 10px 14px 4px; border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 10px; }
.pref { display: flex; align-items: center; gap: 10px; min-height: 44px; font-size: 15px; }
.pref input { width: 22px; height: 22px; accent-color: var(--accent); }
.pref-note { margin: 0; font-size: 12px; color: var(--text-faint); }
.drawer-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 14px 0; border-top: 1px solid var(--line); }
.drawer-runtime { font-size: 12px; color: var(--text-faint); }

@media (min-width: 720px) {
  .view-chat { max-width: 820px; width: 100%; margin: 0 auto; border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
  .msg-jarvis { max-width: 88%; }
  .orb { width: 150px; height: 150px; }
}
@media (max-height: 640px) {
  .orb { width: 96px; height: 96px; }
  .stage { padding-bottom: 4px; }
}
