:root {
  color-scheme: light;
  --ink: #1b2330;
  --muted: #5f6b7a;
  --line: #e3e7ed;
  --bg: #ffffff;
  --soft: #f6f8fb;
  --brand: #1f3a5f;
  --accent: #2563eb;
  --ok: #15803d;
  --ok-bg: #e7f6ec;
  --bad: #b91c1c;
  --bad-bg: #fdecec;
  --radius: 12px;
  --mono: 'JetBrains Mono', ui-monospace, Menlo, monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.6 Inter, system-ui, -apple-system, 'Segoe UI', sans-serif;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.25; letter-spacing: -0.01em; }
h1 { font-size: 1.75rem; margin: 0 0 0.5rem; }
h2 { font-size: 1.3rem; margin: 2rem 0 0.75rem; }
h3 { font-size: 1.08rem; margin: 1.5rem 0 0.5rem; }
.muted { color: var(--muted); }
.label { margin: 1rem 0 0.35rem; font-size: 0.78rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }

/* ---------- frame ---------- */
.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: 0.7rem 1rem; background: rgba(255, 255, 255, 0.94); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; color: var(--brand); text-decoration: none; font-size: 1.05rem; }
.top-nav { display: flex; gap: 0.25rem; }
.top-nav a { padding: 0.35rem 0.7rem; border-radius: 8px; color: var(--ink); text-decoration: none; font-weight: 500; }
.top-nav a.on, .top-nav a:hover { background: var(--soft); }

.layout { display: grid; grid-template-columns: 210px minmax(0, 1fr); max-width: 1180px; margin: 0 auto; }
.subject-nav {
  position: sticky; top: 57px; align-self: start;
  display: flex; flex-direction: column; gap: 2px; padding: 1.25rem 0.75rem;
}
.subject-nav a {
  padding: 0.45rem 0.7rem; border-radius: 8px; text-decoration: none; color: var(--ink);
  border-left: 4px solid var(--c); font-weight: 500; font-size: 0.95rem;
}
.subject-nav a:hover { background: var(--soft); }
.subject-nav a.on { background: var(--t); }
main { padding: 1.5rem 1.5rem 4rem; min-width: 0; max-width: 820px; }

