:root {
  --bg: #e8e3d8;
  --card-bg: #f9f6ef;
  --text: #2a1f10;
  --muted: #9e8c70;
  --accent: #6b4c2a;
  --border: #d4c8ae;
}

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

body {
  background: var(--bg);
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  font-family: 'Noto Serif KR', 'Nanum Myeongjo', 'Batang', Georgia, serif;
}

.reader-wrapper {
  width: 100%;
  max-width: 660px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* ── TTS floating pill (fixed near the bottom of the viewport) ── */
.tts-controls {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 999px;          /* full pill */
  box-shadow:
    0 1px 2px  rgba(0,0,0,0.06),
    0 6px 18px rgba(0,0,0,0.14),
    0 16px 40px rgba(0,0,0,0.06);
  z-index: 50;
}

.tts-controls[hidden] { display: none; }

.tts-btn {
  width: 40px;
  height: 40px;
  min-width: 40px;
  min-height: 40px;
  padding: 0;
  border: none;                  /* pill carries the visible border */
  background: transparent;
  border-radius: 50%;
  font-size: 13px;
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
}

.tts-btn:hover:not(:disabled),
.tts-btn:active:not(:disabled),
.tts-btn.is-active {
  background: var(--accent);
  color: #fff;
}

.tts-btn:disabled {
  opacity: 0.32;
  cursor: default;
}

/* Per-chapter play button inside the TOC list */
.toc-tts-btn {
  position: relative;            /* anchor for spinner */
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--accent);
  font-family: inherit;
  font-size: 10px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.toc-tts-btn:hover,
.toc-tts-btn:active,
.toc-tts-btn.is-active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* Loading spinner (shown while an API audio is being generated) */
.tts-btn,
.toc-tts-btn { position: relative; }

.tts-btn.is-loading,
.toc-tts-btn.is-loading {
  pointer-events: none;
  color: transparent !important;
}

.tts-btn.is-loading::after,
.toc-tts-btn.is-loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 14px;
  height: 14px;
  margin: -7px 0 0 -7px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: tts-spin 0.8s linear infinite;
}

@keyframes tts-spin {
  to { transform: rotate(360deg); }
}

/* Hidden audio element should not affect layout */
#ttsAudio { display: none; }

/* Pulsing dot next to the chapter title that's currently being read */
.chapter-title.is-speaking::before {
  content: '◉';
  color: var(--accent);
  margin-right: 8px;
  font-size: 0.7em;
  animation: tts-pulse 1.4s ease-in-out infinite;
}

/* TOC entry highlight when its chapter is being read */
.toc-entry.is-speaking .toc-entry-title {
  color: var(--accent);
  font-weight: 600;
}

@keyframes tts-pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.25; }
}

/* Fallback toast (e.g. when no Korean voice is installed) */
.tts-toast {
  position: fixed;
  bottom: 88px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--card-bg);
  padding: 10px 18px;
  border-radius: 4px;
  font-size: 14px;
  max-width: 88%;
  text-align: center;
  z-index: 100;
  box-shadow: 0 4px 14px rgba(0,0,0,0.2);
  animation: tts-toast-fade 3s ease-in-out forwards;
}

@keyframes tts-toast-fade {
  0%        { opacity: 0; transform: translate(-50%, 10px); }
  10%, 80%  { opacity: 1; transform: translate(-50%, 0); }
  100%      { opacity: 0; transform: translate(-50%, -10px); }
}

/* ── Book card ── */
.book-card {
  background: var(--card-bg);
  border-radius: 3px;
  box-shadow:
    0 1px 3px rgba(0,0,0,0.06),
    0 6px 20px rgba(0,0,0,0.09),
    0 20px 60px rgba(0,0,0,0.05);
  min-height: 540px;
  padding: 52px 56px;
  display: flex;
  flex-direction: column;
  transition: opacity 0.2s ease;
}

/* ── Cover page ── */
.book-card.cover {
  align-items: center;
  justify-content: center;
  text-align: center;
}

