:root {
  --bg: #ffffff;
  --bg-soft: #f6f6f4;
  --fg: #1a1a1a;
  --fg-soft: #555;
  --accent: #b08a3e;
  --border: #e5e3df;
  --hi: #fff5d4;
  --header-h: 52px;
  --nav-h: 60px;
  --max-w: 720px;
  --font-serif: Georgia, "Times New Roman", "PT Serif", serif;
  --font-sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", Inter, Roboto, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, monospace;
  --font-body: var(--font-serif);
  --font-ui: var(--font-sans);
  --size-body: 18px;
  --safe-l: env(safe-area-inset-left);
  --safe-r: env(safe-area-inset-right);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15161a;
    --bg-soft: #1d1f24;
    --fg: #ececec;
    --fg-soft: #a8a8a8;
    --accent: #d4a04c;
    --border: #2a2c32;
    --hi: #4a3b14;
  }
}

body[data-font="serif"] { --font-body: var(--font-serif); }
body[data-font="sans"]  { --font-body: var(--font-sans); }
body[data-font="mono"]  { --font-body: var(--font-mono); }

body[data-size="s"]  { --size-body: 16px; }
body[data-size="m"]  { --size-body: 18px; }
body[data-size="l"]  { --size-body: 20px; }
body[data-size="xl"] { --size-body: 22px; }

* { box-sizing: border-box; }
*:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
html, body { margin: 0; padding: 0; overflow-x: hidden; }
html {
  background: var(--bg); color: var(--fg);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
body {
  font-family: var(--font-body);
  font-size: var(--size-body);
  line-height: 1.6;
  padding-top: calc(var(--header-h) + env(safe-area-inset-top));
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom));
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  overflow-wrap: break-word;
  word-wrap: break-word;
}
body[data-view="glossary"] .view-reader,
body[data-view="practice"] .view-reader,
body[data-view="glossary"] #navbar,
body[data-view="practice"] #navbar { display: none; }
body[data-view="reader"] .view-glossary,
body[data-view="reader"] .view-practice { display: none; }

