:root {
  --bg: #fbfaf7;
  --bg-elev: #fbfaf7;
  --fg: #1c1b1a;
  --muted: #807a72;
  --line: #ece8e0;
  --accent: #2b2a28;
  --hover: #f1ede4;
  --active: #e6e0d2;
  --shadow: 0 1px 0 rgba(0,0,0,.04);
  --orp: #c14545;
  --scroll: #d6d1c5;
  --serif: 'Crimson Pro', Georgia, 'Times New Roman', serif;
  --sans: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, Consolas, monospace;
  --dys: 'OpenDyslexic', 'Comic Sans MS', sans-serif;
  --read-font: var(--serif);
  --read-size: 19px;
  --read-lh: 1.7;
  --read-width: 680px;
}

/* Pleasant warm dark — like paper read by candlelight.
   Avoids pure black + pure white (eye-strain combo) and uses
   a subtle elevation step for sidebar / panels. */
[data-theme="dark"] {
  --bg:       #1a1816;   /* base, warm near-black */
  --bg-elev:  #211e1a;   /* sidebar, settings, toolbar — gently lifted */
  --fg:       #e8dec9;   /* warm cream text, not harsh white */
  --muted:    #8c8476;   /* warm secondary text, readable */
  --line:     #2e2924;   /* dividers, hairline borders */
  --accent:   #f4ead6;   /* progress bar, focus ring */
  --hover:    #2a2620;   /* hover wash on elevated bg */
  --active:   #36302a;   /* selected library item */
  --shadow:   0 1px 0 rgba(0,0,0,.4);
  --orp:      #e88a8a;   /* RSVP marker — soft coral, not harsh red */
  --scroll:   #3a352e;
  color-scheme: dark;
}

[data-theme="sepia"] {
  --bg: #f4ecd8;
  --bg-elev: #f4ecd8;
  --fg: #4a3f2c;
  --muted: #8b7d5e;
  --line: #e3d8bd;
  --accent: #3a3020;
  --hover: #ece2c8;
  --active: #e0d4b3;
  --orp: #a83b3b;
  --scroll: #d6c89e;
}

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

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }

/* Custom scrollbars — quieter than the OS default, themed. */
* { scrollbar-width: thin; scrollbar-color: var(--scroll) transparent; }
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb {
  background: var(--scroll);
  border-radius: 8px;
  border: 2px solid transparent;
  background-clip: padding-box;
}
*::-webkit-scrollbar-thumb:hover { background: var(--muted); background-clip: padding-box; }
::selection { background: var(--active); color: var(--fg); }
body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--fg);
  display: grid;
  grid-template-columns: 280px 1fr;
  transition: background .25s ease, color .25s ease;
  overflow: hidden;
}
body:not(:has(#sidebar.open)) { grid-template-columns: 0 1fr; }

button {
  font: inherit;
  color: inherit;
  background: transparent;
  border: 0;
  cursor: pointer;
  padding: 0;
}
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

kbd {
  font-family: var(--mono);
  font-size: 11px;
  padding: 2px 6px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--hover);
}

/* ---------- sidebar ---------- */
#sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;        /* respects mobile browser chrome (URL bar) */
  overflow: hidden;
  transition: transform .25s ease, opacity .25s ease, background .25s ease;
}
#sidebar:not(.open) { transform: translateX(-100%); opacity: 0; pointer-events: none; }

.side-head {
  padding: 18px 18px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.brand {
  font-family: var(--serif);
  font-size: 22px;
  font-style: italic;
  letter-spacing: -.5px;
}
.icon-btn {
  width: 30px; height: 30px;
  border-radius: 6px;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
}
.icon-btn:hover { background: var(--hover); color: var(--fg); }

.primary {
  margin: 4px 14px 14px;
  padding: 9px 12px;
  border-radius: 8px;
  border: 1px dashed var(--line);
  color: var(--muted);
  font-size: 13px;
  text-align: left;
  transition: all .15s;
}
.primary:hover { border-color: var(--accent); color: var(--fg); background: var(--hover); }

#library {
  list-style: none;
  margin: 0;
  padding: 4px 8px;
  flex: 1;
  overflow-y: auto;
}
#library li {
  padding: 9px 10px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--fg);
  position: relative;
}
#library li:hover { background: var(--hover); }
#library li.active { background: var(--active); }
#library li .name {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#library li .meta {
  font-size: 11px;
  color: var(--muted);
  flex-shrink: 0;
}
#library li .del {
  visibility: hidden;
  color: var(--muted);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 14px;
}
#library li:hover .del { visibility: visible; }
#library li .del:hover { color: var(--fg); background: var(--bg); }

