:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --panel-2: #f0f2f5;
  --border: #d9dde3;
  --text: #1c2026;
  --muted: #5d6673;
  --link: #1b5fd1;
  --accent: #2f6fed;
  --accent-hover: #1f5cd6;
  --accent-text: #ffffff;
  --danger: #c62f2f;
  --warn: #9a6200;
  --ok: #1f7a35;
  --warn-bg: #fff4dc;
  --error-bg: #fde8e8;
  --info-bg: #e5efff;
  --ok-bg: #e3f5e7;
  --input-bg: #ffffff;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  /* coverage cells */
  --c-available: #7cc4ff;
  --c-multi: #3d8fe0;
  --c-downloaded: #3fb35d;
  --c-failed: #e0564f;
  --c-absent: #e7e9ed;
  --chosen: rgba(0, 0, 0, 0.45);
  --radius: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111417;
    --panel: #1b1f24;
    --panel-2: #232830;
    --border: #2f353d;
    --text: #e6e6e6;
    --muted: #9aa3ae;
    --link: #9fd3ff;
    --accent: #2f6fed;
    --accent-hover: #4b83f0;
    --danger: #f06868;
    --warn: #f0c95c;
    --ok: #6fd97f;
    --warn-bg: #3a2f14;
    --error-bg: #3d1c1c;
    --info-bg: #182a44;
    --ok-bg: #173222;
    --input-bg: #0f1216;
    --shadow: none;
    --c-available: #2c6aa0;
    --c-multi: #4c97e6;
    --c-downloaded: #2f9c4c;
    --c-failed: #c9443e;
    --c-absent: #2a2f36;
    --chosen: rgba(255, 255, 255, 0.6);
  }
}

* { box-sizing: border-box; }

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

body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
}

a { color: var(--link); }
h1 { font-size: 1.5rem; margin: 0 0 1rem; }
h2 { font-size: 1.15rem; margin: 0 0 0.75rem; }
h3 { font-size: 1rem; margin: 0.5rem 0; }

.sr-only, .skip-link:not(:focus) {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}
.skip-link:focus { position: absolute; top: 0.5rem; left: 0.5rem; z-index: 100; background: var(--panel); padding: 0.5rem; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* --- top bar --- */
.topbar {
  display: flex; align-items: center; gap: 1.5rem; flex-wrap: wrap;
  padding: 0.75rem 1.5rem;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.1rem; color: var(--text); text-decoration: none; }
.topbar nav { display: flex; gap: 0.25rem; flex-wrap: wrap; }
.topbar nav a {
  color: var(--muted); text-decoration: none; padding: 0.35rem 0.6rem; border-radius: 6px;
}
.topbar nav a:hover { color: var(--text); background: var(--panel-2); }
.topbar nav a[aria-current="page"] { color: var(--text); background: var(--panel-2); font-weight: 600; }
.logout { margin-left: auto; }
.dev-strip { background: var(--warn-bg); color: var(--warn); text-align: center; font-size: 0.8rem; padding: 0.2rem; }

main { max-width: 1200px; margin: 1.5rem auto; padding: 0 1rem; }

/* --- panels / layout --- */
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem;
  margin-bottom: 1.25rem;
  box-shadow: var(--shadow);
}
.panel-header, .page-head {
  display: flex; justify-content: space-between; align-items: center; gap: 0.75rem; flex-wrap: wrap;
  margin-bottom: 0.75rem;
}
.panel-header h2, .page-head h1 { margin: 0; }
.actions { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 1.25rem; }
.grid-2 > .panel { margin-bottom: 0; }
.grid-2 { margin-bottom: 1.25rem; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.prewrap { white-space: pre-wrap; }
.count { color: var(--muted); font-weight: normal; font-size: 0.9rem; }
.plain-list { list-style: none; padding: 0; margin: 0.25rem 0; }
.plain-list li { padding: 0.15rem 0; }

/* --- forms --- */
input, select, button, .button, textarea {
  font: inherit; font-size: 0.95rem;
  padding: 0.45rem 0.65rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--input-bg);
  color: var(--text);
}
input[type="checkbox"], input[type="radio"] { padding: 0; width: auto; accent-color: var(--accent); }
button, .button {
  background: var(--panel-2); cursor: pointer; text-decoration: none; display: inline-block; line-height: 1.2;
}
button:hover:not(:disabled), .button:hover { border-color: var(--accent); }
button:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-danger { color: var(--danger); }
.btn-ghost { background: transparent; }
.btn-sm { padding: 0.2rem 0.45rem; font-size: 0.8rem; }
button.busy { cursor: progress; opacity: 0.7; }

