:root {
  color-scheme: light;
  --bg: #dbc3a0;
  --bg-accent: rgba(251, 243, 229, 0.76);
  --panel: rgba(74, 48, 29, 0.1);
  --text: #332316;
  --muted: #725b46;
  --shadow: rgba(43, 27, 15, 0.26);
  --shelf-wood: linear-gradient(180deg, #8b5b35 0%, #714424 100%);
  --shelf-edge: #4a2f1d;
  --book-text: #fff9f1;
  --reader-bg: rgba(255, 248, 239, 0.8);
  --button-bg: rgba(84, 56, 34, 0.12);
  --button-border: rgba(84, 56, 34, 0.18);
  --wall-dark: rgba(55, 33, 20, 0.34);
  --wall-light: rgba(255, 236, 204, 0.4);
  --panel-line: rgba(255, 255, 255, 0.2);
  --paper: #f2e2c6;
  --paper-shadow: rgba(115, 88, 54, 0.35);
  --glow: rgba(255, 215, 152, 0.28);
}

body[data-theme="midnight"] {
  --bg: #221912;
  --bg-accent: rgba(41, 29, 22, 0.88);
  --panel: rgba(255, 240, 226, 0.06);
  --text: #f3e7d8;
  --muted: #bea992;
  --shadow: rgba(0, 0, 0, 0.4);
  --shelf-wood: linear-gradient(180deg, #4d3426 0%, #382418 100%);
  --shelf-edge: #21140e;
  --book-text: #f5eee4;
  --reader-bg: rgba(34, 24, 18, 0.9);
  --button-bg: rgba(255, 241, 226, 0.08);
  --button-border: rgba(255, 241, 226, 0.14);
  --wall-dark: rgba(8, 4, 2, 0.56);
  --wall-light: rgba(190, 142, 95, 0.12);
  --panel-line: rgba(255, 235, 214, 0.12);
  --paper: #d9ccb9;
  --paper-shadow: rgba(18, 10, 7, 0.44);
  --glow: rgba(198, 141, 91, 0.14);
}

body[data-theme="sage"] {
  --bg: #d5c6ae;
  --bg-accent: rgba(247, 241, 232, 0.76);
  --panel: rgba(75, 58, 37, 0.08);
  --text: #30251b;
  --muted: #6c5d4a;
  --shadow: rgba(34, 25, 18, 0.2);
  --shelf-wood: linear-gradient(180deg, #76624f 0%, #59483a 100%);
  --shelf-edge: #3d2e24;
  --book-text: #f6f0e8;
  --reader-bg: rgba(249, 244, 236, 0.82);
  --button-bg: rgba(78, 61, 42, 0.12);
  --button-border: rgba(78, 61, 42, 0.18);
  --wall-dark: rgba(41, 29, 20, 0.3);
  --wall-light: rgba(255, 240, 220, 0.28);
  --panel-line: rgba(255, 255, 255, 0.22);
  --paper: #ece2d0;
  --paper-shadow: rgba(90, 72, 56, 0.3);
  --glow: rgba(236, 201, 153, 0.18);
}

* {
  box-sizing: border-box;
}

body {
  position: relative;
  margin: 0;
  min-height: 100vh;
  font-family: "Manrope", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at 50% -8%, var(--wall-light), transparent 36%),
    linear-gradient(180deg, color-mix(in srgb, var(--bg) 94%, #5c3a22 6%) 0%, color-mix(in srgb, var(--bg) 80%, #2a180f 20%) 100%);
}

body::before,
body::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
}

body::before {
  background:
    linear-gradient(90deg, var(--wall-dark) 0%, transparent 12%, transparent 88%, var(--wall-dark) 100%),
    radial-gradient(circle at 50% 0%, var(--glow), transparent 28%),
    repeating-linear-gradient(90deg, rgba(255, 248, 234, 0.016) 0 2px, transparent 2px 160px);
  opacity: 0.9;
}

body::after {
  inset: auto 0 0;
  height: 38vh;
  background: linear-gradient(180deg, transparent 0%, rgba(18, 10, 6, 0.24) 100%);
}

button,
a {
  font: inherit;
}

.page-shell {
  position: relative;
  z-index: 1;
  width: min(1460px, calc(100% - 32px));
  margin: 0 auto;
  padding: 34px 0 46px;
}

.hero,
.layout {
  display: grid;
  gap: 24px;
}

.hero {
  grid-template-columns: 1fr auto;
  align-items: start;
  margin-bottom: 24px;
}

.eyebrow,
.section-label,
.panel-meta {
  letter-spacing: 0.12em;
  text-transform: uppercase;
  font-size: 0.78rem;
}

.eyebrow,
.section-label {
  color: var(--muted);
}

h1,
h2,
.shelf-title {
  margin: 0;
  font-family: "Cormorant Garamond", serif;
  line-height: 0.95;
}

h1 {
  font-size: clamp(3rem, 6vw, 5.75rem);
  max-width: 9ch;
}

h2 {
  font-size: clamp(2rem, 4vw, 3rem);
}

.hero-copy,
.reader-description {
  max-width: 62ch;
  color: var(--muted);
  line-height: 1.7;
}

.toolbar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: end;
}

.toolbar-button,
.download-link {
  border: 1px solid var(--button-border);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.18), transparent 100%),
    var(--button-bg);
  color: var(--text);
  padding: 0.85rem 1.1rem;
  border-radius: 999px;
  backdrop-filter: blur(10px);
  text-decoration: none;
  box-shadow: 0 10px 24px -18px var(--shadow);
  transition: transform 180ms ease, background 180ms ease, border-color 180ms ease, box-shadow 180ms ease;
}

