:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e1e4ea;
  --border-strong: #c9ced8;
  --text: #1c2230;
  --text-2: #5a6377;
  --text-3: #8a92a5;
  --accent: #3b6fd8;
  --accent-hover: #2f5ec2;
  --accent-soft: #e8effc;
  --ok: #1f8a4c;
  --ok-soft: #e3f4ea;
  --warn: #a76200;
  --warn-soft: #fdf1dc;
  --bad: #c23a3a;
  --bad-soft: #fbe6e6;
  --muted-soft: #eef0f4;
  --on-accent: #ffffff;
  --shadow: 0 1px 2px rgba(20, 30, 50, 0.06), 0 2px 8px rgba(20, 30, 50, 0.04);
  --shadow-up: 0 -4px 16px rgba(20, 30, 50, 0.08);
  --shadow-dialog: 0 20px 60px rgba(15, 20, 30, 0.25);
  --backdrop: rgba(15, 20, 30, 0.45);
  --radius: 10px;
  --mono: ui-monospace, 'Cascadia Mono', 'Consolas', 'SF Mono', monospace;
  color-scheme: light;
}

/* data-theme is set before first paint by the inline script in index.html,
   resolving the "System" choice to light or dark. */
:root[data-theme="dark"] {
  --bg: #12151b;
  --surface: #1a1e26;
  --surface-2: #20252f;
  --border: #2c323e;
  --border-strong: #3b4351;
  --text: #e6e9ef;
  --text-2: #a5adbd;
  --text-3: #808a9c;
  --accent: #6d9bff;
  --accent-hover: #87adff;
  --accent-soft: #1f2b45;
  --ok: #4cc683;
  --ok-soft: #173225;
  --warn: #f0b04f;
  --warn-soft: #3a2c13;
  --bad: #ff7b7b;
  --bad-soft: #3d1c1f;
  --muted-soft: #252a34;
  --on-accent: #0b1220;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-up: 0 -4px 16px rgba(0, 0, 0, 0.35);
  --shadow-dialog: 0 20px 60px rgba(0, 0, 0, 0.6);
  --backdrop: rgba(0, 0, 0, 0.6);
  color-scheme: dark;
}

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

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  min-height: 100vh;
}

svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

code, .mono { font-family: var(--mono); font-size: 0.92em; }
code { background: var(--muted-soft); padding: 1px 5px; border-radius: 4px; }

h1, h2, h3 { margin: 0; font-weight: 600; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark { width: 34px; height: 34px; flex: none; color: var(--accent); }
.brand-mark rect { fill: var(--accent); stroke: none; }
.brand-mark path { stroke: var(--on-accent); }
.brand h1 { font-size: 17px; }
.topbar-actions { display: flex; align-items: center; gap: 8px; flex: none; }
.seg.theme-toggle > label > span { display: flex; align-items: center; padding: 6px 9px; }
.theme-toggle svg { width: 16px; height: 16px; }
.theme-toggle label:not(:last-child) > span { border-right: 1px solid var(--border); }
.theme-toggle input:not(:checked) + span:hover { background: var(--muted-soft); color: var(--text); }
@media (max-width: 480px) {
  .btn-label { display: none; }
  .brand h1 { font-size: 15px; }
}
.brand p { margin: 0; color: var(--text-2); font-size: 13px; }

/* ---------- Layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(320px, 400px) minmax(0, 1fr);
  gap: 20px;
  padding: 20px 24px 0;
  max-width: 1500px;
  margin: 0 auto;
  align-items: start;
}
.setup { display: flex; flex-direction: column; gap: 14px; position: sticky; top: 20px; max-height: calc(100vh - 40px); overflow-y: auto; padding-bottom: 20px; }
.results { min-width: 0; padding-bottom: 20px; display: flex; flex-direction: column; min-height: calc(100vh - 110px); }

@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 16px 16px 0; }
  .setup { position: static; max-height: none; overflow: visible; }
  .topbar { padding: 12px 16px; }
  .brand p { display: none; }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.card h2 { font-size: 15px; display: flex; align-items: center; gap: 8px; }
.step {
  display: inline-grid; place-items: center;
  width: 22px; height: 22px; border-radius: 50%;
  background: var(--accent-soft); color: var(--accent);
  font-size: 12px; font-weight: 700;
}

/* ---------- Form controls ---------- */
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.label { font-weight: 600; font-size: 13px; display: flex; align-items: center; gap: 6px; }
.ico { width: 15px; height: 15px; color: var(--text-2); }
.hint { color: var(--text-3); font-size: 12.5px; margin: 0; }
.small { font-size: 12.5px; }

input[type="text"], input[type="search"], select {
  width: 100%;
  min-width: 0;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  padding: 7px 10px;
  outline: none;
  transition: border-color 0.12s, box-shadow 0.12s;
}
input.mono { font-family: var(--mono); font-size: 13px; }
input:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input.invalid { border-color: var(--bad); }

.path-input { display: flex; gap: 6px; }
.path-input input { flex: 1; }

.check { display: flex; align-items: flex-start; gap: 8px; cursor: pointer; font-size: 13px; }
.check input { margin: 2px 0 0; accent-color: var(--accent); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 500;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  padding: 7px 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s, border-color 0.12s;
}
.btn svg { width: 16px; height: 16px; }
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--text-2); }
.btn.ghost:hover { background: var(--muted-soft); color: var(--text); }
.btn.danger { color: var(--bad); }
.btn.small { padding: 3px 9px; font-size: 12.5px; }
.btn.wide { width: 100%; }

