* {
  box-sizing: border-box;
}

:root {
  --bg: #111;
  --fg: #e6e6e6;
  --muted: #cfcfcf;
  --muted2: #9d9d9d;
  --font-scale: 1;

  --choice: #cfcfcf;
  --choiceDisabled: 0.45;

  --maxw: 680px;
  --ui-border: rgba(255, 255, 255, 0.15);
  --ui-border-strong: rgba(255, 255, 255, 0.35);
  --ui-bg: rgba(255, 255, 255, 0.06);
  --ui-bg-hover: rgba(255, 255, 255, 0.1);
  --divider: rgba(255, 255, 255, 0.12);
  --choice-bg: rgba(255, 255, 255, 0.04);
  --choice-bg-hover: rgba(255, 255, 255, 0.08);
  --choice-border: rgba(255, 255, 255, 0.18);
  --choice-border-hover: rgba(255, 255, 255, 0.35);
  --quote-border: rgba(255, 255, 255, 0.18);
  --quote-fg: rgba(230, 230, 230, 0.92);
  --backdrop: rgba(0, 0, 0, 0.18);
  --handle-grad-1: rgba(120, 132, 150, 0.55);
  --handle-grad-2: transparent;
  --handle-notch: #000;
  --accent: #8f8f8f;
  --accent-soft: rgba(255, 255, 255, 0.08);
}

:root[data-theme="light"] {
  --bg: #f4efe6;
  --fg: #1f1b16;
  --muted: #6f665b;
  --muted2: #8a8277;

  --choice: #2a2420;
  --choiceDisabled: 0.45;
  --ui-border: rgba(31, 27, 22, 0.18);
  --ui-border-strong: rgba(31, 27, 22, 0.35);
  --ui-bg: rgba(31, 27, 22, 0.06);
  --ui-bg-hover: rgba(31, 27, 22, 0.1);
  --divider: rgba(31, 27, 22, 0.12);
  --choice-bg: rgba(31, 27, 22, 0.04);
  --choice-bg-hover: rgba(31, 27, 22, 0.08);
  --choice-border: rgba(31, 27, 22, 0.18);
  --choice-border-hover: rgba(31, 27, 22, 0.35);
  --quote-border: rgba(31, 27, 22, 0.2);
  --quote-fg: rgba(31, 27, 22, 0.88);
  --backdrop: rgba(0, 0, 0, 0.18);
  --handle-grad-1: rgba(140, 118, 86, 0.55);
  --handle-grad-2: rgba(214, 197, 168, 0.15);
  --handle-notch: #fff;
  --accent: #8a6423;
  --accent-soft: rgba(138, 100, 35, 0.12);
}

@media (max-width: 767px) {
  :root[data-theme="light"] {
    --bg: #f4efe6;
  }
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: Georgia, "Libre Baskerville", serif;
  overscroll-behavior-x: none;
}

.library-page {
  min-height: 100vh;
  padding: 48px 20px 80px;
  background:
    radial-gradient(circle at top right, rgba(255, 255, 255, 0.06), transparent 45%),
    radial-gradient(circle at 20% 10%, rgba(255, 255, 255, 0.08), transparent 40%),
    var(--bg);
}

.library-hero {
  max-width: 720px;
  margin: 0 auto 28px;
  text-align: left;
  display: grid;
  gap: 8px;
}

.library-brand {
  text-transform: uppercase;
  letter-spacing: 0.4em;
  font-size: 0.75rem;
  color: var(--muted2);
}

.library-title {
  font-size: clamp(2rem, 4vw, 2.8rem);
  margin: 0;
}

.library-subtitle {
  color: var(--muted2);
  font-size: 1rem;
  margin: 0;
}

.library-panel {
  max-width: 720px;
  margin: 0 auto;
  padding: 18px;
  border-radius: 18px;
  border: 1px solid var(--ui-border-strong);
  background: color-mix(in oklab, var(--bg), #fff 4%);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.25);
}

.library-list {
  display: grid;
  gap: 10px;
}

.library-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--ui-border);
  background: var(--ui-bg);
  color: var(--fg);
  cursor: pointer;
  font-size: 1rem;
  text-align: left;
}

