:root {
  color-scheme: dark;
  --void: #0b1113;
  --graphite: #172024;
  --steel: #223035;
  --paper: #f7f3e9;
  --stone: #e7e0d3;
  --ink: #182022;
  --muted: #9cb0b4;
  --light: #e8f0ee;
  --gate: #58c5db;
  --gate-deep: #208ba7;
  --alert: #ff756f;
  --border: rgba(214, 225, 223, 0.16);
  --shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
  --radius: 8px;
  --topbar-height: 72px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-synthesis: none;
}

* { box-sizing: border-box; }

html { background: var(--void); }

body {
  margin: 0;
  min-width: 320px;
  background: var(--void);
  color: var(--light);
}

button, input { font: inherit; }

button, a, input { -webkit-tap-highlight-color: transparent; }

:focus-visible {
  outline: 3px solid var(--gate);
  outline-offset: 3px;
}

.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;
}

.skip-link {
  position: fixed;
  z-index: 100;
  top: 12px;
  left: 12px;
  transform: translateY(-150%);
  padding: 12px 16px;
  border-radius: var(--radius);
  background: var(--paper);
  color: var(--ink);
  font-weight: 800;
}

.skip-link:focus { transform: translateY(0); }

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 292px minmax(0, 1fr);
  grid-template-rows: var(--topbar-height) minmax(0, 1fr);
}

.topbar {
  position: sticky;
  z-index: 40;
  top: 0;
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 292px minmax(280px, 680px) auto;
  gap: 20px;
  align-items: center;
  min-height: var(--topbar-height);
  padding: 10px 24px;
  border-bottom: 1px solid var(--border);
  background: rgba(11, 17, 19, 0.96);
  backdrop-filter: blur(16px);
}

.brand-block { min-width: 0; }
.brand-title, .brand-subtitle { margin: 0; }
.brand-title { font-weight: 800; letter-spacing: -0.02em; }
.brand-subtitle { color: var(--muted); font-size: 0.82rem; }

.menu-button { display: none !important; }

.icon-button, .control-button, .view-button {
  min-width: 44px;
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--steel);
  color: var(--light);
  cursor: pointer;
}

.icon-button:hover, .control-button:hover, .view-button:hover { border-color: var(--gate); }
.icon-button:disabled, .control-button:disabled { opacity: 0.42; cursor: not-allowed; }

.search-form {
  position: relative;
  display: flex;
  align-items: center;
}

.search-form input {
  width: 100%;
  min-height: 46px;
  padding: 0 50px 0 44px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--graphite);
  color: var(--light);
}

.search-form input::placeholder { color: #91a3a7; }
.search-icon { position: absolute; left: 15px; color: var(--gate); font-size: 1.3rem; }
kbd { position: absolute; right: 12px; padding: 2px 7px; border: 1px solid var(--border); border-radius: 4px; color: var(--muted); }

.download-link {
  justify-self: end;
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--gate-deep);
  border-radius: var(--radius);
  color: var(--light);
  text-decoration: none;
  font-weight: 750;
}

.sidebar {
  position: sticky;
  z-index: 30;
  top: var(--topbar-height);
  grid-column: 1;
  grid-row: 2;
  height: calc(100vh - var(--topbar-height));
  overflow: auto;
  padding: 24px 18px;
  border-right: 1px solid var(--border);
  background: var(--graphite);
}

.sidebar-heading, .search-panel-heading { display: flex; align-items: start; justify-content: space-between; gap: 16px; }
.sidebar-heading h1, .search-panel-heading h2 { margin: 3px 0 0; font-size: 1.45rem; }
.sidebar-close { display: none; }
.section-label { margin: 0; color: var(--gate); font-size: 0.76rem; font-weight: 800; letter-spacing: 0.08em; text-transform: uppercase; }

