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

:root {
  --bg: #faf8f4;
  --bg-elevated: rgba(250, 248, 244, 0.86);
  --card: #ffffff;
  --ink: #1c1917;
  --ink-2: #57534e;
  --ink-3: #a8a29e;
  --line: #e7e2da;
  --accent: #9a5b1f;
  --accent-ink: #7c4a17;
  --accent-soft: #f3e8d9;
  --shadow: 0 1px 2px rgba(28, 25, 23, 0.05), 0 4px 16px rgba(28, 25, 23, 0.06);
  --shadow-lift: 0 2px 4px rgba(28, 25, 23, 0.08), 0 12px 32px rgba(28, 25, 23, 0.12);
  --radius: 14px;
  --tabbar-h: calc(54px + env(safe-area-inset-bottom));
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, ui-serif, serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141210;
    --bg-elevated: rgba(20, 18, 16, 0.86);
    --card: #1e1b18;
    --ink: #ece8e3;
    --ink-2: #b3aca4;
    --ink-3: #746e66;
    --line: #2c2925;
    --accent: #d29b5c;
    --accent-ink: #e0b17c;
    --accent-soft: #2b241c;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.35);
    --shadow-lift: 0 2px 4px rgba(0, 0, 0, 0.5), 0 12px 32px rgba(0, 0, 0, 0.5);
  }
}

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

body {
  margin: 0;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--ink);
  line-height: 1.45;
  padding-bottom: var(--tabbar-h);
}

button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
a { color: inherit; text-decoration: none; }
img { max-width: 100%; display: block; }

/* ---------- App bar ---------- */

.appbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--bg-elevated);
  -webkit-backdrop-filter: saturate(1.6) blur(16px);
  backdrop-filter: saturate(1.6) blur(16px);
  border-bottom: 1px solid var(--line);
}

.appbar .back {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: var(--accent-ink);
  margin-left: -6px;
}
.appbar .back:active { background: var(--accent-soft); }

.appbar h1 {
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
  font-family: var(--serif);
  font-size: 19px;
  font-weight: 600;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.appbar .count-pill {
  flex: 0 0 auto;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--ink-2);
  background: var(--accent-soft);
  border-radius: 999px;
  padding: 3px 10px;
}

/* ---------- Variant toggle ---------- */

.variant-toggle {
  display: inline-flex;
  background: var(--accent-soft);
  border-radius: 999px;
  padding: 3px;
  flex: 0 0 auto;
}
.variant-toggle button {
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-2);
  transition: color 0.15s;
}
.variant-toggle button[aria-pressed="true"] {
  background: var(--card);
  color: var(--ink);
  box-shadow: var(--shadow);
}

.toggle-row {
  display: flex;
  justify-content: center;
  padding: 12px 14px 2px;
}

.sidebar { display: none; }

/* ---------- Home ---------- */

.hero {
  padding: calc(26px + env(safe-area-inset-top)) 20px 8px;
  text-align: center;
}
.hero .kicker {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--accent-ink);
}
.hero h1 {
  margin: 6px 0 4px;
  font-family: var(--serif);
  font-size: 34px;
  font-weight: 600;
  letter-spacing: 0.01em;
}
.hero p {
  margin: 0 auto;
  max-width: 30em;
  color: var(--ink-2);
  font-size: 15px;
}

.search-launch {
  display: flex;
  align-items: center;
  gap: 8px;
  width: calc(100% - 40px);
  max-width: 430px;
  margin: 18px auto 4px;
  padding: 11px 16px;
  border-radius: 999px;
  background: var(--card);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  color: var(--ink-3);
  font-size: 15px;
}
.search-launch svg { flex: 0 0 auto; color: var(--ink-3); }

.section { padding: 18px 14px 4px; }
.section > h2 {
  margin: 0 0 10px 6px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-3);
}

.tile-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tile {
  display: block;
  padding: 16px 16px 14px;
  border-radius: var(--radius);
  background: var(--card);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}
