:root {
  --bg: #0f1116;
  --bg2: #161922;
  --card: #1a1e29;
  --line: #2a3040;
  --text: #eef1f7;
  --muted: #8d95a8;
  --accent: #ff7a45;
  --accent2: #ffb347;
  --mid: #4fc3f7;
  --ok: #46d39a;
  --warn: #ff6a6a;
  --shadow: 0 10px 30px rgba(0,0,0,.35);
  --radius: 18px;
  --state: var(--muted);
}
[data-theme="light"] {
  --bg: #f3f4f8;
  --bg2: #e9ebf2;
  --card: #ffffff;
  --line: #dde1ea;
  --text: #171a22;
  --muted: #646c80;
  --ok: #1fae74;
  --shadow: 0 8px 24px rgba(30,40,70,.10);
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: radial-gradient(1200px 600px at 50% -200px, rgba(255,122,69,.13), transparent 70%), var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* ---------- header ---------- */
.top {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px;
  max-width: 1100px; margin: 0 auto; padding: 14px 18px;
  padding-top: max(14px, env(safe-area-inset-top));
}
.brand { display: flex; align-items: center; gap: 10px; font-size: 1.25rem; letter-spacing: .2px; }
.brand b { color: var(--accent); }
.top-actions { display: flex; gap: 8px; justify-self: end; }
.maker-logo {
  justify-self: center; display: block; background: #fff; border-radius: 12px; padding: 5px 12px;
  box-shadow: 0 2px 10px rgba(0,0,0,.18); transition: transform .12s, box-shadow .12s; line-height: 0;
}
[data-theme="light"] .maker-logo { box-shadow: 0 0 0 1px var(--line); }
.maker-logo:hover { transform: translateY(-1px); box-shadow: 0 4px 16px rgba(79,195,247,.35); }
.maker-logo img { height: 36px; width: auto; display: block; }
.foot .dev { color: var(--text); }
.foot .dev a { color: var(--accent); font-weight: 600; text-decoration: none; }
.foot .dev a:hover { text-decoration: underline; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); display: grid; place-items: center; font-weight: 700;
}
.icon-btn:hover, .chip:hover { border-color: var(--accent); }

main { max-width: 1100px; margin: 0 auto; padding: 0 18px 20px; }

.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
}

