/* ============================================================
   Turning Confusion into Clarity — Reader Styles
   Aesthetic: Quiet Monastery Library
   ============================================================ */

:root {
  --bg: #F6F1E9;
  --bg-reader: #FBF8F3;
  --bg-sidebar: #EDE7DB;
  --bg-audio: #E6DFD1;
  --text: #2E2118;
  --text-secondary: #70614F;
  --text-heading: #1C120A;
  --accent: #B8860B;
  --accent-hover: #9A7209;
  --accent-soft: rgba(184, 134, 11, 0.12);
  --highlight-word: rgba(184, 134, 11, 0.3);
  --highlight-played: rgba(184, 134, 11, 0.06);
  --border: rgba(44, 32, 22, 0.08);
  --red: #9B2D1F;
  --red-soft: rgba(155, 45, 31, 0.08);
  --shadow-sm: 0 1px 3px rgba(44, 32, 22, 0.06);
  --shadow-md: 0 4px 12px rgba(44, 32, 22, 0.08);

  --font-body: 'Literata', Georgia, 'Times New Roman', serif;
  --font-display: 'Cormorant', Georgia, serif;

  --sidebar-w: 272px;
  --audio-h: 52px;
  --chat-w: 380px;
  --reading-max-w: 66ch;

  /* Tunable values */
  --gutter-size: 40px;
  --gutter-top: 2px;
  --gutter-left: -48px;
  --listen-color: rgba(184, 134, 11, 0.18);
  --read-color: rgba(184, 134, 11, 0.10);
  --note-indent: 8%;

  color-scheme: light;
}

[data-theme="dark"] {
  --bg: #141110;
  --bg-reader: #1A1613;
  --bg-sidebar: #1E1A15;
  --bg-audio: #221D17;
  --text: #CFC3B0;
  --text-secondary: #887A6A;
  --text-heading: #E4DAC8;
  --accent: #D4A438;
  --accent-hover: #E2B94E;
  --accent-soft: rgba(212, 164, 56, 0.2);
  --red: #C45A4A;
  --red-soft: rgba(196, 90, 74, 0.08);
  --highlight-word: rgba(212, 164, 56, 0.35);
  --highlight-played: rgba(212, 164, 56, 0.04);
  --border: rgba(212, 164, 56, 0.06);
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.15);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.2);
  color-scheme: dark;
}

/* --- Reset & Base --- */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  font-size: 17px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-optical-sizing: auto;
}

body {
  font-family: var(--font-body);
  color: var(--text);
  background: var(--bg);
  line-height: 1.55;
  overflow: hidden;
  hyphens: auto;
  -webkit-hyphens: auto;
}

/* --- Layout --- */

#app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr 0px;
  grid-template-rows: 1fr var(--audio-h);
  height: 100vh;
  height: 100dvh;
  transition: grid-template-columns 0.3s ease;
}

#app.chat-open {
  grid-template-columns: var(--sidebar-w) 1fr var(--chat-w);
}

#sidebar {
  grid-row: 1 / -1;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
  transition: transform 0.3s ease, opacity 0.3s ease;
  z-index: 10;
}

#reader {
  grid-column: 2;
  grid-row: 1;
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--bg-reader);
  scroll-behavior: smooth;
}

#audio-bar {
  grid-column: 2 / -1;
  grid-row: 2;
  background: var(--bg-audio);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 12px;
}

/* --- Sidebar --- */

#sidebar-header {
  padding: 32px 24px 24px;
  border-bottom: 1px solid var(--border);
}

.book-title {
  font-family: var(--font-display);
  font-size: 1.5rem;
  font-weight: 500;
  line-height: 1.25;
  color: var(--text-heading);
  letter-spacing: 0.01em;
}

.book-title em {
  font-style: italic;
  color: var(--accent);
}

.book-author {
  font-family: var(--font-display);
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin-top: 8px;
  font-weight: 400;
  letter-spacing: 0.03em;
}

.book-subtitle {
  font-family: var(--font-display);
  font-size: 0.78rem;
  color: var(--text-secondary);
  opacity: 0.7;
  margin-top: 2px;
  font-style: italic;
}

