/* ===========================
   HANOK · A quiet reader
   =========================== */

:root {
  /* Paper theme (default) */
  --bg: #f4ede0;
  --bg-soft: #ebe2d2;
  --paper: #faf5ea;
  --ink: #2a2018;
  --ink-soft: #5a4a3a;
  --ink-faint: #8a7860;
  --rule: #d9cdb8;
  --accent: #8b3a2f;
  --accent-soft: #c4685c;
  --shadow: 0 1px 2px rgba(60, 40, 20, 0.06), 0 8px 24px rgba(60, 40, 20, 0.08);
  --shadow-lg: 0 4px 12px rgba(60, 40, 20, 0.08), 0 20px 60px rgba(60, 40, 20, 0.15);

  --font-serif: 'Cormorant Garamond', 'Noto Serif KR', Georgia, serif;
  --font-korean: 'Noto Serif KR', 'Apple SD Gothic Neo', serif;
  --font-mono: 'JetBrains Mono', ui-monospace, monospace;

  --reader-size: 17px;
  --reader-line: 1.9;

  --safe-top: env(safe-area-inset-top, 0);
  --safe-bottom: env(safe-area-inset-bottom, 0);
}

[data-theme="sepia"] {
  --bg: #e8d9b8;
  --bg-soft: #ddcca5;
  --paper: #ede0bf;
  --ink: #3a2818;
  --ink-soft: #6a4a30;
  --ink-faint: #9a7850;
  --rule: #c8b58c;
  --accent: #7a3320;
  --accent-soft: #b05a3c;
}

[data-theme="night"] {
  --bg: #1a1410;
  --bg-soft: #221a14;
  --paper: #1f1814;
  --ink: #e6dac8;
  --ink-soft: #b8a890;
  --ink-faint: #786858;
  --rule: #3a2e22;
  --accent: #d4856a;
  --accent-soft: #b8654a;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 4px 12px rgba(0, 0, 0, 0.4), 0 20px 60px rgba(0, 0, 0, 0.6);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

/* CRITICAL: ensure [hidden] always wins over our display:flex rules on .sheet, .popup, .view */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-serif);
  font-feature-settings: "kern", "liga";
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background-image:
    radial-gradient(ellipse at top, rgba(255, 230, 180, 0.15), transparent 60%),
    radial-gradient(ellipse at bottom right, rgba(180, 100, 60, 0.06), transparent 70%);
}

[data-theme="night"] body {
  background-image:
    radial-gradient(ellipse at top, rgba(80, 60, 40, 0.3), transparent 60%),
    radial-gradient(ellipse at bottom right, rgba(180, 100, 60, 0.05), transparent 70%);
}

button { background: none; border: none; color: inherit; font: inherit; cursor: pointer; }

.view {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

.view.active { display: flex; }

/* ===========================
   LIBRARY
   =========================== */

.library-header {
  padding: 28px 24px 16px;
  border-bottom: 1px solid var(--rule);
  background: var(--paper);
  position: relative;
}

.library-header::after {
  content: '';
  position: absolute;
  left: 24px;
  right: 24px;
  bottom: -2px;
  height: 1px;
  background: var(--ink-faint);
  opacity: 0.3;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand-mark {
  font-family: var(--font-korean);
  font-size: 36px;
  font-weight: 500;
  color: var(--accent);
  width: 56px;
  height: 56px;
  display: grid;
  place-items: center;
  border: 1.5px solid var(--accent);
  border-radius: 4px;
  letter-spacing: 0;
  line-height: 1;
}

.brand-text h1 {
  font-family: var(--font-serif);
  font-size: 30px;
  font-weight: 500;
  letter-spacing: 0.02em;
  line-height: 1;
}

.brand-text p {
  font-family: var(--font-korean);
  font-size: 12px;
  font-weight: 300;
  color: var(--ink-soft);
  font-style: italic;
  letter-spacing: 0.04em;
  margin-top: 4px;
}

.library-content {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0 100px;
  -webkit-overflow-scrolling: touch;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 80px 32px;
  text-align: center;
  color: var(--ink-soft);
}

.empty-illustration {
  width: 90px;
  height: 90px;
  margin-bottom: 28px;
  color: var(--accent);
  opacity: 0.7;
}

.empty-state h2 {
  font-family: var(--font-serif);
  font-weight: 500;
  font-size: 24px;
  letter-spacing: 0.02em;
  margin-bottom: 12px;
  color: var(--ink);
}

.empty-state p {
  font-size: 14px;
  line-height: 1.7;
  font-family: var(--font-serif);
}

.empty-state em {
  font-family: var(--font-mono);
  font-size: 12px;
  font-style: normal;
  background: var(--bg-soft);
  padding: 1px 6px;
  border-radius: 3px;
  color: var(--accent);
}

.library-list {
  list-style: none;
  padding: 16px 16px 0;
}

.library-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px;
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 6px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  position: relative;
  overflow: hidden;
}

.library-item:active {
  transform: scale(0.98);
}

.library-item::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: var(--accent);
}

