:root {
  --bg: #f6f5f2;
  --card: #ffffff;
  --ink: #1d1d1f;
  --muted: #6b6b70;
  --line: #deddd8;
  --accent: #2f6fdf;
  --accent-ink: #ffffff;
  --warn: #c7581c;
  --paper: #ffffff;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 2px 8px rgb(0 0 0 / 5%);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17181b;
    --card: #212226;
    --ink: #ececef;
    --muted: #9a9aa2;
    --line: #34353b;
    --accent: #6b9cff;
    --accent-ink: #0d1422;
    --warn: #ff9a5c;
    --paper: #e9e9e6;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #17181b; --card: #212226; --ink: #ececef; --muted: #9a9aa2;
  --line: #34353b; --accent: #6b9cff; --accent-ink: #0d1422; --warn: #ff9a5c;
  --paper: #e9e9e6; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}
header, main, footer { max-width: 1200px; margin: 0 auto; padding: 0 16px; }
header { padding-top: 28px; }
footer { padding-top: 12px; padding-bottom: 28px; color: var(--muted); font-size: 13.5px; }
footer a { color: inherit; }
h1 { margin: 0; font-size: 26px; letter-spacing: -0.01em; }
h2 { font-size: 18px; margin: 28px 0 4px; }
.sub, .hint { color: var(--muted); margin: 4px 0 12px; }
.hint { font-size: 13.5px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px;
  margin: 16px 0;
  box-shadow: var(--shadow);
}
.drop {
  border: 2px dashed var(--line);
  border-radius: 10px;
  padding: 18px;
  text-align: center;
  transition: border-color .15s, background .15s;
}
.drop.over { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); }
.drop p { margin: 4px 0; }

.btn {
  display: inline-block;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 8px;
  padding: 5px 12px;
  font: inherit;
  cursor: pointer;
}
.btn.ghost { background: transparent; color: var(--accent); }
.btn:disabled { opacity: .5; cursor: default; }
button.icon {
  border: 0; background: none; color: var(--muted);
  font-size: 22px; cursor: pointer; line-height: 1;
}
select, input[type=text] {
  font: inherit; color: var(--ink); background: var(--card);
  border: 1px solid var(--line); border-radius: 6px; padding: 3px 6px;
}

#files, #tpls { list-style: none; padding: 0; margin: 10px 0 0; display: flex; flex-wrap: wrap; gap: 6px; }
#files li, #tpls li {
  font-size: 13px; padding: 2px 8px; border-radius: 99px;
  background: var(--bg); border: 1px solid var(--line);
}
.bar { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; margin-top: 12px; }
.bar span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .1s; }

.stats { display: flex; flex-wrap: wrap; gap: 8px 28px; margin: 0; }
.stats div { min-width: 120px; }
.stats dt { font-size: 12.5px; color: var(--muted); }
.stats dd { margin: 0; font-size: 17px; font-weight: 600; }
.settings { display: flex; flex-wrap: wrap; gap: 8px 24px; align-items: center;
  margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); font-size: 14px; }
.settings input[type=range] { vertical-align: middle; width: 140px; }
.settings label { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; max-width: 100%; }
.settings select { max-width: 100%; min-width: 0; }

/* parts: one card per output PDF, its pages as draggable tiles */
.part-cards { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-start; }
.part-card {
  --tw: 92px;
  position: relative; display: flex; flex-direction: column; gap: 8px;
  min-width: 190px; max-width: 100%; padding: 8px 10px 9px;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: inset 0 3px 0 var(--c), var(--shadow);
  transition: opacity .12s, box-shadow .12s;
}
.card-head { display: flex; align-items: center; gap: 6px; }
.card-head .name { flex: 1; min-width: 60px; field-sizing: content; font-weight: 600; border-color: transparent; background: transparent; }
.card-head .name:hover, .card-head .name:focus { border-color: var(--line); background: var(--bg); }
.grip { color: var(--muted); cursor: grab; padding: 2px; touch-action: none; display: flex; }
.grip:hover { color: var(--ink); }
button.icon.dl { font-size: 0; padding: 4px; border-radius: 6px; color: var(--muted); display: flex; }
button.icon.dl:hover:not(:disabled) { color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); }
button.icon.dl:disabled { opacity: .35; cursor: default; }
.busy { animation: pulse .8s ease-in-out infinite alternate; }
@keyframes pulse { to { opacity: .3; } }
.pages { display: flex; flex-wrap: wrap; row-gap: 8px; }
.thumb {
  position: relative; flex: none; width: var(--tw); margin-right: 8px;
  cursor: grab; touch-action: none; user-select: none;
  transition: width .14s ease, margin .14s ease, opacity .14s ease;
}
.pages .thumb:last-child { margin-right: 0; }
.thumb img {
  width: 100%; aspect-ratio: 256 / 332; display: block; border-radius: 6px;
  background: var(--paper); border: 1px solid var(--line); box-shadow: 0 1px 2px rgb(0 0 0 / 8%);
}
.thumb .pg {
  position: absolute; left: 5px; top: 5px; padding: 0 5px; border-radius: 4px;
  background: var(--c); color: #fff; font-size: 11px; font-weight: 700;
}
.thumb .zoom { right: 5px; top: 5px; font-size: 13px; padding: 1px 6px; opacity: 0; transition: opacity .12s; }
.thumb:hover .zoom, .thumb:focus-within .zoom { opacity: 1; }
.card-foot { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; font-size: 12.5px; color: var(--muted); }
.card-foot .warn { color: var(--warn); }
.card-foot .tree-actions { display: flex; gap: 4px; margin-left: auto; }
#parts-msg { color: var(--warn); font-size: 13.5px; margin: 0 0 10px; }

