/* ---------- Токены ---------- */
:root {
  --bg: #f6f3ec;
  --bg-2: #fffdf8;
  --fg: #1f1d1a;
  --fg-2: #6b665c;
  --line: #ddd7ca;
  --accent: #b5462b;
  --accent-bg: #f3e2da;
  --new: #b5462b;
  --learn: #c98a1b;
  --known: transparent;
  --ok: #3b7a4a;
  --hanzi-size: 22px;
  --pinyin-size: 0.55em;
  --radius: 12px;
  --tap: 48px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --font-cjk: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "Source Han Sans SC", "WenQuanYi Micro Hei", sans-serif;
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]):not([data-theme^="book"]) {
    --bg: #1b1a17; --bg-2: #24221e; --fg: #ece7dc; --fg-2: #a39d90; --line: #3a3731;
    --accent: #e0714f; --accent-bg: #3b2a24; --new: #e0714f; --learn: #e0b04a; --ok: #7bc48c;
  }
}
:root[data-theme="dark"] {
  --bg: #1b1a17; --bg-2: #24221e; --fg: #ece7dc; --fg-2: #a39d90; --line: #3a3731;
  --accent: #e0714f; --accent-bg: #3b2a24; --new: #e0714f; --learn: #e0b04a; --ok: #7bc48c;
}
/* «Книга» — как электронная книга: чистая чёрно-белая раскраска, Literata, плоские поверхности.
   Literata (SIL OFL, googlefonts/literata) нарисована для чтения с экрана — Google Play Books; файлы в public/fonts. */
