/* ============ Theme ============ */
:root {
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --type: "American Typewriter", "Courier Prime", "Courier New", monospace;

  --bg: #ece2cc;
  --paper: #f6efdf;
  --ink: #2a2119;
  --ink-soft: #5b4d3c;
  --muted: #8d7c63;
  --rule: #cdbb98;
  --accent: #9a6a2c;
  --accent-2: #7b2d26;
  --ok: #4f6d3a;
  --warn: #b17c1f;
  --bad: #a83a2a;
  --ghost: rgba(154, 106, 44, 0.16);
  --ghost-line: rgba(154, 106, 44, 0.75);
  --shadow: 0 1px 0 rgba(255,255,255,.6) inset, 0 12px 30px -18px rgba(60, 40, 15, .45);
  --grain-opacity: .5;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #13100c;
  --paper: #1c1712;
  --ink: #eadcbf;
  --ink-soft: #bfae8d;
  --muted: #8a7a5f;
  --rule: #3b3227;
  --accent: #caa055;
  --accent-2: #d2735e;
  --ok: #92b06c;
  --warn: #d9ab48;
  --bad: #de6c56;
  --ghost: rgba(202, 160, 85, 0.14);
  --ghost-line: rgba(202, 160, 85, 0.7);
  --shadow: 0 1px 0 rgba(255,255,255,.03) inset, 0 16px 40px -20px rgba(0,0,0,.8);
  --grain-opacity: .35;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 17px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  transition: background-color .3s, color .3s;
}
/* paper grain */
body::before {
  content: "";
  position: fixed; inset: 0;
  pointer-events: none;
  opacity: var(--grain-opacity);
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='180' height='180'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.35  0 0 0 0 0.25  0 0 0 0 0.1  0 0 0 .09 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
  z-index: 0;
}

button { font: inherit; color: inherit; cursor: pointer; }
kbd {
  font-family: var(--type);
  font-size: .78em;
  padding: .05em .4em;
  border: 1px solid var(--rule);
  border-bottom-width: 2px;
  border-radius: 3px;
  color: var(--ink-soft);
}

.app {
  position: relative; z-index: 1;
  max-width: 1080px;
  margin: 0 auto;
  padding: 0 28px 40px;
  min-height: 100%;
}

/* ============ Top bar ============ */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 22px 0 18px;
  border-bottom: 1px solid var(--rule);
  box-shadow: 0 3px 0 -2px var(--rule);
}
.brand {
  display: flex; align-items: baseline; gap: 12px;
  background: none; border: 0; padding: 0;
}
.brand-mark { font-family: var(--type); color: var(--accent); font-size: 20px; letter-spacing: .1em; }
.brand-name { font-variant: small-caps; letter-spacing: .14em; font-size: 20px; }
.top-actions { display: flex; gap: 10px; }

.ghost-btn {
  background: transparent;
  border: 1px solid var(--rule);
  border-radius: 999px;
  padding: 6px 16px;
  font-variant: small-caps;
  letter-spacing: .08em;
  color: var(--ink-soft);
  transition: border-color .2s, color .2s, background .2s;
}
.ghost-btn:hover { border-color: var(--accent); color: var(--ink); }
.ghost-btn.icon { width: 38px; height: 38px; padding: 0; display: grid; place-items: center; }
.ghost-btn .i { color: var(--accent); margin-right: 4px; }

/* ============ Screens ============ */
.screen { display: none; animation: fade .35s ease; }
.screen.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ============ Menu ============ */
.hero { text-align: center; padding: 46px 0 30px; }
.eyebrow {
  margin: 0;
  font-variant: small-caps; letter-spacing: .3em; font-size: 13px; color: var(--muted);
}
.hero h1 {
  margin: 8px 0 4px;
  font-weight: 400;
  font-size: clamp(54px, 9vw, 92px);
  letter-spacing: .02em;
  line-height: 1;
}
.hero h1 em { color: var(--accent); font-style: italic; }
.morse-ornament {
  font-family: var(--type); color: var(--accent); letter-spacing: .35em;
  margin: 14px 0 6px; font-size: 14px; opacity: .85;
}
.lede { margin: 0; color: var(--ink-soft); font-style: italic; }

