:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #f0ede6;
  --text: #1d2321;
  --muted: #5f6b67;
  --border: #e2ddd2;
  --accent: #0f5c58;
  --accent-2: #137a74;
  --accent-soft: #e2f0ee;
  --gold: #c98a12;
  --gold-soft: #fbf0d9;
  --ok: #1b7a43;
  --ok-soft: #e3f4ea;
  --bad: #b3261e;
  --bad-soft: #fbe7e5;
  --shadow: 0 1px 2px rgba(20, 30, 28, .06), 0 4px 16px rgba(20, 30, 28, .06);
  --radius: 14px;
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", Consolas, "SF Mono", Menlo, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131716;
    --surface: #1b201f;
    --surface-2: #232a28;
    --text: #e8ecea;
    --muted: #9aa7a3;
    --border: #2f3836;
    --accent: #3fb3a9;
    --accent-2: #5cc7bd;
    --accent-soft: #19302d;
    --gold: #e7b354;
    --gold-soft: #33291a;
    --ok: #57c486;
    --ok-soft: #17301f;
    --bad: #f08a80;
    --bad-soft: #3a1e1c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    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 var(--font);
  min-height: 100vh;
}
a { color: var(--accent); }
button, input, textarea, select { font: inherit; color: inherit; }
h1, h2, h3, h4 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.1rem; }
p { margin: 0 0 .75em; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.hidden { display: none !important; }

/* Layout */
.shell { min-height: 100vh; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 16px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; text-decoration: none; color: var(--text); }
.brand img { width: 28px; height: 28px; }
.brand small { font-weight: 500; color: var(--muted); }
.topnav { display: flex; gap: 4px; margin-left: auto; }
.topnav a {
  padding: 8px 12px; border-radius: 10px; text-decoration: none; color: var(--muted); font-weight: 500;
}
.topnav a.active, .topnav a:hover { background: var(--accent-soft); color: var(--accent); }
main { flex: 1; width: 100%; max-width: 920px; margin: 0 auto; padding: 20px 16px 96px; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  display: none; justify-content: space-around;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 4px 2px; font-size: .72rem; text-decoration: none; color: var(--muted);
}
.tabbar a .ico { font-size: 1.25rem; line-height: 1; }
.tabbar a.active { color: var(--accent); font-weight: 600; }

@media (max-width: 760px) {
  .topnav { display: none; }
  .tabbar { display: flex; }
  .brand small { display: none; }
  h1 { font-size: 1.35rem; }
}

/* Surfaces */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow);
}
.card + .card, .stack > * + * { margin-top: 14px; }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(min(200px, 42vw), 1fr)); }
@media (max-width: 760px) { .tile { min-height: 0; padding: 14px; } .tile span { font-size: .8rem; } }
.tile {
  display: flex; flex-direction: column; gap: 6px; padding: 16px; min-height: 112px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  text-decoration: none; color: var(--text); box-shadow: var(--shadow);
  transition: transform .12s ease, border-color .12s ease;
}
.tile:hover { transform: translateY(-2px); border-color: var(--accent); }
.tile .ico { font-size: 1.5rem; }
.tile strong { font-size: 1.02rem; }
.tile span { color: var(--muted); font-size: .88rem; }

