/* ==========================================================================
   BookOrbit stats: themes, layout, charts. No external resources.
   ========================================================================== */

/* ---- Themes ---------------------------------------------------------- */
/* Claret (default): deep wine background with a red accent. */
:root,
[data-theme='claret'] {
  --bg: #1d0b0e;
  --surface: #2a1116;
  --surface-2: #3a171e;
  --text: #f6e9e8;
  --muted: #c9a6a4;
  --border: #4c222a;
  --accent: #ff7a6e;
  --c1: #ff7a6e;
  --c2: #e9c46a;
  --c3: #8fb8de;
  --c4: #f4a261;
  --heat0: #3a171e;
  --heat1: #6b2330;
  --heat2: #95303f;
  --heat3: #c4465a;
  --heat4: #ff7a6e;
  --shadow: 0 1px 0 rgb(255 255 255 / 0.03) inset, 0 10px 30px -12px rgb(0 0 0 / 0.6);
  color-scheme: dark;
}

[data-theme='forest'] {
  --bg: #0d1e18;
  --surface: #14291f;
  --surface-2: #1b3529;
  --text: #e8f1ec;
  --muted: #9bb3a6;
  --border: #26403a;
  --accent: #7fd1a0;
  --c1: #7fd1a0;
  --c2: #e8c770;
  --c3: #7fb8d9;
  --c4: #e58f7a;
  --heat0: #1b3529;
  --heat1: #235a40;
  --heat2: #2e7a55;
  --heat3: #46a66f;
  --heat4: #7fd1a0;
  --shadow: 0 1px 0 rgb(255 255 255 / 0.03) inset, 0 10px 30px -12px rgb(0 0 0 / 0.5);
  color-scheme: dark;
}

[data-theme='midnight'] {
  --bg: #0b1020;
  --surface: #131b33;
  --surface-2: #1a2444;
  --text: #e6e9f5;
  --muted: #9aa4c7;
  --border: #25305a;
  --accent: #8ab4ff;
  --c1: #8ab4ff;
  --c2: #f2c46d;
  --c3: #b59cff;
  --c4: #ff8fa3;
  --heat0: #1a2444;
  --heat1: #243b75;
  --heat2: #3558a8;
  --heat3: #5a7fd6;
  --heat4: #8ab4ff;
  --shadow: 0 10px 30px -12px rgb(0 0 0 / 0.6);
  color-scheme: dark;
}

[data-theme='paper'] {
  --bg: #f6f3ea;
  --surface: #fffdf7;
  --surface-2: #efeadb;
  --text: #1f2a24;
  --muted: #5d6b63;
  --border: #dcd5c2;
  --accent: #2f7a55;
  --c1: #2f7a55;
  --c2: #b8862b;
  --c3: #3b6e8f;
  --c4: #b35c44;
  --heat0: #ebe6d6;
  --heat1: #bfe0c9;
  --heat2: #86c49d;
  --heat3: #4fa374;
  --heat4: #2f7a55;
  --shadow: 0 1px 2px rgb(60 50 20 / 0.06), 0 10px 24px -16px rgb(60 50 20 / 0.25);
  color-scheme: light;
}

[data-theme='sepia'] {
  --bg: #f3e9d7;
  --surface: #faf3e4;
  --surface-2: #ebdfc6;
  --text: #3b2e1e;
  --muted: #7a6850;
  --border: #d8c8a8;
  --accent: #8a4b1f;
  --c1: #8a4b1f;
  --c2: #b08a2e;
  --c3: #4f7a7a;
  --c4: #a33d3d;
  --heat0: #ebdfc6;
  --heat1: #d9b98a;
  --heat2: #c08f55;
  --heat3: #9a6330;
  --heat4: #6e3f17;
  --shadow: 0 1px 2px rgb(80 55 20 / 0.08), 0 10px 24px -16px rgb(80 55 20 / 0.3);
  color-scheme: light;
}

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

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font: 400 1rem/1.5 ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  transition: background-color 0.25s ease, color 0.25s ease;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: clamp(1.25rem, 3.5vw, 1.6rem);
  line-height: 1.2;
  letter-spacing: -0.01em;
}