.toolbar-button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}

.toolbar-button-primary {
  background: color-mix(in srgb, var(--button-bg) 55%, var(--shelf-edge) 45%);
  border-color: color-mix(in srgb, var(--button-border) 55%, var(--shelf-edge) 45%);
  color: var(--book-text);
}

.toolbar-button:hover,
.download-link:hover {
  transform: translateY(-1px);
  box-shadow: 0 18px 26px -18px var(--shadow);
}

.layout {
  grid-template-columns: minmax(0, 1.25fr) minmax(320px, 0.75fr);
}

.library-panel,
.reader-panel,
.admin-panel {
  position: relative;
  padding: 24px;
  border-radius: 28px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.16), rgba(255, 255, 255, 0.04)),
    var(--bg-accent);
  box-shadow:
    0 24px 54px -28px var(--shadow),
    inset 0 1px 0 rgba(255, 255, 255, 0.3);
  border: 1px solid var(--panel-line);
  overflow: hidden;
}

.library-panel::before,
.reader-panel::before,
.admin-panel::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    radial-gradient(circle at top, rgba(255, 255, 255, 0.16), transparent 42%),
    repeating-linear-gradient(135deg, rgba(255, 255, 255, 0.025) 0 2px, transparent 2px 12px);
  pointer-events: none;
}

.panel-heading {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: start;
}

.library {
  display: grid;
  gap: 6px;
  margin-top: 24px;
}

.shelf {
  position: relative;
  display: grid;
  gap: 0;
}

.shelf-header {
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 12px;
  margin-bottom: 14px;
}

.shelf-title {
  font-size: 1.6rem;
}

.shelf-stage {
  position: relative;
  display: flex;
  align-items: flex-end;
  min-height: 450px;
  margin-top: -18px;
  padding: 44px 40px 42px;
  border-radius: 0 0 30px 30px;
  overflow: hidden;
  background-color: #59371f;
  background-size: 100% 100%;
  background-position: center;
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.12),
    0 24px 44px -28px rgba(0, 0, 0, 0.38);
}

.shelf-divider {
  position: relative;
  height: 98px;
  background-repeat: no-repeat;
  background-size: 100% auto;
  background-position: center 58%;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
}

.shelf-divider-title {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: min(42vw, 300px);
  max-width: min(44vw, 320px);
  padding: 0 18px;
  text-align: center;
  font-family: "Cormorant Garamond", serif;
  font-size: clamp(1.15rem, 2vw, 1.55rem);
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: 0.03em;
  color: #3e362d;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.45);
}