@font-face {
  font-family: "Literata"; font-style: normal; font-weight: 400 700; font-display: swap;
  src: url("fonts/literata-cyrillic.woff2") format("woff2");
  unicode-range: U+0301, U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
}
@font-face {
  font-family: "Literata"; font-style: normal; font-weight: 400 700; font-display: swap;
  src: url("fonts/literata-latin.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
:root[data-theme="book-light"] {
  --bg: #fff; --bg-2: #fff; --fg: #000; --fg-2: #5a5a5a; --line: #c9c9c9;
  --accent: #000; --accent-bg: #ececec; --new: #000; --learn: #8a8a8a; --ok: #000;
  --radius: 3px;
}
:root[data-theme="book-dark"] {
  --bg: #000; --bg-2: #000; --fg: #e8e8e8; --fg-2: #9a9a9a; --line: #3d3d3d;
  --accent: #fff; --accent-bg: #202020; --new: #fff; --learn: #909090; --ok: #fff;
  --radius: 3px;
}
:root[data-theme^="book"] {
  --font-ui: "Literata", ui-serif, "New York", "Iowan Old Style", Charter, "Noto Serif", serif;
}
:root[data-theme^="book"] .popup { border-radius: 0; box-shadow: none; }
:root[data-theme^="book"] .hint-box { background: transparent; border-left: 3px solid var(--accent); border-radius: 0; padding-left: 11px; }
:root[data-theme^="book"] .badge { border-radius: 3px; }
/* на чёрном фоне белая заливка требует тёмного текста */
:root[data-theme="book-dark"] button.primary,
:root[data-theme="book-dark"] .seg button.on,
:root[data-theme="book-dark"] .badge { color: #000; }
:root[data-theme="book-dark"] .switch::after { background: #9a9a9a; }
:root[data-theme="book-dark"] .switch.on::after { background: #000; }

/* e-ink: чёрно-белый, без теней и анимаций */
:root[data-eink="1"] {
  --bg: #fff; --bg-2: #fff; --fg: #000; --fg-2: #333; --line: #000;
  --accent: #000; --accent-bg: #fff; --new: #000; --learn: #000; --ok: #000; --radius: 0px; --tap: 56px;
}
:root[data-eink="1"] * { transition: none !important; animation: none !important; box-shadow: none !important; }

/* ---------- База ---------- */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--fg); font-family: var(--font-ui); font-size: 17px; line-height: 1.4; }
body { min-height: 100vh; -webkit-text-size-adjust: 100%; }
a { color: var(--accent); text-decoration: none; }
button, .btn { font: inherit; color: var(--fg); background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius); min-height: var(--tap); padding: 8px 16px; cursor: pointer; }
button:active, .btn:active { background: var(--accent-bg); }
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
:root[data-eink="1"] button.primary { background: #000; color: #fff; }
button.small { min-height: 36px; padding: 4px 10px; font-size: 15px; }
button:disabled { opacity: .5; }
input, select, textarea { font: inherit; color: var(--fg); background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; min-height: var(--tap); }
textarea { width: 100%; min-height: 160px; font-family: var(--font-cjk); }
h1 { font-size: 22px; margin: 0 0 12px; }
h2 { font-size: 18px; margin: 20px 0 8px; }
.muted { color: var(--fg-2); }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.grow { flex: 1; }
.cjk { font-family: var(--font-cjk); }
.loading, .error { padding: 40px 16px; text-align: center; color: var(--fg-2); }
.error { color: var(--accent); }
#toast { position: fixed; left: 50%; bottom: calc(80px + var(--sab)); transform: translateX(-50%); background: var(--fg); color: var(--bg); padding: 10px 16px; border-radius: 20px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 50; max-width: 90vw; }
#toast.show { opacity: 1; }

#app { display: flex; flex-direction: column; min-height: 100vh; }
.screen { flex: 1; padding: calc(12px + var(--sat)) calc(16px + var(--sar)) calc(80px + var(--sab)) calc(16px + var(--sal)); max-width: 760px; width: 100%; margin: 0 auto; }
#app.no-nav .screen { padding-bottom: var(--sab); }

/* ---------- Нижняя панель ---------- */
.tabbar { position: fixed; left: 0; right: 0; bottom: 0; display: flex; background: var(--bg-2); border-top: 1px solid var(--line); padding-bottom: var(--sab); z-index: 20; }
.tab { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 56px; color: var(--fg-2); position: relative; }
.tab.active { color: var(--accent); }
:root[data-eink="1"] .tab.active { font-weight: bold; text-decoration: underline; }
.tab-icon { font-family: var(--font-cjk); font-size: 20px; line-height: 1; }
.tab-label { font-size: 11px; margin-top: 3px; }
.badge { position: absolute; top: 6px; right: calc(50% - 26px); background: var(--accent); color: #fff; font-size: 11px; border-radius: 10px; padding: 1px 6px; min-width: 18px; text-align: center; }
:root[data-eink="1"] .badge { background: #000; }

/* ---------- Библиотека ---------- */
.level-group { margin-bottom: 8px; }
.card-list { display: flex; flex-direction: column; gap: 8px; }
.text-card { display: block; background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; color: var(--fg); }
.text-card.recommended { border-color: var(--ok); border-width: 2px; }
.text-card .title { font-family: var(--font-cjk); font-size: 20px; }
.text-card .sub { color: var(--fg-2); font-size: 14px; margin-top: 2px; display: flex; justify-content: space-between; gap: 8px; }
.text-card .cov { font-weight: 600; }
.text-card .cov.good { color: var(--ok); }
.text-card .cov.hard { color: var(--accent); }
.text-card .bar { height: 4px; background: var(--line); border-radius: 2px; margin-top: 8px; overflow: hidden; }
.text-card .bar > i { display: block; height: 100%; background: var(--ok); }
:root[data-eink="1"] .text-card .bar > i { background: #000; }
.hint-box { background: var(--accent-bg); border-radius: var(--radius); padding: 10px 14px; font-size: 15px; margin-bottom: 12px; }

/* ---------- Читалка ---------- */
.screen-read { padding-top: 0; padding-bottom: 0; max-width: none; }
.reader-top { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 6px; padding: calc(6px + var(--sat)) 8px 6px; background: var(--bg); border-bottom: 1px solid var(--line); }
.reader-top .title { font-family: var(--font-cjk); font-weight: 600; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reader-top button { min-height: 40px; padding: 4px 10px; }
.reader-body { padding: 12px 16px calc(120px + var(--sab)); max-width: 760px; margin: 0 auto; font-family: var(--font-cjk); font-size: var(--hanzi-size); line-height: 1.9; }
.reader-body.pages { overflow: hidden; padding-bottom: 12px; }
.reader-body p { margin: 0 0 1em; text-align: left; }
.reader-body .tok { border-bottom: 2px solid transparent; cursor: pointer; padding-bottom: 1px; }
.reader-body ruby.tok { ruby-position: over; ruby-align: center; white-space: nowrap; }
.reader-body .tok.punct { cursor: default; border: 0; }
.tok rt.py { display: none; font-size: var(--pinyin-size); line-height: 1.2; color: var(--fg-2); font-family: var(--font-ui); white-space: nowrap; text-align: center; }
.reader-body.py-all .tok rt.py, .reader-body.py-new .tok.st-new rt.py, .reader-body.py-new .tok.st-learning rt.py { display: ruby-text; }
.reader-body.py-all, .reader-body.py-new { line-height: 2.4; }
.tok.st-new { border-bottom-color: var(--new); }
.tok.st-learning { border-bottom-color: var(--learn); border-bottom-style: dotted; }
.tok.st-known { border-bottom-color: transparent; }
.tok.proper { border-bottom-style: double; }
.tok.hl { background: var(--accent-bg); }
:root[data-eink="1"] .tok.st-new { border-bottom-style: solid; }
:root[data-eink="1"] .tok.st-learning { border-bottom-style: dotted; border-bottom-width: 3px; }
:root[data-eink="1"] .tok.hl { background: #000; color: #fff; }
.reader-body.no-marks .tok.st-new { border-bottom-color: transparent; }
.page-nav { position: fixed; left: 0; right: 0; bottom: 0; display: flex; justify-content: space-between; align-items: center; padding: 8px 16px calc(8px + var(--sab)); background: var(--bg); border-top: 1px solid var(--line); font-size: 15px; }
.page-nav button { min-width: 100px; }
.reader-end { text-align: center; padding: 24px 0; font-family: var(--font-ui); font-size: 16px; }
.reader-end .row { justify-content: center; }

/* ---------- Всплывающая карточка слова ---------- */
.popup-backdrop { position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.25); }
:root[data-eink="1"] .popup-backdrop { background: transparent; }
.popup { position: fixed; left: 0; right: 0; bottom: 0; z-index: 31; background: var(--bg-2); border-top: 1px solid var(--line); border-radius: 16px 16px 0 0; padding: 12px 16px calc(12px + var(--sab)); max-height: 70vh; overflow-y: auto; box-shadow: 0 -4px 24px rgba(0,0,0,.15); }
:root[data-eink="1"] .popup { border: 2px solid #000; border-bottom: 0; border-radius: 0; max-height: 60vh; }
.popup .head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.popup .word { font-family: var(--font-cjk); font-size: 34px; line-height: 1.2; }
.popup .pinyin { font-size: 20px; color: var(--fg-2); }
.popup .meaning { margin: 8px 0 10px; font-size: 17px; }
.popup .status-line { font-size: 13px; color: var(--fg-2); margin-bottom: 8px; }
.popup .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.popup .actions button { flex: 1; min-width: 100px; }
.popup .article { margin-top: 10px; border-top: 1px solid var(--line); padding-top: 10px; font-size: 16px; }
.article .a-head { font-weight: 700; margin-top: 8px; }
.article .a-sense { margin: 4px 0; }
.article .a-sense b { color: var(--accent); margin-right: 4px; }
.article .a-ex { font-family: var(--font-cjk); color: var(--fg-2); font-size: 15px; margin-left: 14px; }
.article .a-note { color: var(--fg-2); }
.article .a-entry + .a-entry { border-top: 1px dashed var(--line); margin-top: 8px; padding-top: 8px; }
.approx { font-size: 12px; color: var(--fg-2); }

/* ---------- Повторение ---------- */
.review-card { background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px 16px; min-height: 40vh; display: flex; flex-direction: column; justify-content: center; }
.review-card .sentence { font-family: var(--font-cjk); font-size: calc(var(--hanzi-size) + 4px); line-height: 1.8; text-align: center; }
.review-card .sentence .target { border-bottom: 3px solid var(--accent); font-weight: 600; }
.review-card .sentence ruby.tok { ruby-position: over; }
.review-card .sentence rt.py { display: ruby-text; font-size: 0.5em; color: var(--fg-2); font-family: var(--font-ui); line-height: 1.2; }
.review-card .sentence.with-py { line-height: 2.4; }
.review-card .answer { border-top: 1px solid var(--line); margin-top: 16px; padding-top: 16px; text-align: center; }
.review-card > .word { font-family: var(--font-cjk); font-size: 64px; text-align: center; line-height: 1.3; }
.review-card .answer .word { font-family: var(--font-cjk); font-size: 36px; }
.review-card .answer .pinyin { font-size: 22px; color: var(--fg-2); }
.review-card .answer .meaning { font-size: 18px; margin: 8px 0; }
.review-card .source { color: var(--fg-2); font-size: 14px; margin-top: 10px; }
.review-actions { display: flex; gap: 8px; margin-top: 12px; }
.review-actions button { flex: 1; display: flex; flex-direction: column; align-items: center; padding: 8px 4px; min-height: 60px; }
.review-actions button small { color: var(--fg-2); font-size: 12px; margin-top: 2px; }
.review-actions .again { border-color: var(--accent); }
.review-progress { color: var(--fg-2); font-size: 14px; margin-bottom: 10px; display: flex; justify-content: space-between; }
.empty { text-align: center; color: var(--fg-2); padding: 40px 16px; }

/* ---------- Грамматика ---------- */
.reader-top button.on { background: var(--accent); color: #fff; border-color: var(--accent); }
:root[data-theme="book-dark"] .reader-top button.on { color: #000; }
:root[data-eink="1"] .reader-top button.on { background: #000; color: #fff; }
.gram .sent.has-g { background: var(--accent-bg); border-radius: 3px; box-decoration-break: clone; -webkit-box-decoration-break: clone; }
.gram .tok.g-anchor { outline: 1px dashed var(--accent); outline-offset: 1px; border-radius: 2px; }
:root[data-eink="1"] .gram .sent.has-g { background: transparent; border-bottom: 1px solid #000; }
:root[data-eink="1"] .gram .tok.g-anchor { outline: 1px solid #000; }
.g-head { font-weight: 700; }
.g-sentence { font-size: 18px; line-height: 1.6; margin: 8px 0 12px; }
.g-sentence mark { background: var(--accent-bg); color: var(--fg); border-bottom: 2px solid var(--accent); padding: 0 1px; }
.g-item { border-top: 1px solid var(--line); padding: 10px 0 2px; }
.g-item:first-of-type { border-top: 0; }
.g-name { font-weight: 600; margin-bottom: 4px; }
.g-formula { color: var(--accent); font-size: 15px; margin-bottom: 4px; }
.g-note { font-size: 15px; margin-bottom: 6px; }
.g-example { font-size: 15px; }
.g-level { font-size: 13px; margin-top: 4px; }

/* ---------- Статистика ---------- */
.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.stat { background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.stat .num { font-size: 28px; font-weight: 700; }
.stat .lbl { color: var(--fg-2); font-size: 13px; }
.level-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.level-row .name { width: 70px; font-weight: 600; }
.level-row .bar { flex: 1; height: 8px; background: var(--line); border-radius: 4px; overflow: hidden; }
.level-row .bar > i { display: block; height: 100%; background: var(--ok); }
:root[data-eink="1"] .level-row .bar > i { background: #000; }
.level-row .val { width: 110px; text-align: right; font-size: 14px; color: var(--fg-2); }

/* ---------- Настройки ---------- */
.setting { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.setting .lbl { flex: 1; }
.setting .lbl small { display: block; color: var(--fg-2); font-size: 13px; }
.setting select, .setting input[type=number] { min-width: 110px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.seg button { border: 0; border-radius: 0; min-height: 40px; padding: 4px 12px; background: transparent; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent); color: #fff; }
:root[data-eink="1"] .seg button.on { background: #000; }
.switch { width: 52px; height: 30px; border-radius: 15px; background: var(--line); position: relative; border: 0; min-height: 30px; padding: 0; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 12px; background: #fff; transition: left .15s; }
.switch.on { background: var(--accent); }
.switch.on::after { left: 25px; }
:root[data-eink="1"] .switch { border: 2px solid #000; background: #fff; }
:root[data-eink="1"] .switch::after { background: #000; }
:root[data-eink="1"] .switch.on { background: #fff; }

/* ---------- e-ink специфика ---------- */
:root[data-eink="1"] body { font-size: 20px; }
:root[data-eink="1"] .reader-body { font-size: max(var(--hanzi-size), 32px); line-height: 1.7; }
:root[data-eink="1"] .text-card, :root[data-eink="1"] .review-card, :root[data-eink="1"] .stat { border: 2px solid #000; }
:root[data-eink="1"] .popup .word { font-size: 44px; }
:root[data-eink="1"] .tabbar { border-top: 2px solid #000; }

@media (min-width: 700px) {
  .stat-grid { grid-template-columns: repeat(4, 1fr); }
  .popup { left: 50%; right: auto; width: 640px; transform: translateX(-50%); border-radius: 16px 16px 0 0; }
}
