:root {
  --bg: #0f1115;
  --panel: #161921;
  --panel-2: #1d212b;
  --line: #2a2f3b;
  --text: #e7e9ee;
  --muted: #8d93a3;
  --accent: #4f8cff;
  --accent-2: #3a6fd6;
  --good: #3ecf8e;
  --danger: #ff5d5d;
  --radius: 10px;
  --k-image: #4f8cff;
  --k-model: #c27cff;
  --k-audio: #3ecf8e;
  --k-video: #ff9f43;
  --k-text: #9aa3b5;
  --k-other: #6b7385;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.btn {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 7px 14px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel-2); cursor: pointer;
  white-space: nowrap; text-decoration: none;
}
.btn:hover { border-color: #3a4152; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover:not(:disabled) { background: var(--accent-2); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.link { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 0; }
.pill {
  min-width: 22px; padding: 0 7px; border-radius: 999px; background: var(--accent);
  color: #fff; font-size: 12px; font-weight: 700; text-align: center;
}
input[type="search"], input[type="text"], input[type="password"], select {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; outline: none;
}
input:focus, select:focus { border-color: var(--accent); }

/* ---- Login ---- */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login {
  width: min(380px, 100%); background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 28px;
}
.login h1 { margin: 0 0 4px; font-size: 22px; }
.login p { margin: 0 0 20px; color: var(--muted); }
.login label { display: block; margin: 12px 0 6px; color: var(--muted); font-size: 13px; }
.login input { width: 100%; }
.login .btn { width: 100%; justify-content: center; margin-top: 20px; padding: 10px; }
.error { color: var(--danger); min-height: 20px; margin-top: 12px; font-size: 13px; }

/* ---- App shell ---- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 16px; background: rgba(15,17,21,.92); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; white-space: nowrap; }
.logo {
  width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--k-model)); color: #fff; font-size: 14px;
}
.search { flex: 1; max-width: 640px; }
.search input { width: 100%; }
.top-actions { display: flex; align-items: center; gap: 10px; margin-left: auto; }
.user { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.icon-btn.menu-btn { display: none; }

.layout { display: grid; grid-template-columns: 240px 1fr; min-height: calc(100% - 57px); }
.sidebar {
  border-right: 1px solid var(--line); padding: 16px 12px; position: sticky; top: 57px;
  height: calc(100vh - 57px); overflow: auto;
}
.sidebar h3 { margin: 16px 8px 8px; font-size: 11px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.sidebar h3:first-child { margin-top: 0; }
.nav-item {
  display: flex; align-items: center; gap: 8px; width: 100%; text-align: left;
  padding: 6px 8px; border-radius: 7px; border: 0; background: none; cursor: pointer; color: var(--text);
}
.nav-item:hover { background: var(--panel-2); }
.nav-item.active { background: rgba(79,140,255,.15); color: #fff; }
.nav-item .count { margin-left: auto; color: var(--muted); font-size: 12px; }
.nav-item .dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.nav-item .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-foot { margin: 20px 8px 0; color: var(--muted); font-size: 12px; }

main { padding: 16px; min-width: 0; }
.toolbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar-right { display: flex; gap: 8px; margin-left: auto; flex-wrap: wrap; }
#resultInfo { color: var(--muted); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; }
.card {
  position: relative; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; transition: border-color .12s, transform .12s;
}
.card:hover { border-color: #3a4152; }
.card.selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.card .thumb {
  display: block; width: 100%; aspect-ratio: 1; border: 0; padding: 0; cursor: zoom-in;
  background: var(--panel-2); position: relative; overflow: hidden;
}
.card .thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.checker {
  background-color: #2a2f3b;
  background-image:
    linear-gradient(45deg, #232733 25%, transparent 25%), linear-gradient(-45deg, #232733 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #232733 75%), linear-gradient(-45deg, transparent 75%, #232733 75%);
  background-size: 16px 16px; background-position: 0 0, 0 8px, 8px -8px, -8px 0;
}
.tile {
  width: 100%; height: 100%; display: grid; place-items: center; align-content: center; gap: 6px;
  color: #fff; font-weight: 800; letter-spacing: .04em;
}
.tile .ext { font-size: 22px; }
.tile .k { font-size: 11px; font-weight: 600; opacity: .8; text-transform: uppercase; letter-spacing: .1em; }
.k-image { background: linear-gradient(135deg, #24365c, #1b2235); }
.k-model { background: linear-gradient(135deg, #43295f, #1f1b2e); }
.k-audio { background: linear-gradient(135deg, #1d4a3a, #17231f); }
.k-video { background: linear-gradient(135deg, #5a3a1c, #261d15); }
.k-text  { background: linear-gradient(135deg, #343a48, #1d2028); }
.k-other { background: linear-gradient(135deg, #2c313d, #1b1e25); }
.card .meta { padding: 8px 10px 10px; }
.card .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .sub { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .check {
  position: absolute; top: 8px; left: 8px; z-index: 2; width: 24px; height: 24px; border-radius: 6px;
  background: rgba(0,0,0,.55); display: grid; place-items: center; opacity: 0; transition: opacity .12s; cursor: pointer;
}
.card:hover .check, .card.selected .check, body.selecting .card .check { opacity: 1; }
.card .check input { margin: 0; accent-color: var(--accent); width: 15px; height: 15px; cursor: pointer; }
.card .add {
  position: absolute; top: 8px; right: 8px; z-index: 2; height: 28px; padding: 0 10px; border-radius: 7px;
  border: 0; background: rgba(0,0,0,.6); color: #fff; cursor: pointer; font-weight: 600; font-size: 12px;
}
.card .add:hover { background: var(--accent); }
.card .add.in { background: var(--good); color: #062; }
.badge {
  position: absolute; left: 8px; bottom: 8px; z-index: 1; font-size: 10px; font-weight: 700; letter-spacing: .06em;
  padding: 2px 6px; border-radius: 4px; background: rgba(0,0,0,.65); color: #fff; text-transform: uppercase;
}
.empty { text-align: center; color: var(--muted); padding: 60px 16px; }
#sentinel { height: 40px; }

/* ---- Preview ---- */
dialog.preview {
  width: min(1100px, calc(100vw - 32px)); height: min(820px, calc(100vh - 32px)); padding: 0; border: 1px solid var(--line);
  border-radius: 14px; background: var(--panel); color: var(--text); overflow: hidden;
}
dialog.preview[open] { display: grid; grid-template-rows: auto 1fr auto; }
dialog::backdrop { background: rgba(5,6,9,.75); }
.pv-head, .pv-foot { display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.pv-foot { border-bottom: 0; border-top: 1px solid var(--line); }
.pv-title { min-width: 0; }
.pv-title .name { font-weight: 700; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pv-title .sub { color: var(--muted); font-size: 12px; }
.pv-head .spacer, .pv-foot .spacer { flex: 1; }
.icon-btn {
  width: 34px; height: 34px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel-2);
  cursor: pointer; display: grid; place-items: center; flex: none;
}
.pv-body { position: relative; min-height: 0; display: grid; place-items: center; overflow: auto; }
.pv-body img { max-width: 100%; max-height: 100%; object-fit: contain; display: block; cursor: zoom-in; }
.pv-body img.small { position: absolute; inset: 0; width: 100%; height: 100%; image-rendering: pixelated; }
.pv-body img.actual { position: static; max-width: none; max-height: none; width: auto; height: auto; cursor: zoom-out; }
.pv-body video { max-width: 100%; max-height: 100%; }
.pv-body audio { width: min(560px, 90%); }
.pv-body pre {
  margin: 0; padding: 16px; width: 100%; height: 100%; overflow: auto; font: 12.5px/1.5 ui-monospace, Consolas, monospace;
  white-space: pre-wrap; word-break: break-word; place-self: stretch;
}
.pv-body .model { position: absolute; inset: 0; }
.pv-body .model canvas { display: block; width: 100%; height: 100%; }
.pv-msg { color: var(--muted); text-align: center; padding: 24px; }
.pv-msg .tile { width: 140px; height: 140px; border-radius: 14px; margin: 0 auto 14px; }
.audio-wrap { display: grid; gap: 18px; justify-items: center; width: 100%; }
.audio-wrap .tile { width: 140px; height: 140px; border-radius: 14px; }
.hint { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); color: var(--muted); font-size: 12px; pointer-events: none; }

/* ---- Basket drawer ---- */
.scrim { position: fixed; inset: 0; background: rgba(5,6,9,.55); z-index: 30; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(440px, 100vw); z-index: 31;
  background: var(--panel); border-left: 1px solid var(--line);
  display: grid; grid-template-rows: auto 1fr auto; transform: translateX(100%); transition: transform .18s ease;
}
.drawer.open { transform: none; }
.drawer-head { display: flex; align-items: center; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.drawer-head h2 { margin: 0; font-size: 17px; }
.drawer-list { overflow: auto; padding: 8px; }
.b-item { display: grid; grid-template-columns: 44px 1fr auto; gap: 10px; align-items: center; padding: 6px 8px; border-radius: 8px; }
.b-item:hover { background: var(--panel-2); }
.b-item .mini { width: 44px; height: 44px; border-radius: 6px; overflow: hidden; }
.b-item .mini img { width: 100%; height: 100%; object-fit: cover; }
.b-item .mini .tile .ext { font-size: 11px; }
.b-item .mini .tile .k { display: none; }
.b-item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.b-item .sub { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.b-item button { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 18px; padding: 4px 8px; }
.b-item button:hover { color: var(--danger); }
.drawer-foot { border-top: 1px solid var(--line); padding: 14px 16px; display: grid; gap: 12px; }
.opt-group { display: grid; gap: 6px; }
.opt-group .lbl { font-size: 11px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.seg label {
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; cursor: pointer; background: var(--panel-2);
}
.seg label:has(input:checked) { border-color: var(--accent); background: rgba(79,140,255,.12); }
.seg input { display: none; }
.seg .t { font-weight: 600; display: block; }
.seg .d { color: var(--muted); font-size: 12px; }
.totals { display: flex; justify-content: space-between; color: var(--muted); }
.totals strong { color: var(--text); }
.drawer-foot .row { display: flex; gap: 8px; }
.drawer-foot .row .primary { flex: 1; justify-content: center; padding: 10px; }
.note { color: var(--muted); font-size: 12px; }
.note code { color: var(--text); }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 50;
  background: #222836; border: 1px solid var(--line); padding: 10px 16px; border-radius: 10px;
  box-shadow: 0 8px 30px rgba(0,0,0,.4); max-width: calc(100vw - 32px);
}
.toast.err { border-color: var(--danger); }

@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; width: min(280px, 85vw); height: 100vh; z-index: 32;
    background: var(--panel); transform: translateX(-100%); transition: transform .18s ease;
  }
  body.nav-open .sidebar { transform: none; }
  .icon-btn.menu-btn { display: grid; }
  .brand .name-text, .user span { display: none; }
  .topbar { gap: 10px; flex-wrap: wrap; }
  .search { order: 5; flex-basis: 100%; max-width: none; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
  .toolbar-right { margin-left: 0; width: 100%; }
}