/* ---------- paste-text dialog ---------- */
#pasteDialog {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, .42);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  animation: fadeIn .15s ease;
}
@keyframes fadeIn { from { opacity: 0; } }
.paste-card {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 22px 22px 18px;
  max-width: 560px;
  width: 100%;
  max-height: 90dvh;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 14px 50px rgba(0, 0, 0, .25);
}
[data-theme="dark"] .paste-card { box-shadow: 0 16px 60px rgba(0, 0, 0, .65); }
.paste-card h3 {
  margin: 0;
  font-family: var(--serif);
  font-style: italic;
  font-weight: 400;
  font-size: 22px;
  letter-spacing: -.3px;
}
.paste-hint {
  margin: 0;
  font-size: 12.5px;
  color: var(--muted);
  line-height: 1.45;
}
#pasteTitle, #pasteBody {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--fg);
  border-radius: 7px;
  font: inherit;
  outline: none;
  font-size: 13.5px;
}
#pasteTitle:focus, #pasteBody:focus { border-color: var(--accent); }
#pasteBody {
  resize: vertical;
  min-height: 200px;
  max-height: 50dvh;
  font-family: var(--sans);
  line-height: 1.5;
}
.paste-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}
.paste-actions .ghost {
  padding: 8px 14px;
  border: 1px solid var(--line);
  font-size: 13px;
}
#pasteSave { background: var(--active); color: var(--fg); }
#pasteSave:hover { background: var(--hover); }

/* ---------- folders ---------- */
#library li.folder-head {
  padding: 14px 10px 5px;
  margin: 0;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: .9px;
  text-transform: uppercase;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
  background: transparent;
  border-radius: 0;
  position: relative;
}
#library li.folder-head:hover { background: transparent; color: var(--fg); }
#library li.folder-head:first-child { padding-top: 6px; }
#library li.folder-head .folder-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 12px;
  height: 12px;
  font-size: 9px;
  transition: transform .15s ease;
  flex-shrink: 0;
  color: var(--muted);
}
#library li.folder-head.collapsed .folder-toggle { transform: rotate(-90deg); }
#library li.folder-head .folder-name {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  outline: none;
}
#library li.folder-head .folder-name[contenteditable="true"] {
  background: var(--hover);
  color: var(--fg);
  font-weight: 500;
  letter-spacing: 0;
  text-transform: none;
  padding: 2px 6px;
  border-radius: 3px;
  cursor: text;
}
#library li.folder-head .folder-count {
  font-size: 10px;
  font-weight: 400;
  letter-spacing: 0;
  text-transform: none;
  color: var(--muted);
  padding: 1px 6px;
  background: var(--hover);
  border-radius: 8px;
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
}
#library li.folder-head .folder-del {
  visibility: hidden;
  font-size: 14px;
  line-height: 1;
  color: var(--muted);
  padding: 2px 6px;
  border-radius: 3px;
  flex-shrink: 0;
  text-transform: none;
  letter-spacing: 0;
  font-weight: 400;
}
#library li.folder-head:hover .folder-del { visibility: visible; }
#library li.folder-head .folder-del:hover { color: var(--fg); background: var(--bg); }
#library li.folder-head.unfiled { opacity: .8; }
#library li.folder-head.unfiled .folder-name { font-style: italic; }

/* per-doc move-to-folder button */
#library li.lib-item .lib-menu {
  visibility: hidden;
  cursor: pointer;
  font-size: 14px;
  letter-spacing: 1px;
  color: var(--muted);
  padding: 2px 4px;
  border-radius: 3px;
  flex-shrink: 0;
  line-height: .6;
}
#library li.lib-item:hover .lib-menu { visibility: visible; }
#library li.lib-item .lib-menu:hover { color: var(--fg); background: var(--bg); }

@media (hover: none) {
  #library li.lib-item .lib-menu     { visibility: visible; opacity: .55; }
  #library li.folder-head .folder-del { visibility: visible; opacity: .55; }
}

/* the floating "Move to…" popover */
.folder-menu {
  position: fixed;
  z-index: 50;
  min-width: 180px;
  max-width: 240px;
  max-height: 50vh;
  overflow-y: auto;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 4px;
  box-shadow: 0 8px 24px rgba(0,0,0,.15);
  font-family: var(--sans);
  font-size: 13px;
  animation: folderMenuPop .12s ease;
}
@keyframes folderMenuPop { from { opacity: 0; transform: translateY(-3px); } }
[data-theme="dark"] .folder-menu { box-shadow: 0 10px 28px rgba(0,0,0,.55); }
.folder-menu .folder-menu-head {
  font-size: 10.5px;
  letter-spacing: .8px;
  text-transform: uppercase;
  color: var(--muted);
  padding: 7px 10px 4px;
  font-weight: 600;
}
.folder-menu .folder-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border-radius: 5px;
  font-size: 13px;
  color: var(--fg);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.folder-menu .folder-menu-item:hover { background: var(--hover); }
.folder-menu .folder-menu-item.unfile { color: var(--muted); font-style: italic; }
.folder-menu .folder-menu-item.new {
  color: var(--fg);
  border-top: 1px solid var(--line);
  margin-top: 4px;
  padding-top: 10px;
}
.folder-menu .folder-menu-empty {
  padding: 8px 10px;
  font-size: 12px;
  color: var(--muted);
  font-style: italic;
}