/* ---------- top bar ---------- */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: calc(var(--header-h) + env(safe-area-inset-top));
  padding-top: env(safe-area-inset-top);
  padding-left: max(8px, var(--safe-l));
  padding-right: max(8px, var(--safe-r));
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center;
  gap: 6px;
  z-index: 10;
  font-family: var(--font-ui);
}
.topbar .title { flex: 1; min-width: 0; text-align: center; }
.title-main { font-size: 14px; font-weight: 600; line-height: 1.15; }
.title-sub  { font-size: 11px; color: var(--fg-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.2; margin-top: 2px; }

.iconbtn {
  background: none; border: 1px solid var(--border);
  color: var(--fg);
  width: 44px; height: 44px;
  border-radius: 10px;
  font-size: 18px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  font-family: var(--font-ui);
  -webkit-touch-callout: none;
  user-select: none;
}
.iconbtn:active { background: var(--bg-soft); }
.iconbtn.is-active { background: var(--accent); color: #fff; border-color: var(--accent); }
.lang-toggle { font-weight: 700; font-size: 13px; min-width: 44px; }

/* ---------- views ---------- */
.view { max-width: var(--max-w); margin: 0 auto; padding: 14px 16px 28px; }
.pane-inner { padding: 0; }
.hidden { display: none !important; }

/* reader page styles */
.page { font-family: var(--font-body); }
.page h1 { font-size: 1.45em; margin: 8px 0 14px; line-height: 1.25; font-family: var(--font-ui); font-weight: 700; }
.page h2 {
  font-size: 1em; margin: 28px 0 10px;
  font-family: var(--font-ui); font-weight: 600;
  letter-spacing: 0.02em; text-transform: uppercase;
  color: var(--accent);
}
.page h3 { font-size: 0.98em; margin: 20px 0 8px; font-family: var(--font-ui); }
.page h4, .page h5, .page h6 { font-size: 0.92em; margin: 16px 0 6px; font-family: var(--font-ui); }
.page p { margin: 10px 0; }
.page ul, .page ol { padding-left: 22px; margin: 10px 0; }
.page li { margin: 4px 0; }
.page em { font-style: italic; }
.page strong { font-weight: 700; }
.page hr { border: 0; border-top: 1px solid var(--border); margin: 24px 0; }
.page a { color: var(--accent); text-decoration: underline; }

/* tables: horizontal scroll on narrow screens */
.page table {
  width: 100%;
  border-collapse: collapse;
  margin: 14px 0;
  font-size: 0.9em;
  font-family: var(--font-ui);
  display: block;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  white-space: normal;
}
.page table tbody, .page table thead { display: table; width: 100%; }
.page th, .page td {
  border: 1px solid var(--border);
  padding: 8px 10px;
  text-align: left;
  vertical-align: top;
  min-width: 120px;
}
.page th { background: var(--bg-soft); font-weight: 600; }

/* code, pre — should not break layout */
.page pre, .page code {
  font-family: var(--font-mono);
  background: var(--bg-soft);
  border-radius: 6px;
  font-size: 0.88em;
}
.page code { padding: 1px 5px; }
.page pre { padding: 10px 12px; overflow-x: auto; -webkit-overflow-scrolling: touch; }

.page .ocr-warn {
  font-size: 0.78em; color: var(--fg-soft);
  background: var(--bg-soft);
  padding: 8px 10px; border-radius: 8px;
  margin: 12px 0; font-family: var(--font-ui);
}

.chapter-card {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px 18px;
  margin: 10px 0 22px;
  text-align: center;
  font-family: var(--font-ui);
}
.chapter-card .num {
  display: inline-block;
  background: var(--fg); color: var(--bg);
  width: 44px; height: 44px; line-height: 44px;
  border-radius: 10px;
  font-weight: 700; font-size: 22px;
  margin-bottom: 14px;
}
.chapter-card .title { font-size: 1.15em; font-weight: 700; }

.loading { text-align: center; color: var(--fg-soft); padding: 40px 0; }

/* ---------- bottom nav ---------- */
.navbar {
  position: fixed; left: 0; right: 0; bottom: 0;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: max(10px, var(--safe-l));
  padding-right: max(10px, var(--safe-r));
  background: var(--bg);
  border-top: 1px solid var(--border);
  display: flex; align-items: center; gap: 8px;
  z-index: 10;
  font-family: var(--font-ui);
}
.navbtn {
  flex: 0 0 auto;
  background: none; border: 1px solid var(--border);
  color: var(--fg);
  height: 44px; min-width: 44px; padding: 0 14px;
  border-radius: 10px;
  font-size: 14px; cursor: pointer;
  font-family: var(--font-ui);
  -webkit-touch-callout: none;
  user-select: none;
}
.navbtn:active { background: var(--bg-soft); }
.navbtn:disabled { opacity: 0.35; cursor: default; }
.pageinfo { flex: 1; text-align: center; font-size: 12px; color: var(--fg-soft); padding: 0 4px; }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0;
  background: var(--bg);
  z-index: 20;
  display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: var(--safe-l);
  padding-right: var(--safe-r);
}
.overlay-head {
  height: var(--header-h);
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 8px;
  padding: 0 8px;
  font-family: var(--font-ui);
}
.overlay-title { flex: 1; font-weight: 600; font-size: 14px; text-align: center; }

/* menu */
.menu-body { overflow-y: auto; flex: 1; font-family: var(--font-ui); padding-bottom: 24px; -webkit-overflow-scrolling: touch; }
.menu-section { padding: 4px 0; border-bottom: 1px solid var(--border); }
.menu-section:last-child { border-bottom: 0; }
.menu-section-title {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--fg-soft);
  padding: 14px 18px 6px;
}
.menu-list { list-style: none; padding: 0; margin: 0; }
.menu-item {
  padding: 14px 18px;
  min-height: 48px;
  display: flex; align-items: center; gap: 12px;
  cursor: pointer;
  font-size: 15px;
}
.menu-item:active { background: var(--bg-soft); }
.menu-item.is-active { background: var(--bg-soft); color: var(--accent); }
.menu-item .menu-icon { font-size: 18px; width: 24px; text-align: center; flex-shrink: 0; }
.menu-item .menu-label { flex: 1; min-width: 0; }
.menu-item .menu-meta { font-size: 11px; color: var(--fg-soft); flex-shrink: 0; margin-left: 8px; }
.menu-item .toc-num {
  display: inline-block; min-width: 28px;
  font-weight: 700; color: var(--accent); font-size: 13px;
  flex-shrink: 0;
}

