:root {
  --bg: #f6f8fb;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --line: #e6ebf1;
  --text: #1e293b;
  --muted: #64748b;
  --accent: #3b82f6;
  --accent-2: #10b981;
  --amber: #f59e0b;
  --rec: #ef4444;
  --font: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --mono: "JetBrains Mono", "Fira Code", "Consolas", monospace;
  --shadow-sm: 0 1px 2px 0 rgba(15, 23, 42, 0.05);
  --shadow-md: 0 6px 20px -6px rgba(15, 23, 42, 0.12);
  --shadow-lg: 0 20px 40px -12px rgba(15, 23, 42, 0.22);
  --radius-lg: 18px;
  --radius-md: 12px;
  --radius-sm: 8px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  overflow: hidden;
}

/* ── top bar ── */
.topbar {
  flex: 0 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  font-size: 24px;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), #6366f1);
  border-radius: var(--radius-md);
  box-shadow: 0 4px 12px -2px rgba(59, 130, 246, 0.5);
}
.brand-text { min-width: 0; }
.brand-text h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: #0f172a;
}
.brand-text span {
  font-size: 12.5px;
  color: var(--muted);
  white-space: nowrap;
}

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

.start-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 18px;
  font-family: inherit;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s, box-shadow 0.2s;
  box-shadow: 0 3px 10px -2px rgba(59, 130, 246, 0.5);
  white-space: nowrap;
}
.start-btn:hover:not(:disabled) { background: #2563eb; transform: translateY(-1px); }
.start-btn:active:not(:disabled) { transform: translateY(0); }
.start-btn:disabled { opacity: 0.55; cursor: default; box-shadow: none; }
.start-btn.on { background: var(--rec); box-shadow: 0 3px 10px -2px rgba(239, 68, 68, 0.5); }
.start-btn.on:hover:not(:disabled) { background: #dc2626; }

.icon-btn {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  font-size: 18px;
  color: var(--muted);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s;
}
.icon-btn:hover { color: var(--text); background: #eef2f7; border-color: #cbd5e1; }

/* ── level meter ── */
.meter { display: flex; align-items: center; gap: 8px; opacity: 0; transition: opacity 0.25s; pointer-events: none; }
.meter.active { opacity: 1; }
.meter-bar {
  width: 90px;
  height: 10px;
  background: #e8edf3;
  border-radius: 5px;
  overflow: hidden;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.06);
}
.meter-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent-2), #34d399);
  transition: width 0.06s linear, background 0.15s;
  border-radius: 5px;
}
.meter-fill.hot { background: linear-gradient(90deg, var(--amber), var(--rec)); }
.meter-val {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
  min-width: 62px;
  text-align: right;
  font-weight: 500;
}

