:root {
  --bg0: #0b1220;
  --bg1: #122033;
  --bg2: #1a2d45;
  --surface: rgba(255, 255, 255, 0.06);
  --surface-2: rgba(255, 255, 255, 0.1);
  --border: rgba(148, 163, 184, 0.22);
  --text: #e8eef7;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-2: #34d399;
  --warn: #fbbf24;
  --bad: #f87171;
  --good: #4ade80;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
  --radius: 18px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --font: "DM Sans", system-ui, sans-serif;
  --display: "Fraunces", Georgia, serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(56, 189, 248, 0.18), transparent 55%),
    radial-gradient(900px 500px at 100% 0%, rgba(52, 211, 153, 0.12), transparent 50%),
    linear-gradient(165deg, var(--bg0), var(--bg1) 45%, var(--bg2));
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
}

body {
  min-height: 100dvh;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.hidden {
  display: none !important;
}

.app {
  max-width: 720px;
  margin: 0 auto;
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto auto auto 1fr auto auto;
  padding: 12px 14px calc(12px + var(--safe-b));
  gap: 10px;
}

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

.brand h1 {
  margin: 0;
  font-family: var(--display);
  font-size: clamp(1.45rem, 4.5vw, 1.85rem);
  font-weight: 700;
  letter-spacing: -0.02em;
}

.tagline {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.scenario-field select,
.custom-scenario input,
#transcriptEdit {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 12px;
  padding: 10px 12px;
  font: inherit;
}

.scenario-field select {
  max-width: 190px;
}

.custom-scenario {
  margin-top: -2px;
}

.status-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 0.9rem;
  color: var(--muted);
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--muted);
  flex-shrink: 0;
}

.status-bar[data-state="recording"] {
  color: #fecaca;
  border-color: rgba(248, 113, 113, 0.45);
}
.status-bar[data-state="recording"] .status-dot {
  background: var(--bad);
  box-shadow: 0 0 0 0 rgba(248, 113, 113, 0.6);
  animation: pulse 1.2s infinite;
}

.status-bar[data-state="processing"] .status-dot {
  background: var(--accent);
  animation: spin-dot 0.9s linear infinite;
}

.status-bar[data-state="speaking"] {
  color: #a7f3d0;
}
.status-bar[data-state="speaking"] .status-dot {
  background: var(--accent-2);
  animation: pulse 1.4s infinite;
}

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(248, 113, 113, 0.55); }
  70% { box-shadow: 0 0 0 12px rgba(248, 113, 113, 0); }
  100% { box-shadow: 0 0 0 0 rgba(248, 113, 113, 0); }
}

@keyframes spin-dot {
  from { transform: scale(0.7); opacity: 0.5; }
  50% { transform: scale(1.15); opacity: 1; }
  to { transform: scale(0.7); opacity: 0.5; }
}

.chat-log {
  overflow-y: auto;
  padding: 6px 2px 12px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}

