:root {
  color-scheme: light dark;
  --bg: #f6f5fa;
  --panel: #ffffff;
  --ink: #2b2d42;
  --muted: #6b7088;
  --line: #e6e4f0;
  --accent: #5b9aa9;
  --accent-ink: #12303a;
  --fill-down: #d6ecf5;
  --fill-seed: #dcf1dc;
  --fill-idle: #ebebf3;
  --fill-stop: #fbe8d6;
  --fill-check: #e4dcf6;
  --danger: #c9505a;
  --warn: #8a5a00;
  --focus: #0a58ca;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16,24,40,.06);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171f;
    --panel: #1e2029;
    --ink: #e8e9f2;
    --muted: #9a9db5;
    --line: #2e303d;
    --accent: #8cc7d6;
    --accent-ink: #14282e;
    --fill-down: #274456;
    --fill-seed: #2a4433;
    --fill-idle: #2a2c3a;
    --fill-stop: #4a3b30;
    --fill-check: #3d3664;
    --danger: #f09098;
    --warn: #f0b44c;
    --focus: #7db4ff;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 13px/1.4 var(--font);
  min-height: 100vh;
}
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
button, input, select { font: inherit; color: inherit; }
.num, .size, .pct { font-variant-numeric: tabular-nums; }

/* ---------- Boutons et champs ---------- */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 7px;
  padding: 5px 11px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.small { padding: 3px 9px; font-size: 12px; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .55; cursor: default; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 14px; }
.field label { font-weight: 600; }
.field .hint { color: var(--muted); font-size: 13px; }
input[type="text"], input[type="password"], input[type="number"], select {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 9px;
}