.panel {
  background: var(--paper);
  border: 1px solid var(--rule);
  outline: 1px solid var(--rule);
  outline-offset: 5px;
  border-radius: 4px;
  padding: 34px 40px 30px;
  box-shadow: var(--shadow);
  margin-top: 6px;
}
.field { margin-bottom: 28px; }
.field h2 {
  font-weight: 400; font-variant: small-caps; letter-spacing: .14em;
  font-size: 19px; margin: 0 0 12px;
  display: flex; align-items: baseline; gap: 10px;
}
.field h2 .num { color: var(--accent); font-style: italic; font-variant: normal; letter-spacing: 0; }
.field h2 small {
  margin-left: auto; font-variant: normal; letter-spacing: 0;
  font-size: 14px; color: var(--muted); font-style: italic;
}
.note { margin: 10px 0 0; color: var(--muted); font-style: italic; font-size: 15px; }

.cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.card {
  display: flex; flex-direction: column; align-items: flex-start; gap: 4px;
  text-align: left;
  background: transparent;
  border: 1px solid var(--rule);
  border-radius: 4px;
  padding: 16px 18px 18px;
  transition: border-color .2s, background .2s, transform .2s;
}
.card:hover { border-color: var(--accent); transform: translateY(-1px); }
.card.on { border-color: var(--accent); background: var(--ghost); box-shadow: 0 0 0 1px var(--accent) inset; }
.card-glyph { font-family: var(--type); font-size: 30px; color: var(--accent); line-height: 1.2; }
.card-title { font-variant: small-caps; letter-spacing: .1em; font-size: 18px; }
.card-desc { color: var(--muted); font-size: 14px; font-style: italic; }

.seg { display: inline-flex; flex-wrap: wrap; border: 1px solid var(--rule); border-radius: 4px; overflow: hidden; }
.seg button {
  background: transparent; border: 0; border-right: 1px solid var(--rule);
  padding: 9px 18px; color: var(--ink-soft);
  font-variant: small-caps; letter-spacing: .06em;
  transition: background .2s, color .2s;
}
.seg button:last-child { border-right: 0; }
.seg button b { font-family: var(--type); font-weight: 400; color: var(--muted); margin-left: 4px; font-size: .9em; }
.seg button:hover { color: var(--ink); }
.seg button.on { background: var(--ink); color: var(--paper); }
.seg button.on b { color: var(--accent); }
.seg.small button { padding: 6px 13px; font-size: 15px; }

.options { display: grid; gap: 14px; }
.opt { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.opt.hidden { display: none; }
.opt-label { min-width: 150px; color: var(--ink-soft); font-style: italic; }
.toggles { gap: 22px; padding-top: 4px; }

.toggle {
  display: inline-flex; align-items: center; gap: 10px;
  background: none; border: 0; padding: 0;
  font-variant: small-caps; letter-spacing: .08em; color: var(--ink-soft);
}
.toggle .sw {
  position: relative; width: 36px; height: 20px;
  border-radius: 999px; border: 1px solid var(--rule);
  background: transparent; transition: background .2s, border-color .2s;
}
.toggle .sw::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--muted); transition: transform .2s, background .2s;
}
.toggle.on { color: var(--ink); }
.toggle.on .sw { background: var(--accent); border-color: var(--accent); }
.toggle.on .sw::after { transform: translateX(16px); background: var(--paper); }

.start-row { display: flex; align-items: center; gap: 18px; padding-top: 8px; border-top: 1px solid var(--rule); margin-top: 6px; padding-top: 24px; }
.primary {
  background: var(--ink); color: var(--paper);
  border: 0; border-radius: 3px;
  padding: 13px 30px;
  font-variant: small-caps; letter-spacing: .16em; font-size: 18px;
  box-shadow: 0 0 0 1px var(--ink), 0 0 0 4px var(--paper), 0 0 0 5px var(--rule);
  transition: transform .15s, background .2s;
}
.primary:hover { background: var(--accent-2); box-shadow: 0 0 0 1px var(--accent-2), 0 0 0 4px var(--paper), 0 0 0 5px var(--rule); }
.primary:active { transform: translateY(1px); }
.hint { color: var(--muted); font-style: italic; }

