/* Tokens, the page column and the header come from ../shared/theme.css;
   this file holds only what is the drill's own. */

:root {
  --correct: #147d4a;
  --correct-bg: #dff3e6;
  --wrong: #b3261e;
  --wrong-bg: #fbe4e2;
  --paper: #ffffff;
  --staff-ink: #16181c;
  /* The staff card is white in both themes, so its state colours are fixed. */
  --staff-wrong: #c62828;
  --staff-correct: #1b7f4c;
  --correct-ink: #ffffff;
  --wrong-ink: #ffffff;
  /* The staff needs more room than the theme's column gives it. */
  --app-width: 520px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --correct: #6ede9f;
    --correct-bg: #17301f;
    --wrong: #ff9c92;
    --wrong-bg: #331a19;
    --correct-ink: #0f1115;
    --wrong-ink: #0f1115;
  }
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 10px;
  transition: background 90ms ease, border-color 90ms ease, color 90ms ease;
}

button:disabled {
  cursor: default;
}

button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* --- clef toggle --- */

.header-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

/* Clef, then mode, then sound; the mode toggle takes the rest of the line. */
.header-controls .mode-toggle {
  flex: 1;
}

.sound {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  color: var(--muted);
}

.sound[aria-pressed='true'] {
  color: var(--ink);
  border-color: var(--accent);
}

.icon-fill {
  fill: currentColor;
}

.icon-stroke {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
}

/* A segmented control: the buttons stay put and a thumb slides under them
   between positions. Used for both clef (two-up) and mode (three-up) — the
   shell sets --segments, since only it knows how many choices there are. */
.toggle {
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--segments, 2), 1fr);
  padding: 4px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.toggle-thumb {
  position: absolute;
  top: 4px;
  left: 4px;
  width: calc((100% - 8px) / var(--segments, 2));
  height: calc(100% - 8px);
  background: var(--accent);
  border-radius: 9px;
  pointer-events: none;
}

/* Added after the first paint so the thumb does not slide in on load. */
.toggle.is-ready .toggle-thumb {
  transition: transform 220ms cubic-bezier(0.34, 1.15, 0.64, 1);
}

/* A percentage translate is against the thumb's own width, so one segment
   of travel is 100% however many segments the control has. */
.toggle[data-position='1'] .toggle-thumb {
  transform: translateX(100%);
}

.toggle[data-position='2'] .toggle-thumb {
  transform: translateX(200%);
}

@media (prefers-reduced-motion: reduce) {
  .toggle.is-ready .toggle-thumb {
    transition: none;
  }
}

.segment {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 54px;
  height: 40px;
  border: 0;
  background: transparent;
  color: var(--muted);
}

/* Mode names are words, not glyphs, so they need less width than a clef. */
.mode-toggle .segment {
  min-width: 48px;
  padding: 0 6px;
  font-size: 13px;
  font-weight: 600;
}

/* The infinity symbol is a glyph among words: at the word size it reads as a
   smudge, so it is set larger and its box kept the width of a short word. */
.mode-toggle .segment[data-value='infinite'] {
  min-width: 40px;
  font-size: 20px;
  line-height: 1;
}

.segment.is-active {
  color: var(--accent-ink);
}

/* --- levels --- */

.level-row {
  display: grid;
  grid-template-columns: repeat(11, 1fr);
  gap: 4px;
}

.level {
  padding: 9px 0;
  min-width: 0;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--muted);
}

.level.is-active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.level-caption {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

/* --- staff --- */

.board {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.staff-card {
  background: var(--paper);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 12px;
  transition: border-color 120ms ease, box-shadow 120ms ease;
}

.staff-card.is-tappable {
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}

.staff-card.is-correct {
  border-color: var(--staff-correct);
}

.staff-card.is-wrong {
  border-color: var(--staff-wrong);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--staff-wrong) 22%, transparent);
  animation: shake 380ms ease;
}

@keyframes shake {
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(3px); }
}

@media (prefers-reduced-motion: reduce) {
  .staff-card.is-wrong {
    animation: none;
  }
}

.staff {
  display: block;
  width: 100%;
  height: auto;
}

/* Line thicknesses come from Bravura's engraving defaults, set per element. */
.staff-line,
.ledger-line {
  stroke: var(--staff-ink);
  stroke-linecap: butt;
}

.clef,
.note-head {
  fill: var(--staff-ink);
}

.staff.is-correct .note-head {
  fill: var(--staff-correct);
}

.staff.is-wrong .note-head {
  fill: var(--staff-wrong);
}

/* Drawn with a paper-coloured outline underneath so it stays legible
   wherever it lands on the staff lines. */
.note-label {
  font: 700 23px system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  fill: var(--staff-ink);
  stroke: var(--paper);
  stroke-width: 5;
  paint-order: stroke fill;
}

.staff.is-correct .note-label {
  fill: var(--staff-correct);
}

.staff.is-wrong .note-label {
  fill: var(--staff-wrong);
}

.feedback {
  margin: 0;
  padding: 9px 12px;
  min-height: 1.5em;
  text-align: center;
  font-weight: 600;
  color: var(--muted);
  border: 1px solid transparent;
  border-radius: 10px;
  transition: background 120ms ease, color 120ms ease;
}

.feedback.is-correct {
  color: var(--correct);
  border-color: var(--correct);
}

.feedback.is-wrong {
  font-size: 18px;
  font-weight: 700;
  color: var(--wrong-ink);
  background: var(--wrong);
  border-color: var(--wrong);
}

/* The end of a run is a result, not a verdict: no red, no green. */
.feedback.is-done {
  font-size: 17px;
  color: var(--ink);
  border-color: var(--accent);
}

/* --- answering --- */

.answer {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.letter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.letter {
  flex: 1 1 60px;
  min-width: 56px;
  min-height: 52px;
  font-size: 19px;
  font-weight: 700;
}

.letter:hover:not(:disabled) {
  border-color: var(--accent);
}

/* On a touch screen the letters are the main target and a mistap grades
   instantly, so they get taller, wider gaps, and a basis that wraps a full
   seven-letter row 4+3 on narrow phones rather than an uneven 5+2. */
@media (pointer: coarse) {
  .letter-row {
    gap: 10px;
  }

  .letter {
    flex-basis: 72px;
    min-width: 72px;
    min-height: 60px;
    /* Without a cap, a one- or two-letter last row stretches across the
       whole card. */
    max-width: 140px;
  }

}

.letter.is-answer {
  background: var(--correct);
  border-color: var(--correct);
  color: var(--correct-ink);
}

.letter.is-wrong {
  background: var(--wrong);
  border-color: var(--wrong);
  color: var(--wrong-ink);
}

/* No text box: a hardware keyboard answers through the document keydown
   handler, so the row only holds the next/new-run buttons. */
.action-row {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: center;
}

.next {
  padding: 9px 14px;
  font-weight: 600;
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

/* --- stats --- */

.stats {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-top: 4px;
  border-top: 1px solid var(--line);
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(82px, 1fr));
  gap: 8px;
  margin: 8px 0 0;
  padding: 0;
  list-style: none;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 4px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
}

.stat-value {
  font-size: 19px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.stat-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.trouble-title {
  margin: 6px 0 0;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.trouble {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.chip {
  padding: 3px 9px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  background: var(--wrong-bg);
  color: var(--wrong);
  border-radius: 999px;
}

.reset {
  align-self: flex-start;
  padding: 7px 12px;
  font-size: 13px;
  color: var(--muted);
}

@media (max-width: 420px) {
  .level-row {
    grid-template-columns: repeat(6, 1fr);
  }
}