/* settings */
.settings-body { overflow-y: auto; flex: 1; padding: 18px 16px; font-family: var(--font-ui); -webkit-overflow-scrolling: touch; }
.setting-block { margin: 18px 0; }
.setting-label { font-size: 13px; color: var(--fg-soft); margin-bottom: 8px; text-transform: uppercase; letter-spacing: 0.04em; }
.seg {
  display: flex; flex-wrap: wrap;
  border: 1px solid var(--border); border-radius: 10px; overflow: hidden;
}
.seg-btn {
  flex: 1 0 auto;
  background: none; border: 0; color: var(--fg);
  padding: 12px 14px; font-size: 14px; cursor: pointer;
  border-right: 1px solid var(--border);
  font-family: var(--font-ui);
  min-height: 44px;
  min-width: 60px;
}
.seg-btn:last-child { border-right: 0; }
.seg-btn.active { background: var(--accent); color: #fff; }
.seg-btn:active { background: var(--bg-soft); }
.seg-btn.active:active { background: var(--accent); }
.voice-select {
  background: var(--bg); color: var(--fg);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 12px; font-size: 16px; width: 100%;
  font-family: var(--font-ui);
  -webkit-appearance: none; appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--fg-soft) 50%), linear-gradient(135deg, var(--fg-soft) 50%, transparent 50%);
  background-position: calc(100% - 18px) 18px, calc(100% - 13px) 18px;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 36px;
}

/* glossary */
.search-input {
  width: 100%; padding: 12px 14px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--bg-soft); color: var(--fg);
  font-size: 16px; font-family: var(--font-ui);
  margin-bottom: 16px;
  -webkit-appearance: none;
}
.gloss-item {
  border-bottom: 1px solid var(--border);
  padding: 14px 0;
}
.gloss-item:last-child { border-bottom: 0; }
.gloss-term {
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 8px;
  font-family: var(--font-ui);
}
.gloss-term .gloss-en { font-weight: 700; font-size: 1.05em; }
.gloss-term .gloss-uk { color: var(--accent); font-weight: 600; }
.gloss-term .gloss-audio { margin-left: auto; }
.gloss-def { margin-top: 6px; color: var(--fg-soft); font-size: 0.95em; }
.gloss-meta { margin-top: 6px; font-size: 11px; color: var(--fg-soft); font-family: var(--font-ui); }
.gloss-meta a { color: var(--accent); text-decoration: none; }

/* practice */
.practice-tabs { display: flex; gap: 8px; margin-bottom: 12px; font-family: var(--font-ui); }
.practice-tabs .tab {
  flex: 1 1 0; padding: 12px 10px;
  background: var(--bg-soft); border: 1px solid var(--border);
  border-radius: 10px; cursor: pointer; font-size: 13px;
  color: var(--fg); font-family: var(--font-ui);
  min-height: 44px;
}
.practice-tabs .tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.practice-filter { margin-bottom: 14px; font-family: var(--font-ui); }
.practice-filter select {
  background: var(--bg); color: var(--fg);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; font-size: 16px; width: 100%;
  font-family: var(--font-ui); min-height: 44px;
  -webkit-appearance: none;
}
.q-item {
  border: 1px solid var(--border); border-radius: 12px;
  padding: 14px 16px; margin-bottom: 14px;
  background: var(--bg);
}
.q-head {
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 8px;
  font-family: var(--font-ui);
  font-size: 13px; color: var(--fg-soft); margin-bottom: 8px;
}
.q-head .q-id { font-weight: 700; color: var(--accent); }
.q-head .q-page { margin-left: auto; }
.q-body p { margin: 6px 0; }
.q-body ul { padding-left: 20px; }
.q-options { list-style: none; padding: 0; margin: 10px 0; }
.q-options li { padding: 6px 0; }
.q-actions {
  margin-top: 12px; display: flex; flex-wrap: wrap; gap: 8px;
  font-family: var(--font-ui);
}
.q-btn {
  background: var(--bg-soft); border: 1px solid var(--border);
  color: var(--fg); padding: 10px 14px; border-radius: 8px;
  font-size: 13px; cursor: pointer;
  font-family: var(--font-ui);
  min-height: 40px;
}
.q-btn:active { background: var(--accent); color: #fff; }
.q-answer {
  margin-top: 12px; padding: 12px 14px;
  background: var(--hi); border-radius: 10px;
  font-size: 0.95em;
}
.q-answer .a-label {
  display: block; font-family: var(--font-ui);
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--accent); font-weight: 700; margin-bottom: 6px;
}