.shelf-divider-count {
  position: absolute;
  right: 24px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: rgba(255, 241, 222, 0.78);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.34);
}

.shelf-stage::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    radial-gradient(circle at 50% 18%, rgba(255, 234, 188, 0.18), transparent 32%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.08) 0%, transparent 24%, rgba(0, 0, 0, 0.08) 100%);
  pointer-events: none;
}

.shelf-stage::after {
  content: "";
  position: absolute;
  inset: auto 0 0;
  height: 18%;
  background: linear-gradient(180deg, transparent 0%, rgba(0, 0, 0, 0.2) 100%);
  pointer-events: none;
}

.shelf-books {
  position: relative;
  display: flex;
  align-items: end;
  gap: 8px;
  width: 100%;
  min-height: 326px;
  padding: 0 14px 16px 10px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.18) transparent;
  z-index: 1;
}

.shelf-books::-webkit-scrollbar {
  height: 10px;
}

.shelf-books::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.16);
  border-radius: 999px;
}

.book {
  position: relative;
  flex: 0 0 auto;
  width: var(--book-width, 76px);
  height: var(--book-height, 248px);
  padding: 0;
  border: none;
  background: transparent;
  cursor: pointer;
  transform-origin: bottom center;
  transition: transform 220ms ease, filter 220ms ease;
}

.book::before {
  content: "";
  position: absolute;
  left: 8%;
  right: 8%;
  bottom: -6px;
  height: 14px;
  border-radius: 999px;
  background:
    radial-gradient(circle, rgba(0, 0, 0, 0.34) 0%, rgba(0, 0, 0, 0.08) 58%, transparent 100%);
  opacity: 0.7;
  pointer-events: none;
}

.book:hover,
.book:focus-visible {
  filter: saturate(1.04);
  outline: none;
}

.book-media,
.book-label-wrap {
  position: absolute;
  inset: 0;
}

.book-media {
  border-radius: 8px 4px 4px 8px;
  background-color: rgba(84, 53, 31, 0.9);
  box-shadow:
    0 16px 24px rgba(0, 0, 0, 0.28),
    inset 0 1px 0 rgba(255, 255, 255, 0.1);
  transform: rotate(var(--book-tilt, 0deg));
  transform-origin: bottom center;
  transition: transform 220ms ease, box-shadow 220ms ease, filter 220ms ease;
  filter: saturate(1.05) contrast(1.04);
}

.book-label-wrap {
  inset: 54px 15px 48px 14px;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.book:hover .book-media,
.book:focus-visible .book-media {
  animation: book-ready 520ms ease;
}

.book.is-active .book-media {
  transform: translateY(-10px) rotate(var(--book-tilt, 0deg));
  box-shadow:
    0 26px 34px rgba(0, 0, 0, 0.32),
    inset 0 1px 0 rgba(255, 255, 255, 0.1);
}

.book-label {
  position: relative;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  text-orientation: mixed;
  font-family: "Cormorant Garamond", serif;
  font-size: clamp(0.78rem, 1.2vw, 0.98rem);
  font-weight: 700;
  letter-spacing: 0.05em;
  line-height: 1;
  text-align: center;
  color: #f3dfad;
  text-shadow:
    0 1px 0 rgba(0, 0, 0, 0.42),
    0 0 8px rgba(0, 0, 0, 0.22);
  max-height: 100%;
  overflow: hidden;
}

.reader-panel {
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: 18px;
}

.admin-panel {
  margin-top: 24px;
}

.admin-panel-standalone {
  margin-top: 0;
}

.admin-copy {
  max-width: 72ch;
  margin-bottom: 0;
  color: var(--muted);
  line-height: 1.7;
}

.admin-root {
  display: grid;
  gap: 22px;
  margin-top: 24px;
}

.admin-status-bar {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: start;
}

.save-status {
  min-width: 220px;
  margin: 0;
  padding: 0.85rem 1rem;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.14);
  color: var(--muted);
  line-height: 1.5;
}

.save-status.is-pending {
  color: var(--text);
}

