/* iPhone-first, Apple system colours, light / dark, safe areas. */
:root {
  --bg: #F2F2F7; --card: #FFFFFF; --fill: #E9E9EB; --text: #1D1D1F; --muted: #6E6E73; --sep: #E5E5EA;
  --accent: #007AFF; --accent-soft: #E5F0FF; --ok: #248A3D; --ok-fill: #34C759; --ok-soft: #E8F7EC;
  --bad: #D70015; --bad-fill: #FF3B30; --bad-soft: #FFECEB; --warn-soft: #FFF4E5; --warn: #C93400;
  --key-white: #FFFFFF; --key-black: #1D1D1F; --key-bed: #D1D1D6;
  --kb-height: clamp(170px, 30vh, 250px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000; --card: #1C1C1E; --fill: #2C2C2E; --text: #F5F5F7; --muted: #98989D; --sep: #38383A;
    --accent: #0A84FF; --accent-soft: #0B2A4A; --ok: #30D158; --ok-fill: #30D158; --ok-soft: #0F2E19;
    --bad: #FF453A; --bad-fill: #FF453A; --bad-soft: #3A1614; --warn-soft: #3A2A0C; --warn: #FF9F0A;
    --key-white: #E5E5EA; --key-black: #111113; --key-bed: #1C1C1E;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "PingFang TC", "Helvetica Neue", sans-serif;
  background: var(--bg); color: var(--text);
  display: flex; flex-direction: column; overflow: hidden;
  padding-top: env(safe-area-inset-top);
  -webkit-user-select: none; user-select: none;
}
button { font: inherit; color: inherit; border: none; background: none; cursor: pointer; touch-action: manipulation; }
button:active { transform: scale(0.97); }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* header */
.top { display: flex; align-items: center; gap: 10px; padding: 10px 16px 6px; }
.top h1 { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; margin: 0; flex: 1; }
.pill { background: var(--fill); border-radius: 999px; padding: 4px 12px; font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }
.icon-btn { width: 38px; height: 38px; border-radius: 19px; background: var(--fill); font-size: 19px; }

/* drill chips */
.drills { display: flex; gap: 8px; overflow-x: auto; padding: 6px 16px 10px; scrollbar-width: none; scroll-snap-type: x proximity; }
.drills::-webkit-scrollbar { display: none; }
.chip { flex: none; scroll-snap-align: center; padding: 8px 14px; border-radius: 999px; background: var(--card); font-size: 15px; font-weight: 500; white-space: nowrap; box-shadow: 0 0 0 1px var(--sep) inset; }
.chip[aria-pressed="true"] { background: var(--accent); color: #fff; box-shadow: none; font-weight: 600; }

/* main card */
.scroll { flex: 1; overflow-y: auto; padding: 0 16px 12px; -webkit-overflow-scrolling: touch; }
.card { background: var(--card); border-radius: 16px; padding: 16px; }
.prompt { margin: 0 0 14px; font-size: 17px; font-weight: 600; line-height: 1.4; }
.play-row { display: flex; align-items: center; gap: 14px; }
.play { flex: none; width: 64px; height: 64px; border-radius: 32px; background: var(--accent); color: #fff; font-size: 26px; padding-left: 4px; box-shadow: 0 6px 16px rgba(0,122,255,.28); }
.tools { display: flex; flex-wrap: wrap; gap: 8px; }
.tool { padding: 8px 12px; border-radius: 10px; background: var(--fill); font-size: 15px; }
.tool.danger { color: var(--bad); }

.options { margin-top: 14px; border-top: 1px solid var(--sep); padding-top: 10px; }
.options summary { font-weight: 600; color: var(--accent); cursor: pointer; padding: 4px 0; }
.options label { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--sep); font-size: 15px; }
.options label:last-child { border-bottom: none; }
.options select { font: inherit; font-size: 15px; color: var(--accent); background: transparent; border: none; text-align: right; max-width: 60%; }
.options .switch { justify-content: flex-start; }
input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--accent); }

