:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #f0ece4;
  --text: #1f2933;
  --muted: #5f6b76;
  --line: #e2ddd3;
  --accent: #0f766e;
  --accent-2: #0d9488;
  --accent-soft: #d9f0ec;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --bad: #b91c1c;
  --bad-soft: #fee2e2;
  --warn-soft: #fef3c7;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(30,30,20,.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14181c; --surface: #1d2329; --surface-2: #262d34; --text: #e8e6e1; --muted: #a0a8b0; --line: #333b44;
    --accent: #2dd4bf; --accent-2: #14b8a6; --accent-soft: #123f3a; --ok: #4ade80; --ok-soft: #14351f;
    --bad: #f87171; --bad-soft: #3f1a1a; --warn-soft: #3d3117; --shadow: 0 1px 2px rgba(0,0,0,.4);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: var(--accent); text-decoration: none; }
button { font: inherit; }
img { max-width: 100%; height: auto; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.6rem; } h2 { font-size: 1.3rem; } h3 { font-size: 1.1rem; }
p { margin: 0 0 .8em; }
i, em { color: inherit; }

.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top)); background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; color: var(--text); font-size: 1.05rem; white-space: nowrap; }
.nav { display: flex; gap: 6px; align-items: center; font-size: .9rem; }
.nav .user { color: var(--muted); }
.container { max-width: 880px; margin: 0 auto; padding: 16px; padding-bottom: 48px; }
.loading { color: var(--muted); text-align: center; padding: 40px; }

/* buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  padding: 9px 14px; border-radius: 10px; font-weight: 600; min-height: 42px; text-decoration: none;
  transition: background .12s, transform .05s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { padding: 5px 10px; min-height: 32px; font-size: .88rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }
.btn-row.between { justify-content: space-between; }

/* cards */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card + .card { margin-top: 12px; }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); }
.course-card { display: flex; flex-direction: column; gap: 8px; cursor: pointer; color: var(--text); }
.course-card .icon { font-size: 2rem; }
.course-card h3 { margin: 0; }
.course-card .sub { color: var(--muted); font-size: .92rem; }
.meta { color: var(--muted); font-size: .85rem; display: flex; flex-wrap: wrap; gap: 4px 12px; }
.progress { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); border-radius: 99px; transition: width .3s; }
.pill { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: .8rem; font-weight: 600; background: var(--surface-2); color: var(--muted); }
.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.accent { background: var(--accent-soft); color: var(--accent); }

/* auth */
.auth { max-width: 380px; margin: 40px auto; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field label { font-size: .88rem; color: var(--muted); font-weight: 600; }
input[type=text], input[type=password] {
  font: inherit; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--text); width: 100%;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
.error { color: var(--bad); background: var(--bad-soft); padding: 8px 12px; border-radius: 8px; margin-bottom: 12px; font-size: .92rem; }
.hint { color: var(--muted); font-size: .88rem; }

/* page header */
.page-head { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 16px; }
.page-head .icon { font-size: 2.4rem; line-height: 1; }
.page-head h1 { margin-bottom: 2px; }
.page-head .sub { color: var(--muted); }
.back { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); font-size: .9rem; margin-bottom: 10px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin: 14px 0; }
.tile { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 16px 10px; text-align: center; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--line); color: var(--text); cursor: pointer; box-shadow: var(--shadow); }
.tile:hover { border-color: var(--accent); }
.tile .t-icon { font-size: 1.8rem; }
.tile .t-title { font-weight: 700; }
.tile .t-sub { color: var(--muted); font-size: .82rem; }
.section-list { list-style: none; padding: 0; margin: 0; }
.section-list li { border-bottom: 1px solid var(--line); }
.section-list li:last-child { border-bottom: 0; }
.section-list a { display: flex; align-items: center; gap: 10px; padding: 12px 4px; color: var(--text); }
.section-list .num { width: 26px; height: 26px; border-radius: 50%; background: var(--surface-2); color: var(--muted); display: inline-flex; align-items: center; justify-content: center; font-size: .82rem; font-weight: 700; flex-shrink: 0; }
.section-list .done .num { background: var(--ok-soft); color: var(--ok); }