h2 {
  font-size: 1.05rem;
  font-weight: 650;
}

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.skip {
  position: absolute;
  left: 12px;
  top: -60px;
  padding: 10px 14px;
  background: var(--c2);
  color: #111;
  border-radius: 8px;
  z-index: 100;
  text-decoration: none;
}

.skip:focus {
  top: 12px;
}

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

/* ---- Top bar --------------------------------------------------------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px 16px;
  padding: 12px clamp(14px, 4vw, 40px);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.logo {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: var(--surface-2);
  font-size: 1.25rem;
  flex: none;
}

.sub {
  color: var(--muted);
  font-size: 0.70rem;
}

.controls {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

select,
input[type='search'] {
  min-height: 44px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
}

select {
  padding-right: 32px;
}

input[type='search'] {
  width: 100%;
  min-width: 0;
}

/* ---- Layout ---------------------------------------------------------- */
main {
  max-width: 1200px;
  margin: 0 auto;
  padding: clamp(14px, 3vw, 28px) clamp(14px, 4vw, 40px) 40px;
}

.status {
  padding: 24px;
  color: var(--muted);
  text-align: center;
}

.status.error {
  color: var(--c4);
}

.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
}

.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  box-shadow: var(--shadow);
  min-width: 0;
}

.kpi .label {
  color: var(--muted);
  font-size: 0.85rem;
}

.kpi .value {
  margin-top: 4px;
  font-size: clamp(1.45rem, 4vw, 2rem);
  font-weight: 750;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  overflow-wrap: anywhere;
}

.kpi .note {
  color: var(--muted);
  font-size: 0.8rem;
}

.grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  margin-top: 16px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: clamp(16px, 2.5vw, 22px);
  box-shadow: var(--shadow);
  min-width: 0;
}

.card-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 4px 12px;
  margin-bottom: 4px;
}

.hint {
  color: var(--muted);
  font-size: 0.85rem;
  margin-bottom: 12px;
}

.chart-wrap {
  position: relative;
  width: 100%;
}

.chart {
  display: block;
  width: 100%;
  height: auto;
  overflow: visible;
}

/* Chart primitives */
.gridline {
  stroke: var(--border);
  stroke-width: 1;
}