/* ============ Game ============ */
.game-head {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 16px;
  padding: 18px 0;
}
.game-title { font-variant: small-caps; letter-spacing: .12em; color: var(--ink-soft); }
.dot-sep { color: var(--accent); margin: 0 6px; }
.game-actions { display: flex; justify-content: flex-end; align-items: center; gap: 18px; }

.hud { display: flex; gap: 26px; justify-content: center; }
.stat { text-align: center; min-width: 58px; }
.stat .v { font-family: var(--type); font-size: 21px; line-height: 1.1; display: block; }
.stat .k { font-variant: small-caps; letter-spacing: .12em; font-size: 12px; color: var(--muted); }
.stat.lives .v { color: var(--accent-2); letter-spacing: .1em; }

.stage {
  text-align: center;
  padding: 50px 0 16px;
  min-height: 250px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.target {
  font-family: var(--type);
  font-size: 120px; line-height: 1;
  letter-spacing: .06em;
  min-height: 1em;
  white-space: nowrap;
}
.target.mode-words { font-size: 76px; }
.target.mode-sentence { font-size: 42px; letter-spacing: .04em; }
.target .ch { display: inline-block; position: relative; transition: color .2s; }
.target .ch.pending { color: var(--muted); opacity: .55; }
.target .ch.ok { color: var(--ok); }
.target .ch.bad { color: var(--bad); text-decoration: line-through; text-decoration-thickness: 2px; }
.target .ch.current { color: var(--ink); }
.target.mode-words .ch.current::after,
.target.mode-sentence .ch.current::after {
  content: ""; position: absolute; left: 8%; right: 8%; bottom: -10px; height: 3px;
  background: var(--accent); border-radius: 2px;
}
.target .sp { display: inline-block; width: .7em; }
.target .ch.retry-shake { animation: retry-shake .45s ease; }
@keyframes retry-shake {
  0%   { transform: none; color: var(--bad); }
  20%  { transform: translateX(-6px); color: var(--bad); }
  40%  { transform: translateX(6px); color: var(--bad); }
  60%  { transform: translateX(-4px); }
  80%  { transform: translateX(3px); }
  100% { transform: none; color: var(--ink); }
}
.target.pop .ch { animation: pop .18s ease-out; }
@keyframes pop { from { transform: translateY(5px); opacity: .55; } to { transform: none; opacity: 1; } }

.tape {
  width: 100%;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: baseline; gap: 34px;
}
.history, .upcoming {
  display: flex; gap: 16px; align-items: baseline;
  font-family: var(--type); font-size: 24px; letter-spacing: .05em;
  overflow: hidden; white-space: nowrap; min-width: 0;
}
.history { justify-content: flex-end; }
.upcoming { justify-content: flex-start; color: var(--muted); }
.history .ok { color: var(--ok); }
.history .bad, .history .skip { color: var(--bad); text-decoration: line-through; text-decoration-thickness: 2px; }
.history .skip { opacity: .6; }
.target .ch.skip { color: var(--bad); opacity: .5; text-decoration: line-through; text-decoration-thickness: 2px; }
.status {
  min-height: 22px; margin-top: 6px;
  font-variant: small-caps; letter-spacing: .12em; font-size: 14px; color: var(--accent);
}

.hint-pattern { min-height: 34px; margin-top: 22px; display: flex; gap: 14px; justify-content: center; align-items: center; }
.heard {
  min-height: 24px; margin-top: 8px;
  font-style: italic; color: var(--muted);
}
.heard b { font-family: var(--type); font-style: normal; font-weight: 400; }
.heard.ok b { color: var(--ok); }
.heard.bad b { color: var(--bad); }

/* dots & dashes */
.pat { display: inline-flex; align-items: center; gap: 6px; }
.pat i { display: inline-block; height: 9px; background: var(--accent); border-radius: 5px; }
.pat i.dit { width: 9px; }
.pat i.dah { width: 27px; }
.pat.big i { height: 12px; border-radius: 6px; }
.pat.big i.dit { width: 12px; }
.pat.big i.dah { width: 36px; }
.pat.ink i { background: var(--ink); }

.timeline-wrap {
  position: relative;
  background: var(--paper);
  border: 1px solid var(--rule);
  outline: 1px solid var(--rule); outline-offset: 4px;
  border-radius: 4px;
  box-shadow: var(--shadow);
  margin: 10px 0 0;
}
#timeline { display: block; width: 100%; height: 150px; }
.live-pattern {
  position: absolute; right: 14px; top: 10px;
  min-height: 12px;
}

.keyrow { display: flex; align-items: center; gap: 22px; justify-content: center; padding: 30px 0 0; }
.telegraph {
  width: 84px; height: 84px; border-radius: 50%;
  border: 1px solid var(--rule);
  background: var(--paper);
  display: grid; place-items: center;
  box-shadow: var(--shadow);
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}
.telegraph .knob {
  width: 52px; height: 52px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #f1d28d, #b98a3c 45%, #6d4a18 100%);
  box-shadow: 0 5px 0 #4a3210, 0 8px 14px rgba(0,0,0,.35);
  transition: transform .04s, box-shadow .04s;
}
.telegraph.down .knob { transform: translateY(4px); box-shadow: 0 1px 0 #4a3210, 0 2px 4px rgba(0,0,0,.35); }
.keyhint { margin: 0; color: var(--muted); font-style: italic; font-size: 15px; }
.keyhint em { color: var(--ink-soft); }
.legend { font-style: normal; font-family: var(--type); font-size: 13px; }

/* ============ Drawer (code chart) ============ */
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0;
  width: min(440px, 92vw);
  background: var(--paper);
  border-left: 1px solid var(--rule);
  box-shadow: -20px 0 50px -30px rgba(0,0,0,.6);
  padding: 22px 26px;
  overflow-y: auto;
  transform: translateX(105%);
  transition: transform .35s cubic-bezier(.2,.8,.2,1);
  z-index: 20;
}
.drawer.open { transform: none; }
.drawer-head { display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid var(--rule); padding-bottom: 12px; }
.drawer-head h2 { margin: 0; font-weight: 400; font-variant: small-caps; letter-spacing: .16em; }
.drawer-sub { color: var(--muted); font-style: italic; font-size: 14px; margin: 12px 0 4px; }
.drawer-foot { color: var(--muted); font-style: italic; font-size: 13px; text-align: center; }
.chart-group h3 {
  font-weight: 400; font-variant: small-caps; letter-spacing: .14em; font-size: 15px;
  color: var(--accent); margin: 20px 0 8px;
}
.chart-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 2px 14px; }
.chart-cell {
  display: flex; align-items: center; gap: 14px;
  background: none; border: 0; border-bottom: 1px dotted var(--rule);
  padding: 7px 6px; text-align: left;
  transition: background .15s;
}
.chart-cell:hover { background: var(--ghost); }
.chart-cell .c { font-family: var(--type); font-size: 20px; width: 22px; text-align: center; }
.chart-cell.playing { background: var(--ghost); }