/* ---------- table of contents ---------- */
#library li .toc-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  font-size: 10px;
  color: var(--muted);
  flex-shrink: 0;
  margin-right: -2px;
  transition: transform .18s ease, color .12s ease;
  user-select: none;
}
#library li .toc-toggle:hover { color: var(--fg); }
#library li .toc-toggle.collapsed { transform: rotate(-90deg); }

#library .toc-list {
  list-style: none;
  margin: 0 0 6px;
  padding: 0;
  border-left: 1px solid var(--line);
  margin-left: 18px;
}
#library .toc-list li {
  /* override the library li base styles */
  padding: 4px 8px 4px 10px;
  margin: 0;
  border-radius: 4px;
  font-size: 12px;
  font-family: var(--sans);
  color: var(--muted);
  display: block;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  position: static;
  transition: background .12s, color .12s;
}
#library .toc-list li:hover { background: var(--hover); color: var(--fg); }
#library .toc-list li[data-level="1"] { padding-left: 10px; font-weight: 500; color: var(--fg); }
#library .toc-list li[data-level="2"] { padding-left: 22px; }
#library .toc-list li[data-level="3"] { padding-left: 34px; }
#library .toc-list li[data-level="4"] { padding-left: 46px; }
#library .toc-list li[data-level="5"] { padding-left: 58px; }
#library .toc-list li[data-level="6"] { padding-left: 70px; }
#library .toc-list li.unlinked { opacity: .55; cursor: default; }
#library .toc-list li.unlinked:hover { background: transparent; color: var(--muted); }

/* brief flash to confirm a TOC jump landed on the right paragraph */
.toc-flash {
  animation: tocFlash 1.4s ease;
  border-radius: 4px;
}
@keyframes tocFlash {
  0%   { background: rgba(247, 200, 70, .55); box-shadow: 0 0 0 6px rgba(247, 200, 70, .55); }
  100% { background: transparent;             box-shadow: 0 0 0 6px transparent; }
}
[data-theme="dark"] .toc-flash { animation-name: tocFlashDark; }
@keyframes tocFlashDark {
  0%   { background: rgba(244, 220, 130, .22); box-shadow: 0 0 0 6px rgba(244, 220, 130, .22); }
  100% { background: transparent;              box-shadow: 0 0 0 6px transparent; }
}

.side-foot {
  padding: 10px 14px 14px;
  border-top: 1px solid var(--line);
}
.ghost {
  padding: 7px 10px;
  font-size: 13px;
  color: var(--muted);
  border-radius: 6px;
}
.ghost:hover { color: var(--fg); background: var(--hover); }

/* ---------- main reader ---------- */
#reader {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  position: relative;
}

#topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 22px;
  border-bottom: 1px solid var(--line);
}
#docTitle {
  margin: 0;
  font-family: var(--serif);
  font-style: italic;
  font-weight: 400;
  font-size: 16px;
  color: var(--muted);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#modes {
  display: flex;
  gap: 2px;
  background: var(--hover);
  padding: 3px;
  border-radius: 8px;
}
#modes button {
  padding: 6px 12px;
  font-size: 12.5px;
  color: var(--muted);
  border-radius: 6px;
  font-weight: 500;
  transition: all .15s;
}
#modes button:hover { color: var(--fg); }
#modes button.active {
  background: var(--bg);
  color: var(--fg);
  box-shadow: var(--shadow);
}

/* ---------- stage ---------- */
#stage {
  flex: 1;
  overflow-y: auto;
  position: relative;
}

#empty {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.empty-inner {
  max-width: 380px;
  text-align: center;
  color: var(--muted);
}
.empty-inner h2 {
  font-family: var(--serif);
  font-weight: 400;
  font-size: 28px;
  margin: 0 0 6px;
  color: var(--fg);
  letter-spacing: -.3px;
}
.empty-inner p { margin: 0 0 24px; font-size: 14px; }
.empty-inner .primary {
  margin: 0 auto 28px;
  display: inline-block;
  padding: 10px 18px;
  border-style: solid;
}
.empty-inner .hints {
  list-style: none;
  padding: 0;
  margin: 0;
  text-align: left;
  font-size: 12.5px;
  display: grid;
  gap: 6px;
}

/* ---------- normal / bionic / focus content ---------- */
#content {
  max-width: var(--read-width);
  margin: 60px auto 120px;
  padding: 0 32px;
  font-family: var(--read-font);
  font-size: var(--read-size);
  line-height: var(--read-lh);
  color: var(--fg);
}
#content h1, #content h2, #content h3 {
  font-family: var(--read-font);
  font-weight: 600;
  letter-spacing: -.3px;
  line-height: 1.3;
  margin-top: 1.6em;
  margin-bottom: .5em;
}
#content h1 { font-size: 1.6em; }
#content h2 { font-size: 1.35em; }
#content h3 { font-size: 1.15em; }
#content p { margin: 0 0 1.05em; }
#content b, #content strong { font-weight: 600; color: var(--fg); }

