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

:root {
  --ink: #1a1410;
  --paper: #f5f0e8;
  --paper-dark: #ede7d9;
  --accent: #5c4a2a;
  --gold: #c8a84b;
  --panel-bg: #fdf8f0;
  --panel-border: #d4b896;
  --muted: #6b5d4f;
  --link: #4a6741;
  --serif: 'Lora', Georgia, serif;
  --sans: 'Source Sans 3', system-ui, sans-serif;
  --max-prose: 68ch;
}

body { background: var(--paper); color: var(--ink); font-family: var(--serif); font-size: 1.125rem; line-height: 1.85; }

header { position: sticky; top: 0; z-index: 100; background: var(--accent); color: var(--paper); padding: 1rem 2rem; display: flex; align-items: center; gap: 1.5rem; transition: transform .25s ease; }
header.header-hidden { transform: translateY(-100%); }
header h1 { font-size: 1.25rem; font-weight: 600; display: flex; flex-direction: column; }
.header-chapter { font-size: .78rem; font-weight: 400; opacity: .8; font-family: var(--sans); line-height: 1.3; }

#nav-btns { display: flex; align-items: center; gap: .5rem; flex-shrink: 0; }
#back-btn, #home-btn { background: none; border: 1px solid rgba(255,255,255,.4); color: var(--paper); padding: .3rem .8rem; border-radius: 4px; cursor: pointer; font-family: var(--sans); font-size: .875rem; display: none; white-space: nowrap; }
#back-btn:hover, #home-btn:hover { background: rgba(255,255,255,.15); }

#header-controls { margin-left: auto; display: flex; align-items: center; gap: .75rem; flex-shrink: 0; }
#font-size-ctrl { display: flex; align-items: baseline; gap: .35rem; }
.fs-btn { background: none; border: none; color: rgba(255,255,255,.5); cursor: pointer; padding: .1rem .25rem; font-family: var(--serif); font-weight: 600; line-height: 1; transition: color .15s; }
.fs-btn:nth-child(1) { font-size: .8rem; }
.fs-btn:nth-child(2) { font-size: 1rem; }
.fs-btn:nth-child(3) { font-size: 1.25rem; }
.fs-btn:hover { color: rgba(255,255,255,.85); }
.fs-btn.active { color: var(--paper); border-bottom: 1px solid var(--gold); }
body.font-md { font-size: 1.3rem; }
body.font-lg { font-size: 1.5rem; }

