:root, :root[data-theme="light"] {
  --bg: #f7f5f0;
  --panel: #ffffff;
  --ink: #1f2328;
  --muted: #6b7280;
  --accent: #7c4a2d;
  --accent-soft: #f0e6dd;
  --line: #e3ddd2;
  --correct: #2f7d4f;
  --wrong: #b0322b;
  --tip-bg: #2b2b2b;
  --tip-ink: #ffffff;
  --tip-muted: #c8c8c8;
  --tip-ctx: #e6d3c2;
  --opt-correct-bg: #e7f4ec;
  --opt-wrong-bg: #fbeae9;
}

:root[data-theme="dark"] {
  --bg: #16140f;
  --panel: #211e18;
  --ink: #ece7dd;
  --muted: #9a9285;
  --accent: #d9a273;
  --accent-soft: #352c22;
  --line: #38322a;
  --correct: #6cc28d;
  --wrong: #e0817a;
  --tip-bg: #0d0c0a;
  --tip-ink: #f3eee4;
  --tip-muted: #b6ad9d;
  --tip-ctx: #d9a273;
  --opt-correct-bg: #1d3528;
  --opt-wrong-bg: #3a201e;
}

* { box-sizing: border-box; }

html, body { transition: background 0.2s, color 0.2s; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
}

header {
  padding: 18px 24px 0;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  position: sticky;
  top: 0;
  z-index: 5;
}

h1 {
  margin: 0 0 12px;
  font-size: 22px;
  font-weight: 600;
}
h1 .sub { color: var(--muted); font-weight: 400; font-size: 14px; }

.tabs { display: flex; gap: 4px; }
.tab {
  border: none;
  background: none;
  padding: 10px 16px;
  font-size: 15px;
  cursor: pointer;
  color: var(--muted);
  border-bottom: 2px solid transparent;
}
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

.right-stack {
  margin-left: auto;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
}

.sponsor {
  display: inline-block;
  color: var(--muted);
  font-size: 12px;
  text-decoration: none;
  padding: 4px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  white-space: nowrap;
}
.sponsor:hover { color: var(--accent); border-color: var(--accent); }

.theme-toggle {
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--bg);
  color: var(--ink);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.theme-toggle:hover { border-color: var(--accent); }

.view { display: none; max-width: 820px; margin: 0 auto; padding: 24px; }
.view.active { display: block; }

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: center;
  margin-bottom: 20px;
}
.controls label { font-size: 13px; color: var(--muted); display: flex; flex-direction: column; gap: 4px; }
.controls select, .controls button {
  font-size: 14px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel);
  color: var(--ink);
}
.controls .hint { font-size: 12px; color: var(--muted); margin-left: auto; }

button.primary, button#quiz-start {
  background: var(--accent);
  color: #fff;
  border: none;
  cursor: pointer;
  font-weight: 600;
  align-self: flex-end;
}
button.primary:hover { filter: brightness(1.05); }

