@import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600;700&family=Libre+Caslon+Text:ital@0;1&display=swap');

:root {
  --bg: #f5f3ed;
  --surface: rgba(255, 255, 252, .88);
  --surface-solid: #fffefa;
  --text: #1f2926;
  --muted: #68736f;
  --line: rgba(31, 41, 38, .14);
  --accent: #254a40;
  --accent-hover: #18382f;
  --accent-text: #fffdf8;
  --gold: #d6a84f;
  --danger: #9a3c3c;
  --success: #2f6b53;
  --shadow: 0 24px 70px rgba(37, 52, 47, .11);
  --reader-font: 'Libre Caslon Text', Georgia, serif;
  --reader-size: 20px;
  --reader-line: 1.8;
}

html[data-theme='dark'] {
  --bg: #111715;
  --surface: rgba(28, 37, 34, .9);
  --surface-solid: #1b2522;
  --text: #e9ece7;
  --muted: #a4aea9;
  --line: rgba(236, 241, 236, .13);
  --accent: #e2bc73;
  --accent-hover: #f0cf8d;
  --accent-text: #17201d;
  --gold: #e2bc73;
  --danger: #f19a9a;
  --success: #8fd0ad;
  --shadow: 0 24px 80px rgba(0, 0, 0, .32);
}

html[data-theme='sepia'] {
  --bg: #e9dfca;
  --surface: rgba(244, 235, 216, .92);
  --surface-solid: #f2e8d4;
  --text: #463c30;
  --muted: #756655;
  --line: rgba(70, 60, 48, .16);
  --accent: #665038;
  --accent-hover: #4f3c29;
  --accent-text: #fffaf1;
  --gold: #a6793f;
  --shadow: 0 24px 70px rgba(69, 49, 29, .13);
}

* { box-sizing: border-box; }

html { min-height: 100%; scroll-behavior: smooth; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  background: var(--bg);
  font-family: 'DM Sans', system-ui, sans-serif;
  transition: background .25s ease, color .25s ease;
}

body[data-view='auth'] {
  color-scheme: dark;
  --bg: #171613;
  --surface: #211f1a;
  --surface-solid: #2e291f;
  --text: #e8d5a9;
  --muted: #baac90;
  --line: #4b4232;
  --accent: #c6aa74;
  --accent-hover: #d0b887;
  --accent-text: #211b12;
  --gold: #d5b97e;
  --danger: #e9a5a0;
  --success: #d5bf92;
  --shadow: 0 22px 64px rgba(0, 0, 0, .29);
  background-image: radial-gradient(ellipse at 12% 18%, #262219 0%, transparent 58%);
}

body[data-view='auth'] .ambient { display: none; }

button, input, select { font: inherit; }
button { color: inherit; }
button:focus-visible, input:focus-visible, select:focus-visible, a:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--gold) 65%, transparent);
  outline-offset: 3px;
}

[hidden] { display: none !important; }

.skip-link {
  position: fixed;
  z-index: 100;
  left: 1rem;
  top: -5rem;
  padding: .75rem 1rem;
  color: var(--accent-text);
  background: var(--accent);
  border-radius: .6rem;
}
.skip-link:focus { top: 1rem; }

.ambient {
  position: fixed;
  z-index: -1;
  width: 32rem;
  height: 32rem;
  border-radius: 50%;
  filter: blur(4px);
  pointer-events: none;
  opacity: .55;
}
.ambient-one { top: -18rem; left: -12rem; background: radial-gradient(circle, rgba(214,168,79,.26), transparent 68%); }
.ambient-two { right: -15rem; bottom: -19rem; background: radial-gradient(circle, rgba(52,110,94,.22), transparent 68%); }

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: min(1180px, calc(100% - 2rem));
  min-height: 84px;
  margin: 0 auto;
  border-bottom: 1px solid var(--line);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: .7rem;
  color: var(--text);
  font-family: 'Libre Caslon Text', Georgia, serif;
  font-size: 1.05rem;
  text-decoration: none;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 2.3rem;
  height: 2.3rem;
  color: var(--accent-text);
  background: var(--accent);
  border-radius: 50% 50% 46% 54%;
  font-style: italic;
}