#chapter-list {
  list-style: none;
  padding: 12px 0;
}

#chapter-list li {
  position: relative;
}

/* Chapter buttons — direct child only, not section TOC buttons */
#chapter-list > li > button {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  padding: 8px 24px;
  font-family: var(--font-body);
  font-size: 0.82rem;
  color: var(--text-secondary);
  cursor: pointer;
  transition: color 0.2s, background 0.2s;
  line-height: 1.4;
  border-left: 3px solid transparent;
}

#chapter-list > li > button:hover {
  color: var(--text);
  background: var(--accent-soft);
}

#chapter-list > li.active > button {
  color: var(--accent);
  font-weight: 500;
  border-left-color: var(--accent);
  background: var(--accent-soft);
}

/* Section TOC — nested headings under active chapter */

.section-list {
  list-style: none;
  padding: 4px 0 10px;
  margin: 0;
}

.section-item button {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  border-radius: 4px;
  font-family: var(--font-body);
  font-size: 0.73rem;
  color: var(--text-secondary);
  opacity: 0.45;
  cursor: pointer;
  line-height: 1.35;
  padding: 5px 14px 5px 28px;
  margin: 1px 8px 1px 0;
  transition: color 0.06s, opacity 0.06s, background 0.06s;
}

.section-level-4 button { padding-left: 38px; }
.section-level-5 button {
  padding-left: 50px;
  font-size: 0.67rem;
  opacity: 0.35;
  font-style: italic;
}

.section-item button:hover {
  color: var(--text);
  opacity: 0.7;
  background: rgba(0, 0, 0, 0.03);
}

[data-theme="dark"] .section-item button:hover {
  background: rgba(255, 255, 255, 0.04);
}

/* Active section: darker text on neutral tint — NO gold (gold = chapter level only) */
.section-item.active button {
  color: var(--text-heading);
  opacity: 1;
  background: rgba(0, 0, 0, 0.05);
}

[data-theme="dark"] .section-item.active button {
  background: rgba(255, 255, 255, 0.06);
}

#sidebar-footer {
  padding: 12px 24px;
  flex-shrink: 0;
}

#tuning-panel {
  margin-bottom: 12px;
}

#tuning-panel label {
  display: block;
  font-size: 0.68rem;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

#tuning-panel input[type="range"] {
  width: 100%;
  height: 4px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--border);
  border-radius: 2px;
  outline: none;
  margin-top: 3px;
}

#tuning-panel input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

#tuning-panel span {
  font-weight: 600;
  color: var(--accent);
}

#theme-toggle {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s;
  display: flex;
  align-items: center;
}

#theme-toggle:hover {
  color: var(--accent);
  border-color: var(--accent);
}

[data-theme="dark"] #theme-toggle .sun-icon { display: none; }
[data-theme="dark"] #theme-toggle .moon-icon { display: block; }

#sidebar-toggle {
  display: none;
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 20;
  background: var(--bg-reader);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  color: var(--text-secondary);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: color 0.2s;
}

#sidebar-toggle:hover { color: var(--accent); }

/* --- Reader / Chapter Content --- */

#chapter-content {
  max-width: var(--reading-max-w);
  margin: 0 auto;
  padding: 48px 32px 120px;
}

#loading-state {
  text-align: center;
  color: var(--text-secondary);
  font-style: italic;
  padding: 80px 0;
  font-family: var(--font-body);
  font-size: 1.1rem;
}

#chapter-content h2 {
  font-family: var(--font-body);
  font-size: 1.6rem;
  font-weight: 400;
  color: var(--text-heading);
  margin: 2.4em 0 0.6em;
  line-height: 1.25;
  letter-spacing: 0.01em;
}

#chapter-content h2:first-child {
  margin-top: 0;
}

#chapter-content h3 {
  font-family: var(--font-body);
  font-size: 1.35rem;
  font-weight: 600;
  color: var(--text-heading);
  margin: 3em 0 0.6em;
  line-height: 1.3;
  padding-top: 1.5em;
  border-top: 1px solid var(--border);
}

#chapter-content h3:first-child,
#chapter-content hr + h3 {
  border-top: none;
  padding-top: 0;
  margin-top: 1.5em;
}