.inline-form { display: flex; gap: 0.5rem; align-items: flex-end; }
.inline-form input[type="search"], .inline-form .grow { flex: 1; min-width: 0; }
.inline-form.wrap { flex-wrap: wrap; }
.inline-form label { display: flex; flex-direction: column; gap: 0.2rem; font-size: 0.85rem; color: var(--muted); }
.stacked-form { display: flex; flex-direction: column; gap: 0.8rem; max-width: 480px; }
.stacked-form.wide { max-width: none; }
.stacked-form label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.9rem; color: var(--muted); }
.stacked-form label.checkbox-label, .checkbox-label { flex-direction: row; align-items: center; gap: 0.5rem; color: var(--text); }
.stacked-form .help, .help { margin: -0.5rem 0 0; font-size: 0.8rem; color: var(--muted); }
.label { font-size: 0.9rem; color: var(--muted); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0.8rem; }
.form-actions, .dialog-actions { display: flex; gap: 0.5rem; align-items: center; justify-content: flex-end; }
.form-actions { justify-content: flex-start; }
fieldset { border: 1px solid var(--border); border-radius: 6px; padding: 0.6rem 0.8rem; margin: 0; }
legend { color: var(--muted); font-size: 0.9rem; padding: 0 0.3rem; }
.checks { display: flex; flex-wrap: wrap; gap: 0.4rem 1rem; }
.checks label { display: flex; flex-direction: row; gap: 0.35rem; align-items: center; color: var(--text); }
.preview { margin: -0.3rem 0 0; font-size: 0.9rem; }
code { background: var(--panel-2); padding: 0.1rem 0.3rem; border-radius: 4px; }

.login-panel { max-width: 380px; margin: 3rem auto; }

/* --- alerts / banners / toasts --- */
.alert { padding: 0.6rem 0.8rem; border-radius: 6px; margin: 0 0 1rem; border: 1px solid transparent; }
.alert-error { background: var(--error-bg); color: var(--danger); border-color: var(--danger); }
.alert-warn { background: var(--warn-bg); color: var(--warn); border-color: var(--warn); }
.alert-info { background: var(--info-bg); border-color: var(--border); }
.alert ul { margin: 0.3rem 0 0; padding-left: 1.2rem; }
.banners .alert strong { margin-right: 0.3rem; }
.toasts { position: fixed; right: 1rem; bottom: 1rem; display: flex; flex-direction: column; gap: 0.5rem; z-index: 50; max-width: min(420px, calc(100vw - 2rem)); }
.toast { background: var(--panel); border: 1px solid var(--border); border-left: 4px solid var(--ok); padding: 0.6rem 0.8rem; border-radius: 6px; box-shadow: 0 4px 14px rgba(0,0,0,0.25); }
.toast.error { border-left-color: var(--danger); }
.result.ok { color: var(--ok); }
.result.error { color: var(--danger); }