.library-item:hover {
  background: var(--ui-bg-hover);
}

.library-item-id {
  color: var(--muted2);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.library-empty {
  margin-top: 12px;
  color: var(--muted2);
  font-size: 0.9rem;
}

.cover-open body {
  overflow: hidden;
}

.page {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 24px 18px 160px;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  position: sticky;
  top: 0;
  padding: 6px 0 8px;
  margin-left: -10px;
  background: var(--bg);
  margin-bottom: 12px;
  border-bottom: 1px solid var(--divider);
}

.brand {
  min-width: 0;
  display: flex;
  align-items: center;
  margin-left: auto;
  text-align: right;
}

.title {
  font-size: 0.95rem;
  color: var(--muted2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.reset {
  appearance: none;
  border: 1px solid var(--ui-border);
  background: var(--ui-bg);
  color: var(--muted2);
  padding: 6px;
  border-radius: 10px;
  cursor: pointer;
  line-height: 0;
}
.reset:hover {
  background: var(--ui-bg-hover);
  color: var(--fg);
}

.settings-icon {
  width: 18px;
  height: 18px;
  display: block;
}

.settings-panel {
  position: fixed;
  top: 0;
  left: 0;
  width: min(80vw, 280px);
  height: 100vh;
  padding: 0 16px 16px;
  background: var(--bg);
  border-right: 1px solid var(--divider);
  transform: translateX(-100%);
  transition: transform 0.2s ease;
  z-index: 20;
  box-shadow: 6px 0 18px rgba(0, 0, 0, 0.18);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.settings-panel.is-open {
  transform: translateX(0);
}

.settings-backdrop {
  position: fixed;
  inset: 0;
  background: var(--backdrop);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 10;
}

.settings-backdrop.is-open {
  opacity: 1;
  pointer-events: auto;
}

.edge-swipe-guard {
  position: fixed;
  top: 0;
  left: 0;
  width: 16px;
  height: 100vh;
  z-index: 30;
  background: transparent;
  touch-action: none;
}

@media (max-width: 767px) {
  .edge-swipe-guard::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 9px;
    height: 100%;
    border-radius: 999px;
    background: linear-gradient(
      180deg,
      var(--handle-grad-2),
      var(--handle-grad-1),
      var(--handle-grad-2)
    );
    opacity: 0.8;
    pointer-events: none;
  }

  .edge-swipe-guard::before {
    content: "";
    position: absolute;
    top: 40%;
    left: 2px;
    width: 5px;
    height: 2px;
    background: var(--handle-notch);
    border-radius: 2px;
    transform: translateY(-50%);
    box-shadow:
      0 -6px 0 var(--handle-notch),
      0 6px 0 var(--handle-notch);
    opacity: 0.9;
    pointer-events: none;
  }

  .edge-swipe-guard.is-open::after {
    opacity: 0;
  }

  :root.settings-open .edge-swipe-guard::after,
  :root.settings-dragging-open .edge-swipe-guard::after {
    opacity: 0;
  }

  :root.settings-open .edge-swipe-guard::before,
  :root.settings-dragging-open .edge-swipe-guard::before {
    opacity: 0;
  }

  .settings-panel.is-open::after,
  :root.settings-dragging-open .settings-panel::after {
    content: "";
    position: absolute;
    top: 0;
    right: 0;
    width: 9px;
    height: 100%;
    border-radius: 999px;
    background: linear-gradient(
      180deg,
      var(--handle-grad-2),
      var(--handle-grad-1),
      var(--handle-grad-2)
    );
    opacity: 1;
    pointer-events: none;
  }

  .settings-panel.is-open::before,
  :root.settings-dragging-open .settings-panel::before {
    content: "";
    position: absolute;
    top: 40%;
    right: 2px;
    width: 5px;
    height: 2px;
    background: var(--handle-notch);
    border-radius: 2px;
    transform: translateY(-50%);
    box-shadow:
      0 -6px 0 var(--handle-notch),
      0 6px 0 var(--handle-notch);
    opacity: 0.9;
    pointer-events: none;
  }
}

@media (min-width: 768px) and (hover: none) and (pointer: coarse) {
  .edge-swipe-guard::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 9px;
    height: 100%;
    border-radius: 999px;
    background: linear-gradient(
      180deg,
      var(--handle-grad-2),
      var(--handle-grad-1),
      var(--handle-grad-2)
    );
    opacity: 0.8;
    pointer-events: none;
  }

  .edge-swipe-guard::before {
    content: "";
    position: absolute;
    top: 40%;
    left: 2px;
    width: 5px;
    height: 2px;
    background: var(--handle-notch);
    border-radius: 2px;
    transform: translateY(-50%);
    box-shadow:
      0 -6px 0 var(--handle-notch),
      0 6px 0 var(--handle-notch);
    opacity: 0.9;
    pointer-events: none;
  }

  .edge-swipe-guard.is-open::after {
    opacity: 0;
  }

  :root.settings-open .edge-swipe-guard::after,
  :root.settings-dragging-open .edge-swipe-guard::after {
    opacity: 0;
  }

  :root.settings-open .edge-swipe-guard::before,
  :root.settings-dragging-open .edge-swipe-guard::before {
    opacity: 0;
  }
}

.settings-item {
  width: 100%;
  text-align: left;
  padding: 12px 6px;
  border: 0;
  border-bottom: 1px solid var(--divider);
  background: transparent;
  color: var(--fg);
  cursor: pointer;
  font-size: 0.95rem;
}

.settings-item.settings-character-name {
  color: var(--muted2);
  font-weight: 600;
}

.settings-item:hover {
  background: var(--ui-bg);
}

.settings-item:last-of-type {
  border-bottom: 0;
}

.settings-section {
  padding-bottom: 14px;
  margin-bottom: 14px;
  border-bottom: 0;
}

.settings-group-item {
  padding-bottom: 10px;
  margin-bottom: 10px;
  border-bottom: 1px solid var(--divider);
}

.settings-group-end {
  border-bottom: 0;
  padding-bottom: 0;
  margin-bottom: 0;
}

.settings-section:last-of-type {
  margin-bottom: 0;
  padding-bottom: 0;
  border-bottom: 0;
}

.chapters-current {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 10px;
}

.chapters-read {
  border-top: 1px solid var(--divider);
  padding-top: 10px;
}

.chapters-read[style*="display: none"] {
  border-top: 0;
  padding-top: 0;
}

.chapters-read-toggle {
  cursor: pointer;
  list-style: none;
  color: var(--muted2);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
}

.chapters-read-toggle::-webkit-details-marker {
  display: none;
}

.chapters-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 8px;
}

.chapter-item {
  width: 100%;
  text-align: left;
  padding: 8px 4px;
  border: 0;
  background: transparent;
  color: var(--fg);
  cursor: pointer;
  font-size: 0.9rem;
  line-height: 1.2;
  border-bottom: 1px solid var(--divider);
}

.chapters-list .chapter-item {
  font-style: italic;
  color: var(--muted2);
}

.chapters-list .chapter-item:last-child {
  border-bottom: 0;
}

.chapter-item:hover {
  background: var(--ui-bg);
}

.chapter-item.is-disabled {
  cursor: default;
  opacity: 0.55;
}

.chapter-item.is-disabled:hover {
  background: transparent;
}

.chapter-item.is-current {
  border-left: 2px solid var(--accent);
  padding-left: 10px;
  background: var(--accent-soft);
  font-weight: 600;
  color: var(--muted2);
}

#chaptersSection.no-previous #chaptersCurrent .chapter-item {
  border-bottom: 0;
}