/* bionic */
.b { font-weight: 700; color: var(--fg); }

/* focus — dim non-active sentences, full strength on the active window */
.line {
  display: block;
  padding: 2px 0;
  margin: 2px 0;
  color: var(--fg);
  transition: opacity .18s ease, color .18s ease;
}
.line:not(.live)                       { opacity: .22; }
[data-theme="dark"] .line:not(.live)   { opacity: .28; }
[data-theme="sepia"] .line:not(.live)  { opacity: .26; }
.line.live { opacity: 1; color: var(--fg); }

/* ---------- RSVP ---------- */
#rsvpStage {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0 24px;
  user-select: none;
}
#rsvpWord {
  font-family: var(--read-font);
  font-size: clamp(40px, 7vw, 80px);
  letter-spacing: -.5px;
  position: relative;
  display: flex;
  align-items: baseline;
  min-height: 1.2em;
}
#rsvpWord::before,
#rsvpWord::after {
  content: '';
  position: absolute;
  top: 0; bottom: 0;
  width: 1px;
  background: var(--line);
}
#rsvpWord::before { left: 41%; }
#rsvpWord::after  { left: 41%; }
#rsvpWord .pre  { color: var(--fg); }
#rsvpWord .orp  { color: var(--orp); font-weight: 600; padding: 0 1px; }
#rsvpWord .post { color: var(--fg); }
#rsvpMeta {
  margin-top: 60px;
  color: var(--muted);
  font-size: 13px;
  letter-spacing: .3px;
}

/* ---------- paragraph ---------- */
#paraStage {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
}
#paraText {
  max-width: var(--read-width);
  font-family: var(--read-font);
  font-size: calc(var(--read-size) + 2px);
  line-height: var(--read-lh);
  color: var(--fg);
}
#paraText p { margin: 0; }
#paraMeta {
  margin-top: 50px;
  color: var(--muted);
  font-size: 12.5px;
  letter-spacing: .3px;
}

/* ---------- bottombar ---------- */
#bottombar {
  border-top: 1px solid var(--line);
  background: var(--bg-elev);
  padding: 10px 22px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: background .25s ease;
}
#progressWrap {
  height: 2px;
  background: var(--line);
  border-radius: 2px;
  overflow: hidden;
}
#progressBar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width .2s;
}
#footControls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--muted);
  min-height: 24px;
}
#rsvpCtrls,
#scrollCtrls {
  display: flex;
  align-items: center;
  gap: 10px;
}
#rsvpCtrls label,
#scrollCtrls label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
}
#rsvpCtrls input[type="range"],
#scrollCtrls input[type="range"] { width: 120px; }
#scrollPlay.on { color: var(--fg); background: var(--hover); }

#voiceCtrls {
  display: flex;
  align-items: center;
  gap: 10px;
}
#voiceCtrls label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
}
#voiceCtrls input[type="range"] { width: 120px; }
#voiceBtn.on { color: var(--fg); background: var(--hover); }
#voicePlay.paused { color: var(--muted); }

/* ---------- pomodoro ---------- */
#pomoBtn {
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  font-weight: 500;
  min-width: 30px;
  white-space: nowrap;
  padding: 0 6px;
  height: 30px;
  border-radius: 6px;
  transition: color .2s, background .2s;
}
#pomoBtn .pomo-time { display: none; }
#pomoBtn.on { color: var(--fg); background: var(--hover); padding: 0 9px; }
#pomoBtn.on .pomo-icon { display: none; }
#pomoBtn.on .pomo-time { display: inline; }
#pomoBtn.break { color: #4d8f4d; background: rgba(80, 140, 80, .10); }
[data-theme="dark"] #pomoBtn.break { color: #95cf95; background: rgba(140, 200, 140, .10); }
#pomoBtn.ending { animation: pomoPulse 1.2s ease-in-out infinite; }
@keyframes pomoPulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: .55; }
}

.pomo-row {
  display: flex;
  gap: 10px;
  margin-top: 4px;
}
.pomo-field {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}
.pomo-field input {
  width: 48px;
  padding: 5px 7px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--bg);
  color: var(--fg);
  font: inherit;
  font-size: 12.5px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.pomo-field input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------- in-doc search ---------- */