.book-cover {
  width: 52px;
  height: 70px;
  background: linear-gradient(135deg, var(--accent), var(--accent-soft));
  border-radius: 2px;
  display: grid;
  place-items: center;
  font-family: var(--font-korean);
  font-size: 22px;
  font-weight: 500;
  color: var(--paper);
  flex-shrink: 0;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
  position: relative;
}

.book-cover::after {
  content: '';
  position: absolute;
  left: 4px;
  top: 0;
  bottom: 0;
  width: 1px;
  background: rgba(0,0,0,0.2);
}

.book-info {
  flex: 1;
  min-width: 0;
}

.book-title {
  font-family: var(--font-serif);
  font-size: 17px;
  font-weight: 500;
  line-height: 1.3;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-meta {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-faint);
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.book-delete {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: var(--ink-faint);
  opacity: 0.5;
  transition: opacity 0.15s, background 0.15s;
}

.book-delete:active {
  opacity: 1;
  background: var(--bg-soft);
  color: var(--accent);
}

.fab-stack {
  position: absolute;
  bottom: calc(20px + var(--safe-bottom));
  right: 24px;
  z-index: 10;
}

.fab {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 22px;
  background: var(--ink);
  color: var(--paper);
  border-radius: 32px;
  font-family: var(--font-serif);
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 0.02em;
  box-shadow: var(--shadow-lg);
  cursor: pointer;
  transition: transform 0.15s;
}

.fab:active { transform: scale(0.95); }

.fab svg {
  width: 18px;
  height: 18px;
}

[data-theme="night"] .fab {
  background: var(--accent);
  color: var(--paper);
}

/* ===========================
   READER
   =========================== */

.reader-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--paper);
  border-bottom: 1px solid var(--rule);
  z-index: 5;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: var(--ink-soft);
  transition: background 0.15s;
  flex-shrink: 0;
}

.icon-btn:active { background: var(--bg-soft); }

.icon-btn svg { width: 22px; height: 22px; }

.reader-title {
  flex: 1;
  text-align: center;
  min-width: 0;
}

.reader-title p {
  font-family: var(--font-serif);
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.reader-meta {
  font-family: var(--font-mono) !important;
  font-size: 10px !important;
  font-weight: 400 !important;
  color: var(--ink-faint);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  margin-top: 2px;
}

.reader-content {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 32px 24px 40px;
  background: var(--paper);
  position: relative;
}

#reader-text {
  max-width: 640px;
  margin: 0 auto;
  font-family: var(--font-korean);
  font-size: var(--reader-size);
  line-height: var(--reader-line);
  color: var(--ink);
  font-weight: 400;
  word-break: keep-all;
  /* selection style — gold ink underline */
}

#reader-text ::selection {
  background: rgba(196, 104, 92, 0.25);
  color: var(--ink);
}

#reader-text h1, #reader-text h2 {
  font-family: var(--font-korean);
  font-weight: 700;
  font-size: 1.4em;
  margin: 1.8em 0 0.8em;
  letter-spacing: -0.01em;
  color: var(--accent);
  text-align: center;
  position: relative;
  padding-bottom: 0.6em;
}

#reader-text h1::after, #reader-text h2::after {
  content: '◆';
  position: absolute;
  bottom: -0.2em;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.5em;
  color: var(--ink-faint);
}

#reader-text p {
  margin-bottom: 1.1em;
  text-indent: 1.4em;
}

#reader-text p:first-of-type { text-indent: 0; }

/* Each Korean word is wrapped in .word for tap detection */
#reader-text .word {
  cursor: pointer;
  position: relative;
  transition: background 0.1s, color 0.1s;
  border-radius: 2px;
  padding: 0 0.5px;
}

#reader-text .word.active {
  background: rgba(139, 58, 47, 0.18);
  color: var(--accent);
}

[data-theme="night"] #reader-text .word.active {
  background: rgba(212, 133, 106, 0.25);
}

.reader-footer {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px calc(12px + var(--safe-bottom));
  background: var(--paper);
  border-top: 1px solid var(--rule);
}