.link { background: none; border: none; padding: 0; font: inherit; color: var(--accent); cursor: pointer; }
.link:hover { text-decoration: underline; }
.link.danger { color: var(--bad); }

/* ---------- Scheme ---------- */
.scheme-desc { margin: -4px 0 0; color: var(--text-2); font-size: 13px; }
.scheme-desc a { color: var(--accent); }
.templates { display: flex; flex-direction: column; gap: 12px; }
.tpl-error { color: var(--bad); font-size: 12.5px; }
.tpl-error:empty { display: none; }
.example {
  font-family: var(--mono); font-size: 12px; color: var(--text-2);
  background: var(--surface-2); border: 1px dashed var(--border); border-radius: 6px;
  padding: 6px 8px; display: flex; flex-direction: column; gap: 2px; overflow-wrap: anywhere;
}
.example .sep { color: var(--text-3); padding: 0 3px; }
.modified {
  display: flex; flex-wrap: wrap; gap: 4px 14px; align-items: center;
  font-size: 12.5px; color: var(--warn); background: var(--warn-soft);
  padding: 6px 10px; border-radius: 6px;
}
.tokens summary { cursor: pointer; color: var(--text-2); font-size: 13px; font-weight: 500; }
.tokens[open] summary { margin-bottom: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 5px; margin: 8px 0; }
.chip {
  font-family: var(--mono); font-size: 12px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  border-radius: 999px; padding: 2px 9px; cursor: pointer;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
.syntax { display: grid; grid-template-columns: auto 1fr; gap: 5px 10px; margin: 6px 0 0; font-size: 12.5px; color: var(--text-2); }
.syntax dt { white-space: nowrap; }
.syntax dd { margin: 0; }

/* ---------- Results ---------- */
.empty {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  gap: 10px; padding: 48px 24px;
  border: 1px dashed var(--border-strong); border-radius: var(--radius);
  color: var(--text-2);
}
.empty svg { width: 64px; height: 64px; color: var(--text-3); stroke-width: 1.5; }
.empty h2 { color: var(--text); font-size: 17px; }
.empty p { max-width: 520px; margin: 0; }

.progress-panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; display: flex; flex-direction: column; gap: 8px; }
.progress-label { display: flex; justify-content: space-between; font-weight: 500; }
.progress { height: 8px; border-radius: 999px; background: var(--muted-soft); overflow: hidden; }
.progress .bar { height: 100%; width: 0; background: var(--accent); border-radius: 999px; transition: width 0.2s; }
.progress.indeterminate .bar { width: 30%; animation: slide 1.1s ease-in-out infinite; }
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(340%); } }
#progressCurrent { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.summary { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 14px; min-width: 110px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 2px; cursor: default; text-align: left; font: inherit; color: inherit;
}
button.stat { cursor: pointer; }
button.stat:hover { border-color: var(--border-strong); }
.stat .num { font-size: 20px; font-weight: 650; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat .lbl { font-size: 12px; color: var(--text-2); }
.stat.ok .num { color: var(--ok); }
.stat.warn .num { color: var(--warn); }
.stat.bad .num { color: var(--bad); }

.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.tabs { display: inline-flex; background: var(--muted-soft); border-radius: 8px; padding: 3px; }
.tab { font: inherit; font-weight: 500; border: none; background: none; color: var(--text-2); padding: 5px 14px; border-radius: 6px; cursor: pointer; }
.tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.filters { display: flex; gap: 8px; flex: 1; justify-content: flex-end; min-width: 260px; }
.filters select { width: auto; }
.filters input { max-width: 280px; }

.list-head { display: flex; justify-content: space-between; align-items: center; padding: 0 4px 8px; }

.items { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.item {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; box-shadow: var(--shadow);
  display: grid; grid-template-columns: auto auto minmax(0, 1fr) auto; gap: 4px 12px; align-items: start;
}
.item.excluded { opacity: 0.55; }
.item.conflict { border-color: color-mix(in srgb, var(--bad) 45%, var(--border)); }
.item > input[type="checkbox"] { margin-top: 3px; accent-color: var(--accent); width: 16px; height: 16px; }
.kind-icon { width: 30px; height: 30px; border-radius: 8px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); }
.kind-icon svg { width: 17px; height: 17px; }
.kind-icon.ebook { background: var(--ok-soft); color: var(--ok); }
.item-main { min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.item-title { font-weight: 600; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.item-title .author { font-weight: 400; color: var(--text-2); }
.badge { font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 999px; background: var(--muted-soft); color: var(--text-2); white-space: nowrap; }
.badge.tags { background: var(--ok-soft); color: var(--ok); }
.badge.guessed { background: var(--warn-soft); color: var(--warn); }
.badge.missing, .badge.conflict { background: var(--bad-soft); color: var(--bad); }
.badge.edited { background: var(--accent-soft); color: var(--accent); }
.paths { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 1px 8px; font-size: 12.5px; }
.paths .k { color: var(--text-3); }
.paths .v { font-family: var(--mono); font-size: 12px; overflow-wrap: anywhere; color: var(--text-2); }
.paths .v.to { color: var(--text); }
.paths .v.same { color: var(--text-3); font-style: italic; font-family: inherit; }
.problems { margin: 4px 0 0; padding: 0; list-style: none; font-size: 12.5px; }
.problems li { color: var(--bad); }
.problems li.warn { color: var(--warn); }
.item-actions { display: flex; gap: 6px; }

.editor {
  grid-column: 3 / -1;
  margin-top: 8px; padding: 12px; border-radius: 8px; background: var(--surface-2); border: 1px solid var(--border);
  display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px;
  scroll-margin-bottom: 110px; /* clear the sticky action bar */
}
.editor .field.wide { grid-column: span 2; }
.editor .src { font-weight: 400; color: var(--text-3); font-size: 11.5px; }
.editor-foot { grid-column: 1 / -1; display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.files { grid-column: 3 / -1; margin: 6px 0 0; padding: 8px 10px; list-style: none; border-radius: 6px; background: var(--surface-2); font-family: var(--mono); font-size: 12px; color: var(--text-2); max-height: 220px; overflow: auto; }

@media (max-width: 640px) {
  .item { grid-template-columns: auto minmax(0, 1fr); }
  .kind-icon { display: none; }
  .item-actions { grid-column: 2; }
  .editor, .files { grid-column: 1 / -1; }
  .editor .field.wide { grid-column: auto; }
  .filters { min-width: 0; width: 100%; }
}

/* Tree */
.tree { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; font-size: 13px; box-shadow: var(--shadow); }
.tree ul { list-style: none; margin: 0; padding-left: 18px; border-left: 1px solid var(--border); }
.tree > ul { padding-left: 0; border-left: none; }
.tree li { margin: 1px 0; }
.tree summary, .tree .leaf { display: flex; align-items: center; gap: 6px; padding: 2px 4px; border-radius: 5px; cursor: pointer; list-style: none; }
.tree summary::-webkit-details-marker { display: none; }
.tree summary:hover { background: var(--muted-soft); }
.tree .leaf { cursor: default; color: var(--text-2); font-family: var(--mono); font-size: 12px; }
.tree svg { width: 15px; height: 15px; flex: none; }
.tree .folder-ico { color: var(--warn); }
.tree .count { color: var(--text-3); font-size: 11.5px; margin-left: 4px; }
.tree .book { font-weight: 600; }
.tree .dim { opacity: 0.55; }
.tree .caret { transition: transform 0.12s; color: var(--text-3); }
.tree details[open] > summary .caret { transform: rotate(90deg); }

.extra { margin-top: 14px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px; }
.extra summary { cursor: pointer; font-weight: 500; color: var(--text-2); }
.extra ul { margin: 8px 0 0; padding-left: 18px; max-height: 240px; overflow: auto; color: var(--text-2); }

/* Action bar */
.actionbar {
  position: sticky; bottom: 0; margin-top: auto;
  display: flex; flex-wrap: wrap; align-items: center; gap: 12px 16px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius) var(--radius) 0 0;
  padding: 12px 16px; box-shadow: var(--shadow-up);
  margin-bottom: -20px;
}
#resultsBody { margin-bottom: 16px; }
.spacer { flex: 1; }
.seg { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 7px; overflow: hidden; }
.seg label { cursor: pointer; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg > label > span { display: block; padding: 6px 14px; font-weight: 500; color: var(--text-2); }
.seg input:checked + span { background: var(--accent); color: var(--on-accent); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }

/* Dialogs */
.dialog {
  border: 1px solid var(--border); border-radius: 12px; padding: 0;
  background: var(--surface); color: var(--text);
  width: min(560px, calc(100vw - 32px));
  box-shadow: var(--shadow-dialog);
}
.dialog.wide-dialog { width: min(760px, calc(100vw - 32px)); }
.dialog::backdrop { background: var(--backdrop); }
.dialog-inner { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.dialog h2 { font-size: 17px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
#confirmBody, #resultBody { display: flex; flex-direction: column; gap: 10px; }
.browse-list { list-style: none; margin: 0; padding: 4px; border: 1px solid var(--border); border-radius: 8px; height: 320px; overflow: auto; }
.browse-list button {
  width: 100%; text-align: left; font: inherit; color: var(--text); background: none; border: none;
  display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 6px; cursor: pointer;
}
.browse-list button:hover, .browse-list button:focus-visible { background: var(--muted-soft); outline: none; }
.browse-list svg { width: 16px; height: 16px; color: var(--warn); flex: none; }
.browse-list .empty-note { color: var(--text-3); padding: 10px; }

.facts { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0; font-size: 13.5px; }
.facts dt { color: var(--text-2); }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.note { font-size: 13px; padding: 8px 10px; border-radius: 6px; background: var(--muted-soft); color: var(--text-2); margin: 0; }
.note.warn { background: var(--warn-soft); color: var(--warn); }
.note.bad { background: var(--bad-soft); color: var(--bad); }
.errlist { max-height: 200px; overflow: auto; font-size: 12.5px; color: var(--bad); padding-left: 18px; margin: 0; }

.history { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; max-height: 60vh; overflow: auto; }
.history li { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; display: flex; gap: 12px; align-items: center; }
.history .h-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.history .h-paths { font-family: var(--mono); font-size: 12px; color: var(--text-2); overflow-wrap: anywhere; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px;
  box-shadow: var(--shadow-dialog); max-width: min(560px, calc(100vw - 32px)); z-index: 10; font-size: 13.5px;
}
.toast.error { background: var(--bad-soft); color: var(--bad); border: 1px solid var(--bad); }

[hidden] { display: none !important; }
