*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  --color-bg: #ffffff;
  --color-text: #111111;
  --color-muted: #666666;
  --color-border: #e5e5e5;
  --reader-font-size: 18px;
  --layout-header-max-width: 1200px;
  --layout-content-max-width: 800px;
  --font-sans: "Roboto", system-ui, -apple-system, sans-serif;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-sans);
  line-height: 1.5;
}

a {
  color: inherit;
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

.hidden {
  display: none !important;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem 1rem;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  background: var(--color-bg);
  color: var(--color-text);
  font: inherit;
  cursor: pointer;
}

.btn:hover {
  background: #f5f5f5;
}

.btn-primary {
  border-color: var(--color-text);
}

.btn-ghost {
  border-color: transparent;
  background: transparent;
}

.btn-small {
  padding: 0.25rem 0.5rem;
  font-size: 0.875rem;
}

.upload-form input[type="file"] {
  display: none;
}

/* Flash */
.flash {
  margin: 0 auto;
  padding: 0.75rem 1.5rem;
  border-bottom: 1px solid var(--color-border);
  font-size: 0.9rem;
  max-width: var(--layout-header-max-width);
  width: 100%;
}

.flash-notice {
  background: #f8f8f8;
}

.flash-alert {
  background: #fff5f5;
  color: #8b0000;
}

/* Library */
.library-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--color-border);
  max-width: var(--layout-header-max-width);
  margin: 0 auto;
  width: 100%;
}

.library-header h1 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 500;
}

.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 1.5rem;
  padding: 1.5rem;
  max-width: var(--layout-header-max-width);
  margin: 0 auto;
  width: 100%;
}

.library-empty {
  grid-column: 1 / -1;
  color: var(--color-muted);
  text-align: center;
  padding: 3rem 1rem;
}

.book-card {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.book-card-link {
  display: block;
  text-decoration: none;
}

.book-card-link:hover {
  text-decoration: none;
  opacity: 0.85;
}

.book-cover {
  aspect-ratio: 2 / 3;
  background: #f5f5f5;
  border: 1px solid var(--color-border);
  overflow: hidden;
}

.book-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.book-cover-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--color-muted);
  font-size: 0.875rem;
}

.book-title {
  margin: 0.5rem 0 0;
  font-size: 0.95rem;
  font-weight: 500;
  line-height: 1.3;
}

.book-author {
  margin: 0;
  font-size: 0.8rem;
  color: var(--color-muted);
}

/* Reader */
.reader {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  background: var(--color-bg);
}

@media (display-mode: fullscreen) {
  .reader {
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  }
}

.reader-chrome {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 20;
  transform: translateY(-100%);
  transition: transform 0.22s ease;
  pointer-events: none;
}

.reader-chrome--visible {
  transform: translateY(0);
  pointer-events: auto;
}

.reader-chrome-inner {
  background: var(--color-bg);
  border-bottom: 1px solid var(--color-border);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
}

.reader-header-trigger {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 3.5rem;
  z-index: 15;
  display: none;
}

.reader-footer-trigger {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 3.5rem;
  z-index: 15;
  display: none;
}

.reader-header {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.6rem 1rem;
  min-height: 3rem;
}

.reader-header-side {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex: 1;
  min-width: 0;
  z-index: 1;
}

.reader-header-side--left {
  justify-content: flex-start;
}

.reader-header-side--right {
  justify-content: flex-end;
}

.reader-back {
  font-size: 0.9rem;
  white-space: nowrap;
}

.reader-title {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  max-width: min(55vw, 28rem);
  font-size: 1.15rem;
  font-weight: 500;
  line-height: 1.3;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  pointer-events: none;
}

.reader-font-btn {
  font-weight: 600;
  letter-spacing: -0.05em;
}

.reader-font-panel {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 0.5rem 1rem 0.75rem;
  font-size: 0.9rem;
}

.reader-main {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
}

.reader-viewport {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1.5rem 2rem;
  z-index: 1;
}