/* ---------- stage ---------- */
.stage { padding: 16px 20px 18px; text-align: center; position: relative; overflow: hidden; transition: box-shadow .25s; }
body.intune .stage { box-shadow: var(--shadow), 0 0 0 2px color-mix(in srgb, var(--ok) 70%, transparent), 0 0 40px color-mix(in srgb, var(--ok) 25%, transparent); }
.stage-top { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.pill { padding: 3px 11px; border-radius: 99px; background: var(--bg2); border: 1px solid var(--line); color: var(--muted); font-size: .82rem; }

.strings { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; margin: 12px auto 2px; }
.str {
  min-width: 54px; padding: 6px 10px 5px; border-radius: 14px; border: 1px solid var(--line);
  background: var(--bg2); display: flex; flex-direction: column; align-items: center; gap: 1px;
  position: relative; transition: border-color .12s, background .12s, transform .08s;
}
.str b { font-size: 1.15rem; line-height: 1.1; }
.str b sup { font-size: .65em; }
.str small { font-size: .66rem; color: var(--muted); }
.str.auto { border-color: color-mix(in srgb, var(--accent) 70%, var(--line)); background: color-mix(in srgb, var(--accent) 14%, var(--bg2)); }
.str.locked { border-color: var(--accent); background: var(--accent); color: #fff; }
.str.locked small { color: #fff; opacity: .85; }
.str.done::after {
  content: "✓"; position: absolute; top: -7px; right: -6px; width: 18px; height: 18px; border-radius: 50%;
  background: var(--ok); color: #fff; font-size: .7rem; display: grid; place-items: center; font-weight: 800;
}
.hint { margin: 4px 0 0; font-size: .76rem; color: var(--muted); }

.meter { position: relative; max-width: 560px; margin: 4px auto 0; }
#gauge { width: 100%; height: auto; display: block; overflow: visible; }
#gauge .tick { stroke: var(--line); stroke-width: 2; stroke-linecap: round; }
#gauge .tick.major { stroke: var(--muted); stroke-width: 3; }
#gauge .tick.zero { stroke: var(--ok); stroke-width: 4; }
#gauge .tlabel { fill: var(--muted); font-size: 13px; text-anchor: middle; font-variant-numeric: tabular-nums; }
#gauge .zone { fill: color-mix(in srgb, var(--ok) 22%, transparent); }
.needle line { stroke: var(--state); stroke-width: 5; stroke-linecap: round; transition: stroke .2s; }
.needle circle { fill: var(--state); transition: fill .2s; }
.needle { will-change: transform; }
body.idle .needle { opacity: .35; }
body.intune { --state: var(--ok); }
body.off { --state: var(--accent); }
body.far { --state: var(--warn); }

.strobe { width: 100%; height: auto; aspect-ratio: 760 / 200; display: block; border-radius: 14px; background: var(--bg2); border: 1px solid var(--line); margin: 10px 0 4px; }

.readout { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 10px; margin-top: -6px; }
.note {
  font-size: 5.4rem; font-weight: 800; line-height: 1; letter-spacing: -2px; min-width: 190px;
  color: var(--text); transition: color .2s, text-shadow .2s; white-space: nowrap;
}
body.intune .note { color: var(--ok); text-shadow: 0 0 24px color-mix(in srgb, var(--ok) 55%, transparent); }
body.idle .note { opacity: .5; }
.note sup { font-size: .42em; vertical-align: top; position: relative; top: .1em; margin-left: 2px; letter-spacing: 0; }
.note sub { font-size: .34em; color: var(--muted); font-weight: 600; letter-spacing: 0; margin-left: 2px; }
.dir { display: flex; align-items: center; gap: 6px; font-weight: 700; color: var(--line); font-size: 1rem; transition: color .15s; }
.dir .arr { font-size: 1.4rem; }
.dir-flat { justify-content: flex-end; }
.dir-sharp { justify-content: flex-start; }
.dir.on { color: var(--state); }

.nums { display: flex; justify-content: center; gap: 22px; color: var(--muted); font-size: .95rem; margin-top: 6px; }
.nums b { color: var(--text); font-size: 1.25rem; font-variant-numeric: tabular-nums; }
.advice { margin: 8px 0 4px; min-height: 1.4em; font-weight: 600; color: var(--state); }
body.idle .advice { color: var(--muted); font-weight: 500; }

.controls { display: flex; align-items: center; justify-content: center; gap: 22px; margin: 10px 0 12px; }
.play {
  width: 96px; height: 96px; border-radius: 50%; border: 0;
  background: linear-gradient(145deg, var(--accent2), var(--accent)); color: #fff;
  box-shadow: 0 10px 30px rgba(255,122,69,.45); display: grid; place-items: center;
  transition: transform .08s;
}
.play:active { transform: scale(.95); }
.play svg { width: 46px; height: 46px; }
.play .i-stop { display: none; }
body.listening .play .i-stop { display: block; }
body.listening .play .i-play { display: none; }
body.listening .play { background: linear-gradient(145deg, #ff6a6a, #e0445a); box-shadow: 0 10px 30px rgba(224,68,90,.45); }
body.listening .play::before {
  content: ""; position: absolute; width: 96px; height: 96px; border-radius: 50%;
  border: 2px solid #ff6a6a; animation: pulse 1.6s ease-out infinite; pointer-events: none;
}
.play { position: relative; }
@keyframes pulse { from { transform: scale(1); opacity: .7; } to { transform: scale(1.45); opacity: 0; } }
.tap {
  width: 76px; height: 76px; border-radius: 22px; border: 1px solid var(--line);
  background: var(--bg2); font-weight: 800; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 4px; user-select: none;
}
.tap small { font-weight: 500; font-size: .7rem; color: var(--muted); }
.tap:active { border-color: var(--accent); }
.level-wrap { width: 76px; height: 76px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; }
.level-wrap small { font-size: .7rem; color: var(--muted); }
.level { position: relative; width: 16px; height: 52px; border-radius: 8px; background: var(--bg2); border: 1px solid var(--line); overflow: hidden; }
#levelBar { position: absolute; left: 0; right: 0; bottom: 0; height: 0; background: linear-gradient(to top, var(--ok), var(--accent2) 70%, var(--warn)); transition: height .06s linear; }
#gateMark { position: absolute; left: -2px; right: -2px; height: 2px; background: var(--text); opacity: .6; }

.history { width: 100%; height: 90px; display: block; border-radius: 12px; background: var(--bg2); border: 1px solid var(--line); }

/* ---------- panels ---------- */
.panels { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; margin-top: 16px; }
.panel { padding: 16px 18px 18px; }
.panel h2 { margin: 0 0 6px; font-size: 1.05rem; }
.lbl { display: flex; justify-content: space-between; margin: 14px 0 6px; font-size: .85rem; color: var(--muted); }
.lbl b { color: var(--text); font-weight: 600; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line); background: var(--bg2); border-radius: 10px; padding: 7px 11px;
  font-size: .9rem; line-height: 1.1;
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

.sig { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.stepper { display: flex; align-items: center; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--bg2); }
.stepper button { width: 40px; height: 42px; border: 0; background: transparent; font-size: 1.3rem; user-select: none; }
.stepper span { min-width: 46px; text-align: center; font-size: 1.3rem; font-weight: 700; font-variant-numeric: tabular-nums; }
select {
  background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; height: 44px; padding: 0 12px; font-size: 1rem;
}
select.wide { width: 100%; }
select:focus, input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 1px; }
.slider { width: 100%; accent-color: var(--accent); height: 28px; }

.toggles { display: grid; gap: 10px; }
.switch { display: flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch span {
  width: 42px; height: 24px; border-radius: 99px; background: var(--line); position: relative; flex: none;
  transition: background .15s;
}
.switch span::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%;
  background: #fff; transition: transform .15s;
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(18px); }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch em { font-style: normal; }

.btn {
  border: 0; border-radius: 12px; padding: 9px 16px; background: var(--accent); color: #fff; font-weight: 600;
}
.btn.ghost { background: var(--bg2); color: var(--text); border: 1px solid var(--line); margin-left: auto; }

/* reference tone */
.ref-strings { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 4px; }
.ref-strings .chip.playing, .piano .key.playing { background: var(--accent); border-color: var(--accent); color: #fff; }
.piano { position: relative; height: 120px; margin: 12px 0 10px; display: flex; user-select: none; touch-action: manipulation; }
.piano .key { border: 1px solid var(--line); font-size: .72rem; display: flex; align-items: flex-end; justify-content: center; padding-bottom: 6px; }
.piano .white { flex: 1; background: #f7f8fb; color: #555c6c; border-radius: 0 0 8px 8px; margin-right: -1px; height: 100%; }
.piano .black {
  position: absolute; top: 0; width: 9%; height: 62%; background: #1b1f29; color: #9aa2b4; border-color: #000;
  border-radius: 0 0 6px 6px; z-index: 2; margin-left: -4.5%; font-size: .62rem;
}
.piano .white.playing { background: var(--accent); }
.ref-row { display: flex; align-items: center; gap: 10px; }

.steps { margin: 8px 0 10px; padding-left: 20px; display: grid; gap: 7px; line-height: 1.4; }

.foot { text-align: center; padding: 10px 18px 30px; color: var(--muted); font-size: .9rem; }
.foot p { margin: 4px 0; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: .92rem;
  transition: .2s; pointer-events: none; z-index: 60; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

dialog {
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); color: var(--text);
  max-width: 460px; width: calc(100% - 32px); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0,0,0,.5); }
dialog h3 { margin-top: 0; }
.keys { width: 100%; border-collapse: collapse; margin-bottom: 10px; }
.keys td { padding: 6px 4px; border-bottom: 1px solid var(--line); }
kbd {
  display: inline-block; padding: 2px 7px; border-radius: 6px; border: 1px solid var(--line);
  background: var(--bg2); font-size: .82rem; font-family: inherit;
}

/* ---------- responsive ---------- */
@media (max-width: 760px) {
  .panels { grid-template-columns: 1fr; }
}
@media (max-width: 560px) {
  .top { grid-template-columns: 1fr auto; row-gap: 10px; }
  .maker-logo { grid-column: 1 / -1; grid-row: 1; }
  .maker-logo img { height: 30px; }
  .brand { font-size: 1.1rem; white-space: nowrap; gap: 8px; }
  .brand img { width: 30px; height: 30px; }
}
@media (max-width: 480px) {
  main { padding: 0 10px 16px; }
  .stage { padding: 12px 10px 14px; }
  .note { font-size: 4.2rem; min-width: 130px; }
  .dir { font-size: .85rem; gap: 3px; }
  .dir .arr { font-size: 1.1rem; }
  .str { min-width: 44px; padding: 5px 7px 4px; }
  .controls { gap: 16px; }
  .play { width: 86px; height: 86px; }
  body.listening .play::before { width: 86px; height: 86px; }
  .tap, .level-wrap { width: 68px; height: 68px; }
  .nums { gap: 14px; }
}
@media (prefers-reduced-motion: reduce) {
  body.listening .play::before { animation: none; }
}
