:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --surface-2: #f2f2ef;
  --border: #e6e5e0;
  --border-strong: #d6d5cf;
  --text: #1c1b19;
  --muted: #6b6a65;
  --faint: #9c9b95;
  --accent: #4f46e5;
  --accent-soft: #eef0ff;
  --accent-ring: #4f46e533;
  --mark-bg: #fff1a8;
  --mark-text: #1c1b19;
  --danger: #dc2626;
  --danger-soft: #fdecec;
  --ok: #15803d;
  --ok-soft: #e8f6ec;
  --warn: #b45309;
  --warn-soft: #fdf3e2;
  --shadow-sm: 0 1px 2px #0000000a;
  --shadow: 0 1px 2px #0000000a, 0 4px 16px -4px #0000000f;
  --shadow-lg: 0 12px 48px -8px #00000030;
  --map-hit: #4f46e5;
  --map-parsed: #dcdbd5;
  --map-pending: #efeeea;
  --fuzzy-bg: #ffe2c2;
  --fuzzy-text: #1c1b19;
  --hl: #ffe45c;
  --hl-approx: #ffd08a;
  --hl-passage: #c9c6ff;
  --hl-ring: #eab308;
  --hl-ring-soft: #eab30855;

  --sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, monospace;
  --r: 14px;
  --r-sm: 10px;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 var(--sans);
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}
::selection { background: var(--accent-ring); }
button { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.hidden { display: none !important; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r); box-shadow: var(--shadow);
}

/* ---------- view tabs ---------- */
nav {
  display: flex; gap: 2px; padding: 3px; width: max-content; margin: 24px auto 0;
  background: var(--surface-2); border-radius: 10px;
}
.tab {
  background: none; border: 0; color: var(--muted); cursor: pointer;
  font-size: 13.5px; font-weight: 500; padding: 6px 16px; border-radius: 8px;
  transition: color .15s, background .15s, box-shadow .15s;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); background: var(--surface); box-shadow: var(--shadow-sm), 0 0 0 1px var(--border); }

main { max-width: 920px; margin: 0 auto; padding: 0 24px 96px; }

/* ---------- hero (empty library only) ---------- */
.hero { text-align: center; padding: 48px 0 28px; display: none; }
body:not(.has-docs) .hero { display: block; animation: rise .4s ease both; }
.hero h1 { margin: 0; font-size: clamp(28px, 5vw, 40px); font-weight: 700; letter-spacing: -.03em; line-height: 1.15; }
.hero p { margin: 12px auto 0; max-width: 46ch; color: var(--muted); font-size: 16px; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }

/* ---------- search bar ---------- */
.search-sticky {
  position: sticky; top: 0; z-index: 20;
  padding: 20px 0 10px; background: var(--bg);
}
.search-sticky::after {
  content: ""; position: absolute; left: 0; right: 0; top: 100%; height: 14px;
  background: linear-gradient(var(--bg), transparent); pointer-events: none;
}
body:not(.has-docs) .search-sticky::after { display: none; }
body:not(.has-docs) .search-sticky { position: static; background: none; }
.searchbar {
  display: flex; align-items: center; gap: 10px;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: var(--r); padding: 6px 8px 6px 16px;
  box-shadow: var(--shadow);
  transition: border-color .15s, box-shadow .15s;
}
.searchbar:focus-within { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-ring); }
.search-icon { width: 19px; height: 19px; color: var(--faint); flex-shrink: 0; }
.searchbar:focus-within .search-icon { color: var(--accent); }
#q {
  flex: 1; min-width: 0; background: none; border: 0; outline: 0;
  color: var(--text); font: 16px var(--mono); padding: 9px 2px;
}
#q::placeholder { color: var(--faint); font-family: var(--sans); }
.toggle { position: relative; cursor: pointer; user-select: none; }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle span {
  display: block; font: 600 12.5px/1 var(--sans); color: var(--muted);
  padding: 8px 10px; border-radius: 8px; border: 1px solid transparent;
  transition: background .15s, color .15s, border-color .15s;
}
.toggle:hover span { background: var(--surface-2); color: var(--text); }
.toggle input:checked + span { background: var(--accent-soft); color: var(--accent); border-color: var(--accent-ring); }
.toggle input:focus-visible + span { box-shadow: 0 0 0 3px var(--accent-ring); }
kbd.hint {
  font: 500 11.5px/1 var(--mono); color: var(--faint);
  border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 6px;
  padding: 4px 7px; margin-right: 4px;
}
.searchbar:focus-within kbd.hint { display: none; }
.qstatus { font-size: 12.5px; color: var(--muted); padding: 8px 4px 0; min-height: 1.6em; }