#chaptersSection.no-previous + .settings-divider {
  display: none;
}

.chapter-item .chapter-status {
  display: block;
  font-size: 0.7rem;
  color: var(--muted2);
  margin-top: 4px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.story.is-transitioning {
  filter: blur(4px);
  opacity: 0.7;
  transition:
    filter 0.55s ease,
    opacity 0.55s ease;
}

.chapter-end-gap {
  height: 48px;
}

.settings-divider {
  height: 1px;
  margin: 10px 0 10px;
  background: var(--divider);
}

.reset-section {
  margin-top: 18px;
}

.reset-section .settings-item {
  color: var(--muted2);
  font-weight: 400;
}

.settings-lang-section {
  margin-top: 0px;
}

.settings-subtitle {
  font-size: 0.8rem;
  color: var(--muted2);
  margin: 0 0 8px;
}

.settings-book-title {
  font-family: "Baskerville", "Libre Baskerville", Georgia, serif;
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--muted2);
  margin: 0 0 12px;
}

.settings-lang {
  display: grid;
  gap: 8px;
  max-width: 50%;
}

.settings-size-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.settings-toggle-row {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0px;
}

.settings-label {
  font-size: 0.95rem;
  color: var(--fg);
}

.settings-toggle {
  border: 0;
  background: transparent;
  padding: 0;
  cursor: pointer;
}

