/* ============================================================
   TypeForge — styles
   Theme system: CSS variables swapped via body[data-theme].
   ============================================================ */

* { box-sizing: border-box; }
html { color-scheme: dark; }

:root{
  --font-ui: 'Poppins', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, monospace;
  --radius: 14px;
  --radius-sm: 9px;
}

/* ---------- Theme palettes ---------- */
body[data-theme="violet"]{
  --bg: #0e0b1a; --bg2: #16112b; --surface: #1c1638; --surface2: #241d47;
  --line: #332a5c; --ink: #eee7ff; --ink-dim: #a89fd6; --ink-faint: #6c6296;
  --accent: #8b7bff; --accent2: #ff7bd0; --good: #4ade80; --bad: #ff6b6b; --warn: #ffb84d;
}
body[data-theme="mint"]{
  --bg: #06120f; --bg2: #0a1a15; --surface: #0f2119; --surface2: #142c22;
  --line: #1f4534; --ink: #e4fff2; --ink-dim: #8fd6b8; --ink-faint: #4f8f74;
  --accent: #34e5a8; --accent2: #ffd166; --good: #34e5a8; --bad: #ff6b6b; --warn: #ffd166;
}
body[data-theme="sunset"]{
  --bg: #170b10; --bg2: #23101a; --surface: #2c1421; --surface2: #391a2a;
  --line: #4d2338; --ink: #ffece9; --ink-dim: #e3a6a0; --ink-faint: #9c6c68;
  --accent: #ff7a59; --accent2: #ffce54; --good: #4ade80; --bad: #ff5c7a; --warn: #ffce54;
}
body[data-theme="ocean"]{
  --bg: #05121c; --bg2: #081b29; --surface: #0d2436; --surface2: #113046;
  --line: #1c4159; --ink: #e6f6ff; --ink-dim: #93c4dd; --ink-faint: #547f96;
  --accent: #38bdf8; --accent2: #a78bfa; --good: #4ade80; --bad: #ff6b6b; --warn: #fbbf24;
}
body[data-theme="mono"]{
  --bg: #0b0b0c; --bg2: #131314; --surface: #19191b; --surface2: #212123;
  --line: #313134; --ink: #f2f2f3; --ink-dim: #a6a6ab; --ink-faint: #6a6a70;
  --accent: #e5e5ea; --accent2: #8b8b92; --good: #4ade80; --bad: #ff6b6b; --warn: #ffb84d;
}
body[data-theme="light"]{
  color-scheme: light;
  --bg: #f6f5fb; --bg2: #ffffff; --surface: #ffffff; --surface2: #f0eefb;
  --line: #dcd8ef; --ink: #1c1730; --ink-dim: #5c5580; --ink-faint: #948dbb;
  --accent: #6c5ce7; --accent2: #e6499a; --good: #16a34a; --bad: #dc2626; --warn: #d97706;
}

body{
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  -webkit-font-smoothing: antialiased;
  position: relative;
  overflow-x: hidden;
}

.bg-decor{
  position: fixed; inset: 0; z-index: -1; pointer-events: none;
  background:
    radial-gradient(600px 400px at 12% -5%, color-mix(in srgb, var(--accent) 22%, transparent), transparent 60%),
    radial-gradient(500px 380px at 100% 10%, color-mix(in srgb, var(--accent2) 16%, transparent), transparent 60%),
    var(--bg);
}

a { color: var(--accent); }

/* ---------- Topbar ---------- */
.topbar{
  display: flex; align-items: center; gap: 20px;
  padding: 16px clamp(14px, 4vw, 40px);
  flex-wrap: wrap;
}
.brand{ display: flex; align-items: center; gap: 8px; font-weight: 800; font-size: 1.15rem; }
.brand-mark{
  width: 34px; height: 34px; display: grid; place-items: center;
  background: linear-gradient(140deg, var(--accent), var(--accent2));
  border-radius: 10px; color: #fff; font-size: 1rem;
}
.tabs{ display: flex; gap: 4px; background: var(--surface); padding: 4px; border-radius: 12px; border: 1px solid var(--line); }
.tab{
  border: none; background: transparent; color: var(--ink-dim); font-family: var(--font-ui);
  font-weight: 600; font-size: .85rem; padding: 8px 14px; border-radius: 9px; cursor: pointer;
  transition: background .15s, color .15s;
}
.tab:hover{ color: var(--ink); }
.tab.active{ background: var(--accent); color: #fff; }

.top-actions{ display: flex; align-items: center; gap: 10px; margin-left: auto; }
.streak-pill{
  display: flex; align-items: center; gap: 5px; background: var(--surface); border: 1px solid var(--line);
  padding: 7px 12px; border-radius: 999px; font-weight: 700; font-size: .85rem;
}
.icon-btn{
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface);
  cursor: pointer; font-size: 1rem; color: var(--ink); display: grid; place-items: center;
  transition: transform .12s, border-color .12s;
}
.icon-btn:hover{ border-color: var(--accent); transform: translateY(-1px); }
.icon-btn[aria-pressed="false"]{ opacity: .45; }

