:root {
  --bg: #f4f1ea;
  --paper: #fbf9f4;
  --ink: #2a2724;
  --muted: #8a8175;
  --accent: #7c4dff;
  --accent2: #00a6a6;
  --line: #e4ddcf;
  --rail: #ffffff;
  --hl: #fff2b8;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 8px 24px rgba(0,0,0,.06);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: Georgia, 'Iowan Old Style', 'Times New Roman', serif;
  background: var(--bg); color: var(--ink);
}
.muted { color: var(--muted); }
.small { font-size: .82rem; }

#app {
  display: grid;
  grid-template-columns: 280px 1fr 360px;
  grid-template-rows: 1fr 72px;
  grid-template-areas: "toc reader rail" "player player player";
  height: 100vh;
}

/* ---- TOC ---- */
.toc { grid-area: toc; background: var(--paper); border-right: 1px solid var(--line); display: flex; flex-direction: column; overflow: hidden; min-height: 0; }
.toc-head { padding: 18px 18px 8px; flex-shrink: 0; max-height: min(340px, 42vh); overflow-y: auto; }
.toc-head h1 { font-size: 1.15rem; margin: 0 0 4px; line-height: 1.25; }
.toc-head .muted {
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical;
  overflow: hidden; font-size: .82rem; line-height: 1.4;
}
.toc-search { margin: 0 16px 8px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; font: inherit; font-size: .85rem; flex-shrink: 0; }
.toc-list { overflow-y: auto; padding: 4px 8px 20px; flex: 1; min-height: 0; }
.toc-item { display: block; width: 100%; text-align: left; border: 0; background: none; font: inherit; cursor: pointer; color: var(--ink);
  padding: 6px 10px; border-radius: 7px; font-size: .86rem; line-height: 1.3; }
