:root {
  --page: #f0eee4;
  --paper: #faf8f1;
  --paper-strong: #fffdf8;
  --ink: #201f1a;
  --muted: #6d695f;
  --green: #2f685e;
  --green-deep: #184a40;
  --green-soft: #e4eeea;
  --orange: #bd6038;
  --orange-soft: #f5ded6;
  --gold: #d59b58;
  --line: #ddd8ca;
  --shadow: 0 20px 60px rgba(48, 43, 31, .09);
  --reader-size: 18px;
  --topbar: 74px;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  color: var(--ink);
  background: var(--page);
  font-family: "Noto Sans Thai", "Leelawadee UI", Tahoma, sans-serif;
  line-height: 1.75;
}

a { color: inherit; }
button { font: inherit; }
img { max-width: 100%; }
:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }

.read-progress {
  position: fixed;
  inset: 0 0 auto;
  z-index: 100;
  height: 4px;
  background: rgba(47, 104, 94, .12);
}
.read-progress span { display: block; width: 0; height: 100%; background: linear-gradient(90deg, var(--orange), var(--gold), var(--green)); }

.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  height: var(--topbar);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 0 28px;
  border-bottom: 1px solid var(--line);
  background: rgba(240, 238, 228, .94);
  backdrop-filter: blur(14px);
}
.topbar-title { display: flex; align-items: baseline; gap: 12px; min-width: 0; }
.topbar-title a { text-decoration: none; white-space: nowrap; }
.topbar-title strong { font-family: Georgia, "Noto Serif Thai", serif; font-size: 1.22rem; }
.topbar-title span { color: #968d7d; font-size: .98rem; white-space: nowrap; }
.topbar-tools { display: flex; align-items: center; gap: 8px; }
.reading-percent { min-width: 44px; color: var(--green); font-weight: 800; font-size: .9rem; }
.text-button, .icon-button {
  min-width: 42px;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--ink);
  background: rgba(255,255,255,.55);
  cursor: pointer;
}
.text-button:hover, .icon-button:hover { border-color: var(--green); color: var(--green); }
.menu-button { display: none; }

.page-shell {
  width: min(1480px, 100%);
  margin: 0 auto;
  display: grid;
  grid-template-columns: 285px minmax(0, 790px) 230px;
  justify-content: center;
  gap: 54px;
  padding: 54px 28px 100px;
}