#searchBar {
  /* Lives in #reader (which doesn't scroll), positioned just below the topbar.
     Was previously inside #stage where absolute positioning scrolled with content. */
  position: absolute;
  top: 64px;
  right: 22px;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 8px 6px 12px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0,0,0,.10);
  font-family: var(--sans);
  font-size: 13px;
  animation: searchPop .18s ease;
}
@keyframes searchPop { from { opacity: 0; transform: translateY(-6px); } }
[data-theme="dark"] #searchBar { box-shadow: 0 8px 24px rgba(0,0,0,.5); }
#searchInput {
  border: 0;
  background: transparent;
  color: var(--fg);
  outline: none;
  width: 200px;
  font-size: 13px;
  font-family: var(--sans);
}
#searchInput::placeholder { color: var(--muted); }
.search-count {
  color: var(--muted);
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
  min-width: 38px;
  text-align: right;
  padding: 0 4px;
}

.search-hit {
  background: rgba(120, 200, 80, 0.30);
  border-radius: 2px;
  padding: 0 1px;
  transition: background .12s;
}
.search-hit.current {
  background: rgba(120, 200, 80, 0.65);
  outline: 1px solid rgba(80, 160, 40, 0.7);
}
[data-theme="dark"]  .search-hit         { background: rgba(160, 220, 130, 0.18); }
[data-theme="dark"]  .search-hit.current { background: rgba(160, 220, 130, 0.42); outline-color: rgba(140, 220, 140, 0.6); }
[data-theme="sepia"] .search-hit         { background: rgba(60, 110, 30, 0.25); }
[data-theme="sepia"] .search-hit.current { background: rgba(60, 110, 30, 0.55); outline-color: rgba(40, 90, 20, 0.7); }

/* live sentence — soft warm wash, distinct from amber highlights */
.voice-live {
  background: rgba(70, 130, 220, .14);
  border-radius: 3px;
  padding: 1px 3px;
  margin: 0 -1px;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
  transition: background .15s ease;
}
[data-theme="dark"]  .voice-live { background: rgba(150, 195, 240, .15); }
[data-theme="sepia"] .voice-live { background: rgba(110, 80, 30, .16); }

/* voice picker in settings */
#voicePicker {
  width: 100%;
  padding: 8px 10px;
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--line);
  border-radius: 6px;
  font: inherit;
  font-size: 13px;
  margin-top: 4px;
}
#voicePicker:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.voice-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 8px;
  gap: 10px;
}
.voice-row .ghost { padding: 6px 10px; font-size: 12px; }
.voice-hint {
  font-size: 11px;
  color: var(--muted);
  flex: 1;
  text-align: right;
}

.backup-row {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}
.backup-row .ghost {
  flex: 1;
  padding: 8px 10px;
  font-size: 12.5px;
  text-align: center;
  border: 1px solid var(--line);
}
.set-hint {
  font-size: 11px;
  color: var(--muted);
  margin-top: 8px;
  line-height: 1.5;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  height: 3px;
  background: var(--line);
  border-radius: 2px;
  outline: none;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid var(--bg);
}
input[type="range"]::-moz-range-thumb {
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid var(--bg);
}

/* ---------- settings ---------- */
#settings {
  position: fixed;
  top: 0; right: 0;
  width: 320px;
  height: 100vh;
  height: 100dvh;
  background: var(--bg-elev);
  border-left: 1px solid var(--line);
  padding: 18px 22px;
  overflow-y: auto;
  z-index: 20;
  box-shadow: -10px 0 40px rgba(0,0,0,.25);
  animation: slideIn .22s ease;
}
[data-theme="dark"] #settings { box-shadow: -10px 0 40px rgba(0,0,0,.5); }
@keyframes slideIn { from { transform: translateX(100%); } }
.set-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 14px;
}
.set-head h3 {
  margin: 0;
  font-family: var(--serif);
  font-style: italic;
  font-weight: 400;
  font-size: 22px;
}
#settings section {
  padding: 14px 0;
  border-top: 1px solid var(--line);
}
#settings section:first-of-type { border-top: 0; }
#settings label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12.5px;
  color: var(--muted);
  margin-bottom: 10px;
  font-weight: 500;
}
#settings input[type="range"] { width: 100%; }
.seg {
  display: flex;
  background: var(--hover);
  padding: 3px;
  border-radius: 8px;
  gap: 2px;
}
.seg button {
  flex: 1;
  padding: 7px 4px;
  font-size: 12px;
  color: var(--muted);
  border-radius: 5px;
  font-weight: 500;
}
.seg button:hover { color: var(--fg); }
.seg button.active {
  background: var(--bg);
  color: var(--fg);
  box-shadow: var(--shadow);
}
.danger button { color: #b85a5a; }
.danger button:hover { color: #b85a5a; background: rgba(184,90,90,.08); }

/* ---------- drop overlay ---------- */
#dropOverlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.35);
  backdrop-filter: blur(4px);
  z-index: 100;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}
#dropOverlay div {
  font-family: var(--serif);
  font-size: 32px;
  font-style: italic;
  color: white;
  border: 2px dashed rgba(255,255,255,.6);
  padding: 60px 100px;
  border-radius: 16px;
}

/* ---------- toast ---------- */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--accent);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 13px;
  z-index: 200;
  animation: toastIn .2s ease;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 8px); } }