.chapter-nav {
  font-family: var(--font-serif);
  font-size: 14px;
  color: var(--ink-soft);
  padding: 8px 12px;
  font-style: italic;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.chapter-nav:disabled { opacity: 0.3; }
.chapter-nav:not(:disabled):active { color: var(--accent); }

.progress-bar {
  flex: 1;
  height: 2px;
  background: var(--rule);
  border-radius: 1px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  width: 0;
  transition: width 0.3s ease;
}

/* ===========================
   POPUP (translation card)
   =========================== */

.popup {
  position: fixed;
  z-index: 100;
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 10px;
  box-shadow: var(--shadow-lg);
  max-width: min(360px, calc(100vw - 24px));
  min-width: 260px;
  max-height: 60vh;
  overflow-y: auto;
  animation: popupIn 0.18s cubic-bezier(0.2, 0.9, 0.3, 1.1);
  transform-origin: top center;
}

@keyframes popupIn {
  from { opacity: 0; transform: translateY(-4px) scale(0.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.popup.below { transform-origin: bottom center; }

.popup-arrow {
  position: absolute;
  top: -7px;
  left: 50%;
  width: 12px;
  height: 12px;
  background: var(--paper);
  border-top: 1px solid var(--rule);
  border-left: 1px solid var(--rule);
  transform: translateX(-50%) rotate(45deg);
}

.popup.below .popup-arrow {
  top: auto;
  bottom: -7px;
  border-top: none;
  border-left: none;
  border-bottom: 1px solid var(--rule);
  border-right: 1px solid var(--rule);
}

.popup-content {
  padding: 12px 14px 14px;
  position: relative;
}

.popup-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.popup-mode {
  font-family: var(--font-mono);
  font-size: 9px;
  font-weight: 500;
  color: var(--accent);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  background: var(--bg-soft);
  padding: 3px 8px;
  border-radius: 3px;
}

.popup-close {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 18px;
  line-height: 1;
  color: var(--ink-faint);
}

.popup-close:active { background: var(--bg-soft); }

.popup-original {
  font-family: var(--font-korean);
  font-size: 17px;
  font-weight: 500;
  line-height: 1.4;
  color: var(--ink);
  margin-bottom: 6px;
  padding-bottom: 8px;
  border-bottom: 1px dashed var(--rule);
  word-break: keep-all;
}

.popup-translation {
  font-family: var(--font-serif);
  font-size: 15px;
  line-height: 1.5;
  color: var(--ink);
  font-weight: 400;
  min-height: 22px;
}

/* ===== Dictionary (word mode) ===== */

.popup-translation .dict-quick {
  font-family: var(--font-serif);
  font-size: 16px;
  font-style: italic;
  color: var(--accent);
  font-weight: 500;
  margin-bottom: 8px;
  padding-bottom: 8px;
  border-bottom: 1px dashed var(--rule);
}

.popup-translation .dict-base {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 10px;
  padding: 6px 10px;
  background: var(--bg-soft);
  border-radius: 4px;
}

.popup-translation .dict-base .label {
  font-family: var(--font-mono);
  font-size: 9px;
  color: var(--accent);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  flex-shrink: 0;
}

.popup-translation .dict-base-word {
  font-family: var(--font-korean);
  font-size: 16px;
  font-weight: 500;
  color: var(--ink);
  word-break: keep-all;
}

/* Morphological breakdown — shows "학교 + 에서 (from)" decomposition */
.popup-translation .dict-breakdown {
  margin-bottom: 10px;
  padding: 8px 10px;
  background: rgba(139, 58, 47, 0.04);
  border-left: 2px solid var(--accent);
  border-radius: 4px;
}

.popup-translation .dict-breakdown .label {
  display: block;
  font-family: var(--font-mono);
  font-size: 9px;
  color: var(--accent);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  margin-bottom: 4px;
}

.popup-translation .breakdown-chain {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 6px 10px;
}

.popup-translation .breakdown-head {
  font-family: var(--font-korean);
  font-size: 14px;
  font-weight: 500;
  color: var(--ink);
}

.popup-translation .breakdown-piece {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  font-size: 13px;
}

.popup-translation .piece-text {
  font-family: var(--font-korean);
  color: var(--ink-soft);
}

.popup-translation .piece-meaning {
  font-family: var(--font-serif);
  font-size: 11px;
  font-style: italic;
  color: var(--ink-faint);
}

.popup-translation .grammar-offline-note {
  background: rgba(180, 130, 30, 0.08);
  padding: 8px 12px;
  border-radius: 4px;
  color: var(--ink-soft);
  font-size: 13px;
  margin-bottom: 8px;
}

.popup-translation .grammar-offline-note em {
  font-style: italic;
}

.popup-translation .grammar-offline-note .dict-note {
  margin-top: 6px;
  font-size: 12px;
  color: var(--ink-faint);
}

.popup-translation .dict-entry {
  margin-bottom: 10px;
}

.popup-translation .dict-entry:last-child {
  margin-bottom: 0;
}

.popup-translation .dict-pos {
  font-family: var(--font-mono);
  font-size: 9px;
  color: var(--accent);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  margin-bottom: 4px;
  font-style: normal;
}

.popup-translation .dict-meanings {
  list-style: none;
  padding: 0;
  margin: 0;
}

.popup-translation .dict-meanings li {
  padding: 4px 0;
  border-bottom: 1px solid rgba(217, 205, 184, 0.4);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.popup-translation .dict-meanings li:last-child {
  border-bottom: none;
}

.popup-translation .dict-meaning {
  font-family: var(--font-serif);
  font-size: 14px;
  color: var(--ink);
  font-weight: 500;
  font-style: normal;
}

.popup-translation .dict-synonyms {
  font-family: var(--font-korean);
  font-size: 11px;
  color: var(--ink-soft);
  font-style: italic;
  word-break: keep-all;
}

.popup-translation .dict-note {
  font-family: var(--font-serif);
  font-size: 12px;
  font-style: italic;
  color: var(--ink-faint);
  margin-top: 6px;
}

/* ===== Sentence mode ===== */

.popup-translation .sentence-translation {
  font-family: var(--font-serif);
  font-size: 15px;
  line-height: 1.55;
  color: var(--ink);
  font-style: italic;
}

/* ===== Grammar mode ===== */

.popup-translation .grammar-translation {
  font-family: var(--font-serif);
  font-size: 14px;
  line-height: 1.5;
  color: var(--ink-soft);
  font-style: italic;
  margin-bottom: 10px;
  padding-bottom: 10px;
  border-bottom: 1px dashed var(--rule);
}

.popup-translation .grammar-block {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.popup-translation .grammar-block.grammar-empty {
  font-style: italic;
  color: var(--ink-soft);
  font-size: 13px;
  line-height: 1.5;
}

.popup-translation .grammar-item {
  padding: 8px 10px;
  background: var(--bg-soft);
  border-left: 2px solid var(--accent);
  border-radius: 0 4px 4px 0;
}

.popup-translation .grammar-name {
  font-family: var(--font-korean);
  font-size: 13px;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 4px;
  letter-spacing: -0.01em;
}

.popup-translation .grammar-meaning {
  font-family: var(--font-serif);
  font-size: 13px;
  line-height: 1.5;
  color: var(--ink);
  font-style: normal;
  margin-bottom: 4px;
}

.popup-translation .grammar-example {
  font-family: var(--font-korean);
  font-size: 11px;
  color: var(--ink-soft);
  font-style: italic;
  word-break: keep-all;
}

.popup-translation .label {
  font-family: var(--font-mono);
  font-size: 9px;
  color: var(--accent);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  margin-right: 4px;
}

.popup-romanization {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-faint);
  margin-top: 4px;
  letter-spacing: 0.02em;
}

.popup-loading {
  display: flex;
  gap: 5px;
  padding: 4px 0;
}

.popup-loading .dot {
  width: 6px;
  height: 6px;
  background: var(--accent);
  border-radius: 50%;
  animation: dotPulse 1.2s infinite ease-in-out;
}

.popup-loading .dot:nth-child(2) { animation-delay: 0.15s; }
.popup-loading .dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes dotPulse {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1); }
}

.popup-actions {
  display: flex;
  gap: 4px;
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--rule);
}

.popup-action {
  flex: 1;
  padding: 7px 4px;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-faint);
  border-radius: 4px;
  font-weight: 500;
  transition: background 0.12s, color 0.12s;
}

.popup-action.active {
  background: var(--ink);
  color: var(--paper);
}

[data-theme="night"] .popup-action.active {
  background: var(--accent);
}

.popup-action:not(.active):active { background: var(--bg-soft); }

/* ===========================
   SHEETS (toc, settings)
   =========================== */

/* CRITICAL: [hidden] must override display rules */
[hidden] { display: none !important; }

.sheet {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(20, 14, 8, 0.5);
  animation: fadeIn 0.2s ease;
  cursor: pointer;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.sheet-content {
  position: relative;
  background: var(--paper);
  border-top-left-radius: 18px;
  border-top-right-radius: 18px;
  padding: 12px 24px calc(24px + var(--safe-bottom));
  /* Constrain sheet to no more than 85% of viewport so a peek of background
     is always visible above. dvh handles mobile address bar; vh is fallback.
     The sheet is portaled to <body> in JS (see openSheet) so it can't be
     visually constrained by any ancestor's padding-bottom (e.g. .view's
     reservation for the bottom nav). */
  max-height: 85vh;
  max-height: 85dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  animation: sheetUp 0.25s cubic-bezier(0.2, 0.9, 0.3, 1);
}

@keyframes sheetUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.sheet-handle {
  width: 36px;
  height: 4px;
  background: var(--ink-faint);
  opacity: 0.3;
  border-radius: 2px;
  margin: 0 auto 18px;
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}

.sheet-content h3 {
  font-family: var(--font-serif);
  font-size: 22px;
  font-weight: 500;
  letter-spacing: 0.02em;
  margin: 0;
}

.sheet-close {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 24px;
  line-height: 1;
  color: var(--ink-soft);
  background: var(--bg-soft);
  transition: background 0.15s;
  flex-shrink: 0;
}

.sheet-close:active {
  background: var(--rule);
  color: var(--accent);
}

.sheet-done {
  display: block;
  width: 100%;
  margin-top: 20px;
  padding: 14px;
  background: var(--ink);
  color: var(--paper);
  border-radius: 8px;
  font-family: var(--font-serif);
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 0.02em;
  transition: transform 0.15s;
}

.sheet-done:active {
  transform: scale(0.98);
}

[data-theme="night"] .sheet-done {
  background: var(--accent);
}

.toc-list { list-style: none; }

.toc-item {
  padding: 14px 4px;
  border-bottom: 1px solid var(--rule);
  font-family: var(--font-korean);
  font-size: 15px;
  font-weight: 400;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 12px;
}

.toc-item:active { background: var(--bg-soft); }

.toc-item.current {
  color: var(--accent);
  font-weight: 500;
}

.toc-num {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--ink-faint);
  letter-spacing: 0.08em;
  min-width: 32px;
}

.toc-item.current .toc-num { color: var(--accent); }

/* Settings */
.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 0;
  border-bottom: 1px solid var(--rule);
  gap: 16px;
}

