:root {
  --bg: #0c0f14;
  --ink: #eef2f6;
  --muted: #8b95a5;
  --panel: rgba(16, 20, 28, 0.72);
  --line: rgba(255, 255, 255, 0.08);
  --phase: #6f7f93;
  --phase-rgb: 111, 127, 147;
  --font: "Outfit", system-ui, sans-serif;
  --display: "Source Serif 4", Georgia, serif;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  min-height: 100dvh;
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
}

body {
  overflow-x: hidden;
  transition: background-color 500ms ease;
}

/* ── Ambient background ─────────────────────────────────── */
.bg {
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
  background:
    radial-gradient(120% 80% at 50% -10%, rgba(var(--phase-rgb), 0.22), transparent 55%),
    radial-gradient(90% 60% at 100% 100%, rgba(20, 28, 40, 0.9), transparent 50%),
    var(--bg);
  transition: background 600ms ease;
}

.bg-orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(48px);
  opacity: 0.55;
  mix-blend-mode: screen;
  will-change: transform, opacity;
  transition: background 500ms ease, opacity 500ms ease;
}

.bg-orb-a {
  width: 55vmax;
  height: 55vmax;
  left: -15%;
  top: -10%;
  background: rgba(var(--phase-rgb), 0.45);
  animation: drift-a 14s ease-in-out infinite;
}

.bg-orb-b {
  width: 45vmax;
  height: 45vmax;
  right: -12%;
  bottom: 5%;
  background: rgba(var(--phase-rgb), 0.28);
  animation: drift-b 18s ease-in-out infinite;
}

.bg-orb-c {
  width: 30vmax;
  height: 30vmax;
  left: 35%;
  top: 40%;
  background: rgba(255, 255, 255, 0.04);
  animation: drift-c 11s ease-in-out infinite;
}

.bg-noise {
  position: absolute;
  inset: 0;
  opacity: 0.04;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

.bg-ring {
  position: absolute;
  left: 50%;
  top: 42%;
  width: min(78vw, 420px);
  height: min(78vw, 420px);
  translate: -50% -50%;
  border-radius: 50%;
  border: 1px solid rgba(var(--phase-rgb), 0.18);
  box-shadow: inset 0 0 60px rgba(var(--phase-rgb), 0.08);
  opacity: 0.7;
  transition: border-color 400ms ease, box-shadow 400ms ease, transform 400ms ease, opacity 400ms ease;
  animation: breathe 5.5s ease-in-out infinite;
}

@keyframes drift-a {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(8%, 6%) scale(1.08); }
}
@keyframes drift-b {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(-6%, -8%) scale(1.12); }
}
@keyframes drift-c {
  0%, 100% { transform: translate(0, 0); }
  50% { transform: translate(4%, -5%); }
}
@keyframes breathe {
  0%, 100% { transform: translate(-50%, -50%) scale(0.96); opacity: 0.45; }
  50% { transform: translate(-50%, -50%) scale(1.04); opacity: 0.85; }
}
@keyframes spin-slow {
  to { transform: rotate(360deg); }
}
@keyframes pulse-core {
  0%, 100% { transform: scale(1); opacity: 0.35; }
  50% { transform: scale(1.18); opacity: 0.7; }
}

