:root {
  --bg: #f4f2ec;
  --bg-gradient: radial-gradient(circle at 15% 0%, #eef3ff 0%, transparent 45%),
                 radial-gradient(circle at 100% 10%, #fff1e6 0%, transparent 40%),
                 var(--bg);
  --panel: #ffffff;
  --text: #23241f;
  --muted: #6b6f76;
  --accent: #3a6ff7;
  --accent-2: #7c5cff;
  --correct: #2e9e5b;
  --error: #d64545;
  --border: #e1dfd7;

  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --shadow-sm: 0 1px 2px rgba(20, 20, 30, 0.06);
  --shadow-md: 0 6px 20px rgba(30, 30, 50, 0.08);
  --shadow-lg: 0 16px 40px rgba(30, 30, 50, 0.12);

  --f-l-pinky: #e07a5f;
  --f-l-ring: #f2b134;
  --f-l-middle: #f4d35e;
  --f-l-index: #81b29a;
  --f-r-index: #3d7cc9;
  --f-r-middle: #6a5acd;
  --f-r-ring: #b06ab3;
  --f-r-pinky: #d45087;
  --f-thumb: #9aa1ab;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Nunito", -apple-system, "Segoe UI", Verdana, Arial, sans-serif;
  background: var(--bg-gradient);
  background-attachment: fixed;
  color: var(--text);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

body.contrast {
  --bg: #000000;
  --bg-gradient: #000000;
  --panel: #0c0c0c;
  --text: #ffff00;
  --muted: #cccccc;
  --accent: #00e5ff;
  --accent-2: #00e5ff;
  --correct: #4dff88;
  --error: #ff5c5c;
  --border: #444444;
  --shadow-sm: none;
  --shadow-md: none;
  --shadow-lg: none;
}

body.easy-read {
  font-size: 1.15em;
  letter-spacing: 0.03em;
  line-height: 1.85;
}

h1, h2, h3 { font-family: "Nunito", inherit; font-weight: 800; }

/* ---------- Topbar ---------- */

.topbar {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  background: rgba(255, 255, 255, 0.72);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

body.contrast .topbar {
  background: var(--panel);
  backdrop-filter: none;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  font-size: 1.9rem;
  line-height: 1;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.12));
}

.brand-text h1 {
  font-size: 1.35rem;
  margin: 0;
  letter-spacing: -0.01em;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

body.contrast .brand-text h1 {
  -webkit-background-clip: unset;
  background-clip: unset;
  color: var(--text);
}

.brand-tagline {
  margin: 0;
  font-size: 0.72rem;
  color: var(--muted);
  font-weight: 600;
  letter-spacing: 0.02em;
}

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

.control-field {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.control-field-slider {
  min-width: 90px;
}

/* ---------- Buttons & inputs ---------- */

button, select, input[type="text"] {
  font: inherit;
  padding: 9px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.15s ease, background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

select { cursor: pointer; }
input[type="text"] { cursor: text; }

button:hover {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  box-shadow: var(--shadow-sm);
}
button:active { transform: translateY(1px) scale(0.98); }
button:focus-visible, select:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  border-color: transparent;
  font-weight: 800;
  box-shadow: var(--shadow-sm);
}
.btn-primary:hover {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  filter: brightness(1.08);
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}
body.contrast .btn-primary { background: var(--accent); color: #000; }

/* ---------- Layout & shared card style ---------- */

main {
  max-width: 900px;
  margin: 0 auto;
  padding: 24px 20px 40px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 20px 22px;
  margin-bottom: 20px;
}

/* ---------- Lesson info ---------- */

.lesson-info h2 { margin: 0 0 6px; font-size: 1.15rem; }
.lesson-info p { margin: 0; color: var(--muted); font-size: 0.92rem; }

.progress-bar {
  height: 9px;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
  margin-top: 12px;
}
.progress-bar > div {
  height: 100%;
  background: linear-gradient(90deg, var(--correct), #58c988);
  width: 0%;
  border-radius: 999px;
  transition: width 0.3s ease;
}

/* ---------- Stats ---------- */

.stats {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.stats.hidden { display: none; }

.stat-chip {
  display: flex;
  align-items: baseline;
  gap: 6px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 16px;
  box-shadow: var(--shadow-sm);
  font-size: 0.88rem;
  color: var(--muted);
}
.stat-icon { font-size: 0.95rem; }
.stat-label { font-weight: 700; }
.stat-chip b { color: var(--text); font-size: 1rem; font-weight: 800; }

/* ---------- Practice area ---------- */

.practice-area {
  text-align: center;
  padding: 42px 22px;
  position: relative;
  overflow: hidden;
}

#replayBtn {
  position: absolute;
  top: 16px;
  right: 16px;
  border-radius: 999px;
  width: 40px;
  height: 40px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
}

.word-display {
  font-size: 2.7rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  min-height: 3.3rem;
}

/* Each word's letters are grouped into one unbreakable unit (see
   renderWord()) so a long word/sentence wraps at real word boundaries
   only — never splitting a word mid-way and leaving a stranded letter
   on its own line. */
.word-display .word-group {
  display: inline-block;
  white-space: nowrap;
  /* Safety net only: if a single word is wider than the container even by
     itself (very long word on a narrow phone), let it wrap rather than
     overflow off-screen — this is a rare last resort, not the normal path. */
  overflow-wrap: anywhere;
}

.word-display .ch {
  display: inline-block;
  transition: color 0.15s ease;
}
.word-display .ch.space-ch {
  display: inline; /* a normal breakable space between word-groups */
}
.word-display .ch.done { color: var(--correct); }
.word-display .ch.current {
  border-bottom: 4px solid var(--accent);
  border-radius: 2px;
}
.word-display .ch.shake { animation: shake 0.25s; color: var(--error); }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

.listen-hint {
  margin-top: 12px;
  color: var(--muted);
  font-size: 0.95rem;
}

.advance-hint {
  margin-top: 16px;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  font-size: 1.05rem;
  font-weight: 800;
  padding: 9px 20px;
  border-radius: 999px;
  box-shadow: var(--shadow-sm);
  animation: advance-pulse 2.2s ease-in-out infinite;
}
/* Setting display: inline-block unconditionally on the class would beat
   the browser's default `[hidden] { display: none }` rule (author CSS
   wins over the UA stylesheet at equal specificity), so the hint would
   stay visible even with the hidden attribute set — exactly the bug
   reported. Only apply it once the element is actually shown. */
.advance-hint:not([hidden]) {
  display: inline-block;
}

@keyframes advance-pulse {
  0%, 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(0, 0, 0, 0); }
  50% { transform: scale(1.015); box-shadow: 0 0 0 3px rgba(0, 0, 0, 0); }
}

.session-message {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--correct);
}

/* ========================================================================
   Keyboard, keys, and the animated finger-position hands overlaid on it.
   This section is intentionally left as-is — geometry and colors here are
   tuned carefully and shouldn't be touched by unrelated styling passes.
   ======================================================================== */

.keyboard {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  margin-bottom: 16px;
}

.kb-row {
  display: flex;
  gap: 6px;
}

.key {
  width: 42px;
  height: 42px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
  font-weight: 600;
  color: #1a1a1a;
  background: var(--f-thumb);
  opacity: 0.55;
  transition: transform 0.1s ease, opacity 0.15s ease, box-shadow 0.15s ease;
  user-select: none;
}

/* Shared finger-color classes — used by both keyboard keys and the hand graphic. */
.f-l-pinky { background: var(--f-l-pinky); }
.f-l-ring { background: var(--f-l-ring); }
.f-l-middle { background: var(--f-l-middle); }
.f-l-index { background: var(--f-l-index); }
.f-r-index { background: var(--f-r-index); }
.f-r-middle { background: var(--f-r-middle); }
.f-r-ring { background: var(--f-r-ring); }
.f-r-pinky { background: var(--f-r-pinky); }
.f-thumb { background: var(--f-thumb); }

.key.active {
  opacity: 1;
  transform: scale(1.15);
  box-shadow: 0 0 0 3px var(--accent);
}

/* ---------- Animated finger-position hands, overlaid on the keyboard ---------- */
/* Each hand is a static .palm (positioned once, doesn't move) plus five
   .finger-limb bars anchored at a fixed base point near the palm. JS sets
   each limb's width (= reach distance) and a translate+rotate transform
   (base point technique: transform-origin at the left edge, translate to
   the base, rotate toward the target key) so it visually stretches from
   the palm up to whichever key it's pressing — home row at rest, or
   further for any other key — instead of a disconnected floating block. */

.keyboard-wrap {
  position: relative;
  display: block;
  width: fit-content;
  margin: 0 auto;
}

.hands {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.palm {
  position: absolute;
  top: 0;
  left: 0;
  width: 92px;
  height: 46px;
  margin-left: -46px;
  border-radius: 40% 40% 55% 55% / 55% 55% 45% 45%;
  background: var(--border);
  opacity: 0.4;
  transition: transform 0.16s ease;
}

.finger-limb {
  position: absolute;
  top: -7px;
  left: 0;
  height: 14px;
  border-radius: 7px;
  /* Resting fingers are a bit more see-through and only slightly less
     saturated than the active one — enough that a resting finger is
     obviously not "press this now," and enough to read the letter under
     it, but still close enough to the finger's true color that the
     color-matches-its-keys teaching effect (the whole point) holds up
     even at rest. Only .ready gets fully bright/saturated. */
  opacity: 0.18;
  filter: saturate(0.8) brightness(0.97);
  transform-origin: 0% 50%;
  transition: transform 0.16s ease, width 0.16s ease, opacity 0.15s ease, box-shadow 0.15s ease, filter 0.12s ease;
}

.finger-limb::after {
  /* fingertip cap */
  content: '';
  position: absolute;
  right: -5px;
  top: -1px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: inherit;
}

.finger-limb.thumb {
  height: 16px;
  top: -8px;
}

.finger-limb.ready {
  /* Even the active finger stays a little see-through — fully opaque
     (opacity: 1) was still hiding the letter on the key you're about to
     press, which is the one moment legibility matters most. */
  opacity: 0.78;
  filter: saturate(1) brightness(1.15);
  box-shadow: 0 0 0 2px var(--accent), 0 3px 6px rgba(0, 0, 0, 0.25);
}

.finger-limb.ready-shift {
  box-shadow: 0 0 0 2px var(--accent);
}

.finger-limb.tap {
  opacity: 0.82;
  filter: saturate(1) brightness(1.4);
  box-shadow: 0 0 0 3px var(--accent);
}

.finger-limb.tap-error {
  opacity: 0.82;
  filter: saturate(1) brightness(1.1);
  box-shadow: 0 0 0 3px var(--error);
}

.key.error {
  opacity: 1;
  box-shadow: 0 0 0 3px var(--error);
}

.key.space {
  width: 220px;
}

.key.new-key::after {
  content: '';
  position: absolute;
}

.finger-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  font-size: 0.8rem;
  color: var(--muted);
}

.finger-legend span {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.legend-swatch {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  display: inline-block;
}

/* ========================================================================
   End of the untouched keyboard/hands block.
   ======================================================================== */

.keyboard-card {
  padding-bottom: 18px;
}

.keyboard-card .finger-legend {
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

/* ---------- Dashboard ---------- */

.dashboard-title {
  margin: 0 0 4px;
  font-size: 1.1rem;
}

.dashboard table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 10px;
  font-size: 0.85rem;
}
.dashboard td, .dashboard th {
  padding: 8px 10px;
  text-align: left;
}
.dashboard th {
  color: var(--muted);
  font-weight: 700;
  border-bottom: 2px solid var(--border);
}
.dashboard tr:not(:first-child) td { border-top: 1px solid var(--border); }
.dashboard tr:hover td { background: color-mix(in srgb, var(--accent) 6%, transparent); }

.muted { color: var(--muted); font-size: 0.85rem; }

.badge {
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 999px;
  padding: 2px 10px;
  margin-left: 6px;
  vertical-align: middle;
}

/* ---------- AI Lesson Lab ---------- */

.ai-lab h2 { margin: 0 0 6px; font-size: 1.05rem; }

.ai-lab-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin: 12px 0;
}

.ai-lab-controls input[type="text"] {
  flex: 1;
  min-width: 180px;
  background: var(--bg);
}

#aiLessonList {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.ai-saved-lesson {
  font-size: 0.8rem;
  border-radius: 999px;
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  opacity: 0;
  overflow: hidden;
}

footer {
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
  padding: 24px 20px 32px;
}

@media (max-width: 480px) {
  .word-display { font-size: 1.8rem; }
  .key { width: 28px; height: 28px; font-size: 0.75rem; }
  .key.space { width: 140px; }
  .card { padding: 16px; border-radius: var(--radius-md); }
  .topbar { padding: 12px 16px; }
}