.header-actions { display: flex; align-items: center; gap: .6rem; }
.member-email { color: var(--muted); font-size: .88rem; }

.button {
  min-height: 44px;
  padding: .68rem 1rem;
  border: 1px solid transparent;
  border-radius: .75rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform .15s ease, background .15s ease, border-color .15s ease;
}
.button:hover { transform: translateY(-1px); }
.button:disabled { opacity: .55; cursor: not-allowed; transform: none; }
.button-primary { color: var(--accent-text); background: var(--accent); }
.button-primary:hover { background: var(--accent-hover); }
.button-quiet { color: var(--text); background: transparent; border-color: var(--line); }
.button-quiet:hover { background: color-mix(in srgb, var(--surface-solid) 65%, transparent); }

.text-button {
  padding: .3rem;
  color: var(--accent);
  background: none;
  border: 0;
  font-weight: 600;
  cursor: pointer;
}
.text-button:hover { text-decoration: underline; text-underline-offset: .25rem; }

.auth-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.18fr) minmax(340px, .82fr);
  align-items: center;
  gap: clamp(2rem, 7vw, 7rem);
  width: min(1120px, calc(100% - 2rem));
  min-height: calc(100vh - 85px);
  margin: 0 auto;
  padding: 4rem 1.25rem 6rem;
}

.welcome-copy { max-width: 650px; }
.eyebrow {
  margin: 0 0 1rem;
  color: var(--accent);
  font-size: .78rem;
  font-weight: 700;
  letter-spacing: .16em;
  text-transform: uppercase;
}
.welcome-copy h1, .library-heading h1 {
  margin: 0;
  font-family: 'Libre Caslon Text', Georgia, serif;
  font-size: clamp(2.8rem, 6vw, 5.65rem);
  font-weight: 400;
  letter-spacing: -.055em;
  line-height: 1.02;
}
.welcome-copy h1 em { color: var(--accent); font-weight: 400; }
.welcome-lead { max-width: 590px; margin: 1.75rem 0; color: var(--muted); font-size: 1.08rem; line-height: 1.75; }
.feature-row { display: flex; flex-wrap: wrap; gap: .65rem; }
.feature-row span {
  padding: .48rem .7rem;
  color: var(--muted);
  background: color-mix(in srgb, var(--surface-solid) 72%, transparent);
  border: 1px solid var(--line);
  border-radius: 99px;
  font-size: .82rem;
}

.auth-card {
  width: 100%;
  max-width: 440px;
  justify-self: end;
  padding: 1.1rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 1.4rem;
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}
.auth-tabs { display: grid; grid-template-columns: 1fr 1fr; gap: .35rem; padding: .3rem; background: color-mix(in srgb, var(--bg) 68%, transparent); border-radius: .85rem; }
.auth-tab { min-height: 42px; border: 0; border-radius: .65rem; background: transparent; font-weight: 600; cursor: pointer; }
.auth-tab.is-active { background: var(--surface-solid); box-shadow: 0 2px 12px rgba(20,30,25,.08); }
.auth-form { display: grid; gap: 1rem; padding: 1.7rem .55rem .55rem; }
.form-heading { margin-bottom: .35rem; }
.form-heading h2 { margin: 0 0 .35rem; font-family: 'Libre Caslon Text', Georgia, serif; font-size: 1.75rem; font-weight: 400; }
.form-heading p { margin: 0; color: var(--muted); }
.auth-form label:not(.check-label), .settings-popover > label { display: grid; gap: .45rem; color: var(--muted); font-size: .88rem; font-weight: 600; }
.auth-form input:not([type='checkbox']), .settings-popover select {
  width: 100%;
  min-height: 48px;
  padding: .7rem .8rem;
  color: var(--text);
  background: color-mix(in srgb, var(--surface-solid) 88%, transparent);
  border: 1px solid var(--line);
  border-radius: .7rem;
}
.auth-form small { margin-top: -.6rem; color: var(--muted); }
.check-label { display: flex; align-items: flex-start; gap: .6rem; color: var(--muted); font-size: .82rem; line-height: 1.4; }
.check-label input { width: 1.05rem; height: 1.05rem; margin-top: .1rem; accent-color: var(--accent); }
.notice { margin: 1rem .55rem 0; padding: .8rem; color: var(--success); background: color-mix(in srgb, var(--success) 9%, transparent); border: 1px solid color-mix(in srgb, var(--success) 24%, transparent); border-radius: .7rem; font-size: .88rem; line-height: 1.45; }
.notice.is-error { color: var(--danger); background: color-mix(in srgb, var(--danger) 9%, transparent); border-color: color-mix(in srgb, var(--danger) 24%, transparent); }
.notice a { color: inherit; font-weight: 700; }