/* ---------- page map ---------- */
#mapwrap { position: relative; margin-top: 14px; padding: 14px 16px 16px; }
.map-head {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 12.5px; font-weight: 600; color: var(--muted); margin-bottom: 10px;
}
.legend { display: flex; align-items: center; gap: 6px; font-weight: 400; color: var(--faint); }
.legend i { width: 9px; height: 9px; border-radius: 2.5px; display: inline-block; margin-left: 8px; }
.lg-hit { background: var(--map-hit); }
.lg-parsed { background: var(--map-parsed); }
.lg-pending { background: var(--map-pending); box-shadow: inset 0 0 0 1px var(--border); }
#map { display: block; width: 100%; cursor: pointer; }
#maptip {
  position: fixed; z-index: 60; pointer-events: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-sm);
  padding: 8px 12px; max-width: 46ch; box-shadow: var(--shadow-lg);
  font-size: 12.5px; line-height: 1.45;
}
#maptip .tip-head { font-weight: 600; margin-bottom: 2px; }
#maptip .tip-line { color: var(--muted); font-family: var(--mono); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- dropzone ---------- */
#dropzone {
  margin-top: 16px; border: 1.5px dashed var(--border-strong); border-radius: var(--r);
  background: var(--surface); transition: border-color .15s, background .15s;
}
#dropzone:hover { border-color: var(--faint); }
#dropzone.drag { border-color: var(--accent); background: var(--accent-soft); }
.dz-inner {
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  padding: 44px 20px; text-align: center; cursor: pointer;
}
.dz-icon {
  width: 44px; height: 44px; border-radius: 12px; display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent);
}
.dz-icon svg { width: 22px; height: 22px; }
.dz-title { font-size: 15px; font-weight: 600; }
.dz-title u { text-decoration-color: var(--accent-ring); text-underline-offset: 3px; color: var(--accent); }
.dz-sub { color: var(--muted); margin-top: 4px; font-size: 13.5px; }
/* once there are documents, the dropzone shrinks to a slim row */
body.has-docs #dropzone { border-style: dashed; background: transparent; }
body.has-docs .dz-inner { flex-direction: row; justify-content: center; gap: 12px; padding: 12px 16px; }
body.has-docs .dz-icon { width: 30px; height: 30px; border-radius: 8px; }
body.has-docs .dz-icon svg { width: 16px; height: 16px; }
body.has-docs .dz-title { font-size: 13.5px; font-weight: 500; }
body.has-docs .dz-sub { display: none; }

