:root {
  --bg: #f4f5f8; --panel: #ffffff; --ink: #17202e; --muted: #5b6577; --line: #dfe3ea;
  --accent: #2c5fd4; --accent-ink: #ffffff; --accent-soft: #e6edfc; --bad: #b3261e; --bad-soft: #fbe4e2;
  --meter: #2c5fd4; --meter-bg: #e4e8f0; --keep: #1f7a4a; --keep-soft: #e1f3e8; --focus: #2c5fd4;
  --radius: 14px; --shadow: 0 1px 2px rgba(15, 25, 45, .06), 0 6px 24px rgba(15, 25, 45, .06);
}
:root[data-theme="dark"] {
  --bg: #0f131a; --panel: #171c25; --ink: #e7ebf2; --muted: #9aa5b6; --line: #2a3140;
  --accent: #7fa6ff; --accent-ink: #0b1220; --accent-soft: #1d2a45; --bad: #ff9c94; --bad-soft: #3c1f1e;
  --meter: #7fa6ff; --meter-bg: #283041; --keep: #6fd39b; --keep-soft: #183326; --focus: #9dbbff;
  --shadow: none;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
body.dragging { outline: 4px dashed var(--accent); outline-offset: -8px; }
a { color: var(--accent); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

.topbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 12px 16px; max-width: 1180px; margin: 0 auto; }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--ink); }
.brand-name { font-weight: 750; font-size: 1.15rem; letter-spacing: -.01em; }
.pill { font-size: .78rem; padding: 3px 10px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.top-actions { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.top-link { font-size: .9rem; text-decoration: none; color: var(--muted); }
.icon-btn { border: 1px solid var(--line); background: var(--panel); color: var(--ink); border-radius: 10px; width: 36px; height: 36px; display: grid; place-items: center; cursor: pointer; }

.wrap { max-width: 1180px; margin: 0 auto; padding: 0 16px 32px; }
.hero { padding: 18px 0 8px; }
.hero h1 { font-size: clamp(1.6rem, 4vw, 2.3rem); line-height: 1.15; margin: 0 0 8px; letter-spacing: -.02em; }
.lede { margin: 0 0 16px; max-width: 46rem; color: var(--muted); font-size: 1.05rem; }
.hero-actions { display: flex; flex-wrap: wrap; gap: 10px; }
.status-line { margin: 12px 0 0; font-size: .92rem; color: var(--muted); }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  border-radius: 10px; padding: 10px 16px; font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; min-height: 44px; }
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn-ghost { background: transparent; }
.btn-small { padding: 6px 12px; min-height: 36px; font-size: .9rem; }
.btn-danger { color: var(--bad); }
.btn-danger:hover { border-color: var(--bad); background: var(--bad-soft); }
.link-btn { border: 0; background: none; color: var(--accent); font: inherit; font-size: .85rem; cursor: pointer; padding: 2px 4px; }

.banner { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; justify-content: space-between; background: var(--accent-soft); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 10px 14px; margin: 12px 0; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; margin: 12px 0; }
.progress-head { display: flex; align-items: center; gap: 10px; }
.progress-head .btn { margin-left: auto; }
.stages { list-style: none; display: flex; flex-wrap: wrap; gap: 6px 16px; padding: 0; margin: 10px 0; font-size: .88rem; color: var(--muted); }
.stages li::before { content: "\25CB"; margin-right: 6px; }
.stages li.active { color: var(--ink); font-weight: 600; }
.stages li.active::before { content: "\25D4"; color: var(--accent); }
.stages li.done::before { content: "\2713"; color: var(--keep); }
.stages li.failed { color: var(--bad); }
.stages li.failed::before { content: "\2715"; }
.bar { height: 8px; border-radius: 99px; background: var(--meter-bg); overflow: hidden; }
.bar span { display: block; height: 100%; width: 0; background: var(--meter); transition: width .2s; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: 18px 0 0; overflow-x: auto; }
.tab { border: 0; background: none; font: inherit; font-weight: 600; color: var(--muted); padding: 10px 14px; border-bottom: 3px solid transparent; cursor: pointer; min-height: 44px; }
.tab[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }
.panel { padding: 16px 0; }

.search-row { display: flex; gap: 8px; }
.search-row input { flex: 1; min-width: 0; font: inherit; font-size: 1.05rem; padding: 10px 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); color: var(--ink); min-height: 48px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.chip { border: 1px solid var(--line); background: var(--panel); color: var(--ink); border-radius: 999px; padding: 5px 12px; font: inherit; font-size: .88rem; cursor: pointer; min-height: 34px; }
.chip:hover { border-color: var(--accent); color: var(--accent); }
.by-image { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 6px 0 12px; }
.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; padding: 10px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); margin-bottom: 10px; font-size: .92rem; }
.controls input[type="range"] { width: 140px; accent-color: var(--accent); }
.controls select { font: inherit; padding: 6px 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); color: var(--ink); }
.controls output { font-weight: 700; font-variant-numeric: tabular-nums; }
.check { display: inline-flex; align-items: center; gap: 6px; }
.understood { margin: 0 0 8px; }
.query-ref { display: flex; align-items: center; gap: 10px; margin: 0 0 10px; }
.query-ref img { width: 56px; height: 56px; object-fit: cover; border-radius: 8px; }
.result-count { margin: 0 0 10px; font-weight: 600; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.grid-small { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
.tile { margin: 0; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; display: flex; flex-direction: column; }
.tile-img { position: relative; border: 0; padding: 0; background: var(--meter-bg); cursor: zoom-in; aspect-ratio: 4 / 3; display: block; width: 100%; overflow: hidden; }
.tile-img img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: block; }
.tile figcaption { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; padding: 6px 8px; font-size: .82rem; }
.score { display: inline-flex; align-items: center; gap: 6px; font-weight: 700; font-variant-numeric: tabular-nums; }
.meter { width: 44px; height: 6px; border-radius: 99px; background: var(--meter-bg); overflow: hidden; display: inline-block; }
.meter span { display: block; height: 100%; background: var(--meter); }
.fname { flex: 1 1 100%; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); order: 3; }
.tile .link-btn { margin-left: auto; }
.keep { background: var(--keep-soft); color: var(--keep); font-weight: 700; border-radius: 6px; padding: 1px 8px; font-size: .78rem; }
.group { margin: 0 0 18px; }
.h3 { font-size: 1.05rem; margin: 18px 0 8px; }
.h4 { font-size: .95rem; margin: 0 0 8px; }

