:root {
  color-scheme: dark;
  --bg: #0e0d13;
  --panel: #17151f;
  --panel-2: #1f1c2a;
  --line: #2c2839;
  --text: #ece8f4;
  --muted: #9a93ad;
  --accent: #c9a6ff;
  --accent-2: #8f6bff;
  --ok: #5fd39a;
  --warn: #f2c66d;
  --bad: #ff7a8a;
  --radius: 14px;
  --rank-E: #8d8f99;
  --rank-D: #3fc7b0;
  --rank-C: #7fb2ff;
  --rank-B: #b07bff;
  --rank-A: #f0c24b;
  --rank-S: linear-gradient(120deg, #3ee6ff, #9a6bff 50%, #ff6fc8);
  --rank-SR: linear-gradient(120deg, #fff6d8, #ffd1e8 40%, #b9c8ff 70%, #fff3c4);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { min-height: 100vh; }
code { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 0.92em; color: var(--accent); }
.muted { color: var(--muted); }
.error { color: var(--bad); }
[hidden] { display: none !important; }

/* ------------------------------------------------------------ header */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 14px 24px;
  background: rgba(14, 13, 19, 0.86); backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  display: grid; place-items: center; width: 40px; height: 56px; border-radius: 7px;
  background: var(--panel-2); border: 2px solid var(--accent); font-size: 22px; font-family: serif;
}
h1 { margin: 0; font-size: 18px; letter-spacing: 0.02em; }
h2 { margin: 0 0 12px; font-size: 18px; }
.sub { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.top-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ------------------------------------------------------------ buttons */
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  padding: 8px 14px; border-radius: 10px; font: inherit; font-size: 13px; cursor: pointer;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn:hover { border-color: var(--accent-2); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.ghost { background: transparent; }
.btn.primary { background: linear-gradient(135deg, var(--accent-2), #6a4bd8); border-color: transparent; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.1); }
.btn.danger { color: var(--bad); }
.btn.small { padding: 5px 10px; font-size: 12px; border-radius: 8px; }
.row { display: flex; gap: 8px; align-items: center; }
.row.end { justify-content: flex-end; margin-top: 16px; }

/* ------------------------------------------------------------ toolbar */
.toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 12px;
  padding: 16px 24px 4px;
}
#search, select, dialog input {
  background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 10px;
  padding: 9px 12px; font: inherit; font-size: 14px;
}
#search { flex: 1 1 260px; max-width: 420px; }
#search:focus, select:focus, dialog input:focus { outline: none; border-color: var(--accent-2); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line); background: var(--panel); color: var(--muted);
  border-radius: 999px; padding: 6px 12px; font: inherit; font-size: 12px; font-weight: 700; cursor: pointer;
}
.chip[aria-pressed="true"] { color: var(--text); border-color: var(--accent); background: var(--panel-2); }
.count { color: var(--muted); font-size: 13px; margin-left: auto; }

/* ------------------------------------------------------------ grid */
.grid {
  display: grid; gap: 16px; padding: 16px 24px 48px;
  grid-template-columns: repeat(auto-fill, minmax(330px, 1fr));
}
.empty { text-align: center; color: var(--muted); padding: 48px 16px; }
.tile {
  position: relative; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px; display: flex; flex-direction: column; gap: 10px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.tile.dragover { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(201, 166, 255, 0.25); }
.tile.busy { opacity: 0.75; }
.tile.flash { animation: flash 1.6s ease-out; }
@keyframes flash { 0% { box-shadow: 0 0 0 3px var(--ok); } 100% { box-shadow: 0 0 0 0 transparent; } }
.tile-head { display: flex; align-items: flex-start; gap: 10px; }
.tile-head .titles { min-width: 0; flex: 1; }
.serial { font-family: ui-monospace, Menlo, monospace; font-size: 12px; color: var(--muted); }
.name { font-weight: 650; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meta { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rank {
  flex: none; min-width: 34px; height: 26px; padding: 0 8px; border-radius: 8px;
  display: grid; place-items: center; font-weight: 800; font-size: 13px; color: #111;
  background: var(--rank-E);
}
.rank[data-rank="D"] { background: var(--rank-D); }
.rank[data-rank="C"] { background: var(--rank-C); }
.rank[data-rank="B"] { background: var(--rank-B); }
.rank[data-rank="A"] { background: var(--rank-A); }
.rank[data-rank="S"] { background: var(--rank-S); }
.rank[data-rank="SR"] { background: var(--rank-SR); }
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pair figure, .compare figure { margin: 0; }
figcaption { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; margin-bottom: 4px; }
.slot {
  aspect-ratio: 5 / 7; border-radius: 10px; overflow: hidden; background: #0a090e;
  border: 1px solid var(--line); display: grid; place-items: center; position: relative;
}
.slot img { width: 100%; height: 100%; object-fit: contain; display: block; }
.slot .none { font-size: 12px; color: var(--muted); padding: 8px; text-align: center; }
.slot.new { border-color: var(--accent-2); }
.badges { display: flex; gap: 6px; flex-wrap: wrap; }
.badge { font-size: 11px; border-radius: 6px; padding: 2px 7px; background: var(--panel-2); color: var(--muted); border: 1px solid var(--line); }
.badge.manual { color: var(--accent); border-color: rgba(201, 166, 255, 0.4); }
.badge.pending { color: var(--warn); }
.badge.failed { color: var(--bad); }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.drop-hint {
  position: absolute; inset: 0; border-radius: var(--radius); display: none; place-items: center;
  background: rgba(23, 21, 31, 0.88); font-weight: 600; color: var(--accent); pointer-events: none;
}
.tile.dragover .drop-hint { display: grid; }

/* ------------------------------------------------------------ dialogs */
dialog {
  background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 18px;
  padding: 0; width: min(460px, calc(100vw - 32px)); max-height: calc(100vh - 48px);
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
}
dialog.wide { width: min(980px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(5, 4, 9, 0.7); backdrop-filter: blur(3px); }
dialog form, .dialog-body { padding: 22px; }
dialog label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); margin-top: 12px; }
dialog input { width: 100%; }
.compare { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.compare .slot { max-height: 52vh; }
.check { min-height: 1.4em; margin: 14px 0 0; font-size: 14px; }
.check.ok { color: var(--ok); }
.check.bad { color: var(--bad); }
.check.warn { color: var(--warn); }
.progress { height: 6px; background: var(--panel-2); border-radius: 99px; overflow: hidden; margin-top: 12px; }
.progress > div { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent-2), var(--accent)); transition: width 0.15s; }

.dropzone {
  display: flex !important; flex-direction: column; align-items: center; gap: 6px; margin: 0 !important;
  border: 2px dashed var(--line); border-radius: 14px; padding: 26px; cursor: pointer; color: var(--text) !important;
  transition: border-color 0.15s, background 0.15s;
}
.dropzone:hover, .dropzone.dragover { border-color: var(--accent); background: rgba(201, 166, 255, 0.05); }
.dropzone small { color: var(--muted); }
.bulk-list { margin-top: 14px; display: flex; flex-direction: column; gap: 6px; max-height: 52vh; overflow: auto; }
.bulk-row {
  display: grid; grid-template-columns: 22px 54px 54px 1fr auto; gap: 10px; align-items: center;
  padding: 6px 8px; border-radius: 10px; background: var(--panel-2); border: 1px solid var(--line);
}
.bulk-row .slot { width: 54px; border-radius: 6px; }
.bulk-row .info { min-width: 0; font-size: 13px; }
.bulk-row .info .meta { font-size: 12px; }
.bulk-row .state { font-size: 12px; white-space: nowrap; }
.bulk-row .state.ok { color: var(--ok); }
.bulk-row .state.bad { color: var(--bad); }
.bulk-row .state.warn { color: var(--warn); }
.bulk-row .mini { height: 4px; background: var(--line); border-radius: 9px; overflow: hidden; margin-top: 4px; }
.bulk-row .mini > div { height: 100%; width: 0; background: var(--accent); }
.bulk-skip { font-size: 12px; color: var(--muted); padding: 4px 8px; }

/* ------------------------------------------------------------ drop overlay, toasts */
.drop-overlay {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 20; pointer-events: none;
  padding: 12px 20px; border-radius: 12px; background: rgba(23, 21, 31, 0.95); border: 2px dashed var(--accent);
  font-size: 14px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}
.toasts { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 30; }
.toast {
  background: var(--panel-2); border: 1px solid var(--line); border-left: 4px solid var(--accent);
  padding: 10px 14px; border-radius: 10px; font-size: 13px; max-width: 380px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}
.toast.ok { border-left-color: var(--ok); }
.toast.bad { border-left-color: var(--bad); }
.toast.warn { border-left-color: var(--warn); }

@media (max-width: 640px) {
  .top, .toolbar, .grid { padding-left: 16px; padding-right: 16px; }
  .grid { grid-template-columns: 1fr; }
  .compare { grid-template-columns: 1fr 1fr; }
  .compare figure:first-child { display: none; }
  .count { margin-left: 0; }
}