/* ---------- Écran de connexion ---------- */
.auth {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.auth form {
  width: min(380px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 28px;
}
.auth h1 { margin: 0 0 4px; font-size: 22px; }
.auth p.lead { margin: 0 0 20px; color: var(--muted); }
.error { color: var(--danger); min-height: 1.5em; margin: 0 0 8px; }

/* ---------- Structure principale ---------- */
.shell { max-width: 1280px; margin: 0 auto; padding: 0 16px 48px; }
header.top {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 0;
  flex-wrap: wrap;
}
header.top h1 { margin: 0; font-size: 17px; letter-spacing: -.01em; }
.spacer { flex: 1; }
.who { color: var(--muted); }

.disk { min-width: 200px; }
.disk .bar { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; margin-top: 4px; }
.disk .bar i { display: block; height: 100%; background: var(--accent); width: 0; }
.disk small { color: var(--muted); }

.adder {
  display: flex;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 8px;
  flex-wrap: wrap;
}
.adder input[type="text"] { flex: 1 1 280px; width: auto; }

/* ---------- Liste ---------- */
.list-head, .row-main {
  display: grid;
  grid-template-columns: 22px minmax(0, 1fr) 70px 82px 82px 58px 44px 92px 70px;
  gap: 10px;
  align-items: center;
}
.list-head {
  padding: 8px 13px 4px;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .04em;
}
.list-head span:nth-child(n+3) { text-align: right; }
ul.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }

.row {
  --p: 0;
  --fill: var(--fill-idle);
  background:
    linear-gradient(90deg, var(--fill) calc(var(--p) * 1%), transparent 0) no-repeat,
    var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.row:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.row[data-state="downloading"], .row[data-state="metadata"] { --fill: var(--fill-down); }
.row[data-state="seeding"] { --fill: var(--fill-seed); }
.row[data-state="checking"] { --fill: var(--fill-check); }
.row[data-state="stopped"], .row[data-state="finished"] { --fill: var(--fill-stop); }
.row[data-state="error"] { border-color: var(--danger); }

.row-main { padding: 6px 13px; }
.name-btn {
  appearance: none;
  background: none;
  border: 0;
  padding: 0;
  text-align: left;
  cursor: pointer;
  min-width: 0;
  display: block;
}
.name-btn .name { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.name-btn .sub { display: flex; gap: 10px; color: var(--muted); font-size: 11px; }
.row-main .num, .row-main .pct { text-align: right; }
.row-main .pct { font-weight: 600; }
.row .actions { display: flex; gap: 6px; justify-content: flex-end; }

.detail { padding: 4px 13px 12px; border-top: 1px solid var(--line); background: var(--panel); }
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 6px 16px; margin: 8px 0; }
.facts dt { color: var(--muted); font-size: 11px; }
.facts dd { margin: 0; font-variant-numeric: tabular-nums; }
.detail .err { color: var(--danger); margin: 8px 0; }
.files { list-style: none; margin: 8px 0 0; padding: 0; max-height: 280px; overflow: auto; border: 1px solid var(--line); border-radius: 6px; }
.files li { display: grid; grid-template-columns: minmax(0, 1fr) 90px 64px 90px; gap: 10px; padding: 3px 10px; border-bottom: 1px solid var(--line); align-items: center; }
.files li:last-child { border-bottom: 0; }
.files .fname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.files .num { text-align: right; color: var(--muted); }
.files a { color: var(--accent); }

.empty { text-align: center; color: var(--muted); padding: 40px 20px; border: 1px dashed var(--line); border-radius: var(--radius); }
.empty strong { display: block; color: var(--ink); font-size: 17px; margin-bottom: 4px; }

/* ---------- Dialogues ---------- */
dialog {
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 20px;
  width: min(560px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(5, 10, 15, .55); }
dialog h2 { margin: 0 0 12px; font-size: 16px; }
dialog h3 { margin: 18px 0 6px; font-size: 13px; }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }
.check { display: flex; gap: 8px; align-items: center; margin-top: 10px; }
.check.err { color: var(--danger); align-items: flex-start; }
.check[hidden] { display: none; }
table.users { width: 100%; border-collapse: collapse; }
table.users td, table.users th { text-align: left; padding: 6px 4px; border-bottom: 1px solid var(--line); }
table.users td:last-child { text-align: right; }
.inline { display: grid; grid-template-columns: 1fr 1fr 110px auto; gap: 8px; align-items: end; }
.three { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }

/* ---------- Notifications ---------- */
#toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 50; width: min(440px, calc(100vw - 24px)); }
.toast { background: var(--ink); color: var(--bg); padding: 10px 14px; border-radius: 8px; box-shadow: 0 6px 24px rgba(0,0,0,.25); }
.toast.bad { background: var(--danger); color: #fff; }
.dropzone { position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; background: color-mix(in srgb, var(--bg) 82%, transparent); border: 4px dashed var(--accent); font-size: 22px; font-weight: 600; pointer-events: none; }

/* ---------- Petits écrans ---------- */
@media (max-width: 760px) {
  .list-head { display: none; }
  .row-main { grid-template-columns: auto minmax(0, 1fr) auto auto auto auto; row-gap: 6px; }
  .row-main .name-cell { grid-column: 2 / -1; }
  .row-main .num, .row-main .pct { text-align: left; }
  .row-main .pct { order: 2; }
  .row-main .actions { grid-column: 2; grid-row: 2; }
  .row-main .num:last-child { grid-column: 1 / -1; }
  .inline, .three { grid-template-columns: 1fr; }
  .files li { grid-template-columns: minmax(0, 1fr) 60px; }
  .files li .size, .files li .fdl { display: none; }
}
@media (prefers-reduced-motion: no-preference) {
  .row { transition: border-color .2s; }
  .disk .bar i { transition: width .4s; }
}
.toolbar { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 10px; }
.name-cell { display: flex; align-items: center; gap: 8px; min-width: 0; }
.name-cell .name-btn { flex: 1; }
.dl-btn { flex: none; text-decoration: none; color: inherit; }
.dl-btn[hidden] { display: none; }
.sel-count { color: var(--ink); font-size: 12px; font-weight: 600; margin-right: 6px; min-width: 150px; }
.del-list { max-height: 160px; overflow: auto; padding-left: 18px; }
.btn:disabled { opacity: .5; cursor: default; }

dialog.fm { width: min(1240px, calc(100vw - 24px)); height: min(920px, calc(100vh - 24px)); max-height: none; }
dialog.fm[open] { display: flex; flex-direction: column; }
.fm-body { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.fm-scroll { flex: 1; min-height: 0; overflow: auto; border: 1px solid var(--line); border-radius: 8px; margin-top: 8px; }
.fm-scroll .fm-table thead th { position: sticky; top: 0; background: var(--panel); z-index: 1; box-shadow: 0 1px 0 var(--line); }
.fm-files td { background: var(--bg); padding: 4px 8px 8px; }
.fm-files ul { list-style: none; margin: 0; padding: 0; }
.fm-files li { display: grid; grid-template-columns: minmax(0, 1fr) 90px auto; gap: 10px; align-items: center; padding: 2px 0; border-bottom: 1px solid var(--line); }
.fm-files li:last-child { border-bottom: 0; }
.fm-files .fname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fm-files a.btn { text-decoration: none; color: inherit; }
.crumbs { margin-bottom: 8px; font-size: 12px; }
.link { appearance: none; background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; font: inherit; text-align: left; }
.link:hover { text-decoration: underline; }
.fm-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.fm-table td { padding: 4px 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.fm-table .fm-name { max-width: 0; width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fm-table .num { text-align: right; white-space: nowrap; color: var(--muted); }
.fm-act { display: flex; gap: 6px; justify-content: flex-end; }
.fm-act a.btn { text-decoration: none; color: inherit; }
.fm-table th { text-align: left; font-weight: 500; color: var(--muted); padding: 6px 8px; border-bottom: 1px solid var(--line); }
.fm-table th.num { text-align: right; }
.fm-table td:first-child, .fm-table th:first-child { width: 28px; }
.fm-table td.fm-name:first-child, .fm-table th:first-child:not(:has(input)) { width: 100%; }
.fm-users td:first-child { width: auto; }
.fm-tabs { display: flex; gap: 6px; margin-bottom: 12px; }
.fm-tabs .btn.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.fm-chk { font-size: 13px; color: var(--muted); display: inline-flex; align-items: center; gap: 4px; }
.fm-table .hint { margin: 2px 0 0; white-space: normal; }

/* ---------- Totaux ---------- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 10px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 7px 12px; display: flex; flex-direction: column; }
.stat-label { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .04em; }
.stat b { font-size: 17px; font-weight: 650; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.stat:nth-child(1) b { color: var(--accent); }
@media (max-width: 760px) { .stats { grid-template-columns: repeat(2, 1fr); } }

/* ---------- Tri des colonnes ---------- */
.sort-btn { appearance: none; background: none; border: 0; padding: 0; font: inherit; color: inherit; text-transform: inherit; letter-spacing: inherit; cursor: pointer; display: inline-flex; gap: 3px; align-items: center; }
.sort-btn:hover, .sort-btn.on { color: var(--ink); }
.sort-btn .arrow { font-style: normal; font-size: 9px; min-width: 9px; }

.trackers { display: flex; flex-wrap: wrap; gap: 4px 6px; align-items: center; margin: 4px 0 8px; font-size: 11px; }
.trackers code { background: var(--bg); border: 1px solid var(--line); border-radius: 5px; padding: 0 6px; }

.tracker-sel { width: auto; max-width: 260px; padding: 3px 8px; font-size: 12px; }

/* ---------- Menu utilisateur ---------- */
.menu { position: relative; }
.menu-trigger { appearance: none; display: flex; align-items: center; gap: 7px; background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 3px 10px 3px 3px; cursor: pointer; box-shadow: var(--shadow); }
.menu-trigger:hover, .menu-trigger[aria-expanded="true"] { border-color: var(--muted); }
.avatar { width: 24px; height: 24px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-weight: 700; font-size: 12px; }
.menu-name { font-weight: 600; max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.caret { color: var(--muted); font-size: 10px; }
.menu-pop { position: absolute; right: 0; top: calc(100% + 6px); min-width: 210px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 8px 28px rgba(16, 24, 40, .16); padding: 5px; z-index: 30; }
.menu-head { display: flex; flex-direction: column; padding: 6px 10px 8px; }
.menu-head small { color: var(--muted); }
.menu-pop hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }
.menu-item { appearance: none; display: block; width: 100%; text-align: left; background: none; border: 0; border-radius: 6px; padding: 7px 10px; cursor: pointer; }
.menu-item:hover, .menu-item:focus-visible { background: var(--bg); }
.menu-item.danger { color: var(--danger); }

/* ---------- Logo ---------- */
.brand { display: flex; align-items: center; gap: 9px; color: inherit; text-decoration: none; }
.brand img { display: block; border-radius: 7px; box-shadow: var(--shadow); }
header.top .brand h1 { font-size: 18px; font-weight: 700; }
.auth-logo { display: block; margin: 0 auto 14px; border-radius: 14px; box-shadow: var(--shadow); }
.auth h1 { text-align: center; }
.auth p.lead { text-align: center; }

.imdb { display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: center; margin: 4px 0; font-size: 12px; }
.imdb a { color: var(--accent); font-weight: 600; }
.imdb-rating { font-weight: 700; background: #f5e3a3; color: #5a4300; border-radius: 5px; padding: 0 6px; font-variant-numeric: tabular-nums; }

.import-map { display: flex; flex-wrap: wrap; gap: 6px 16px; margin: 4px 0 6px; }
.import-map-row { display: flex; align-items: center; gap: 6px; }
.import-map-row select { width: auto; padding: 3px 8px; font-size: 12px; }

.v-ok { color: #2e7d32; font-weight: 600; }
.v-warn { color: var(--warn); font-weight: 600; }
.v-bad { color: var(--danger); font-weight: 600; }

/* ---------- Attente (démarrage du serveur) ---------- */
.spinner { display: inline-block; width: 12px; height: 12px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; vertical-align: -2px; }
@media (prefers-reduced-motion: no-preference) { .spinner { animation: spin .8s linear infinite; } }
@keyframes spin { to { transform: rotate(360deg); } }
.banner { position: fixed; top: 12px; left: 50%; transform: translateX(-50%); z-index: 60; display: flex; align-items: center; gap: 10px; background: var(--fill-check); color: var(--ink); border: 1px solid var(--line); border-radius: 999px; padding: 7px 16px; box-shadow: 0 6px 24px rgba(16, 24, 40, .18); max-width: calc(100vw - 24px); }
.loading { text-align: center; color: var(--muted); }
.info { margin: 8px 0 0; padding: 6px 12px; background: var(--fill-check); border: 1px solid var(--line); border-radius: 8px; font-size: 12px; }

.diag { white-space: pre-wrap; word-break: break-all; font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 10px; margin: 0; overflow: auto; max-height: 60vh; }