/* ── status line ── */
.status {
  flex: 0 0 auto;
  padding: 6px 20px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

/* ── stage ── */
.stage {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 16px;
}

/* Hero plot */
.plot-card {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  overflow: hidden;
}
.formant-plot {
  width: 100%;
  height: 100%;
  display: block;
}
.plot-controls {
  position: absolute;
  left: 16px;
  bottom: 16px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  max-width: calc(100% - 32px);
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(10px);
  padding: 8px 10px;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--line);
}
.btn-hero {
  background: linear-gradient(135deg, #fde68a, #fbbf24);
  color: #7c2d12;
  border: none;
  border-radius: var(--radius-sm);
  padding: 9px 16px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.2s, opacity 0.2s;
  box-shadow: 0 2px 8px -2px rgba(245, 158, 11, 0.6);
}
.btn-hero:hover:not(:disabled) { transform: translateY(-1px); box-shadow: 0 4px 12px -2px rgba(245, 158, 11, 0.7); }
.btn-hero:disabled { opacity: 0.5; cursor: default; box-shadow: none; }
.btn-soft {
  background: var(--panel-2);
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.btn-soft:hover:not(:disabled) { color: var(--text); background: #eef2f7; border-color: #cbd5e1; }
.btn-soft:disabled { opacity: 0.45; cursor: default; }
.synth-info { font-family: var(--mono); font-size: 11.5px; color: var(--muted); }

.plot-hint {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(560px, calc(100% - 48px));
  text-align: center;
  font-size: 14px;
  line-height: 1.55;
  color: var(--text);
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(12px);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-md);
  padding: 18px 22px;
  border-radius: var(--radius-md);
}
.plot-hint.hidden { display: none; }

/* Spectrogram strip */
.spec-card {
  position: relative;
  flex: 0 0 210px;
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-md);
  background: #440154;
  border: 1px solid var(--line);
}
.spectrogram { width: 100%; height: 100%; display: block; }
.freq-axis {
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 46px;
  pointer-events: none;
  background: rgba(255, 255, 255, 0.9);
  border-right: 1px solid var(--line);
}
.freq-axis span {
  position: absolute;
  right: 7px;
  transform: translateY(50%);
  font-family: var(--mono);
  font-size: 10.5px;
  color: var(--muted);
  font-weight: 500;
}
.live-axis {
  position: absolute;
  right: 0; top: 0; bottom: 0;
  width: 46px;
  pointer-events: none;
  border-left: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(0, 0, 0, 0.12);
}
.live-marker {
  position: absolute;
  right: 7px;
  transform: translateY(50%);
  font-family: var(--mono);
  font-size: 10.5px;
  font-weight: 700;
  padding: 2px 4px;
  border-radius: 3px;
  color: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: bottom 0.06s linear;
}
.live-marker.f1 { background: #ff3366; }
.live-marker.f2 { background: #00e5ff; color: #000; }

/* ── setup overlay ── */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(4px);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 24px;
  overflow-y: auto;
}
.overlay-panel {
  width: 100%;
  max-width: 1100px;
  margin: auto;
  background: var(--bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
.overlay-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 20px 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.overlay-header h3 { margin: 0 0 4px; font-size: 19px; font-weight: 800; color: #0f172a; }
.overlay-sub { margin: 0; font-size: 13px; line-height: 1.5; color: var(--muted); max-width: 640px; }
.overlay-body { padding: 20px 24px 28px; }

.under-hood {
  margin-top: 20px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 4px 16px;
}
.under-hood summary {
  cursor: pointer;
  padding: 10px 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  list-style: none;
}
.under-hood summary::-webkit-details-marker { display: none; }
.under-hood summary::before { content: "▸ "; }
.under-hood[open] summary::before { content: "▾ "; }
.under-hood p { margin: 0 0 14px; font-size: 13px; line-height: 1.6; color: var(--muted); }

/* ── calibration ── */
.calib-root { width: 100%; }
.calib-bar {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 20px;
  background: var(--panel);
  padding: 14px 18px;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--line);
}
.calib-note { color: var(--muted); font-size: 13px; line-height: 1.5; margin: 0; flex: 1; }
.btn-export {
  flex: 0 0 auto;
  background: #f0fdf4;
  color: #166534;
  border: 1px solid #bbf7d0;
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.2s;
}
.btn-export:hover { background: #dcfce7; border-color: #86efac; }

/* vowel cards */
.vowel-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 18px;
}
.vowel-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: var(--shadow-sm);
  transition: box-shadow 0.2s, transform 0.2s;
}
.vowel-card:hover { box-shadow: var(--shadow-md); transform: translateY(-2px); }
.vowel-head { display: flex; align-items: flex-start; justify-content: space-between; }
.vowel-head-main { display: flex; align-items: baseline; gap: 12px; }
.vowel-letter { font-size: 30px; font-weight: 800; color: var(--text); line-height: 1; letter-spacing: -0.03em; }
.vowel-hint { color: var(--muted); font-size: 12.5px; }
.vowel-status {
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--muted);
  background: var(--panel-2);
  padding: 4px 8px;
  border-radius: 4px;
}
.vowel-graphs { display: flex; flex-direction: column; gap: 4px; }
.wave-canvas {
  width: 100%;
  height: 56px;
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 6px 6px 2px 2px;
  display: block;
}
.spec-canvas {
  width: 100%;
  height: 130px;
  background: #440154;
  border-radius: 2px 2px 6px 6px;
  display: block;
}
.vowel-footer { display: flex; flex-direction: column; gap: 12px; margin-top: auto; }
.vowel-meta { display: flex; align-items: center; justify-content: space-between; }
.vowel-formants {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 22px;
  font-family: var(--mono);
  font-size: 12px;
  flex-wrap: wrap;
}
.fmt-cell { background: #f1f5f9; padding: 2px 6px; border-radius: 4px; color: var(--muted); }
.fmt-cell b { color: var(--text); font-weight: 700; margin-left: 4px; }
.fmt-cell.fmt-bad { background: #fef2f2; color: #ef4444; }
.fmt-cell.fmt-bad b { color: #dc2626; }
.fmt-bad { color: #dc2626; font-size: 12px; }
.fmt-frames { color: var(--muted); font-size: 11px; margin-left: 4px; }
.vowel-dur { font-family: var(--mono); font-size: 11px; color: var(--muted); text-align: right; }

.vowel-controls { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.controls-main { display: flex; gap: 8px; flex: 1; }
.controls-secondary { display: flex; gap: 6px; }
.vowel-controls button {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 9px 14px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.15s;
}
.vowel-controls button:hover:not(:disabled) { background: var(--panel-2); border-color: #cbd5e1; }
.vowel-controls button:disabled { opacity: 0.5; cursor: default; }
.btn-rec { color: #dc2626 !important; border-color: #fca5a5 !important; background: #fef2f2 !important; flex: 1; }
.btn-rec:hover:not(:disabled) { background: #fee2e2 !important; border-color: #f87171 !important; }
.btn-rec.recording { background: #ef4444 !important; color: #fff !important; border-color: #ef4444 !important; animation: pulse-rec 1.5s infinite; }
.btn-play { flex: 1; }
.btn-icon { padding: 9px !important; color: var(--muted) !important; }
.btn-icon:hover:not(:disabled) { color: var(--text) !important; }

@keyframes pulse-rec {
  0% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4); }
  70% { box-shadow: 0 0 0 6px rgba(239, 68, 68, 0); }
  100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); }
}

/* ── tips modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(4px);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal-content {
  background: var(--panel);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 640px;
  max-height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid var(--line);
}
.modal-header h3 { margin: 0; font-size: 18px; color: var(--text); }
.btn-close {
  background: none;
  border: none;
  font-size: 18px;
  color: var(--muted);
  cursor: pointer;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  line-height: 1;
}
.btn-close:hover { color: var(--text); background: var(--panel-2); }
.modal-body {
  padding: 22px;
  overflow-y: auto;
  font-size: 14px;
  line-height: 1.6;
  color: #334155;
}
.modal-body p { margin-top: 0; margin-bottom: 20px; }
.accent-title {
  color: var(--accent);
  margin-bottom: 12px;
  font-size: 1.1em;
  border-bottom: 1px solid var(--line);
  padding-bottom: 5px;
}
.steering-list {
  background: rgba(59, 130, 246, 0.08);
  padding: 15px 15px 15px 35px;
  border-radius: var(--radius-md);
  margin-bottom: 20px;
}
.steering-list li { margin-bottom: 10px; }
.tip-card {
  background: var(--panel-2);
  padding: 12px 16px;
  border-radius: var(--radius-md);
  margin-bottom: 12px;
  border-left: 4px solid var(--amber);
}
.tip-card h5 { margin: 0 0 8px 0; font-size: 1.05em; color: var(--text); }
.tip-card p { margin: 0; font-size: 0.95em; color: var(--muted); }
.words-line { font-size: 1.15em; letter-spacing: 0.02em; color: var(--text); }

/* ── mobile ── */
@media (max-width: 680px) {
  body { overflow: auto; }
  .topbar { flex-wrap: wrap; gap: 10px; padding: 10px 14px; }
  .brand-text span { white-space: normal; font-size: 11.5px; }
  .topbar-actions { width: 100%; justify-content: space-between; }
  .meter { order: 3; width: 100%; justify-content: flex-end; }
  .start-btn { flex: 1; text-align: center; }
  .status { padding: 5px 14px; }

  .stage { padding: 12px; gap: 12px; height: auto; min-height: 0; }
  .plot-card { min-height: 62dvh; }
  .spec-card { flex: 0 0 150px; }

  .plot-controls {
    left: 12px; right: 12px; bottom: 12px;
    max-width: none;
    justify-content: center;
  }
  .btn-hero { flex: 1 1 100%; text-align: center; padding: 11px 16px; }
  .synth-info { flex: 1 1 100%; text-align: center; }
  .plot-hint { font-size: 13px; padding: 14px 16px; }

  .overlay { padding: 0; }
  .overlay-panel { border-radius: 0; min-height: 100dvh; }
  .vowel-grid { grid-template-columns: 1fr; }
  .calib-bar { flex-direction: column; align-items: stretch; }
}