.bubble {
  max-width: 92%;
  padding: 12px 14px;
  border-radius: 16px;
  line-height: 1.45;
  box-shadow: var(--shadow);
  animation: rise 0.28s ease-out;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.bubble.user {
  align-self: flex-end;
  background: linear-gradient(145deg, #2563eb, #1d4ed8);
  border-bottom-right-radius: 6px;
}

.bubble.ai {
  align-self: flex-start;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}

.bubble .meta {
  display: block;
  font-size: 0.72rem;
  opacity: 0.75;
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.coach-card {
  align-self: stretch;
  background: rgba(15, 23, 42, 0.55);
  border: 1px dashed rgba(148, 163, 184, 0.35);
  border-radius: var(--radius);
  padding: 12px 14px;
  color: var(--muted);
  font-size: 0.92rem;
  white-space: pre-wrap;
}

.coach-card strong {
  color: var(--text);
  display: block;
  margin-bottom: 6px;
  font-size: 0.8rem;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}

.mini-heatmap {
  margin-top: 8px;
  font-size: 0.95rem;
  line-height: 1.7;
}

.review-panel {
  background: rgba(15, 23, 42, 0.72);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
}

.review-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}

.review-head h2 {
  margin: 0;
  font-size: 1rem;
  font-family: var(--display);
}

.score-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.badge {
  font-size: 0.72rem;
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
}

.badge em {
  font-style: normal;
  color: var(--text);
  font-weight: 600;
}

.heatmap {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 8px;
  margin-bottom: 12px;
  line-height: 1.6;
}

.heat-word {
  cursor: pointer;
  border: none;
  background: transparent;
  color: inherit;
  font: inherit;
  padding: 0 1px;
  border-bottom: 3px solid transparent;
}

.heat-word.good { border-bottom-color: var(--good); color: #bbf7d0; }
.heat-word.fair { border-bottom-color: var(--warn); color: #fde68a; }
.heat-word.weak { border-bottom-color: var(--bad); color: #fecaca; }
.heat-word:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.field .label {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  margin-bottom: 4px;
}

.review-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.controls {
  position: sticky;
  bottom: 0;
  padding-top: 4px;
  background: linear-gradient(180deg, transparent, rgba(11, 18, 32, 0.92) 18%);
}

.controls-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.controls-row.prefs {
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.controls-row.main {
  justify-content: center;
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.86rem;
  color: var(--muted);
  user-select: none;
}

.rate-field {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.86rem;
  color: var(--muted);
}

.rate-field input[type="range"] {
  width: 110px;
  accent-color: var(--accent);
}

.btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 12px;
  padding: 10px 14px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.15s ease, opacity 0.15s ease;
}

.btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.14);
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn.ghost {
  background: transparent;
}

.btn.primary {
  background: linear-gradient(135deg, #0ea5e9, #2563eb);
  border-color: transparent;
}

.btn.danger {
  color: #fecaca;
  border-color: rgba(248, 113, 113, 0.45);
}

.btn.record {
  min-width: min(100%, 320px);
  padding: 18px 22px;
  border-radius: 999px;
  font-size: 1.05rem;
  background: linear-gradient(135deg, #22c55e, #16a34a);
  border: none;
  box-shadow: 0 10px 30px rgba(22, 163, 74, 0.35);
}

.btn.record[aria-pressed="true"] {
  background: linear-gradient(135deg, #f87171, #dc2626);
  box-shadow: 0 10px 30px rgba(220, 38, 38, 0.35);
}

.btn.record.busy {
  background: linear-gradient(135deg, #64748b, #475569);
  box-shadow: none;
}

.hint {
  text-align: center;
  color: var(--muted);
  font-size: 0.78rem;
  margin: 8px 0 0;
}

kbd {
  font-family: ui-monospace, monospace;
  font-size: 0.85em;
  padding: 1px 5px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--surface);
}

.phoneme-popover {
  position: fixed;
  z-index: 40;
  max-width: min(320px, calc(100vw - 24px));
  padding: 12px 14px;
  border-radius: 14px;
  background: #0f172a;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: 0.9rem;
}

.phoneme-popover h3 {
  margin: 0 0 8px;
  font-size: 0.95rem;
}

.phoneme-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.phoneme-chip {
  padding: 4px 8px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-family: ui-monospace, monospace;
  font-size: 0.82rem;
}

.phoneme-chip.weak {
  border-color: rgba(248, 113, 113, 0.55);
  color: #fecaca;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-b));
  transform: translateX(-50%);
  z-index: 50;
  max-width: min(92vw, 420px);
  padding: 12px 16px;
  border-radius: 12px;
  background: #111827;
  border: 1px solid rgba(248, 113, 113, 0.4);
  color: #fee2e2;
  box-shadow: var(--shadow);
}

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--muted);
  padding: 40px 20px;
  max-width: 28rem;
}

.empty-state h2 {
  font-family: var(--display);
  color: var(--text);
  margin: 0 0 8px;
  font-size: 1.35rem;
}

@media (max-width: 480px) {
  .scenario-field select {
    max-width: 150px;
  }
  .btn.record {
    min-width: 100%;
  }
}