.setting-row.column {
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}

.setting-row label {
  font-family: var(--font-serif);
  font-size: 15px;
  font-weight: 500;
  color: var(--ink);
}

.setting-hint {
  font-family: var(--font-serif);
  font-size: 12px;
  font-style: italic;
  color: var(--ink-soft);
  line-height: 1.5;
}

.size-controls, .theme-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.size-btn, .theme-btn {
  padding: 6px 12px;
  border: 1px solid var(--rule);
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.04em;
  color: var(--ink-soft);
  background: var(--bg-soft);
  transition: all 0.15s;
}

.size-btn:active, .theme-btn:active {
  background: var(--ink);
  color: var(--paper);
}

.theme-btn.active {
  background: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
}

#size-display, #spacing-display {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-soft);
  min-width: 36px;
  text-align: center;
}

.setting-divider {
  height: 1px;
  background: var(--rule);
  margin: 8px 0;
}

/* ===========================
   Translation mode buttons
   =========================== */
.mode-controls {
  display: flex;
  gap: 8px;
  width: 100%;
  margin-top: 8px;
}
.mode-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 8px;
  border: 1px solid var(--rule);
  border-radius: 6px;
  background: var(--bg-soft);
  color: var(--ink-soft);
  cursor: pointer;
  transition: all 0.15s;
  text-align: center;
  min-width: 0;
}
.mode-btn .mode-title {
  font-family: var(--font-serif);
  font-size: 13px;
  font-weight: 500;
}
.mode-btn .mode-sub {
  font-family: var(--font-mono);
  font-size: 9px;
  letter-spacing: 0.04em;
  color: var(--ink-faint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.mode-btn.active {
  background: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
}
.mode-btn.active .mode-sub {
  color: rgba(250, 245, 234, 0.6);
}

.dict-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  flex-wrap: wrap;
}
.dict-action-btn {
  padding: 6px 12px;
  border: 1px solid var(--rule);
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
  background: var(--bg-soft);
  cursor: pointer;
  transition: all 0.15s;
}
.dict-action-btn:active {
  background: var(--ink);
  color: var(--paper);
}

.dict-coverage-info {
  margin-top: 10px;
  padding: 12px;
  background: var(--bg);
  border: 1px solid var(--rule);
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-soft);
  line-height: 1.8;
}
.dict-coverage-info div {
  display: flex;
  justify-content: space-between;
}
.dict-coverage-info b {
  color: var(--ink);
}