/* Phase color maps */
body[data-phase="idle"]   { --phase: #6f7f93; --phase-rgb: 111, 127, 147; }
body[data-phase="ready"]  { --phase: #3dbe8c; --phase-rgb: 61, 190, 140; }
body[data-phase="you"]    { --phase: #ff6b5a; --phase-rgb: 255, 107, 90; }
body[data-phase="hear"]   { --phase: #e0a35c; --phase-rgb: 224, 163, 92; }
body[data-phase="think"]  { --phase: #e0a35c; --phase-rgb: 224, 163, 92; }
body[data-phase="ask"]    { --phase: #4ec2c8; --phase-rgb: 78, 194, 200; }
body[data-phase="talk"]   { --phase: #6aa8ff; --phase-rgb: 106, 168, 255; }
body[data-phase="error"]  { --phase: #ff6060; --phase-rgb: 255, 96, 96; }

body[data-phase="you"] .bg-orb-a,
body[data-phase="you"] .bg-orb-b {
  animation-duration: 4s, 5s;
  opacity: 0.85;
}
body[data-phase="think"] .bg-ring,
body[data-phase="hear"] .bg-ring {
  animation: breathe 2.2s ease-in-out infinite;
}
body[data-phase="ask"] .bg-ring {
  border-style: dashed;
  animation: spin-slow 10s linear infinite;
  opacity: 0.9;
}
body[data-phase="talk"] .bg-orb-c {
  background: rgba(var(--phase-rgb), 0.35);
  animation-duration: 3.5s;
}
body[data-phase="you"] .bg-ring {
  animation: pulse-core 1.1s ease-in-out infinite;
  border-width: 2px;
}

/* ── Layout ─────────────────────────────────────────────── */
main {
  position: relative;
  z-index: 1;
  width: min(560px, 100%);
  margin: 0 auto;
  min-height: 100dvh;
  padding: calc(18px + var(--safe-t)) 18px calc(28px + var(--safe-b));
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
}

.brand-mark {
  margin: 0;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.phase-label {
  margin: 4px 0 0;
  font-family: var(--display);
  font-size: clamp(28px, 7vw, 36px);
  font-weight: 600;
  letter-spacing: -0.03em;
  line-height: 1.05;
  color: var(--ink);
  transition: color 300ms ease;
}

.status {
  flex-shrink: 0;
  max-width: 46%;
  font-size: 12px;
  font-weight: 500;
  line-height: 1.3;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--muted);
  backdrop-filter: blur(10px);
  text-align: right;
}
.status.live,
.status.thinking,
.status.speaking,
.status.ptt,
.status.error {
  color: rgb(var(--phase-rgb));
  border-color: rgba(var(--phase-rgb), 0.35);
  background: rgba(var(--phase-rgb), 0.1);
}

.settings {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  backdrop-filter: blur(12px);
  overflow: hidden;
}
.settings summary {
  list-style: none;
  cursor: pointer;
  padding: 12px 14px;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  user-select: none;
}
.settings summary::-webkit-details-marker { display: none; }
.settings summary::after {
  content: "+";
  float: right;
  color: var(--phase);
}
.settings[open] summary::after { content: "–"; }
.settings-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 0 14px 14px;
}
.settings label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
select, button {
  font: inherit;
  color: var(--ink);
  border: 1px solid var(--line);
  background: rgba(8, 10, 14, 0.55);
  border-radius: 12px;
  outline: none;
}
select {
  width: 100%;
  padding: 12px 12px;
  font-size: 14px;
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0;
}
select:focus { border-color: rgba(var(--phase-rgb), 0.55); }

.steps {
  list-style: none;
  margin: 0;
  padding: 10px 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  backdrop-filter: blur(12px);
}
.step {
  flex: 1;
  position: relative;
  text-align: center;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: rgba(255, 255, 255, 0.28);
  padding: 8px 2px 6px;
  transition: color 250ms ease, opacity 250ms ease;
}
.step::before {
  content: "";
  display: block;
  width: 8px;
  height: 8px;
  margin: 0 auto 6px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.18);
  box-shadow: none;
  transition: background 250ms ease, box-shadow 250ms ease, transform 250ms ease;
}
body:not([data-bus="1"]) .step-bus { display: none; }
.step.is-done { color: rgba(255, 255, 255, 0.55); }
.step.is-done::before { background: rgba(var(--phase-rgb), 0.55); }
.step.is-active {
  color: rgb(var(--phase-rgb));
}
.step.is-active::before {
  background: rgb(var(--phase-rgb));
  transform: scale(1.25);
  box-shadow: 0 0 0 4px rgba(var(--phase-rgb), 0.18);
}
.step.is-active::after {
  content: "";
  position: absolute;
  left: 18%;
  right: 18%;
  bottom: 0;
  height: 2px;
  border-radius: 2px;
  background: rgb(var(--phase-rgb));
}

.stage {
  position: relative;
  flex: 1;
  min-height: 200px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 20px 12px 12px;
}

body[data-turn="locked"] .ptt:not(.hot) {
  opacity: 0.4;
}
body[data-turn="locked"] .ptt-hint::after {
  content: none;
}

.pulse {
  position: absolute;
  width: min(58vw, 240px);
  height: min(58vw, 240px);
  border-radius: 50%;
  background: radial-gradient(circle, rgba(var(--phase-rgb), 0.28), transparent 68%);
  animation: pulse-core 3.2s ease-in-out infinite;
  pointer-events: none;
}
body[data-phase="you"] .pulse { animation-duration: 0.9s; }
body[data-phase="ask"] .pulse { animation-duration: 1.6s; }
body[data-phase="think"] .pulse,
body[data-phase="hear"] .pulse { animation-duration: 1.8s; }

.stage-title {
  position: relative;
  margin: 0;
  font-family: var(--display);
  font-size: clamp(34px, 9vw, 48px);
  font-weight: 600;
  letter-spacing: -0.03em;
  line-height: 1;
}
.stage-sub {
  position: relative;
  margin: 10px 0 0;
  max-width: 22rem;
  font-size: 14px;
  line-height: 1.4;
  color: var(--muted);
}
#viz {
  position: relative;
  margin-top: 22px;
  width: min(100%, 280px);
  height: 48px;
  opacity: 0.85;
}

.controls {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin-top: auto;
}

.ptt {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  width: min(100%, 220px);
  height: min(100%, 220px);
  aspect-ratio: 1;
  margin: 0 auto;
  border-radius: 50%;
  border: 1px solid rgba(var(--phase-rgb), 0.35);
  background:
    radial-gradient(circle at 50% 40%, rgba(var(--phase-rgb), 0.28), rgba(8, 10, 14, 0.75) 70%);
  color: var(--ink);
  cursor: pointer;
  user-select: none;
  touch-action: none;
  transition: transform 120ms ease, border-color 200ms ease, background 200ms ease;
}
.ptt:disabled { opacity: 0.35; cursor: not-allowed; }
.ptt:not(:disabled):active,
.ptt.hot {
  transform: scale(0.97);
  border-color: rgba(var(--phase-rgb), 0.8);
  background:
    radial-gradient(circle at 50% 40%, rgba(var(--phase-rgb), 0.5), rgba(8, 10, 14, 0.8) 70%);
}
.ptt-ring {
  position: absolute;
  inset: 10px;
  border-radius: 50%;
  border: 1px dashed rgba(var(--phase-rgb), 0.35);
  animation: spin-slow 12s linear infinite;
  opacity: 0.7;
}
.ptt.hot .ptt-ring {
  border-style: solid;
  animation-duration: 3s;
  opacity: 1;
}
.ptt-label {
  position: relative;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.ptt-hint {
  position: relative;
  font-size: 12px;
  color: var(--muted);
}

.toggle {
  width: 100%;
  padding: 14px 16px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  background: rgba(255, 255, 255, 0.04);
  transition: background 120ms ease, border-color 120ms ease;
}
.toggle:hover { background: rgba(255, 255, 255, 0.07); }
.toggle.active {
  border-color: rgba(61, 190, 140, 0.45);
  color: #b6f0d4;
  background: rgba(61, 190, 140, 0.12);
}

.transcript {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 28vh;
  overflow: auto;
  padding-right: 2px;
}
.msg {
  padding: 12px 14px;
  border-radius: 14px;
  line-height: 1.45;
  font-size: 15px;
  backdrop-filter: blur(8px);
}
.msg.user {
  align-self: flex-end;
  max-width: 88%;
  background: rgba(106, 168, 255, 0.14);
  border: 1px solid rgba(106, 168, 255, 0.2);
}
.msg.assistant {
  align-self: flex-start;
  max-width: 92%;
  background: var(--panel);
  border: 1px solid var(--line);
}
.msg.interim { opacity: 0.55; font-style: italic; }

@media (min-width: 640px) {
  main { padding-top: 32px; gap: 18px; }
  .controls {
    grid-template-columns: 1fr 160px;
    align-items: center;
  }
  .ptt {
    width: 180px;
    height: 180px;
    margin: 0;
  }
  .toggle { height: 56px; }
  .transcript { max-height: none; }
}

@media (prefers-reduced-motion: reduce) {
  .bg-orb, .bg-ring, .pulse, .ptt-ring { animation: none !important; }
}