.save-status.is-saving {
  color: var(--text);
}

.save-status.is-success {
  color: #155724;
  background: rgba(80, 200, 120, 0.18);
}

.save-status.is-error {
  color: #7b1d1d;
  background: rgba(220, 87, 87, 0.18);
}

.admin-category {
  padding: 18px;
  border-radius: 22px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.18), rgba(255, 255, 255, 0.06));
}

.admin-category-heading,
.admin-document-heading {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: start;
}

.admin-documents {
  display: grid;
  gap: 14px;
  margin-top: 18px;
}

.admin-document {
  padding: 18px;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.14);
  border: 1px solid rgba(255, 255, 255, 0.16);
}

.admin-document-title {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
  line-height: 1.4;
}

.admin-document-current {
  margin: 6px 0 0;
  color: var(--muted);
  line-height: 1.6;
}

.admin-options {
  display: grid;
  gap: 12px;
  margin-top: 16px;
}

.title-option {
  display: grid;
  grid-template-columns: minmax(120px, 170px) minmax(0, 1fr);
  gap: 14px;
  align-items: start;
  padding: 14px;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.12);
  transition: border-color 180ms ease, transform 180ms ease, background 180ms ease;
}

.title-option.is-selected {
  border-color: rgba(255, 255, 255, 0.34);
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-1px);
}

.title-option-toggle {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  color: var(--text);
  font-weight: 700;
  cursor: pointer;
}

.title-option-toggle input {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--shelf-edge);
}

.title-option-toggle input:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.title-option-body {
  display: grid;
  gap: 8px;
}

.title-option-label {
  color: var(--muted);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  font-size: 0.76rem;
}

.title-option-field {
  width: 100%;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.2);
  color: var(--text);
  padding: 0.9rem 1rem;
}

.title-option-field:focus {
  outline: 2px solid rgba(255, 255, 255, 0.32);
  outline-offset: 2px;
}

.title-option-field[readonly] {
  opacity: 0.88;
}

.title-option-hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

.reader-frame-wrap {
  min-height: 68vh;
  border-radius: 22px;
  overflow: hidden;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.12), transparent 22%),
    var(--reader-bg);
  border: 1px solid rgba(255, 255, 255, 0.22);
  box-shadow:
    inset 0 0 0 10px rgba(73, 45, 26, 0.12),
    inset 0 30px 40px rgba(255, 255, 255, 0.06);
}

#reader-frame {
  width: 100%;
  height: 100%;
  min-height: 68vh;
  border: 0;
  background: transparent;
}

.download-link.is-disabled {
  pointer-events: none;
  opacity: 0.45;
}

.empty-state {
  padding: 22px;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.16);
  color: var(--muted);
}

.empty-state-compact {
  padding: 16px;
}

@keyframes book-ready {
  0%,
  100% {
    transform: translateY(0) rotate(0deg);
  }
  30% {
    transform: translateY(-6px) rotate(-1.4deg);
  }
  65% {
    transform: translateY(-3px) rotate(1deg);
  }
}

@media (max-width: 1100px) {
  .layout {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 720px) {
  .page-shell {
    width: min(100% - 20px, 1400px);
    padding-top: 20px;
  }

  .hero {
    grid-template-columns: 1fr;
  }

  .toolbar {
    justify-content: start;
  }

  .library-panel,
  .reader-panel,
  .admin-panel {
    padding: 18px;
    border-radius: 22px;
  }

  .shelf-books {
    min-height: 250px;
    padding-bottom: 10px;
  }

  .shelf-divider {
    height: 86px;
    background-size: 145% auto;
    background-position: center 56%;
  }

  .shelf-divider-title {
    min-width: 210px;
    max-width: 230px;
    font-size: 1.1rem;
  }

  .shelf-divider-count {
    display: none;
  }

  .book {
    width: max(58px, var(--book-width, 66px));
  }

  .title-option {
    grid-template-columns: 1fr;
  }

  .title-option-toggle {
    min-height: auto;
  }

  .admin-status-bar {
    flex-direction: column;
  }

  .save-status {
    min-width: 0;
    width: 100%;
  }
}
