/* ============================================================
   Math Academy — Apple-inspired theme
   Light by default, adapts to system dark mode.
   Color-rich accents per topic, calm high-readability reading.
   ============================================================ */

:root {
  /* light appearance */
  --bg: #f5f5f7;
  --panel: #ffffff;
  --panel-2: #eef0f4;
  --text: #1d1d1f;
  --text-soft: #38383c;
  --muted: #6e6e73;
  --border: rgba(0, 0, 0, 0.10);
  --hair: rgba(0, 0, 0, 0.07);
  --sidebar-bg: rgba(255, 255, 255, 0.72);
  --code-bg: rgba(0, 0, 0, 0.05);

  --primary: #0071e3;
  --primary-2: #6e5ef0;
  --indigo: #5e5ce6;
  --good: #34c759;
  --good-ink: #1c8f43;
  --warn: #ff9f0a;
  --bad: #ff3b30;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04), 0 6px 20px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 12px 48px rgba(0, 0, 0, 0.14);

  --radius: 22px;
  --radius-sm: 14px;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", "Roboto Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --panel: #1c1c1e;
    --panel-2: #2c2c2e;
    --text: #f5f5f7;
    --text-soft: #d8d8de;
    --muted: #9a9aa1;
    --border: rgba(255, 255, 255, 0.14);
    --hair: rgba(255, 255, 255, 0.09);
    --sidebar-bg: rgba(22, 22, 24, 0.72);
    --code-bg: rgba(255, 255, 255, 0.09);

    --primary: #0a84ff;
    --primary-2: #8b7dff;
    --indigo: #8b88ff;
    --good: #30d158;
    --good-ink: #5be07d;
    --warn: #ffd60a;
    --bad: #ff453a;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 28px rgba(0, 0, 0, 0.45);
    --shadow-lg: 0 16px 56px rgba(0, 0, 0, 0.6);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0; min-height: 100%;
  color: var(--text);
  font-family: var(--sans);
  font-size: 17px; line-height: 1.6;
  letter-spacing: -0.01em;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  background:
    radial-gradient(900px 700px at 88% -8%, color-mix(in srgb, var(--primary) 16%, transparent), transparent 60%),
    radial-gradient(800px 600px at -10% 0%, color-mix(in srgb, var(--primary-2) 14%, transparent), transparent 55%),
    var(--bg);
  background-attachment: fixed;
}

.hidden { display: none !important; }

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: none; }

h1, h2, h3 { line-height: 1.18; font-weight: 700; letter-spacing: -0.022em; }

::selection { background: color-mix(in srgb, var(--primary) 26%, transparent); }

/* ---------------- boot / gate ---------------- */
.boot { position: fixed; inset: 0; display: grid; place-items: center; color: var(--muted); }

.gate { position: fixed; inset: 0; display: grid; place-items: center; padding: 24px; }
.gate-card {
  max-width: 480px; text-align: center; background: var(--panel);
  border: 1px solid var(--hair); border-radius: 30px; padding: 52px 42px;
  box-shadow: var(--shadow-lg);
}
.gate-logo {
  width: 84px; height: 84px; margin: 0 auto 22px; border-radius: 24px;
  display: grid; place-items: center; font-size: 46px; font-weight: 800; color: #fff;
  background: linear-gradient(140deg, var(--primary), var(--primary-2));
  box-shadow: 0 12px 30px color-mix(in srgb, var(--primary) 40%, transparent);
}
.gate-card h1 { margin: 0 0 12px; font-size: 2rem; }
.gate-sub { color: var(--muted); margin: 0 0 30px; font-size: 1.05rem; line-height: 1.55; }
.gate-foot { color: var(--muted); font-size: 0.85rem; margin-top: 26px; }

/* ---------------- app shell ---------------- */
.app { display: grid; grid-template-columns: 280px 1fr; min-height: 100vh; }

.sidebar {
  position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column;
  padding: 20px 14px; background: var(--sidebar-bg);
  backdrop-filter: saturate(180%) blur(22px); -webkit-backdrop-filter: saturate(180%) blur(22px);
  border-right: 1px solid var(--hair);
}
.brand { font-weight: 800; font-size: 1.18rem; display: flex; align-items: center; gap: 11px; padding: 8px 10px 20px; letter-spacing: -0.02em; }
.brand-mark {
  width: 36px; height: 36px; border-radius: 11px; display: grid; place-items: center; color: #fff; font-weight: 800;
  background: linear-gradient(140deg, var(--primary), var(--primary-2));
  box-shadow: 0 6px 16px color-mix(in srgb, var(--primary) 36%, transparent);
}