#chapter-content h4 {
  font-family: var(--font-body);
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text-heading);
  margin: 2.4em 0 0.4em;
  line-height: 1.35;
}

#chapter-content h5 {
  font-family: var(--font-body);
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin: 1.4em 0 0.3em;
  line-height: 1.35;
}

#chapter-content p {
  margin-bottom: 1em;
  font-variant-numeric: oldstyle-nums;
}

#chapter-content hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2.5em 4em;
}

/* Block quotes — dharma verses (red = sacred/root text tradition) */
#chapter-content p.blockquote {
  border-left: 2px solid var(--red);
  padding-left: 1.4em;
  margin: 1.5em 0;
  font-style: italic;
  color: var(--text-secondary);
}

/* Images */
.book-image {
  margin: 2em 0;
  text-align: center;
}

.book-image img {
  max-width: 100%;
  max-height: 420px;
  border-radius: 4px;
  box-shadow: var(--shadow-md);
}

.book-image figcaption {
  font-size: 0.78rem;
  color: var(--text-secondary);
  margin-top: 8px;
  font-style: italic;
  line-height: 1.4;
  max-width: 80%;
  margin-left: auto;
  margin-right: auto;
}

/* --- Word Spans (the foundation of audio sync) --- */

.word {
  cursor: pointer;
  border-radius: 2px;
  padding: 1px 0;
  transition: background 0.15s ease;
}

.word:hover {
  background: var(--accent-soft);
}

.word.italic {
  font-style: italic;
}

.word.bold {
  font-weight: 600;
}

/* Glossary terms — no underline, discoverable on hover */
.glossary-term {
  cursor: pointer;
  transition: color 0.1s;
}

.glossary-term:hover {
  color: var(--accent);
}

#glossary-tooltip {
  position: fixed;
  z-index: 100;
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  max-width: 340px;
  font-family: var(--font-body);
  font-size: 0.78rem;
  line-height: 1.5;
  color: var(--text);
  box-shadow: var(--shadow-md);
  pointer-events: none;
}

#glossary-tooltip strong {
  color: var(--accent);
  font-size: 0.85rem;
}

/* Loading scan — left-to-right glow on clicked word while audio loads */
.word.loading-scan {
  position: relative;
  overflow: hidden;
  background: var(--highlight-word);
  border-radius: 2px;
}

.word.loading-scan::after {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(184, 134, 11, 0.3), transparent);
  animation: scanGlow 1s ease-in-out infinite;
  border-radius: 2px;
}

[data-theme="dark"] .word.loading-scan::after {
  background: linear-gradient(90deg, transparent, rgba(212, 164, 56, 0.35), transparent);
}

@keyframes scanGlow {
  0% { left: -100%; }
  100% { left: 100%; }
}

/* Play button loading state */
#play-btn.loading {
  animation: pulseBtn 1s ease-in-out infinite;
}

@keyframes pulseBtn {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* Currently playing word */
.word.active {
  background: var(--highlight-word);
  border-radius: 2px;
  box-shadow: 0 0 0 2px var(--highlight-word);
}

/* CSS Custom Highlight API — no DOM modification, native rendering */
::highlight(played-trail) {
  background-color: var(--listen-color, rgba(184, 134, 11, 0.08));
}

::highlight(listened-history) {
  background-color: var(--listen-color, rgba(184, 134, 11, 0.08));
}

/* Legacy .listened-range marks — replaced by CSS Highlight API */

/* Paragraph marked as read — subtle gold tint */
[data-read] {
  background: var(--read-color, rgba(184, 134, 11, 0.04));
  border-radius: 4px;
}

[data-theme="dark"] [data-read] {
  background: rgba(212, 164, 56, 0.05);
}

/* Both read paragraph + listened range — colors layer */
[data-read] .listened-range {
  background: rgba(184, 134, 11, 0.1);
}

/* Left gutter — checkmark + note actions */
#chapter-content [data-para-index] {
  position: relative;
}

.para-gutter {
  position: absolute;
  left: var(--gutter-left);
  top: var(--gutter-top);
  display: flex;
  flex-direction: column;
  gap: 5px;
  align-items: center;
  z-index: 2;
}