.cover-genre {
  font-size: 11px;
  letter-spacing: 0.18em;
  color: var(--muted);
  text-transform: uppercase;
  margin-bottom: 32px;
}

.cover-title {
  font-size: 26px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.5;
  margin-bottom: 14px;
}

.cover-author {
  font-size: 14px;
  color: var(--muted);
  margin-bottom: 44px;
}

.cover-divider {
  width: 44px;
  height: 1px;
  background: var(--border);
  margin: 0 auto 40px;
}

.cover-quote {
  font-size: 16px;
  line-height: 2;
  color: var(--text);
  max-width: 440px;
  font-style: italic;
}

/* ── Table of Contents ── */
.toc-header {
  font-size: 16px;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.1em;
  text-align: center;
  padding-bottom: 20px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--border);
}

.toc-list {
  display: flex;
  flex-direction: column;
}

.toc-entry {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 0;
  border-bottom: 1px solid transparent;
  cursor: pointer;
  min-height: 44px;
}

.toc-entry:not(:last-child) {
  border-bottom-color: var(--border);
}

.toc-entry:hover .toc-entry-title,
.toc-entry:active .toc-entry-title {
  color: var(--accent);
}

.toc-ch {
  font-size: 10px;
  letter-spacing: 0.08em;
  color: var(--muted);
  width: 40px;
  flex-shrink: 0;
  text-align: right;
}

.toc-entry-title {
  flex: 1;
  font-size: 14px;
  color: var(--text);
  line-height: 1.45;
  transition: color 0.12s;
}

.toc-entry-title.is-front {
  font-style: italic;
  color: var(--muted);
}

.toc-page-num {
  font-size: 12px;
  color: var(--muted);
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
}

.toc-separator {
  height: 1px;
  background: var(--border);
  margin: 6px 0;
}

/* ── Chapter first page ── */
.book-card.chapter-first .chapter-header {
  text-align: center;
  padding-bottom: 28px;
  margin-bottom: 36px;
  border-bottom: 1px solid var(--border);
}

.chapter-eyebrow {
  font-size: 10px;
  letter-spacing: 0.14em;
  color: var(--muted);
  text-transform: uppercase;
  margin-bottom: 10px;
}

.book-card.chapter-first .chapter-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.55;
}

/* ── Continuation page ── */
.book-card.chapter-cont .chapter-header {
  padding-bottom: 14px;
  margin-bottom: 28px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
}

.book-card.chapter-cont .chapter-title {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.04em;
}

/* ── Page text ── */
.page-text {
  flex: 1;
  line-height: 1.97;
  color: var(--text);
  word-break: keep-all;
  overflow-wrap: break-word;
  /* Prevent text selection on swipe */
  -webkit-user-select: none;
  user-select: none;
}

.page-text p {
  margin-bottom: 1.1em;
}

.page-text p:last-child {
  margin-bottom: 0;
}

/* ── Controls ── */
.controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 4px;
}

.nav-group {
  display: flex;
  align-items: center;
  gap: 10px;
}

.font-group {
  display: flex;
  gap: 6px;
}

.btn {
  background: var(--card-bg);
  border: 1px solid var(--border);
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  /* Minimum tap target on all devices */
  min-height: 44px;
  min-width: 44px;
}

.nav-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.first-btn {
  font-size: 14px;
}

