:root {
  color-scheme: dark;
  --bg: #0b0e14;
  --surface: #12151c;
  --surface-2: #181c26;
  --surface-3: #202533;
  --border: #262c3a;
  --grid: #1f2430;
  --text: #e8ebf1;
  --text-2: #a9b0bf;
  --muted: #6e7689;
  --accent: #3987e5;
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --radius: 10px;
  --mono: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  font-family: -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Segoe UI', sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.5; }
button, select, input { font: inherit; color: inherit; }
a { color: var(--accent); }
h1, h2, h3, h4 { margin: 0; font-weight: 600; }
.muted { color: var(--muted); }
.t2 { color: var(--text-2); }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }

/* ---------- header ---------- */
header.app {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 16px; background: rgba(11, 14, 20, 0.92); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; letter-spacing: 0.2px; }
.brand svg { width: 26px; height: 26px; }
nav.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
nav.tabs button {
  background: none; border: 1px solid transparent; color: var(--text-2);
  padding: 6px 12px; border-radius: 8px; cursor: pointer;
}
nav.tabs button:hover { background: var(--surface-2); color: var(--text); }
nav.tabs button.active { background: var(--surface-3); color: var(--text); border-color: var(--border); }
.spacer { flex: 1; }

.btn {
  display: inline-flex; align-items: center; gap: 6px; justify-content: center;
  background: var(--surface-3); border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 12px; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: #3a4254; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary.recording { background: var(--critical); border-color: var(--critical); }
.btn.danger { color: #ff8a8a; }
.btn:disabled { opacity: 0.5; cursor: default; }
select, input[type=number], input[type=text] {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px;
}
input[type=range] { accent-color: var(--accent); }

main { padding: 14px 16px 40px; max-width: 1680px; margin: 0 auto; }
.tab { display: none; }
.tab.active { display: block; }

/* ---------- panels ---------- */
.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px; min-width: 0; position: relative;
}
.ph { display: flex; align-items: baseline; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.ph h3 { font-size: 13px; color: var(--text); }
.ph .sub { font-size: 12px; color: var(--muted); }
.ph .info {
  margin-left: auto; width: 18px; height: 18px; border-radius: 50%; border: 1px solid var(--border);
  color: var(--muted); font-size: 11px; display: grid; place-items: center; cursor: help; flex: none;
}
canvas.viz { width: 100%; display: block; border-radius: 6px; }

.live-controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.live-controls .status { font-size: 12px; color: var(--muted); }
.level { width: 110px; height: 8px; background: var(--surface-3); border-radius: 4px; overflow: hidden; }
.level > div { height: 100%; width: 0; background: var(--good); transition: width 60ms linear; }
.timer { font-family: var(--mono); font-size: 13px; color: var(--text-2); min-width: 52px; }

.grid-live {
  display: grid; gap: 12px;
  grid-template-columns: minmax(260px, 1.05fr) minmax(220px, 0.8fr) minmax(300px, 1.15fr);
  grid-template-areas:
    'reg pitch anat'
    'track track track'
    'spec spec sgram'
    'metrics metrics metrics'
    'vowel vrp regmap'
    'feed feed feed';
}
.a-reg { grid-area: reg; } .a-pitch { grid-area: pitch; } .a-anat { grid-area: anat; }
.a-track { grid-area: track; } .a-spec { grid-area: spec; } .a-sgram { grid-area: sgram; }
.a-metrics { grid-area: metrics; } .a-vowel { grid-area: vowel; } .a-vrp { grid-area: vrp; }
.a-regmap { grid-area: regmap; } .a-feed { grid-area: feed; }
@media (max-width: 1100px) {
  .grid-live {
    grid-template-columns: 1fr 1fr;
    grid-template-areas: 'reg pitch' 'anat anat' 'track track' 'spec spec' 'sgram sgram' 'metrics metrics' 'vowel vrp' 'regmap regmap' 'feed feed';
  }
}
@media (max-width: 680px) {
  .grid-live { grid-template-columns: 1fr; grid-template-areas: 'reg' 'pitch' 'anat' 'track' 'spec' 'sgram' 'metrics' 'vowel' 'vrp' 'regmap' 'feed'; }
}

/* register card */
.reg-main { display: flex; align-items: center; gap: 12px; min-height: 64px; }
.reg-chip {
  font-size: 26px; font-weight: 800; letter-spacing: 0.5px; padding: 2px 12px; border-radius: 10px; white-space: nowrap;
  border: 2px solid currentColor; line-height: 1.3; min-width: 92px; text-align: center; transition: color 120ms;
}
.reg-name { font-size: 13px; color: var(--text-2); }
.reg-alert {
  margin-top: 6px; font-size: 13px; font-weight: 600; min-height: 22px; display: flex; gap: 6px; flex-wrap: wrap;
}
.alert-tag { padding: 1px 8px; border-radius: 6px; border: 1px solid; font-size: 12px; }
.alert-tag.crit { color: #ff9c9c; border-color: var(--critical); background: rgba(208, 59, 59, 0.14); }
.alert-tag.warn { color: var(--warning); border-color: rgba(250, 178, 25, 0.5); background: rgba(250, 178, 25, 0.08); }
.alert-tag.info { color: var(--text-2); border-color: var(--border); }
.meter { margin-top: 10px; }
.meter .mlabel { display: flex; justify-content: space-between; font-size: 12px; color: var(--text-2); margin-bottom: 3px; }
.meter .bar { height: 10px; border-radius: 5px; background: var(--surface-3); position: relative; overflow: hidden; }
.meter .bar .fill { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 5px; transition: width 80ms linear; }
.meter .bar.thick {
  background: linear-gradient(90deg, #3987e5 0%, #c98500 40%, #008300 60%, #e66767 100%); opacity: 0.9; overflow: visible;
}
.meter .bar.thick .needle {
  position: absolute; top: -3px; width: 4px; height: 16px; margin-left: -2px; background: #fff; border-radius: 2px;
  box-shadow: 0 0 0 2px var(--surface); transition: left 80ms linear;
}
.meter .ticks { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); margin-top: 2px; }
.tip-box {
  margin-top: 10px; padding: 8px 10px; background: var(--surface-2); border-radius: 8px; border-left: 3px solid var(--accent);
  font-size: 12.5px; color: var(--text-2); min-height: 40px;
}

/* pitch card */
.pitch-note { font-size: 44px; font-weight: 800; line-height: 1.05; font-variant-numeric: tabular-nums; }
.pitch-hz { font-family: var(--mono); color: var(--text-2); font-size: 13px; }
.pitch-row { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; font-size: 12.5px; margin-top: 8px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-family: var(--mono); color: var(--text); }

/* metrics */
.metrics { display: grid; grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); gap: 8px; }
.metric { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; border: 1px solid transparent; }
.metric .mname { font-size: 12px; color: var(--text-2); display: flex; justify-content: space-between; gap: 4px; }
.metric .mname .q { color: var(--muted); cursor: help; }
.metric .mval { font-size: 20px; font-weight: 700; font-family: var(--mono); margin: 2px 0 4px; }
.metric .mval small { font-size: 11px; color: var(--muted); font-weight: 400; margin-left: 3px; }
.metric .mbar { height: 6px; border-radius: 3px; position: relative; display: flex; overflow: visible; gap: 2px; }
.metric .mbar span { height: 100%; border-radius: 3px; opacity: 0.55; }
.metric .mbar i { position: absolute; top: -3px; width: 3px; height: 12px; background: #fff; border-radius: 2px; margin-left: -1.5px; box-shadow: 0 0 0 1.5px var(--surface-2); }
.metric .mstate { font-size: 11px; color: var(--muted); margin-top: 4px; min-height: 16px; }
.z-good { background: var(--good); } .z-warn { background: var(--warning); } .z-bad { background: var(--critical); } .z-neutral { background: #4a5266; }

/* legend */
.legend { display: flex; gap: 10px; flex-wrap: wrap; font-size: 12px; color: var(--text-2); }
.legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; }

/* feed */
.feed { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 800px) { .feed { grid-template-columns: 1fr; } }
.feed-list { max-height: 260px; overflow: auto; display: flex; flex-direction: column; gap: 6px; }
.feed-item { display: grid; grid-template-columns: 58px 1fr; gap: 8px; font-size: 12.5px; padding: 6px 8px; background: var(--surface-2); border-radius: 8px; border-left: 3px solid; }
.feed-item .ft { font-family: var(--mono); color: var(--muted); }
.feed-item b { font-weight: 600; }
.empty { color: var(--muted); font-size: 12.5px; padding: 12px; text-align: center; }

/* tooltip */
.tooltip {
  position: fixed; pointer-events: none; z-index: 50; background: #1c2130; border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 9px; font-size: 12px; color: var(--text); box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
  max-width: 320px; display: none;
}

/* ---------- history ---------- */
.hist-layout { display: grid; grid-template-columns: 300px 1fr; gap: 12px; }
@media (max-width: 900px) { .hist-layout { grid-template-columns: 1fr; } }
.sess-list { display: flex; flex-direction: column; gap: 6px; max-height: calc(100vh - 160px); overflow: auto; }
.sess-item { padding: 9px 10px; border-radius: 8px; background: var(--surface-2); cursor: pointer; border: 1px solid transparent; }
.sess-item:hover { border-color: var(--border); }
.sess-item.active { border-color: var(--accent); }
.sess-item .top { display: flex; justify-content: space-between; font-size: 12.5px; }
.sess-item .mini { height: 6px; display: flex; border-radius: 3px; overflow: hidden; margin-top: 6px; gap: 1px; }
.score-row { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
.score { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; }
.score .v { font-size: 24px; font-weight: 800; font-family: var(--mono); }
.score .n { font-size: 12px; color: var(--text-2); }
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 12px; }
@media (max-width: 1000px) { .detail-grid { grid-template-columns: 1fr; } }
.span2 { grid-column: 1 / -1; }
table.data { width: 100%; border-collapse: collapse; font-size: 12.5px; }
table.data th, table.data td { text-align: left; padding: 5px 8px; border-bottom: 1px solid var(--grid); vertical-align: top; }
table.data th { color: var(--muted); font-weight: 500; position: sticky; top: 0; background: var(--surface); }
table.data tr.click { cursor: pointer; }
table.data tr.click:hover td { background: var(--surface-2); }
.table-wrap { max-height: 320px; overflow: auto; }
.distbar { display: flex; height: 16px; border-radius: 4px; overflow: hidden; gap: 2px; }
.distbar > div { height: 100%; }
audio { width: 100%; margin: 8px 0; }

/* ---------- coach / science ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 12px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.card h4 { font-size: 14px; margin-bottom: 6px; }
.card p, .card li { font-size: 13px; color: var(--text-2); }
.card ol, .card ul { padding-left: 18px; margin: 6px 0; }
.card .tagline { font-size: 12px; color: var(--muted); }
.card .why { font-size: 12px; color: var(--muted); border-top: 1px dashed var(--border); padding-top: 6px; margin-top: 6px; }
.section-title { font-size: 16px; margin: 18px 0 10px; }
.pill { display: inline-block; font-size: 11.5px; padding: 1px 8px; border-radius: 10px; background: var(--surface-3); color: var(--text-2); margin: 2px 4px 2px 0; }
.pill.ok { background: rgba(12, 163, 12, 0.15); color: #6fdc6f; }
.pill.todo { background: rgba(57, 135, 229, 0.15); color: #8dbdf5; }
.rec { border-left: 3px solid var(--serious); }
.plan-block { margin-bottom: 10px; }
.plan-block h4 { font-size: 13px; color: var(--text); margin-bottom: 4px; }

/* ---------- settings ---------- */
.form { display: grid; grid-template-columns: 200px 1fr; gap: 10px 16px; align-items: center; max-width: 760px; }
.form label { color: var(--text-2); }
.form .help { grid-column: 2; font-size: 12px; color: var(--muted); margin-top: -6px; }
@media (max-width: 640px) { .form { grid-template-columns: 1fr; } .form .help { grid-column: 1; } }
.calib-step { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin: 6px 0; }

.modal-back { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.55); z-index: 40; display: grid; place-items: center; padding: 16px; }
.modal { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 18px; max-width: 720px; width: 100%; max-height: 90vh; overflow: auto; }
.disclaimer { font-size: 12px; color: var(--muted); border: 1px dashed var(--border); padding: 8px 10px; border-radius: 8px; margin-top: 12px; }

/* help */
.help-toggle {
  margin-left: auto; background: none; border: 1px solid var(--border); color: var(--text-2);
  border-radius: 12px; padding: 1px 10px; font-size: 12px; cursor: pointer;
}
.help-toggle:hover, .help-toggle.on { color: var(--text); border-color: var(--accent); }
.help-box {
  background: #151b28; border: 1px solid #243150; border-radius: 8px; padding: 8px 12px; margin-bottom: 10px;
  font-size: 12.5px; color: var(--text-2); line-height: 1.6;
}
.help-box p { margin: 4px 0; }
.help-box ul { margin: 2px 0 6px; padding-left: 18px; }
.help-box b { color: var(--text); }
.feed-help .ph { display: flex; align-items: baseline; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.feed-help .ph h3 { font-size: 13px; }
.feed-help .ph .sub { font-size: 12px; color: var(--muted); }
.mplain { font-size: 11px; color: var(--muted); margin-top: -1px; min-height: 16px; }

/* guide */
.guide { margin-bottom: 12px; border-color: #2a3b5c; background: linear-gradient(180deg, #131a28, var(--surface)); }
.guide-steps { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 10px; margin: 4px 0 0; padding: 0; list-style: none; counter-reset: g; }
.guide-steps li { counter-increment: g; background: var(--surface-2); border-radius: 8px; padding: 10px 12px 10px 40px; position: relative; font-size: 12.5px; color: var(--text-2); }
.guide-steps li::before {
  content: counter(g); position: absolute; left: 12px; top: 10px; width: 20px; height: 20px; border-radius: 50%;
  background: var(--accent); color: #fff; font-weight: 700; font-size: 12px; display: grid; place-items: center;
}
.guide-steps li b { display: block; color: var(--text); font-size: 13px; margin-bottom: 2px; }

/* review */
.review { display: grid; gap: 10px; }
.review h4 { font-size: 13px; margin: 0 0 4px; }
.review ul { margin: 0; padding-left: 0; list-style: none; }
.review li { font-size: 12.5px; color: var(--text-2); padding: 3px 0 3px 22px; position: relative; }
.review li.good::before { content: '✓'; position: absolute; left: 4px; color: #6fdc6f; font-weight: 700; }
.review li.zone::before { content: '◆'; position: absolute; left: 4px; color: var(--accent); font-size: 10px; top: 5px; }
.review li.note::before { content: 'i'; position: absolute; left: 6px; color: var(--muted); font-style: italic; font-weight: 700; }
.score[title] { cursor: help; }
.score .how { font-size: 11px; color: var(--muted); margin-top: 2px; line-height: 1.4; }