.toc-item:hover { background: #efe9dc; }
.toc-item.active { background: var(--accent); color: #fff; }
.toc-item.lvl-1 { font-weight: 700; margin-top: 8px; }
.toc-item.lvl-2 { padding-left: 18px; }
.toc-item.lvl-3 { padding-left: 30px; color: var(--muted); font-size: .8rem; }
.toc-item .pg { float: right; opacity: .6; font-size: .75em; }

/* ---- Reader ---- */
.reader { grid-area: reader; overflow-y: auto; scroll-behavior: smooth; }
.content { max-width: 720px; margin: 0 auto; padding: 56px 40px 120px; }
.content h1 { font-size: 2rem; margin: 1.4em 0 .3em; }
.content h2 { font-size: 1.5rem; margin: 1.6em 0 .3em; border-bottom: 1px solid var(--line); padding-bottom: .2em; }
.content h3 { font-size: 1.18rem; margin: 1.3em 0 .2em; color: #4a463f; }
.content p, .content li { font-size: 1.08rem; line-height: 1.75; text-align: justify; }
.content ul { padding-left: 1.3em; }
.sec { scroll-margin-top: 20px; }
.sec .pageno { font-size: .72rem; color: var(--muted); letter-spacing: .04em; text-transform: uppercase; margin: 2.2em 0 .4em; border-top: 1px dashed var(--line); padding-top: .6em; }
.spk { cursor: pointer; border-radius: 4px; transition: background .15s; }
.spk:hover { background: #f0ead9; }
.spk.speaking { background: var(--hl); box-shadow: 0 0 0 2px var(--hl); }

/* ---- Right rail ---- */
.rail { grid-area: rail; background: var(--rail); border-left: 1px solid var(--line); display: flex; flex-direction: column; overflow: hidden; }
.rail-tabs { display: flex; border-bottom: 1px solid var(--line); }
.tab { flex: 1; padding: 12px; border: 0; background: none; font: inherit; font-size: .92rem; cursor: pointer; color: var(--muted); }
.tab.active { color: var(--accent); box-shadow: inset 0 -2px 0 var(--accent); font-weight: 700; }
.tab-panel { display: none; flex: 1; min-height: 0; flex-direction: column; }
.tab-panel.active { display: flex; }

.chat-log { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.msg { padding: 10px 13px; border-radius: 12px; max-width: 92%; font-size: .95rem; line-height: 1.5; white-space: pre-wrap; }
.msg.bot { background: #f1eee7; align-self: flex-start; border-bottom-left-radius: 3px; }
.msg.user { background: var(--accent); color: #fff; align-self: flex-end; border-bottom-right-radius: 3px; }
.msg .src { display: block; margin-top: 8px; font-size: .76rem; color: var(--muted); }
.msg.user .src { color: #e8defff; }
.msg.thinking { opacity: .6; font-style: italic; }
.chat-form { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--line); }
.chat-form textarea { flex: 1; resize: none; border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px; font: inherit; font-size: .92rem; }
.chat-form button { border: 0; background: var(--accent); color: #fff; border-radius: 10px; padding: 0 16px; font: inherit; cursor: pointer; font-weight: 600; }
.chat-form button:disabled { opacity: .5; cursor: default; }

.voice-box { padding: 18px; display: flex; flex-direction: column; gap: 10px; height: 100%; }
.voice-status { font-size: .95rem; color: var(--muted); }
.voice-transcript { flex: 1; overflow-y: auto; font-size: .92rem; line-height: 1.55; display: flex; flex-direction: column; gap: 8px; }
.voice-transcript .u { color: var(--accent); }
.voice-transcript .b { color: var(--ink); }
.voice-controls { display: flex; flex-direction: column; gap: 6px; }
.voice-btn { padding: 13px; border: 1px solid var(--line); background: #fff; border-radius: 12px; font: inherit; font-size: .98rem; cursor: pointer; font-weight: 600; }
.voice-btn.primary { background: linear-gradient(135deg, var(--accent), var(--accent2)); color: #fff; border: 0; }
.voice-btn.recording { background: #ff3b3b; color: #fff; border: 0; animation: pulse 1s infinite; }
.voice-btn.live-on { background: #ff3b3b; color: #fff; border: 0; }
@keyframes pulse { 0%,100%{ box-shadow: 0 0 0 0 rgba(255,59,59,.5);} 50%{ box-shadow: 0 0 0 10px rgba(255,59,59,0);} }
.voice-controls hr { border: 0; border-top: 1px solid var(--line); width: 100%; margin: 6px 0; }

/* ---- Player ---- */
.player { grid-area: player; background: #2a2724; color: #f4f1ea; display: flex; align-items: center; gap: 18px; padding: 0 20px; box-shadow: var(--shadow); }
.player-left { display: flex; gap: 8px; }
.player button { background: #423d37; color: #f4f1ea; border: 0; width: 40px; height: 40px; border-radius: 50%; font-size: 1rem; cursor: pointer; }
.player button.play { width: 50px; height: 50px; font-size: 1.3rem; background: var(--accent); }
.player button:hover { filter: brightness(1.15); }
.player-mid { flex: 1; min-width: 0; }
.now-playing { font-size: .88rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 6px; }
.bar { height: 5px; background: #4a443d; border-radius: 3px; overflow: hidden; }
.bar-fill { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), var(--accent2)); transition: width .2s; }
.player-lang, .player-right { display: flex; gap: 14px; }
.vlabel { font-size: .7rem; color: #c9c1b4; display: flex; flex-direction: column; gap: 3px; font-family: system-ui, sans-serif; }
.vlabel select { background: #423d37; color: #f4f1ea; border: 0; border-radius: 6px; padding: 4px 6px; font: inherit; font-size: .8rem; }

/* Elements that only matter on mobile — hidden on desktop. */
.topbar, .backdrop, .rail-close { display: none; }
.icon-btn { background: none; border: 0; font-size: 1.5rem; line-height: 1; cursor: pointer; color: var(--ink); padding: 6px 10px; border-radius: 8px; }
.icon-btn:active { background: rgba(0,0,0,.08); }

/* Tablet: drop the TOC column, keep reader + rail side by side. */
@media (max-width: 1000px) and (min-width: 861px) {
  #app { grid-template-columns: 1fr 320px; grid-template-areas: "reader rail" "player player"; }
  .toc { display: none; }
}

/* Phone layout */
@media (max-width: 860px) {
  #app { display: block; height: 100dvh; }

  .topbar {
    display: flex; align-items: center; gap: 8px;
    position: fixed; top: 0; left: 0; right: 0; height: 52px; z-index: 40;
    background: var(--paper); border-bottom: 1px solid var(--line); padding: 0 6px;
  }
  .topbar-title { flex: 1; text-align: center; font-weight: 700; font-size: 1rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  .reader { position: fixed; top: 52px; bottom: 60px; left: 0; right: 0; overflow-y: auto; }
  .content { padding: 22px 18px 50px; }
  .content h1 { font-size: 1.6rem; } .content h2 { font-size: 1.3rem; } .content h3 { font-size: 1.12rem; }
  .content p, .content li { font-size: 1.06rem; }

  /* TOC as a left slide-in drawer */
  .toc { display: flex; position: fixed; top: 0; bottom: 0; left: 0; width: 84vw; max-width: 330px; z-index: 60;
    transform: translateX(-100%); transition: transform .25s ease; }
  .toc.open { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.35); }

  /* Rail (chat/voice) as a bottom sheet */
  .rail { position: fixed; left: 0; right: 0; bottom: 0; height: 82dvh; z-index: 60;
    border-left: 0; border-top: 1px solid var(--line); border-radius: 18px 18px 0 0;
    transform: translateY(100%); transition: transform .28s ease; }
  .rail.open { transform: none; box-shadow: 0 -12px 50px rgba(0,0,0,.35); }
  .rail-close { display: block; position: absolute; top: 8px; right: 10px; z-index: 2;
    background: #f1eee7; border: 0; width: 32px; height: 32px; border-radius: 50%; font-size: 1rem; cursor: pointer; }

  .backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 50; opacity: 0; pointer-events: none; transition: opacity .25s; }
  .backdrop.show { display: block; opacity: 1; pointer-events: auto; }

  /* Compact bottom player */
  .player { position: fixed; bottom: 0; left: 0; right: 0; height: 60px; z-index: 45; gap: 10px; padding: 0 10px; }
  .player button { width: 36px; height: 36px; }
  .player button.play { width: 44px; height: 44px; font-size: 1.15rem; }
  .player-mid .bar { display: none; }
  .now-playing { font-size: .8rem; }
  /* Language stays visible in the player bar; voice stays in the ⚙ popover. */
  .player-lang { flex-direction: row; align-items: center; gap: 6px; flex-shrink: 0; }
  .player-lang .lang-label { display: none; }
  .player-lang::before { content: '🌐'; font-size: 1rem; line-height: 1; }
  .player-lang select { max-width: 108px; padding: 6px 4px; font-size: .72rem; }
  .player-right { gap: 8px; }
}

/* Settings gear: desktop hides it (controls are inline); mobile shows it. */
.settings-btn { display: none; }

@media (max-width: 860px) {
  .settings-btn { display: inline-flex; align-items: center; justify-content: center; }
  /* On mobile only Voice lives in the ⚙ popover; Language is always in the player bar. */
  .player-right {
    display: flex; flex-direction: column; gap: 12px;
    position: fixed; right: 8px; bottom: 70px; z-index: 47;
    background: #2a2724; padding: 14px; border-radius: 14px; min-width: 200px;
    box-shadow: 0 10px 40px rgba(0,0,0,.45);
    transform: translateY(12px); opacity: 0; pointer-events: none; transition: .2s;
  }
  .player-right.open { transform: none; opacity: 1; pointer-events: auto; }
  .player-right .player-voice { flex-direction: row; align-items: center; justify-content: space-between; gap: 12px; width: 100%; }
  .player-right .player-voice span { display: inline; font-size: .8rem; }
  .player-right .player-voice select { flex: 1; max-width: 60%; padding: 7px 8px; }
}

/* Resume toast */
.resume-toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: 78px; z-index: 70;
  background: var(--ink); color: #f4f1ea; border-radius: 12px; padding: 10px 12px;
  display: flex; align-items: center; gap: 10px; box-shadow: var(--shadow); max-width: 92vw;
  font-family: system-ui, sans-serif; font-size: .9rem;
}
.resume-toast button { border: 0; border-radius: 8px; padding: 7px 12px; font: inherit; font-weight: 600; cursor: pointer; }
.resume-toast .go { background: var(--accent); color: #fff; }
.resume-toast .dismiss { background: transparent; color: #c9c1b4; padding: 7px 6px; }
.resume-toast .label { opacity: .9; }

/* ---- Landing / library ---- */
body.landing { height: auto; min-height: 100%; }
.lib { max-width: 980px; margin: 0 auto; padding: 48px 24px 80px; }
.lib-head { text-align: center; margin-bottom: 36px; }
.lib-head h1 { font-size: 2rem; margin: 0 0 8px; }
.lib-head p { margin: 0; font-size: 1.02rem; }
.author-spot { margin-top: 28px; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.author-photo { width: 120px; height: 120px; border-radius: 50%; object-fit: cover; object-position: center 20%;
  box-shadow: 0 4px 16px rgba(0,0,0,.15); border: 3px solid #fff; }
.author-name { margin: 0; font-size: 1.05rem; font-weight: 600; }
.author-native { font-weight: 400; color: var(--muted); }
.author-row { display: flex; align-items: center; gap: 10px; margin: 8px 0 4px; }
.author-thumb { width: 44px; height: 44px; border-radius: 50%; object-fit: cover; object-position: center 20%;
  flex-shrink: 0; box-shadow: 0 1px 6px rgba(0,0,0,.12); }
.author-label { font-size: .82rem; color: var(--muted); line-height: 1.3; }
.book-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 24px; }
.book-card { display: flex; flex-direction: column; gap: 14px; align-items: center; text-align: center;
  text-decoration: none; color: var(--ink);
  background: var(--paper); border: 1px solid var(--line); border-radius: 14px; padding: 20px 18px 18px; box-shadow: var(--shadow);
  transition: transform .12s ease, box-shadow .12s ease; }
.book-card:hover { transform: translateY(-3px); box-shadow: 0 6px 14px rgba(0,0,0,.12), 0 18px 40px rgba(0,0,0,.10); }
.book-cover { width: 100%; max-width: 150px; aspect-ratio: 5 / 7; border-radius: 8px; display: grid; place-items: center;
  font-family: Georgia, serif; font-size: 2.4rem; font-weight: 700; color: #fff;
  background: linear-gradient(150deg, var(--accent), var(--accent2)); box-shadow: inset 0 0 0 1px rgba(255,255,255,.15); }
.book-cover.has-img { padding: 0; overflow: hidden; background: #1a2744;
  box-shadow: 0 4px 14px rgba(0,0,0,.2); }
.book-cover.has-img img { width: 100%; height: 100%; object-fit: cover; display: block; }
.toc-cover { width: 100%; max-width: 140px; margin: 0 auto 12px; border-radius: 8px; display: block;
  box-shadow: 0 2px 10px rgba(0,0,0,.15); }
.book-info h2 { font-size: 1.1rem; margin: 0 0 4px; line-height: 1.25; }
.book-info .book-sub { margin: 0 0 4px; font-size: .9rem; color: var(--muted); }
.book-info .book-author { margin: 0 0 6px; font-size: .9rem; }
.book-info .small { margin: 0; }

/* "All books" link in the reader TOC */
.home-link { display: inline-block; margin-bottom: 8px; font-size: .82rem; color: var(--accent); text-decoration: none; }
.home-link:hover { text-decoration: underline; }

.original-pdf-link, .pdf-link {
  display: inline-flex; align-items: center; gap: 4px;
  font-family: system-ui, sans-serif; font-size: .82rem; font-weight: 600;
  color: var(--accent2); text-decoration: none;
}
.original-pdf-link { margin-top: 10px; padding: 7px 12px; border: 1px solid var(--line); border-radius: 8px; background: #fff; }
.original-pdf-link:hover, .pdf-link:hover { text-decoration: underline; }
.book-card-wrap { display: flex; flex-direction: column; align-items: stretch; gap: 8px; }
.book-card-wrap .pdf-link { align-self: center; margin-bottom: 4px; }

/* Developer credit */
.dev-credit {
  margin: 0; font-family: system-ui, sans-serif; font-size: .72rem; letter-spacing: .02em;
  color: var(--muted); opacity: .85;
}
.lib-foot {
  text-align: center; margin-top: 48px; padding-top: 24px; border-top: 1px solid var(--line);
}
.toc-foot {
  flex-shrink: 0; padding: 12px 18px 16px; border-top: 1px solid var(--line);
}
.topbar-credit, .player-credit { display: none; }
@media (max-width: 1000px) {
  .player-credit { display: block; color: #a89f92; white-space: nowrap; font-size: .68rem; }
}
@media (max-width: 860px) {
  .player-credit { display: none; }
  .topbar { flex-wrap: wrap; height: auto; min-height: 52px; padding-bottom: 4px; }
  .topbar-title { order: 0; }
  .topbar-credit {
    display: block; flex: 0 0 100%; order: 3;
    text-align: center; font-size: .58rem; margin: -2px 0 0; opacity: .75;
  }
  .reader { top: 64px; }
}