/* ---------- queue ---------- */
#queue { margin-top: 12px; display: flex; flex-direction: column; gap: 8px; }
#queue:empty { display: none; }
.qitem {
  display: grid; grid-template-columns: 1fr auto; gap: 6px 14px; align-items: baseline;
  background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow);
  border-radius: var(--r-sm); padding: 12px 16px;
  animation: rise .25s ease;
}
.qitem .name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.qitem .qmeta { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.qitem .bar { grid-column: 1 / -1; height: 4px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.qitem .bar i { display: block; height: 100%; width: 0; border-radius: 99px; background: var(--accent); transition: width .25s ease; }
.qitem.err { border-color: var(--danger); }
.qitem.err .qmeta { color: var(--danger); }
.qitem.err .bar i { background: var(--danger); width: 100% !important; }

/* ---------- results ---------- */
#results { margin-top: 20px; display: flex; flex-direction: column; }
.res-head {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; font-weight: 600; color: var(--text);
  padding: 18px 4px 8px;
}
.res-head:first-child { padding-top: 4px; }
.res-head::before {
  content: ""; width: 14px; height: 16px; flex-shrink: 0;
  background: var(--faint);
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linejoin='round'%3E%3Cpath d='M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z'/%3E%3Cpath d='M14 3v5h5'/%3E%3C/svg%3E") center / contain no-repeat;
          mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linejoin='round'%3E%3Cpath d='M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z'/%3E%3Cpath d='M14 3v5h5'/%3E%3C/svg%3E") center / contain no-repeat;
}
.res {
  display: grid; grid-template-columns: 76px minmax(0, 1fr); gap: 2px 14px;
  background: var(--surface); border: 1px solid var(--border);
  padding: 11px 16px; cursor: pointer; margin-top: -1px;
  transition: background .12s;
}
.res-head + .res { border-top-left-radius: var(--r-sm); border-top-right-radius: var(--r-sm); margin-top: 0; }
.res:has(+ .res-head), .res:last-child, .res:has(+ .nores) { border-bottom-left-radius: var(--r-sm); border-bottom-right-radius: var(--r-sm); }
.res:hover { background: var(--surface-2); }
.res .where { font: 12px var(--mono); color: var(--faint); padding-top: 2px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.res > * { min-width: 0; }
.res .line { font: 13.5px/1.6 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere; }
.res .ctx { font: 12.5px/1.6 var(--mono); color: var(--faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.res.passage { display: block; border-radius: var(--r-sm); margin: 0 0 10px; padding: 14px 18px; }
.p-head { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; min-width: 0; }
.p-file { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.p-head .where { padding: 0; }
.p-head .score { margin-left: auto; }
.p-text { font-size: 14.5px; line-height: 1.6; color: var(--text); overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; }
mark { background: var(--mark-bg); color: var(--mark-text); border-radius: 3px; padding: 1px 2px; }
.nores { color: var(--muted); text-align: center; padding: 40px 0; font-size: 14px; }

/* ---------- library ---------- */
#view-library { padding-top: 28px; }
.lib-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 20px; margin-bottom: 20px; flex-wrap: wrap; }
.lib-head h2 { margin: 0 0 4px; font-size: 24px; font-weight: 700; letter-spacing: -.02em; }
.lib-head p { margin: 0; font-size: 13.5px; max-width: 52ch; }
.lib-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.btn {
  background: var(--surface); border: 1px solid var(--border-strong); color: var(--text);
  font-size: 13.5px; font-weight: 500; padding: 8px 14px; border-radius: var(--r-sm); cursor: pointer;
  box-shadow: var(--shadow-sm); transition: background .15s, border-color .15s, color .15s;
}
.btn:hover { background: var(--surface-2); }
.btn:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--accent-ring); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }

.table-card { overflow: hidden; }
#lib-table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
#lib-table thead th {
  text-align: left; font-size: 12px; font-weight: 600; color: var(--muted);
  padding: 10px 14px; border-bottom: 1px solid var(--border); background: var(--surface-2);
}
#lib-table td { padding: 12px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
#lib-table tbody tr:last-child td { border-bottom: 0; }
#lib-table tbody tr { transition: background .12s; }
#lib-table tbody tr:hover { background: var(--surface-2); }
#lib-table td.file { font-weight: 600; max-width: 22em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#lib-table .num { text-align: right; font-variant-numeric: tabular-nums; }
#lib-table td.date { color: var(--muted); white-space: nowrap; }
#lib-table .row-actions { text-align: right; }
#lib-table .row-actions button {
  background: none; border: 0; color: var(--faint); cursor: pointer; font-size: 13px;
  padding: 4px 10px; border-radius: 7px;
}
#lib-table .row-actions button:hover { color: var(--danger); background: var(--danger-soft); }
.pill { font-size: 12px; font-weight: 500; padding: 3px 10px; border-radius: 99px; display: inline-block; white-space: nowrap; }
.status-ok { color: var(--ok); background: var(--ok-soft); }
.status-run { color: var(--accent); background: var(--accent-soft); }
.status-err { color: var(--danger); background: var(--danger-soft); }
.empty { padding: 48px 20px; text-align: center; color: var(--muted); font-size: 14px; }
#lib-table:has(tbody:empty) { display: none; }
#lib-table:not(:has(tbody:empty)) + .empty { display: none; }

/* ---------- search modes ---------- */
.search-meta { display: flex; align-items: center; gap: 14px; padding-top: 10px; }
.search-meta .qstatus { padding: 0; min-height: 0; }
.modes { display: flex; gap: 2px; padding: 3px; background: var(--surface-2); border-radius: 9px; flex-shrink: 0; }
.modes button {
  background: none; border: 0; cursor: pointer; color: var(--muted);
  font-size: 12.5px; font-weight: 500; padding: 4px 12px; border-radius: 7px;
  transition: color .15s, background .15s, box-shadow .15s;
}
.modes button:hover { color: var(--text); }
.modes button.active { color: var(--text); background: var(--surface); box-shadow: var(--shadow-sm), 0 0 0 1px var(--border); }
.adv { position: relative; flex-shrink: 0; }
.adv-btn {
  background: none; border: 0; cursor: pointer; color: var(--faint);
  font-size: 12.5px; font-weight: 500; padding: 5px 8px; border-radius: 7px;
}
.adv-btn:hover, .adv-btn[aria-expanded="true"] { color: var(--text); background: var(--surface-2); }
.adv-pop {
  position: absolute; top: calc(100% + 6px); left: 0; z-index: 25; width: 320px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-sm);
  box-shadow: var(--shadow-lg); padding: 12px 14px; animation: rise .15s ease;
}
.switch-row { display: flex; gap: 12px; align-items: flex-start; cursor: pointer; }
.switch-row input { position: absolute; opacity: 0; pointer-events: none; }
.switch {
  flex-shrink: 0; width: 34px; height: 20px; border-radius: 99px; margin-top: 1px;
  background: var(--border-strong); position: relative; transition: background .15s;
}
.switch::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%;
  background: #fff; box-shadow: var(--shadow-sm); transition: transform .15s;
}
.switch-row input:checked + .switch { background: var(--accent); }
.switch-row input:checked + .switch::after { transform: translateX(14px); }
.switch-row input:focus-visible + .switch { box-shadow: 0 0 0 3px var(--accent-ring); }
.switch-title { display: block; font-size: 13.5px; font-weight: 600; }
.switch-sub { display: block; font-size: 12.5px; color: var(--muted); line-height: 1.45; margin-top: 2px; }
mark.fz { background: var(--fuzzy-bg); color: var(--fuzzy-text); }
.score { margin-left: auto; font-size: 12px; font-weight: 500; color: var(--accent); background: var(--accent-soft); padding: 2px 9px; border-radius: 99px; }