/* --- counters / chips --- */
.counters { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 1rem; margin-bottom: 1.25rem; }
.counter { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.8rem 1rem; display: flex; flex-direction: column; }
.counter-value { font-size: 1.6rem; font-weight: 700; }
.counter-label { color: var(--muted); font-size: 0.85rem; }
.chips { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.chip { display: inline-flex; align-items: center; gap: 0.35rem; padding: 0.2rem 0.6rem; border-radius: 999px; font-size: 0.85rem; border: 1px solid var(--border); background: var(--panel-2); }
.chip::before { content: ""; width: 0.55rem; height: 0.55rem; border-radius: 50%; background: var(--muted); }
.chip.ok::before { background: var(--ok); }
.chip.warn::before { background: var(--warn); }
.chip.bad::before { background: var(--danger); }

/* --- tables --- */
.table-wrap { overflow-x: auto; position: relative; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 0.4rem 0.5rem; border-bottom: 1px solid var(--border); font-size: 0.9rem; vertical-align: middle; }
th { color: var(--muted); font-weight: 600; font-size: 0.8rem; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.error-text { color: var(--danger); font-size: 0.8rem; overflow-wrap: anywhere; }

.progress { position: relative; height: 0.55rem; background: var(--panel-2); border-radius: 999px; overflow: hidden; min-width: 80px; border: 1px solid var(--border); }
.progress > span { position: absolute; inset: 0 auto 0 0; background: var(--accent); }

/* --- badges --- */
.badge { display: inline-block; padding: 0.1rem 0.45rem; border-radius: 4px; font-size: 0.75rem; font-weight: 600; background: var(--panel-2); border: 1px solid var(--border); white-space: nowrap; }
.st-done { color: var(--ok); background: var(--ok-bg); border-color: transparent; }
.st-queued, .st-downloading, .st-processing, .st-wanted { color: var(--link); background: var(--info-bg); border-color: transparent; }
.st-failed, .st-failed_all { color: var(--danger); background: var(--error-bg); border-color: transparent; }
.st-missing_everywhere { color: var(--warn); background: var(--warn-bg); border-color: transparent; }
.st-split_only { color: var(--warn); }
.st-ignored { color: var(--muted); text-decoration: line-through; }
.level-error { color: var(--danger); }
.level-warning { color: var(--warn); }

/* --- cards (search results, library) --- */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 1rem; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; box-shadow: var(--shadow); }
.card .cover { aspect-ratio: 2 / 3; width: 100%; object-fit: cover; background: var(--panel-2); display: block; }
.card-body { padding: 0.6rem; display: flex; flex-direction: column; gap: 0.4rem; flex: 1; }
.card-title { font-weight: 600; font-size: 0.95rem; overflow-wrap: anywhere; }
.card-title a { color: var(--text); text-decoration: none; }
.card-title a:hover { text-decoration: underline; }
.card-foot { margin-top: auto; display: flex; gap: 0.4rem; flex-wrap: wrap; align-items: center; }
.entry-badges { display: flex; flex-direction: column; gap: 0.25rem; }
.entry-badge { display: flex; gap: 0.35rem; align-items: flex-start; font-size: 0.8rem; background: var(--panel-2); border-radius: 4px; padding: 0.2rem 0.35rem; }
.entry-badge span { overflow-wrap: anywhere; }
.entry-badge .src { font-weight: 600; }
.card.dim { opacity: 0.6; }
.search-cards .cards { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
.source-status { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-top: 0.75rem; }
.source-picks { margin-top: 0.25rem; }

/* --- series page --- */
.series-header { display: flex; gap: 1.25rem; align-items: flex-start; }
.cover-lg { width: 160px; aspect-ratio: 2 / 3; object-fit: cover; border-radius: 6px; background: var(--panel-2); flex: 0 0 auto; }
.series-meta { flex: 1; min-width: 0; }
.series-meta h1 { margin-bottom: 0.3rem; overflow-wrap: anywhere; }
.order-list { margin: 0.3rem 0 0.5rem; padding-left: 1.5rem; }
.order-list li { padding: 0.2rem 0; display: flex; gap: 0.4rem; align-items: center; }
.order-list li span { flex: 1; }
.links-table input[type="text"] { width: 5rem; }

.cov-toolbar { display: flex; justify-content: space-between; align-items: center; gap: 0.75rem; flex-wrap: wrap; margin: 0.5rem 0; }
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; flex-wrap: wrap; }
.segmented label { position: relative; padding: 0.35rem 0.7rem; cursor: pointer; font-size: 0.85rem; display: flex; align-items: center; gap: 0.3rem; border-right: 1px solid var(--border); }
.segmented label:last-child { border-right: none; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label:has(input:checked) { background: var(--accent); color: var(--accent-text); }
.segmented label:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: -2px; }
.legend { display: flex; gap: 0.9rem; flex-wrap: wrap; color: var(--muted); margin-bottom: 0.5rem; }
.legend span { display: inline-flex; align-items: center; gap: 0.3rem; }
.legend .cell { display: inline-block; width: 0.9rem; height: 0.9rem; border-radius: 3px; }

table.matrix { width: auto; min-width: 100%; }
table.matrix th, table.matrix td { padding: 0.25rem 0.4rem; font-size: 0.85rem; }
table.matrix th.srccol { writing-mode: horizontal-tb; max-width: 7rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
table.matrix td.c { width: 2.2rem; min-width: 2.2rem; padding: 0.15rem; }
table.matrix td.c > span { display: block; height: 1.3rem; border-radius: 3px; }
table.matrix td.c.clickable > span { cursor: pointer; }
table.matrix td.c.clickable > span:hover { outline: 2px solid var(--text); }
.legend .cell-available, td.c.cell-available > span { background: var(--c-available); }
.legend .cell-available_multi, td.c.cell-available_multi > span { background: var(--c-multi); }
.legend .cell-downloaded_from_here, td.c.cell-downloaded_from_here > span { background: var(--c-downloaded); }
.legend .cell-failed_here, td.c.cell-failed_here > span { background: var(--c-failed); }
.legend .cell-absent, td.c.cell-absent > span { background: var(--c-absent); }
td.c.chosen > span { box-shadow: inset 0 0 0 2px var(--chosen); }
table.matrix td.title { max-width: 18rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
tr.fold td { background: var(--panel-2); }
tr.fold button { font-size: 0.8rem; }
tr.row-ignored td.n, tr.row-ignored td.title { color: var(--muted); }
.row-menu-btn { padding: 0.1rem 0.45rem; }

.popover { position: absolute; z-index: 40; background: var(--panel); border: 1px solid var(--border); border-radius: 6px; box-shadow: 0 6px 20px rgba(0,0,0,0.3); padding: 0.3rem; min-width: 220px; display: flex; flex-direction: column; }
.popover[hidden] { display: none; }
.popover button { background: transparent; border: none; text-align: left; padding: 0.45rem 0.6rem; border-radius: 4px; }
.popover button:hover, .popover button:focus-visible { background: var(--panel-2); }
.popover .menu-head { font-size: 0.75rem; color: var(--muted); padding: 0.3rem 0.6rem; border-bottom: 1px solid var(--border); margin-bottom: 0.2rem; }

.candidates { display: flex; flex-direction: column; gap: 0.5rem; margin-top: 0.75rem; max-height: 55vh; overflow-y: auto; }
.candidate { display: flex; gap: 0.75rem; align-items: center; padding: 0.4rem; border: 1px solid var(--border); border-radius: 6px; }
.candidate img { width: 48px; aspect-ratio: 2 / 3; object-fit: cover; border-radius: 4px; background: var(--panel-2); }
.candidate .info { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.score { font-variant-numeric: tabular-nums; font-weight: 600; }

/* --- dialogs --- */
dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--panel); color: var(--text); padding: 1.25rem; width: min(480px, calc(100vw - 2rem)); }
dialog.wide-dialog { width: min(760px, calc(100vw - 2rem)); }
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }
dialog h2 { margin-top: 0; }

/* --- extensions --- */
.ext-list { display: flex; flex-direction: column; }
.ext-row { display: flex; align-items: center; gap: 0.75rem; padding: 0.45rem 0; border-bottom: 1px solid var(--border); }
.ext-row img { width: 32px; height: 32px; border-radius: 6px; background: var(--panel-2); }
.ext-row .info { flex: 1; min-width: 0; }
tr.dragging { opacity: 0.5; }
tr.drag-over td { border-top: 2px solid var(--accent); }
td.handle { cursor: grab; color: var(--muted); user-select: none; }
.integration { border-top: 1px solid var(--border); padding-top: 0.5rem; margin-top: 0.5rem; }
.integration:first-of-type { border-top: none; margin-top: 0; }

/* --- phones --- */
@media (max-width: 640px) {
  .topbar { padding: 0.6rem 0.75rem; gap: 0.5rem; }
  .topbar nav { order: 3; width: 100%; overflow-x: auto; flex-wrap: nowrap; position: relative; }
  main { margin-top: 1rem; padding: 0 0.6rem; }
  .panel { padding: 0.8rem; }
  .series-header { flex-direction: column; }
  .cover-lg { width: 120px; }
  .cards { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 0.6rem; }
  .inline-form { flex-wrap: wrap; }
  table.hide-sm-cols .hide-sm { display: none; }
  table.matrix td.title { max-width: 8rem; }
}