@media (max-width: 860px) {
  .layout { display: block; }
  .subject-nav {
    position: static; flex-direction: row; overflow-x: auto; padding: 0.6rem 1rem; gap: 0.4rem;
    border-bottom: 1px solid var(--line); scrollbar-width: none;
  }
  .subject-nav a { white-space: nowrap; border-left: 0; border-bottom: 3px solid var(--c); border-radius: 6px 6px 0 0; font-size: 0.9rem; }
  main { padding: 1.25rem 1rem 4rem; }
  h1 { font-size: 1.5rem; }
}

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 0.4rem;
  padding: 0.55rem 0.95rem; border-radius: 10px; border: 1px solid var(--line);
  background: #fff; color: var(--ink); font: inherit; font-weight: 500; text-decoration: none; cursor: pointer;
}
.btn:hover { background: var(--soft); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { filter: brightness(1.15); }
.btn.small { padding: 0.25rem 0.6rem; font-size: 0.85rem; border-radius: 8px; }
.btn:disabled { opacity: 0.6; cursor: wait; }
.pill { background: #fff; color: var(--brand); border-radius: 99px; padding: 0 0.45rem; font-size: 0.8rem; font-weight: 700; }

/* ---------- home & timetable ---------- */
.today { background: var(--soft); border-radius: var(--radius); padding: 1.1rem 1.2rem; }
.today-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.today-head h1 { margin: 0; }
.more { margin: 0.9rem 0 0; }
.group-switch { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: #fff; }
.group-switch button { border: 0; background: none; padding: 0.35rem 0.9rem; font: inherit; font-weight: 600; cursor: pointer; color: var(--muted); }
.group-switch button.on { background: var(--brand); color: #fff; }

.class-line {
  display: grid; grid-template-columns: 7.2rem 1fr; gap: 0 0.8rem; align-items: baseline;
  padding: 0.6rem 0.8rem; border-radius: 10px; background: var(--t); border-left: 5px solid var(--c);
  color: var(--ink); text-decoration: none; margin-bottom: 0.4rem;
}
.class-line .time { font-variant-numeric: tabular-nums; font-weight: 600; }
.class-line .where { grid-column: 2; color: var(--muted); font-size: 0.9rem; }
@media (max-width: 480px) {
  .class-line { grid-template-columns: 1fr; }
  .class-line .where { grid-column: 1; }
}

.subject-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 0.8rem; }
.subject-card {
  display: flex; flex-direction: column; gap: 0.2rem; padding: 1rem; border-radius: var(--radius);
  border: 1px solid var(--line); border-top: 5px solid var(--c); color: var(--ink); text-decoration: none;
  transition: transform 0.12s, box-shadow 0.12s;
}
.subject-card:hover { transform: translateY(-2px); box-shadow: 0 6px 18px rgba(20, 30, 50, 0.08); }
.subject-card .title { font-weight: 600; font-size: 1.05rem; }
.subject-card .teacher { color: var(--muted); font-size: 0.88rem; }
.subject-card .prog { margin-top: auto; padding-top: 0.6rem; }
.subject-card small { color: var(--muted); }
.bar { height: 6px; border-radius: 99px; background: var(--line); overflow: hidden; margin: 0.3rem 0; }
.bar span { display: block; height: 100%; background: var(--c); border-radius: 99px; }

.tt-wrap { overflow-x: auto; margin: 1rem 0; border: 1px solid var(--line); border-radius: var(--radius); }
.tt { border-collapse: collapse; width: 100%; min-width: 720px; font-size: 0.85rem; }
.tt th, .tt td { border: 1px solid var(--line); padding: 4px; vertical-align: top; }
.tt thead th { background: var(--soft); font-weight: 600; font-variant-numeric: tabular-nums; }
.tt tbody th { text-align: left; padding: 0.5rem; background: var(--soft); width: 90px; }
.tt tr.is-today th { background: var(--brand); color: #fff; }
.tt-class { display: flex; flex-direction: column; min-height: 64px; padding: 0.4rem 0.5rem; border-radius: 6px; background: var(--t); border-left: 4px solid var(--c); color: var(--ink); text-decoration: none; }
.tt-class small { color: var(--muted); }
.days { display: grid; gap: 1rem; }
.day h3 { margin: 0.5rem 0; }
.day.is-today h3::after { content: ' · today'; color: var(--accent); font-weight: 500; }

/* ---------- subject & chapter ---------- */
.subject-head { background: var(--t); border-radius: var(--radius); padding: 1.3rem 1.3rem 1.1rem; border-top: 6px solid var(--c); }
.subject-head p { margin: 0 0 0.6rem; color: var(--muted); }
.subject-head .actions { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-top: 0.9rem; }
.chapters { list-style: none; padding: 0; margin: 0; counter-reset: ch; }
.chapters li { counter-increment: ch; }
.chapters a {
  display: grid; grid-template-columns: 2.2rem 1fr; padding: 0.75rem 0.5rem; border-bottom: 1px solid var(--line);
  color: var(--ink); text-decoration: none;
}
.chapters a::before {
  content: counter(ch); grid-row: span 3; width: 1.7rem; height: 1.7rem; border-radius: 50%;
  display: grid; place-items: center; background: var(--soft); font-weight: 600; font-size: 0.85rem;
}
.chapters li.done a::before { content: '✓'; background: var(--ok-bg); color: var(--ok); }
.chapters a:hover { background: var(--soft); }
.chapters .t { font-weight: 600; }
.chapters .s { color: var(--muted); font-size: 0.93rem; }
.chapters .meta { color: var(--muted); font-size: 0.8rem; }
.crumbs { font-size: 0.9rem; color: var(--muted); margin-bottom: 1rem; display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.crumbs a { font-weight: 600; text-decoration: none; color: var(--ink); border-bottom: 3px solid var(--c); }

/* ---------- prose (Markdown content) ---------- */
.prose { overflow-wrap: break-word; }
.prose p, .prose ul, .prose ol { margin: 0.7rem 0; }
.prose li { margin: 0.25rem 0; }
.prose img { max-width: 100%; }
.prose code { font-family: var(--mono); font-size: 0.88em; background: var(--soft); padding: 0.1em 0.35em; border-radius: 5px; }
.prose pre.code { background: #f4f6fa; border: 1px solid var(--line); border-radius: 10px; padding: 0.8rem 1rem; overflow-x: auto; font-size: 0.86rem; line-height: 1.5; }
.prose pre.code code { background: none; padding: 0; }
.prose pre.code[data-lang]::before { content: attr(data-lang); display: block; font: 600 0.72rem var(--mono); color: var(--muted); text-transform: uppercase; margin-bottom: 0.3rem; }
.prose blockquote { margin: 1rem 0; padding: 0.6rem 1rem; border-left: 4px solid var(--accent); background: #eef4ff; border-radius: 0 10px 10px 0; }
.prose blockquote p { margin: 0.3rem 0; }
.prose table { border-collapse: collapse; margin: 1rem 0; display: block; overflow-x: auto; max-width: 100%; }
.prose th, .prose td { border: 1px solid var(--line); padding: 0.4rem 0.7rem; text-align: left; vertical-align: top; }
.prose th { background: var(--soft); }
.prose .katex-display { position: relative; overflow-x: auto; overflow-y: hidden; padding: 0.2rem 0; }
.prose hr { border: 0; border-top: 1px solid var(--line); margin: 1.5rem 0; }
.prose h2 { padding-top: 0.5rem; }

/* ---------- live parts ---------- */
.pyrun { border: 1px solid #c9d6ea; border-radius: 10px; margin: 1rem 0; overflow: hidden; background: #fbfcfe; }
.pyrun-head { display: flex; justify-content: space-between; align-items: center; gap: 0.5rem; padding: 0.4rem 0.6rem; background: #eef3fb; font-size: 0.8rem; color: var(--muted); font-weight: 600; }
.pyrun textarea {
  display: block; width: 100%; border: 0; resize: vertical; padding: 0.8rem 1rem; background: transparent;
  font: 0.86rem/1.5 var(--mono); color: var(--ink); tab-size: 4; white-space: pre; overflow-x: auto; outline: none;
}
.pyrun-out { border-top: 1px dashed #c9d6ea; padding: 0.6rem 1rem; background: #fff; }
.pyrun-out pre { margin: 0; font: 0.84rem/1.45 var(--mono); white-space: pre-wrap; max-height: 360px; overflow: auto; }
.pyrun-out pre.err { color: var(--bad); }
.pyrun-out img { max-width: 100%; display: block; margin-top: 0.5rem; }
.demo { border: 1px solid var(--line); border-radius: 10px; padding: 1rem; margin: 1rem 0; background: var(--soft); }
.demo canvas, .demo svg { max-width: 100%; display: block; }
.demo input[type='range'] { width: 100%; }

.exercise { border: 1px solid var(--line); border-radius: var(--radius); padding: 0.4rem 1rem 0.8rem; margin-bottom: 0.8rem; }
.exercise details { margin-top: 0.4rem; }
.exercise summary { cursor: pointer; font-weight: 600; color: var(--accent); }
.exercise details .prose { border-top: 1px dashed var(--line); margin-top: 0.5rem; }

.q { border: 1px solid var(--line); border-radius: var(--radius); padding: 0.4rem 1rem 1rem; margin: 0 0 0.9rem; }
.q legend { padding: 0; width: 100%; float: left; margin-bottom: 0.3rem; }
.q legend + * { clear: both; }
.choice {
  display: block; width: 100%; text-align: left; margin: 0.35rem 0; padding: 0.6rem 0.8rem; border-radius: 10px;
  border: 1px solid var(--line); background: #fff; font: inherit; color: var(--ink); cursor: pointer;
}
.choice:hover:not(:disabled) { background: var(--soft); border-color: #c5cdd8; }
.choice:disabled { cursor: default; }
.choice.right { background: var(--ok-bg); border-color: var(--ok); }
.choice.wrong { background: var(--bad-bg); border-color: var(--bad); }
.explain { margin-top: 0.5rem; padding: 0.2rem 0.9rem; background: var(--soft); border-radius: 10px; }
.score { font-size: 1.05rem; display: flex; gap: 0.8rem; align-items: center; flex-wrap: wrap; }

.chapter-end { margin-top: 2rem; border-top: 1px solid var(--line); padding-top: 1rem; }
.done-toggle { display: inline-flex; gap: 0.5rem; align-items: center; font-weight: 600; cursor: pointer; }
.done-toggle input { width: 1.15rem; height: 1.15rem; accent-color: var(--ok); }
.pager { display: flex; justify-content: space-between; gap: 0.6rem; margin-top: 1rem; flex-wrap: wrap; }
.pager .btn { max-width: 100%; }

.flashcard {
  min-height: 220px; border-radius: 16px; padding: 1.5rem; margin: 1rem 0; background: var(--t);
  border: 1px solid var(--line); border-top: 6px solid var(--c); font-size: 1.1rem;
}
.flashcard .front { font-weight: 600; }
.card-actions { display: flex; gap: 0.6rem; justify-content: center; }
.card-actions .btn { min-width: 9rem; justify-content: center; }
.card-done { text-align: center; padding: 2rem 0; }
.card-done .btn { margin: 0.3rem; }

@media print {
  .top, .subject-nav, .no-print { display: none !important; }
  main { max-width: none; padding: 0; }
  .sheet { font-size: 11pt; }
}