/* while dragging: the lifted tile closes up, a stand-in grows where it lands */
body.dragging, body.dragging * { cursor: grabbing !important; user-select: none; }
.thumb.lifted { width: 0; margin-right: 0; opacity: 0; }
.thumb.stand { animation: grow .14s ease; }
.thumb.stand img { border: 2px dashed var(--accent); opacity: .55; box-shadow: none; }
@keyframes grow { from { width: 0; margin-right: 0; opacity: 0; } }
.part-card.cannot { opacity: .4; }
#parts .part-card.over { box-shadow: inset 0 3px 0 var(--c), 0 0 0 2px var(--accent); }
#parts .trash.over { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); }
#parts .trash.over .basket { color: var(--accent); }
.part-card.stand-card {
  border: 2px dashed var(--accent); box-shadow: none; background: transparent;
  animation: fade .14s ease; color: var(--accent);
}
@keyframes fade { from { opacity: 0; } }
.part-card.lifted-card { display: none; }
.drag-ghost {
  position: fixed; left: 0; top: 0; z-index: 50; pointer-events: none;
  display: flex; flex-direction: column; align-items: center; gap: 4px; margin: -30px 0 0 -26px;
}
.drag-ghost img {
  width: 52px; aspect-ratio: 256 / 332; border-radius: 4px; background: var(--paper);
  box-shadow: 0 6px 18px rgb(0 0 0 / 28%); transform: rotate(-3deg);
}
.drag-ghost span:empty { display: none; }
.drag-ghost span {
  padding: 2px 8px; border-radius: 99px; font-size: 12px; white-space: nowrap;
  background: var(--accent); color: var(--accent-ink); box-shadow: var(--shadow);
}
.drag-ghost.no span { background: var(--ink); color: var(--bg); }
.drag-ghost.card-ghost {
  flex-direction: row; margin: -14px 0 0 -14px; padding: 6px 12px; border-radius: 10px;
  background: var(--card); border: 1px solid var(--line); box-shadow: inset 0 3px 0 var(--c), 0 6px 18px rgb(0 0 0 / 25%);
}
.drag-ghost.card-ghost span:not(.tag) { background: none; color: var(--ink); box-shadow: none; font-weight: 600; }

/* the basket: discarded pages */
.trash {
  margin-top: 14px; padding: 10px 12px; border: 1.5px dashed var(--line); border-radius: 12px;
  display: flex; flex-direction: column; align-items: center; gap: 8px; transition: box-shadow .12s;
}
.basket { display: flex; flex-direction: column; align-items: center; color: var(--muted); font-size: 12.5px; }
.trash-tiles { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px 0; }
.trash-tiles:empty { display: none; }
.thumb.mini { --tw: 58px; }
.thumb.mini img { opacity: .6; }
.thumb.mini .pg { top: auto; bottom: 4px; left: 4px; font-size: 10px; }
.thumb.mini:not([data-node]) { cursor: default; }
.missing { margin: 14px 0 0; font-size: 13.5px; }
.missing ul { margin: 4px 0 0; padding-left: 20px; max-height: 220px; overflow-y: auto; }
.out-actions .btn { display: inline-flex; align-items: center; gap: 6px; }
@media (max-width: 600px) { .part-card { --tw: 74px; } }
.note {
  font-size: 13.5px; margin: 4px 0 14px; padding: 8px 12px; border-radius: 8px;
  background: var(--card); border: 1px solid var(--line); border-left: 4px solid var(--accent);
}
.example { margin-top: -6px; }
button.link {
  border: 0; padding: 0; background: none; font: inherit; color: var(--accent);
  text-decoration: underline; text-underline-offset: 2px; cursor: pointer;
}
.btn.small { padding: 2px 8px; font-size: 13px; }
.score { opacity: .7; font-variant-numeric: tabular-nums; }
.h-row { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }

.tag {
  display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 5px;
  background: var(--c); color: #fff; font-weight: 700; font-size: 12px; text-align: center;
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.chip {
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  border-radius: 99px; padding: 3px 11px; font: inherit; font-size: 13px; cursor: pointer;
}
.chip.on { border-color: var(--accent); background: var(--accent); color: var(--accent-ink); }

.bulk {
  position: sticky; top: 0; z-index: 5; display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
  background: var(--card); border: 1px solid var(--accent); border-radius: 10px;
  padding: 8px 12px; margin: 8px 0; box-shadow: var(--shadow); font-size: 14px;
}

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 10px; }
.tile {
  position: relative; background: var(--card); border: 1px solid var(--line);
  border-top: 4px solid var(--c); border-radius: 8px; padding: 5px; cursor: pointer;
  user-select: none;
}
.tile.sel { outline: 3px solid var(--accent); outline-offset: 1px; }
.tile.discard img { opacity: .3; }
.tile .front { width: 100%; aspect-ratio: 256 / 332; object-fit: cover; display: block;
  border-radius: 3px; background: var(--paper); }
.tile .back {
  position: absolute; right: 8px; top: 40%; width: 38%; aspect-ratio: 256 / 332; object-fit: cover;
  border: 1px solid var(--line); border-radius: 3px; background: var(--paper);
  box-shadow: 0 1px 4px rgb(0 0 0 / 25%);
}
.tile .meta { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; margin-top: 4px; font-size: 11.5px; color: var(--muted); }
.tile .badge { border-radius: 4px; padding: 0 4px; border: 1px solid currentColor; }
.tile .badge.warn { color: var(--warn); }
.zoom {
  position: absolute; border: 0; border-radius: 5px; font-size: 13px;
  background: rgb(0 0 0 / 55%); color: #fff; cursor: pointer; padding: 1px 5px;
}
.tile .zoom { left: 8px; top: 10px; opacity: 0; }
.tile:hover .zoom, .tile:focus-within .zoom { opacity: 1; }

.out-actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-top: 14px; }

dialog#preview {
  width: min(1100px, 96vw); max-height: 94vh; border: 1px solid var(--line); border-radius: 12px;
  background: var(--card); color: var(--ink); padding: 0;
}
dialog::backdrop { background: rgb(0 0 0 / 50%); }
.pv-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 14px;
  border-bottom: 1px solid var(--line); margin: 0; font-size: 14px; }
.pv-body { display: flex; gap: 12px; padding: 12px; overflow: auto; }
.pv-body figure { margin: 0; flex: 1; min-width: 0; text-align: center; }
.pv-body canvas { width: 100%; height: auto; background: var(--paper); border: 1px solid var(--line); border-radius: 4px; }
.pv-body figcaption { font-size: 12px; color: var(--muted); }
.pv-foot { display: flex; flex-wrap: wrap; gap: 10px; justify-content: space-between; align-items: center;
  padding: 10px 14px; border-top: 1px solid var(--line); }

@media (max-width: 600px) {
  .pv-body { flex-direction: column; }
  .tile .back { display: none; }
}

dialog#inspect {
  width: min(1100px, 96vw); max-height: 94vh; border: 1px solid var(--line); border-radius: 12px;
  background: var(--card); color: var(--ink); padding: 0;
}
.in-body { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 16px; padding: 12px 14px; }
.in-body figure { margin: 0; text-align: center; }
.in-body canvas { width: 100%; height: auto; background: var(--paper); border: 1px solid var(--line); border-radius: 4px; }
.in-body figcaption { font-size: 12px; color: var(--muted); }
.in-side h3 { margin: 0 0 4px; font-size: 15px; }
.in-kids { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 8px 0 10px; }
.in-side .btn { margin: 0 8px 8px 0; }
canvas.zoomable { cursor: zoom-in; }
.loupe {
  position: fixed; z-index: 10; pointer-events: none; width: 240px; height: 240px;
  border-radius: 50%; border: 2px solid var(--accent); box-shadow: 0 4px 18px rgb(0 0 0 / 35%);
  background: #fff;
}
@media (max-width: 700px) {
  .in-body { grid-template-columns: 1fr; }
}

.tpl-row {
  display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px 16px;
  margin-top: 14px; padding: 10px 12px; border: 1px dashed var(--line); border-radius: 10px;
}
.tpl-row.over { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); }
.tpl-row .hint { margin: 0; }
.tpl-actions { display: flex; gap: 8px; }
#files li a, #tpls li a { color: inherit; text-decoration: none; }
#files li a:hover, #tpls li a:hover { text-decoration: underline; }
#files li a.dl, #tpls li a.dl { color: var(--muted); padding-left: 2px; }
#tpls li button.icon { font-size: 15px; padding: 0 0 0 4px; vertical-align: baseline; }
.clear-row { margin: 8px 0 0; text-align: right; }
.clear-row:has(> [hidden]) { display: none; }
.in-nav { display: flex; justify-content: center; align-items: center; gap: 12px; margin-bottom: 6px; font-size: 14px; }
.in-nav span { min-width: 110px; text-align: center; font-variant-numeric: tabular-nums; }