/* ---------- Reader ---------- */
.greek-text {
  font-family: "SBL Greek", "New Athena Unicode", "Cardo", "Gentium Plus", Georgia, serif;
  font-size: clamp(26px, 3.2vw, 34px);
  line-height: 2.05;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 28px 32px;
}
.verse { display: block; }
.verse-num {
  font-family: -apple-system, sans-serif;
  font-size: 11px;
  color: var(--accent);
  vertical-align: super;
  margin: 0 3px 0 6px;
  font-weight: 700;
}
.w {
  cursor: pointer;
  border-radius: 4px;
  padding: 0 1px;
  transition: background 0.08s;
}
.w:hover { background: var(--accent-soft); }
.w.pinned { background: var(--accent); color: #fff; }

/* ---------- Reader: chapter nav + reading modes ---------- */
.chapter-nav { display: flex; gap: 10px; align-items: flex-end; }
.navbtn {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  border-radius: 7px;
  padding: 7px 11px;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
}
.navbtn:hover { border-color: var(--accent); color: var(--accent); }

/* Interlinear layout: stack a small romanisation / gloss under each word. */
.greek-text.mode-translit .w,
.greek-text.mode-gloss .w {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  vertical-align: top;
  line-height: 1.25;
  margin: 0 2px 10px;
}
.w-sub { display: none; }
.greek-text.mode-translit .w-translit,
.greek-text.mode-gloss .w-gloss {
  display: block;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 12px;
  color: var(--muted);
  font-weight: 400;
}
.greek-text.mode-gloss .w-gloss { color: var(--accent); }

/* Interlinear: show romanisation AND gloss under every word at once. */
.greek-text.mode-interlinear .w {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  vertical-align: top;
  line-height: 1.25;
  margin: 0 3px 12px;
}
.greek-text.mode-interlinear .w-translit,
.greek-text.mode-interlinear .w-gloss {
  display: block;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 12px;
  font-weight: 400;
}
.greek-text.mode-interlinear .w-translit { color: var(--muted); }
.greek-text.mode-interlinear .w-gloss { color: var(--accent); }

/* Learned-word highlighting (driven by the flashcard deck). */
.greek-text .w-known { background: var(--opt-correct-bg); }
.greek-text .w-learning { box-shadow: inset 0 -2px 0 var(--accent); }

/* Reading streak + chapters-read line, and the highlight toggle. */
.reader-progress { font-size: 13px; color: var(--muted); margin: -8px 0 16px; }
.controls label.hl-toggle { flex-direction: row; align-items: center; gap: 6px; cursor: pointer; }
.controls label.hl-toggle input { margin: 0; }

/* ---------- Reader: per-verse commentary cards ---------- */
#reader-view { max-width: 1180px; } /* wider than other views to fit the side column */
.greek-text .verse-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  column-gap: 28px;
  align-items: start;
  margin-bottom: 6px;
}
.verse-card {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
}
.verse-card-ref { font-size: 11px; font-weight: 700; color: var(--accent); margin-bottom: 3px; }
.verse-card-body { color: var(--muted); }
.verse-card-body:empty::before { content: '—'; opacity: 0.35; } /* placeholder until commentary lands */

@media (max-width: 760px) {
  #reader-view { max-width: 820px; }
  .greek-text .verse-row { grid-template-columns: 1fr; row-gap: 6px; margin-bottom: 14px; }
  .verse-card { margin-top: 4px; }
}

/* ---------- Tooltip ---------- */
.tooltip {
  position: absolute;
  z-index: 50;
  max-width: min(420px, 92vw);
  background: var(--tip-bg);
  color: var(--tip-ink);
  border-radius: 10px;
  padding: 14px 18px;
  font-size: 16px;
  line-height: 1.45;
  box-shadow: 0 8px 30px rgba(0,0,0,0.3);
  pointer-events: none;
}
.tooltip .tt-gloss { font-size: 21px; font-weight: 600; margin-bottom: 6px; }
.tooltip .tt-lemma { font-family: "SBL Greek", Cardo, Georgia, serif; font-size: 24px; }
.tooltip .tt-meta { color: var(--tip-muted); font-size: 15px; margin-top: 6px; }
.tooltip .tt-ctx { color: var(--tip-ctx); font-style: italic; font-size: 16px; margin-top: 6px; }
.tooltip .tt-translit { color: var(--tip-muted); font-size: 15px; font-family: -apple-system, sans-serif; margin-left: 6px; }
.tooltip .tt-term { white-space: nowrap; }
.tooltip.pinned { pointer-events: auto; } /* pinned tip is interactive (Add button) */
.tt-add {
  margin-top: 10px;
  font: inherit;
  font-size: 13px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 6px 11px;
  cursor: pointer;
}
.tt-add:hover { filter: brightness(1.08); }
.tt-add:disabled { opacity: 0.75; cursor: default; }
.tt-known { margin-top: 10px; font-size: 13px; color: var(--tip-ctx); }

/* ---------- Learn: alphabet + grammar reference ---------- */
.learn-intro { font-size: 16px; color: var(--ink); margin: 0 0 20px; }
.learn-h { font-size: 19px; margin: 30px 0 12px; }
.learn-sub { color: var(--muted); font-size: 14px; margin: 0 0 14px; }