body[data-view='auth'] .auth-tab {
  color: var(--muted);
  border: 1px solid transparent;
}
body[data-view='auth'] .auth-tab:hover { color: var(--text); }
body[data-view='auth'] .auth-tab.is-active {
  color: var(--text);
  border-color: #615238;
}
body[data-view='auth'] .auth-form input:not([type='checkbox']) {
  background: #191813;
  border-color: #746346;
}

.library-layout { width: min(1120px, calc(100% - 2rem)); margin: 0 auto; padding: clamp(3rem, 8vw, 7rem) 1rem; }
.library-heading { display: flex; align-items: end; justify-content: space-between; gap: 2rem; margin-bottom: 3rem; }
.library-heading h1 { font-size: clamp(2.5rem, 5vw, 4.6rem); }
.library-note { max-width: 330px; color: var(--muted); line-height: 1.6; }
.book-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 1.4rem; }
.book-card { display: grid; grid-template-columns: 118px 1fr; gap: 1.1rem; padding: 1rem; background: var(--surface); border: 1px solid var(--line); border-radius: 1.2rem; box-shadow: 0 12px 35px rgba(30,40,35,.06); }
.book-cover { position: relative; display: flex; flex-direction: column; justify-content: space-between; min-height: 170px; padding: 1rem .85rem; overflow: hidden; color: #f8eacb; background: linear-gradient(145deg, #23453d, #172c28); border-radius: .8rem .45rem .45rem .8rem; box-shadow: 7px 8px 20px rgba(18,35,30,.18); }
.book-cover::after { content: ''; position: absolute; inset: 0; background: radial-gradient(circle at 75% 20%, rgba(226,188,115,.35), transparent 34%); }
.book-cover strong { z-index: 1; font-family: 'Libre Caslon Text', Georgia, serif; line-height: 1.15; }
.book-cover span { z-index: 1; font-size: .65rem; letter-spacing: .13em; text-transform: uppercase; }
.book-info { display: flex; flex-direction: column; align-items: flex-start; }
.book-info h2 { margin: .25rem 0 .4rem; font-family: 'Libre Caslon Text', Georgia, serif; font-size: 1.35rem; font-weight: 400; }
.book-info p { margin: 0; color: var(--muted); font-size: .87rem; line-height: 1.5; }
.book-info .button { width: 100%; margin-top: auto; }
.book-meta { margin-bottom: 1rem !important; font-size: .75rem !important; text-transform: uppercase; letter-spacing: .09em; }

.reader-shell { display: grid; grid-template-columns: 250px minmax(0, 1fr); width: min(1440px, 100%); min-height: calc(100vh - 85px); margin: 0 auto; }
.reader-sidebar { position: sticky; top: 0; align-self: start; height: calc(100vh - 85px); padding: 2rem 1.4rem; border-right: 1px solid var(--line); }
.book-mini { margin-top: 3rem; }
.mini-cover { display: grid; place-items: center; width: 92px; height: 128px; color: #e8c47f; background: linear-gradient(150deg, #294c43, #152a25); border-radius: .7rem .35rem .35rem .7rem; box-shadow: 8px 10px 24px rgba(14,30,25,.18); }
.mini-cover span { font-family: 'Libre Caslon Text', Georgia, serif; font-style: italic; }
.mini-title { margin: 1.1rem 0 .3rem; font-family: 'Libre Caslon Text', Georgia, serif; font-size: 1.15rem; }
.mini-chapter { margin: 0; color: var(--muted); font-size: .82rem; }
.progress-block { margin-top: 2.4rem; }
.progress-block > div:first-child { display: flex; justify-content: space-between; margin-bottom: .5rem; color: var(--muted); font-size: .75rem; }
.progress-track { height: 5px; overflow: hidden; background: var(--line); border-radius: 99px; }
.progress-track span { display: block; width: 0; height: 100%; background: var(--gold); transition: width .2s ease; }

.reader-main { position: relative; min-width: 0; padding: 0 clamp(1rem, 4vw, 4rem) 4rem; }
.reader-toolbar { position: sticky; z-index: 5; top: 0; display: flex; justify-content: space-between; align-items: center; max-width: 820px; min-height: 72px; margin: 0 auto; background: color-mix(in srgb, var(--bg) 88%, transparent); border-bottom: 1px solid var(--line); backdrop-filter: blur(16px); }
.segmented { display: flex; gap: .25rem; padding: .28rem; background: color-mix(in srgb, var(--surface-solid) 68%, transparent); border: 1px solid var(--line); border-radius: .7rem; }
.segmented button { min-height: 36px; padding: .4rem .7rem; color: var(--muted); background: transparent; border: 0; border-radius: .5rem; font-size: .8rem; font-weight: 600; cursor: pointer; }
.segmented button.is-active { color: var(--text); background: var(--surface-solid); box-shadow: 0 2px 8px rgba(20,30,25,.08); }
.icon-button { width: 42px; height: 42px; color: var(--text); background: var(--surface-solid); border: 1px solid var(--line); border-radius: 50%; font-family: 'Libre Caslon Text', Georgia, serif; cursor: pointer; }

.settings-popover { position: fixed; z-index: 10; top: 146px; right: max(1rem, calc((100vw - 1390px) / 2)); display: grid; gap: 1rem; width: 280px; padding: 1.1rem; background: var(--surface-solid); border: 1px solid var(--line); border-radius: 1rem; box-shadow: var(--shadow); }
.settings-popover fieldset { margin: 0; padding: 0; border: 0; }
.settings-popover legend { margin-bottom: .6rem; color: var(--muted); font-size: .85rem; font-weight: 600; }
.theme-options { display: flex; gap: .6rem; }
.theme-dot { width: 38px; height: 38px; border: 2px solid transparent; border-radius: 50%; cursor: pointer; box-shadow: inset 0 0 0 1px rgba(20,30,25,.14); }
.theme-dot.is-active { border-color: var(--gold); }
.theme-dot.light { background: #faf9f4; }
.theme-dot.dark { background: #17201d; }
.theme-dot.sepia { background: #e7dcc5; }
.settings-popover input[type='range'] { width: 100%; accent-color: var(--accent); }
.settings-popover output { color: var(--text); font-weight: 500; }

.chapter { width: min(100%, 820px); min-height: calc(100vh - 160px); margin: 0 auto; padding: clamp(3rem, 8vw, 7rem) clamp(.25rem, 4vw, 3rem) 5rem; }
.chapter-heading { margin-bottom: 3.2rem; text-align: center; }
.chapter-heading p { margin: 0 0 .7rem; color: var(--muted); font-size: .72rem; font-weight: 700; letter-spacing: .18em; text-transform: uppercase; }
.chapter-heading h1 { margin: 0; font-family: var(--reader-font); font-size: clamp(2.1rem, 5vw, 3.5rem); font-weight: 400; }
.chapter-content { font-family: var(--reader-font); font-size: var(--reader-size); line-height: var(--reader-line); }
.chapter-content p { margin: 0 0 1.4em; }
.chapter-content p:first-child::first-letter { float: left; margin: .11em .12em 0 0; color: var(--accent); font-size: 3.5em; line-height: .72; }
.chapter-footer { display: flex; align-items: center; gap: 1rem; margin-top: 4rem; color: var(--muted); font-size: .75rem; letter-spacing: .1em; text-transform: uppercase; }
.chapter-footer::before, .chapter-footer::after { content: ''; height: 1px; flex: 1; background: var(--line); }
.chapter[data-font='sans'] { --reader-font: 'DM Sans', system-ui, sans-serif; }
.chapter[data-font='accessible'] { --reader-font: Arial, Verdana, sans-serif; letter-spacing: .02em; }

.chapter.mode-paged { height: calc(100vh - 205px); min-height: 520px; overflow: hidden; padding-bottom: 2rem; }
.chapter.mode-paged .chapter-content { height: calc(100% - 150px); column-width: 660px; column-gap: 7rem; column-fill: auto; transition: transform .25s ease; }
.chapter.mode-paged .chapter-footer { display: none; }
.paged-controls { position: sticky; bottom: 1rem; display: flex; justify-content: center; align-items: center; gap: 1rem; width: max-content; margin: 0 auto; padding: .4rem; background: var(--surface-solid); border: 1px solid var(--line); border-radius: .9rem; box-shadow: 0 10px 30px rgba(20,30,25,.12); }
.paged-controls span { min-width: 68px; color: var(--muted); font-size: .82rem; text-align: center; }
.flow-controls { position: fixed; z-index: 6; right: 1.4rem; bottom: 1.4rem; display: flex; align-items: center; gap: .8rem; padding: .5rem; background: var(--surface-solid); border: 1px solid var(--line); border-radius: 1rem; box-shadow: var(--shadow); }
.flow-controls label { display: flex; align-items: center; gap: .4rem; color: var(--muted); font-size: .75rem; }
.flow-controls input { width: 80px; accent-color: var(--accent); }

@media (max-width: 840px) {
  .auth-layout { grid-template-columns: 1fr; padding-top: 3rem; }
  .auth-card { justify-self: stretch; max-width: none; }
  .welcome-copy h1 { font-size: clamp(2.8rem, 12vw, 4.8rem); }
  .library-heading { display: block; }
  .library-note { margin-top: 1rem; }
  .reader-shell { display: block; }
  .reader-sidebar { position: static; display: flex; align-items: center; gap: 1rem; width: 100%; height: auto; padding: 1rem; border-right: 0; border-bottom: 1px solid var(--line); }
  .reader-sidebar .book-mini { display: none; }
  .progress-block { flex: 1; margin: 0; }
  .reader-main { padding: 0 1rem 3rem; }
  .settings-popover { top: 154px; right: 1rem; }
}

@media (max-width: 560px) {
  .site-header { min-height: 70px; }
  .brand span:last-child { display: none; }
  body[data-view='auth'] .brand span:last-child { display: inline; }
  .member-email { display: none !important; }
  .auth-layout { min-height: calc(100vh - 70px); width: calc(100% - 1rem); padding: 2.5rem .5rem 4rem; }
  .auth-card { padding: .65rem; border-radius: 1rem; }
  .feature-row { display: none; }
  .book-card { grid-template-columns: 100px 1fr; }
  .book-cover { min-height: 150px; }
  .reader-toolbar { min-height: 64px; }
  .segmented button { padding-inline: .55rem; }
  .chapter { min-height: auto; padding-top: 3rem; }
  .settings-popover { top: 138px; width: calc(100% - 2rem); }
  .flow-controls { right: .7rem; bottom: .7rem; left: .7rem; justify-content: center; }
}

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