/* ---- theme tokens: dark (default) ---- */
:root {
  color-scheme: dark;
  --bg: #0f1216; --panel: #171b21; --panel2: #1e232b; --line: #2a313b; --line-hover: #3a4452;
  --text: #e6e9ee; --muted: #8b95a3; --accent: #3b82f6; --accent2: #2563eb;
  --ok: #22c55e; --warn: #f59e0b; --err: #ef4444; --radius: 8px;
  --sel-bg: #1d2f4d; --hover: #24303f; --stage: #08090b; --code-bg: #0b0e11; --overlay: rgba(0,0,0,.6);
  --danger-bg: #3a1517; --danger-line: #6b2226; --danger-text: #fca5a5;
  --b-lab: #93c5fd; --b-lab-line: #1e3a8a; --b-ok: #86efac; --b-ok-line: #14532d; --b-err-line: #7f1d1d;
}
/* ---- light ---- */
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f6f9; --panel: #ffffff; --panel2: #f0f2f5; --line: #d8dde4; --line-hover: #b8c0cc;
  --text: #1a1f27; --muted: #5f6b7a; --accent: #2563eb; --accent2: #1d4ed8;
  --ok: #16a34a; --warn: #d97706; --err: #dc2626;
  --sel-bg: #dbe7fd; --hover: #e6edf7; --stage: #dfe3e8; --code-bg: #f6f8fa; --overlay: rgba(15,18,22,.45);
  --danger-bg: #fdecec; --danger-line: #f3b4b4; --danger-text: #b91c1c;
  --b-lab: #1d4ed8; --b-lab-line: #93c5fd; --b-ok: #15803d; --b-ok-line: #86efac; --b-err-line: #fca5a5;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]):not([data-theme="light"]) {
    color-scheme: light;
    --bg: #f4f6f9; --panel: #ffffff; --panel2: #f0f2f5; --line: #d8dde4; --line-hover: #b8c0cc;
    --text: #1a1f27; --muted: #5f6b7a; --accent: #2563eb; --accent2: #1d4ed8;
    --ok: #16a34a; --warn: #d97706; --err: #dc2626;
    --sel-bg: #dbe7fd; --hover: #e6edf7; --stage: #dfe3e8; --code-bg: #f6f8fa; --overlay: rgba(15,18,22,.45);
    --danger-bg: #fdecec; --danger-line: #f3b4b4; --danger-text: #b91c1c;
    --b-lab: #1d4ed8; --b-lab-line: #93c5fd; --b-ok: #15803d; --b-ok-line: #86efac; --b-err-line: #fca5a5;
  }
}
.theme-toggle { display: flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.theme-toggle button { border: none; border-radius: 0; background: transparent; color: var(--muted); padding: 4px 9px; font-size: 12px; }
.theme-toggle button.active { background: var(--panel2); color: var(--text); }
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 14px/1.45 "Segoe UI", system-ui, -apple-system, sans-serif; }
button, input, select, textarea { font: inherit; color: var(--text); background: var(--panel2);
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 10px; }
button { cursor: pointer; }
button:hover { border-color: var(--line-hover); }
button.primary { background: var(--accent2); border-color: var(--accent2); color: #fff; }
button.primary:hover { background: var(--accent); }
button.danger { background: var(--danger-bg); border-color: var(--danger-line); color: var(--danger-text); }
button:disabled { opacity: .5; cursor: default; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
textarea { resize: vertical; width: 100%; }
h2 { margin: 0 0 14px; font-size: 20px; font-weight: 600; }
h3 { margin: 0 0 14px; }
h4 { margin: 14px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.muted { color: var(--muted); } .small { font-size: 12px; }
.hidden { display: none !important; }
.inline { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }
.row-between { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.topbar { display: flex; align-items: center; gap: 20px; height: 52px; padding: 0 16px;
  background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.brand { font-weight: 700; letter-spacing: .02em; } .brand span { color: var(--accent); font-weight: 500; }
#nav { display: flex; gap: 4px; }
#nav button { background: transparent; border-color: transparent; color: var(--muted); }
#nav button.active { color: var(--text); background: var(--panel2); border-color: var(--line); }
.proj-pick { margin-left: auto; display: flex; gap: 8px; align-items: center; color: var(--muted); }
.proj-pick select { min-width: 200px; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.dot.ok { background: var(--ok); } .dot.bad { background: var(--err); }

main { padding: 20px; }
.view { display: none; } .view.active { display: block; }
#view-review.active { margin: -20px; }
#view-review #reviewPicker { padding: 20px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 14px; }
.form-grid { display: grid; grid-template-columns: 200px minmax(0, 560px); gap: 10px 14px; align-items: center; }
.form-grid > label { color: var(--muted); }
.form-grid input:not([type=checkbox]), .form-grid select { width: 100%; }
.form-grid .inline input { flex: 1; width: auto; }
.checks { display: flex; gap: 18px; }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 14px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.card.current { border-color: var(--accent); }
.card h3 { margin: 0 0 4px; font-size: 16px; }
.card .path { font-family: Consolas, monospace; font-size: 12px; color: var(--muted); word-break: break-all; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; margin: 8px 0; }
.chip { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--panel2); border: 1px solid var(--line); }
.card .actions { display: flex; gap: 6px; margin-top: 10px; flex-wrap: wrap; }

.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin-bottom: 14px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; }
.stat .v { font-size: 22px; font-weight: 600; } .stat .k { color: var(--muted); font-size: 12px; }
.progress { height: 8px; background: var(--panel2); border-radius: 4px; overflow: hidden; }
.progress.small { height: 5px; }
.progress > div { height: 100%; width: 0; background: var(--accent); transition: width .3s; }
#runText { margin-top: 6px; }

.test-grid { display: grid; grid-template-columns: 1.4fr 1fr; gap: 12px; margin-top: 8px; }
.test-canvas-wrap canvas { max-width: 100%; background: var(--stage); border-radius: 6px; }
pre { margin: 0; background: var(--code-bg); border: 1px solid var(--line); border-radius: 6px; padding: 10px;
  max-height: 480px; overflow: auto; white-space: pre-wrap; font-size: 12px; }

.gallery-bar { margin-top: 8px; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; cursor: pointer; }
.tile:hover { border-color: var(--accent); }
.tile .imgbox { position: relative; aspect-ratio: 4/3; background: var(--stage); }
.tile img, .tile svg { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.tile .meta { padding: 6px 8px; font-size: 12px; display: flex; justify-content: space-between; gap: 6px; }
.tile .meta .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
.badge { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--panel2); border: 1px solid var(--line); white-space: nowrap; }
.badge.pending { color: var(--muted); } .badge.labeled { color: var(--b-lab); border-color: var(--b-lab-line); }
.badge.validated { color: var(--b-ok); border-color: var(--b-ok-line); } .badge.error { color: var(--danger-text); border-color: var(--b-err-line); }

.table { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.table th, .table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.table th { color: var(--muted); font-weight: 500; font-size: 12px; text-transform: uppercase; }
.table td .progress { width: 140px; }
.table select, .table input { padding: 3px 6px; }

/* editor */
.editor { display: grid; grid-template-columns: 240px 1fr 280px; height: calc(100vh - 52px); }
.ed-left, .ed-right { background: var(--panel); border-color: var(--line); overflow: auto; padding: 10px; }
.ed-left { border-right: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; }
.ed-right { border-left: 1px solid var(--line); }
.ed-head { display: flex; gap: 8px; align-items: center; }
.ed-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ed-list { overflow: auto; flex: 1; display: flex; flex-direction: column; gap: 2px; }
.ed-item { display: flex; gap: 6px; align-items: center; padding: 4px 6px; border-radius: 5px; cursor: pointer; font-size: 12px; }
.ed-item:hover { background: var(--panel2); }
.ed-item.cur { background: var(--sel-bg); }
.ed-item .n { color: var(--muted); width: 30px; flex-shrink: 0; }
.ed-item .f { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sdot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; background: var(--muted); }
.sdot.labeled { background: var(--accent); } .sdot.validated { background: var(--ok); } .sdot.error { background: var(--err); }
.ed-center { display: flex; flex-direction: column; min-width: 0; }
.ed-toolbar, .ed-bottom { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 10px;
  background: var(--panel); border-bottom: 1px solid var(--line); }
.ed-bottom { border-top: 1px solid var(--line); border-bottom: none; justify-content: center; }
.canvas-wrap { flex: 1; position: relative; background: var(--stage); overflow: hidden; }
.canvas-wrap canvas { position: absolute; inset: 0; width: 100%; height: 100%; cursor: crosshair; }
.class-list { display: flex; flex-direction: column; gap: 3px; }
.class-btn { display: flex; align-items: center; gap: 8px; text-align: left; padding: 5px 8px; }
.class-btn.active { border-color: var(--accent); background: var(--sel-bg); }
.class-btn .sw { width: 12px; height: 12px; border-radius: 3px; }
.class-btn .key { margin-left: auto; color: var(--muted); font-size: 11px; }
.box-list { display: flex; flex-direction: column; gap: 2px; max-height: 260px; overflow: auto; }
.box-item { display: flex; gap: 6px; align-items: center; padding: 4px 6px; border-radius: 5px; cursor: pointer; font-size: 12px; }
.box-item:hover { background: var(--panel2); } .box-item.sel { background: var(--sel-bg); }
.box-item .sw { width: 10px; height: 10px; border-radius: 2px; flex-shrink: 0; }
.box-item .src { margin-left: auto; color: var(--muted); font-size: 11px; }
.sel-panel { margin-top: 10px; padding-top: 4px; border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 6px; }
.sel-panel label { color: var(--muted); font-size: 12px; }
.help { margin-top: 16px; color: var(--muted); font-size: 12px; }
.help ul { padding-left: 16px; margin: 6px 0; }

.modal { position: fixed; inset: 0; background: var(--overlay); display: flex; align-items: center; justify-content: center; z-index: 20; }
.modal-box { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 20px; width: min(820px, 94vw); max-height: 90vh; overflow: auto; }
.modal-box .form-grid { grid-template-columns: 160px 1fr; }
.modal-actions { display: flex; justify-content: flex-end; align-items: center; gap: 8px; margin-top: 16px; }
.modal-actions .muted { margin-right: auto; }
.fs-list { margin-top: 8px; height: 50vh; overflow: auto; border: 1px solid var(--line); border-radius: 6px; background: var(--panel2); }
.fs-item { padding: 6px 10px; cursor: pointer; border-bottom: 1px solid var(--line); }
.fs-item:hover { background: var(--hover); }
#fsInfo { margin-top: 6px; }

.toast { position: fixed; bottom: 18px; right: 18px; background: var(--panel2); border: 1px solid var(--line);
  border-radius: 8px; padding: 10px 14px; z-index: 30; max-width: 480px; }
.toast.err { border-color: var(--err); color: var(--danger-text); }
body.not-admin .admin-only { display: none !important; }
/* ---- reference images (manual few-shot examples) ---- */
.ref-panel { border-left: 3px solid var(--warn); }
.ref-panel.ready { border-left-color: var(--ok); }
.ref-strip { display: flex; gap: 8px; overflow-x: auto; margin-top: 10px; padding-bottom: 4px; }
.ref-strip .ref { position: relative; flex: 0 0 140px; cursor: pointer; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; background: var(--stage); }
.ref-strip .ref:hover { border-color: var(--accent); }
.ref-strip .ref img { display: block; width: 100%; aspect-ratio: 4/3; object-fit: contain; }
.ref-strip .ref span { position: absolute; left: 4px; bottom: 4px; font-size: 11px; background: var(--overlay); color: #fff; padding: 0 5px; border-radius: 4px; }
.ref-steps { margin: 6px 0 0; padding-left: 18px; color: var(--muted); font-size: 13px; }
.star { color: var(--warn); }
button.ref-on { border-color: var(--warn); color: var(--warn); }
button:disabled { opacity: .5; cursor: not-allowed; }
.ed-counts { font-size: 12px; color: var(--muted); display: flex; gap: 10px; flex-wrap: wrap; }
.ed-counts b { color: var(--text); }
.ed-list:focus { outline: 1px solid var(--accent); outline-offset: 1px; border-radius: 5px; }
.ed-item .mk { width: 28px; flex-shrink: 0; text-align: right; font-size: 11px; }
.mk .hand { color: var(--ok); } .mk .sugg { color: var(--muted); }
/* ---- clear labels / backups ---- */
.clear-opts { display: flex; flex-direction: column; gap: 8px; }
.clear-opts .opt { display: flex; gap: 10px; align-items: flex-start; padding: 10px; border: 1px solid var(--line); border-radius: var(--radius); cursor: pointer; }
.clear-opts .opt:has(input:checked) { border-color: var(--accent); background: var(--sel-bg); }
.clear-opts input { margin-top: 4px; }
.backup-list { max-height: 220px; overflow: auto; display: flex; flex-direction: column; gap: 4px; }
.backup-list .bk { display: flex; gap: 10px; align-items: center; justify-content: space-between; padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; font-size: 12px; }
.backup-list .bk .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
/* ---- "labeling finished" banner (stays until dismissed) ---- */
.done-banner { position: fixed; top: 64px; left: 50%; transform: translateX(-50%); z-index: 60; width: min(760px, calc(100vw - 32px));
  display: flex; align-items: center; gap: 12px; padding: 16px 18px; border-radius: 10px; border: 2px solid var(--ok);
  background: var(--panel); box-shadow: 0 12px 40px rgba(0,0,0,.35); animation: db-in .35s ease-out, db-pulse 1.2s ease-in-out 3; }
.done-banner.warn { border-color: var(--warn); }
.done-banner.hidden { display: none; }
.done-banner .db-text { flex: 1; min-width: 0; }
.done-banner .db-head { font-size: 20px; font-weight: 700; color: var(--ok); }
.done-banner.warn .db-head { color: var(--warn); }
.done-banner .db-detail { color: var(--muted); margin-top: 2px; }
@keyframes db-in { from { opacity: 0; transform: translate(-50%, -16px); } to { opacity: 1; transform: translate(-50%, 0); } }
@keyframes db-pulse { 50% { box-shadow: 0 0 0 8px color-mix(in srgb, var(--ok) 30%, transparent), 0 12px 40px rgba(0,0,0,.35); } }
@media (max-width: 600px) { .done-banner { flex-wrap: wrap; top: 8px; } }
/* one-click filter chips above the editor list */
.ed-counts { align-items: center; gap: 6px; }
.fchip { font-size: 12px; padding: 3px 9px; border-radius: 999px; background: var(--panel2); color: var(--muted); }
.fchip b { color: var(--text); }
.fchip:hover { border-color: var(--line-hover); color: var(--text); }
.fchip.on { background: var(--sel-bg); border-color: var(--accent); color: var(--text); }
#edBack { white-space: nowrap; }