.sidebar-inner { position: sticky; top: calc(var(--topbar) + 28px); max-height: calc(100vh - var(--topbar) - 48px); overflow: auto; padding-right: 8px; }
.toc-label { margin: 0 0 12px; color: #91897b; font-size: .84rem; font-weight: 700; letter-spacing: .05em; }
.toc { display: grid; border-left: 1px solid var(--line); }
.toc a {
  display: block;
  margin-left: -1px;
  padding: 9px 12px 9px 20px;
  border-left: 2px solid transparent;
  color: var(--muted);
  font-size: .95rem;
  line-height: 1.5;
  text-decoration: none;
}
.toc a:hover { color: var(--green); }
.toc a.active { border-left-color: var(--green); color: var(--green-deep); font-weight: 700; }

.reader-wrap { min-width: 0; }
.reader {
  font-size: var(--reader-size);
  line-height: 1.9;
  text-wrap: pretty;
}
.reader section { scroll-margin-top: calc(var(--topbar) + 22px); }
.book-intro { padding-bottom: 40px; }
.intro-hero {
  display: grid;
  grid-template-columns: minmax(220px, 290px) minmax(0, 1fr);
  align-items: center;
  gap: 42px;
  margin: 4px 0 46px;
  padding: 28px;
  border: 1px solid rgba(221, 216, 202, .78);
  background: rgba(250, 248, 241, .55);
}
.intro-hero-copy { min-width: 0; }
.book-cover {
  display: block;
  width: 100%;
  border-radius: 7px;
  box-shadow: 0 18px 42px rgba(46, 39, 28, .18);
}
.reader h1, .reader h2, .reader h3, .reader h4 { font-family: Georgia, "Noto Serif Thai", serif; color: #181813; line-height: 1.38; text-wrap: balance; }
.reader h1 { margin: 0 0 14px; letter-spacing: -.03em; }
.book-name { display: block; color: var(--green-deep); font-size: clamp(2rem, 3vw, 2.75rem); }
.book-name em { color: var(--orange); font-style: normal; }
.book-subtitle { display: block; margin-top: 14px; color: var(--ink); font-family: "Noto Sans Thai", "Leelawadee UI", Tahoma, sans-serif; font-size: 1.15rem; font-weight: 600; letter-spacing: 0; line-height: 1.55; }
.book-subtitle small { color: var(--muted); font-size: .9em; font-weight: 500; }
.intro-hero h3 { margin: 0; color: var(--muted); font-family: inherit; font-size: 1rem; font-weight: 500; line-height: 1.65; }
.reader h2 { margin: 0 0 26px; padding-top: 12px; font-size: clamp(1.55rem, 2.45vw, 2.05rem); letter-spacing: -.02em; }
.reader h2::after { content: ""; display: block; width: 54px; height: 3px; margin-top: 16px; background: var(--orange); }
.reader h3 { margin: 32px 0 12px; font-size: 1.24rem; }
.reader h4 { margin: 28px 0 10px; font-size: 1.1rem; }
.reader p { margin: 0 0 21px; }
.reader strong { color: #11120f; font-weight: 800; }
.reader a { color: var(--green-deep); text-decoration-thickness: 1px; text-underline-offset: 3px; }
.reader ul, .reader ol { margin: 8px 0 26px; padding-left: 1.35em; }
.reader li { margin: 8px 0; padding-left: 5px; }
.reader li::marker { color: var(--orange); }
.reader hr { height: 1px; margin: 52px 0; border: 0; background: var(--line); }
.reader blockquote {
  margin: 22px 0 28px;
  padding: 22px 24px;
  border-left: 3px solid var(--green);
  background: var(--paper-strong);
  box-shadow: 0 8px 26px rgba(51, 46, 34, .04);
}
.reader blockquote p:last-child { margin-bottom: 0; }
.reader code { padding: 2px 6px; border-radius: 5px; background: #e7e3d8; font-size: .9em; }

.book-section {
  margin: 0 0 38px;
  padding: 42px 34px 34px;
  border: 1px solid rgba(221, 216, 202, .8);
  border-radius: 5px;
  background: rgba(250, 248, 241, .6);
}
.book-section:nth-of-type(even) { background: rgba(255, 253, 248, .76); }
.book-section .callout {
  margin: 24px -10px;
  padding: 20px 22px;
  border-left: 3px solid var(--green);
  background: var(--green-soft);
}
.book-section .callout.warning { border-left-color: #b44535; background: var(--orange-soft); }
.book-section .callout.tip { border-left-color: var(--orange); background: #faece2; }
.book-section .callout p:last-child { margin-bottom: 0; }
.full-book-hint {
  margin: 30px -10px !important;
  padding: 21px 22px;
  border: 1px solid var(--green);
  border-radius: 7px;
  background: var(--green-soft);
}
.inline-cta { display: inline-flex; margin-top: 12px; font-size: .9rem; font-weight: 800; }

.finale { border-color: var(--green); background: var(--green-soft) !important; }
.finale h2::after { background: var(--green); }
.final-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 30px; }
.button {
  display: inline-flex;
  min-height: 48px;
  align-items: center;
  justify-content: center;
  padding: 11px 16px;
  border: 1px solid var(--green);
  border-radius: 8px;
  color: var(--green-deep) !important;
  background: transparent;
  font-weight: 800;
  line-height: 1.35;
  text-align: center;
  text-decoration: none !important;
}
.button:hover { background: rgba(47,104,94,.08); }
.button-primary { color: #fff !important; background: var(--green); }
.button-primary:hover { background: var(--green-deep); }

.reader-rail { padding-top: 4px; }
.rail-card { position: sticky; top: calc(var(--topbar) + 28px); padding: 19px; border: 1px solid var(--line); border-radius: 9px; background: rgba(250,248,241,.76); }
.rail-card + .rail-card { top: calc(var(--topbar) + 190px); margin-top: 14px; }
.rail-kicker { margin: 0; color: var(--muted); font-size: .83rem; }
.rail-card > strong { display: block; margin: 2px 0 8px; color: var(--green); font-family: Georgia, serif; font-size: 2.2rem; }
.rail-card > p { margin: 0; color: var(--muted); font-size: .82rem; line-height: 1.6; }
.next-card span { display: block; color: var(--orange); font-size: .78rem; font-weight: 800; }
.next-card > strong { margin: 8px 0 16px; color: var(--ink); font-family: inherit; font-size: .98rem; line-height: 1.55; }
.next-card .button { width: 100%; font-size: .82rem; }

.loading { min-height: 70vh; display: grid; align-content: center; gap: 12px; }
.loading span { display: block; height: 18px; border-radius: 6px; background: linear-gradient(90deg, #e4e0d4, #f7f4eb, #e4e0d4); background-size: 220% 100%; animation: shimmer 1.6s infinite; }
.loading span:nth-child(1) { width: 72%; height: 48px; }
.loading span:nth-child(2) { width: 100%; }
.loading span:nth-child(3) { width: 94%; }
.loading span:nth-child(4) { width: 84%; }
.loading p { color: var(--muted); }
@keyframes shimmer { to { background-position: -220% 0; } }

.back-top { position: fixed; right: 24px; bottom: 24px; z-index: 25; width: 46px; height: 46px; border: 0; border-radius: 50%; color: #fff; background: var(--green); box-shadow: var(--shadow); cursor: pointer; opacity: 0; pointer-events: none; transform: translateY(8px); transition: .2s ease; }
.back-top.visible { opacity: 1; pointer-events: auto; transform: translateY(0); }
.mobile-drawer, .drawer-backdrop { display: none; }
.drawer-backdrop[hidden] { display: none !important; }

.error-card { padding: 28px; border: 1px solid #b44535; background: var(--orange-soft); }

@media (max-width: 1160px) {
  .page-shell { grid-template-columns: 260px minmax(0, 760px); gap: 42px; }
  .reader-rail { display: none; }
}

@media (max-width: 800px) {
  :root { --topbar: 64px; --reader-size: 17px; }
  .topbar { padding: 0 14px; }
  .menu-button { display: inline-grid; place-items: center; }
  .topbar-title { align-items: center; gap: 10px; }
  .topbar-title strong { font-size: 1rem; }
  .topbar-title span { display: none; }
  .reading-percent { display: none; }
  .page-shell { display: block; padding: 24px 14px 80px; }
  .sidebar { display: none; }
  .intro-hero {
    grid-template-columns: minmax(0, 220px) minmax(0, 1fr);
    gap: 24px;
    margin: 0 0 34px;
    padding: 22px;
  }
  .book-name { font-size: clamp(1.8rem, 6.4vw, 2.25rem); }
  .book-subtitle { font-size: 1.02rem; }
  .reader h2 { font-size: clamp(1.42rem, 5.8vw, 1.8rem); }
  .book-section { padding: 30px 20px 24px; }
  .book-section .callout, .full-book-hint { margin-inline: -6px !important; }
  .final-actions { grid-template-columns: 1fr; }
  .drawer-backdrop { position: fixed; inset: 0; z-index: 70; display: block; background: rgba(21, 25, 21, .38); backdrop-filter: blur(3px); }
  .mobile-drawer { position: fixed; inset: 0 auto 0 0; z-index: 80; display: block; width: min(86vw, 360px); padding: 20px; background: var(--paper); box-shadow: 20px 0 60px rgba(0,0,0,.18); transform: translateX(-105%); transition: transform .25s ease; overflow: auto; }
  .mobile-drawer.open { transform: translateX(0); }
  .drawer-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
  .toc-mobile { border-left: 0; }
  .toc-mobile a { border-left: 2px solid transparent; }
}

@media (max-width: 480px) {
  .topbar-tools { gap: 4px; }
  .text-button { min-width: 38px; }
  .reader { line-height: 1.86; }
  .intro-hero { display: block; padding: 18px; }
  .book-cover { width: min(76vw, 250px); margin: 0 auto 24px; }
  .book-name { font-size: clamp(1.75rem, 8vw, 2.1rem); }
  .book-section { margin-bottom: 18px; border-radius: 3px; }
}

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