:root {
  --bg: #fbfaf8;
  --bg-raised: #fff;
  --fg: #1c1a17;
  --fg-muted: #6b6560;
  --line: #e4e0da;
  --accent: #7a5c3e;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 8px 24px rgb(0 0 0 / 0.06);
  --radius: 10px;
  --bar-h: 3.25rem;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16151a;
    --bg-raised: #201f26;
    --fg: #eceaf0;
    --fg-muted: #9d97a6;
    --line: #322f3a;
    --accent: #c8a97e;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4), 0 8px 24px rgb(0 0 0 / 0.35);
    color-scheme: dark;
  }
}

/* Manual override wins over the media query in both directions. */
:root[data-theme="light"] {
  --bg: #fbfaf8;
  --bg-raised: #fff;
  --fg: #1c1a17;
  --fg-muted: #6b6560;
  --line: #e4e0da;
  --accent: #7a5c3e;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 8px 24px rgb(0 0 0 / 0.06);
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #16151a;
  --bg-raised: #201f26;
  --fg: #eceaf0;
  --fg-muted: #9d97a6;
  --line: #322f3a;
  --accent: #c8a97e;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.4), 0 8px 24px rgb(0 0 0 / 0.35);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

/* Wins over the id-level layout rules below, which would otherwise re-display
   hidden panels. */
[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

button {
  font: inherit;
  cursor: pointer;
  border-radius: 7px;
  border: 1px solid transparent;
  padding: 0.4em 0.85em;
  transition: background 0.12s ease, border-color 0.12s ease;
}

button.ghost {
  background: transparent;
  color: var(--fg-muted);
  border-color: var(--line);
}

button.ghost:hover {
  background: var(--bg-raised);
  color: var(--fg);
}

/* Fixed width so cycling Auto/Light/Dark doesn't shuffle the bar. */
.theme-toggle {
  min-width: 4.75rem;
}

button.primary {
  background: var(--accent);
  color: var(--bg-raised);
  font-weight: 550;
}

button.primary:hover {
  filter: brightness(1.08);
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* --- shared bar --- */

.bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
  height: var(--bar-h);
  padding: 0 clamp(0.75rem, 3vw, 2rem);
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}

.bar > button {
  flex: none;
  white-space: nowrap;
}

.bar h1 {
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin: 0;
  flex: 1;
}

.bar-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

/* --- shelf --- */

#shelf {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

.storage {
  margin: 0;
  padding: 0.6rem clamp(0.75rem, 3vw, 2rem) 0;
  font-size: 0.8rem;
  color: var(--fg-muted);
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: clamp(1rem, 3vw, 1.75rem);
  padding: 1.25rem clamp(0.75rem, 3vw, 2rem) 4rem;
}

.card {
  position: relative;
  min-width: 0;
}

.card-cover {
  position: relative;
  display: block;
  aspect-ratio: 2 / 3;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg-raised);
  box-shadow: var(--shadow);
  transition: transform 0.14s ease, box-shadow 0.14s ease;
}

.card-cover:hover {
  transform: translateY(-3px);
  box-shadow: 0 2px 4px rgb(0 0 0 / 0.08), 0 14px 32px rgb(0 0 0 / 0.14);
}

.card-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.cover-fallback {
  display: flex;
  align-items: center;
  height: 100%;
  padding: 1rem 0.85rem;
  background: linear-gradient(
    160deg,
    hsl(var(--hue) 30% 42%),
    hsl(calc(var(--hue) + 40) 28% 28%)
  );
  color: #fff;
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.3;
  overflow: hidden;
}

.card-progress {
  position: absolute;
  inset: auto 0 0 0;
  height: 4px;
  background: rgb(0 0 0 / 0.25);
}

.card-progress::after {
  content: "";
  display: block;
  height: 100%;
  width: calc(var(--fraction) * 100%);
  background: var(--accent);
}

.card-title {
  margin: 0.6rem 0 0;
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1.3;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.card-author {
  margin: 0.15rem 0 0;
  font-size: 0.8rem;
  color: var(--fg-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-remove {
  position: absolute;
  top: 0.4rem;
  right: 0.4rem;
  width: 1.75rem;
  height: 1.75rem;
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 1.1rem;
  line-height: 1;
  border-radius: 50%;
  background: rgb(0 0 0 / 0.55);
  color: #fff;
  opacity: 0;
  transition: opacity 0.12s ease;
}

.card:hover .card-remove,
.card-remove:focus-visible {
  opacity: 1;
}

.card-remove:hover {
  background: #b3261e;
}

.empty {
  margin: auto;
  max-width: 34ch;
  padding: 3rem 1.5rem;
  text-align: center;
  color: var(--fg-muted);
}

.empty-title {
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--fg);
  margin: 0 0 0.35rem;
}

.empty code {
  font-size: 0.9em;
  padding: 0.1em 0.35em;
  border-radius: 4px;
  background: var(--bg-raised);
  border: 1px solid var(--line);
}

/* --- reader --- */

#reader {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-rows: var(--bar-h) 1fr auto;
  /* Without an explicit column the implicit one sizes to max-content, and a
     long book title pushes the bar (and the paginator) wider than the screen. */
  grid-template-columns: minmax(0, 1fr);
  background: var(--bg);
}

.reader-bar {
  gap: 1rem;
}

.book-title {
  flex: 1;
  min-width: 0;
  text-align: center;
  font-size: 0.9rem;
  color: var(--fg-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.view-host {
  position: relative;
  min-height: 0;
}

.view-host.loading::after {
  content: "Opening…";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--fg-muted);
}

foliate-view {
  display: block;
  width: 100%;
  height: 100%;
}

.page-nav {
  position: absolute;
  top: var(--bar-h);
  bottom: 3rem;
  width: clamp(2.5rem, 8vw, 5rem);
  border: 0;
  background: transparent;
  color: var(--fg-muted);
  opacity: 0;
  transition: opacity 0.15s ease;
  z-index: 2;
}

.page-nav.prev {
  left: 0;
}

.page-nav.next {
  right: 0;
}

.page-nav::before {
  font-size: 1.5rem;
}

.page-nav.prev::before {
  content: "‹";
}

.page-nav.next::before {
  content: "›";
}

.page-nav:hover,
.page-nav:focus-visible {
  opacity: 1;
}

.reader-foot {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  height: 3rem;
  padding: 0 clamp(0.75rem, 3vw, 2rem);
  border-top: 1px solid var(--line);
  font-size: 0.78rem;
  color: var(--fg-muted);
}

.chapter {
  flex: 0 1 auto;
  min-width: 0;
  max-width: 40%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.slider {
  flex: 1;
  min-width: 0;
  accent-color: var(--accent);
}

.percent {
  flex: none;
  font-variant-numeric: tabular-nums;
}

/* --- table of contents --- */

.toc-panel {
  position: absolute;
  top: var(--bar-h);
  bottom: 0;
  left: 0;
  width: min(22rem, 82vw);
  z-index: 4;
  overflow-y: auto;
  padding: 0.75rem 0.5rem 2rem;
  background: var(--bg-raised);
  border-right: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.dimmer {
  position: absolute;
  inset: 0;
  z-index: 3;
  background: rgb(0 0 0 / 0.35);
}

/* foliate's tree widget builds a nested <ol role="tree">; it sets the indent
   inline per depth and expects the host page to supply the collapse rules. */
#toc-view li,
#toc-view ol {
  margin: 0;
  padding: 0;
  list-style: none;
}

#toc-view a,
#toc-view span {
  display: block;
  margin: 1px 0;
  padding-block: 0.4rem;
  padding-inline-end: 0.6rem;
  border-radius: 6px;
  color: inherit;
  text-decoration: none;
  font-size: 0.88rem;
  line-height: 1.35;
}

#toc-view span {
  color: var(--fg-muted);
}

#toc-view a:hover {
  background: var(--bg);
}

#toc-view [aria-current] {
  background: color-mix(in oklab, var(--accent) 18%, transparent);
  font-weight: 600;
}