.tile .t-title {
  font-family: var(--serif);
  font-size: 17px;
  font-weight: 600;
}
.tile .t-sub { font-size: 13px; color: var(--ink-2); margin-top: 2px; }

.cat-list {
  border-radius: var(--radius);
  background: var(--card);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.cat-list a {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 13px 16px;
  border-bottom: 1px solid var(--line);
}
.cat-list a:last-child { border-bottom: 0; }
.cat-list a:active { background: var(--accent-soft); }
.cat-list .c-title { flex: 1 1 auto; min-width: 0; font-size: 15.5px; font-weight: 500; }
.cat-list .c-count {
  flex: 0 0 auto;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: var(--ink-3);
}
.cat-list .chev { flex: 0 0 auto; color: var(--ink-3); }

.foot {
  margin: 30px 20px 18px;
  text-align: center;
  font-size: 12.5px;
  color: var(--ink-3);
}
.foot a { color: var(--accent-ink); }

/* ---------- Figure grid ---------- */

.grid-wrap { padding: 12px 12px 24px; }

.masonry {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  align-items: start;
}

.fig-card {
  position: relative;
  margin: 0;
  border-radius: var(--radius);
  background: #fff;
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.sel-btn {
  position: absolute;
  top: 7px;
  right: 7px;
  z-index: 2;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.94);
  border: 1.5px solid #d6d0c6;
  color: transparent;
  box-shadow: var(--shadow);
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}
.sel-btn.on {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.fig-card:active { transform: scale(0.975); }

.fig-card .ph {
  background: #fff;
  padding: 8px 8px 2px;
}
.fig-card img {
  width: 100%;
  height: auto;
  margin: 0 auto;
}
.fig-card figcaption { padding: 4px 12px 11px; background: var(--card); }
.fig-card .f-name {
  font-size: 13.5px;
  font-weight: 600;
  line-height: 1.28;
  color: var(--ink);
}
.fig-card .f-date {
  font-size: 12px;
  color: var(--ink-2);
  margin-top: 1px;
  font-variant-numeric: tabular-nums;
}

.era-divider {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 16px 4px 12px;
}
.era-divider::before, .era-divider::after {
  content: "";
  flex: 1 1 auto;
  height: 1px;
  background: var(--line);
}
.era-divider span {
  font-family: var(--serif);
  font-size: 16px;
  font-weight: 600;
  color: var(--accent-ink);
  white-space: nowrap;
}
.era-divider small {
  display: block;
  text-align: center;
  font-family: var(--sans);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.06em;
  color: var(--ink-3);
}

.list-end {
  grid-column: 1 / -1;
  text-align: center;
  padding: 18px 0 6px;
  font-size: 12.5px;
  color: var(--ink-3);
}

/* ---------- Search ---------- */

.search-head {
  position: sticky;
  top: 0;
  z-index: 20;
  padding: 10px 14px 12px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--bg-elevated);
  -webkit-backdrop-filter: saturate(1.6) blur(16px);
  backdrop-filter: saturate(1.6) blur(16px);
  border-bottom: 1px solid var(--line);
}
.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 9px 14px;
  box-shadow: var(--shadow);
}
.search-box svg { flex: 0 0 auto; color: var(--ink-3); }
.search-box input {
  flex: 1 1 auto;
  min-width: 0;
  border: 0;
  outline: 0;
  background: transparent;
  font: inherit;
  font-size: 16px;
  color: var(--ink);
}
.search-box input::placeholder { color: var(--ink-3); }
.search-box input::-webkit-search-cancel-button,
.search-box input::-webkit-search-decoration { -webkit-appearance: none; appearance: none; }
.search-box .clear {
  flex: 0 0 auto;
  color: var(--ink-3);
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: grid;
  place-items: center;
}

.search-hint {
  padding: 40px 30px;
  text-align: center;
  color: var(--ink-3);
  font-size: 14.5px;
}

/* ---------- Tab bar ---------- */

#tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: flex;
  height: var(--tabbar-h);
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--bg-elevated);
  -webkit-backdrop-filter: saturate(1.6) blur(16px);
  backdrop-filter: saturate(1.6) blur(16px);
  border-top: 1px solid var(--line);
}
#tabbar a {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 10.5px;
  font-weight: 500;
  color: var(--ink-3);
}
#tabbar a.active { color: var(--accent-ink); }
#tabbar a { position: relative; }
#tabbar svg { width: 22px; height: 22px; }

.tab-badge {
  position: absolute;
  top: 5px;
  left: calc(50% + 6px);
  min-width: 17px;
  height: 17px;
  padding: 0 4px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: #fff;
  font-size: 10.5px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}
.tab-badge[hidden] { display: none; }

/* ---------- Lightbox ---------- */

#lightbox {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
#lightbox[hidden] { display: none; }

.lb-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top));
}
.lb-bar .lb-close {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--accent-soft);
  color: var(--accent-ink);
  margin-left: -4px;
}
.lb-bar .lb-meta { flex: 1 1 auto; min-width: 0; text-align: center; padding: 0 2px; }
.lb-bar .lb-name {
  font-family: var(--serif);
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.lb-bar .lb-date { font-size: 12px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.lb-bar .lb-spacer { width: 34px; flex: 0 0 auto; }

.lb-stage {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: 10px 16px;
  touch-action: pan-y pinch-zoom;
}
.lb-frame {
  background: #fff;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-lift);
  padding: 14px;
  max-width: min(92vw, 640px);
}
.lb-frame img {
  max-height: min(58vh, 560px);
  width: auto;
  max-width: 100%;
  margin: 0 auto;
}

.lb-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 6px 16px calc(16px + env(safe-area-inset-bottom));
}
.lb-controls .nav-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--card);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  color: var(--accent-ink);
}
.lb-controls .nav-btn:disabled { opacity: 0.35; }
.lb-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  padding: 0 16px 8px;
}
.lb-actions a, #lb-tray {
  font-size: 13px;
  font-weight: 500;
  color: var(--accent-ink);
  background: var(--accent-soft);
  padding: 7px 14px;
  border-radius: 999px;
}
#lb-tray.in-tray { background: var(--accent); color: #fff; }

/* ---------- Print tray ---------- */

.appbar .text-btn {
  flex: 0 0 auto;
  font-size: 14.5px;
  font-weight: 600;
  color: var(--accent-ink);
  padding: 5px 12px;
  border-radius: 999px;
  background: var(--accent-soft);
}

.tray-tools { display: flex; justify-content: center; padding: 12px 14px 0; }

.tray-actions {
  display: flex;
  gap: 10px;
  padding: 12px 14px 2px;
  max-width: 560px;
  margin: 0 auto;
  width: 100%;
}

.btn-primary, .btn-ghost, .btn-danger {
  flex: 1 1 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 11px 14px;
  border-radius: 999px;
  font-size: 14.5px;
  font-weight: 600;
  white-space: nowrap;
  transition: opacity 0.12s, background 0.15s;
}
.btn-primary { background: var(--accent); color: #fff; box-shadow: var(--shadow); }
.btn-ghost { background: var(--accent-soft); color: var(--accent-ink); }
.btn-danger { background: var(--accent-soft); color: #b91c1c; }
.btn-primary:disabled, .btn-danger:disabled { opacity: 0.4; }
.btn-ghost.armed { background: #b91c1c; color: #fff; }
@media (prefers-color-scheme: dark) {
  .btn-danger { color: #f87171; }
}

.tray-card .tray-remove {
  position: absolute;
  top: 7px;
  right: 7px;
  z-index: 2;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.94);
  border: 1.5px solid #d6d0c6;
  color: #78716c;
  box-shadow: var(--shadow);
}
.tray-card .sel-btn.pick { pointer-events: none; }
.tray-card.picked { outline: 2.5px solid var(--accent); outline-offset: -1px; }
.f-variant {
  display: inline-block;
  margin-top: 4px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--accent-ink);
  background: var(--accent-soft);
  border-radius: 5px;
  padding: 1.5px 6px;
}

.v-swap {
  display: inline-flex;
  margin-top: 5px;
  background: var(--accent-soft);
  border-radius: 999px;
  padding: 2px;
}
.v-swap button {
  border-radius: 999px;
  padding: 3.5px 9px;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--ink-2);
}
.v-swap button[aria-pressed="true"] {
  background: var(--card);
  color: var(--accent-ink);
  box-shadow: var(--shadow);
}

.tray-empty {
  text-align: center;
  padding: 60px 34px 30px;
  max-width: 26em;
  margin: 0 auto;
}
.tray-empty .te-icon {
  width: 58px;
  height: 58px;
  margin: 0 auto 14px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--accent-soft);
  color: var(--accent-ink);
}
.tray-empty .te-icon svg { width: 26px; height: 26px; }
.tray-empty h2 { font-family: var(--serif); font-size: 22px; margin: 0 0 8px; }
.tray-empty p { color: var(--ink-2); font-size: 14.5px; margin: 0 0 22px; }
.tray-empty .btn-primary { display: inline-flex; flex: 0 0 auto; padding: 12px 26px; }