.axis {
  fill: var(--muted);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.bar.c1,
.line.c1,
.dot.c1,
.seg.c1 {
  stroke: none;
}

.bar.c1 { fill: var(--c1); }
.bar.c2 { fill: var(--c2); }
.bar.c3 { fill: var(--c3); }
.bar.c4 { fill: var(--c4); }
.bar { transition: opacity 0.15s ease; }
.bar:hover { opacity: 0.8; }

.line {
  fill: none;
  stroke-width: 2.5;
  stroke-linejoin: round;
  stroke-linecap: round;
}
.line.c1 { stroke: var(--c1); }
.line.c2 { stroke: var(--c2); }
.line.c3 { stroke: var(--c3); }
.line.c4 { stroke: var(--c4); }
.line.dashed { stroke-dasharray: 6 6; stroke-width: 2; }

.area.c1 { fill: var(--c1); opacity: 0.16; }
.area.c2 { fill: var(--c2); opacity: 0.12; }

.dot { stroke: none; }
.dot.c1 { fill: var(--c1); }
.dot.c2 { fill: var(--c2); }
.dot.c3 { fill: var(--c3); }
.dot.c4 { fill: var(--c4); }

.scatter .dot { fill-opacity: 0.55; }

.track { stroke: var(--surface-2); }
.seg.c1 { stroke: var(--c1); }
.seg.c2 { stroke: var(--c2); }
.seg.c3 { stroke: var(--c3); }
.seg.c4 { stroke: var(--c4); }
.donut-center {
  fill: var(--text);
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.lvl0 { fill: var(--heat0); }
.lvl1 { fill: var(--heat1); }
.lvl2 { fill: var(--heat2); }
.lvl3 { fill: var(--heat3); }
.lvl4 { fill: var(--heat4); }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  margin-top: 10px;
  color: var(--muted);
  font-size: 0.82rem;
}

.legend span::before {
  content: '';
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 6px;
  border-radius: 3px;
  background: var(--swatch, var(--c1));
  vertical-align: -1px;
}

.swatch-heat span::before {
  background: none;
}

.heat-scale {
  display: inline-flex;
  gap: 3px;
  vertical-align: middle;
}

.heat-scale i {
  width: 11px;
  height: 11px;
  border-radius: 2px;
}

.donut-wrap {
  display: grid;
  grid-template-columns: minmax(150px, 200px) 1fr;
  gap: 16px;
  align-items: center;
}

.donut-wrap .legend {
  flex-direction: column;
  gap: 10px;
  color: var(--text);
  font-size: 0.95rem;
}

.donut-wrap .legend span {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
}

.donut-wrap .legend b {
  font-variant-numeric: tabular-nums;
}

/* Horizontal bar rows (HTML, so they reflow and are screen-reader friendly) */
.rows {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.row {
  display: grid;
  grid-template-columns: minmax(0, 38%) 1fr auto;
  gap: 10px;
  align-items: center;
  font-size: 0.92rem;
}

.row .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.row .val {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.hbar {
  height: 10px;
  border-radius: 99px;
  background: var(--surface-2);
  overflow: hidden;
}

.hbar > i {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--c1);
  transition: width 0.4s ease;
}

.hbar > i.c2 { background: var(--c2); }
.hbar > i.c3 { background: var(--c3); }

/* Records */
.records {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 12px;
}

.record {
  background: var(--surface-2);
  border-radius: 14px;
  padding: 14px;
  min-width: 0;
}

.record .label {
  color: var(--muted);
  font-size: 0.8rem;
}

.record .value {
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--c2);
  font-variant-numeric: tabular-nums;
}

.record .title {
  font-size: 0.9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Book list */
.book-tools {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin-bottom: 12px;
}

@media (min-width: 640px) {
  .book-tools {
    grid-template-columns: 1fr auto;
  }
}

.books {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

@media (min-width: 640px) {
  .books {
    grid-template-columns: 1fr 1fr;
  }
}

@media (min-width: 1000px) {
  .books {
    grid-template-columns: 1fr 1fr 1fr;
  }
}

.book {
  background: var(--surface-2);
  border-radius: 12px;
  padding: 12px 14px;
  min-width: 0;
}

.book .title {
  font-weight: 600;
  overflow-wrap: anywhere;
}

.book .author {
  color: var(--muted);
  font-size: 0.85rem;
  overflow-wrap: anywhere;
}

.book .meta {
  margin-top: 6px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
  color: var(--muted);
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
}

.pill {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 99px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
}

.more {
  margin-top: 14px;
  display: flex;
  justify-content: center;
}

.btn {
  min-height: 44px;
  padding: 0 18px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.btn:hover {
  border-color: var(--accent);
}

.empty {
  color: var(--muted);
  font-style: italic;
  padding: 12px 0;
}

footer {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 clamp(14px, 4vw, 40px) 32px;
  color: var(--muted);
  font-size: 0.8rem;
}

/* ---- Responsive ------------------------------------------------------ */
@media (min-width: 860px) {
  .grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .card.full {
    grid-column: 1 / -1;
  }
}

@media (max-width: 520px) {
  .donut-wrap {
    grid-template-columns: 1fr;
    justify-items: center;
  }

  .donut-wrap .legend {
    width: 100%;
  }

  .row {
    grid-template-columns: minmax(0, 42%) 1fr auto;
  }

  .topbar .controls {
    width: 100%;
  }

  .topbar .controls label {
    flex: 1;
  }

  .topbar select {
    width: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition: none !important;
    animation: none !important;
  }
}

@media print {
  .topbar .controls,
  .more,
  .book-tools {
    display: none;
  }

  body {
    background: #fff;
    color: #000;
  }

  .card,
  .kpi {
    box-shadow: none;
    break-inside: avoid;
  }
}

/* ---- Additions: utilities, forms, calendar, key-value lists ---------- */
[hidden] {
  display: none !important;
}

.field {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 0.85rem;
}

.hero {
  margin-bottom: 4px;
}

.hero p {
  color: var(--muted);
  margin-top: 6px;
}

.logo svg {
  width: 24px;
  height: 24px;
  color: var(--accent);
}

.chart-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 4px;
}

.heat .chart {
  width: 880px;
  max-width: none;
}

.legend-heat {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.8rem;
}

.legend-heat i {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  display: inline-block;
}

dl.kv {
  margin: 0;
  display: grid;
  gap: 2px;
}

dl.kv div {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

dl.kv div:last-child {
  border-bottom: 0;
}

dl.kv dt {
  color: var(--muted);
}

dl.kv dd {
  margin: 0;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}

.search-input {
  min-height: 44px;
}

.count {
  color: var(--muted);
  font-size: 0.85rem;
  align-self: center;
}

.book-tools {
  align-items: center;
}

.muted {
  color: var(--muted);
}

/* Legend swatches and heat-scale keys (classes, not inline styles, for CSP) */
.legend .sw::before { background: var(--c1); }
.legend .sw.c1::before { background: var(--c1); }
.legend .sw.c2::before { background: var(--c2); }
.legend .sw.c3::before { background: var(--c3); }
.legend .sw.c4::before { background: var(--c4); }
.heat-scale .lvl0 { background: var(--heat0); }
.heat-scale .lvl1 { background: var(--heat1); }
.heat-scale .lvl2 { background: var(--heat2); }
.heat-scale .lvl3 { background: var(--heat3); }
.heat-scale .lvl4 { background: var(--heat4); }
.legend-heat .heat-scale i { display: inline-block; }

/* Heatmap sits in a scrollable box on phones; fill the card on wide screens. */
@media (min-width: 860px) {
  .heat .chart {
    width: 100%;
    max-width: 960px;
  }
}

/* Slightly larger axis text on phones, where the chart is scaled down. */
@media (max-width: 520px) {
  .axis {
    font-size: 13px;
  }
}

/* ==========================================================================
   Multi-page additions: navigation, refresh, shelf, ranked lists, dialog
   ========================================================================== */

.brand-text { min-width: 0; }
.brand-text h1 { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.logo { color: var(--accent); text-decoration: none; }
.logo:hover { border-color: var(--accent); }

.nav {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.nav a {
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  padding: 0 14px;
  border-radius: 10px;
  color: var(--muted);
  text-decoration: none;
  border: 1px solid transparent;
}

.nav a:hover { color: var(--text); border-color: var(--border); }

.nav a[aria-current='page'] {
  color: var(--text);
  background: var(--surface-2);
  border-color: var(--border);
  font-weight: 600;
}

.controls .btn { align-self: stretch; }

.refresh-note {
  width: 100%;
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
  min-height: 0;
}

.refresh-note:empty { display: none; }

.btn:disabled { opacity: 0.6; cursor: progress; }

.sub a, .muted a { color: var(--accent); }

/* Ranked lists (Records) */
.ranked {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 12px;
}

.rank {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 4px 12px;
  align-items: center;
}

.rank-main { min-width: 0; }
.rank-main .title { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-main .author { display: block; color: var(--muted); font-size: 0.82rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank .val { color: var(--c2); font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap; }
.rank-bar { grid-column: 1 / -1; height: 6px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.rank-bar > i { display: block; height: 100%; background: var(--c1); border-radius: inherit; }

/* Detail dialog */
.detail {
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100vh - 24px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.detail::backdrop { background: rgb(0 0 0 / 0.55); }

.detail-head {
  display: flex;
  justify-content: flex-end;
  padding: 12px 12px 0;
}

#detail-body {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  padding: 0 20px 20px;
}

@media (min-width: 520px) {
  #detail-body { grid-template-columns: 150px 1fr; }
}

.detail-cover img {
  width: 100%;
  max-width: 180px;
  height: auto;
  border-radius: 8px;
  box-shadow: 0 10px 22px -10px rgb(0 0 0 / 0.6);
}

.detail-text h3 { font-size: 1.2rem; margin: 0 0 4px; overflow-wrap: anywhere; }

/* Keep .kv rows readable inside the dialog */
.detail dl.kv div { padding: 8px 0; }

/* Mobile nav: stack under the brand, full width */
@media (max-width: 520px) {
  .nav { width: 100%; }
  .nav a { flex: 1; justify-content: center; padding: 0 8px; font-size: 0.9rem; }
  .controls .btn { min-height: 44px; }
}


/* ==========================================================================
   Bookshelf: timber case with walls, back panel, planks and printed spines.
   ========================================================================== */
.shelf-case {
  --wall: 18px;
  --wood: #6b4a2f;
  --wood-dark: #3b2615;
  --wood-light: #9a7350;
  --back: #22160b;
  --seam: rgb(0 0 0 / 0.35);
  --grain: rgb(20 10 0 / 0.2);
  --plank-shadow: rgb(0 0 0 / 0.6);
  --gold: rgb(214 178 98 / 0.85);
  position: relative;
  padding: 18px var(--wall) 16px;
  border-radius: 6px;
  background-color: var(--back);
  background-image:
    linear-gradient(180deg, var(--wood-light) 0 3px, var(--wood) 3px 13px, var(--wood-dark) 13px 18px, transparent 18px),
    linear-gradient(180deg, rgb(0 0 0 / 0.45), transparent),
    linear-gradient(0deg, var(--wood-dark) 0 16px, var(--wood) 16px 17px, transparent 17px),
    repeating-linear-gradient(90deg, var(--seam) 0 2px, transparent 2px 92px),
    repeating-linear-gradient(92deg, transparent 0 13px, var(--grain) 13px 14px, transparent 14px 37px);
  background-size: 100% 18px, 100% 40px, 100% 17px, auto, auto;
  background-position: top left, top left, bottom left, 0 0, 0 0;
  background-repeat: no-repeat, no-repeat, no-repeat, repeat, repeat;
  box-shadow: inset 0 0 0 1px rgb(0 0 0 / 0.4), 0 18px 28px -18px rgb(0 0 0 / 0.7);
}

/* Side walls of the case. Planks slot into them, so they sit in front. */
.shelf-case::before,
.shelf-case::after {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  width: var(--wall);
  background: linear-gradient(90deg, var(--wood-dark), var(--wood-light) 30%, var(--wood) 65%, var(--wood-dark));
  pointer-events: none;
}
.shelf-case::before { left: 0; border-radius: 6px 0 0 6px; box-shadow: inset -3px 0 4px rgb(0 0 0 / 0.35); }
.shelf-case::after { right: 0; border-radius: 0 6px 6px 0; box-shadow: inset 3px 0 4px rgb(0 0 0 / 0.35); }

/* One tier = one row of books standing on one plank. */
.shelf-tier {
  position: relative;
  z-index: 1;
  margin: 0 0 34px;
}
.shelf-tier:last-child { margin-bottom: 0; }

.shelf-tier::after {
  content: '';
  position: absolute;
  left: calc(-1 * var(--wall));
  right: calc(-1 * var(--wall));
  bottom: -16px;
  height: 16px;
  border-radius: 1px 1px 3px 3px;
  background-color: var(--wood);
  background-image:
    linear-gradient(180deg, rgb(255 255 255 / 0.25) 0 2px, transparent 2px),
    repeating-linear-gradient(90deg, transparent 0 210px, var(--seam) 210px 212px),
    repeating-linear-gradient(178deg, transparent 0 3px, rgb(0 0 0 / 0.1) 3px 4px, transparent 4px 9px),
    linear-gradient(180deg, var(--wood-light), var(--wood) 40%, var(--wood-dark));
  box-shadow: 0 10px 14px -6px var(--plank-shadow), inset 0 -2px 0 rgb(0 0 0 / 0.35);
}

.shelf-row {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  align-items: flex-end;
  gap: 2px;
  min-height: 0;
}
.shelf-row > li { display: block; flex: none; list-style: none; }

/* A book. Height, width and tone vary per book (set from JS via custom properties). */
.spine {
  all: unset;
  position: relative;
  display: block;
  box-sizing: border-box;
  flex: none;
  width: var(--w, 26px);
  height: var(--h, 166px);
  border-radius: 2px 2px 0 0;
  overflow: hidden;
  cursor: pointer;
  background-color: var(--spine, #5a6b5e);
  background-image: repeating-linear-gradient(0deg, rgb(255 255 255 / 0.035) 0 1px, transparent 1px 3px);
  filter: brightness(var(--tone, 1));
  box-shadow: 1px 0 0 rgb(0 0 0 / 0.4), 2px 0 3px -1px rgb(0 0 0 / 0.5), inset 0 -3px 0 rgb(0 0 0 / 0.18);
  transform-origin: bottom center;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.spine:hover {
  transform: translateY(-8px) rotate(-1.2deg);
  box-shadow: 1px 0 0 rgb(0 0 0 / 0.4), 4px 14px 14px -6px rgb(0 0 0 / 0.6);
}
.spine:focus-visible { outline: 3px solid var(--c2); outline-offset: 3px; }

/* Gold bands near the head and tail of the spine. */
.spine::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
  opacity: 0;
}
.spine.ribs-1::before,
.spine.ribs-2::before {
  opacity: 1;
  background-image: linear-gradient(180deg,
    transparent 9px, var(--gold) 9px 11px, transparent 11px,
    transparent calc(100% - 15px), var(--gold) calc(100% - 15px) calc(100% - 13px), transparent calc(100% - 13px));
}
.spine.ribs-2::before {
  background-image: linear-gradient(180deg,
    transparent 9px, var(--gold) 9px 11px, transparent 11px, transparent 14px, var(--gold) 14px 15px, transparent 15px,
    transparent calc(100% - 19px), var(--gold) calc(100% - 19px) calc(100% - 18px), transparent calc(100% - 18px),
    transparent calc(100% - 15px), var(--gold) calc(100% - 15px) calc(100% - 13px), transparent calc(100% - 13px));
}

/* Cylinder shading: a rounded spine catches light on one side and falls into shadow on the other. */
.spine::after {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;
  background: linear-gradient(90deg,
    rgb(0 0 0 / 0.45) 0%, rgb(255 255 255 / 0.18) 14%, rgb(255 255 255 / 0.04) 32%,
    rgb(0 0 0 / 0) 50%, rgb(0 0 0 / 0.12) 78%, rgb(0 0 0 / 0.5) 100%);
}

.spine-stack { position: absolute; inset: 0; z-index: 1; }
.spine-fallback {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px 2px;
}

/* Printed label on the spine. */
.spine-title {
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  max-height: calc(100% - 48px);
  padding: 7px 3px;
  border-radius: 2px;
  background: linear-gradient(90deg, #efe4c6, #fbf5e4 45%, #e7dbb8);
  color: #1d1a16;
  box-shadow: 0 0 0 1px rgb(80 55 20 / 0.5), 0 1px 2px rgb(0 0 0 / 0.45);
  font-family: ui-serif, 'Iowan Old Style', Georgia, 'Times New Roman', serif;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1.15;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Cover spines: the cover is a dimmed texture under the printed label. */
.spine.has-cover img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
  filter: saturate(0.8) brightness(0.5) blur(0.6px);
  transform: scale(1.1);
  opacity: 0;
  transition: opacity 0.3s ease;
}
.spine.has-cover img.ready { opacity: 1; }

.shelf-empty { color: var(--muted); font-style: italic; padding: 24px 8px; }

/* Timber tones for light themes. */
[data-theme='paper'] .shelf-case,
[data-theme='sepia'] .shelf-case {
  --wood: #b7895c;
  --wood-dark: #7f5a34;
  --wood-light: #d6b184;
  --back: #cfae83;
  --seam: rgb(80 50 20 / 0.25);
  --grain: rgb(90 60 30 / 0.14);
  --plank-shadow: rgb(60 40 20 / 0.4);
}

/* Phones: slimmer walls so more of the shelf fits. */
@media (max-width: 520px) {
  .shelf-case { --wall: 10px; padding-top: 14px; }
  .shelf-case::before, .shelf-case::after { border-radius: 4px 0 0 4px; }
  .shelf-case::after { border-radius: 0 4px 4px 0; }
}

@media (prefers-reduced-motion: reduce) {
  .spine, .spine img { transition: none !important; }
}

/* Realism pass: hinge grooves, rounded head and tail, and labels that use the spine. */
.spine { border-radius: 3px 3px 0 0; }
.spine::before { border-radius: inherit; }
.spine-fallback::before,
.spine-fallback::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  height: 6px;
  pointer-events: none;
  z-index: 5;
}
.spine-fallback::before {
  top: 4px;
  background: linear-gradient(180deg, rgb(0 0 0 / 0.35), transparent);
}
.spine-fallback::after {
  bottom: 4px;
  background: linear-gradient(0deg, rgb(0 0 0 / 0.35), transparent);
}

.spine::after {
  background:
    linear-gradient(90deg, transparent 0 3px, rgb(0 0 0 / 0.5) 3px 4px, rgb(255 255 255 / 0.12) 4px 5px, transparent 5px calc(100% - 5px), rgb(0 0 0 / 0.5) calc(100% - 5px) calc(100% - 4px), transparent calc(100% - 4px)),
    linear-gradient(90deg, rgb(0 0 0 / 0.45) 0%, rgb(255 255 255 / 0.18) 14%, rgb(255 255 255 / 0.04) 32%, rgb(0 0 0 / 0) 50%, rgb(0 0 0 / 0.12) 78%, rgb(0 0 0 / 0.5) 100%);
}

/* Labels: take most of the spine, and shrink the type for long titles. */
.spine-title {
  max-height: calc(100% - 22px);
  font-size: 0.66rem;
  padding: 6px 2px;
}
.spine[data-len='long'] .spine-title { font-size: 0.56rem; }
.spine[data-len='xlong'] .spine-title { font-size: 0.5rem; letter-spacing: 0; }

/* Depth: the back is in shadow, each plank casts shade onto the tier below,
   and the base is a thicker board. */
.shelf-case {
  background-image:
    linear-gradient(180deg, rgb(0 0 0 / 0.55), rgb(0 0 0 / 0.15) 40%, rgb(0 0 0 / 0.35)),
    repeating-linear-gradient(90deg, rgb(0 0 0 / 0.25) 0 1px, transparent 1px 62px),
    repeating-linear-gradient(0deg, rgb(255 255 255 / 0.03) 0 1px, transparent 1px 9px),
    linear-gradient(180deg, var(--back), var(--back));
  background-size: 100% 100%, auto, auto, 100% 100%;
  background-position: 0 0;
  background-repeat: no-repeat, repeat, repeat, no-repeat;
}

/* Shadow cast by each plank onto the books of the tier below it. */
.shelf-tier + .shelf-tier::before {
  content: '';
  position: absolute;
  left: calc(-1 * var(--wall));
  right: calc(-1 * var(--wall));
  top: -34px;
  height: 22px;
  pointer-events: none;
  background: linear-gradient(180deg, transparent, rgb(0 0 0 / 0.45));
  z-index: 2;
}

/* The base board is thicker and darker than the shelves. */
.shelf-tier:last-child::after {
  height: 26px;
  bottom: -26px;
  box-shadow: 0 16px 22px -8px rgb(0 0 0 / 0.75), inset 0 -3px 0 rgb(0 0 0 / 0.4);
}

/* Donut legend: swatch, name and value in a grid so the swatch sits right beside its label. */
.donut-wrap .legend .sw {
  display: grid;
  grid-template-columns: 12px minmax(0, 1fr) auto;
  align-items: center;
  column-gap: 10px;
  width: 100%;
}
.donut-wrap .legend .sw::before {
  width: 12px;
  height: 12px;
  margin: 0;
  vertical-align: baseline;
}

/* Sign-in page */
.login-body { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-main { width: 100%; display: grid; place-items: center; }
.login-card {
  width: min(400px, 100%);
  padding: clamp(22px, 5vw, 34px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow);
  text-align: center;
}
.login-card .logo { margin: 0 auto 14px; }
.login-card h1 { font-size: 1.5rem; }
.login-card > .muted { margin: 4px 0 20px; }
.login-form { display: grid; gap: 14px; text-align: left; }
.login-field { display: grid; gap: 6px; font-size: 0.9rem; color: var(--muted); }
.login-field input {
  min-height: 46px;
  padding: 0 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
}
.login-field input:focus-visible { outline: 3px solid var(--c2); outline-offset: 1px; }
.btn-primary {
  min-height: 46px;
  background: var(--accent);
  color: var(--bg);
  border-color: var(--accent);
  font-weight: 650;
}
.btn-primary:disabled { opacity: 0.6; cursor: progress; }
.login-error { color: var(--c4); font-size: 0.9rem; margin: 0; }
.login-note { margin-top: 18px; color: var(--muted); font-size: 0.8rem; }

/* Identical top spacing on every page: the status line takes no space once hidden,
   and each page's first block starts at the same offset below the header. */
.status[hidden] { display: none !important; }
main > #content > *:first-child { margin-top: 0; }
main { padding-top: 22px; }


/* ==========================================================================
   Header (final): flexible row. Items wrap onto a new line instead of overlapping.
   Brand keeps a fixed width so the navigation stays in the same place on every page.
   ========================================================================== */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 16px;
  grid-template-columns: none;
  grid-template-areas: none;
}
.topbar > * { min-width: 0; }

.brand {
  flex: 0 0 260px;
  width: 260px;
  max-width: 100%;
  min-width: 0;
}
.brand-text { min-width: 0; }
.brand-text h1,
.brand-text .sub {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav {
  flex: 1 1 0;
  min-width: 0;
  max-width: 100%;
  justify-content: center;
  flex-wrap: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
}
.nav::-webkit-scrollbar { display: none; }

.controls {
  flex: 0 0 auto;
  flex-wrap: nowrap;
  white-space: nowrap;
}
.controls select,
.controls .btn { flex-shrink: 0; }

.refresh-note {
  flex: 1 0 100%;
  order: 9;
}
.refresh-note:empty { display: none; }

/* Phones: brand, navigation and controls each take a full line, so nothing can overlap. */
@media (max-width: 720px) {
  .brand { flex: 1 0 100%; width: 100%; }
  .nav { flex: 1 0 100%; order: 3; }
  .controls { flex: 1 0 100%; justify-content: flex-start; flex-wrap: wrap; }
  .controls select { flex: 1; }
}

/* ==========================================================================
   Header (definitive): row 1 = title | theme, year, Refresh, Sign out.
   Row 2 = navigation, always full width, so it can never be hidden.
   ========================================================================== */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 14px;
}

.brand {
  flex: 1 1 0;
  min-width: 0;
  width: auto;
  max-width: none;
}
.brand-text { min-width: 0; }

.controls {
  flex: 0 0 auto;
  flex-wrap: nowrap;
  white-space: nowrap;
  margin-left: auto;
}

.nav {
  order: 5;
  flex: 1 0 100%;
  width: 100%;
  justify-content: flex-start;
  flex-wrap: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
}

.refresh-note { order: 9; flex: 1 0 100%; }

@media (max-width: 520px) {
  .controls { flex: 1 0 100%; margin-left: 0; flex-wrap: wrap; }
  .controls select { flex: 1 1 0; min-width: 0; }
  .controls .btn { flex: 1 1 0; }
}


/* ==========================================================================
   Sign-in page (final): its own layout, independent of the header and page styles.
   ========================================================================== */
body.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  padding: clamp(16px, 4vw, 40px);
  box-sizing: border-box;
  background:
    radial-gradient(1200px 500px at 50% -10%, var(--surface-2), transparent 60%),
    var(--bg);
}
body.login-body > main.login-main {
  width: 100%;
  max-width: none;
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: center;
}
.login-card {
  width: min(440px, 100%);
  padding: clamp(24px, 6vw, 40px);
  border-radius: 22px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  text-align: center;
  box-sizing: border-box;
}
.login-card .logo {
  width: 56px;
  height: 56px;
  margin: 0 auto 16px;
  border-radius: 16px;
  background: var(--surface-2);
  display: grid;
  place-items: center;
}
.login-card .logo svg { width: 28px; height: 28px; color: var(--accent); }
.login-card h1 { font-size: clamp(1.4rem, 4vw, 1.7rem); margin: 0; }
.login-card > .muted { margin: 6px 0 24px; font-size: 0.95rem; }
.login-form { display: grid; gap: 16px; text-align: left; }
.login-field { display: grid; gap: 6px; font-size: 0.85rem; color: var(--muted); }
.login-field input {
  width: 100%;
  min-height: 48px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  box-sizing: border-box;
}
.login-field input:focus-visible { outline: 3px solid var(--c2); outline-offset: 1px; }
.login-form .btn-primary {
  width: 100%;
  min-height: 48px;
  margin-top: 4px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--bg);
  border: 1px solid var(--accent);
  font: inherit;
  font-weight: 650;
  font-size: 1rem;
  cursor: pointer;
}
.login-form .btn-primary:hover { filter: brightness(1.05); }
.login-form .btn-primary:disabled { opacity: 0.6; cursor: progress; }
.login-error {
  margin: 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: color-mix(in srgb, var(--c4) 14%, transparent);
  color: var(--c4);
  font-size: 0.9rem;
  text-align: left;
}
.login-error[hidden] { display: none; }
.login-note { margin: 22px 0 0; color: var(--muted); font-size: 0.8rem; line-height: 1.5; }