.popover{
  position: fixed; top: 60px; right: clamp(14px, 4vw, 40px); z-index: 50;
  background: var(--surface2); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; box-shadow: 0 20px 50px rgba(0,0,0,.4); width: 220px;
}
.popover-title{ font-weight: 700; font-size: .8rem; color: var(--ink-dim); margin-bottom: 10px; text-transform: uppercase; letter-spacing: .04em; }
.theme-grid{ display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.theme-swatch{
  height: 44px; border-radius: 9px; cursor: pointer; border: 2px solid transparent;
  display: grid; place-items: center; font-size: .65rem; font-weight: 700; color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,.5);
}
.theme-swatch.active{ border-color: var(--accent); }

/* ---------- Main ---------- */
main{ max-width: 980px; margin: 0 auto; padding: 10px clamp(14px, 4vw, 40px) 60px; }
.view-title{ font-size: 1.4rem; margin: 10px 0 4px; }
.view-sub{ color: var(--ink-dim); margin: 0 0 20px; font-size: .9rem; }

/* ---------- Mode bar ---------- */
.mode-bar{
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  background: var(--surface); border: 1px solid var(--line); border-radius: 999px;
  padding: 8px 14px; margin: 18px 0 30px; justify-content: center;
}
.seg-group{ display: flex; gap: 2px; }
.seg{
  border: none; background: transparent; color: var(--ink-faint); font-family: var(--font-mono);
  font-size: .8rem; padding: 6px 11px; border-radius: 999px; cursor: pointer; font-weight: 600;
  transition: background .15s, color .15s;
}
.seg:hover{ color: var(--ink-dim); }
.seg.active{ background: var(--accent); color: #fff; }
.divider{ width: 1px; height: 18px; background: var(--line); }
.chip-toggle{
  display: flex; align-items: center; gap: 6px; font-family: var(--font-mono); font-size: .78rem;
  color: var(--ink-faint); cursor: pointer; padding: 4px 8px;
}
.chip-toggle input{ accent-color: var(--accent); }
.chip-toggle:has(input:checked){ color: var(--accent); }

.custom-textarea{
  width: 100%; min-height: 100px; margin-bottom: 20px; resize: vertical;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  color: var(--ink); font-family: var(--font-mono); font-size: .95rem; padding: 14px;
}
.custom-textarea:focus{ outline: 2px solid var(--accent); }

/* ---------- HUD ---------- */
.hud{ display: flex; gap: 34px; justify-content: center; margin-bottom: 22px; min-height: 54px; }
.hud-item{ display: flex; flex-direction: column; align-items: center; opacity: .35; transition: opacity .2s; }
.hud-item.live{ opacity: 1; }
.hud-value{ font-family: var(--font-mono); font-size: 2rem; font-weight: 700; color: var(--accent); line-height: 1; }
.hud-label{ font-size: .72rem; color: var(--ink-faint); text-transform: uppercase; letter-spacing: .08em; margin-top: 4px; }

/* ---------- Typing stage ---------- */
.type-stage{
  position: relative; min-height: 150px; display: flex; align-items: center; justify-content: center;
}
.hidden-input{
  position: absolute; opacity: 0; pointer-events: none; width: 1px; height: 1px;
}
.focus-veil{
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--bg) 55%, transparent); backdrop-filter: blur(3px);
  border-radius: var(--radius); cursor: text; color: var(--ink-dim); font-size: .9rem; z-index: 5;
}
.passage-viewport{
  font-size: clamp(1.25rem, 2.6vw, 1.7rem); line-height: 2.1;
  max-height: 4.4em; overflow: hidden; width: 100%;
}
.passage{
  font-family: var(--font-mono); font-size: clamp(1.25rem, 2.6vw, 1.7rem); line-height: 2.1;
  letter-spacing: .3px; color: var(--ink-faint);
  outline: none; user-select: none; width: 100%; text-align: left;
}
.passage .word{ display: inline-block; white-space: nowrap; }
.passage .char{ position: relative; }
.passage .char.space{ display: inline-block; min-width: .5em; }
.passage .char.correct{ color: var(--ink); }
.passage .char.incorrect{ color: var(--bad); text-decoration: underline; text-decoration-color: var(--bad); }
.passage .char.current::before{
  content: ""; position: absolute; left: -1px; top: -2px; bottom: -2px; width: 2px;
  background: var(--accent); animation: caret-blink 1s step-end infinite;
}
.passage .char.extra{ color: var(--warn); }
@keyframes caret-blink{ 50% { opacity: 0; } }