.choices { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-top: 16px; }
.choices[data-count="0"] { display: none; }
.choices[data-count="12"] { grid-template-columns: repeat(3, 1fr); }
.choice { padding: 12px 6px; border-radius: 12px; background: var(--fill); font-size: 16px; font-weight: 500; min-height: 48px; }
.choice.right { background: var(--ok-fill); color: #fff; }
.choice.wrong { background: var(--bad-fill); color: #fff; }

.hint { margin: 12px 0 0; color: var(--warn); font-size: 15px; min-height: 0; }
.hint:empty { display: none; }
.result { margin-top: 12px; padding: 12px 14px; border-radius: 12px; background: var(--fill); font-size: 15px; min-height: 48px; }
.result[data-state="ok"] { background: var(--ok-soft); }
.result[data-state="ok"] b { color: var(--ok); }
.result[data-state="bad"] { background: var(--bad-soft); }
.result[data-state="bad"] b { color: var(--bad); }
.result[data-state="hint"] { background: var(--warn-soft); }
.next-row { display: flex; align-items: center; justify-content: space-between; margin-top: 14px; }
.primary { background: var(--accent); color: #fff; font-weight: 600; padding: 12px 22px; border-radius: 12px; font-size: 17px; }
.primary.big { width: 100%; padding: 15px; font-size: 18px; }

/* keyboard */
.keyboard { flex: none; background: var(--key-bed); padding: 6px 6px calc(6px + env(safe-area-inset-bottom)); border-top: 1px solid var(--sep); }
.kb-bar { display: flex; align-items: center; gap: 8px; }
.kb-oct { width: 40px; height: 30px; border-radius: 8px; background: var(--card); font-size: 22px; line-height: 1; }
.kb-overview { position: relative; flex: 1; height: 22px; border-radius: 6px; touch-action: none;
  background: repeating-linear-gradient(90deg, var(--key-white) 0 calc(100% / 35 - 1px), var(--sep) calc(100% / 35 - 1px) calc(100% / 35)); }
.kb-window { position: absolute; top: -2px; bottom: -2px; border: 2px solid var(--accent); border-radius: 6px; background: rgba(0,122,255,.12); pointer-events: none; transition: left .18s ease-out; }
.kb-dot { position: absolute; top: 50%; width: 8px; height: 8px; margin: -4px 0 0 -4px; border-radius: 4px; background: var(--ok-fill); pointer-events: none; }
.kb-range { text-align: center; font-size: 12px; color: var(--muted); margin: 3px 0; font-variant-numeric: tabular-nums; }
.kb-keys { position: relative; height: var(--kb-height); touch-action: none; }
.kb-key { position: absolute; top: 0; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; }
.kb-key.white { height: 100%; background: var(--key-white); border-radius: 0 0 7px 7px; box-shadow: inset 0 0 0 1px rgba(0,0,0,.18); padding-bottom: 6px; }
.kb-key.black { height: 60%; background: var(--key-black); border-radius: 0 0 5px 5px; z-index: 2; padding-bottom: 5px; }
.kb-key.down.white { background: #CFE3FF; }
.kb-key.down.black { background: #3A5A8C; }
.kb-key.m-answer.white { background: var(--ok-fill); }
.kb-key.m-answer.black { background: #1F8A3F; }
.kb-key.m-wrong.white { background: var(--bad-fill); }
.kb-key.m-wrong.black { background: #B3261E; }
.kb-c { font-size: 11px; color: #8E8E93; font-weight: 600; }
.kb-key.m-answer .kb-c, .kb-key.m-wrong .kb-c { color: rgba(255,255,255,.9); }
.kb-label { font-size: 15px; font-weight: 700; color: #fff; margin-bottom: 4px; }

/* sound was suspended by iOS (app switch): one tap brings it back */
.sound-off { position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 64px); transform: translateX(-50%); z-index: 9;
  background: var(--text); color: var(--bg); font-weight: 600; font-size: 15px; padding: 10px 18px; border-radius: 999px;
  box-shadow: 0 6px 20px rgba(0,0,0,.25); }

/* splash */
.splash { position: fixed; inset: 0; z-index: 10; background: color-mix(in srgb, var(--bg) 92%, transparent); -webkit-backdrop-filter: blur(20px); backdrop-filter: blur(20px); display: grid; place-items: center; padding: 24px; }
.splash[hidden] { display: none; }
.splash-card { background: var(--card); border-radius: 22px; padding: 28px 24px; max-width: 360px; text-align: center; }
.splash-icon { width: 72px; height: 72px; margin: 0 auto 10px; border-radius: 18px; background: var(--accent); color: #fff; font-size: 40px; display: grid; place-items: center; }
.splash-card h2 { margin: 0 0 6px; font-size: 24px; letter-spacing: -0.01em; }
.splash-card p { color: var(--muted); font-size: 15px; margin: 0 0 20px; }

/* settings sheet */
.sheet { border: none; border-radius: 18px; padding: 18px; width: min(420px, calc(100vw - 32px)); background: var(--card); color: var(--text); }
.sheet::backdrop { background: rgba(0,0,0,.35); }
.sheet header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.sheet h2 { font-size: 20px; margin: 0; }
.sheet h3 { font-size: 15px; color: var(--muted); font-weight: 600; margin: 18px 0 6px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 10px 0; }
.row input[type="range"] { flex: 1; accent-color: var(--accent); }
.text-btn { color: var(--accent); font-weight: 600; padding: 6px 4px; }
.text-btn.danger { color: var(--bad); }
.stats { list-style: none; padding: 0; margin: 0; }
.stats li { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--sep); font-variant-numeric: tabular-nums; }
#midi-status { text-align: right; font-size: 14px; }
#midi-status[data-state="connected"] { color: var(--ok); }

/* landscape: shorter keyboard, more keys */
@media (orientation: landscape) and (max-height: 500px) {
  :root { --kb-height: 42vh; }
  .top h1 { font-size: 22px; }
  .prompt { font-size: 16px; }
}
@media (prefers-reduced-motion: reduce) {
  .kb-window { transition: none; }
  button:active { transform: none; }
}