.gutter-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--gutter-size);
  height: var(--gutter-size);
  border-radius: 50%;
  border: none;
  background: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 10px;
  box-sizing: content-box;
  margin: -10px;
  opacity: 0.18;
  transition: opacity 0.1s, color 0.1s;
}

[data-para-index]:hover .gutter-btn {
  opacity: 0.45;
}

.gutter-btn:hover {
  opacity: 1 !important;
  color: var(--accent);
}

/* When checked, the SVG itself handles the gold fill/stroke */
[data-read] .gutter-read {
  opacity: 0.9;
}

/* Note pencil — below the circle in the gutter */
.gutter-note {
  opacity: 0.12;
}

[data-para-index]:hover .gutter-note {
  opacity: 0.35;
}

/* Notes — right-aligned, marginalia style */
.para-note {
  margin: 4px 0 14px;
  margin-left: var(--note-indent);
  padding: 8px 12px;
  background: var(--accent-soft);
  border-radius: 4px;
  border-left: 2px solid var(--accent);
  font-size: 0.82rem;
}

@media (min-width: 800px) {
  .para-note {
    margin-left: var(--note-indent);
    margin-right: 0;
    text-align: right;
  }

  .para-note textarea {
    text-align: left;
  }
}

.para-note.collapsed { display: none; }

.para-note textarea {
  width: 100%;
  background: none;
  border: none;
  font-family: var(--font-body);
  font-size: 0.8rem;
  line-height: 1.5;
  color: var(--text);
  resize: none;
  min-height: 32px;
  outline: none;
}

.para-note textarea::placeholder {
  color: var(--text-secondary);
  opacity: 0.5;
  font-style: italic;
}

/* --- Audio Bar --- */

#play-btn {
  background: none;
  border: 1.5px solid var(--text-secondary);
  border-radius: 50%;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s, opacity 0.2s;
  flex-shrink: 0;
}

#play-btn:hover:not(:disabled) {
  color: var(--accent);
  border-color: var(--accent);
}

#play-btn:disabled {
  opacity: 0.4;
  cursor: default;
}

#play-btn .icon-play,
#play-btn .icon-pause { pointer-events: none; }

#time-current, #time-total {
  font-size: 0.72rem;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  min-width: 36px;
  text-align: center;
  flex-shrink: 0;
}

#progress-track {
  flex: 1;
  height: 28px;
  position: relative;
  display: flex;
  align-items: center;
}

#progress-fill {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  height: 3px;
  background: var(--accent);
  border-radius: 2px;
  pointer-events: none;
  width: 0%;
  transition: width 0.1s linear;
}

#progress-track::before {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  height: 3px;
  background: var(--border);
  border-radius: 2px;
}

#progress-input {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 28px;
  background: transparent;
  cursor: pointer;
  position: relative;
  z-index: 1;
}

#progress-input::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg-audio);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  transition: transform 0.15s ease;
  margin-top: -4.5px; /* center on 3px track */
}

#progress-input::-webkit-slider-thumb:hover {
  transform: scale(1.3);
}

#progress-input::-webkit-slider-runnable-track {
  height: 3px;
  background: transparent;
}

/* Listen history ranges on progress bar */
.history-range {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  height: 3px;
  background: var(--accent);
  opacity: 0.25;
  border-radius: 2px;
  pointer-events: none;
  z-index: 0;
}

#speed-select {
  -webkit-appearance: none;
  appearance: none;
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 8px;
  font-family: var(--font-body);
  font-size: 0.72rem;
  color: var(--text-secondary);
  cursor: pointer;
  flex-shrink: 0;
  text-align: center;
  text-align-last: center;
  transition: color 0.2s, border-color 0.2s;
}

#speed-select:hover {
  color: var(--accent);
  border-color: var(--accent);
}

/* --- Chat Panel --- */

#chat-panel {
  grid-column: 3;
  grid-row: 1;
  background: var(--bg-sidebar);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.chat-open #chat-panel {
  opacity: 1;
  pointer-events: auto;
}

#chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-title {
  font-family: var(--font-body);
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-heading);
  letter-spacing: 0.02em;
}

