:root {
  --pinky: #5c4f86;
  --ring: #8d5300;
  --middle: #1f7a56;
  --index: #1d5fbf;
  --thumb: #a84368;
  --gold: #e2a122;
  --good: #157a45;
  --bad: #c5362b;
  --now: #ffe7a3;
  --paper: #f6f2e8;
  --paper-2: #fffdf8;
  --ink: #1a2332;
  --ink-soft: #3c485c;
  --muted: #6b7585;
  --line: #e3dac8;
  --sidebar: #141c2c;
  --key: #fffdf8;
  --hero-size: 2.15rem;
  --pass-size: 1.15rem;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  --sans: "Avenir Next", "Segoe UI", system-ui, sans-serif;
  color-scheme: light;
}

body[data-theme="dark"] {
  --paper: #101722;
  --paper-2: #172232;
  --ink: #f6f1e6;
  --ink-soft: #d9d3c7;
  --muted: #a9b3c4;
  --line: #2c3a50;
  --key: #223044;
  --good: #86d7a8;
  --bad: #ff9b90;
  --now: #5c4a1c;
  --sidebar: #0c121c;
  color-scheme: dark;
}

body[data-size="sm"] { --hero-size: 1.65rem; --pass-size: 1rem; }
body[data-size="md"] { --hero-size: 2.15rem; --pass-size: 1.15rem; }
body[data-size="lg"] { --hero-size: 2.7rem; --pass-size: 1.35rem; }

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  overflow: hidden;
  font-family: var(--sans);
  color: var(--ink);
  background: var(--paper);
  font-size: 16px;
  user-select: none;
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; }
button:focus { outline: none; }
button:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}
button:disabled { opacity: 0.35; cursor: default; }

[hidden] { display: none !important; }

[data-finger="pinky"] { --fc: var(--pinky); }
[data-finger="ring"] { --fc: var(--ring); }
[data-finger="middle"] { --fc: var(--middle); }
[data-finger="index"] { --fc: var(--index); }
[data-finger="thumb"] { --fc: var(--thumb); }

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.app {
  height: 100dvh;
  display: grid;
  grid-template-columns: 16rem 1fr;
}

#sidebar {
  background: var(--sidebar);
  color: #f4efe4;
  overflow: auto;
  min-height: 0;
  border-right: 1px solid rgba(255, 255, 255, 0.06);
}

.brand { padding: 0.95rem 0.9rem 0.2rem; }
.brand strong {
  display: block;
  font-family: Georgia, "Iowan Old Style", Palatino, serif;
  font-size: 1.6rem;
  font-weight: 600;
  letter-spacing: -0.03em;
}
.brand span { color: #b7c0d0; font-size: 0.85rem; }

.grade h2 {
  margin: 0.85rem 0.75rem 0.1rem;
  font-size: 0.68rem;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--gold);
}
.blurb {
  margin: 0 0.75rem 0.3rem;
  color: #b7c0d0;
  font-size: 0.75rem;
}