/* ---------- highlights ---------- */
mark.hl {
  background: rgba(245, 200, 80, .38);
  color: inherit;
  padding: 0 1px;
  border-radius: 2px;
  cursor: pointer;
  transition: background .15s;
}
mark.hl:hover { background: rgba(245, 200, 80, .65); }
[data-theme="dark"]  mark.hl       { background: rgba(232, 188, 90, .26); }
[data-theme="dark"]  mark.hl:hover { background: rgba(232, 188, 90, .42); }
[data-theme="sepia"] mark.hl       { background: rgba(170, 110, 30, .25); }
[data-theme="sepia"] mark.hl:hover { background: rgba(170, 110, 30, .42); }

/* ---------- note marker (left margin dot) ---------- */
#content > p,
#content > h1, #content > h2, #content > h3, #content > h4, #content > h5, #content > h6,
#content > li, #content > blockquote,
#paraText > p, #paraText > h1, #paraText > h2, #paraText > h3,
#paraText > h4, #paraText > h5, #paraText > h6, #paraText > blockquote, #paraText > li {
  position: relative;
}
.note-marker {
  position: absolute;
  left: -22px;
  top: .55em;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #c8a64a;
  cursor: pointer;
  transition: transform .15s, background .15s, box-shadow .15s;
  z-index: 1;
}
.note-marker:hover {
  transform: scale(1.25);
  background: #e0bc50;
  box-shadow: 0 0 0 4px rgba(200, 166, 74, .15);
}
[data-theme="dark"]  .note-marker { background: #d4a847; }
[data-theme="dark"]  .note-marker:hover { background: #efbf55; box-shadow: 0 0 0 4px rgba(212, 168, 71, .2); }
[data-theme="sepia"] .note-marker { background: #a8782a; }

/* ---------- note panel ---------- */
#notePanel {
  position: fixed;
  top: 70px;
  right: 24px;
  width: 290px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px 12px;
  z-index: 35;
  box-shadow: 0 8px 24px rgba(0,0,0,.12);
  font-family: var(--sans);
  animation: notePop .18s ease;
}
@keyframes notePop { from { opacity: 0; transform: translateY(-4px); } }
[data-theme="dark"] #notePanel { box-shadow: 0 10px 30px rgba(0,0,0,.55); }
.note-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
  margin-bottom: 8px;
}
.note-anchor {
  font-size: 11.5px;
  color: var(--muted);
  letter-spacing: .3px;
  flex: 1;
}
#notePanel textarea {
  width: 100%;
  min-height: 110px;
  border: 0;
  background: transparent;
  color: var(--fg);
  font-family: var(--sans);
  font-size: 13.5px;
  line-height: 1.55;
  resize: vertical;
  outline: none;
  padding: 0;
}
.note-foot {
  font-size: 10.5px;
  color: var(--muted);
  margin-top: 8px;
  letter-spacing: .3px;
}
.note-foot kbd { padding: 1px 5px; font-size: 10px; }

/* ---------- dictionary tooltip ---------- */
.dict-tip {
  position: fixed;
  z-index: 80;
  width: 290px;
  max-height: 360px;
  overflow-y: auto;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 14px 12px;
  font-family: var(--sans);
  font-size: 13px;
  line-height: 1.55;
  color: var(--fg);
  box-shadow: 0 8px 24px rgba(0,0,0,.12);
  animation: dictPop .15s ease;
}
@keyframes dictPop { from { opacity: 0; transform: translateY(-3px); } }
[data-theme="dark"] .dict-tip { box-shadow: 0 10px 30px rgba(0,0,0,.55); }
.dict-head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
}
.dict-word {
  font-family: var(--serif);
  font-size: 19px;
  font-style: italic;
  color: var(--fg);
  letter-spacing: -.2px;
}
.dict-close {
  width: 22px; height: 22px;
  font-size: 16px;
}
.dict-phon {
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--muted);
  margin-bottom: 4px;
}
.dict-pos {
  font-size: 10.5px;
  letter-spacing: .6px;
  text-transform: uppercase;
  color: var(--muted);
  margin: 8px 0 3px;
  font-weight: 500;
}
.dict-defs {
  margin: 0 0 6px;
  padding-left: 18px;
}
.dict-defs li { margin-bottom: 5px; }
.dict-ex {
  font-style: italic;
  color: var(--muted);
  margin-top: 2px;
  font-size: 12.5px;
}
.dict-loading, .dict-empty {
  color: var(--muted);
  font-style: italic;
  font-size: 12.5px;
  padding: 6px 0;
}