/* ============ Summary modal ============ */
.modal {
  position: fixed; inset: 0;
  background: rgba(20, 14, 8, .45);
  backdrop-filter: blur(3px);
  display: none; align-items: center; justify-content: center;
  z-index: 30; padding: 20px;
}
.modal.open { display: flex; animation: fade .3s ease; }
.modal-card {
  background: var(--paper);
  border: 1px solid var(--rule);
  outline: 1px solid var(--rule); outline-offset: 5px;
  border-radius: 4px;
  width: min(680px, 100%);
  max-height: 92vh; overflow-y: auto;
  padding: 34px 40px 30px;
  box-shadow: 0 30px 80px -30px rgba(0,0,0,.6);
}
.sum-head { text-align: center; }
.sum-head .eyebrow { margin-bottom: 6px; }
.sum-head h2 { margin: 0; font-weight: 400; font-size: 42px; }
.sum-head h2 em { color: var(--accent); }
.sum-head p { margin: 4px 0 0; color: var(--muted); font-style: italic; }
.sum-stats {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 0;
  margin: 26px 0; border-top: 1px solid var(--rule); border-bottom: 1px solid var(--rule);
}
.sum-stats .stat { padding: 14px 4px; border-right: 1px solid var(--rule); }
.sum-stats .stat:last-child { border-right: 0; }
.sum-stats .stat .v { font-size: 28px; }
.sum-section h3 {
  font-weight: 400; font-variant: small-caps; letter-spacing: .14em; font-size: 16px;
  color: var(--accent); margin: 0 0 10px;
}
.rhythm { display: grid; gap: 9px; margin-bottom: 24px; }
.rrow { display: grid; grid-template-columns: 170px 1fr 120px; align-items: center; gap: 14px; font-size: 15px; }
.rrow .name { color: var(--ink-soft); }
.rrow .val { font-family: var(--type); font-size: 13px; text-align: right; color: var(--ink-soft); }
.scale { position: relative; height: 14px; }
.scale::before { content: ""; position: absolute; left: 0; right: 0; top: 6px; height: 2px; background: var(--rule); }
.scale .mid { position: absolute; left: 50%; top: 0; width: 1px; height: 14px; background: var(--muted); }
.scale .band { position: absolute; top: 3px; height: 8px; background: var(--ghost); border-radius: 4px; }
.scale .mk { position: absolute; top: 1px; width: 12px; height: 12px; margin-left: -6px; border-radius: 50%; background: var(--ink); }
.scale .mk.ok { background: var(--ok); }
.scale .mk.warn { background: var(--warn); }
.scale .mk.bad { background: var(--bad); }
.rrow.empty { opacity: .4; }
.missed { display: flex; flex-wrap: wrap; gap: 10px 22px; margin-bottom: 24px; }
.missed span { display: inline-flex; align-items: center; gap: 8px; }
.missed .c { font-family: var(--type); font-size: 20px; }
.missed .n { color: var(--muted); font-size: 13px; }
.sum-actions { display: flex; justify-content: center; gap: 16px; align-items: center; border-top: 1px solid var(--rule); padding-top: 22px; }
.best { text-align: center; color: var(--accent); font-style: italic; margin: -10px 0 18px; font-size: 15px; }