.nav { display: flex; flex-direction: column; gap: 3px; overflow-y: auto; flex: 1; padding-right: 2px; }
.nav-group { color: var(--muted); font-size: 0.7rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.07em; padding: 18px 12px 7px; }
.nav a {
  display: flex; align-items: center; gap: 11px; padding: 8px 11px; border-radius: 12px;
  color: var(--text); font-size: 0.92rem; font-weight: 500;
  transition: background 0.15s ease;
}
.nav a:hover { background: var(--hair); }
.nav a.active { background: color-mix(in srgb, var(--c) 15%, transparent); color: var(--c); font-weight: 650; }
.nav-ico {
  width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center; flex: none;
  font-size: 14px; font-weight: 700; color: #fff;
  background: linear-gradient(140deg, color-mix(in srgb, var(--c) 86%, #fff), var(--c));
  box-shadow: 0 2px 7px color-mix(in srgb, var(--c) 32%, transparent);
}
.nav-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-check { color: var(--good); font-weight: 800; font-size: 0.8rem; }

.sidebar-foot { border-top: 1px solid var(--hair); padding-top: 14px; margin-top: 8px; display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.who { color: var(--muted); font-size: 0.82rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.view { padding: 48px clamp(20px, 5vw, 72px) 80px; max-width: 1120px; outline: none; }

/* ---------------- buttons ---------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid transparent; border-radius: 980px; padding: 11px 22px;
  font: inherit; font-weight: 600; letter-spacing: -0.01em; cursor: pointer;
  background: var(--panel-2); color: var(--text);
  transition: transform 0.08s ease, background 0.16s ease, border-color 0.16s ease, box-shadow 0.16s ease;
}
.btn:hover { text-decoration: none; }
.btn:active { transform: scale(0.97); }
.btn-primary {
  background: var(--primary); color: #fff;
  box-shadow: 0 6px 18px color-mix(in srgb, var(--primary) 34%, transparent);
}
.btn-primary:hover { filter: brightness(1.06); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-ghost:hover { background: var(--hair); }
.btn-lg { padding: 14px 30px; font-size: 1.05rem; }
.btn-sm { padding: 7px 14px; font-size: 0.84rem; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------------- headings / layout ---------------- */
.page-head { margin-bottom: 32px; }
.page-head h1 { margin: 0 0 8px; font-size: 2.4rem; }
.page-head p { color: var(--muted); margin: 0; font-size: 1.08rem; }
.kicker { color: var(--c, var(--primary)); font-weight: 700; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.09em; margin-bottom: 4px; }
.section-title { font-size: 1.4rem; margin: 40px 0 16px; }

.grid { display: grid; gap: 18px; }
.grid-2 { grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); }

.card {
  background: var(--panel); border: 1px solid var(--hair); border-radius: var(--radius);
  padding: 24px; box-shadow: var(--shadow-sm);
}
.card h3 { margin: 0 0 8px; font-size: 1.15rem; }
.card .muted { color: var(--muted); font-size: 0.95rem; }

/* color-rich tiles (lessons + games) */
.tile {
  position: relative; display: flex; flex-direction: column; padding: 22px;
  border-radius: var(--radius); background: var(--panel); border: 1px solid var(--hair);
  box-shadow: var(--shadow-sm); color: var(--text); text-decoration: none; overflow: hidden;
  transition: transform 0.2s cubic-bezier(0.2, 0.7, 0.2, 1), box-shadow 0.2s ease, border-color 0.2s ease;
}
.tile::after {
  content: ""; position: absolute; left: 0; right: 0; top: 0; height: 60%;
  background: linear-gradient(180deg, color-mix(in srgb, var(--c) 8%, transparent), transparent);
  pointer-events: none;
}
.tile:hover {
  transform: translateY(-4px); box-shadow: var(--shadow-lg);
  border-color: color-mix(in srgb, var(--c) 45%, var(--hair));
}
.tile-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; position: relative; }
.tile-tags { display: flex; gap: 6px; align-items: center; }
.tile-chip {
  width: 52px; height: 52px; border-radius: 15px; display: grid; place-items: center;
  font-size: 25px; font-weight: 700; color: #fff;
  background: linear-gradient(140deg, color-mix(in srgb, var(--c) 88%, #fff), var(--c));
  box-shadow: 0 8px 18px color-mix(in srgb, var(--c) 36%, transparent);
}
.tile-title { font-size: 1.2rem; margin: 0 0 5px; position: relative; }
.tile-sum { color: var(--muted); font-size: 0.94rem; line-height: 1.5; margin: 0; position: relative; }

.badge { display: inline-block; font-size: 0.72rem; font-weight: 650; padding: 4px 11px; border-radius: 980px; background: var(--panel-2); color: var(--muted); }
.badge.done { color: var(--good-ink); background: color-mix(in srgb, var(--good) 16%, transparent); }
.badge.accent { color: var(--c, var(--primary)); background: color-mix(in srgb, var(--c, var(--primary)) 15%, transparent); }

.progress-bar { height: 9px; background: var(--panel-2); border-radius: 980px; overflow: hidden; margin-top: 12px; }
.progress-bar > i { display: block; height: 100%; border-radius: 980px; background: linear-gradient(90deg, var(--primary), var(--primary-2)); transition: width 0.4s ease; }

/* ---------------- lesson reading ---------------- */
.lesson { max-width: 720px; }
.lesson-head { display: flex; align-items: center; gap: 18px; }
.lesson-chip {
  width: 62px; height: 62px; border-radius: 18px; display: grid; place-items: center; flex: none;
  font-size: 30px; font-weight: 700; color: #fff;
  background: linear-gradient(140deg, color-mix(in srgb, var(--c) 88%, #fff), var(--c));
  box-shadow: 0 10px 22px color-mix(in srgb, var(--c) 38%, transparent);
}
.lesson p { font-size: 1.075rem; line-height: 1.78; color: var(--text-soft); margin: 0 0 18px; }
.lesson .lead { font-size: 1.28rem; line-height: 1.55; color: var(--text); font-weight: 450; margin-bottom: 24px; }
.lesson h2 { font-size: 1.45rem; margin: 42px 0 16px; padding-left: 15px; border-left: 4px solid var(--c, var(--primary)); }
.lesson ul, .lesson ol { margin: 0 0 18px; padding-left: 24px; }
.lesson li { margin: 8px 0; font-size: 1.06rem; line-height: 1.7; color: var(--text-soft); }
.lesson b { color: var(--text); font-weight: 650; }

.callout {
  background: color-mix(in srgb, var(--primary) 7%, var(--panel));
  border: 1px solid var(--hair); border-left: 4px solid var(--primary);
  border-radius: var(--radius-sm); padding: 16px 20px; margin: 24px 0;
  font-size: 1.02rem; line-height: 1.65; color: var(--text-soft);
}
.callout.tip { border-left-color: var(--good); background: color-mix(in srgb, var(--good) 9%, var(--panel)); }
.callout.warn { border-left-color: var(--warn); background: color-mix(in srgb, var(--warn) 11%, var(--panel)); }
.callout b { color: var(--text); }

.var { font-family: var(--mono); color: var(--indigo); background: color-mix(in srgb, var(--indigo) 13%, transparent); padding: 1px 6px; border-radius: 6px; font-size: 0.94em; font-weight: 500; }
code { font-family: var(--mono); background: var(--code-bg); padding: 2px 7px; border-radius: 6px; font-size: 0.9em; }

.example {
  background: var(--panel-2); border: 1px solid var(--hair); border-radius: var(--radius-sm);
  padding: 20px 22px; margin: 24px 0; overflow-x: auto;
}
.example .label { color: var(--muted); font-size: 0.74rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; margin-bottom: 12px; }
.example p { font-size: 1rem; margin: 8px 0 0; color: var(--text-soft); }

.lesson-foot { margin-top: 46px; padding-top: 26px; border-top: 1px solid var(--hair); display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; }

/* ---------------- matrices (pure CSS, no MathJax) ---------------- */
.mrow { display: inline-flex; align-items: center; gap: 14px; vertical-align: middle; margin: 6px 0; flex-wrap: wrap; }
.mrow .op { font-size: 1.5rem; color: var(--muted); font-weight: 300; }
.matrix {
  display: inline-grid; gap: 7px 18px; padding: 8px 16px; position: relative;
  font-family: var(--mono); font-size: 1.02rem; color: var(--text);
}
.matrix::before, .matrix::after {
  content: ""; position: absolute; top: 0; bottom: 0; width: 9px;
  border: 2px solid color-mix(in srgb, var(--text) 42%, transparent);
}
.matrix::before { left: 0; border-right: none; border-radius: 5px 0 0 5px; }
.matrix::after { right: 0; border-left: none; border-radius: 0 5px 5px 0; }
.matrix .cell { text-align: center; min-width: 1.6em; padding: 2px; }
.scalar { font-family: var(--mono); font-size: 1.18rem; color: var(--text); }

.matrix input {
  width: 3em; text-align: center; font: inherit; font-family: var(--mono);
  background: var(--panel); color: var(--text); border: 1.5px solid var(--border);
  border-radius: 9px; padding: 6px 2px;
}
.matrix input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 22%, transparent); }
.matrix input.ok { border-color: var(--good); background: color-mix(in srgb, var(--good) 12%, var(--panel)); }
.matrix input.err { border-color: var(--bad); background: color-mix(in srgb, var(--bad) 12%, var(--panel)); }

/* ---------------- games ---------------- */
.game-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 22px; }
.stats { display: flex; gap: 12px; flex-wrap: wrap; }
.stat { background: var(--panel-2); border: 1px solid var(--hair); border-radius: var(--radius-sm); padding: 12px 18px; text-align: center; min-width: 90px; }
.stat .n { font-size: 1.6rem; font-weight: 800; font-family: var(--mono); letter-spacing: -0.02em; }
.stat .l { color: var(--muted); font-size: 0.7rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; margin-top: 2px; }
.stat.time .n { color: var(--primary); }
.stat.score .n { color: var(--good); }

.game-stage { background: var(--panel); border: 1px solid var(--hair); border-radius: var(--radius); padding: 32px; box-shadow: var(--shadow-sm); }
.prompt { text-align: center; margin-bottom: 24px; }
.prompt .q { color: var(--muted); margin-bottom: 16px; font-size: 1.05rem; }
.answers { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 12px; max-width: 540px; margin: 0 auto; }
.answers .btn {
  padding: 18px; font-size: 1.3rem; font-family: var(--mono); border-radius: var(--radius-sm);
  background: var(--panel-2); border: 1.5px solid var(--hair);
}
.answers .btn:hover { border-color: var(--c, var(--primary)); background: color-mix(in srgb, var(--c, var(--primary)) 12%, var(--panel-2)); }
.answer-row { display: flex; gap: 10px; justify-content: center; align-items: center; flex-wrap: wrap; }
.answer-row input {
  width: 5em; text-align: center; font: inherit; font-family: var(--mono); font-size: 1.3rem;
  background: var(--panel-2); color: var(--text); border: 1.5px solid var(--border); border-radius: 12px; padding: 12px;
}
.answer-row input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 22%, transparent); }

.feedback { text-align: center; min-height: 28px; margin-top: 18px; font-weight: 650; }
.feedback.good { color: var(--good-ink); }
.feedback.bad { color: var(--bad); }

.timerbar { height: 7px; background: var(--panel-2); border-radius: 980px; overflow: hidden; margin-bottom: 24px; }
.timerbar > i { display: block; height: 100%; border-radius: 980px; background: linear-gradient(90deg, var(--c, var(--primary)), color-mix(in srgb, var(--c, var(--primary)) 50%, var(--primary-2))); transition: width 0.25s linear; }

.lb { width: 100%; border-collapse: collapse; margin-top: 10px; }
.lb th, .lb td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--hair); }
.lb th { color: var(--muted); font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; }
.lb td.rank { font-family: var(--mono); color: var(--muted); width: 2.5em; }
.lb td.sc { font-family: var(--mono); color: var(--good-ink); text-align: right; font-weight: 600; }
.lb tr.me td { color: var(--primary); font-weight: 700; }

.pill { display: inline-flex; gap: 6px; align-items: center; background: var(--panel-2); border: 1px solid var(--hair); border-radius: 980px; padding: 5px 13px; font-size: 0.8rem; color: var(--muted); }

.center { text-align: center; }
.mt { margin-top: 24px; }
.row-actions { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }

/* ---------------- scrollbars ---------------- */
.nav::-webkit-scrollbar { width: 8px; }
.nav::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }

/* ---------------- responsive ---------------- */
@media (max-width: 820px) {
  body { font-size: 16px; }
  .app { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; }
  .nav { max-height: none; }
  .view { padding: 28px 18px 64px; }
  .page-head h1 { font-size: 1.9rem; }
}