.alpha-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
}
.alpha-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
}
.alpha-glyph { font-family: "SBL Greek", Cardo, Georgia, serif; font-size: 30px; line-height: 1.1; }
.alpha-name { display: flex; justify-content: space-between; align-items: baseline; margin: 4px 0; }
.alpha-letter-name { font-weight: 600; font-size: 14px; }
.alpha-translit { color: var(--accent); font-size: 15px; font-weight: 600; }
.alpha-sound { color: var(--muted); font-size: 12.5px; }

.extra-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px; }
.extra-card { display: flex; gap: 12px; align-items: center; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; }
.extra-sign { font-family: "SBL Greek", Cardo, Georgia, serif; font-size: 24px; min-width: 56px; }
.extra-name { font-weight: 600; font-size: 14px; }
.extra-note { color: var(--muted); font-size: 13px; }

.gram-h { font-size: 15px; margin: 20px 0 6px; color: var(--accent); }
.gram-list { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; margin: 0; }
.gram-list dt { font-weight: 600; font-size: 14px; }
.gram-list dd { margin: 0; color: var(--muted); font-size: 14px; }

/* ---------- Flashcards (SRS) ---------- */
.srs-stats { display: flex; gap: 12px; margin-bottom: 18px; }
.stat-card { flex: 1; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 16px; text-align: center; }
.stat-num { font-size: 30px; font-weight: 700; color: var(--accent); }
.stat-label { font-size: 12.5px; color: var(--muted); margin-top: 2px; }

.mastery { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 16px; margin-bottom: 18px; }
.mastery-row { display: flex; align-items: center; gap: 12px; margin: 8px 0; }
.mastery-label { font-size: 13px; width: 100px; color: var(--muted); }
.bar { flex: 1; height: 9px; background: var(--accent-soft); border-radius: 6px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--accent); border-radius: 6px; transition: width 0.3s; }
.mastery-pct { font-size: 12.5px; color: var(--muted); width: 64px; text-align: right; }