.nav-btn:hover:not(:disabled),
.nav-btn:active:not(:disabled) {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.nav-btn:disabled {
  opacity: 0.28;
  cursor: default;
}

.page-indicator {
  font-size: 12px;
  color: var(--muted);
  min-width: 88px;
  text-align: center;
  letter-spacing: 0.04em;
}

.size-btn {
  padding: 0 12px;
  height: 44px;
  border-radius: 3px;
  font-size: 12px;
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: center;
}

.size-btn:hover,
.size-btn:active {
  background: var(--border);
  color: var(--text);
}

/* ═══════════════════════════════════════════════════════════
   MOBILE — full-width, true page experience
   ═══════════════════════════════════════════════════════════ */
@media (max-width: 600px) {

  /* Lock the viewport — no body scroll on reading pages */
  body {
    padding: 0;
    height: 100vh;
    height: 100dvh;       /* dynamic viewport (hides browser chrome) */
    overflow: hidden;
    align-items: stretch; /* reader-wrapper fills full width */
  }

  .reader-wrapper {
    max-width: 100%;
    height: 100%;
    gap: 0;
  }

  /* ── Book card: fills remaining height, scrolls internally when text overflows ── */
  .book-card {
    flex: 1;
    min-height: 0;                       /* allow flex to constrain height */
    border-radius: 0;
    box-shadow: none;
    padding: 32px 22px 20px;
    overflow-y: auto;                    /* enlarged text stays reachable */
    -webkit-overflow-scrolling: touch;   /* smooth momentum scroll on iOS */
    overscroll-behavior: contain;        /* don't chain scroll to body */
  }

  /* ── TOC: same scroll behavior, slightly more bottom padding ── */
  .book-card.toc {
    padding-bottom: 8px;
  }

  /* ── Cover: keep centered, no scroll needed ── */
  .book-card.cover {
    padding: 40px 24px;
    justify-content: center;
    overflow: hidden;
  }

  /* ── TTS pill on mobile: above the 60 px fixed page-nav, with
        larger buttons that meet the iOS 44 × 44 tap-target guideline. */
  .tts-controls {
    bottom: 76px;                /* 60 px page-nav + 16 px gap */
    padding: 8px;
    gap: 10px;
  }

  .tts-btn {
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    font-size: 14px;
  }

  /* Reserve room at the bottom of reading pages so the floating pill
     never covers the last lines of text once the content scrolls. */
  .book-card.chapter-first,
  .book-card.chapter-cont {
    padding-bottom: 80px;
  }

  .toc-tts-btn {
    width: 30px;
    height: 30px;
  }

  .cover-genre {
    margin-bottom: 24px;
  }

  .cover-title {
    font-size: 22px;
  }

  .cover-author {
    font-size: 13px;
    margin-bottom: 32px;
  }

  .cover-divider {
    margin-bottom: 28px;
  }

  .cover-quote {
    font-size: 15px;
    max-width: 100%;
    line-height: 1.9;
  }

  /* ── Chapter first page ── */
  .book-card.chapter-first .chapter-header {
    padding-bottom: 20px;
    margin-bottom: 24px;
  }

  .book-card.chapter-first .chapter-title {
    font-size: 18px;
  }

  /* ── Chapter continuation ── */
  .book-card.chapter-cont .chapter-header {
    padding-bottom: 12px;
    margin-bottom: 20px;
  }

  /* ── TOC entries ── */
  .toc-header {
    font-size: 14px;
    padding-bottom: 16px;
    margin-bottom: 12px;
  }

  .toc-entry {
    padding: 10px 0;
    min-height: 48px;     /* generous touch target in list */
  }

  .toc-entry-title {
    font-size: 13px;
  }

  .toc-ch {
    width: 34px;
    font-size: 9px;
  }

  /* ── Controls bar: fixed-height strip at the bottom ── */
  .controls {
    flex-direction: row;  /* always stay in one row on mobile */
    flex-shrink: 0;
    height: 60px;
    padding: 0 16px;
    background: var(--card-bg);
    border-top: 1px solid var(--border);
    box-shadow: 0 -2px 8px rgba(0,0,0,0.05);
  }

  .nav-group {
    gap: 6px;
  }

  .page-indicator {
    min-width: 68px;
    font-size: 11px;
  }

  .nav-btn {
    width: 44px;
    height: 44px;
    font-size: 17px;
  }

  .size-btn {
    padding: 0 12px;
    font-size: 12px;
  }

  .font-group {
    gap: 4px;
  }
}
