:root {
  color-scheme: light;
  --paper: #f3efe7;
  --surface: #fffdf9;
  --surface-raised: rgba(255, 253, 249, 0.88);
  --ink: #101d38;
  --muted: #667085;
  --accent: #a47417;
  --accent-strong: #7d5510;
  --line: rgba(16, 29, 56, 0.14);
  --overlay: rgba(10, 18, 35, 0.46);
  --shadow: 0 20px 70px rgba(26, 34, 49, 0.14);
  --topbar-height: 72px;
  --controls-height: 76px;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --paper: #0d1422;
  --surface: #151e2e;
  --surface-raised: rgba(21, 30, 46, 0.9);
  --ink: #f1eee8;
  --muted: #aeb7c7;
  --accent: #d1a84e;
  --accent-strong: #e5c878;
  --line: rgba(230, 235, 244, 0.14);
  --overlay: rgba(0, 0, 0, 0.68);
  --shadow: 0 20px 70px rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; }

html,
body {
  margin: 0;
  min-width: 320px;
  min-height: 100%;
  overscroll-behavior: none;
}

body {
  min-height: 100dvh;
  overflow: hidden;
  color: var(--ink);
  background:
    radial-gradient(circle at 18% -10%, color-mix(in srgb, var(--accent) 13%, transparent), transparent 37%),
    var(--paper);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button,
input,
select,
textarea { font: inherit; font-size: max(16px, 1em); }

button { touch-action: manipulation; }

button:focus-visible,
[tabindex]:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 70%, white);
  outline-offset: 2px;
}

[hidden] { display: none !important; }

.app-shell {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  width: 100%;
  min-height: 100dvh;
  height: 100dvh;
  padding:
    env(safe-area-inset-top)
    max(14px, env(safe-area-inset-right))
    env(safe-area-inset-bottom)
    max(14px, env(safe-area-inset-left));
}

.topbar,
.controls {
  position: relative;
  z-index: 10;
  display: grid;
  align-items: center;
  gap: 12px;
  width: min(100%, 1180px);
  margin: 0 auto;
}

.topbar {
  grid-template-columns: 48px minmax(0, 1fr) auto;
  min-height: var(--topbar-height);
  border-bottom: 1px solid var(--line);
}

.top-actions { display: flex; gap: 8px; }

.book-heading { min-width: 0; padding-inline: 6px; text-align: center; }

