/* ChonkMath practice app: shared design system.
   Used by the student app (index.html) and the parent dashboard (parent.html).
   Tokens first, then base elements, then components. */

:root {
  /* Paper and ink */
  --bg: #faf7f0;
  --surface: #ffffff;
  --surface-2: #f3eee3;
  --surface-3: #e9e2d3;
  --ink: #0f172a;
  --ink-2: #475569;
  --ink-3: #64748b;
  --line: #e5decf;
  --line-strong: #cfc6b3;

  /* Brand and meaning */
  --gold: #f59e0b;
  --gold-strong: #d97706;
  --gold-soft: #fef3c7;
  --gold-ink: #7c3e06;
  --on-gold: #0f172a;
  --green: #15803d;
  --green-soft: #dcfce7;
  --red: #c81e1e;
  --red-soft: #fee2e2;
  --blue: #1d4ed8;
  --blue-soft: #dbeafe;
  --mul: #c2410c;      /* multiplication accent */
  --mul-soft: #ffedd5;
  --div: #6d28d9;      /* division accent */
  --div-soft: #ede9fe;

  /* Fact status ramp: empty, then warm to cool as a fact is learned */
  --st-new: #ece6d8;
  --st-new-ink: #64748b;
  --st-learning: #fdba74;
  --st-learning-ink: #7c2d12;
  --st-practicing: #fde047;
  --st-practicing-ink: #713f12;
  --st-fluent: #86efac;
  --st-fluent-ink: #14532d;
  --st-mastered: #15803d;
  --st-mastered-ink: #ffffff;

  --shadow-1: 0 1px 2px rgba(15, 23, 42, 0.06), 0 1px 1px rgba(15, 23, 42, 0.04);
  --shadow-2: 0 6px 20px rgba(15, 23, 42, 0.08), 0 2px 6px rgba(15, 23, 42, 0.05);
  --shadow-key: 0 2px 0 var(--line-strong);

  --radius-s: 10px;
  --radius-m: 16px;
  --radius-l: 24px;

  --font: "Lexend", ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --gutter: 16px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1220;
    --surface: #131c2e;
    --surface-2: #1b2740;
    --surface-3: #24324f;
    --ink: #f1f5f9;
    --ink-2: #b4bfd1;
    --ink-3: #8795ab;
    --line: #26334d;
    --line-strong: #37476a;
    --gold-soft: #3a2a08;
    --gold-ink: #fcd34d;
    --green: #4ade80;
    --green-soft: #0f3320;
    --red: #ff7b7b;
    --red-soft: #3f1518;
    --blue: #7fb0ff;
    --blue-soft: #14274d;
    --mul: #fb923c;
    --mul-soft: #3b1f0b;
    --div: #b79cf5;
    --div-soft: #2a1c52;
    --st-new: #1b2740;
    --st-new-ink: #8795ab;
    --st-learning: #c2410c;
    --st-learning-ink: #fff7ed;
    --st-practicing: #ca8a04;
    --st-practicing-ink: #fffbeb;
    --st-fluent: #15803d;
    --st-fluent-ink: #f0fdf4;
    --st-mastered: #4ade80;
    --st-mastered-ink: #052e16;
    --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-2: 0 8px 24px rgba(0, 0, 0, 0.45);
    --shadow-key: 0 2px 0 #0a101c;
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

h1, h2, h3, p { margin: 0; }
h1 { font-size: 1.6rem; font-weight: 700; letter-spacing: -0.01em; line-height: 1.2; }
h2 { font-size: 1.15rem; font-weight: 650; letter-spacing: -0.005em; }
h3 { font-size: 0.8rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.07em; color: var(--ink-3); }
a { color: inherit; }
button { font: inherit; color: inherit; }
.num { font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; }
.muted { color: var(--ink-2); }
.small { font-size: 0.875rem; }
.center { text-align: center; }
[hidden] { display: none !important; }

:focus-visible {
  outline: 3px solid var(--gold);
  outline-offset: 2px;
  border-radius: 6px;
}

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

/* ── Layout ─────────────────────────────────────────────── */

.page {
  width: 100%;
  max-width: 1040px;
  margin: 0 auto;
  padding: var(--gutter) var(--gutter) 48px;
}
.page.narrow { max-width: 560px; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.stack.tight { gap: 8px; }
.stack.center { align-items: center; }
.stack.center > .card, .stack.center > form { align-self: stretch; }
.row { display: flex; align-items: center; gap: 12px; }
.row.wrap { flex-wrap: wrap; }
.spread { justify-content: space-between; }
.grow { flex: 1 1 auto; min-width: 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-m);
  box-shadow: var(--shadow-1);
  padding: 20px;
}
.card.flush { padding: 0; overflow: hidden; }
.card.tinted { background: var(--surface-2); box-shadow: none; }

/* ── Buttons ────────────────────────────────────────────── */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 20px;
  border-radius: 999px; border: 1px solid transparent;
  background: var(--surface); border-color: var(--line-strong);
  font-weight: 600; font-size: 1rem; text-decoration: none; white-space: nowrap;
  cursor: pointer; user-select: none; touch-action: manipulation;
  transition: transform 80ms ease, background 120ms ease, box-shadow 120ms ease;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px) scale(0.985); }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }
