:root {
  color-scheme: dark;
  --sky: #2b3559;
  --night: #11172a;
  --stone: #46515c;
  --stone-dark: #252d36;
  --parchment: #f1d79c;
  --parchment-light: #ffe6aa;
  --parchment-dark: #b87939;
  --ink: #2a1608;
  --muted: #694522;
  --ruby: #8f2832;
  --ruby-dark: #4e1720;
  --gold: #f7c857;
  --moss: #39573a;
  --panel-border: #2b170b;
  --reader-font-size: 100%;
  --reader-font-family: "Literata";
  font-family: "Pixelify Sans", ui-monospace, monospace;
  image-rendering: pixelated;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  color: var(--ink);
}

body {
  min-height: 100vh;
  background:
    linear-gradient(90deg, transparent 0 6px, rgba(255,255,255,.03) 6px 8px, transparent 8px 16px),
    linear-gradient(180deg, #29375f 0 24%, #1c263f 24% 46%, #182234 46% 100%);
  overflow-x: hidden;
}

body::before,
body::after {
  content: "";
  position: fixed;
  pointer-events: none;
  image-rendering: pixelated;
}

body::before {
  left: 0;
  right: 0;
  bottom: 0;
  height: 34vh;
  z-index: 0;
  background:
    linear-gradient(90deg, transparent 0 5%, #1b2b24 5% 12%, transparent 12% 18%, #1d3327 18% 24%, transparent 24% 100%),
    linear-gradient(180deg, transparent 0 24%, #304332 24% 42%, #1d2c24 42% 100%);
  clip-path: polygon(0 38%, 8% 30%, 16% 36%, 26% 25%, 36% 34%, 49% 18%, 63% 35%, 75% 24%, 88% 32%, 100% 20%, 100% 100%, 0 100%);
}

body::after {
  right: 6vw;
  top: 72px;
  width: min(420px, 48vw);
  height: 300px;
  z-index: 0;
  background:
    linear-gradient(#1d2533 0 0) 12% 54% / 18% 46% no-repeat,
    linear-gradient(#1d2533 0 0) 35% 42% / 18% 58% no-repeat,
    linear-gradient(#1d2533 0 0) 58% 50% / 18% 50% no-repeat,
    linear-gradient(#1d2533 0 0) 82% 38% / 18% 62% no-repeat,
    linear-gradient(#263142 0 0) 50% 76% / 92% 34% no-repeat,
    repeating-linear-gradient(90deg, #121826 0 18px, transparent 18px 28px) 50% 30% / 94% 16px no-repeat;
  opacity: .86;
  filter: drop-shadow(10px 12px 0 rgba(0,0,0,.18));
}

button,
input,
select {
  font: inherit;
}

.page-shell {
  position: relative;
  z-index: 1;
  width: min(100%, 1720px);
  margin: 0 auto;
  padding: 28px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 24px;
  margin-bottom: 22px;
  padding: 18px;
  color: var(--parchment-light);
  background: #202838;
  border: 4px solid #101724;
  box-shadow: 0 0 0 4px #5e3a1b, 10px 10px 0 rgba(0,0,0,.34);
}

.eyebrow,
.panel-label,
.status-label,
.empty-kicker {
  margin: 0 0 8px;
  font-size: .78rem;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--gold);
}

.topbar h1,
.panel-card h2,
.empty-state h2 {
  margin: 0;
  font-weight: 700;
}

.topbar h1 {
  font-size: clamp(2.8rem, 7vw, 6.5rem);
  line-height: .82;
  color: #ffd66b;
  text-shadow: 4px 0 #6d241f, 0 4px #6d241f, 4px 4px #12090a;
}

.subtitle {
  margin: 10px 0 0;
  color: #f4dca5;
  font-size: clamp(1rem, 2vw, 1.3rem);
}

.reader-layout {
  display: grid;
  grid-template-columns: minmax(280px, 350px) minmax(0, 1fr);
  gap: 24px;
  align-items: start;
}

.control-panel {
  display: grid;
  gap: 18px;
  position: sticky;
  top: 24px;
}

.library-card,
.controls-card,
.nav-card {
  display: grid;
  gap: 14px;
}

.panel-card,
.reader-frame,
.toc-panel {
  background:
    linear-gradient(45deg, rgba(91,54,25,.12) 25%, transparent 25% 75%, rgba(91,54,25,.12) 75%) 0 0 / 18px 18px,
    linear-gradient(135deg, rgba(255,255,255,.16), transparent 34%),
    var(--parchment);
  border: 4px solid var(--panel-border);
  border-radius: 0;
  box-shadow: 0 0 0 4px var(--parchment-dark), 12px 12px 0 rgba(0,0,0,.28);
}

.panel-card {
  padding: 18px;
}

.book-selector {
  display: grid;
  gap: 12px;
}

.book-option {
  border: 3px solid #4b2610;
  border-radius: 0;
  background: #ffd98b;
  padding: 12px;
  text-align: left;
  color: var(--ink);
  cursor: pointer;
  box-shadow: inset -4px -4px 0 rgba(88,44,13,.18), 5px 5px 0 rgba(0,0,0,.18);
  transition: transform 80ms linear, box-shadow 80ms linear, background 80ms linear;
}

.book-option:hover {
  transform: translate(-2px, -2px);
  background: #ffe8aa;
  box-shadow: inset -4px -4px 0 rgba(88,44,13,.18), 7px 7px 0 rgba(0,0,0,.2);
}

.book-option.is-active {
  background: var(--gold);
  box-shadow: inset 0 0 0 4px rgba(143,40,50,.34), 5px 5px 0 rgba(0,0,0,.2);
}

.book-option-title {
  display: block;
  font-weight: 700;
  color: var(--ink);
}

.muted {
  color: var(--muted);
  line-height: 1.35;
}

code {
  padding: 1px 4px;
  color: #f8e2a4;
  background: #3a2111;
  font-family: "Pixelify Sans", ui-monospace, monospace;
}

.upload-button,
.toc-button,
.reader-nav,
.toc-close {
  border: 4px solid #240e0e;
  border-radius: 0;
  padding: 12px 18px;
  background: linear-gradient(180deg, #c74f40, var(--ruby));
  color: #ffe9ad;
  cursor: pointer;
  text-shadow: 2px 2px 0 #3c1115;
  box-shadow: inset -4px -4px 0 rgba(39,8,11,.32), inset 4px 4px 0 rgba(255,255,255,.16), 6px 6px 0 rgba(0,0,0,.28);
  transition: transform 80ms linear, box-shadow 80ms linear;
}

.upload-button {
  position: relative;
  overflow: hidden;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  font-weight: 700;
}

.upload-button input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.upload-button:hover,
.toc-button:hover,
.reader-nav:hover,
.toc-close:hover {
  transform: translate(-2px, -2px);
  box-shadow: inset -4px -4px 0 rgba(39,8,11,.32), inset 4px 4px 0 rgba(255,255,255,.16), 8px 8px 0 rgba(0,0,0,.3);
}

.toc-button:disabled {
  opacity: .55;
  cursor: not-allowed;
  transform: none;
}

.control-row {
  display: grid;
  gap: 10px;
}

.control-row label {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-weight: 700;
}

select,
input[type="range"] {
  width: 100%;
}

select {
  appearance: none;
  border: 3px solid #4b2610;
  border-radius: 0;
  padding: 12px;
  background: #ffe4a3;
  color: var(--ink);
  box-shadow: inset -4px -4px 0 rgba(88,44,13,.14);
}

input[type="range"] {
  accent-color: var(--ruby);
}

.status-block strong {
  display: block;
  font-size: 1.1rem;
  margin-bottom: 6px;
}

.reader-stage {
  min-width: 0;
}

.reader-frame {
  position: relative;
  height: calc(100vh - 126px);
  min-height: calc(100vh - 126px);
  overflow: hidden;
}

.reader-frame::before {
  content: "";
  position: absolute;
  inset: 14px;
  border: 3px solid rgba(78,37,15,.42);
  pointer-events: none;
  z-index: 3;
}

.reader-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 4;
  width: 52px;
  height: 52px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.reader-nav:hover {
  transform: translate(calc(-2px), calc(-50% - 2px));
}

.reader-nav svg {
  width: 24px;
  height: 24px;
  filter: drop-shadow(2px 2px 0 #3c1115);
}

.reader-nav-prev {
  left: 22px;
}

.reader-nav-next {
  right: 22px;
}

.viewer {
  width: 100%;
  height: 100%;
  min-height: calc(100vh - 126px);
  visibility: hidden;
}

.viewer.is-ready {
  visibility: visible;
}

.viewer iframe {
  background:
    linear-gradient(90deg, rgba(85,43,16,.08) 0 2px, transparent 2px 100%),
    #fff1bd;
}

.toc-panel {
  position: absolute;
  top: 22px;
  right: 22px;
  bottom: 22px;
  width: min(390px, calc(100% - 44px));
  z-index: 5;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 14px;
  padding: 18px;
  background-color: #f1d79c;
}

.toc-panel[hidden] {
  display: none;
}

.toc-header {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
}

.toc-title {
  margin: 0;
}

.toc-close {
  padding: 9px 12px;
  font-weight: 700;
}

.toc-list {
  overflow: auto;
  padding-right: 6px;
  display: grid;
  gap: 8px;
  align-content: start;
}

.toc-item {
  border: 3px solid #4b2610;
  border-radius: 0;
  background: #ffdf97;
  padding: 10px 12px;
  text-align: left;
  color: var(--ink);
  cursor: pointer;
  box-shadow: 4px 4px 0 rgba(0,0,0,.16);
}

.toc-item:hover {
  background: #ffeab7;
}

.toc-item-label {
  display: block;
  line-height: 1.25;
}

.toc-item-depth-1 {
  padding-left: 24px;
}

.toc-item-depth-2 {
  padding-left: 36px;
}

.toc-item-depth-3 {
  padding-left: 48px;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: none;
}

.empty-state[hidden] {
  display: none;
}

@media (max-width: 980px) {
  body::after {
    opacity: .42;
    right: -12vw;
  }

  .page-shell {
    padding: 18px;
  }

  .reader-layout {
    grid-template-columns: 1fr;
  }

  .control-panel {
    position: static;
  }

  .reader-frame,
  .viewer {
    height: 70vh;
    min-height: 70vh;
  }

  .reader-nav-prev {
    left: 14px;
  }

  .reader-nav-next {
    right: 14px;
  }
}

@media (max-width: 640px) {
  .topbar {
    flex-direction: column;
  }

  .topbar h1 {
    font-size: clamp(2.6rem, 18vw, 4.6rem);
  }

  .reader-nav {
    width: 44px;
    height: 44px;
  }

  .toc-panel {
    top: 12px;
    right: 12px;
    left: 12px;
    bottom: 12px;
    width: auto;
  }
}