/* Status pill on mode (online/offline indicator on popup) */
.popup-mode-badge {
  display: inline-block;
  padding: 2px 8px;
  font-family: var(--font-mono);
  font-size: 9px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-faint);
  background: var(--bg-soft);
  border-radius: 8px;
  margin-left: 8px;
}
.popup-mode-badge.offline {
  background: rgba(80, 130, 60, 0.15);
  color: #4a7a3a;
}
.popup-mode-badge.fallback {
  background: rgba(180, 130, 30, 0.15);
  color: #8a6810;
}

.library-settings {
  position: absolute;
  top: 24px;
  right: 24px;
  color: var(--ink-soft);
}
@media (max-width: 600px) {
  .library-settings { top: 18px; right: 16px; }
}

/* ===========================
   TOAST
   =========================== */

.toast {
  position: fixed;
  bottom: calc(80px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--paper);
  padding: 10px 18px;
  border-radius: 24px;
  font-family: var(--font-serif);
  font-size: 14px;
  font-style: italic;
  box-shadow: var(--shadow-lg);
  z-index: 300;
  animation: toastIn 0.2s ease;
  display: flex;
  align-items: center;
  gap: 14px;
}
.toast-undo {
  background: transparent;
  border: 1px solid rgba(250, 245, 234, 0.3);
  color: var(--paper);
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 4px 10px;
  border-radius: 12px;
  cursor: pointer;
  font-style: normal;
}
.toast-undo:hover {
  background: rgba(250, 245, 234, 0.1);
}