.unit {
  width: calc(100% - 0.7rem);
  margin: 0.12rem 0.35rem;
  display: grid;
  grid-template-columns: 1.3rem 1fr auto;
  gap: 0.4rem;
  align-items: center;
  text-align: left;
  border: 0;
  background: transparent;
  color: inherit;
  border-radius: 8px;
  padding: 0.32rem 0.4rem;
  cursor: pointer;
}
.unit strong { display: block; font-size: 0.86rem; font-weight: 600; }
.unit small { display: block; font-size: 0.7rem; color: #b7c0d0; }
.num { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--gold); }
.unit:hover { background: rgba(255, 255, 255, 0.06); }
.unit.is-on,
.unit.is-on:hover { background: var(--gold); color: #1c1608; }
.unit.is-on .num,
.unit.is-on small { color: #4a3b12; }

.pips { display: flex; gap: 3px; }
.pip {
  width: 6px;
  height: 6px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.22);
  display: block;
}
.pip.on { background: var(--gold); }
.unit.is-on .pip { background: rgba(28, 22, 8, 0.25); }
.unit.is-on .pip.on { background: #1c1608; }

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}
.top {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
  padding: 0.55rem 0.85rem;
  border-bottom: 1px solid var(--line);
  background: var(--paper);
}
.logo-btn {
  display: none;
  border: 0;
  background: transparent;
  color: var(--ink);
  font-family: Georgia, "Iowan Old Style", Palatino, serif;
  font-size: 1.25rem;
  font-weight: 600;
  padding: 0.1rem 0.2rem;
  cursor: pointer;
}
.spacer { flex: 1; }
.stars { color: var(--muted); font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.tool {
  border: 1px solid var(--line);
  background: var(--paper-2);
  color: var(--ink);
  border-radius: 999px;
  padding: 0.28rem 0.7rem;
  font-size: 0.85rem;
  cursor: pointer;
}
.tool[aria-pressed="true"] {
  background: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
}

.workspace {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
#lesson {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 0.9rem 1.1rem 0.5rem;
  outline: none;
}
.sheet { width: min(52rem, 100%); margin: 0 auto; }

.kicker {
  margin: 0;
  color: var(--muted);
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
h1 {
  font-family: Georgia, "Iowan Old Style", Palatino, serif;
  font-size: 1.85rem;
  font-weight: 600;
  letter-spacing: -0.03em;
  margin: 0.1rem 0 0;
}
.teach { margin: 0.2rem 0 0.35rem; color: var(--muted); max-width: 46rem; }
.lead { color: var(--ink-soft); line-height: 1.45; max-width: 40rem; }
.lead.small { color: var(--muted); font-size: 0.92rem; }

.chips { display: flex; gap: 0.35rem; flex-wrap: wrap; margin: 0.45rem 0 0.2rem; }
.chip {
  min-width: 2rem;
  text-align: center;
  border-top: 3px solid var(--fc, var(--ink));
  border-radius: 7px;
  padding: 0.28rem 0.5rem 0.2rem;
  background: var(--paper-2);
  box-shadow: 0 1px 0 var(--line);
  font-family: var(--mono);
  font-weight: 700;
}

.modes {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
  margin: 0.55rem 0 0.35rem;
}
.mode {
  border: 0;
  background: transparent;
  color: var(--ink-soft);
  font-size: 0.95rem;
  padding: 0.35rem 0.8rem;
  border-radius: 999px;
  cursor: pointer;
}
.mode.is-on { background: var(--ink); color: var(--paper); }
.nav-arrow {
  width: 2rem;
  height: 2rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--paper-2);
  color: var(--ink);
  cursor: pointer;
}
.instruct { margin: 0 0 0.55rem; color: var(--ink-soft); }
.best { margin: -0.15rem 0 0.55rem; color: var(--muted); font-size: 0.85rem; }

.caps {
  background: #fff1c9;
  color: #6a4b00;
  border-radius: 8px;
  padding: 0.35rem 0.6rem;
  margin: 0 0 0.5rem;
}
body[data-theme="dark"] .caps { background: #3d3420; color: #ffd98a; }

.hero {
  border: 3px solid var(--ink);
  border-radius: 14px;
  min-height: 4.8rem;
  padding: 0.75rem 1rem;
  background: var(--paper-2);
  font-family: var(--mono);
  font-size: var(--hero-size);
  line-height: 1.35;
  text-align: center;
}
.hero .ch.now {
  box-shadow: inset 0 0 0 2.5px var(--ink);
  background: var(--now);
  border-radius: 4px;
}
.passage {
  font-family: var(--mono);
  font-size: var(--pass-size);
  line-height: 1.65;
  max-height: 7.2rem;
  overflow: auto;
  padding: 0.7rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--paper-2);
  position: relative;
}
.ch.ok { color: var(--good); }
.ch.wait { color: var(--ink); }
.ch.now { color: var(--ink); }
.ch.space { white-space: pre; }
.passage .ch.space.now {
  background: var(--now);
  box-shadow: inset 0 0 0 2px var(--ink);
  border-radius: 4px;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem 1rem;
  margin: 0.65rem 0 0.35rem;
}
.bar {
  flex: 1 1 8rem;
  height: 0.45rem;
  background: var(--line);
  border-radius: 99px;
  overflow: hidden;
  min-width: 6rem;
}
.bar > span {
  display: block;
  height: 100%;
  width: 0;
  background: var(--gold);
}
.stat b {
  display: block;
  font-size: 1.25rem;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}
.stat span {
  color: var(--muted);
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.restart {
  border: 0;
  background: transparent;
  color: var(--ink);
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
  padding: 0.2rem;
}

.hint {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0.15rem 0 0.45rem;
  font-size: 1.02rem;
}
.hint::before {
  content: "";
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 99px;
  background: var(--fc, var(--ink));
  flex: none;
}
.note { color: var(--bad); margin: 0 0 0.45rem; }

.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
  margin-top: 0.8rem;
}
.card {
  text-align: left;
  border: 1px solid var(--line);
  background: var(--paper-2);
  border-radius: 14px;
  padding: 0.85rem 0.9rem 0.95rem;
  color: inherit;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.12rem;
}
.card .n {
  font-family: Georgia, "Iowan Old Style", Palatino, serif;
  font-size: 2rem;
  line-height: 1;
}
.card .promise, .card .count { color: var(--muted); font-size: 0.88rem; }
.card.is-next { border-color: var(--gold); box-shadow: inset 0 0 0 1px var(--gold); }
.card:hover { border-color: var(--ink); }
.continue { margin-top: 0.8rem; }

.results { text-align: center; padding-top: 0.4rem; }
.results .stats { justify-content: center; }
.star-row { font-size: 2.3rem; letter-spacing: 0.2rem; margin: 0.35rem 0; }
.star { color: var(--line); }
.star.on { color: var(--gold); }
.result-line { font-size: 1.15rem; margin: 0.2rem 0 0.8rem; }
.actions { display: flex; gap: 0.5rem; justify-content: center; flex-wrap: wrap; }
.primary, .ghost {
  border-radius: 999px;
  padding: 0.55rem 1rem;
  cursor: pointer;
}
.primary { background: var(--ink); color: var(--paper); border: 0; }
.ghost { background: transparent; color: var(--ink); border: 1px solid var(--line); }

#dock {
  border-top: 1px solid var(--line);
  padding: 0.45rem 0.7rem 0.55rem;
  background: var(--paper-2);
  overflow-x: auto;
  container-type: inline-size;
}
.board { width: max-content; margin: 0 auto; }
.row {
  display: flex;
  justify-content: center;
  gap: 0.28rem;
  margin: 0.28rem 0;
}
.row:nth-child(2) { padding-left: 0.85rem; }
.row:nth-child(3) { padding-left: 1.15rem; }
.key {
  position: relative;
  width: 2.15rem;
  width: clamp(1.35rem, 5.8cqi, 2.55rem);
  height: 2.4rem;
  border: 1px solid var(--line);
  border-top: 3px solid var(--fc, var(--line));
  border-radius: 7px;
  background: var(--key);
  color: var(--ink);
  font-family: var(--mono);
  font-size: 0.78rem;
  box-shadow: 0 1px 0 rgba(20, 28, 44, 0.18);
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
}
.key.shift, .key.back {
  width: clamp(1.8rem, 8cqi, 3.3rem);
  font-family: var(--sans);
  font-size: 0.72rem;
}
.key.space { width: min(16rem, 42cqi); font-family: var(--sans); }
.key.is-dim { opacity: 0.34; }
.key.is-next {
  opacity: 1;
  background: var(--fc, var(--ink));
  color: #fff;
  border-color: transparent;
}
.key.is-shift { opacity: 1; box-shadow: inset 0 0 0 2px var(--gold); }
.key.is-press { transform: translateY(1px); }
.bump {
  position: absolute;
  left: 50%;
  bottom: 0.28rem;
  width: 0.7rem;
  height: 2px;
  margin-left: -0.35rem;
  border-radius: 1px;
  background: currentColor;
  opacity: 0.75;
}
.legend {
  display: flex;
  justify-content: center;
  gap: 0.9rem;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: 0.75rem;
  margin-top: 0.25rem;
}
.legend span { display: inline-flex; align-items: center; gap: 0.3rem; }
.legend i {
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 99px;
  display: block;
  background: var(--fc);
}

dialog {
  border: none;
  border-radius: 18px;
  padding: 0;
  background: var(--paper-2);
  color: var(--ink);
  max-width: 38rem;
  width: calc(100% - 2rem);
  box-shadow: 0 24px 80px rgba(16, 20, 28, 0.28);
}
dialog::backdrop { background: rgba(12, 16, 24, 0.55); }
dialog .sheet {
  padding: 1.2rem 1.3rem 1.3rem;
  max-height: min(80dvh, 40rem);
  overflow: auto;
}
dialog h2 {
  font-family: Georgia, "Iowan Old Style", Palatino, serif;
  margin: 0 0 0.35rem;
}
dialog h3 { margin: 1rem 0 0.3rem; font-size: 0.95rem; }
dialog p, dialog li { color: var(--ink-soft); line-height: 1.45; }
dialog ul { margin: 0.2rem 0 0; padding-left: 1.1rem; }
dialog .actions { justify-content: flex-start; margin-top: 1rem; }
.noscript {
  margin: 1rem;
  font-family: var(--sans);
}

@media (prefers-reduced-motion: no-preference) {
  .bar > span { transition: width 80ms linear; }
  .hero.is-miss { animation: shake 0.18s linear; }
  .key.is-next { animation: pulse 1.4s ease-in-out infinite; }
}
@keyframes shake {
  25% { transform: translateX(-3px); }
  75% { transform: translateX(3px); }
}
@keyframes pulse {
  50% { transform: translateY(-2px); }
}

@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); }
  .logo-btn { display: inline-block; }
  .brand { display: none; }
  .top { flex-wrap: nowrap; overflow-x: auto; }
  .tool, .stars, .logo-btn { flex: none; }
  #sidebar {
    display: flex;
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    border-right: 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    max-height: 4.5rem;
  }
  .grade { display: flex; flex-direction: row; align-items: center; }
  .grade h2, .blurb { display: none; }
  .unit { width: auto; min-width: 9.2rem; margin: 0.35rem 0.15rem; }
  .unit small { display: none; }
  .cards { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 560px) {
  .cards { grid-template-columns: 1fr; }
  h1 { font-size: 1.55rem; }
}