#theme-ctrl { display: flex; align-items: center; gap: .4rem; padding-left: .75rem; border-left: 1px solid rgba(255,255,255,.25); }
.theme-btn { cursor: pointer; padding: .2rem .45rem; border-radius: 3px; font-family: var(--serif); font-size: .75rem; font-weight: 600; line-height: 1; border: 2px solid transparent; transition: border-color .15s; }
.theme-btn[data-theme=""] { background: #f5f0e8; color: #1a1410; }
.theme-btn[data-theme="theme-white"] { background: #ffffff; color: #111; }
.theme-btn:hover { border-color: rgba(255,255,255,.55); }
.theme-btn.active { border-color: var(--gold); }

@media (max-width: 480px) {
  header { padding: .6rem 1rem; gap: .75rem; }
  #nav-btns { flex-direction: column; align-items: flex-start; gap: .3rem; }
  #home-btn { order: -1; }
  #home-btn .btn-label { display: none; }
  #header-controls { flex-direction: column; align-items: flex-end; gap: .3rem; }
  #theme-ctrl { padding-left: 0; border-left: none; }
}

@media (max-width: 600px) {
  .panel-image { margin-top: .5rem; }
}

/* White theme overrides */
body.theme-white { --ink: #111; --paper: #fff; --paper-dark: #f0f0f0; --accent: #222; --panel-bg: #fafafa; --panel-border: #ccc; --muted: #555; }

main { padding: 2rem; }

/* Shelf */
#shelf { max-width: 900px; margin: 0 auto; }
#shelf h2 { font-size: 1.75rem; margin-bottom: 1.5rem; color: var(--accent); }
.book-card { background: white; border: 1px solid var(--panel-border); border-radius: 6px; padding: 1.5rem 2rem; margin-bottom: 1rem; cursor: pointer; transition: box-shadow .2s, transform .15s; }
.book-card:hover { box-shadow: 0 4px 16px rgba(0,0,0,.12); transform: translateY(-2px); }
.book-card h3 { font-size: 1.4rem; color: var(--accent); }
.book-card .meta { font-family: var(--sans); font-size: .875rem; color: var(--muted); margin: .25rem 0 .5rem; }

/* Chapter list */
#chapter-list { max-width: 700px; margin: 0 auto; }
#chapter-list h2 { font-size: 1.5rem; color: var(--accent); margin-bottom: .25rem; }
.book-meta { font-family: var(--sans); color: var(--muted); margin-bottom: 1.5rem; }
.chapter-item { display: flex; align-items: center; gap: 1rem; padding: .75rem 1rem; border: 1px solid var(--panel-border); border-radius: 4px; margin-bottom: .5rem; cursor: pointer; background: white; transition: background .15s; }
.chapter-item:hover { background: var(--paper-dark); }
.chapter-num { font-family: var(--sans); color: var(--muted); font-size: .875rem; min-width: 2rem; }

/* Reader */
#reader { max-width: 900px; margin: 0 auto; }
.reader-header { margin-bottom: 2.5rem; border-bottom: 2px solid var(--gold); padding-bottom: 1rem; }
.reader-header .book-name { font-family: var(--sans); color: var(--muted); font-size: .9rem; text-transform: uppercase; letter-spacing: .08em; }
.reader-header h2 { font-size: 2rem; color: var(--accent); margin-top: .25rem; }

.prose { max-width: var(--max-prose); }
.prose p { margin-bottom: 1.4em; }
.prose blockquote.verse {
  margin: 1.4em 0 1.4em 1.5em;
  padding-left: 1.1em;
  border-left: 2px solid var(--panel-border);
  font-style: italic;
  color: var(--muted);
  line-height: 1.7;
}

/* Enhancement triggers */
.enhance-trigger { border-bottom: 2px dotted var(--gold); cursor: pointer; font-weight: 700; border-radius: 2px; }
.enhance-trigger:hover { color: var(--accent); }
.enhance-trigger--active { background: rgba(200, 168, 75, 0.25); border-bottom-style: solid; }

/* Enhancement panels */
.enhancement-panel { background: transparent; border: none; border-top: 1px solid var(--accent); border-bottom: 1px solid var(--accent); border-radius: 0; margin: 2rem 0; overflow: visible; }
.enhancement-panel summary { padding: .6rem 0; cursor: pointer; font-family: var(--serif); font-weight: 600; font-style: italic; color: var(--accent); list-style: none; display: flex; align-items: center; gap: .6rem; }
.enhancement-panel summary::before { content: '▾'; font-size: 1.75rem; font-style: normal; color: var(--gold); transition: transform .2s; flex-shrink: 0; line-height: 1; }
.enhancement-panel[open] summary::before { transform: rotate(180deg); }
.enhancement-panel summary:hover { color: var(--ink); }

.panel-body { padding: .75rem 0 1.25rem 1.75rem; display: grid; grid-template-columns: 1fr 160px; gap: 1rem; align-items: start; border-left: 3px solid var(--gold); }
@media (max-width: 600px) { .panel-body { grid-template-columns: 1fr; } }
.panel-text p { font-family: var(--serif); line-height: 1.75; margin-bottom: .75rem; }
.panel-link a { color: var(--link); font-family: var(--sans); font-size: .875rem; }
.panel-image img { width: 160px; height: 180px; object-fit: cover; object-position: top center; border-radius: 4px; display: block; background: var(--paper-dark); -webkit-transform: translateZ(0); transform: translateZ(0); }
.panel-image figcaption { font-family: var(--sans); font-size: .75rem; color: var(--muted); margin-top: .3rem; max-width: 160px; }
.commons-link { display: block; font-size: .65rem; color: var(--muted); margin-top: .15rem; opacity: .7; }
.commons-link:hover { opacity: 1; color: var(--link); }

/* Summary */
.chapter-summary { margin-top: 3rem; padding: 2rem; background: var(--paper-dark); border: 1px solid var(--panel-border); border-radius: 6px; max-width: var(--max-prose); }
.chapter-summary h3 { font-size: 1.25rem; color: var(--accent); margin-bottom: 1rem; }
.summary-item { display: flex; gap: .75rem; margin-bottom: .9rem; font-family: var(--sans); font-size: .95rem; }
.summary-item::before { content: '◆'; color: var(--gold); flex-shrink: 0; margin-top: .2rem; font-size: .7rem; }
.summary-item a { color: var(--link); font-size: .85rem; margin-left: .4rem; }

.next-chapter { max-width: var(--max-prose); margin-top: 2.5rem; padding-top: 1.5rem; border-top: 1px solid var(--panel-border); font-family: var(--sans); }
.next-chapter a { color: var(--accent); font-size: 1rem; font-weight: 600; text-decoration: none; }
.next-chapter a:hover { text-decoration: underline; }

#loading, #error { text-align: center; padding: 4rem 2rem; font-family: var(--sans); color: var(--muted); }
#error { color: #c0392b; }

/* ── Word lookup ── */
@keyframes popup-in {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}

#def-popup {
  position: fixed;
  inset: 0;
  z-index: 600;
  pointer-events: none;
  display: none;
}

#def-popup.open { display: block; }

#def-popup-card {
  position: fixed;
  pointer-events: auto;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  box-shadow: 0 8px 32px rgba(0,0,0,.2);
  width: 320px;
  max-width: calc(100vw - 24px);
  max-height: 65vh;
  display: flex;
  flex-direction: column;
  animation: popup-in .15s ease;
}

#def-popup-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .75rem 1rem .6rem;
  border-bottom: 1px solid var(--panel-border);
  flex-shrink: 0;
}