#toc-view svg {
  margin-inline-start: -24px;
  padding-inline: 5px 6px;
  fill: currentColor;
  opacity: 0.5;
  cursor: default;
  transition: transform 0.2s ease;
}

#toc-view svg:hover {
  opacity: 1;
}

#toc-view [aria-expanded="false"] svg {
  transform: rotate(-90deg);
}

#toc-view [aria-expanded="false"] + [role="group"] {
  display: none;
}

/* --- overlays --- */

.drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  background: color-mix(in oklab, var(--bg) 82%, transparent);
  backdrop-filter: blur(2px);
  font-size: 1.1rem;
  font-weight: 550;
  pointer-events: none;
}

.drop-overlay span {
  padding: 2rem 2.5rem;
  border: 2px dashed var(--accent);
  border-radius: var(--radius);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 1.5rem;
  transform: translateX(-50%);
  z-index: 20;
  max-width: min(36rem, 92vw);
  padding: 0.65rem 1rem;
  border-radius: 8px;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  font-size: 0.88rem;
}

.toast[data-tone="error"] {
  border-color: #b3261e;
  color: #b3261e;
}

@media (max-width: 600px) {
  /* Page turns are by swipe on touch screens, and the bar needs the room. */
  .page-nav {
    display: none;
  }

  .chapter {
    display: none;
  }

  .bar {
    gap: 0.4rem;
  }

  .reader-bar {
    gap: 0.4rem;
  }

  .bar > button {
    padding: 0.4em 0.6em;
  }

  .theme-toggle {
    min-width: 0;
  }
}