.stage-actions{ display: flex; justify-content: center; margin: 22px 0 8px; }

.btn{
  font-family: var(--font-ui); font-weight: 600; font-size: .85rem; border-radius: 10px;
  padding: 10px 18px; cursor: pointer; border: 1px solid var(--line); background: var(--surface);
  color: var(--ink); display: inline-flex; align-items: center; gap: 8px; transition: transform .12s, border-color .12s;
}
.btn:hover{ border-color: var(--accent); transform: translateY(-1px); }
.btn.primary{ background: var(--accent); color: #fff; border-color: transparent; }
.btn.ghost.danger{ color: var(--bad); }
.btn kbd{
  font-family: var(--font-mono); font-size: .68rem; background: var(--surface2); padding: 2px 6px;
  border-radius: 5px; border: 1px solid var(--line); color: var(--ink-faint);
}

/* ---------- Keyboard ---------- */
.keyboard-wrap{ margin-top: 34px; display: flex; justify-content: center; }
.keyboard{ display: flex; flex-direction: column; gap: 6px; }
.kb-row{ display: flex; gap: 6px; justify-content: center; }
.key{
  min-width: 34px; height: 34px; border-radius: 7px; background: var(--surface);
  border: 1px solid var(--line); display: grid; place-items: center; font-size: .68rem;
  font-family: var(--font-mono); color: var(--ink-faint); transition: background .1s, transform .06s, color .1s;
}
.key.w-1_5{ min-width: 51px; } .key.w-1_75{ min-width: 60px; } .key.w-2{ min-width: 68px; } .key.w-2_25{ min-width: 76px; } .key.w-6_25{ min-width: 210px; }
.key.home{ box-shadow: inset 0 0 0 1px var(--accent2); }
.key.active{ background: var(--accent); color: #fff; transform: scale(.94); }
.key.next-hint{ border-color: var(--accent); color: var(--accent); }
.key.finger-l1, .key.finger-r1{ } /* reserved for future finger coloring */

/* Trouble-key heatmap reuses .keyboard styling with intensity overlay.
   #heatmapKeyboard receives the same .kb-row children buildKeyboard()
   produces for #keyboard, so it needs the same flex layout, plus
   horizontal scroll since it isn't hidden on narrow screens like the
   practice keyboard is. */
.heatmap-keyboard{
  display: flex; flex-direction: column; gap: 6px;
  overflow-x: auto; padding-bottom: 4px; width: max-content; max-width: 100%;
}
.heatmap-keyboard .key{ position: relative; }
.heatmap-keyboard .key .heat{
  position: absolute; inset: 0; border-radius: 7px; background: var(--bad); opacity: var(--h, 0); pointer-events: none;
}

/* ---------- Results overlay ---------- */
.results-overlay{
  position: fixed; inset: 0; background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: blur(6px); display: flex; align-items: center; justify-content: center;
  z-index: 100; padding: 20px;
}
.results-card{
  background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 28px;
  width: 100%; max-width: 640px; max-height: 90vh; overflow-y: auto;
}
.results-header{ display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; flex-wrap: wrap; gap: 8px; }
.results-header h2{ margin: 0; font-size: 1.3rem; }
.pb-badge{
  background: linear-gradient(140deg, var(--accent), var(--accent2)); color: #fff; font-size: .75rem;
  font-weight: 700; padding: 6px 12px; border-radius: 999px;
}
.results-grid{ display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 18px; }
.rstat{ background: var(--surface2); border-radius: 12px; padding: 14px 10px; text-align: center; }
.rval{ display: block; font-family: var(--font-mono); font-size: 1.5rem; font-weight: 700; color: var(--accent); }
.rlabel{ font-size: .7rem; color: var(--ink-faint); text-transform: uppercase; letter-spacing: .05em; }
#resultsChart{ width: 100%; height: 140px; margin-bottom: 10px; }
.unlocked-row{ display: flex; align-items: center; gap: 10px; margin: 14px 0; flex-wrap: wrap; }
.unlocked-label{ font-size: .8rem; color: var(--ink-dim); font-weight: 600; }
.unlocked-badges{ display: flex; gap: 8px; flex-wrap: wrap; }
.unlocked-badges .mini-badge{
  background: var(--surface2); border: 1px solid var(--accent); border-radius: 999px; padding: 5px 10px;
  font-size: .72rem; font-weight: 700; color: var(--accent);
}
.results-actions{ display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap; }

/* ---------- Lessons ---------- */
.lesson-grid{ display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }
.lesson-card{
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px;
  cursor: pointer; transition: transform .12s, border-color .12s; position: relative;
}
.lesson-card:hover{ transform: translateY(-2px); border-color: var(--accent); }
.lesson-card h4{ margin: 0 0 6px; font-size: 1rem; }
.lesson-card p{ margin: 0; color: var(--ink-dim); font-size: .82rem; }
.lesson-card .lesson-keys{ font-family: var(--font-mono); color: var(--ink-faint); font-size: .74rem; margin-top: 10px; }
.lesson-card .done-check{
  position: absolute; top: 12px; right: 12px; color: var(--good); font-size: 1rem; display: none;
}
.lesson-card.done .done-check{ display: block; }

/* ---------- Stats ---------- */
.stats-summary{ display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 12px; margin-bottom: 24px; }
.stats-summary .sitem{ background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 14px; text-align: center; }
.stats-summary .sval{ display: block; font-family: var(--font-mono); font-size: 1.4rem; font-weight: 700; color: var(--accent); }
.stats-summary .slabel{ font-size: .7rem; color: var(--ink-faint); text-transform: uppercase; letter-spacing: .04em; }
.stats-panels{ display: grid; grid-template-columns: 1.3fr 1fr; gap: 16px; margin-bottom: 16px; }
@media (max-width: 760px){ .stats-panels{ grid-template-columns: 1fr; } }
.panel{ background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; min-width: 0; }
.panel h3{ margin: 0 0 4px; font-size: .95rem; }
.panel-sub{ margin: 0 0 12px; color: var(--ink-faint); font-size: .78rem; }
#historyChart, #resultsChart{ max-width: 100%; }
.history-table-wrap{ overflow-x: auto; }
.history-table{ width: 100%; border-collapse: collapse; font-size: .85rem; }
.history-table th{ text-align: left; color: var(--ink-faint); font-weight: 600; padding: 6px 10px; font-size: .72rem; text-transform: uppercase; }
.history-table td{ padding: 8px 10px; border-top: 1px solid var(--line); font-family: var(--font-mono); }

/* ---------- Badges ---------- */
.badge-grid{ display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 14px; }
.badge-card{
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px;
  opacity: .45; filter: grayscale(1); transition: opacity .2s, filter .2s;
}
.badge-card.unlocked{ opacity: 1; filter: none; border-color: var(--accent); }
.badge-card .b-icon{ font-size: 1.5rem; }
.badge-card h4{ margin: 8px 0 4px; font-size: .92rem; }
.badge-card p{ margin: 0; font-size: .78rem; color: var(--ink-dim); }

/* ---------- Toast ---------- */
.toast{
  position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%);
  background: var(--surface2); border: 1px solid var(--accent); color: var(--ink);
  padding: 12px 20px; border-radius: 999px; font-size: .85rem; font-weight: 600; z-index: 200;
  box-shadow: 0 10px 30px rgba(0,0,0,.4);
  animation: toast-in .18s ease-out;
}
@keyframes toast-in{ from{ opacity: 0; transform: translate(-50%, 10px);} to{ opacity: 1; transform: translate(-50%, 0);} }

.site-footer{ text-align: center; color: var(--ink-faint); font-size: .76rem; padding: 20px; }

/* ---------- Misc / responsive ---------- */
[hidden]{ display: none !important; }
@media (max-width: 640px){
  .keyboard-wrap{ display: none; }
  .results-grid{ grid-template-columns: repeat(2, 1fr); }
  .hud{ gap: 20px; }
  .mode-bar{ border-radius: 22px; justify-content: flex-start; }
}

/* Confetti canvas overlay */
#confettiCanvas{ position: fixed; inset: 0; pointer-events: none; z-index: 300; }