.hero {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; border: 0;
}
.hero .muted { color: rgba(255, 255, 255, .82); }
.hero .btn { background: #fff; color: var(--accent); }
@media (prefers-color-scheme: dark) { .hero { color: #0d1a18; } .hero .muted { color: rgba(13, 26, 24, .8); } .hero .btn { background: #0d1a18; color: var(--accent-2); } }
.rank-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.meter { height: 8px; border-radius: 99px; background: rgba(255, 255, 255, .3); overflow: hidden; margin-top: 10px; }
.meter > div { height: 100%; background: var(--gold); border-radius: 99px; }
.progress { height: 6px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); transition: width .3s ease; }

/* Controls */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 10px 18px; border-radius: 12px; border: 1px solid transparent;
  background: var(--accent); color: #fff; font-weight: 600; cursor: pointer; text-decoration: none;
}
@media (prefers-color-scheme: dark) { .btn { color: #0d1a18; } }
.btn:hover { filter: brightness(1.07); }
.btn:disabled { opacity: .55; cursor: default; filter: none; }
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn.ghost { background: transparent; color: var(--accent); border-color: transparent; padding-inline: 10px; }
.btn.danger { background: var(--bad-soft); color: var(--bad); }
.btn.small { min-height: 34px; padding: 6px 12px; font-size: .88rem; border-radius: 10px; }
.btn.block { width: 100%; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }

label { display: block; font-weight: 600; margin-bottom: 6px; }
input[type=text], input[type=password], textarea, select {
  width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; min-height: 96px; }
.field + .field { margin-top: 14px; }
.check { display: flex; gap: 8px; align-items: center; font-weight: 400; }

.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px; border-radius: 99px;
  font-size: .78rem; font-weight: 600; background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.chip.active { background: var(--gold-soft); color: var(--gold); }
.chip.accent { background: var(--accent-soft); color: var(--accent); }
.chip.bad { background: var(--bad-soft); color: var(--bad); }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login .card { width: 100%; max-width: 420px; padding: 28px; }
.login .logo { width: 48px; height: 48px; margin-bottom: 12px; }
.code-box {
  font: 700 1.5rem/1.2 var(--mono); letter-spacing: .08em; text-align: center;
  padding: 16px; border-radius: 12px; background: var(--gold-soft); color: var(--text);
  border: 1px dashed var(--gold); user-select: all; margin: 12px 0;
}

/* Lists */
.list { list-style: none; margin: 0; padding: 0; }
.list > li + li { border-top: 1px solid var(--border); }
.list a, .list button.item {
  display: flex; align-items: center; gap: 12px; width: 100%; padding: 14px 4px;
  text-decoration: none; color: var(--text); background: none; border: 0; text-align: left; cursor: pointer;
}
.list a:hover, .list button.item:hover { color: var(--accent); }
.list .num {
  flex: none; width: 32px; height: 32px; border-radius: 10px; display: grid; place-items: center;
  background: var(--surface-2); font-weight: 700; font-size: .9rem; color: var(--muted);
}
.list .num.ok { background: var(--ok-soft); color: var(--ok); }
.list .num.active { background: var(--gold-soft); color: var(--gold); }
.list .grow { flex: 1; min-width: 0; }
.crumbs { font-size: .88rem; margin-bottom: 10px; }
.crumbs a { text-decoration: none; }

/* Lesson */
.lesson-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.lesson-head h1 { margin: 0; flex: 1; font-size: 1.25rem; }
.case { border-left: 4px solid var(--gold); background: var(--gold-soft); }
.case .label { font-size: .78rem; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--gold); }
.protected, .protected img {
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-user-drag: none;
}
.slide-frame {
  position: relative; border-radius: 12px; overflow: hidden; background: var(--surface-2);
  aspect-ratio: 16 / 9; display: grid; place-items: center;
}
.slide-frame img { width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
.screen-frame { border-radius: 12px; overflow: hidden; background: var(--surface-2); }
.screen-frame img { display: block; width: 100%; height: auto; pointer-events: none; }
.slide-nav { display: flex; align-items: center; gap: 10px; margin-top: 12px; }
.slide-nav .count { flex: 1; text-align: center; color: var(--muted); font-variant-numeric: tabular-nums; }
.options { display: grid; gap: 10px; margin-top: 14px; }
.option {
  text-align: left; padding: 14px 16px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; line-height: 1.4;
}
.option:hover:not(:disabled) { border-color: var(--accent); }
.option:disabled { cursor: default; }
.option.correct { border-color: var(--ok); background: var(--ok-soft); }
.option.wrong { border-color: var(--bad); background: var(--bad-soft); }
.options.numbers { grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); }
.options.numbers .option { text-align: center; font-weight: 700; font-size: 1.1rem; }
.feedback { margin-top: 14px; padding: 14px 16px; border-radius: 12px; }
.feedback.ok { background: var(--ok-soft); }
.feedback.bad { background: var(--bad-soft); }
.feedback strong { display: block; margin-bottom: 4px; }
.redact-text { line-height: 2.1; padding: 14px; border-radius: 12px; background: var(--surface-2); font-family: var(--mono); font-size: .92rem; }
.redact-part {
  border: 1px dashed var(--muted); background: var(--surface); border-radius: 6px; padding: 1px 6px; cursor: pointer;
  font: inherit;
}
.redact-part.on { background: #222; color: #222; border-style: solid; }
@media (prefers-color-scheme: dark) { .redact-part.on { background: #000; color: #000; } }
.redact-part.missed { outline: 2px solid var(--bad); }
.redact-part.extra { outline: 2px solid var(--gold); }
.points-pop { color: var(--gold); font-weight: 700; }
.done-badge { font-size: 3rem; text-align: center; }

/* Rich text */
.md { overflow-wrap: anywhere; }
.md > :last-child { margin-bottom: 0; }
.md ul, .md ol { padding-left: 1.3em; margin: 0 0 .75em; }
.md li + li { margin-top: .25em; }
.md h3, .md h4, .md h5, .md h6 { margin: 1em 0 .4em; }
.md code { font-family: var(--mono); font-size: .88em; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
.md blockquote { margin: 0 0 .75em; padding: 6px 14px; border-left: 3px solid var(--accent); color: var(--muted); }
.codeblock { position: relative; margin: 0 0 .9em; }
.codeblock pre {
  margin: 0; padding: 14px 14px 46px; border-radius: 12px; background: var(--surface-2); border: 1px solid var(--border);
  white-space: pre-wrap; overflow-wrap: anywhere; font: .88rem/1.5 var(--mono);
}
.codeblock pre code { background: none; padding: 0; }
.copy-btn {
  position: absolute; right: 8px; bottom: 8px; padding: 6px 12px; border-radius: 9px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); font-size: .82rem; font-weight: 600; color: var(--accent);
}
.table-wrap { overflow-x: auto; margin: 0 0 .9em; }
table { border-collapse: collapse; width: 100%; font-size: .92rem; }
th, td { border: 1px solid var(--border); padding: 8px 10px; text-align: left; vertical-align: top; }
th { background: var(--surface-2); font-weight: 600; }

/* Chat */
.chat { display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 92%; padding: 12px 16px; border-radius: 16px; }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; white-space: pre-wrap; }
@media (prefers-color-scheme: dark) { .msg.user { color: #0d1a18; } }
.msg.bot { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.note { align-self: center; background: var(--gold-soft); font-size: .92rem; text-align: center; }
.msg .sources { margin-top: 10px; font-size: .85rem; color: var(--muted); }
.msg .actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.typing { display: inline-flex; gap: 4px; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.composer {
  position: sticky; bottom: 72px; margin-top: 16px; display: flex; gap: 8px; align-items: flex-end;
  background: var(--bg); padding: 8px 0;
}
@media (min-width: 761px) { .composer { bottom: 0; } }
.composer textarea { min-height: 52px; max-height: 40vh; }

/* Admin */
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 16px; }
.tabs button {
  padding: 8px 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-weight: 500;
}
.tabs button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
@media (prefers-color-scheme: dark) { .tabs button.active { color: #0d1a18; } }
.stats { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
.stat { padding: 14px; border-radius: 12px; background: var(--surface); border: 1px solid var(--border); }
.stat b { display: block; font-size: 1.5rem; }
.stat span { color: var(--muted); font-size: .85rem; }
.qcard { border-left: 4px solid var(--gold); }
.qcard.answered { border-left-color: var(--ok); }
.qcard .q { white-space: pre-wrap; }
.draft { background: var(--surface-2); padding: 10px 12px; border-radius: 10px; font-size: .9rem; }

/* Overlays */
#toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 12px; font-weight: 500;
  opacity: 0; pointer-events: none; transition: all .2s ease; z-index: 60; max-width: calc(100vw - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.modal {
  position: fixed; inset: 0; z-index: 50; background: rgba(10, 15, 14, .7);
  display: grid; place-items: center; padding: 16px;
}
.modal[hidden] { display: none; }
.modal .sheet {
  width: 100%; max-width: 960px; background: var(--surface); border-radius: var(--radius); padding: 14px;
  max-height: calc(100vh - 32px); overflow: auto;
}
.modal img { width: 100%; height: auto; display: block; border-radius: 10px; pointer-events: none; }
.empty { text-align: center; padding: 32px 16px; color: var(--muted); }
.spinner { width: 28px; height: 28px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; margin: 40px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

@media print { body { display: none; } }