#def-popup-word {
  font-family: var(--serif);
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--accent);
}

#def-popup-close {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--muted);
  font-size: 1.2rem;
  padding: .1rem .4rem;
  line-height: 1;
  border-radius: 4px;
  flex-shrink: 0;
}

#def-popup-close:hover { background: var(--paper-dark); color: var(--ink); }

#def-popup-body {
  overflow-y: auto;
  padding: .75rem 1rem;
  flex: 1;
}

.def-pos {
  font-family: var(--sans);
  font-size: .75rem;
  font-style: italic;
  color: var(--muted);
  margin: .6rem 0 .25rem;
  text-transform: lowercase;
}

.def-pos:first-child { margin-top: 0; }

.def-text {
  font-family: var(--serif);
  font-size: .95rem;
  line-height: 1.55;
  margin-bottom: .35rem;
}

.def-example {
  font-family: var(--serif);
  font-size: .85rem;
  font-style: italic;
  color: var(--muted);
  margin-bottom: .5rem;
  padding-left: .75rem;
  border-left: 2px solid var(--gold);
}

.def-msg { font-family: var(--sans); color: var(--muted); font-size: .9rem; }

#def-popup-foot {
  padding: .5rem 1rem .75rem;
  border-top: 1px solid var(--panel-border);
  flex-shrink: 0;
}

#def-popup-mw { font-family: var(--sans); font-size: .8rem; color: var(--link); }

/* ── Install to home screen ── */
.install-btn {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  margin-bottom: 1.5rem;
  padding: .45rem 1rem;
  background: none;
  border: 1px solid var(--accent);
  border-radius: 4px;
  color: var(--accent);
  font-family: var(--sans);
  font-size: .875rem;
  cursor: pointer;
  transition: background .15s;
}
.install-btn:hover { background: var(--paper-dark); }

#install-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 700;
  display: none;
  align-items: center;
  justify-content: center;
}
#install-modal.open { display: flex; }

#install-modal-card {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  box-shadow: 0 8px 32px rgba(0,0,0,.2);
  width: 320px;
  max-width: calc(100vw - 32px);
  animation: popup-in .15s ease;
}
#install-modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .75rem 1rem .6rem;
  border-bottom: 1px solid var(--panel-border);
}
#install-modal-title { font-family: var(--serif); font-size: 1.05rem; font-weight: 700; color: var(--accent); }
#install-modal-close { background: none; border: none; cursor: pointer; color: var(--muted); font-size: 1.2rem; padding: .1rem .4rem; border-radius: 4px; }
#install-modal-close:hover { background: var(--paper-dark); color: var(--ink); }
#install-modal-body { padding: 1rem 1.25rem 1.25rem; }
.install-steps { padding-left: 1.25rem; }
.install-steps li { font-family: var(--sans); font-size: .95rem; line-height: 1.6; margin-bottom: .5rem; color: var(--ink); }

/* ── Offline / download ── */
.download-btn {
  display: inline-block;
  margin: 1.5rem 0 .5rem;
  padding: .55rem 1.1rem;
  background: none;
  border: 1px solid var(--accent);
  border-radius: 4px;
  color: var(--accent);
  font-family: var(--sans);
  font-size: .9rem;
  cursor: pointer;
  transition: background .15s;
}
.download-btn:hover:not(:disabled) { background: var(--paper-dark); }
.download-btn--done { border-color: var(--link); color: var(--link); }
.download-btn:disabled { opacity: .7; cursor: default; }

.continue-btn {
  display: block;
  width: 100%;
  margin: .25rem 0 1.25rem;
  padding: .7rem 1.1rem;
  background: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 4px;
  color: var(--paper);
  font-family: var(--sans);
  font-size: .95rem;
  text-align: left;
  cursor: pointer;
  transition: opacity .15s;
}
.continue-btn:hover { opacity: .88; }

.chapter-item--cached .chapter-num::after { content: ' ↓'; color: var(--link); font-size: .75rem; }

.offline-legend { display: flex; align-items: center; gap: .35rem; margin-top: .5rem; font-family: var(--sans); font-size: .8rem; color: var(--muted); }
.offline-legend-dot { color: var(--link); font-size: .75rem; line-height: 1; }

.offline-badge {
  display: inline-block;
  margin-left: .6rem;
  padding: .1rem .4rem;
  background: var(--paper-dark);
  border: 1px solid var(--panel-border);
  border-radius: 3px;
  font-size: .7rem;
  font-family: var(--sans);
  color: var(--muted);
  vertical-align: middle;
  letter-spacing: .02em;
}