/* Hide scrollbars on mobile (still scrollable) */
@media (max-width: 767px) {
  .reader-viewport {
    scrollbar-width: none;
  }

  .reader-viewport::-webkit-scrollbar {
    width: 0;
    height: 0;
  }
}

.reader-viewport--cover {
  display: flex;
  align-items: center;
  justify-content: center;
}

.reader-cover-page {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

.reader-cover-page img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.reader-content {
  font-size: var(--reader-font-size);
  line-height: 1.5;
  color: var(--color-text);
}

.reader-content--loading {
  opacity: 0.45;
  pointer-events: none;
}

.reader-content p {
  margin: 0 0 0.75em;
  text-indent: 1.5em;
}

.reader-content p:first-child {
  text-indent: 0;
}

.reader-content h2,
.reader-content h3 {
  margin: 1em 0 0.5em;
  font-weight: 500;
  text-indent: 0;
}

.reader-content h2 {
  font-size: 1.25em;
}

.reader-content .chapter-heading {
  margin: 0;
  font-weight: 500;
  line-height: 1.3;
  text-indent: 0;
  text-align: left;
}

.reader-content .chapter-heading-part {
  margin-bottom: 0.35em;
  font-size: 1.35em;
}

.reader-content .chapter-heading-section {
  margin-bottom: 1.25em;
  font-size: 1.15em;
}

.reader-content h3 {
  font-size: 1.1em;
}

.reader-content img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 1em auto;
}

.reader-content .cite,
.reader-content .epigraph {
  margin: 1em 0;
  padding-left: 1em;
  border-left: 2px solid var(--color-border);
  color: var(--color-muted);
}

.reader-content .annotation {
  margin: 0 0 1.25em;
  padding: 0.75em 1em;
  border-radius: 0.35em;
  background: color-mix(in srgb, var(--color-border) 25%, transparent);
  color: var(--color-muted);
  font-size: 0.95em;
}

.reader-content .text-author {
  margin: 0.75em 0 0;
  text-align: right;
  font-style: italic;
  color: var(--color-muted);
}

.reader-content .poem {
  margin: 1em 0;
}

.reader-content .poem-line {
  margin: 0.15em 0;
  padding-left: 1.5em;
  text-indent: -1.5em;
}

.reader-content code {
  font-family: ui-monospace, monospace;
  font-size: 0.92em;
}

.reader-content table {
  width: 100%;
  margin: 1em 0;
  border-collapse: collapse;
  font-size: 0.95em;
}

.reader-content th,
.reader-content td {
  border: 1px solid var(--color-border);
  padding: 0.4em 0.6em;
  vertical-align: top;
}

.reader-content th {
  font-weight: 600;
  background: color-mix(in srgb, var(--color-border) 20%, transparent);
}

.reader-content .scene-break {
  text-align: center;
  margin: 1.5em 0;
  letter-spacing: 0.2em;
  color: var(--color-muted);
}

.reader-footer {
  flex-shrink: 0;
  padding: 0.5rem 1rem 1rem;
  border-top: 1px solid var(--color-border);
}

.reader-progress-track {
  position: relative;
  height: 1.5rem;
  cursor: pointer;
  touch-action: manipulation;
}

.reader-progress-track::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  height: 3px;
  margin-top: -1.5px;
  background: var(--color-border);
  border-radius: 2px;
}

.reader-progress-fill {
  position: absolute;
  left: 0;
  top: 50%;
  height: 3px;
  margin-top: -1.5px;
  background: var(--color-text);
  border-radius: 2px;
  width: 0;
  transition: width 0.15s ease;
}

.reader-progress-ticks {
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  height: 3px;
  margin-top: -1.5px;
  pointer-events: none;
}

.reader-progress-tick {
  position: absolute;
  top: 50%;
  width: 1px;
  height: 7px;
  margin-top: -3.5px;
  background: var(--color-muted);
  transform: translateX(-50%);
}

