:root {
  color-scheme: light dark;

  --bg: #f6f7f9;
  --bg-accent: rgba(79, 70, 229, 0.05);
  --surface: #ffffff;
  --surface-2: #f1f2f5;
  --ink: #16181d;
  --ink-soft: #4a4e57;
  --muted: #868b95;
  --line: rgba(20, 22, 28, 0.09);
  --line-strong: rgba(20, 22, 28, 0.14);
  --accent: #4f46e5;
  --accent-ink: #ffffff;
  --accent-soft: rgba(79, 70, 229, 0.1);
  --online: #16a34a;
  --offline: #d4992b;
  --shadow-sm: 0 1px 2px rgba(20, 22, 28, 0.05), 0 1px 3px rgba(20, 22, 28, 0.06);
  --shadow-md: 0 4px 16px rgba(20, 22, 28, 0.07), 0 1px 4px rgba(20, 22, 28, 0.05);
  --shadow-lg: 0 20px 50px rgba(20, 22, 28, 0.14);
  --radius: 18px;
  --radius-sm: 12px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d0e11;
    --bg-accent: rgba(99, 102, 241, 0.08);
    --surface: #16181d;
    --surface-2: #1d2026;
    --ink: #f0f1f3;
    --ink-soft: #c4c7cf;
    --muted: #868b95;
    --line: rgba(255, 255, 255, 0.08);
    --line-strong: rgba(255, 255, 255, 0.13);
    --accent: #6366f1;
    --accent-ink: #ffffff;
    --accent-soft: rgba(99, 102, 241, 0.16);
    --online: #34d399;
    --offline: #e0a93f;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 18px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 24px 60px rgba(0, 0, 0, 0.55);
  }
}

* { box-sizing: border-box; }

html { min-height: 100%; background: var(--bg); }

body {
  min-height: 100dvh;
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 100% -10%, var(--bg-accent), transparent 70%),
    var(--bg);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button, input { font: inherit; color: inherit; }

.shell {
  position: relative;
  width: min(100%, 560px);
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(24px, env(safe-area-inset-top)) 20px calc(20px + var(--safe-bottom));
  display: grid;
  grid-template-rows: auto auto minmax(220px, 1fr) auto auto;
  gap: 14px;
}

/* ---------- Masthead ---------- */
.masthead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 2px;
  animation: rise 480ms ease-out both;
}

.brand { display: flex; align-items: center; gap: 12px; }

.logo {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 13px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.02em;
  box-shadow: var(--shadow-sm);
}

.brand-text h1 {
  margin: 0;
  font-size: 19px;
  font-weight: 650;
  letter-spacing: -0.02em;
}

.eyebrow {
  margin: 1px 0 0;
  color: var(--muted);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.02em;
}

.connection {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 6px 11px 6px 10px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink-soft);
  font-size: 12px;
  font-weight: 550;
  box-shadow: var(--shadow-sm);
}

.signal-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--offline);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--offline) 22%, transparent);
  transition: background 220ms ease, box-shadow 220ms ease;
}

.connection[data-state="online"] .signal-dot {
  background: var(--online);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--online) 22%, transparent);
  animation: pulse 2.4s ease-in-out infinite;
}

/* ---------- Status ---------- */
.status {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  animation: rise 480ms 60ms ease-out both;
}

.status-body { flex: 1; min-width: 0; }
.status-body p { margin: 0; }
#statusText { font-size: 14px; font-weight: 550; letter-spacing: -0.01em; }
.status-detail { margin-top: 3px !important; color: var(--muted); font-size: 12px; }

.activity { display: flex; align-items: center; gap: 3px; height: 22px; }
.activity i {
  width: 3px;
  height: 6px;
  border-radius: 2px;
  background: var(--muted);
  opacity: 0.5;
}
.activity.active i { background: var(--accent); opacity: 1; animation: meter 900ms ease-in-out infinite alternate; }
.activity.active i:nth-child(2) { animation-delay: 200ms; }
.activity.active i:nth-child(3) { animation-delay: 400ms; }

/* ---------- Answer card ---------- */
.answer-card {
  position: relative;
  min-height: 0;
  overflow: auto;
  padding: 22px 22px 26px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-md);
  animation: rise 480ms 120ms ease-out both;
  -webkit-overflow-scrolling: touch;
}

.answer-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.answer-copy {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: clamp(20px, 5vw, 27px);
  line-height: 1.42;
  letter-spacing: -0.02em;
  color: var(--ink);
}

.answer-card:not(.has-answer) .answer-copy { color: var(--muted); }
.answer-card.has-answer .answer-copy { animation: answerIn 420ms cubic-bezier(.2, .8, .2, 1) both; }

/* ---------- Controls ---------- */
.controls {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  animation: rise 480ms 180ms ease-out both;
}

.speak-button, .utility-button {
  border: 0;
  border-radius: var(--radius-sm);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: transform 140ms ease, background 180ms ease, box-shadow 180ms ease, opacity 180ms ease;
}

.speak-button:active, .utility-button:active { transform: scale(0.985); }

.speak-button {
  min-height: 66px;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  gap: 14px;
  text-align: left;
  color: var(--accent-ink);
  background: var(--accent);
  box-shadow: 0 6px 18px color-mix(in srgb, var(--accent) 32%, transparent);
}

.speak-button:disabled {
  cursor: not-allowed;
  color: var(--muted);
  background: var(--surface-2);
  box-shadow: none;
}

.speak-button.speaking { background: var(--ink); box-shadow: var(--shadow-md); }

.button-icon {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.16);
  font-size: 14px;
}
.speak-button:disabled .button-icon { background: var(--line); }