#print-sheet { display: none; }

@media print {
  @page { margin: 0.25in; }
  html, body { background: #fff; padding: 0; margin: 0; }
  #tabbar, #lightbox, .appbar, .tray-tools, .tray-actions,
  .grid-wrap, .sidebar, .tray-empty { display: none !important; }
  #app { display: block; }
  #print-sheet { display: block; }
  .print-page {
    display: block;
    page-break-after: always;
    break-after: page;
  }
  .print-page:last-child { page-break-after: auto; break-after: auto; }
  .print-grid {
    display: grid;
    grid-template-columns: 3.6in 3.6in;
    grid-template-rows: 4in 4in;
    justify-content: center;
    overflow: hidden;
  }
  .print-cell {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.1in;
    overflow: hidden;
  }
  .print-cell img {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
}

/* ---------- Skeleton / boot ---------- */

.boot {
  padding: 40vh 30px 0;
  text-align: center;
  color: var(--ink-3);
  font-size: 14px;
}

/* ---------- Desktop ---------- */

@media (min-width: 700px) {
  .masonry { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (max-width: 480px) {
  .appbar .count-pill { display: none; }
}

@media (min-width: 940px) {
  body { padding-bottom: 0; }
  #tabbar { display: none; }

  #app {
    display: grid;
    grid-template-columns: 260px minmax(0, 1fr);
    max-width: 1280px;
    margin: 0 auto;
    gap: 0 28px;
    padding: 0 20px;
  }
  #app > .appbar, #app > .search-head {
    grid-column: 2;
    border-bottom: 0;
    background: transparent;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }
  #app > .appbar .back { display: none; }
  #app > * { grid-column: 2; }
  #app > .sidebar { grid-column: 1; grid-row: 1 / span 30; }

  .sidebar {
    display: block;
    position: sticky;
    top: 0;
    align-self: start;
    max-height: 100vh;
    overflow-y: auto;
    padding: 26px 0 30px;
  }
  .sidebar .brand {
    font-family: var(--serif);
    font-size: 21px;
    font-weight: 600;
    padding: 0 12px 14px;
    display: block;
  }
  .sidebar h3 {
    margin: 14px 0 4px;
    padding: 0 12px;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--ink-3);
  }
  .sidebar a.nav-item {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    padding: 6px 12px;
    border-radius: 8px;
    font-size: 14px;
    color: var(--ink-2);
  }
  .sidebar a.nav-item:hover { background: var(--accent-soft); color: var(--ink); }
  .sidebar a.nav-item.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
  .sidebar a.nav-item .n { color: var(--ink-3); font-size: 12.5px; font-variant-numeric: tabular-nums; }

  .masonry { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .hero { text-align: left; padding-top: 40px; }
  .hero h1 { font-size: 40px; }
  .search-launch { margin-left: 0; }
  .tile-row { grid-template-columns: repeat(4, 1fr); }
  .fig-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lift); }
}

@media (min-width: 1200px) {
  .masonry { grid-template-columns: repeat(5, minmax(0, 1fr)); }
}

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