#model-select {
  -webkit-appearance: none;
  appearance: none;
  background: var(--accent-soft);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 3px 6px;
  font-family: var(--font-body);
  font-size: 0.68rem;
  color: var(--text-secondary);
  cursor: pointer;
  margin-left: auto;
  margin-right: 8px;
}

#model-select:hover { border-color: var(--accent); color: var(--accent); }

#chat-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: color 0.1s;
}

#chat-close:hover { color: var(--text); }

/* Messages area */
#chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Empty state */
#chat-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  text-align: center;
  color: var(--text-secondary);
  gap: 16px;
}

#chat-empty .chat-empty-icon {
  font-size: 2.5rem;
  opacity: 0.25;
  line-height: 1;
}

#chat-empty p {
  font-family: var(--font-body);
  font-size: 0.95rem;
  font-style: italic;
  opacity: 0.6;
}

.chat-suggestions {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
  max-width: 260px;
}

.chat-suggestion {
  background: var(--accent-soft);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  font-family: var(--font-body);
  font-size: 0.76rem;
  color: var(--text-secondary);
  cursor: pointer;
  text-align: left;
  transition: color 0.1s, border-color 0.1s;
}

.chat-suggestion:hover {
  color: var(--accent);
  border-color: var(--accent);
}

/* User messages */
.chat-msg-user {
  background: var(--accent-soft);
  border: 1px solid var(--border);
  border-radius: 12px 12px 4px 12px;
  padding: 10px 14px;
  margin-left: 32px;
  font-family: var(--font-body);
  font-size: 0.84rem;
  line-height: 1.55;
  color: var(--text);
  align-self: flex-end;
}

/* AI messages */
.chat-msg-ai {
  border-left: 2px solid var(--red);
  padding: 4px 8px 4px 14px;
  margin-right: 24px;
  font-family: var(--font-body);
  font-size: 0.84rem;
  line-height: 1.7;
  color: var(--text);
}

.chat-msg-ai p { margin-bottom: 0.6em; }
.chat-msg-ai p:last-child { margin-bottom: 0; }

/* Citations in AI messages */
.citation-link {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px dotted var(--accent);
  cursor: pointer;
  font-size: 0.78em;
  font-weight: 500;
  white-space: nowrap;
  transition: border-color 0.15s;
}

.citation-link:hover {
  border-bottom-style: solid;
}

/* Streaming cursor */
.streaming-cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--accent);
  margin-left: 2px;
  animation: cursorBlink 1s ease-in-out infinite;
  vertical-align: text-bottom;
}

@keyframes cursorBlink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.15; }
}

/* Highlighted passage (when citation is clicked) */
.citation-highlight {
  background: var(--highlight-word) !important;
  border-radius: 3px;
  box-shadow: 0 0 0 4px var(--highlight-word);
  transition: background 0.3s ease, box-shadow 0.3s ease;
}

.citation-highlight-fade {
  background: transparent !important;
  box-shadow: 0 0 0 4px transparent;
  transition: background 1.5s ease, box-shadow 1.5s ease;
}

/* Input area */
#chat-input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#chat-input {
  flex: 1;
  background: var(--bg-reader);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  font-family: var(--font-body);
  font-size: 0.84rem;
  line-height: 1.5;
  color: var(--text);
  resize: none;
  max-height: 120px;
  overflow-y: auto;
  transition: border-color 0.15s;
}

#chat-input:focus {
  outline: none;
  border-color: var(--accent);
}

#chat-input::placeholder {
  color: var(--text-secondary);
  opacity: 0.5;
  font-style: italic;
}

#chat-send {
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 8px;
  color: var(--text-secondary);
  cursor: pointer;
  flex-shrink: 0;
  transition: color 0.1s, border-color 0.1s;
}

#chat-send:hover:not(:disabled) {
  color: var(--accent);
  border-color: var(--accent);
}

#chat-send:disabled {
  opacity: 0.3;
  cursor: default;
}

