:root {
  --bg: #f6f7f9; --panel: #ffffff; --panel2: #f0f3f6; --text: #16202a; --muted: #5b6875; --line: #dde3ea;
  --brand: #0f766e; --brand2: #14b8a6; --brandSoft: #e6f5f3; --good: #15803d; --goodSoft: #e7f6ec;
  --bad: #b91c1c; --badSoft: #fdecec; --warn: #b45309; --warnSoft: #fdf3e3;
  --d1: #2563eb; --d2: #7c3aed; --d3: #0891b2; --d4: #c2410c; --d5: #be123c; --d6: #15803d;
  --radius: 12px; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
[data-theme="dark"] {
  --bg: #0e1419; --panel: #151d24; --panel2: #1b252e; --text: #e5ecf2; --muted: #93a2b0; --line: #26323d;
  --brand: #2dd4bf; --brand2: #14b8a6; --brandSoft: #10302d; --good: #4ade80; --goodSoft: #11291b;
  --bad: #f87171; --badSoft: #2e1517; --warn: #fbbf24; --warnSoft: #2c2412;
  --d1: #60a5fa; --d2: #a78bfa; --d3: #22d3ee; --d4: #fb923c; --d5: #fb7185; --d6: #4ade80;
  --shadow: none;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.55 var(--font); -webkit-font-smoothing: antialiased; }
a { color: var(--brand); text-decoration: none; }
button { font: inherit; color: inherit; cursor: pointer; }
input, select, textarea { font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: .5rem .65rem; width: 100%; }
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--brand2); outline-offset: 1px; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .6rem; }
h1 { font-size: 1.6rem; } h2 { font-size: 1.25rem; } h3 { font-size: 1.05rem; }
.hidden { display: none !important; }
.muted { color: var(--muted); } .small { font-size: .85rem; } .mono { font-family: var(--mono); }

/* Top bar */
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 1rem; padding: .55rem 1rem; background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: .5rem; color: var(--text); font-size: 1.05rem; white-space: nowrap; }
.brand svg { width: 26px; height: 26px; fill: var(--brand); stroke: var(--brand); stroke-width: 3; stroke-linecap: round; }
.brand svg path { fill: none; }
.brand b { color: var(--brand); }
.nav { display: flex; gap: .25rem; flex: 1; overflow-x: auto; }
.nav a { padding: .4rem .7rem; border-radius: 8px; color: var(--muted); white-space: nowrap; }
.nav a:hover { background: var(--panel2); color: var(--text); }
.nav a.active { background: var(--brandSoft); color: var(--brand); font-weight: 600; }
.top-actions { display: flex; gap: .35rem; }
.chip { border: 1px solid var(--line); background: var(--panel); padding: .3rem .65rem; border-radius: 999px; font-size: .85rem; }
.chip:hover { background: var(--panel2); }
.menu-btn { display: none; }
@media (max-width: 900px) {
  .menu-btn { display: inline-block; }
  .nav { display: none; position: absolute; top: 100%; left: 0; right: 0; flex-direction: column; background: var(--panel); border-bottom: 1px solid var(--line); padding: .5rem; }
  .nav.open { display: flex; }
}

