*,
*::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: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

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

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

.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-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-tap-zone {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 20%;
  z-index: 2;
  cursor: pointer;
}

.reader-tap-zone-left {
  left: 0;
}

.reader-tap-zone-right {
  right: 0;
}

.reader-tap-zone-center {
  left: 20%;
  width: 60%;
}

.reader-viewport {
  flex: 1;
  overflow: hidden;
  padding: 1.5rem 2rem;
  z-index: 1;
}

.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.65;
  color: var(--color-text);
}

.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-measure .chapter-heading-part {
  margin-bottom: 0.35em;
}

.reader-measure .chapter-heading-section {
  margin-bottom: 1.25em;
}

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

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

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

.reader-measure {
  position: absolute;
  left: -9999px;
  top: 0;
  visibility: hidden;
  pointer-events: none;
  font-size: var(--reader-font-size);
  line-height: 1.65;
}

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

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

.reader-progress-track {
  position: relative;
  height: 3px;
  background: var(--color-border);
  border-radius: 2px;
  overflow: visible;
}

.reader-progress-fill {
  height: 100%;
  background: var(--color-text);
  border-radius: 2px;
  width: 0;
  transition: width 0.15s ease;
}

.reader-progress-ticks {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.reader-progress-tick {
  position: absolute;
  top: -2px;
  width: 1px;
  height: 7px;
  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;
}

/* 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);
}

@media (min-width: 768px) {
  .reader-tap-zone-left,
  .reader-tap-zone-right,
  .reader-tap-zone-center {
    display: none;
  }

  .reader-header-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 {
    max-width: var(--layout-header-max-width);
    margin-left: auto;
    margin-right: auto;
    width: 100%;
    padding-left: 2rem;
    padding-right: 2rem;
  }
}