.button-text { min-width: 0; }
.speak-button strong { display: block; font-size: 15px; font-weight: 600; letter-spacing: -0.01em; }
.speak-button small {
  display: block;
  margin-top: 2px;
  font-size: 12px;
  font-weight: 450;
  opacity: 0.82;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.utility-button {
  width: 96px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--ink-soft);
  font-size: 11px;
  font-weight: 550;
  box-shadow: var(--shadow-sm);
}

.utility-icon { font-size: 16px; color: var(--muted); transition: color 180ms ease; }
.utility-button.armed { border-color: var(--accent); color: var(--accent); }
.utility-button.armed .utility-icon { color: var(--accent); }

/* ---------- Footer ---------- */
footer {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 2px 4px;
  color: var(--muted);
  font-size: 12px;
  animation: rise 480ms 240ms ease-out both;
}

footer span:first-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.footer-dot { width: 3px; height: 3px; border-radius: 50%; background: currentColor; flex-shrink: 0; opacity: 0.6; }
#modelLabel { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.switch-device {
  flex-shrink: 0;
  padding: 5px 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 11px;
  font-weight: 550;
  transition: color 160ms ease, border-color 160ms ease, background 160ms ease;
}
.switch-device:hover { color: var(--ink); border-color: var(--line-strong); background: var(--surface); }

/* ---------- Dialog ---------- */
dialog {
  width: min(calc(100% - 36px), 400px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--ink);
  background: var(--surface);
  box-shadow: var(--shadow-lg);
}

dialog::backdrop { background: rgba(13, 14, 17, 0.45); backdrop-filter: blur(6px); }
dialog[open] { animation: dialogIn 260ms cubic-bezier(.2, .8, .2, 1) both; }
dialog[open]::backdrop { animation: fade 260ms ease both; }

dialog form { padding: 28px; }
dialog .eyebrow { color: var(--accent); font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; font-size: 11px; }
dialog h2 { margin: 10px 0 8px; font-size: 22px; font-weight: 650; letter-spacing: -0.02em; }
dialog p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.55; }
dialog label { display: grid; gap: 8px; margin: 22px 0 0; font-size: 12px; font-weight: 600; color: var(--ink-soft); }

dialog input {
  width: 100%;
  padding: 14px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--ink);
  text-align: center;
  font-size: 26px;
  font-weight: 600;
  letter-spacing: 0.2em;
  font-variant-numeric: tabular-nums;
  transition: border-color 160ms ease, box-shadow 160ms ease;
}
dialog input::placeholder { color: var(--muted); letter-spacing: 0.2em; }
dialog input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-soft);
}

dialog button {
  width: 100%;
  margin-top: 22px;
  padding: 14px;
  border: 0;
  border-radius: var(--radius-sm);
  color: var(--accent-ink);
  background: var(--accent);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 6px 18px color-mix(in srgb, var(--accent) 32%, transparent);
  transition: transform 140ms ease, opacity 160ms ease;
}
dialog button:active { transform: scale(0.99); }
dialog button:disabled { opacity: 0.6; cursor: progress; box-shadow: none; }

.pair-error { min-height: 18px; margin: 10px 0 0 !important; color: #e5484d !important; font-size: 12px !important; }

/* ---------- Animations ---------- */
@keyframes rise { from { opacity: 0; transform: translateY(10px); } }
@keyframes answerIn { from { opacity: 0; transform: translateY(6px); } }
@keyframes meter { to { height: 18px; } }
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 3px color-mix(in srgb, var(--online) 22%, transparent); }
  50% { box-shadow: 0 0 0 5px color-mix(in srgb, var(--online) 8%, transparent); }
}
@keyframes dialogIn { from { opacity: 0; transform: translateY(12px) scale(0.97); } }
@keyframes fade { from { opacity: 0; } }

/* ---------- iPhone (compact) ---------- */
@media (max-width: 380px) {
  .shell { padding-inline: 16px; gap: 12px; }
  .utility-button { width: 84px; }
  .speak-button { padding: 12px 16px; }
}

/* short iPhones in landscape: trim vertical rhythm so everything fits */
@media (max-height: 480px) and (orientation: landscape) {
  .shell { grid-template-rows: auto auto minmax(120px, 1fr) auto auto; gap: 10px; padding-block: 12px; }
  .answer-card { padding: 16px 18px; }
}

/* ---------- iPad (portrait & up) ---------- */
@media (min-width: 600px) {
  .shell {
    width: min(100%, 680px);
    padding: max(40px, env(safe-area-inset-top)) 32px calc(32px + var(--safe-bottom));
    gap: 20px;
  }
  .logo { width: 48px; height: 48px; border-radius: 15px; font-size: 17px; }
  .brand-text h1 { font-size: 22px; }
  .eyebrow { font-size: 12px; }
  .connection { font-size: 13px; padding: 8px 14px 8px 12px; }
  .status { padding: 18px 20px; }
  #statusText { font-size: 15px; }
  .answer-card { padding: 32px 34px 36px; }
  .answer-copy { font-size: clamp(24px, 3.2vw, 30px); line-height: 1.45; }
  .speak-button { min-height: 74px; }
  .utility-button { width: 116px; }
}

/* ---------- iPad landscape & large screens: frame the app instead of stretching ---------- */
@media (min-width: 900px) and (min-height: 700px) {
  body { display: grid; place-items: center; padding: 32px; }
  .shell {
    width: min(100%, 760px);
    min-height: 0;
    height: min(92dvh, 920px);
    border: 1px solid var(--line);
    border-radius: 28px;
    background: var(--surface);
    box-shadow: var(--shadow-lg);
    padding: 36px;
  }
  .status, .answer-card { background: var(--surface-2); box-shadow: none; }
  .answer-copy { font-size: clamp(26px, 2.4vw, 32px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; animation-iteration-count: 1 !important; transition-duration: 1ms !important; }
}