.btn.primary {
  background: var(--gold); border-color: var(--gold-strong); color: var(--on-gold);
  box-shadow: 0 2px 0 var(--gold-strong);
}
.btn.primary:hover { background: #fbb024; }
.btn.primary:active { box-shadow: 0 0 0 var(--gold-strong); transform: translateY(2px); }
.btn.dark { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.btn.dark:hover { background: var(--ink); opacity: 0.9; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.big { min-height: 58px; font-size: 1.1rem; padding: 0 28px; }
.btn.small { min-height: 36px; padding: 0 14px; font-size: 0.875rem; }
.btn.block { width: 100%; }
.btn.danger { color: var(--red); }
.icon-btn {
  width: 44px; height: 44px; border-radius: 50%; border: 0; background: transparent;
  display: inline-grid; place-items: center; cursor: pointer; touch-action: manipulation;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 22px; height: 22px; }

/* ── Forms ──────────────────────────────────────────────── */

.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 0.875rem; font-weight: 600; color: var(--ink-2); }
.input, select.input {
  min-height: 52px; padding: 0 16px; width: 100%;
  border-radius: var(--radius-s); border: 1.5px solid var(--line-strong);
  background: var(--surface); color: var(--ink);
  font: inherit; font-size: 1.05rem;
}
.input:focus { outline: none; border-color: var(--gold); box-shadow: 0 0 0 3px var(--gold-soft); }
.input.pin { letter-spacing: 0.5em; font-size: 1.5rem; font-weight: 700; text-align: center; }
.form-error {
  background: var(--red-soft); color: var(--red); border-radius: var(--radius-s);
  padding: 10px 14px; font-size: 0.9rem; font-weight: 500;
}
.form-ok {
  background: var(--green-soft); color: var(--green); border-radius: var(--radius-s);
  padding: 10px 14px; font-size: 0.9rem; font-weight: 500;
}

/* ── Chips, badges, meters ──────────────────────────────── */

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 999px;
  background: var(--surface-2); color: var(--ink-2);
  font-size: 0.8rem; font-weight: 600; white-space: nowrap;
}
.chip.gold { background: var(--gold-soft); color: var(--gold-ink); }
.chip.green { background: var(--green-soft); color: var(--green); }
.chip.red { background: var(--red-soft); color: var(--red); }
.chip.mul { background: var(--mul-soft); color: var(--mul); }
.chip.div { background: var(--div-soft); color: var(--div); }

.meter { height: 10px; border-radius: 999px; background: var(--surface-3); overflow: hidden; }
.meter > i {
  display: block; height: 100%; width: 0; border-radius: inherit; background: var(--gold);
  transition: width 400ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
.meter.green > i { background: var(--green); }
.meter.thin { height: 6px; }

/* ── Fact board (heat map) ──────────────────────────────── */

.board { display: grid; gap: 3px; width: 100%; }
.board.mul { grid-template-columns: 1.4em repeat(11, minmax(0, 1fr)); }
.board.div { grid-template-columns: 1.4em repeat(10, minmax(0, 1fr)); }
.board .axis {
  display: grid; place-items: center;
  font-size: 0.7rem; font-weight: 600; color: var(--ink-3);
}
.tile {
  aspect-ratio: 1; min-width: 0; padding: 0; border: 0; border-radius: 5px;
  display: grid; place-items: center;
  font-size: clamp(0.55rem, 1.9vw, 0.8rem); font-weight: 600;
  background: var(--st-new); color: var(--st-new-ink);
  cursor: pointer; touch-action: manipulation;
  transition: transform 80ms ease;
}
.tile:hover { transform: scale(1.12); z-index: 1; }
.tile[data-status="learning"] { background: var(--st-learning); color: var(--st-learning-ink); }
.tile[data-status="practicing"] { background: var(--st-practicing); color: var(--st-practicing-ink); }
.tile[data-status="fluent"] { background: var(--st-fluent); color: var(--st-fluent-ink); }
.tile[data-status="mastered"] { background: var(--st-mastered); color: var(--st-mastered-ink); }
.tile[aria-pressed="true"] { outline: 3px solid var(--ink); outline-offset: 1px; }

.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 0.8rem; color: var(--ink-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 12px; height: 12px; border-radius: 3px; background: var(--st-new); }
.legend i[data-status="learning"] { background: var(--st-learning); }
.legend i[data-status="practicing"] { background: var(--st-practicing); }
.legend i[data-status="fluent"] { background: var(--st-fluent); }
.legend i[data-status="mastered"] { background: var(--st-mastered); }

/* ── Problems in worksheet form ─────────────────────────── */

.problem {
  font-weight: 700; letter-spacing: 0.01em; line-height: 1;
  font-variant-numeric: tabular-nums; white-space: nowrap;
}
.problem .op { color: var(--ink-3); font-weight: 500; margin: 0 0.18em; }
.problem .slot {
  display: inline-block; min-width: 1.3em; padding: 0 0.12em 0.04em; text-align: center;
  border-bottom: 0.09em solid var(--line-strong); color: var(--ink);
}
.problem .slot.empty::after { content: "\00a0"; }
.problem.stack-mul {
  display: inline-grid; justify-items: end; row-gap: 0.12em; text-align: right;
}
.problem.stack-mul .bar { width: 100%; min-width: 2.2em; height: 0.07em; background: var(--ink); border-radius: 2px; }
.problem.stack-mul .slot { border-bottom: 0; min-width: 2em; text-align: right; min-height: 1em; }
.problem.bracket {
  display: inline-grid; grid-template-columns: auto auto; align-items: end; column-gap: 0.1em;
}
.problem.bracket .quotient { grid-column: 2; text-align: right; padding: 0 0.15em 0.1em; min-height: 1.1em; }
.problem.bracket .slot { border-bottom: 0; text-align: right; min-width: 1.6em; }
.problem.bracket .divisor { grid-column: 1; padding-bottom: 0.06em; }
.problem.bracket .dividend {
  grid-column: 2; position: relative; padding: 0.12em 0.2em 0.06em 0.32em;
  border-top: 0.07em solid var(--ink);
}
.problem.bracket .dividend::before {
  content: ""; position: absolute; left: -0.02em; top: -0.07em; bottom: 0;
  width: 0.3em; border-right: 0.07em solid var(--ink); border-radius: 0 60% 60% 0 / 0 50% 50% 0;
}

/* ── Keypad ─────────────────────────────────────────────── */

.keypad {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
  width: 100%; max-width: 420px; margin: 0 auto;
}
.key {
  height: clamp(52px, 8.2dvh, 72px);
  border-radius: 14px; border: 1px solid var(--line-strong);
  background: var(--surface); box-shadow: var(--shadow-key);
  font-size: 1.6rem; font-weight: 600; font-variant-numeric: tabular-nums;
  display: grid; place-items: center;
  cursor: pointer; user-select: none; -webkit-user-select: none; touch-action: manipulation;
}
.key:active, .key.down { transform: translateY(2px); box-shadow: none; background: var(--surface-2); }
.key.fn { background: var(--surface-2); font-size: 1rem; }
.key.go { background: var(--gold); border-color: var(--gold-strong); color: var(--on-gold); box-shadow: 0 2px 0 var(--gold-strong); font-size: 1rem; }
.key svg { width: 26px; height: 26px; }

/* ── Tables ─────────────────────────────────────────────── */

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.data { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
table.data th {
  text-align: left; font-size: 0.75rem; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.06em; color: var(--ink-3); padding: 10px 12px;
  border-bottom: 1px solid var(--line); white-space: nowrap;
}
table.data td { padding: 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.data tr:last-child td { border-bottom: 0; }
table.data td.n, table.data th.n { text-align: right; font-variant-numeric: tabular-nums; }
table.data tr.clickable { cursor: pointer; }
table.data tr.clickable:hover td { background: var(--surface-2); }

/* ── Dialog ─────────────────────────────────────────────── */

dialog.sheet {
  border: 0; padding: 0; background: var(--surface); color: var(--ink);
  border-radius: var(--radius-l); box-shadow: var(--shadow-2);
  width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
}
dialog.sheet::backdrop { background: rgba(15, 23, 42, 0.5); backdrop-filter: blur(2px); }
dialog.sheet .sheet-body { padding: 24px; overflow-y: auto; max-height: calc(100dvh - 24px); }

/* ── Motion ─────────────────────────────────────────────── */

@keyframes pop { 0% { transform: scale(0.9); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
@keyframes rise { 0% { transform: translateY(10px); opacity: 0; } 100% { transform: none; opacity: 1; } }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px); } 40% { transform: translateX(7px); }
  60% { transform: translateX(-5px); } 80% { transform: translateX(3px); }
}
.pop { animation: pop 180ms cubic-bezier(0.2, 0.9, 0.3, 1.2) both; }
.rise { animation: rise 260ms ease both; }
.shake { animation: shake 320ms ease both; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; transition-duration: 1ms !important; }
}
