:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #eef1f8;
  --text: #141a2e;
  --muted: #5b6580;
  --border: #dde2ee;
  --accent: #2f5bea;
  --accent-soft: #e5ecff;
  --on-accent: #ffffff;
  --good: #1a8f5a;
  --good-soft: #e1f5ec;
  --warn: #b26a00;
  --warn-soft: #fff1dc;
  --bad: #c2364b;
  --shadow: 0 1px 2px rgba(20, 26, 46, .06), 0 4px 16px rgba(20, 26, 46, .05);
  --radius: 14px;
  --font: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1220;
    --surface: #161b2c;
    --surface-2: #1e2438;
    --text: #e8ebf5;
    --muted: #9aa3bd;
    --border: #2a3149;
    --accent: #7c9cff;
    --accent-soft: #202b4f;
    --on-accent: #0e1220;
    --good: #4cc68b;
    --good-soft: #15302a;
    --warn: #f0b056;
    --warn-soft: #362a17;
    --bad: #ff7b8e;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5rem; }
h1 { font-size: 1.75rem; letter-spacing: -.01em; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }
p { margin: 0 0 .75rem; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Topbar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 1.5rem;
  padding: .75rem max(16px, calc((100vw - 1120px) / 2));
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .6rem; text-decoration: none; color: var(--text); }
.logo {
  display: grid; place-items: center; width: 36px; height: 36px; border-radius: 10px;
  background: var(--accent); color: var(--on-accent); font-weight: 700; font-size: .95rem;
}
.brand-text { display: flex; flex-direction: column; font-weight: 650; line-height: 1.15; }
.brand-text small { font-weight: 400; color: var(--muted); font-size: .78rem; }
.nav { display: flex; gap: .25rem; overflow-x: auto; scrollbar-width: none; }
.nav a {
  padding: .45rem .75rem; border-radius: 999px; text-decoration: none; color: var(--muted);
  white-space: nowrap; font-size: .93rem;
}
.nav a:hover { color: var(--text); background: var(--surface-2); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

main { max-width: 1120px; margin: 0 auto; padding: 2rem 16px 3rem; outline: none; }

.footer {
  max-width: 1120px; margin: 0 auto; padding: 1.5rem 16px 2.5rem;
  display: flex; flex-wrap: wrap; gap: .5rem 1rem; align-items: center;
  color: var(--muted); font-size: .85rem; border-top: 1px solid var(--border);
}
.linkbtn { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; text-decoration: underline; font-size: .85rem; }
.linkbtn.danger { color: var(--bad); }

/* Generic */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1.25rem 1.4rem; box-shadow: var(--shadow);
}
.card + .card { margin-top: 1rem; }
.hero > .card, .stats > .card, .weeks > .card, .side > .card, .mat > .card { margin-top: 0; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.eyebrow { text-transform: uppercase; letter-spacing: .08em; font-size: .75rem; font-weight: 650; color: var(--accent); margin-bottom: .35rem; }
.grid { display: grid; gap: 1rem; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: .55rem 1rem; border-radius: 10px; cursor: pointer; font-weight: 550; text-decoration: none;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.good { background: var(--good-soft); border-color: transparent; color: var(--good); }
.btn.warn { background: var(--warn-soft); border-color: transparent; color: var(--warn); }
.btn.small { padding: .35rem .7rem; font-size: .875rem; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.row { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.spacer { flex: 1; }
.pill { display: inline-block; padding: .15rem .6rem; border-radius: 999px; font-size: .78rem; font-weight: 600; background: var(--surface-2); color: var(--muted); }
.pill.good { background: var(--good-soft); color: var(--good); }
.pill.accent { background: var(--accent-soft); color: var(--accent); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }

/* Home */
.hero { display: grid; grid-template-columns: 1.4fr 1fr; gap: 1rem; margin-bottom: 1.5rem; }
.hero h1 { font-size: 2rem; }
.today { display: flex; flex-direction: column; justify-content: space-between; gap: 1rem; background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 60%, #7b3fe4)); color: #fff; border: 0; }
.today .eyebrow, .today .muted { color: rgba(255,255,255,.85); }
.today h2 { font-size: 1.4rem; }
.today .btn { background: #fff; color: #1d3bb8; border: 0; align-self: flex-start; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin-bottom: 2rem; }
.stat .num { font-size: 1.8rem; font-weight: 700; line-height: 1.1; }
.stat .bar { height: 6px; border-radius: 3px; background: var(--surface-2); margin-top: .6rem; overflow: hidden; }
.stat .bar span { display: block; height: 100%; background: var(--accent); border-radius: 3px; transition: width .4s; }
.weeks { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; }
.week h3 { margin-bottom: .2rem; }
.week .goal { font-size: .875rem; color: var(--muted); margin-bottom: .9rem; }
.unit-link {
  display: flex; align-items: center; gap: .75rem; padding: .6rem .7rem; border-radius: 10px;
  text-decoration: none; color: var(--text); border: 1px solid transparent;
}
.unit-link:hover { background: var(--surface-2); }
.unit-link.is-today { border-color: var(--accent); background: var(--accent-soft); }
.unit-link .num {
  flex: none; display: grid; place-items: center; width: 34px; height: 34px; border-radius: 50%;
  background: var(--surface-2); font-weight: 650; font-size: .9rem; color: var(--muted);
}
.unit-link.done .num { background: var(--good); color: #fff; }
.unit-link.partial .num { background: var(--warn-soft); color: var(--warn); }
.unit-link .meta { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.unit-link .meta small { color: var(--muted); font-size: .78rem; }

/* Unit */
.unit-head { display: flex; flex-wrap: wrap; gap: 1rem; align-items: flex-end; margin-bottom: 1.25rem; }
.unit-head h1 { margin: 0; }
.unit-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 1rem; align-items: start; }
.side { position: sticky; top: 84px; display: grid; gap: 1rem; }
.goal-box { border-left: 4px solid var(--accent); background: var(--accent-soft); padding: .9rem 1.1rem; border-radius: 0 10px 10px 0; margin-bottom: 1rem; }
.phase { display: flex; gap: .9rem; padding: .9rem 0; border-top: 1px solid var(--border); }
.phase:first-of-type { border-top: 0; padding-top: .25rem; }
.phase .check {
  flex: none; width: 26px; height: 26px; border-radius: 8px; border: 2px solid var(--border);
  background: var(--surface); cursor: pointer; display: grid; place-items: center; color: transparent; margin-top: 2px;
}
.phase .check.on { background: var(--good); border-color: var(--good); color: #fff; }
.phase .body { flex: 1; min-width: 0; }
.phase .label { font-weight: 650; display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.phase ul { margin: .35rem 0 0; padding-left: 1.1rem; }
.phase li { margin: .2rem 0; }
.current { margin-top: .6rem; padding: .7rem .9rem; background: var(--warn-soft); border-radius: 10px; font-size: .92rem; }

/* Timer */
.timer .time { font-size: 2.6rem; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -.02em; line-height: 1; }
.timer .phase-name { color: var(--muted); font-size: .9rem; margin: .35rem 0 .8rem; }
.timer .segments { display: flex; gap: 4px; margin-bottom: .9rem; }
.timer .seg { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.timer .seg span { display: block; height: 100%; background: var(--accent); width: 0; }

/* Questions */
.q { border: 1px solid var(--border); border-radius: 12px; padding: .9rem 1rem; margin-top: .75rem; background: var(--surface); }
.q .qtext { font-weight: 600; display: flex; gap: .6rem; }
.q .qtext .status { flex: none; }
.q .answer { margin-top: .7rem; padding-top: .7rem; border-top: 1px dashed var(--border); }
.q .actions { margin-top: .7rem; }

textarea {
  width: 100%; min-height: 110px; resize: vertical; padding: .7rem .8rem; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-size: .93rem;
}
input[type=search], input[type=number], select {
  padding: .55rem .8rem; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit;
}
input[type=search] { width: 100%; }

.terms { display: flex; flex-wrap: wrap; gap: .4rem; }
.term-chip { padding: .3rem .65rem; border-radius: 8px; background: var(--surface-2); font-size: .85rem; cursor: help; }

.calc { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .6rem; margin-top: .6rem; }
.calc label { display: flex; flex-direction: column; font-size: .8rem; color: var(--muted); gap: .2rem; }
.calc input, .calc select { width: 100%; }
.calc-out { margin-top: .7rem; padding: .7rem .9rem; background: var(--surface-2); border-radius: 10px; font-variant-numeric: tabular-nums; }

.diagram { margin: .75rem 0 0; }
.diagram svg { width: 100%; height: auto; }

.pager { display: flex; justify-content: space-between; margin-top: 1.5rem; gap: .5rem; }

/* Glossary */
.toolbar { display: grid; grid-template-columns: 1fr auto; gap: .75rem; margin-bottom: 1rem; align-items: center; }
.chips { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: 1rem; }
.chip { border: 1px solid var(--border); background: var(--surface); padding: .35rem .8rem; border-radius: 999px; cursor: pointer; font-size: .87rem; }
.chip { text-decoration: none; color: var(--text); }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.gloss { width: 100%; border-collapse: collapse; }
.gloss th, .gloss td { text-align: left; padding: .7rem .6rem; border-bottom: 1px solid var(--border); vertical-align: top; }
.gloss th { font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 650; }
.gloss td.term { font-weight: 600; width: 28%; }
.gloss td.day a { text-decoration: none; }
.gloss tr.known td.term::after { content: " ✓"; color: var(--good); }
.toggle { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: .2rem .55rem; cursor: pointer; font-size: .8rem; white-space: nowrap; }
.toggle.on { background: var(--good-soft); color: var(--good); border-color: transparent; }

/* Flashcards */
.flash-wrap { max-width: 680px; margin: 0 auto; }
.flash {
  perspective: 1200px; cursor: pointer; margin: 1rem 0; min-height: 280px;
}
.flash-inner { position: relative; width: 100%; min-height: 280px; transition: transform .5s; transform-style: preserve-3d; }
.flash.flipped .flash-inner { transform: rotateY(180deg); }
.face {
  position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden;
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  padding: 2rem; border-radius: 18px; background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow);
}
.face.back { transform: rotateY(180deg); background: var(--accent-soft); }
.face .big { font-size: 1.6rem; font-weight: 700; }
.face .def { font-size: 1.15rem; }
.face .hint { position: absolute; bottom: 1rem; font-size: .8rem; color: var(--muted); }
.progress-line { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.progress-line span { display: block; height: 100%; background: var(--accent); transition: width .3s; }
.done-box { text-align: center; padding: 2.5rem 1rem; }
.done-box .big { font-size: 3rem; }

/* Material */
.mat { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 1rem; }
.mat a.card { text-decoration: none; color: var(--text); display: block; transition: transform .15s, border-color .15s; }
.mat a.card:hover { transform: translateY(-2px); border-color: var(--accent); }
.mat h3 { color: var(--accent); }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: .6rem 1.1rem; border-radius: 10px; font-size: .9rem;
  box-shadow: var(--shadow); z-index: 50; animation: fade .2s;
}
@keyframes fade { from { opacity: 0; transform: translate(-50%, 6px); } }

@media (max-width: 900px) {
  .hero, .unit-layout { grid-template-columns: 1fr; }
  .side { position: static; }
  .weeks { grid-template-columns: 1fr; }
  .stats { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 640px) {
  .topbar { flex-direction: column; align-items: stretch; gap: .5rem; }
  .nav { margin: 0 -4px; }
  h1, .hero h1 { font-size: 1.5rem; }
  .calc { grid-template-columns: 1fr; }
  .gloss td.term { width: auto; }
  .gloss .col-cat { display: none; }
  .toolbar { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