.toc { display: grid; gap: 3px; margin: 22px 0; }
.toc a {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: baseline;
  min-height: 40px;
  padding: 9px 10px;
  border-left: 3px solid transparent;
  border-radius: 0 6px 6px 0;
  color: var(--muted);
  text-decoration: none;
  line-height: 1.25;
}
.toc a[data-level="2"] { padding-left: 24px; font-size: 0.9rem; }
.toc a:hover { background: var(--steel); color: var(--light); }
.toc a[aria-current="location"] { border-left-color: var(--gate); background: rgba(88, 197, 219, 0.1); color: white; }
.toc-page { color: #799096; font-variant-numeric: tabular-nums; }

.rights-note { padding: 16px; border: 1px solid rgba(255, 117, 111, 0.35); border-radius: var(--radius); background: rgba(255, 117, 111, 0.06); }
.rights-note strong { color: #ffb1ad; }
.rights-note p { margin: 6px 0 0; color: var(--muted); font-size: 0.86rem; line-height: 1.5; }

.menu-scrim { display: none; }

.reader-main { grid-column: 2; grid-row: 2; min-width: 0; }
.reader-toolbar {
  position: sticky;
  z-index: 20;
  top: var(--topbar-height);
  display: flex;
  justify-content: space-between;
  gap: 20px;
  align-items: center;
  min-height: 68px;
  padding: 10px 24px;
  border-bottom: 1px solid var(--border);
  background: rgba(23, 32, 36, 0.94);
  backdrop-filter: blur(12px);
}
.page-nav, .view-controls { display: flex; align-items: center; gap: 8px; }
.control-button { padding: 0 13px; }
.page-picker { display: flex; align-items: center; gap: 7px; color: var(--muted); }
.page-picker input { width: 70px; min-height: 44px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--void); color: var(--light); text-align: center; }
.view-button { padding: 0 14px; }
.view-button.is-active { border-color: var(--gate); background: rgba(88, 197, 219, 0.13); color: white; }

.page-stage { min-height: calc(100vh - 140px); padding: 30px clamp(16px, 4vw, 60px) 64px; background: radial-gradient(circle at 50% 0%, #26363b 0, var(--void) 58%); }
.page-context { width: min(100%, 920px); margin: 0 auto 16px; }
.page-context h2 { margin: 5px 0 0; font-size: clamp(1.25rem, 2vw, 1.8rem); letter-spacing: -0.02em; }
.page-frame { position: relative; width: max-content; max-width: 100%; margin: 0 auto; border-radius: 4px; background: var(--paper); box-shadow: var(--shadow); overflow: hidden; }
.page-frame.is-fit { width: min(100%, 920px); }
.page-frame img { display: block; width: auto; max-width: none; height: auto; background: var(--stone); }
.page-frame.is-fit img { width: 100%; max-width: 100%; }
.page-frame.is-text { width: min(100%, 820px); }
.page-text { min-height: 72vh; padding: clamp(24px, 6vw, 72px); background: var(--paper); color: var(--ink); white-space: pre-wrap; font-family: Georgia, "Times New Roman", serif; font-size: clamp(1rem, 1.4vw, 1.14rem); line-height: 1.72; }
.page-loading { position: absolute; inset: 0; display: grid; place-items: center; min-height: 420px; background: var(--paper); color: var(--ink); font-weight: 800; }
.page-loading[hidden] { display: none; }

.search-panel {
  position: fixed;
  z-index: 60;
  top: var(--topbar-height);
  right: 0;
  width: min(520px, 100vw);
  height: calc(100vh - var(--topbar-height));
  overflow: auto;
  padding: 24px;
  border-left: 1px solid var(--border);
  background: #11191c;
  box-shadow: -20px 0 60px rgba(0, 0, 0, 0.44);
}
.search-summary { color: var(--muted); }
.search-results { display: grid; gap: 10px; padding: 0; list-style: none; }
.search-result button { width: 100%; padding: 15px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--graphite); color: var(--light); text-align: left; cursor: pointer; }
.search-result button:hover { border-color: var(--gate); }
.search-result-header { display: flex; justify-content: space-between; gap: 16px; margin-bottom: 8px; font-weight: 800; }
.search-result-title { color: var(--gate); }
.search-snippet { margin: 0; color: var(--muted); line-height: 1.48; }
mark { padding: 0 2px; background: #f2d35b; color: #111; }

noscript { display: block; padding: 24px; background: #5b1818; color: white; text-align: center; }

@media (max-width: 900px) {
  :root { --topbar-height: 68px; }
  .app-shell { display: block; }
  .topbar { grid-template-columns: auto minmax(0, 1fr) auto; gap: 10px; padding: 8px 12px; }
  .menu-button { display: inline-flex !important; align-items: center; gap: 8px; padding: 0 12px; }
  .brand-block { display: none; }
  .download-link { padding: 0 12px; }
  .sidebar { position: fixed; top: 0; left: 0; z-index: 80; width: min(340px, 88vw); height: 100vh; transform: translateX(-105%); transition: transform 180ms ease; box-shadow: var(--shadow); }
  .sidebar-close { display: block; }
  .app-shell[data-menu-open="true"] .sidebar { transform: translateX(0); }
  .menu-scrim { position: fixed; inset: 0; z-index: 70; width: 100%; height: 100%; border: 0; background: rgba(0,0,0,.62); }
  .app-shell[data-menu-open="true"] .menu-scrim { display: block; }
  .reader-main { min-height: calc(100vh - var(--topbar-height)); }
  .reader-toolbar { top: var(--topbar-height); padding: 8px 12px; }
}

@media (max-width: 640px) {
  kbd, .download-link { display: none; }
  .topbar { grid-template-columns: auto minmax(0, 1fr); }
  .search-form input { padding-right: 12px; }
  .reader-toolbar { align-items: stretch; flex-direction: column; gap: 8px; }
  .page-nav, .view-controls { justify-content: space-between; }
  .control-button span { display: none; }
  .view-button { flex: 1; }
  .page-stage { padding-top: 22px; }
  .page-text { min-height: 60vh; padding: 24px 20px; }
  .search-panel { top: 0; height: 100vh; padding: 18px; }
}

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