.container { max-width: 1120px; margin: 0 auto; padding: 1.25rem 1rem 4rem; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem 1.2rem; box-shadow: var(--shadow); }
.grid { display: grid; gap: 1rem; }
.g2 { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.g3 { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.row { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.stack > * + * { margin-top: 1rem; }

.btn { border: 1px solid var(--line); background: var(--panel); padding: .5rem .95rem; border-radius: 9px; font-weight: 500; }
.btn:hover { background: var(--panel2); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
[data-theme="dark"] .btn.primary { color: #062421; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--bad); }
.btn.sm { padding: .25rem .6rem; font-size: .85rem; }
.btn:disabled { opacity: .5; cursor: default; }

.badge { display: inline-block; font-size: .75rem; padding: .1rem .5rem; border-radius: 999px; background: var(--panel2); color: var(--muted); font-weight: 600; }
.dom-tag { display: inline-block; font-size: .75rem; font-weight: 700; padding: .1rem .5rem; border-radius: 6px; color: #fff; }
.d1 { --dc: var(--d1); } .d2 { --dc: var(--d2); } .d3 { --dc: var(--d3); } .d4 { --dc: var(--d4); } .d5 { --dc: var(--d5); } .d6 { --dc: var(--d6); }
.dom-tag { background: var(--dc); }
[data-theme="dark"] .dom-tag { color: #0e1419; }

/* Progress */
.bar { height: 8px; border-radius: 999px; background: var(--panel2); overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--dc, var(--brand)); border-radius: 999px; transition: width .4s; }
.ring { --p: 0; width: 120px; height: 120px; border-radius: 50%; display: grid; place-items: center;
  background: conic-gradient(var(--brand) calc(var(--p) * 1%), var(--panel2) 0); }
.ring > div { width: 92px; height: 92px; border-radius: 50%; background: var(--panel); display: grid; place-items: center; text-align: center; }
.ring b { font-size: 1.6rem; display: block; line-height: 1; }

.domain-card { border-left: 4px solid var(--dc); cursor: pointer; }
.domain-card:hover { background: var(--panel2); }
.stat { display: flex; justify-content: space-between; font-size: .85rem; color: var(--muted); margin-top: .35rem; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: .75rem; }
.kpi { background: var(--panel2); border-radius: 10px; padding: .75rem; }
.kpi b { font-size: 1.35rem; display: block; }

/* Study */
.obj { border-top: 1px solid var(--line); padding-top: 1rem; margin-top: 1rem; }
.obj h3 { display: flex; gap: .5rem; align-items: baseline; }
.notes { margin: .5rem 0 .8rem; padding-left: 1.2rem; }
.notes li { margin: .35rem 0; }
.subs { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: .3rem .8rem; }
.sub { display: flex; gap: .5rem; align-items: flex-start; padding: .3rem .4rem; border-radius: 8px; cursor: pointer; font-size: .92rem; }
.sub:hover { background: var(--panel2); }
.sub input { width: auto; margin-top: .25rem; accent-color: var(--brand); }
.sub .acc { margin-left: auto; font-size: .75rem; }
.diagram { background: var(--panel2); border-radius: 10px; padding: .75rem; margin: .5rem 0 1rem; overflow-x: auto; }
.diagram svg { display: block; max-width: 100%; height: auto; margin: 0 auto; }
.diagram svg text { fill: var(--text); font-family: var(--font); }
.diagram .cap { font-size: .8rem; color: var(--muted); text-align: center; margin-top: .3rem; }

/* Flashcards */
.flash-wrap { perspective: 1200px; max-width: 680px; margin: 1rem auto; }
.flash { position: relative; min-height: 260px; transition: transform .45s; transform-style: preserve-3d; cursor: pointer; }
.flash.flipped { transform: rotateY(180deg); }
.flash .face { position: absolute; inset: 0; backface-visibility: hidden; display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center; padding: 2rem; border-radius: 16px; background: var(--panel); border: 1px solid var(--line); box-shadow: var(--shadow); font-size: 1.15rem; }
.flash .back { transform: rotateY(180deg); background: var(--brandSoft); }
.flash .meta { position: absolute; top: .75rem; left: 1rem; right: 1rem; display: flex; justify-content: space-between; font-size: .75rem; color: var(--muted); }
.grade { display: flex; gap: .5rem; justify-content: center; margin-top: 1rem; }

/* Quiz */
.q-text { font-size: 1.08rem; font-weight: 500; margin-bottom: 1rem; }
.answers { display: grid; gap: .55rem; }
.ans { text-align: left; border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: .7rem .9rem; display: flex; gap: .7rem; align-items: flex-start; }
.ans:hover:not(:disabled) { border-color: var(--brand); }
.ans .key { font-weight: 700; color: var(--muted); min-width: 1.2rem; }
.ans.sel { border-color: var(--brand); background: var(--brandSoft); }
.ans.ok { border-color: var(--good); background: var(--goodSoft); }
.ans.no { border-color: var(--bad); background: var(--badSoft); }
.ans:disabled { cursor: default; }
.explain { margin-top: 1rem; padding: .8rem 1rem; border-radius: 10px; background: var(--panel2); border-left: 4px solid var(--brand); }
.explain.ok { border-color: var(--good); } .explain.no { border-color: var(--bad); }
.qnav { display: grid; grid-template-columns: repeat(auto-fill, minmax(34px, 1fr)); gap: .3rem; }
.qnav button { border: 1px solid var(--line); background: var(--panel); border-radius: 6px; padding: .25rem 0; font-size: .8rem; }
.qnav button.done { background: var(--brandSoft); border-color: var(--brand); }
.qnav button.flag { box-shadow: inset 0 -3px 0 var(--warn); }
.qnav button.cur { outline: 2px solid var(--text); }
.qnav button.ok { background: var(--goodSoft); border-color: var(--good); }
.qnav button.no { background: var(--badSoft); border-color: var(--bad); }
.timer { font-family: var(--mono); font-size: 1.2rem; font-weight: 700; }
.timer.low { color: var(--bad); }
.result-big { font-size: 2.6rem; font-weight: 800; }
.pass { color: var(--good); } .fail { color: var(--bad); }

.exam-grid { grid-template-columns: minmax(0,1fr) 290px; align-items: start; }
@media (max-width: 760px) { .exam-grid { grid-template-columns: 1fr; } }

/* Tables */
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { text-align: left; padding: .5rem .55rem; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { color: var(--muted); font-weight: 600; font-size: .8rem; text-transform: uppercase; letter-spacing: .02em; }
.table-wrap { overflow-x: auto; }

/* Calculators */
.calc label { display: block; font-size: .82rem; color: var(--muted); margin: .45rem 0 .15rem; }
.calc .out { margin-top: .75rem; padding: .65rem .8rem; background: var(--brandSoft); border-radius: 9px; font-weight: 600; }
.calc .out small { display: block; font-weight: 400; color: var(--muted); }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; }

/* Modal & toast */
.modal { position: fixed; inset: 0; background: rgba(5,10,15,.55); display: grid; place-items: center; z-index: 50; padding: 1rem; }
.modal-card { background: var(--panel); border-radius: 14px; padding: 1.4rem; width: min(420px, 100%); border: 1px solid var(--line); }
.toast { position: fixed; bottom: 1.2rem; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: .6rem 1rem; border-radius: 10px; z-index: 60; font-size: .9rem; }
.tabs { display: flex; gap: .3rem; border-bottom: 1px solid var(--line); margin-bottom: 1rem; flex-wrap: wrap; }
.tabs button { background: none; border: none; padding: .5rem .8rem; border-bottom: 2px solid transparent; color: var(--muted); }
.tabs button.active { border-color: var(--brand); color: var(--brand); font-weight: 600; }
.err { color: var(--bad); font-size: .88rem; min-height: 1.2em; }
.legend { display: flex; gap: .8rem; flex-wrap: wrap; font-size: .8rem; color: var(--muted); }
@media print { .topbar, .btn { display: none; } }
.obj:first-child { border-top: none; margin-top: 0; padding-top: 0; }
.chip { white-space: nowrap; }
@media (max-width: 520px) {
  .brand span { display: none; }
  .topbar { gap: .5rem; }
  #userBtn { max-width: 110px; overflow: hidden; text-overflow: ellipsis; }
}

/* Book */
.book-grid { display: grid; grid-template-columns: 280px minmax(0, 1fr); gap: 1rem; align-items: start; }
.book-side { position: sticky; top: 70px; max-height: calc(100vh - 90px); overflow: auto; padding: .9rem; }
.book-side .ch { display: flex; gap: .4rem; align-items: center; flex-wrap: wrap; padding: .35rem .5rem; border-radius: 8px; color: var(--text); font-size: .88rem; }
.book-side .ch:hover { background: var(--panel2); }
.book-side .ch.active { background: var(--brandSoft); color: var(--brand); font-weight: 600; }
.book-side .ch .dom-tag { font-size: .65rem; padding: 0 .35rem; }
.book-frame { width: 100%; height: calc(100vh - 140px); min-height: 500px; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
@media (max-width: 900px) { .book-grid { grid-template-columns: 1fr; } .book-side { position: static; max-height: none; } }

/* Read-aloud */
.q-head { display: flex; gap: .75rem; align-items: flex-start; }
.q-head .q-text { flex: 1; }
.btn.tts.on { background: var(--brandSoft); border-color: var(--brand); color: var(--brand); }
.tts-auto { white-space: nowrap; }
.rate { width: auto; padding: .2rem .4rem; font-size: .85rem; }

/* Book viewer (PDF.js) */
.book-bar { position: sticky; top: 56px; z-index: 5; display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; padding: .5rem .7rem; margin-bottom: .7rem; }
.ch-sel { display: none; width: auto; max-width: 100%; }
.pg-in { width: 4.2rem; padding: .25rem .4rem; text-align: center; }
.pages { margin: 0 auto; max-width: 100%; }
.book-main { min-width: 0; }
.pages-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.pdf-page { background: #fff; margin: 0 auto .8rem; box-shadow: var(--shadow); border: 1px solid var(--line); width: 100%; }
.pdf-page canvas { display: block; width: 100%; height: auto; }

/* Bottom tab bar for phones */
.tabbar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: none; background: var(--panel); border-top: 1px solid var(--line);
  padding: .25rem .25rem calc(.25rem + env(safe-area-inset-bottom)); }
.tabbar a, .tabbar button { flex: 1; display: flex; flex-direction: column; align-items: center; gap: .05rem; font-size: .68rem; color: var(--muted); background: none; border: none; padding: .3rem 0; border-radius: 8px; }
.tabbar span { font-size: 1.25rem; line-height: 1; }
.tabbar .active { color: var(--brand); font-weight: 600; }

/* Touch devices: bigger targets, no iOS zoom on inputs */
@media (pointer: coarse) {
  .ans { min-height: 54px; padding: .85rem 1rem; }
  .btn { min-height: 42px; }
  .btn.sm { min-height: 36px; }
  .qnav button { min-height: 36px; }
  .sub { padding: .5rem; }
  input, select, textarea { font-size: 16px; }
}
/* Tablets (iPad portrait/landscape) */
@media (max-width: 1100px) {
  .container { padding: 1rem .9rem 3rem; }
}
@media (max-width: 900px) {
  .ch-sel { display: inline-block; flex: 1 1 100%; }
  .book-side { display: none; }
  .book-bar { top: 52px; }
}
/* Phones */
@media (max-width: 700px) {
  .tabbar:not(.hidden) { display: flex; }
  body { padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
  .menu-btn { display: none; }
  .nav { bottom: calc(64px + env(safe-area-inset-bottom)); top: auto; position: fixed; border-top: 1px solid var(--line); border-bottom: none; box-shadow: 0 -8px 24px rgba(0,0,0,.12); }
  h1 { font-size: 1.35rem; }
  .card { padding: .9rem; }
  .q-text { font-size: 1.02rem; }
  .q-head { flex-direction: column; }
  .flash { min-height: 300px; }
  .flash .face { font-size: 1.05rem; padding: 2.2rem 1.2rem 1.2rem; }
  .grade .btn { flex: 1; }
  .exam-side { order: -1; position: sticky; top: 52px; z-index: 5; }
  .ring { width: 96px; height: 96px; } .ring > div { width: 74px; height: 74px; } .ring b { font-size: 1.3rem; }
  .two { grid-template-columns: 1fr 1fr; }
  table { font-size: .82rem; }
  .toast { bottom: calc(76px + env(safe-area-inset-bottom)); }
  .topbar { padding-top: calc(.55rem + env(safe-area-inset-top)); }
}

/* PDF text layer (selectable text) */
.pdf-page { position: relative; }
.textLayer { position: absolute; inset: 0; overflow: hidden; line-height: 1; text-align: initial; opacity: 1; -webkit-text-size-adjust: none; text-size-adjust: none; transform-origin: 0 0; z-index: 2; }
.textLayer span, .textLayer br { color: transparent; position: absolute; white-space: pre; cursor: text; transform-origin: 0% 0%; }
.textLayer span.markedContent { top: 0; height: 0; }
.textLayer ::selection { background: rgba(20, 184, 166, .35); }
.textLayer .endOfContent { display: block; position: absolute; inset: 100% 0 0; z-index: -1; cursor: default; user-select: none; }
.pdf-page.reading { outline: 3px solid var(--brand2); outline-offset: 2px; }
.read-sel { position: fixed; left: 50%; transform: translateX(-50%); bottom: 1.2rem; z-index: 40; box-shadow: 0 6px 20px rgba(0,0,0,.25); }
@media (max-width: 700px) { .read-sel { bottom: calc(76px + env(safe-area-inset-bottom)); } }

/* Book search */
.book-search { flex: 1 1 220px; min-width: 160px; }
.book-search input { padding: .35rem .6rem; }
.textLayer mark { background: rgba(250, 204, 21, .55); color: transparent; border-radius: 2px; padding: 0; }
.s-results { position: sticky; top: 112px; z-index: 6; max-height: 60vh; overflow: auto; margin-bottom: .7rem; box-shadow: 0 10px 30px rgba(0,0,0,.18); }
.s-list { display: grid; gap: .35rem; margin-top: .6rem; }
.s-hit { text-align: left; border: 1px solid var(--line); background: var(--panel); border-radius: 9px; padding: .55rem .7rem; display: flex; flex-direction: column; gap: .15rem; font-size: .9rem; }
.s-hit:hover { border-color: var(--brand); }
.s-hit mark, .s-results mark { background: rgba(250, 204, 21, .55); color: inherit; border-radius: 2px; }
@media (max-width: 900px) { .s-results { top: 150px; max-height: 55vh; } .book-search { flex-basis: 100%; } }
.book-side .ch { display: grid; grid-template-columns: 1.6rem 1fr auto; align-items: center; flex-wrap: nowrap; }

/* Login & MFA */
.gate { max-width: 440px; margin: 2.5rem auto; }
.gate.wide { max-width: 560px; }
.code-in { font-size: 1.6rem !important; letter-spacing: .35em; text-align: center; font-family: var(--mono); }
.mfa-steps { padding-left: 1.2rem; } .mfa-steps li { margin: .9rem 0; }
.qr { width: 200px; max-width: 70%; margin: .7rem 0; background: #fff; padding: 6px; border-radius: 10px; border: 1px solid var(--line); }
.qr svg { display: block; width: 100%; height: auto; }
.secret { display: inline-block; font-family: var(--mono); background: var(--panel2); padding: .35rem .6rem; border-radius: 6px; word-break: break-all; user-select: all; }
.rc-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .4rem; }
.rc-grid code { font-family: var(--mono); background: var(--panel2); padding: .45rem; border-radius: 6px; text-align: center; font-size: 1.02rem; }