.settings-toggle-track {
  position: relative;
  display: inline-flex;
  align-items: center;
  width: 78px;
  height: 40px;
  border-radius: 999px;
  border: 1px solid var(--ui-border-strong);
  background: transparent;
  padding: 0 10px;
  box-sizing: border-box;
  transition:
    border-color 0.2s ease,
    background 0.2s ease;
  --toggle-offset: 21px;
  --toggle-shift: 36px;
}

.settings-toggle-icon {
  position: absolute;
  top: 50%;
  width: 20px;
  height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--muted2);
  opacity: 0.45;
  line-height: 0;
  transform: translate(-50%, -50%);
  transition:
    opacity 0.2s ease,
    color 0.2s ease;
}

.settings-toggle-icon svg {
  width: 20px;
  height: 20px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.6;
  display: block;
}

.settings-toggle-moon svg {
  fill: currentColor;
  stroke: none;
}

.settings-toggle-sun {
  left: var(--toggle-offset);
}

.settings-toggle-moon {
  left: calc(var(--toggle-offset) + var(--toggle-shift));
}

.settings-toggle-thumb {
  position: absolute;
  left: 6px;
  top: 50%;
  width: 30px;
  height: 30px;
  border-radius: 999px;
  background: var(--ui-bg);
  border: 1px solid var(--ui-border);
  transform: translate(0, -50%);
  transition:
    transform 0.2s ease,
    background 0.2s ease,
    border-color 0.2s ease;
}

.settings-toggle[data-theme="dark"] .settings-toggle-thumb {
  transform: translate(36px, -50%);
}

.settings-toggle[data-theme="light"] .settings-toggle-sun {
  color: var(--fg);
  opacity: 0.9;
}

.settings-toggle[data-theme="dark"] .settings-toggle-moon {
  color: var(--fg);
  opacity: 0.9;
}

.settings-toggle[data-theme="light"] .settings-toggle-sun {
  color: var(--accent);
  opacity: 0.6;
}

.settings-toggle[data-theme="light"] .settings-toggle-track {
  border-color: var(--ui-border-strong);
}

.settings-toggle[data-theme="light"] .settings-toggle-thumb {
  border-color: var(--ui-border-strong);
  background: var(--accent-soft);
}

.settings-toggle[data-theme="light"] .settings-toggle-moon {
  color: var(--accent);
  opacity: 0.6;
}

.settings-toggle:focus-visible .settings-toggle-track {
  outline: 2px solid var(--fg);
  outline-offset: 3px;
}

.settings-size-text {
  font-size: 0.75rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--muted2);
}

.settings-segmented {
  display: flex;
  border: 1px solid var(--ui-border-strong);
  border-radius: 10px;
  overflow: hidden;
  background: transparent;
}

.settings-segment {
  border: 0;
  background: transparent;
  color: var(--muted2);
  padding: 6px 14px;
  min-width: 36px;
  cursor: pointer;
  font-size: 0.85rem;
  letter-spacing: 0.08em;
}

.settings-segment.is-active {
  background: var(--accent-soft);
  color: var(--muted2);
}