.facts { display: grid; grid-template-columns: minmax(140px, 220px) 1fr; gap: 8px 16px; margin: 0 0 14px; }
.facts dt { color: var(--muted); font-size: .9rem; }
.facts dd { margin: 0; }
.lib-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.how { padding-left: 20px; color: var(--muted); }
.skipped { max-height: 220px; overflow: auto; color: var(--muted); }

dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); color: var(--ink); padding: 12px; max-width: min(960px, 94vw); }
dialog::backdrop { background: rgba(5, 10, 20, .6); }
dialog figure { margin: 0; }
dialog img { display: block; max-width: 100%; max-height: 72vh; margin: 0 auto; border-radius: 8px; }
dialog figcaption { font-size: .88rem; margin-top: 8px; overflow-wrap: anywhere; }
.viewer-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; margin-top: 10px; }

.foot { max-width: 1180px; margin: 0 auto; padding: 16px; color: var(--muted); font-size: .85rem; border-top: 1px solid var(--line); }

@media (max-width: 640px) {
  .pill { display: none; }
  .search-row { flex-direction: column; }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .facts { grid-template-columns: 1fr; }
  .facts dt { margin-top: 6px; }
  .controls input[type="range"] { width: 100%; }
  .controls label { flex: 1 1 40%; }
}