/* Wikipedia summary inside the lookup tooltip */
.wiki-desc {
  font-size: 11.5px;
  color: var(--muted);
  font-style: italic;
  margin: 0 0 6px;
  letter-spacing: .1px;
}
.wiki-thumb {
  float: right;
  width: 78px;
  height: 78px;
  object-fit: cover;
  border-radius: 6px;
  margin: 0 0 8px 12px;
  background: var(--hover);
  border: 1px solid var(--line);
}
.wiki-extract {
  margin: 0 0 8px;
  font-size: 13px;
  line-height: 1.55;
  color: var(--fg);
}
.wiki-link {
  display: inline-block;
  font-size: 12px;
  color: var(--muted);
  text-decoration: none;
  border-bottom: 1px dotted var(--line);
  padding-bottom: 1px;
  clear: both;
}
.wiki-link:hover { color: var(--fg); border-bottom-color: var(--fg); }

/* ---------- selection action toolbar (touch + pencil + mouse) ---------- */
#selToolbar {
  position: fixed;
  z-index: 70;
  background: rgba(20, 18, 16, .94);
  color: #f4ead6;
  border-radius: 9px;
  padding: 4px;
  display: flex;
  gap: 2px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, .35);
  font-family: var(--sans);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transform: translateY(4px);
  transition: opacity .15s ease, transform .15s ease;
  -webkit-user-select: none;
  user-select: none;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
#selToolbar.visible {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}
#selToolbar button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  background: transparent;
  color: inherit;
  border: 0;
  border-radius: 6px;
  font: inherit;
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  min-height: 34px;
}
#selToolbar button:hover  { background: rgba(255, 255, 255, .10); }
#selToolbar button:active { background: rgba(255, 255, 255, .16); }
#selToolbar svg { flex-shrink: 0; }

/* On phones, slightly larger to be thumb-friendly */
@media (max-width: 600px) {
  #selToolbar button { padding: 10px 12px; font-size: 13px; min-height: 40px; }
  #selToolbar { gap: 0; }
}

/* ---------- distraction lock (reading-only mode) ----------
   Chrome hides immediately when .locked is added; the .peek class is
   toggled briefly on user activity to reveal chrome on demand. */
body.locked { grid-template-columns: 0 1fr; }
body.locked #sidebar { display: none; }
body.locked #topbar,
body.locked #bottombar {
  transition: opacity .35s ease, transform .35s ease;
}
body.locked #topbar {
  opacity: 0;
  transform: translateY(-100%);
  pointer-events: none;
}
body.locked #bottombar {
  opacity: 0;
  transform: translateY(100%);
  pointer-events: none;
}
body.locked.peek #topbar,
body.locked.peek #bottombar {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}
body.locked:not(.peek) { cursor: none; }
body.locked:not(.peek) .line.live { cursor: none; }
#lockBtn.on { color: var(--fg); background: var(--hover); }

/* persistent corner reminder while locked */
#lockBadge {
  position: fixed;
  bottom: 18px;
  right: 22px;
  padding: 8px 14px;
  background: rgba(20,18,16,.82);
  color: #f4ead6;
  font-family: var(--sans);
  font-size: 12px;
  letter-spacing: .4px;
  border-radius: 14px;
  z-index: 60;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: opacity .4s ease, background .15s ease, transform .15s ease;
}
#lockBadge:hover  { background: rgba(20,18,16,.95); }
#lockBadge:active { transform: scale(.96); }
body.locked:not(.peek) #lockBadge { opacity: .55; }
body.locked.peek #lockBadge       { opacity: 1; }

/* keep clicks landing on the button itself, not the SVG inside it */
.icon-btn svg { pointer-events: none; }

/* ---------- responsive ----------
   Strategy: three tiers
     - desktop          (default)            mouse + keyboard
     - tablet           (max-width: 1024px)  iPad-class touch, plenty of width
     - phone            (max-width: 600px)   one-handed touch, tight width

   Plus a hover-capability query for any device that lacks hover so the UI
   doesn't rely on hover-revealed controls.
*/