/* audio mini bar */
.audio-bar {
  position: fixed;
  left: max(12px, var(--safe-l));
  right: max(12px, var(--safe-r));
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 10px);
  height: 50px;
  background: var(--fg); color: var(--bg);
  border-radius: 25px;
  display: flex; align-items: center; gap: 8px;
  padding: 0 10px;
  z-index: 15;
  font-family: var(--font-ui); font-size: 13px;
  box-shadow: 0 6px 24px rgba(0,0,0,0.25);
  max-width: 460px; margin: 0 auto;
}
.audio-bar .iconbtn {
  background: transparent; color: var(--bg);
  border-color: rgba(255,255,255,0.25);
  width: 38px; height: 38px; font-size: 14px;
}
.audio-status { flex: 1; opacity: 0.85; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
body[data-view="glossary"] .audio-bar,
body[data-view="practice"] .audio-bar {
  bottom: calc(env(safe-area-inset-bottom) + 16px);
}

/* swipe hint */
.view-reader { touch-action: pan-y; }

/* continuous chapter — page anchors */
.page-section { padding-top: 8px; padding-bottom: 4px; scroll-margin-top: calc(var(--header-h) + 8px); }
.page-marker {
  display: inline-block;
  font-family: var(--font-ui);
  font-size: 10px; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--fg-soft);
  margin: 18px 0 4px;
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  opacity: 0.55;
}
/* ---------- boxed callouts (mirror printed shaded blocks) ---------- */
.callout {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  margin: 18px 0;
  background: var(--bg-soft);
  position: relative;
}
.callout > h2:first-child,
.callout > h3:first-child {
  margin-top: 0;
  margin-bottom: 10px;
}
.callout > *:last-child { margin-bottom: 0; }