@keyframes toastIn {
  from { opacity: 0; transform: translate(-50%, 10px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* ===========================================================
   VOCAB TRAINER
   =========================================================== */

/* Save button in popup header */
.popup-save {
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 4px 8px;
  margin-right: 4px;
  color: var(--ink-soft);
  display: flex;
  align-items: center;
  transition: color 0.15s;
}
.popup-save svg { width: 18px; height: 18px; }
.popup-save:hover { color: var(--accent); }
.popup-save.saved {
  color: var(--accent);
}
.popup-save.saved svg { fill: currentColor; stroke: currentColor; }

/* Library trainer chip */
.trainer-chip {
  margin: 16px 24px 8px;
}
.trainer-chip-btn {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  padding: 14px 18px;
  background: var(--paper);
  border: 1px solid var(--rule);
  border-left: 3px solid var(--accent);
  border-radius: 6px;
  font-family: var(--font-serif);
  text-align: left;
  cursor: pointer;
  color: var(--ink);
  transition: background 0.15s, transform 0.15s;
}
.trainer-chip-btn:hover { background: var(--bg-soft); }
.trainer-chip-btn:active { transform: scale(0.99); }
.trainer-chip-btn .chip-icon {
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent);
}
.trainer-chip-btn .chip-icon svg { width: 20px; height: 20px; }
.trainer-chip-btn .chip-text {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.trainer-chip-btn .chip-label {
  font-size: 15px;
  font-weight: 500;
  color: var(--ink);
}
.trainer-chip-btn .chip-count {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.06em;
  color: var(--ink-faint);
}
.trainer-chip-btn .chip-arrow {
  color: var(--ink-faint);
  font-family: var(--font-serif);
  font-size: 18px;
}
.trainer-chip-btn.has-due {
  border-left-color: var(--accent);
  background: rgba(139, 58, 47, 0.04);
}
.trainer-chip-btn.has-due .chip-count {
  color: var(--accent);
  font-weight: 500;
}

/* Trainer view layout */
#trainer-view {
  background: var(--bg);
}
.trainer-start, .trainer-empty, .trainer-done {
  max-width: 460px;
  margin: 0 auto;
  padding: 60px 28px 40px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}
.trainer-empty h2, .trainer-done h2 {
  font-family: var(--font-serif);
  font-weight: 500;
  font-size: 24px;
  color: var(--ink);
}
.trainer-empty p, .trainer-done p {
  font-family: var(--font-serif);
  font-size: 15px;
  color: var(--ink-soft);
  font-style: italic;
}
.trainer-summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  width: 100%;
}
.trainer-stat {
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: 16px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.trainer-stat .stat-num {
  font-family: var(--font-serif);
  font-size: 32px;
  font-weight: 500;
  color: var(--ink);
  line-height: 1;
}
.trainer-stat .stat-label {
  font-family: var(--font-mono);
  font-size: 9px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-faint);
  text-align: center;
}
.trainer-begin-btn {
  width: 100%;
  padding: 14px 24px;
  background: var(--ink);
  color: var(--paper);
  border: none;
  border-radius: 6px;
  font-family: var(--font-serif);
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 0.02em;
  cursor: pointer;
  transition: background 0.15s;
}
.trainer-begin-btn:hover { background: var(--accent); }
.trainer-begin-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.trainer-hint {
  font-family: var(--font-serif);
  font-size: 13px;
  font-style: italic;
  color: var(--ink-faint);
  margin-top: 6px;
}

/* Study session */
.trainer-session {
  max-width: 480px;
  margin: 0 auto;
  padding: 24px 20px 40px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  min-height: calc(100vh - 80px);
}
.trainer-progress {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 4px;
}
.trainer-progress .progress-bar {
  flex: 1;
  height: 3px;
  background: var(--rule);
  border-radius: 2px;
  overflow: hidden;
}
.trainer-progress .progress-fill {
  height: 100%;
  background: var(--accent);
  transition: width 0.3s ease;
}
.trainer-progress .progress-text {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.08em;
  color: var(--ink-faint);
}
.trainer-card-area {
  display: flex;
  flex-direction: column;
  gap: 16px;
  flex: 1;
}
.trainer-card-type {
  font-family: var(--font-mono);
  font-size: 9px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent);
  text-align: center;
}
.trainer-card-front {
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 8px;
  padding: 36px 24px;
  text-align: center;
  font-family: var(--font-korean);
  font-size: 36px;
  font-weight: 500;
  color: var(--ink);
  word-break: keep-all;
  min-height: 110px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.trainer-card-front.grammar {
  font-size: 22px;
  flex-direction: column;
  gap: 10px;
}
.trainer-card-front .grammar-name {
  font-size: 14px;
  color: var(--accent);
  font-family: var(--font-korean);
  letter-spacing: 0.02em;
}
.trainer-card-front .grammar-example {
  font-size: 22px;
  color: var(--ink);
  font-weight: 400;
}
.trainer-card-context {
  font-family: var(--font-serif);
  font-size: 13px;
  font-style: italic;
  color: var(--ink-soft);
  text-align: center;
  padding: 0 12px;
}

/* Multiple-choice grid */
.trainer-choices {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.choice-btn {
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: 14px 16px;
  font-family: var(--font-serif);
  font-size: 15px;
  color: var(--ink);
  cursor: pointer;
  text-align: left;
  transition: background 0.12s, border-color 0.12s, transform 0.06s;
  word-break: break-word;
}
.choice-btn:hover {
  background: var(--bg-soft);
}
.choice-btn:active {
  transform: scale(0.99);
}
.choice-btn.correct {
  background: rgba(74, 122, 58, 0.15);
  border-color: var(--pass, #4a7a3a);
  color: var(--pass, #4a7a3a);
  font-weight: 500;
}
.choice-btn.wrong {
  background: rgba(176, 64, 48, 0.12);
  border-color: var(--fail, #b04030);
  color: var(--fail, #b04030);
}
.choice-btn.dimmed {
  opacity: 0.45;
}
.choice-btn[disabled] { cursor: default; }
.choice-btn .choice-korean {
  font-family: var(--font-korean);
}

/* Reveal panel after answering */
.trainer-reveal {
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.trainer-reveal-meaning {
  font-family: var(--font-serif);
  font-size: 17px;
  color: var(--ink);
}
.trainer-reveal-meaning .label {
  font-family: var(--font-mono);
  font-size: 9px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-right: 6px;
}
.trainer-reveal-extras {
  font-family: var(--font-korean);
  font-size: 14px;
  color: var(--ink-soft);
  line-height: 1.6;
}
.trainer-next-btn {
  margin-top: 6px;
  padding: 10px 16px;
  background: var(--ink);
  color: var(--paper);
  border: none;
  border-radius: 4px;
  font-family: var(--font-serif);
  font-size: 14px;
  cursor: pointer;
  align-self: flex-end;
}
.trainer-next-btn:hover { background: var(--accent); }

/* Manage cards sheet
   Note: don't add max-height/overflow here — the parent .sheet-content is the
   single scrolling container. Two nested scroll areas make the bottom of the
   inner list unreachable on mobile (especially with dynamic toolbars). */
.trainer-card-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.trainer-card-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  background: var(--bg-soft);
  border-radius: 4px;
  border-left: 2px solid transparent;
}
.trainer-card-row.type-grammar { border-left-color: var(--accent); }
.trainer-card-row.type-vocab { border-left-color: var(--ink-faint); }
.trainer-card-row .card-front {
  font-family: var(--font-korean);
  font-size: 15px;
  color: var(--ink);
  flex: 1;
}
.trainer-card-row .card-meta {
  font-family: var(--font-mono);
  font-size: 9px;
  letter-spacing: 0.08em;
  color: var(--ink-faint);
}
.trainer-card-row .card-delete {
  background: transparent;
  border: none;
  color: var(--ink-faint);
  cursor: pointer;
  font-size: 16px;
  padding: 4px 8px;
}
.trainer-card-row .card-delete:hover { color: var(--fail, #b04030); }

/* ===========================================================
   BOTTOM TAB NAVIGATION
   =========================================================== */
.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--paper);
  border-top: 1px solid var(--rule);
  padding: 6px 8px calc(6px + var(--safe-bottom));
  display: flex;
  justify-content: space-around;
  align-items: stretch;
  z-index: 90;
  font-family: var(--font-serif);
}

.bottom-nav .nav-tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 6px 6px;
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--ink-faint);
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  position: relative;
  transition: color 0.15s;
}
.bottom-nav .nav-tab svg {
  width: 22px;
  height: 22px;
}
.bottom-nav .nav-tab:hover { color: var(--ink-soft); }
.bottom-nav .nav-tab.active {
  color: var(--accent);
}
.bottom-nav .nav-tab.active::after {
  content: '';
  position: absolute;
  top: 0;
  left: 30%;
  right: 30%;
  height: 2px;
  background: var(--accent);
  border-radius: 1px;
}

.bottom-nav .nav-tab-badge {
  position: absolute;
  top: 4px;
  right: calc(50% - 18px);
  background: var(--accent);
  color: var(--paper);
  font-family: var(--font-mono);
  font-size: 9px;
  font-weight: 600;
  letter-spacing: 0;
  padding: 1px 5px;
  border-radius: 8px;
  min-width: 14px;
  text-align: center;
}

/* Hide the nav in reader view — reading stays clean */
body.in-reader .bottom-nav {
  display: none;
}

.view {
  padding-bottom: 68px;  /* leave room for the bottom nav */
}
#reader-view {
  padding-bottom: 0;  /* reader has no nav, no padding needed */
}

/* The FAB ("Add book") needs to sit above the bottom nav */
.fab-stack {
  bottom: calc(78px + var(--safe-bottom));
}

/* Trainer home header — matches library brand style */
.trainer-home-header .brand-mark {
  color: var(--accent);
}
.trainer-manage-icon {
  position: absolute;
  top: 24px;
  right: 64px;
  color: var(--ink-soft);
}
@media (max-width: 600px) {
  .trainer-manage-icon { top: 18px; right: 56px; }
}

/* ============================================================
   NEWS VIEW
   ============================================================ */

.news-home-header .brand-mark {
  color: var(--accent);
}
.news-paste-icon {
  position: absolute;
  top: 24px;
  right: 104px;
  color: var(--ink-soft);
}
.news-refresh-icon {
  position: absolute;
  top: 24px;
  right: 64px;
  color: var(--ink-soft);
}
@media (max-width: 600px) {
  .news-paste-icon  { top: 18px; right: 92px; }
  .news-refresh-icon { top: 18px; right: 56px; }
}

/* Source chip bar */
.news-sources-bar {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 4px 20px 12px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.news-sources-bar::-webkit-scrollbar { display: none; }

.news-source-chip {
  flex: 0 0 auto;
  padding: 7px 14px;
  background: transparent;
  border: 1px solid var(--rule);
  border-radius: 999px;
  font-family: var(--font-serif);
  font-size: 13px;
  color: var(--ink-soft);
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
}
.news-source-chip:hover {
  border-color: var(--ink-faint);
  color: var(--ink);
}
.news-source-chip.active {
  background: var(--accent);
  color: var(--paper);
  border-color: var(--accent);
}

/* Article list */
.news-list {
  list-style: none;
  padding: 0 20px;
  margin: 0;
}

.news-item {
  padding: 14px 0;
  border-bottom: 1px solid var(--rule);
  cursor: pointer;
}
.news-item:last-child { border-bottom: none; }
.news-item:active { opacity: 0.6; }

.news-item-title {
  font-family: var(--font-serif);
  font-size: 16px;
  font-weight: 500;
  color: var(--ink);
  line-height: 1.4;
  margin-bottom: 6px;
}

.news-item-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-faint);
  letter-spacing: 0.04em;
}

.news-item-source {
  text-transform: uppercase;
}

.news-item-date::before {
  content: '·';
  margin-right: 10px;
}

.news-difficulty {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding: 2px 7px;
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  font-weight: 600;
}
.news-difficulty.easy {
  background: rgba(76, 134, 86, 0.12);
  color: #4c8656;
}
.news-difficulty.medium {
  background: rgba(186, 134, 64, 0.14);
  color: #a87530;
}
.news-difficulty.hard {
  background: rgba(168, 75, 75, 0.13);
  color: #a04545;
}
.news-difficulty.unknown {
  background: var(--rule);
  color: var(--ink-faint);
}

.news-item-snippet {
  margin-top: 6px;
  font-family: var(--font-serif);
  font-size: 13.5px;
  color: var(--ink-soft);
  line-height: 1.55;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Loading / error */
.news-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 60px 20px;
  color: var(--ink-faint);
  font-family: var(--font-serif);
  font-style: italic;
}
.news-loading-spinner {
  width: 28px;
  height: 28px;
  border: 2px solid var(--rule);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: news-spin 0.8s linear infinite;
}
@keyframes news-spin {
  to { transform: rotate(360deg); }
}

.news-error {
  margin: 24px 20px;
  padding: 18px;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--paper-soft, transparent);
  text-align: center;
}
.news-error-action {
  margin-top: 12px;
  padding: 8px 18px;
  background: var(--accent);
  color: var(--paper);
  border: none;
  border-radius: 6px;
  font-family: var(--font-serif);
  font-size: 14px;
  cursor: pointer;
}

/* Paste-article inputs */
.news-paste-input,
.news-paste-textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--rule);
  border-radius: 6px;
  background: transparent;
  color: var(--ink);
  font-family: var(--font-serif);
  font-size: 15px;
  box-sizing: border-box;
}
.news-paste-textarea {
  resize: vertical;
  min-height: 120px;
  line-height: 1.5;
}
.news-paste-input:focus,
.news-paste-textarea:focus {
  outline: none;
  border-color: var(--accent);
}

/* Sources management list */
.news-sources-list {
  padding: 0 20px;
}
.news-source-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid var(--rule);
}
.news-source-row:last-child { border-bottom: none; }
.news-source-name {
  font-family: var(--font-serif);
  font-size: 15px;
  color: var(--ink);
}
.news-source-meta {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--ink-faint);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-top: 2px;
}
.news-source-toggle {
  position: relative;
  width: 42px;
  height: 24px;
  background: var(--rule);
  border-radius: 12px;
  border: none;
  cursor: pointer;
  transition: background 0.2s;
}
.news-source-toggle::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  background: var(--paper);
  border-radius: 50%;
  transition: transform 0.2s;
}
.news-source-toggle.on {
  background: var(--accent);
}
.news-source-toggle.on::after {
  transform: translateX(18px);
}