.reader-progress-label {
  margin: 0.35rem 0 0;
  font-size: 0.75rem;
  color: var(--color-muted);
  text-align: center;
}

.reader-footnote-back {
  display: block;
  margin-top: 0.75rem;
}

.reader-content a[href^="#"] {
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

/* Chapters panel */
.chapters-panel {
  position: fixed;
  inset: 0;
  z-index: 100;
}

.chapters-panel-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.2);
}

.chapters-panel-content {
  position: absolute;
  top: 0;
  right: 0;
  width: min(320px, 85vw);
  height: 100%;
  background: var(--color-bg);
  border-left: 1px solid var(--color-border);
  padding: 1rem;
  overflow-y: auto;
}

.chapters-panel-content h2 {
  margin: 0 0 1rem;
  font-size: 1rem;
  font-weight: 500;
}

.chapters-panel-content ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.chapters-panel-item {
  display: block;
  width: 100%;
  padding: 0.6rem 0;
  border: none;
  border-bottom: 1px solid var(--color-border);
  background: none;
  text-align: left;
  font: inherit;
  cursor: pointer;
}

.chapters-panel-item:hover {
  color: var(--color-muted);
}

.chapters-panel-item--current {
  font-weight: 600;
  background: #f5f5f5;
  margin: 0 -1rem;
  padding-left: 1rem;
  padding-right: 1rem;
  border-bottom-color: transparent;
}

@media (max-width: 767px) {
  .reader-viewport {
    padding-left: 10px;
    padding-right: 10px;
  }

  .reader-footer {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 20;
    transform: translateY(100%);
    transition: transform 0.22s ease;
    pointer-events: none;
    background: var(--color-bg);
    box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.06);
  }

  .reader-footer.reader-chrome--visible {
    transform: translateY(0);
    pointer-events: auto;
  }

  .reader-header {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
    align-items: center;
    gap: 0.35rem 0.5rem;
    padding: 0.5rem 0.75rem;
  }

  .reader-header-side--left {
    grid-column: 1;
    grid-row: 1;
    flex: none;
  }

  .reader-header-side--right {
    grid-column: 2;
    grid-row: 1;
    flex: none;
    gap: 0.25rem;
  }

  .reader-header-side--right .btn {
    padding: 0.35rem 0.5rem;
    font-size: 0.85rem;
  }

  .reader-title {
    position: static;
    transform: none;
    grid-column: 1 / -1;
    grid-row: 2;
    max-width: 100%;
    width: 100%;
    font-size: 1rem;
    pointer-events: none;
  }

  .reader-back {
    font-size: 0.85rem;
  }
}

@media (min-width: 768px) {
  .reader--page-scroll {
    height: auto;
    min-height: 100dvh;
    overflow: visible;
  }

  .reader--page-scroll .reader-main {
    flex: none;
  }

  .reader--page-scroll .reader-viewport {
    overflow: visible;
  }

  .reader--page-scroll .reader-viewport--cover {
    min-height: 100dvh;
  }

  .reader-header-trigger,
  .reader-footer-trigger {
    display: block;
  }

  .reader-chrome-inner {
    max-width: var(--layout-header-max-width);
    margin-left: auto;
    margin-right: auto;
  }

  .reader-header {
    padding-left: 2rem;
    padding-right: 2rem;
  }

  .reader-main {
    max-width: var(--layout-content-max-width);
    margin-left: auto;
    margin-right: auto;
    width: 100%;
  }

  .reader-footer {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 20;
    transform: translateY(100%);
    transition: transform 0.22s ease;
    pointer-events: none;
    background: var(--color-bg);
    box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.06);
    max-width: var(--layout-header-max-width);
    margin-left: auto;
    margin-right: auto;
    width: 100%;
    padding-left: 2rem;
    padding-right: 2rem;
  }

  .reader-footer.reader-chrome--visible {
    transform: translateY(0);
    pointer-events: auto;
  }
}