.srs-controls { display: flex; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.srs-field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.srs-field select { font-size: 14px; padding: 7px 10px; border: 1px solid var(--line); border-radius: 7px; background: var(--panel); color: var(--ink); }
.srs-start { width: 100%; padding: 13px; font-size: 16px; border-radius: 9px; }
.srs-start:disabled { opacity: 0.5; cursor: default; }
.srs-reset { display: block; margin: 14px auto 0; background: none; border: none; color: var(--muted); font-size: 13px; cursor: pointer; text-decoration: underline; }
.srs-tip { margin-top: 18px; text-align: center; }

.srs-progress { display: flex; justify-content: space-between; align-items: center; font-size: 13px; color: var(--muted); margin-bottom: 14px; }
.new-badge { background: var(--accent); color: #fff; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 20px; letter-spacing: 0.04em; }

.flashcard { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 36px 24px; text-align: center; min-height: 180px; }
.fc-greek { font-family: "SBL Greek", Cardo, Georgia, serif; font-size: clamp(36px, 8vw, 52px); line-height: 1.15; }
.fc-translit { color: var(--muted); font-size: 16px; margin-top: 4px; }
.fc-english { font-size: clamp(24px, 6vw, 34px); font-weight: 600; }
.fc-hint { color: var(--muted); font-size: 13px; margin-top: 10px; }
.fc-back { margin-top: 20px; padding-top: 18px; border-top: 1px solid var(--line); }
.fc-answer { font-size: 22px; font-weight: 600; }
.fc-greek-answer { font-family: "SBL Greek", Cardo, Georgia, serif; font-size: 34px; }
.fc-full { color: var(--muted); font-size: 14px; margin-top: 8px; }
.fc-example { color: var(--muted); font-size: 12.5px; margin-top: 10px; font-style: italic; }

.fc-show { width: 100%; margin-top: 16px; padding: 13px; font-size: 16px; border-radius: 9px; }
.fc-grades { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 16px; }
.grade { display: flex; flex-direction: column; gap: 2px; align-items: center; padding: 11px 6px; border: 1px solid var(--line); border-radius: 9px; background: var(--bg); color: var(--ink); cursor: pointer; }
.grade:hover { border-color: var(--accent); }
.grade-label { font-weight: 600; font-size: 15px; }
.grade-int { font-size: 11px; color: var(--muted); }
.grade.g-again { border-color: var(--wrong); color: var(--wrong); }
.grade.g-easy { border-color: var(--correct); color: var(--correct); }

/* ---------- Quiz ---------- */
#quiz-area { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 22px; }
.q-progress { font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.q-prompt {
  font-size: clamp(30px, 6vw, 40px);
  margin: 6px 0 4px;
  font-family: "SBL Greek", Cardo, Georgia, serif;
}
.q-prompt.english { font-family: -apple-system, sans-serif; font-size: clamp(22px, 5vw, 30px); }
.q-hint { color: var(--muted); font-size: 13px; margin-bottom: 16px; }
.options { display: flex; flex-direction: column; gap: 10px; }
.option {
  text-align: left;
  padding: 13px 16px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--bg);
  font-size: 16px;
  cursor: pointer;
}
.option.greek { font-family: "SBL Greek", Cardo, Georgia, serif; font-size: 20px; }
.option:hover:not(:disabled) { border-color: var(--accent); }
.option.correct { background: var(--opt-correct-bg); border-color: var(--correct); color: var(--correct); font-weight: 600; }
.option.wrong { background: var(--opt-wrong-bg); border-color: var(--wrong); color: var(--wrong); }
.q-foot { margin-top: 18px; display: flex; align-items: center; gap: 14px; }
.q-foot .next { background: var(--accent); color: #fff; border: none; padding: 9px 18px; border-radius: 8px; cursor: pointer; font-weight: 600; }
.score { font-size: 14px; color: var(--muted); }
.result { text-align: center; padding: 20px; }
.result .big { font-size: 40px; font-weight: 700; color: var(--accent); }

/* ---------- Responsive ---------- */
/* Keep everything inside the viewport on every screen size. */
html, body { max-width: 100%; overflow-x: hidden; -webkit-text-size-adjust: 100%; }
img, select, button, input { max-width: 100%; }

/* Tablet and down */
@media (max-width: 760px) {
  .view { padding: 18px 16px; }
  .greek-text { padding: 20px 18px; }
  .controls { gap: 12px; }
  .controls .hint { margin-left: 0; flex-basis: 100%; }
}

/* Phones */
@media (max-width: 520px) {
  header { padding: 12px 14px 0; }
  h1 { font-size: 18px; }
  h1 .sub { display: block; font-size: 12px; margin-top: 2px; }
  .tabs { flex-wrap: wrap; }
  .tab { padding: 9px 12px; font-size: 14px; }
  .right-stack { flex-direction: row; align-items: center; gap: 8px; }
  .view { padding: 14px 12px; }

  /* Stack the form controls full-width so selects are tap-friendly */
  .controls { flex-direction: column; align-items: stretch; gap: 12px; }
  .controls label { width: 100%; }
  .controls select,
  .controls button,
  button#quiz-start { width: 100%; }
  .controls .hint { display: none; }

  .greek-text { padding: 16px 14px; line-height: 1.95; }
  #quiz-area { padding: 18px 14px; }
  .option { padding: 14px; font-size: 17px; }     /* larger tap targets */
  .option.greek { font-size: 22px; }
  .q-foot { flex-direction: column; align-items: stretch; }
  .q-foot .next { width: 100%; padding: 12px; }

  .chapter-nav { width: 100%; }
  .chapter-nav label { flex: 1; }
  .srs-stats { gap: 8px; }
  .stat-card { padding: 12px 6px; }
  .stat-num { font-size: 24px; }
  .gram-list { grid-template-columns: 1fr; gap: 2px 0; }
  .gram-list dd { margin-bottom: 8px; }
}

/* Devices without hover (touch): tap a word to pin its tip */
@media (hover: none) {
  .controls .hint::after { content: "Tap a word for its meaning"; }
}