.settings-chip {
  width: 100%;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--ui-border-strong);
  background: transparent;
  color: var(--muted2);
  cursor: pointer;
  font-size: 0.85rem;
  text-align: left;
}

.settings-chip.is-active {
  background: var(--accent-soft);
  border-color: var(--ui-border-strong);
  color: var(--muted2);
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 40;
}

.modal-backdrop.is-open {
  opacity: 1;
  pointer-events: auto;
}

.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 50;
}

.modal.is-open {
  opacity: 1;
  pointer-events: auto;
}

.modal-card {
  width: min(92vw, 420px);
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--divider);
  border-radius: 12px;
  padding: 18px 18px 16px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.25);
}

.modal.image-modal .modal-card {
  width: min(92vw, 820px);
  padding: 12px;
}

.modal-image {
  display: block;
  max-width: 100%;
  max-height: 80vh;
  border-radius: 10px;
}

.modal-title {
  margin: 0 0 8px;
  font-size: 1.05rem;
  font-weight: 600;
}

.modal-text {
  margin: 0 0 16px;
  line-height: 1.6;
  font-size: 0.95rem;
  color: var(--muted);
}

.modal-text p {
  margin: 0 0 0.9em;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.modal-btn {
  appearance: none;
  border: 1px solid var(--ui-border);
  background: var(--ui-bg);
  color: var(--fg);
  padding: 8px 12px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.9rem;
}

.modal-btn:hover {
  background: var(--ui-bg-hover);
}

.modal-btn.primary {
  border-color: var(--ui-border-strong);
}

@media (max-width: 767px) {
  .settings-panel {
    width: 50vw;
    padding-top: 0;
  }

  .reset {
    display: none;
  }

  .modal-card {
    width: min(92vw, 360px);
  }
}

@media (min-width: 768px) and (hover: hover) and (pointer: fine) {
  .settings-book-title {
    margin-top: 12px;
  }
}

@media (hover: none) and (pointer: coarse) {
  .settings-panel {
    width: 50vw;
    padding-top: 0;
  }

  .settings-book-title {
    margin-top: 12px;
  }


  .modal-card {
    width: min(92vw, 360px);
  }
}

/* Tablet */
@media (min-width: 768px) and (max-width: 1024px) {
  .page {
    max-width: 620px;
    padding: 28px 24px 180px;
  }
}

/* Desktop */
@media (min-width: 1025px) {
  .page {
    padding: 32px 24px 200px;
  }
}

.cover {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: var(--bg);
  color: var(--fg);
  display: grid;
  place-items: center;
  text-align: center;
  padding: 24px;
  cursor: pointer;
  touch-action: none;
  overflow: hidden;
}

.cover[hidden] {
  display: none;
}

.cover-image {
  display: none;
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: var(--cover-position, center);
  opacity: 0.95;
}

.cover-inner {
  display: grid;
  gap: 12px;
  position: relative;
  z-index: 1;
}

.cover-title {
  font-size: clamp(2.1rem, 6vw, 3.4rem);
  letter-spacing: 0.02em;
}

.cover-subtitle {
  font-size: 0.95rem;
  text-transform: uppercase;
  letter-spacing: 0.28em;
  color: var(--muted2);
}

.cover-hint {
  position: absolute;
  bottom: 28px;
  width: 100%;
  text-align: center;
  font-size: 0.9rem;
  color: var(--muted);
  letter-spacing: 0.08em;
  z-index: 1;
}

.cover-hint-mobile {
  display: none;
}

.cover-hint-desktop {
  display: block;
}

@media (max-width: 767px) {
  .cover {
    padding: 0;
    cursor: default;
  }

  .cover-image {
    inset: 0;
    object-fit: cover;
    object-position: var(--cover-position, center 6%);
    border-radius: 0;
  }

  .cover-hint-mobile {
    display: block;
  }

  .cover-hint-desktop {
    display: none;
  }
}

@media (min-width: 1025px) {
  .cover-image {
    width: min(80vw, 900px);
    height: clamp(420px, 75vh, 720px);
    inset: 0;
    margin: auto;
    object-fit: contain;
  }
}