/* Chat toggle FAB — bright gold so it stands out */
#chat-toggle {
  position: fixed;
  bottom: calc(var(--audio-h) + 16px);
  right: 16px;
  z-index: 15;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: #D4A438;
  border: none;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-md);
  transition: transform 0.15s, box-shadow 0.15s;
}

#chat-toggle:hover {
  transform: scale(1.08);
  box-shadow: 0 4px 16px rgba(184, 134, 11, 0.3);
}

.chat-open #chat-toggle { display: none; }

/* --- Responsive --- */

@media (max-width: 800px) {
  #app, #app.chat-open {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr var(--audio-h);
  }

  /* Sidebar overlay with backdrop */
  #sidebar {
    position: fixed;
    inset: 0;
    width: 300px;
    transform: translateX(-100%);
    box-shadow: var(--shadow-md);
    z-index: 50;
    transition: transform 0.25s ease;
  }

  #sidebar.open {
    transform: translateX(0);
  }

  /* Backdrop scrim */
  #sidebar.open::after {
    content: '';
    position: fixed;
    top: 0;
    left: 300px;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
    z-index: -1;
  }

  #sidebar-toggle {
    display: flex;
    top: 14px;
    left: 14px;
    z-index: 30;
  }

  /* Reader fills the screen */
  #reader {
    grid-column: 1;
    grid-row: 1 / 3;
  }

  #audio-bar {
    grid-column: 1;
    grid-row: 3;
    z-index: 20;
  }

  /* Content padding — clear hamburger at top, gutter on left, audio bar at bottom */
  #chapter-content {
    padding: 56px 24px 160px 48px;
    max-width: none;
  }

  /* Hide word hover effect — no hover on touch */
  .word:hover { background: none; }

  /* Paragraph tap target */
  #chapter-content p {
    -webkit-tap-highlight-color: var(--accent-soft);
    border-radius: 4px;
  }

  #chapter-content p:active {
    background: var(--accent-soft);
  }

  /* Chat panel — bottom half of screen on mobile, text visible above */
  #chat-panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: var(--audio-h);
    top: auto;
    height: 50vh;
    width: 100%;
    z-index: 40;
    grid-column: auto;
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: 12px 12px 0 0;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.1);
  }

  .chat-open #chat-panel {
    opacity: 1;
    pointer-events: auto;
  }

  /* Chat toggle — bottom right, above audio bar, gold */
  #chat-toggle {
    bottom: calc(var(--audio-h) + 12px);
    right: 12px;
    width: 44px;
    height: 44px;
    z-index: 25;
  }

  /* Speed selector smaller on mobile */
  #speed-select {
    padding: 3px 4px;
    font-size: 0.68rem;
  }

  /* Play button slightly bigger touch target */
  #play-btn {
    width: 38px;
    height: 38px;
  }

  /* Gutter — invitingly tappable on mobile */
  .para-gutter {
    left: -40px;
    top: -2px;
  }

  .gutter-btn {
    width: 34px;
    height: 34px;
    opacity: 0.3;
    background: var(--accent-soft);
    border-radius: 8px;
  }

  .gutter-btn:active {
    background: var(--accent);
    opacity: 1;
  }

  .gutter-btn svg {
    width: 20px !important;
    height: 20px !important;
  }
}

@media (max-width: 480px) {
  html { font-size: 17px; }

  body { line-height: 1.65; }

  #chapter-content {
    padding: 52px 20px 160px 44px;
  }

  #chapter-content h2 { font-size: 1.5rem; }

  #audio-bar {
    padding: 0 10px;
    gap: 6px;
  }

  #time-current, #time-total { font-size: 0.68rem; min-width: 30px; }
}

/* --- Scrollbar --- */

#reader::-webkit-scrollbar { width: 6px; }
#reader::-webkit-scrollbar-track { background: transparent; }
#reader::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}
#reader::-webkit-scrollbar-thumb:hover { background: var(--text-secondary); }

#chapter-list::-webkit-scrollbar { width: 4px; }
#chapter-list::-webkit-scrollbar-track { background: transparent; }
#chapter-list::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

/* --- Print --- */

@media print {
  #sidebar, #audio-bar, #sidebar-toggle { display: none; }
  #app { display: block; }
  #chapter-content { max-width: none; padding: 0; }
}