/* learn view */
.learn h1 { font-size: 1.4rem; }
.learn-nav { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin: 10px 0 18px; color: var(--muted); font-size: .9rem; }
.block { margin: 0 0 18px; }
.block.text ul, .block.text ol { padding-left: 1.3em; margin: .3em 0 .8em; }
.block.text li { margin-bottom: .25em; }
.callout { border-left: 4px solid var(--accent); background: var(--accent-soft); padding: 10px 14px; border-radius: 8px; }
.callout .c-title { font-weight: 700; margin-bottom: 4px; }
.callout p:last-child { margin-bottom: 0; }
.image { text-align: center; }
.image img, .figure img { border-radius: 10px; background: #fff; cursor: zoom-in; border: 1px solid var(--line); }
.caption { color: var(--muted); font-size: .88rem; margin-top: 6px; text-align: center; }
.figure { display: grid; gap: 12px; grid-template-columns: 1fr; align-items: start; }
@media (min-width: 640px) { .figure.wide { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } }
.figure .img-wrap { text-align: center; }
.figure .legend { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 4px; }
.figure .legend li { display: flex; gap: 8px; align-items: baseline; padding: 5px 8px; border-radius: 8px; cursor: pointer; border: 1px solid transparent; }
.figure .legend li:hover { background: var(--surface-2); }
.figure .legend li.active { background: var(--accent-soft); border-color: var(--accent); }
.figure .legend .n { min-width: 34px; text-align: center; font-weight: 700; color: var(--accent); background: var(--accent-soft); border-radius: 6px; padding: 1px 6px; font-size: .85rem; flex-shrink: 0; }
.figure .legend .t.hidden { color: transparent; background: var(--surface-2); border-radius: 6px; user-select: none; }
.figure .legend .t.hidden::after { content: ''; }
.figure-tools { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; }
table { border-collapse: collapse; width: 100%; font-size: .93rem; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { background: var(--surface-2); font-weight: 700; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
.terms { display: flex; flex-direction: column; gap: 6px; }
.term { border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
.term > button { all: unset; display: flex; justify-content: space-between; align-items: center; width: 100%; padding: 10px 12px; cursor: pointer; font-weight: 600; box-sizing: border-box; }
.term > button::after { content: '+'; color: var(--muted); font-weight: 400; }
.term.open > button::after { content: '−'; }
.term .def { display: none; padding: 0 12px 10px; color: var(--text); }
.term.open .def { display: block; }

/* sort block */
.sort .s-title { font-weight: 700; margin-bottom: 8px; }
.sort .pool { display: flex; flex-wrap: wrap; gap: 6px; min-height: 44px; padding: 8px; border: 1px dashed var(--line); border-radius: 10px; margin-bottom: 10px; }
.sort .groups { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.sort .group { border: 1px solid var(--line); border-radius: 10px; padding: 8px; min-height: 90px; background: var(--surface); cursor: pointer; }
.sort .group.target { border-color: var(--accent); background: var(--accent-soft); }
.sort .group h4 { margin: 0 0 6px; font-size: .95rem; }
.sort .group .items { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-block; padding: 5px 10px; border-radius: 99px; border: 1px solid var(--line); background: var(--surface-2); cursor: pointer; font-size: .9rem; user-select: none; }
.chip.selected { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.chip.ok { background: var(--ok-soft); border-color: var(--ok); color: var(--ok); }
.chip.bad { background: var(--bad-soft); border-color: var(--bad); color: var(--bad); }
.sort .hint { margin-top: 6px; }

/* flashcards */
.flash-wrap { max-width: 560px; margin: 0 auto; }
.flash { perspective: 1200px; height: 300px; cursor: pointer; }
.flash .inner { position: relative; width: 100%; height: 100%; transition: transform .45s; transform-style: preserve-3d; }
.flash.flipped .inner { transform: rotateY(180deg); }
.flash .face { position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden; display: flex; align-items: center; justify-content: center; text-align: center; padding: 22px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); font-size: 1.2rem; overflow: auto; }
.flash .face.back { transform: rotateY(180deg); font-size: 1rem; }
.flash .face .side-label { position: absolute; top: 10px; left: 14px; font-size: .75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.flash-stats { display: flex; justify-content: space-between; color: var(--muted); font-size: .9rem; margin: 8px 0; }

/* quiz */
.quiz-head { display: flex; justify-content: space-between; align-items: center; color: var(--muted); font-size: .9rem; margin-bottom: 8px; }
.q-text { font-size: 1.1rem; font-weight: 600; margin-bottom: 12px; }
.q-course { font-size: .8rem; color: var(--muted); margin-bottom: 4px; }
.options { display: flex; flex-direction: column; gap: 8px; }
.opt { display: flex; align-items: center; gap: 10px; padding: 11px 13px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); cursor: pointer; text-align: left; width: 100%; color: var(--text); }
.opt:hover { border-color: var(--accent); }
.opt.selected { border-color: var(--accent); background: var(--accent-soft); }
.opt.ok { border-color: var(--ok); background: var(--ok-soft); }
.opt.bad { border-color: var(--bad); background: var(--bad-soft); }
.opt.locked { cursor: default; }
.opt .box { width: 20px; height: 20px; border: 2px solid var(--line); border-radius: 6px; flex-shrink: 0; display: inline-flex; align-items: center; justify-content: center; font-size: .75rem; }
.opt.round .box { border-radius: 50%; }
.opt.selected .box, .opt.ok .box { border-color: currentColor; }
.opt.selected .box::after { content: '●'; font-size: .6rem; }
.feedback { margin-top: 12px; padding: 10px 14px; border-radius: 10px; font-size: .95rem; }
.feedback.ok { background: var(--ok-soft); color: var(--ok); }
.feedback.bad { background: var(--bad-soft); color: var(--bad); }
.feedback .expl { color: var(--text); margin-top: 4px; }
.match { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.match .col { display: flex; flex-direction: column; gap: 8px; }
.match .opt { min-height: 48px; font-size: .92rem; }
.match .opt.paired { border-style: dashed; }
.match .tag { font-size: .7rem; font-weight: 700; color: var(--accent); margin-left: auto; }
.fill input { max-width: 360px; }
.result-big { text-align: center; padding: 20px 0; }
.result-big .score { font-size: 3rem; font-weight: 800; color: var(--accent); }
.result-big .score.bad { color: var(--bad); }
.breakdown { list-style: none; padding: 0; margin: 12px 0; }
.breakdown li { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.breakdown li .progress { flex: 1; }
.review { margin-top: 16px; }
.review .item { padding: 10px 12px; border-radius: 10px; margin-bottom: 8px; border: 1px solid var(--line); }
.review .item.bad { border-color: var(--bad); }
.review .item.ok { border-color: var(--ok); }
.review .lbl { font-size: .78rem; color: var(--muted); }

/* results */
.results-table td:first-child { white-space: nowrap; color: var(--muted); font-size: .85rem; }

/* lightbox */
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.88); z-index: 50; display: flex; align-items: center; justify-content: center; padding: 20px; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; background: #fff; border-radius: 8px; }
.lb-close { position: absolute; top: max(12px, env(safe-area-inset-top)); right: 14px; background: #fff; border: 0; border-radius: 50%; width: 40px; height: 40px; font-size: 1.1rem; cursor: pointer; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 60; font-size: .92rem; box-shadow: var(--shadow); max-width: 90vw; }

.stats-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; margin: 12px 0; }
.stat { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.stat .v { font-size: 1.4rem; font-weight: 800; color: var(--accent); }
.stat .l { font-size: .8rem; color: var(--muted); }
.final-card { border: 2px solid var(--accent); }
.empty { color: var(--muted); text-align: center; padding: 24px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
@media (max-width: 480px) {
  .container { padding: 12px; }
  .brand span { display: none; }
  .flash { height: 260px; }
  .match { grid-template-columns: 1fr; }
}
[hidden] { display: none !important; }