/* ============ Responsive ============ */
@media (max-width: 760px) {
  .app { padding: 0 16px 30px; }
  .panel { padding: 24px 20px; }
  .cards { grid-template-columns: 1fr; }
  .game-head { grid-template-columns: 1fr; text-align: center; }
  .game-actions { justify-content: center; }
  .target { font-size: 90px; }
  .target.mode-words { font-size: 52px; }
  .target.mode-sentence { font-size: 28px; white-space: normal; }
  .tape { gap: 14px; }
  .history, .upcoming { font-size: 16px; }
  .sum-stats { grid-template-columns: repeat(2, 1fr); }
  .rrow { grid-template-columns: 1fr; gap: 4px; }
  .rrow .val { text-align: left; }
  .brand-name { display: none; }
}

/* ============ Reverse (listen) ============ */
.replay, .listen-hint { display: none; }
.listen .telegraph, .listen .send-hint { display: none; }
.listen .replay { display: inline-flex; align-items: center; gap: 8px; }
.listen .listen-hint { display: inline; }
.target .ch.hidden { color: var(--muted); }
.target .ch.hidden .pat { vertical-align: middle; margin: 0 .12em; }
.target.mode-letters .ch.hidden .pat { margin: 0; }
.target .ch.hidden.current { color: var(--ink); }
.target .ch.hidden.current .pat i { background: var(--accent); }
.target .ch.hidden.pending .pat i { background: var(--muted); }
.target .ch.fix, .history .fix { color: var(--warn); }

/* chart order toggle */
.chart-sort { display: flex; align-items: center; gap: 12px; margin: 10px 0 2px; }
.chart-sort .opt-label { min-width: 0; font-size: 14px; }
.chart-cell.empty { cursor: default; opacity: .28; }
.chart-cell.empty:hover { background: none; }

/* fun fact on the summary */
.fun-fact {
  margin: -4px auto 22px; max-width: 540px;
  text-align: center; font-style: italic; color: var(--ink-soft); font-size: 16px; line-height: 1.5;
}
.fun-fact .ff-label { font-style: normal; font-variant: small-caps; letter-spacing: .16em; color: var(--accent); }
.fun-fact .ff-dot { color: var(--accent); margin: 0 .55em; font-style: normal; }
.fun-fact strong { font-style: normal; font-weight: 400; color: var(--ink); }
.fun-fact em { font-style: normal; font-variant: small-caps; letter-spacing: .04em; }
.fun-fact code { font-family: var(--type); font-style: normal; color: var(--accent); font-size: .95em; letter-spacing: .08em; }
.fun-fact.menu-fact { margin: 30px auto 0; }