.book-heading h1 {
  margin: 2px 0 0;
  overflow: hidden;
  color: var(--ink);
  font: 600 clamp(1rem, 3vw, 1.2rem)/1.2 Georgia, "Times New Roman", serif;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.eyebrow,
.kicker {
  color: var(--accent-strong);
  font-size: .68rem;
  font-weight: 800;
  letter-spacing: .15em;
  text-transform: uppercase;
}

.icon-button,
.nav-button,
.primary-button {
  min-width: 44px;
  min-height: 44px;
  border: 1px solid var(--line);
  color: var(--ink);
  background: var(--surface-raised);
  cursor: pointer;
  transition: background-color 160ms ease, border-color 160ms ease, transform 160ms ease;
}

.icon-button { display: inline-grid; padding: 0; border-radius: 999px; font-size: 1.25rem; place-items: center; }

.icon-button:hover,
.nav-button:hover,
.primary-button:hover {
  border-color: color-mix(in srgb, var(--accent) 50%, var(--line));
  background: color-mix(in srgb, var(--surface) 88%, var(--accent));
}

.icon-button:active,
.nav-button:active,
.primary-button:active { transform: scale(.97); }

button:disabled { cursor: default; opacity: .42; }

.reader-stage { position: relative; display: grid; min-height: 0; padding: clamp(12px, 2.4vw, 28px) 0; place-items: stretch center; }

.viewer {
  position: relative;
  z-index: 1;
  width: min(100%, 920px);
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 5px 24px 24px 5px;
  box-shadow: var(--shadow), inset 8px 0 22px rgba(35, 37, 44, 0.04);
  background: var(--surface);
  opacity: 0;
  transition: opacity 220ms ease;
  touch-action: pan-y;
}

.viewer.is-ready { opacity: 1; }
.viewer iframe { border: 0 !important; }

.reader-state {
  position: absolute;
  z-index: 2;
  inset: clamp(12px, 2.4vw, 28px) 0;
  display: grid;
  width: min(100%, 920px);
  min-height: 0;
  margin: auto;
  padding: clamp(36px, 8vw, 82px) clamp(28px, 8vw, 72px);
  border: 1px solid var(--line);
  border-radius: 5px 24px 24px 5px;
  box-shadow: var(--shadow);
  background: var(--surface);
  text-align: center;
  align-content: center;
  justify-items: center;
}

.reader-state h2 { max-width: 16ch; margin: 20px auto 12px; font: 500 clamp(2rem, 6vw, 4.25rem)/1 Georgia, "Times New Roman", serif; letter-spacing: -.035em; }
.reader-state p:not(.kicker) { max-width: 42ch; margin: 0 auto; color: var(--muted); line-height: 1.65; }

.book-mark {
  display: grid;
  width: 64px;
  height: 78px;
  margin-bottom: 28px;
  border: 1px solid color-mix(in srgb, var(--accent) 45%, var(--line));
  border-radius: 3px 10px 10px 3px;
  color: var(--accent-strong);
  background: color-mix(in srgb, var(--surface) 90%, var(--accent));
  box-shadow: 5px 5px 0 color-mix(in srgb, var(--accent) 20%, transparent);
  font: 600 1.2rem Georgia, serif;
  place-items: center;
}

.loader { width: 40px; height: 2px; margin-top: 34px; overflow: hidden; border-radius: 99px; background: var(--line); }
.loader::after { display: block; width: 45%; height: 100%; border-radius: inherit; background: var(--accent); animation: loading 1.2s ease-in-out infinite alternate; content: ""; }
@keyframes loading { to { transform: translateX(125%); } }

.error-state .book-mark { color: #b42318; }
.error-state .primary-button { margin-top: 26px; }

.controls {
  grid-template-columns: minmax(44px, auto) minmax(120px, 1fr) minmax(44px, auto);
  min-height: var(--controls-height);
  border-top: 1px solid var(--line);
}

.nav-button { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 0 18px; border-radius: 999px; font-size: .88rem; font-weight: 650; }

.progress-block { display: grid; gap: 8px; width: min(100%, 420px); margin: auto; color: var(--muted); font-size: .72rem; }
.position-line { display: flex; min-width: 0; justify-content: space-between; gap: 12px; }
#section-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#progress-label { flex: 0 0 auto; font-variant-numeric: tabular-nums; }

.progress-track { width: 100%; height: 3px; overflow: hidden; border-radius: 99px; background: var(--line); }
.progress-track i { display: block; width: 0; height: 100%; border-radius: inherit; background: var(--accent); transition: width 220ms ease; }

.drawer-overlay { position: fixed; z-index: 30; inset: 0; background: var(--overlay); backdrop-filter: blur(2px); }

.toc-drawer {
  position: fixed;
  z-index: 40;
  inset: 0 auto 0 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  width: min(88vw, 390px);
  padding: max(18px, env(safe-area-inset-top)) 18px max(18px, env(safe-area-inset-bottom));
  border-right: 1px solid var(--line);
  box-shadow: 24px 0 80px rgba(0, 0, 0, .2);
  background: var(--surface);
  transform: translateX(-105%);
  transition: transform 220ms cubic-bezier(.2, .75, .25, 1);
}

.toc-drawer.is-open { transform: translateX(0); }
.drawer-header { display: flex; min-height: 70px; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--line); }
.drawer-header h2 { margin: 3px 0 0; font: 500 1.8rem Georgia, serif; }

.toc-list { overflow: auto; padding: 16px 2px 24px; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.toc-list ol { margin: 0; padding: 0; list-style: none; }
.toc-list ol ol { margin-left: 14px; border-left: 1px solid var(--line); }
.toc-list li { margin: 2px 0; }

.toc-item { display: block; width: 100%; min-height: 44px; padding: 11px 12px; border: 0; border-radius: 9px; color: var(--muted); background: transparent; text-align: left; line-height: 1.35; cursor: pointer; }
.toc-item:hover { color: var(--ink); background: color-mix(in srgb, var(--accent) 9%, transparent); }
.toc-item.is-current { color: var(--ink); background: color-mix(in srgb, var(--accent) 15%, transparent); font-weight: 700; }
.toc-loading { color: var(--muted); }

.drawer-footer { padding-top: 18px; border-top: 1px solid var(--line); }
.drawer-footer p { margin: 0 0 3px; font: 600 1rem Georgia, serif; }
.drawer-footer span { color: var(--muted); font-size: .72rem; }

.install-dialog { width: min(calc(100% - 32px), 430px); padding: 34px; border: 1px solid var(--line); border-radius: 22px; color: var(--ink); box-shadow: var(--shadow); background: var(--surface); }
.install-dialog::backdrop { background: var(--overlay); backdrop-filter: blur(3px); }
.install-dialog h2 { margin: 8px 0 12px; font: 500 2rem Georgia, serif; }
.install-dialog p { margin: 0 0 24px; color: var(--muted); line-height: 1.6; }
.dialog-close { position: absolute; top: 10px; right: 12px; min-width: 44px; min-height: 44px; border: 0; color: var(--muted); background: transparent; font-size: 1.5rem; cursor: pointer; }

.primary-button { padding: 0 22px; border-radius: 999px; color: #fff; background: var(--ink); font-weight: 700; }

.sr-only { position: absolute !important; width: 1px !important; height: 1px !important; padding: 0 !important; margin: -1px !important; overflow: hidden !important; clip: rect(0, 0, 0, 0) !important; white-space: nowrap !important; border: 0 !important; }

@media (max-width: 640px) {
  :root { --topbar-height: 64px; --controls-height: 70px; }
  .app-shell { padding-inline: 0; }
  .topbar,
  .controls { padding-inline: max(10px, env(safe-area-inset-left)); }
  .topbar { grid-template-columns: 44px minmax(0, 1fr) auto; gap: 7px; }
  .top-actions { gap: 5px; }
  .reader-stage { padding: 0; }
  .viewer,
  .reader-state { inset-block: 0; border-width: 0; border-radius: 0; box-shadow: none; }
  .nav-button { padding: 0 14px; }
  .nav-label { display: none; }
  .progress-block { padding-inline: 3px; }
  .book-heading .eyebrow { display: none; }
}

@media (max-height: 540px) and (orientation: landscape) {
  :root { --topbar-height: 52px; --controls-height: 56px; }
  .reader-stage { padding-block: 4px; }
  .topbar .eyebrow { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