/* h2-level chapter callouts (top-of-chapter framing) */
.callout-must-know   { background: var(--bg-soft); border-left: 4px solid var(--accent); }
.callout-sqe-advice  { background: var(--bg-soft); border-left: 4px solid #6593c8; }
.callout-pre-quiz    { background: var(--bg-soft); border-left: 4px solid #9b6fbc; }
.callout-checklist   { background: var(--bg-soft); border-left: 4px solid #6ba56b; }
.callout-key-terms   { background: var(--bg-soft); border-left: 4px solid #c89a4b; }
.callout-sqe1        { background: var(--bg-soft); border-left: 4px solid #d97a3a; }
.callout-answers     { background: var(--bg-soft); border-left: 4px solid #6ba56b; border-style: dashed; }
.callout-key-refs    { background: var(--bg-soft); border-left: 4px solid var(--fg-soft); }

/* h3-level inline callouts */
.callout-practice {
  background: var(--bg);
  border: 1px solid var(--accent);
  border-radius: 10px;
}
.callout-practice > h3:first-child {
  color: var(--accent);
  font-weight: 700;
  font-family: var(--font-ui);
  text-transform: none;
  letter-spacing: 0;
}
.callout-key-term {
  background: var(--bg);
  border: 1px dashed #c89a4b;
  border-radius: 10px;
}
.callout-key-term > h3:first-child {
  font-family: var(--font-ui);
  font-weight: 600;
  color: #b07a20;
}
.callout-tip {
  background: var(--bg);
  border: 1px dotted #6593c8;
  border-radius: 10px;
  font-size: 0.95em;
}
.callout-tip > h3:first-child {
  color: #4a78b0;
  font-family: var(--font-ui);
  font-weight: 600;
}
.callout-summary {
  background: var(--bg);
  border: 2px solid #6ba56b;
  border-radius: 10px;
}
.callout-summary > h3:first-child {
  color: #4d7a4d;
  font-family: var(--font-ui);
  font-weight: 700;
}

@media (prefers-color-scheme: dark) {
  .callout-practice { border-color: var(--accent); }
  .callout-key-term { border-color: #d4a05c; }
  .callout-tip      { border-color: #80a8d8; }
  .callout-summary  { border-color: #7fbd7f; }
  .callout-key-term > h3:first-child { color: #d4a05c; }
  .callout-tip > h3:first-child      { color: #80a8d8; }
  .callout-summary > h3:first-child  { color: #7fbd7f; }
}

/* tap-translation hint on .translatable */
.translatable { cursor: pointer; transition: background 0.15s; }
.translatable:active { background: var(--hi); border-radius: 6px; }

/* translation popover internals */
.trans-block {
  border-top: 1px solid var(--border);
  padding: 10px 0;
}
.trans-block:first-child { border-top: 0; padding-top: 0; }
.trans-block .trans-lang {
  font-size: 10px; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--accent); font-weight: 700; margin-bottom: 6px;
}
.trans-block .trans-text { font-size: 14px; line-height: 1.5; color: var(--fg); margin-bottom: 8px; }
.trans-block .trans-text p { margin: 4px 0; }
.trans-block .trans-listen { font-size: 12px; padding: 6px 10px; min-height: 32px; }

/* gloss-tip needs to allow tall content */
.gloss-tip-def { max-height: 60vh; overflow-y: auto; -webkit-overflow-scrolling: touch; }

/* cross-references in body text */
.xref {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px dashed currentColor;
  cursor: pointer;
  -webkit-tap-highlight-color: rgba(176,138,62,0.2);
}
.xref:active { background: var(--hi); border-radius: 3px; }
.glossterm {
  border-bottom: 1px dotted var(--fg-soft);
  cursor: help;
  -webkit-tap-highlight-color: rgba(176,138,62,0.15);
}
.glossterm:active { background: var(--hi); border-radius: 3px; }
.xref-target { scroll-margin-top: calc(var(--header-h) + 12px); }
.xref-target:target { outline: 2px solid var(--accent); outline-offset: 4px; }

/* glossary tip popover */
.gloss-tip {
  position: fixed;
  left: max(12px, var(--safe-l));
  right: max(12px, var(--safe-r));
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 8px);
  max-width: 460px; margin: 0 auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 16px 14px 16px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.25);
  z-index: 30;
  font-family: var(--font-ui);
}
body[data-view="glossary"] .gloss-tip,
body[data-view="practice"] .gloss-tip { bottom: calc(env(safe-area-inset-bottom) + 12px); }
.gloss-tip-close {
  position: absolute; top: 6px; right: 6px;
  width: 32px; height: 32px; font-size: 14px;
  border-color: transparent;
}
.gloss-tip-term { font-size: 16px; margin-bottom: 6px; padding-right: 32px; }
.gloss-tip-term .gloss-en { font-weight: 700; }
.gloss-tip-term .gloss-uk { color: var(--accent); font-weight: 600; }
.gloss-tip-def { font-size: 14px; color: var(--fg-soft); margin-bottom: 10px; line-height: 1.5; }
.gloss-tip-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.chapter-footer { margin: 40px 0 20px; text-align: center; }
.studied-btn {
  background: var(--accent); color: #fff;
  border: 0; padding: 14px 22px;
  border-radius: 999px;
  font-family: var(--font-ui); font-size: 15px; font-weight: 600;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(176,138,62,0.25);
  min-height: 48px;
  max-width: 100%;
  overflow-wrap: break-word;
}
.studied-btn:active { transform: translateY(1px); }
.studied-btn.is-done {
  background: #4caf50;
  box-shadow: 0 2px 6px rgba(76,175,80,0.25);
}

/* ---------- narrow phone tweaks ---------- */
@media (max-width: 380px) {
  .view { padding: 12px 12px 24px; }
  .title-main { font-size: 13px; }
  .title-sub  { font-size: 10px; }
  .iconbtn { width: 42px; height: 42px; font-size: 17px; }
  .navbtn { padding: 0 10px; font-size: 13px; }
  .chapter-card { padding: 20px 14px; }
  .chapter-card .title { font-size: 1.05em; }
  .q-item { padding: 12px 12px; }
}

/* large screens: keep comfortable max-width */
@media (min-width: 1024px) {
  .view { padding: 24px 24px 40px; }
}
