/* Mobile-first, compact. Light and dark themes (html[data-theme], set by js/enter.js). */
/* Everything in Google Sans, Thai included: Google Sans Flex for Latin, then the full Google Sans family,
   which the browser uses per character for Thai (system Thai font only if a character is missing). */
@import url("https://fonts.googleapis.com/css2?family=Google+Sans:wght@400;500;600;700&display=swap");
:root {
  color-scheme: light;
  --on-fill: #ffffff;  /* text on accent / right / wrong fills */
  --key-white: #ffffff;
  --key-ink: #14171c;  /* text on white piano keys (same in both themes) */
  --bg: #f6f7f9;
  --card: #ffffff;
  --line: #dfe2e8;
  --track: #e8eaef;
  --text: #14171c;
  --muted: #5d6573;
  --accent: #0b57d0;
  --accent-soft: #e8f0fe;
  --right: #146c2e;
  --wrong: #b3261e;
  --wrong-stripe: #e88a84; /* lighter red for the "wrong" stripes in charts */
  --miss: #f2b600;    /* yellow: a hit that was missed */
  --miss-bg: #fff7d6;
  --key-black: #14171c;
  --key-edge: #8a8f99;
  --hit: #f29900; /* amber: key currently sounding */
  --r: 12px;
  /* Difficulty colours, easiest to hardest */
  --lv-0: #188038; /* green */
  --lv-1: #0b57d0; /* blue */
  --lv-2: #b45309; /* orange */
  --lv-3: #c5221f; /* red */
  --lv-4: #7b1fa2; /* purple */
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --on-fill: #0b0d12;
  --key-white: #e9ebf0;
  --bg: #0e1015;
  --card: #181b22;
  --line: #2a2f3a;
  --track: #232731;
  --text: #e8eaee;
  --muted: #9aa3b2;
  --accent: #7aa2ff;
  --accent-soft: #1d2740;
  --right: #5fd18a;
  --wrong: #ff8a80;
  --wrong-stripe: #8c3a35;
  --miss: #f2c94c;
  --miss-bg: #3a3214;
  --key-black: #08090c;
  --key-edge: #4a4f5c;
  --lv-0: #2e9e4f;
  --lv-1: #3f78ea;
  --lv-2: #c9711a;
  --lv-3: #d9443c;
  --lv-4: #9a4fd1;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

body {
  font-family: "Google Sans Flex", "Google Sans", "Product Sans", system-ui, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  color: var(--text);
  background: var(--bg);
  max-width: 520px;
  margin: 0 auto;
  padding: 10px 12px max(12px, env(safe-area-inset-bottom));
}

h1 { font-size: 1.375rem; margin: 0; letter-spacing: -0.01em; }
p { margin: 4px 0; }
a { color: var(--accent); }
[hidden] { display: none !important; }
.muted { color: var(--muted); font-size: 0.875rem; }
.small { font-size: 0.8125rem; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* Top bar: home button + dropdown menu (kept above <main> so the open menu isn't covered) */
.site-header { position: relative; z-index: 10; }
.menu { position: relative; display: flex; gap: 8px; }
.menu .home {
  display: grid; place-items: center; width: 44px; height: 44px; flex: none;
  border-radius: 999px; background: var(--card); border: 1px solid var(--line); color: var(--muted);
}
.menu .home[aria-current="page"] { color: var(--accent); }
.theme-btn {
  flex: none; display: grid; place-items: center; width: 44px; height: 44px; padding: 0;
  border-radius: 999px; color: var(--muted);
}
.theme-btn:hover { color: var(--text); }
/* EN / ไทย switch, next to the theme button */
.lang-btn {
  flex: none; min-width: 52px; height: 44px; padding: 0 12px; border-radius: 999px;
  color: var(--text); font-weight: 600; font-size: 0.875rem;
}
.lang-btn:hover { border-color: var(--muted); }

/* Material-style outlined text field with a floating label (name on the share image) */
.mfield { position: relative; margin: 4px 0 2px; text-align: left; }
.mfield input {
  width: 100%; height: 52px; padding: 18px 14px 6px; font: inherit; font-size: 1rem; color: var(--text);
  background: var(--card); border: 1px solid var(--line); border-radius: var(--r); outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.mfield label {
  position: absolute; left: 14px; top: 15px; color: var(--muted); font-size: 1rem; pointer-events: none;
  min-height: 0; display: block; transform-origin: left top;
  transition: transform 0.15s ease, color 0.15s ease;
}
.mfield input:focus { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.mfield input:focus + label, .mfield input:not(:placeholder-shown) + label { transform: translateY(-9px) scale(0.75); }
.mfield input:focus + label { color: var(--accent); }
/* Buttons with a leading icon (results popup) */
.ibtn { display: flex; align-items: center; justify-content: center; gap: 8px; }

/* Home page: share-the-app button, version badge (bottom-right) and version history popup */
/* Quiet, small pill: present but not competing with the category cards */
.share-app {
  display: flex; align-items: center; justify-content: center; gap: 6px; margin: 20px auto 0;
  min-height: 36px; padding: 6px 14px; border-radius: 999px;
  font-size: 0.8125rem; font-weight: 500; color: var(--muted); background: transparent;
}
.share-app:hover { color: var(--text); border-color: var(--muted); }
.share-app img { display: block; width: 20px; height: 20px; object-fit: contain; }
.share-msg { text-align: center; color: var(--muted); font-size: 0.8125rem; min-height: 1.2em; margin: 6px 0 0; }
.ver-btn {
  position: fixed; right: 12px; bottom: max(12px, env(safe-area-inset-bottom)); z-index: 12;
  min-height: 32px; padding: 4px 12px; border-radius: 999px; font-size: 0.75rem; font-weight: 600;
  color: var(--muted); background: var(--card); box-shadow: 0 2px 8px rgb(0 0 0 / 0.12);
  font-family: ui-monospace, Menlo, Consolas, monospace;
}
.ver-btn:hover { color: var(--text); }
.ver-list { list-style: none; margin: 12px 0 8px; padding: 0; text-align: left; }
.ver-list li { padding: 10px 0; border-top: 1px solid var(--line); }
.ver-list h3 { margin: 0 0 6px; font-size: 1rem; }
.ver-list h3 small { color: var(--muted); font-weight: 400; font-family: ui-monospace, Menlo, Consolas, monospace; margin-left: 6px; }
.ver-list ul { margin: 0; padding-left: 18px; color: var(--muted); font-size: 0.875rem; line-height: 1.5; }

/* Smooth colour change when switching theme */
body, .cat, .menu-btn, .card, button { transition: background-color 0.25s ease, color 0.25s ease, border-color 0.25s ease; }

/* Footer credit on the home page */
.credit { margin: 28px 0 8px; text-align: center; color: var(--muted); font-size: 0.8125rem; line-height: 1.6; }
.credit a { color: var(--text); font-weight: 600; text-decoration: none; border-bottom: 1px solid var(--line); }
.credit a:hover { color: var(--accent); border-bottom-color: var(--accent); }
.credit .korn { font-family: "Orbitron", "Google Sans Flex", sans-serif; font-weight: 900; letter-spacing: 0.05em; }
.menu-btn {
  flex: 1; display: flex; align-items: center; justify-content: space-between;
  border-radius: 999px; padding: 8px 16px; font-weight: 600;
}
.menu-btn .chev { color: var(--muted); transition: transform 0.25s ease; }
.menu-btn { padding-left: 18px; }
.menu-btn.has-ico { padding-left: 6px; } /* the round icon sits close to the edge */
.mb-label { display: flex; align-items: center; gap: 10px; }
.mb-label .mi { width: 32px; height: 32px; border-radius: 999px; font-size: 0.625rem; }
.menu-btn[aria-expanded="true"] .chev { transform: rotate(180deg); }
.menu-list {
  position: absolute; top: calc(100% + 6px); left: 52px; right: 0; z-index: 20;
  list-style: none; margin: 0; padding: 6px; background: var(--card);
  border: 1px solid var(--line); border-radius: 16px; box-shadow: 0 12px 32px rgb(0 0 0 / 0.14);
  transform-origin: top center;
  /* Closed: invisible, then hidden once the fade-out has finished */
  opacity: 0; transform: translateY(-8px) scale(0.97); visibility: hidden; pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease, visibility 0s linear 0.2s;
}
.menu-list.open {
  opacity: 1; transform: none; visibility: visible; pointer-events: auto;
  transition: opacity 0.25s ease, transform 0.3s cubic-bezier(0.2, 0.9, 0.3, 1.15), visibility 0s;
}
/* Items slide in one after another */
.menu-list li { opacity: 0; transform: translateY(-4px); transition: opacity 0.15s ease, transform 0.15s ease; }
.menu-list.open li {
  opacity: 1; transform: none;
  transition: opacity 0.25s ease, transform 0.25s ease; transition-delay: calc(var(--d) * 30ms + 40ms);
}

/* Page enter (only when coming from the home page, see js/enter.js) / leave */
html.enter main, html.enter .site-header { animation: page-in 0.4s ease-out both; }
@keyframes page-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
body.leaving main, body.leaving .site-header { opacity: 0; transform: translateY(-6px); transition: opacity 0.2s ease, transform 0.2s ease; animation: none; }
.menu-list a {
  display: flex; align-items: center; gap: 12px; padding: 7px 10px; border-radius: 10px;
  color: var(--text); text-decoration: none; font-weight: 500;
}
/* Small coloured icon tile in the menu, same as the home cards */
.mi {
  flex: none; display: grid; place-items: center; width: 34px; height: 34px; border-radius: 10px;
  background: var(--c); color: #fff; font-size: 0.6875rem; font-weight: 700; letter-spacing: -0.01em;
}
.menu-list a:hover, .menu-list a:focus-visible { background: var(--track); outline: none; }
.menu-list a[aria-current="page"] { color: var(--accent); font-weight: 600; }

/* Home page */
.hero { margin: 24px 0 16px; }
.hero h1 { font-size: 2rem; }
.cats { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.cat {
  display: flex; align-items: center; gap: 14px; padding: 14px;
  background: var(--card); border-radius: 16px; text-decoration: none; color: var(--text);
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 4px 14px rgb(0 0 0 / 0.06);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.cat:hover { transform: translateY(-2px); box-shadow: 0 2px 4px rgb(0 0 0 / 0.08), 0 10px 24px rgb(0 0 0 / 0.1); }
.cat:active { transform: scale(0.98); }
.cat:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
/* Coloured icon tile, colour from the lv-N class */
.cat-ico {
  flex: none; display: grid; place-items: center; width: 52px; height: 52px; border-radius: 14px;
  background: var(--c); color: #fff; font-weight: 700; font-size: 0.875rem; letter-spacing: -0.01em;
}
.cat-text { flex: 1; min-width: 0; }
.cat-name { display: block; font-weight: 700; font-size: 1.0625rem; }
.cat-name .start {
  display: inline-block; vertical-align: 2px; margin-left: 6px; padding: 1px 8px; border-radius: 999px;
  background: var(--c); color: #fff; font-size: 0.6875rem; font-weight: 700; letter-spacing: 0.02em;
}
.cat-desc { display: block; color: var(--muted); font-size: 0.8125rem; margin-top: 2px; text-wrap: pretty; }
.cat-go { flex: none; color: var(--c); transition: transform 0.15s ease; } /* same colour as the card's icon tile */
.cat:hover .cat-go { transform: translateX(3px); color: var(--c); }

/* Super hard intervals: Quality + Number dropdowns */
.custombox { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.pickers { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
label.pick { display: block; min-height: 0; }
.pick select {
  width: 100%; min-height: 44px; font: inherit; color: var(--text); background: var(--card);
  border: 1px solid var(--line); border-radius: var(--r); padding: 8px 10px;
}
.pick select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
#checkBtn, #rCheck { width: 100%; margin-top: 10px; }

/* Rhythm: one box per beat / part of a beat */
.rgrid { display: grid; gap: 5px; margin-top: 8px; }
.rcell {
  min-height: 56px; padding: 4px 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; border-radius: 10px; transition: background-color 0.12s ease, color 0.12s ease;
}
.rcell.beat { border-color: var(--key-edge); }
.rcell .sym { font-size: 1.125rem; line-height: 1; letter-spacing: -0.12em; white-space: nowrap; }
.rcell .cnt { font-size: 0.6875rem; color: var(--muted); font-family: ui-monospace, Menlo, Consolas, monospace; }
.rcell.hit { background: var(--text); border-color: var(--text); color: var(--bg); }
.rcell.hit .cnt { color: var(--bg); opacity: 0.7; }
.rcell.now { box-shadow: 0 0 0 3px var(--hit); }
.rcell.good { outline: 2px solid var(--right); outline-offset: 1px; }
.rcell.bad { outline: 2px solid var(--wrong); outline-offset: 1px; }
/* Missed: should have sounded but was left empty */
.rcell.miss { outline: 3px solid var(--miss); outline-offset: 1px; background: var(--miss-bg); }
.rcell.miss.hit { background: var(--text); }
.rview { margin-top: 10px; }

/* Visual metronome: four beat dots */
.metro { display: flex; justify-content: center; gap: 14px; margin: 8px 0 4px; }
.metro span {
  display: grid; place-items: center; width: 30px; height: 30px; border-radius: 50%;
  background: var(--track); color: var(--muted); font-size: 0.75rem; font-weight: 700;
  transition: background-color 0.08s ease, color 0.08s ease, transform 0.08s ease;
}
.metro span.count { background: var(--hit); color: var(--text); transform: scale(1.15); }
.metro span.on { background: var(--accent); color: var(--on-fill); transform: scale(1.15); }
@media (prefers-reduced-motion: reduce) { .metro span { transition: none; } .metro span.on, .metro span.count { transform: none; } }
.rcell.bad .sym { color: var(--wrong); }
.rcell.hit.bad .sym { color: var(--bg); }
.rcell:disabled { opacity: 1; cursor: default; }

/* Melody: tap the notes back */
.inputbox { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.taps { text-align: center; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 0.875rem; }
.keyboard.input { height: 120px; }
#undoBtn { width: 100%; margin-top: 10px; }
#fillUndo { width: 100%; margin-bottom: 10px; }

.titlebar { display: flex; align-items: center; justify-content: space-between; margin: 12px 0 4px; }
.timer {
  font-family: ui-monospace, Menlo, Consolas, monospace; font-weight: 600;
  min-width: 56px; text-align: center; padding: 4px 10px; border-radius: 999px;
  background: var(--card); border: 1px solid var(--line);
}
.timer.low { color: var(--wrong); border-color: var(--wrong); }

/* Buttons */
button {
  font: inherit; color: var(--text); background: var(--card);
  border: 1px solid var(--line); border-radius: var(--r);
  padding: 8px 12px; min-height: 44px; cursor: pointer; touch-action: manipulation;
}
button:active:not(:disabled) { transform: scale(0.98); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary { min-height: 48px; font-weight: 600; background: var(--accent); border-color: var(--accent); color: var(--on-fill); }
button.link { border: 0; background: none; color: var(--accent); min-height: 32px; padding: 4px 6px; font-weight: 500; }

/* Settings screen */
.field { margin: 12px 0 0; }
.flabel { display: block; color: var(--muted); font-size: 0.75rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 4px; }
.begin { width: 100%; margin-top: 20px; font-size: 1.125rem; display: flex; align-items: center; justify-content: center; gap: 8px; }

/* Segmented control with a sliding thumb */
.seg { position: relative; display: flex; background: var(--track); border-radius: 999px; padding: 3px; }
.seg .thumb {
  position: absolute; top: 3px; bottom: 3px; left: 3px;
  width: calc((100% - 6px) / var(--n, 2));
  transform: translateX(calc(100% * var(--i, 0)));
  background: var(--card); border-radius: 999px; box-shadow: 0 1px 4px rgb(0 0 0 / 0.15);
  transition: transform 0.4s cubic-bezier(0.3, 1.4, 0.5, 1);
}
.seg button { position: relative; flex: 1; min-height: 36px; border: 0; background: transparent; border-radius: 999px; color: var(--muted); font-weight: 500; transition: color 0.25s ease; }
.seg button[aria-pressed="true"] { color: var(--text); }
.seg button:active:not(:disabled) { transform: none; }

/* Blocks that fold open/shut when switching Practice / Test */
[data-test-only], [data-practice-only] {
  display: grid; grid-template-rows: 1fr; opacity: 1;
  transition: grid-template-rows 0.4s ease, opacity 0.3s ease, margin-top 0.4s ease, transform 0.4s ease;
}
/* Small padding so shadows and focus rings aren't clipped by overflow: hidden */
[data-test-only] > .inner, [data-practice-only] > .inner { min-height: 0; overflow: hidden; padding: 3px; margin: -3px; }
.collapsed { grid-template-rows: 0fr; opacity: 0; margin-top: 0 !important; transform: translateY(-6px); }

/* Begin button turns into "Start test" with a little pop */
.begin { transition: background-color 0.3s ease, border-color 0.3s ease; }
.begin.test { background: var(--text); border-color: var(--text); }
.begin.pop { animation: pop 0.35s ease-out; }
@keyframes pop { 0% { transform: scale(0.96); } 60% { transform: scale(1.02); } 100% { transform: scale(1); } }

/* What's in the selected level, as chips */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chips .chip {
  display: inline-flex; align-items: baseline; justify-content: center; gap: 6px; text-align: center; padding: 3px 10px; border-radius: 999px;
  font-size: 0.8125rem; font-weight: 500; color: var(--c); background: var(--card); border: 1px solid var(--line);
  animation: chip-in 0.3s ease-out both; animation-delay: calc(var(--d) * 35ms);
}
.chips .chip small { font-size: 0.75rem; color: var(--muted); }
@keyframes chip-in { from { opacity: 0; transform: translateY(4px) scale(0.95); } to { opacity: 1; transform: none; } }

@media (prefers-reduced-motion: reduce) {
  .seg .thumb, [data-test-only], [data-practice-only] { transition: none; }
  .begin.pop, .chips .chip { animation: none; }
}

/* Difficulty pills, each with its own colour */
/* One row of pills; swipe sideways when they don't fit. Edges fade to hint there's more. */
.levelbar {
  display: flex; flex-wrap: nowrap; gap: 6px; overflow-x: auto; padding: 3px 2px;
  scroll-snap-type: x proximity; -webkit-overflow-scrolling: touch; scrollbar-width: none;
}
.levelbar::-webkit-scrollbar { display: none; }
.levelbar .lv { flex: none; scroll-snap-align: center; white-space: nowrap; }
.lv .flash, .pill .flash { text-transform: uppercase; font-size: 0.8125rem; }
.lv { --c: var(--lv-0); min-height: 36px; padding: 4px 14px; border-radius: 999px; font-weight: 500; color: var(--c); border-color: var(--c); }
.lv[aria-pressed="true"] { background: var(--c); color: #fff; }
.lv { transition: background-color 0.25s ease, color 0.25s ease; }
.lv-0 { --c: var(--lv-0); } .lv-1 { --c: var(--lv-1); } .lv-2 { --c: var(--lv-2); }
.lv-3 { --c: var(--lv-3); } .lv-4 { --c: var(--lv-4); }

.inv { display: flex; flex-wrap: wrap; gap: 4px 16px; border: 0; padding: 0; margin: 0; min-width: 0; }
label { display: flex; align-items: center; gap: 8px; min-height: 32px; font-size: 0.9375rem; }
input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--accent); }

/* Quiz screen */
.gamebar { display: flex; align-items: center; gap: 8px; }
.pill { background: var(--c, var(--lv-0)); color: #fff; border-radius: 999px; padding: 3px 12px; font-size: 0.875rem; font-weight: 600; }
.tag { color: var(--muted); font-size: 0.875rem; }
.gamebar .link { margin-left: auto; }

.score { display: flex; gap: 16px; color: var(--muted); font-size: 0.8125rem; margin: 8px 0 0; }
.score b { color: var(--text); }

.player { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 8px; }
.player.solo { grid-template-columns: 1fr; }
.player button { display: inline-flex; align-items: center; justify-content: center; gap: 8px; }

/* Speaker icon: waves are faint at rest and pulse while sound plays */
.ico { width: 20px; height: 20px; flex: none; }
.ico .body { fill: currentColor; }
.ico .w1, .ico .w2 { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; opacity: 0.35; }
.playing .ico .w1 { animation: wave 0.9s ease-in-out infinite; }
.playing .ico .w2 { animation: wave 0.9s ease-in-out 0.2s infinite; }
@keyframes wave { 0%, 100% { opacity: 0.25; } 50% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
  .playing .ico .w1, .playing .ico .w2 { animation: none; opacity: 1; }
}

/* Divider between the play buttons and the answers */
.answers { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin: 12px 0 8px; padding-top: 12px; border-top: 1px solid var(--line); }
.answers button { min-height: 44px; padding: 6px 8px; font-size: 0.9375rem; font-weight: 500; }
.answers .sub { display: block; font-size: 0.75rem; font-weight: 400; opacity: 0.7; }
.answers .right { background: var(--right); border-color: var(--right); color: var(--on-fill); }
.answers .wrong { background: var(--wrong); border-color: var(--wrong); color: var(--on-fill); }
.answers .sel { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Root note picker (chords, "Name the root too") */
.rootbox { margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--line); }
.rootbox:not([hidden]) + .answers { border-top: 0; padding-top: 0; margin-top: 4px; }
.roots { display: grid; grid-template-columns: repeat(6, 1fr); gap: 5px; margin-bottom: 10px; }
.roots button { min-height: 42px; padding: 4px 2px; font-size: 0.875rem; font-weight: 600; line-height: 1.15; }
.roots .sub { display: block; font-size: 0.6875rem; font-weight: 400; opacity: 0.7; }
.roots .right { background: var(--right); border-color: var(--right); color: var(--on-fill); }
.roots .wrong { background: var(--wrong); border-color: var(--wrong); color: var(--on-fill); }
.roots .sel, .answers .sel { outline: 2px solid var(--accent); outline-offset: 2px; }

.status { font-weight: 600; min-height: 1.4em; margin: 6px 0; text-align: center; }

/* FLASH (test) mode: display font for the word, and a bar that runs down before the next question */
.flash { font-family: "Orbitron", "Google Sans Flex", system-ui, sans-serif; font-weight: 900; letter-spacing: 0.06em; }
.tag .flash { color: var(--text); }
/* Test: time left, fixed to the bottom edge of the screen; red for the last 10 seconds */
.timebar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15; height: 8px;
  background: var(--track); padding-bottom: env(safe-area-inset-bottom);
}
.timebar span {
  display: block; height: 8px; background: var(--accent); transform-origin: left;
  transition: transform 0.25s linear, background-color 0.3s ease;
}
.timebar.low span { background: var(--wrong); animation: blink 1s ease-in-out infinite; }
@keyframes blink { 50% { opacity: 0.55; } }
@media (prefers-reduced-motion: reduce) { .timebar span { transition: none; } .timebar.low span { animation: none; } }
body { padding-bottom: 24px; }

/* FLASH: "next question coming" bar, fixed to the bottom of the screen just above the time bar */
.autonext {
  position: fixed; left: 0; right: 0; bottom: calc(8px + env(safe-area-inset-bottom)); z-index: 16;
  height: 5px; background: transparent; overflow: hidden; margin: 0;
}
.autonext span { display: block; height: 100%; width: 100%; background: var(--accent); transform-origin: left; }
.autonext.run span { animation: drain var(--ms, 1000ms) linear forwards; }
@keyframes drain { from { transform: scaleX(1); } to { transform: scaleX(0); } }
.status.right { color: var(--right); }
.status.wrong { color: var(--wrong); }
/* Melody / progression results: each note marked right or wrong; .now = sounding during replay */
.note { border-radius: 6px; padding: 0 3px; transition: background-color 0.15s ease, color 0.15s ease; }
.note.ok { color: var(--right); }
.note.bad { color: var(--wrong); text-decoration: underline wavy; text-underline-offset: 3px; }
.note.now { background: var(--hit); color: var(--text); }

.reveal { background: var(--card); border: 1px solid var(--line); border-radius: var(--r); padding: 10px; }
#nextBtn { width: 100%; }
.preview { font-size: 0.875rem; font-weight: 500; margin: 8px 0 0; }
/* Tap feedback: press in, tint, and a ring that spreads out */
.answers button.tap, .roots button.tap { animation: tap 0.4s ease-out; }
@keyframes tap {
  0% { transform: scale(0.94); background-color: var(--accent-soft); box-shadow: 0 0 0 0 rgb(11 87 208 / 0.45); }
  100% { transform: none; box-shadow: 0 0 0 12px rgb(11 87 208 / 0); }
}
@media (prefers-reduced-motion: reduce) { .answers button.tap, .roots button.tap { animation: none; } }
#detail { color: var(--muted); font-size: 0.8125rem; margin-top: 8px; }
.mono { font-family: ui-monospace, Menlo, Consolas, monospace; color: var(--text); }

/* Piano keyboard */
.keyboard { position: relative; height: 96px; margin: 8px 0 0; }
.key { position: absolute; top: 0; margin: 0; padding: 0; min-height: 0; border-radius: 0 0 5px 5px; display: flex; align-items: flex-end; justify-content: center; }
.key.white { height: 100%; background: var(--key-white); border: 1px solid var(--key-edge); color: var(--key-ink); z-index: 1; padding-bottom: 3px; }
.key.black { height: 62%; background: var(--key-black); border: 1px solid var(--key-black); color: #fff; z-index: 2; padding-bottom: 2px; }
/* Highlighted keys keep a black outline so neighbouring blue keys don't merge into one block */
.key.on { background: var(--accent); border-color: var(--key-black); color: var(--on-fill); }
.key.black.on { box-shadow: inset 0 0 0 1px var(--card); }
.key .lbl { font-size: 0.625rem; font-weight: 600; pointer-events: none; }
/* Key being played right now */
.key.hit { background: var(--hit); border-color: var(--key-black); color: var(--text); animation: press 0.45s ease-out; }
@keyframes press {
  0% { transform: translateY(3px); box-shadow: 0 0 0 4px rgb(242 153 0 / 0.45); }
  100% { transform: translateY(0); box-shadow: 0 0 0 0 rgb(242 153 0 / 0); }
}
@media (prefers-reduced-motion: reduce) { .key.hit { animation: none; } }

/* Results popup */
dialog {
  width: min(88vw, 340px); border: 0; border-radius: 18px; padding: 24px 20px 20px;
  text-align: center; color: var(--text); background: var(--card);
  box-shadow: 0 20px 50px rgb(0 0 0 / 0.3);
}
dialog::backdrop { background: rgb(20 23 28 / 0.55); }
/* Popup pops in with a little overshoot; backdrop fades; rows slide in one by one */
dialog[open] { animation: modal-in 0.4s cubic-bezier(0.2, 0.9, 0.3, 1.2); }
dialog[open]::backdrop { animation: fade-in 0.3s ease; }
dialog.closing { animation: modal-out 0.2s ease-in forwards; }
dialog.closing::backdrop { animation: fade-out 0.2s ease-in forwards; }
@keyframes modal-in { from { opacity: 0; transform: translateY(24px) scale(0.9); } to { opacity: 1; transform: none; } }
@keyframes modal-out { to { opacity: 0; transform: translateY(10px) scale(0.95); } }
@keyframes fade-in { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }
.rlist li { animation: chip-in 0.3s ease-out both; animation-delay: calc(var(--d, 0) * 60ms + 250ms); }
dialog h2 { margin: 0; font-size: 1.375rem; }
dialog .big { font-size: 2.75rem; font-weight: 700; margin: 6px 0 0; line-height: 1.1; }
#resultTime { margin: 2px 0 14px; }
.rlist .rv { display: flex; align-items: baseline; gap: 8px; }
.rlist small { color: var(--muted); font-size: 0.75rem; font-family: ui-monospace, Menlo, Consolas, monospace; }
dialog button { width: 100%; margin-top: 8px; }
.rlist { list-style: none; margin: 0 0 8px; padding: 0; max-height: 40vh; overflow-y: auto; text-align: left; font-size: 0.9375rem; }
.rlist li { display: flex; justify-content: space-between; padding: 6px 2px; border-top: 1px solid var(--line); }
.rlist b { font-family: ui-monospace, Menlo, Consolas, monospace; font-weight: 600; }
.rlist b.miss { color: var(--wrong); }
.rlist li { flex-wrap: wrap; row-gap: 4px; }
.rbar { flex-basis: 100%; height: 6px; border-radius: 3px; background: var(--track); overflow: hidden; }
.rbar span { display: block; height: 100%; border-radius: 3px; background: var(--accent); }

/* Results chart: one column per question, height = time, colour + mark = right / wrong */
.qwrap { text-align: left; margin: 0 0 12px; }
.qhead { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.qlegend { font-size: 0.6875rem; color: var(--muted); display: flex; align-items: center; gap: 4px; white-space: nowrap; }
.qlegend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-left: 4px; }
.qlegend i.ok, .qbar.ok { background: var(--right); }
.qlegend i.bad, .qbar.bad {
  background: repeating-linear-gradient(135deg, var(--wrong) 0 3px, var(--wrong-stripe) 3px 5px);
}
.qchart {
  display: flex; align-items: flex-end; gap: 2px; height: 72px; padding: 0 0 2px;
  border-bottom: 1px solid var(--line); margin-top: 4px;
}
.qbar {
  flex: 1 1 0; min-width: 3px; max-width: 18px; border-radius: 4px 4px 0 0;
  transform-origin: bottom; animation: grow 0.4s ease-out both; animation-delay: calc(var(--d, 0) * 25ms + 300ms);
}
@keyframes grow { from { transform: scaleY(0); } to { transform: none; } }

/* Question log in the results popup */
.qlogwrap { text-align: left; margin: 0 0 8px; }
.qlogwrap summary { cursor: pointer; color: var(--accent); font-weight: 600; font-size: 0.875rem; padding: 6px 0; }
.qlog { list-style: none; margin: 0; padding: 0; max-height: 30vh; overflow-y: auto; font-size: 0.8125rem; }
.qlog li { display: grid; grid-template-columns: 34px 16px 1fr auto; gap: 6px; align-items: baseline; padding: 5px 2px; border-top: 1px solid var(--line); }
.qlog .qn { color: var(--muted); font-family: ui-monospace, Menlo, Consolas, monospace; }
.qlog li.ok b { color: var(--right); }
.qlog li.bad b { color: var(--wrong); }
.qlog .qa { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.qlog small { color: var(--muted); font-family: ui-monospace, Menlo, Consolas, monospace; }
@media (prefers-reduced-motion: reduce) { .qbar { animation: none; } }

@media (min-width: 521px) {
  body { padding-top: 24px; }
  .keyboard { height: 120px; }
}

/* ---------- Phones: no accidental selecting, long-press menus or double-tap zoom on controls ---------- */
button, .key, .pkey, .cat, .chip, .menu-list a, .home {
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; touch-action: manipulation;
}

/* Top bar label: long category names shrink with "…" instead of pushing the buttons off screen */
.menu-btn { min-width: 0; }
.mb-label { min-width: 0; }
.mb-text { min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }

/* Taller answer buttons (perfect pitch) */
.answers.big button { min-height: 72px; font-size: 1.25rem; font-weight: 600; }

/* ---------- Volume (top bar) ---------- */
.vol-pop {
  position: absolute; top: calc(100% + 6px); right: 0; z-index: 21; width: min(280px, 92vw);
  display: grid; grid-template-columns: 1fr auto; gap: 10px 12px; align-items: center; padding: 14px;
  background: var(--card); border: 1px solid var(--line); border-radius: 16px; box-shadow: 0 12px 32px rgb(0 0 0 / 0.14);
  opacity: 0; transform: translateY(-6px); visibility: hidden; pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease, visibility 0s linear 0.2s;
}
.vol-pop.open { opacity: 1; transform: none; visibility: visible; pointer-events: auto; transition: opacity 0.2s ease, transform 0.2s ease, visibility 0s; }
.vol-pop input[type="range"], .range-row input[type="range"] { width: 100%; accent-color: var(--accent); min-height: 32px; touch-action: pan-y; }
.vol-val { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 0.875rem; color: var(--muted); min-width: 3.5em; text-align: right; }
.vol-pop button { grid-column: 1 / -1; }
/* Volume setting (settings screens) */
.vol-row { display: grid; grid-template-columns: 1fr auto auto; align-items: center; gap: 10px; }
.vol-row input[type="range"] { width: 100%; accent-color: var(--accent); min-height: 32px; touch-action: pan-y; }
.vol-test { min-height: 40px; padding: 6px 14px; border-radius: 999px; font-size: 0.875rem; font-weight: 600; }

/* ---------- Keyboard playground ---------- */
.pg-controls { display: grid; gap: 4px 20px; grid-template-columns: 1fr; align-items: end; }
.octave { display: flex; align-items: center; gap: 8px; }
.octave button { width: 48px; font-size: 1.25rem; font-weight: 700; }
.oct-label { min-width: 3em; text-align: center; font-weight: 700; font-family: ui-monospace, Menlo, Consolas, monospace; }
.range-row { display: flex; align-items: center; gap: 12px; }
.range-val { color: var(--muted); font-size: 0.875rem; white-space: nowrap; }
.pkeys {
  position: relative; height: 200px; margin: 16px 0 8px;
  touch-action: none; /* fingers play keys instead of scrolling / zooming the page */
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
.pkey { position: absolute; top: 0; border-radius: 0 0 6px 6px; display: flex; align-items: flex-end; justify-content: center; cursor: pointer; }
.pkey.white { height: 100%; background: var(--key-white); border: 1px solid var(--key-edge); color: var(--key-ink); z-index: 1; padding-bottom: 8px; }
.pkey.black { height: 62%; background: var(--key-black); border: 1px solid var(--key-black); color: #fff; z-index: 2; padding-bottom: 6px; }
.pkey.down { background: var(--accent); border-color: var(--key-black); color: var(--on-fill); }
.pkey.white.down { box-shadow: inset 0 -6px 0 rgb(0 0 0 / 0.15); }
.plbl { font-size: 0.75rem; font-weight: 600; pointer-events: none; }
.pkey.black .plbl { font-size: 0.625rem; }
@media (hover: none) { .pc-only { display: none; } }

/* Fullscreen keyboard: the playground fills the screen and the keys take all the height left */
.fs-btn { min-height: 40px; padding: 6px 14px; border-radius: 999px; font-size: 0.875rem; font-weight: 600; }
#app.pg-full {
  position: fixed; inset: 0; z-index: 30; max-width: none; margin: 0; overflow: auto;
  display: flex; flex-direction: column; background: var(--bg);
  padding: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}
/* Fullscreen = just the piano: no title, text or settings; only a small exit button in the corner */
#app.pg-full h1, #app.pg-full > p, #app.pg-full .pg-controls { display: none; }
#app.pg-full .titlebar { position: absolute; top: max(8px, env(safe-area-inset-top)); right: max(10px, env(safe-area-inset-right)); margin: 0; z-index: 5; }
#app.pg-full .fs-btn span { display: none; }
#app.pg-full .fs-btn { width: 44px; height: 44px; padding: 0; opacity: 0.85; }
#app.pg-full .pkeys { flex: 1; height: auto; min-height: 160px; margin: 52px 0 0; }
body.pg-full-open { overflow: hidden; }

/* Chord boxes (progressions, cadence fill-in): one box per chord, 4 to a row on phones */
.slots { display: block; }
.slot-row { display: grid; grid-template-columns: repeat(min(var(--n, 4), 4), 1fr); gap: 6px; }
@media (min-width: 700px) { .slot-row { grid-template-columns: repeat(var(--n, 4), 1fr); } }
.slot {
  min-height: 56px; padding: 4px; font-size: 1.25rem; font-weight: 700; font-family: inherit;
  border: 2px dashed var(--line); background: transparent; color: var(--text);
}
.slot.filled { border-style: solid; border-color: var(--text); background: var(--card); }
.slot.next { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.slot.ok { background: var(--right); border: 2px solid var(--right); color: var(--on-fill); }
.slot.bad { background: var(--wrong); border: 2px solid var(--wrong); color: var(--on-fill); }
.slot-hint { display: block; margin-top: 8px; text-align: center; color: var(--muted); font-size: 0.8125rem; }

/* ---------- Tablet ---------- */
@media (min-width: 700px) {
  body { max-width: 760px; padding-left: 24px; padding-right: 24px; }
  h1 { font-size: 1.75rem; }
  .hero h1 { font-size: 2.5rem; }
  .cats { grid-template-columns: 1fr 1fr; }
  .answers { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)) !important; gap: 8px; }
  .answers button { min-height: 52px; }
  .roots { grid-template-columns: repeat(12, 1fr); }
  .player button, button.primary { min-height: 52px; }
  .keyboard { height: 150px; }
  .keyboard.input { height: 160px; }
  .pkeys { height: 260px; }
  .pg-controls { grid-template-columns: 1fr 1fr; }
  dialog { width: min(92vw, 460px); }
}

/* ---------- PC ---------- */
@media (min-width: 1100px) {
  body { max-width: 1080px; }
  .hero { margin-top: 40px; }
  .hero h1 { font-size: 3rem; }
  .hero p { max-width: 640px; font-size: 1rem; }
  .cats { grid-template-columns: repeat(3, 1fr); gap: 14px; }
  /* Quizzes stay a comfortable reading width in the middle; the keyboard playground uses it all. */
  #app:not(.wide) { max-width: 820px; margin: 0 auto; }
  .pkeys { height: 300px; }
  .pg-controls { grid-template-columns: repeat(4, 1fr); }
  .keyboard { height: 170px; }
}

@media (prefers-reduced-motion: reduce) {
  button:active, .cat:active, .cat:hover, .cat:hover .cat-go { transform: none; }
  .menu-list, .menu-list li, .menu-btn .chev { animation: none; transition: none; }
  html.enter main, html.enter .site-header { animation: none; }
  dialog[open], dialog[open]::backdrop, dialog.closing, dialog.closing::backdrop, .rlist li { animation: none; }
}