.sem-panel { margin-top: 14px; padding: 20px 22px; display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.sem-panel.hidden { display: none; }
.sem-title { font-weight: 650; font-size: 15px; }
.sem-body { color: var(--muted); font-size: 13.5px; max-width: 62ch; }
.sem-err { color: var(--danger); font-size: 13px; }
.sem-panel .btn { margin-top: 6px; }
.sem-panel .bar { width: 100%; height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin-top: 4px; }
.sem-panel .bar i { display: block; height: 100%; background: var(--accent); border-radius: 99px; transition: width .3s ease; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: color-mix(in srgb, var(--accent) 88%, black); }

/* ---------- source-page viewer ---------- */
body.noscroll { overflow: hidden; }
#viewer {
  position: fixed; inset: 0; z-index: 50;
  background: #1c1b1966; backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
  display: flex; justify-content: center; padding: 24px;
  animation: fade .15s ease;
}
@keyframes fade { from { opacity: 0; } }
.v-panel {
  width: min(1000px, 100%); display: flex; flex-direction: column;
  background: var(--surface-2); border-radius: 18px; overflow: hidden;
  box-shadow: var(--shadow-lg); animation: rise .2s ease;
}
.v-bar {
  display: flex; align-items: center; gap: 16px; padding: 12px 14px 12px 20px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.v-titles { min-width: 0; flex: 1; }
#v-title { font-weight: 650; font-size: 14.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#v-sub { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.v-actions { display: flex; gap: 4px; }
.icon-btn {
  width: 34px; height: 34px; display: grid; place-items: center;
  background: none; border: 0; border-radius: 9px; cursor: pointer; color: var(--muted);
  transition: background .15s, color .15s;
}
.icon-btn:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.icon-btn:disabled { opacity: .35; cursor: default; }
.icon-btn svg { width: 18px; height: 18px; }
#v-body { flex: 1; overflow: auto; padding: 24px; display: flex; justify-content: center; align-items: flex-start; }
#v-page { position: relative; box-shadow: 0 1px 3px #0000001a, 0 8px 30px -8px #00000026; border-radius: 4px; background: #fff; line-height: 0; transition: opacity .15s; }
#v-page.loading { opacity: .55; }
#v-page[data-missing]::after {
  content: attr(data-missing); display: block; padding: 60px 40px; max-width: 46ch;
  line-height: 1.5; color: var(--muted); font-size: 14px; text-align: center;
}
#v-canvas { display: block; border-radius: 4px; }
#v-hl { position: absolute; inset: 0; pointer-events: none; }
.hl {
  position: absolute; border-radius: 3px;
  background: var(--hl); mix-blend-mode: multiply;
  pointer-events: auto; cursor: default;
}
.hl.approx { background: var(--hl-approx); }
.hl.passage { background: var(--hl-passage); border-radius: 2px; }
.hl.active { box-shadow: 0 0 0 2px var(--hl-ring); animation: pulse 1.4s ease 2; }
@keyframes pulse { 50% { box-shadow: 0 0 0 5px var(--hl-ring-soft); } }

/* ---------- small screens ---------- */
@media (max-width: 720px) {
  nav { margin-top: 16px; }
  main { padding: 0 16px 80px; }
  .hero { padding-top: 32px; }
  kbd.hint { display: none; }
  .res { grid-template-columns: minmax(0, 1fr); }
  .search-meta { flex-wrap: wrap; gap: 8px; }
  .search-meta .qstatus { flex-basis: 100%; }
  #viewer { padding: 0; }
  .v-panel { border-radius: 0; }
  #v-body { padding: 12px; }
  #lib-table th:nth-child(4), #lib-table td:nth-child(4),
  #lib-table th:nth-child(5), #lib-table td:nth-child(5) { display: none; }
}