/* Sidebar backdrop — only visible on touch widths when the sidebar is open. */
#sideBackdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.32);
  z-index: 31;
  -webkit-tap-highlight-color: transparent;
  animation: backdropIn .18s ease;
}
@keyframes backdropIn { from { opacity: 0; } }
@media (max-width: 1024px) {
  body:has(#sidebar.open) #sideBackdrop { display: block; }
}

/* Anything tappable: kill double-tap zoom + the iOS tap highlight box. */
button, .icon-btn, #library li, #modes button, .seg button,
input, select, .note, .note-marker, mark.hl, .toc-list li {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* No-hover devices (touch screens) — make hover-revealed UI visible by default. */
@media (hover: none) {
  #library li .del              { visibility: visible; opacity: .55; }
  #library li:hover .del        { opacity: 1; }
  #library li:hover             { background: transparent; }
  /* Bigger comfortable touch targets */
  .icon-btn                     { width: 36px; height: 36px; }
  #modes button                 { min-height: 34px; padding: 7px 12px; }
  .seg button                   { min-height: 34px; }
}

/* ---------- tablet (≤1024px) ---------- */
@media (max-width: 1024px) {
  /* Sidebar slides over the reader instead of pushing it. */
  body            { grid-template-columns: 1fr; }
  body:not(:has(#sidebar.open)) { grid-template-columns: 1fr; }
  #sidebar {
    position: fixed;
    top: 0; left: 0;
    width: 320px;
    height: 100vh;
    height: 100dvh;
    z-index: 32;
    box-shadow: 6px 0 28px rgba(0,0,0,.18);
  }
  [data-theme="dark"] #sidebar { box-shadow: 6px 0 32px rgba(0,0,0,.55); }
  /* Make modes a horizontally scrollable strip if they don't fit */
  #modes {
    overflow-x: auto;
    scrollbar-width: none;
    flex-shrink: 1;
  }
  #modes::-webkit-scrollbar { display: none; }
  #modes button { flex-shrink: 0; }
  /* Slightly bigger reader margins for comfort */
  #content { padding: 0 28px; }
}

/* ---------- phone (≤600px) ---------- */
@media (max-width: 600px) {
  /* Whole-page chrome */
  #topbar { padding: 10px 12px; gap: 8px; }
  #docTitle { display: none; }
  /* Sidebar fills most of the screen for one-handed reach */
  #sidebar {
    width: min(86vw, 320px);
  }
  /* Reading mode (distraction lock) is now reliable on phones via the
     in-app peek/chrome system, even when fullscreen API is denied. */

  /* Compact mode tabs, scroll if needed */
  #modes          { padding: 2px; }
  #modes button   { padding: 6px 10px; font-size: 12px; min-height: 32px; }

  /* Reading column tightens to use available space */
  :root           { --read-width: 100%; }
  #content        { padding: 0 18px; margin: 24px auto 80px; }
  #paraText       { padding: 0 6px; }

  /* Bottombar wraps + controls grow to fit fingers */
  #bottombar      { padding: 8px 12px 10px; }
  #footControls   { flex-wrap: wrap; gap: 8px; row-gap: 6px; }
  #rsvpCtrls input[type="range"],
  #scrollCtrls input[type="range"],
  #voiceCtrls input[type="range"] { width: 100px; }

  /* Settings becomes a full-screen sheet */
  #settings {
    width: 100vw;
    border-left: 0;
    box-shadow: none;
  }

  /* Note panel becomes a bottom sheet */
  #notePanel {
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    border-radius: 14px 14px 0 0;
    border: 1px solid var(--line);
    border-bottom: 0;
    max-height: 55vh;
    padding: 14px 16px 18px;
    animation: notePopMobile .25s ease;
  }
  @keyframes notePopMobile { from { transform: translateY(100%); } }
  #notePanel textarea { min-height: 120px; }

  /* Dictionary tooltip — wider, with viewport-aware sizing */
  .dict-tip {
    width: calc(100vw - 24px);
    max-width: 360px;
  }

  /* Search bar — fill the strip cleanly under the topbar */
  #searchBar {
    top: 56px;
    left: 12px;
    right: 12px;
    width: auto;
  }
  #searchInput { width: auto; flex: 1; min-width: 0; }

  /* TOC — slightly tighter indents on small screens */
  #library .toc-list li[data-level="2"] { padding-left: 18px; }
  #library .toc-list li[data-level="3"] { padding-left: 28px; }
  #library .toc-list li[data-level="4"] { padding-left: 38px; }
  #library .toc-list li[data-level="5"] { padding-left: 48px; }
  #library .toc-list li[data-level="6"] { padding-left: 58px; }

  /* Note marker dot — pull inside the column instead of the (gone) margin */
  .note-marker { left: -14px; width: 7px; height: 7px; }

  /* Hide keyboard-shortcut hints on the empty state — useless on touch */
  .empty-inner .hints { display: none; }
  .empty-inner h2 { font-size: 24px; }
  .empty-inner .primary { padding: 12px 22px; font-size: 14px; }

  /* Bigger toast / lock badge so they're readable on small screens */
  #toast { font-size: 13px; padding: 11px 18px; bottom: 16px; }

  /* Pomodoro button shrinks to icon-only when off; time still visible when on */
  #pomoBtn { padding: 0 4px; }
  #pomoBtn.on { padding: 0 8px; font-size: 12px; }
}

/* Landscape phone — squeeze the bottombar so the reading column has room. */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 500px) {
  #bottombar { padding: 6px 12px 8px; }
  #content { margin-top: 18px; margin-bottom: 60px; }
}

/* Print — strip chrome, just the article. */
@media print {
  #sidebar, #topbar, #bottombar, #settings, #searchBar,
  #notePanel, #lockBadge, #toast, #dropOverlay, .dict-tip,
  #pomoBtn, #voiceBtn, #lockBtn { display: none !important; }
  body { display: block; height: auto; overflow: visible; }
  #reader, #stage, #content { height: auto; overflow: visible; }
  #content { max-width: none; padding: 0; margin: 0; }
}
