:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e7;
  --text: #1c2330;
  --muted: #667085;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --accent-soft: #e6eefc;
  --danger: #c2332d;
  --shadow: 0 12px 32px rgba(15, 23, 42, 0.18);
  --row-hover: #f3f6fb;
  --badge: #eef1f5;
  --badge-text: #3d4656;
  --cat-video: #dbeafe;
  --cat-adult: #fde2e2;
  --cat-audio: #dcfce7;
  --cat-soft: #ede9fe;
  --cat-book: #fef3c7;
  --cat-archive: #e5e7eb;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1218;
    --surface: #171b23;
    --surface-2: #1e2430;
    --border: #2b3240;
    --text: #e6e9ef;
    --muted: #9099a8;
    --accent: #5b8cff;
    --accent-text: #0b1020;
    --accent-soft: #1f2a44;
    --danger: #ff7b72;
    --shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
    --row-hover: #1c2230;
    --badge: #252c39;
    --badge-text: #c9d1dd;
    --cat-video: #1d3357;
    --cat-adult: #4a2327;
    --cat-audio: #1c3b2a;
    --cat-soft: #2f2755;
    --cat-book: #43381a;
    --cat-archive: #2c313b;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1218;
  --surface: #171b23;
  --surface-2: #1e2430;
  --border: #2b3240;
  --text: #e6e9ef;
  --muted: #9099a8;
  --accent: #5b8cff;
  --accent-text: #0b1020;
  --accent-soft: #1f2a44;
  --danger: #ff7b72;
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
  --row-hover: #1c2230;
  --badge: #252c39;
  --badge-text: #c9d1dd;
  --cat-video: #1d3357;
  --cat-adult: #4a2327;
  --cat-audio: #1c3b2a;
  --cat-soft: #2f2755;
  --cat-book: #43381a;
  --cat-archive: #2c313b;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB",
        "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
}

button, input, select {
  font: inherit;
  color: inherit;
}

button {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { background: var(--surface-2); }
button:disabled { opacity: 0.55; cursor: progress; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.primary:hover { filter: brightness(1.08); }
button.ghost { background: transparent; border-color: transparent; }
button.ghost:hover { background: var(--surface-2); }
button.small { padding: 2px 8px; font-size: 12px; }
button.icon { width: 32px; height: 32px; padding: 0; }
button:focus-visible, input:focus-visible, select:focus-visible, .row:focus-visible, .list:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

input, select {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 6px 10px;
  min-width: 0;
}

.muted { color: var(--muted); }
.error { color: var(--danger); }

/* ---- layout --------------------------------------------------------- */

body.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}
/* Bars keep their height; the list takes the rest (its content is the
   multi-million-pixel canvas, which must not become its flex basis). */
body.app > .topbar, body.app > .tabs, body.app > .toolbar, body.app > .summary { flex: none; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 17px; margin: 0; }
.topbar-actions { display: flex; align-items: center; gap: 6px; }

.tabs {
  display: flex;
  gap: 4px;
  padding: 8px 16px 0;
  background: var(--surface);
  overflow-x: auto;
  scrollbar-width: thin;
}
.tabs button {
  border: none;
  border-bottom: 2px solid transparent;
  border-radius: 6px 6px 0 0;
  background: transparent;
  padding: 8px 14px;
}
.tabs button[aria-selected="true"] {
  border-bottom-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}

.toolbar {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.filters, .syncbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}
.filters #q { flex: 2 1 220px; }
.filters #code { flex: 1 1 150px; }

.summary {
  display: flex;
  gap: 12px;
  padding: 6px 16px;
  font-size: 13px;
}

/* ---- virtual list ------------------------------------------------------- */

.list {
  position: relative;
  flex: 1 1 0;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--surface);
  border-top: 1px solid var(--border);
  overscroll-behavior: contain;
}
/* The canvas alone sets the scroll height; rendered rows are clipped to it. */
.canvas {
  position: relative;
  overflow: hidden;
  contain: strict;
  width: 100%;
}
.rows {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  will-change: transform;
}
.row {
  height: 64px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  cursor: pointer;
  overflow: hidden;
}
.row:hover { background: var(--row-hover); }
.row.active { background: var(--accent-soft); }
.row .name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 500;
}
.row .line {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
}
.row.placeholder .name {
  width: 45%;
  height: 14px;
  border-radius: 4px;
  background: var(--surface-2);
}
.row.placeholder .line {
  width: 30%;
  height: 10px;
  border-radius: 4px;
  background: var(--surface-2);
}
.badge {
  display: inline-block;
  padding: 0 6px;
  border-radius: 4px;
  background: var(--badge);
  color: var(--badge-text);
  font-size: 12px;
  line-height: 18px;
}
.badge.cat-video { background: var(--cat-video); }
.badge.cat-adult { background: var(--cat-adult); }
.badge.cat-audio { background: var(--cat-audio); }
.badge.cat-software_game { background: var(--cat-soft); }
.badge.cat-book_doc { background: var(--cat-book); }
.badge.cat-archive_data, .badge.cat-other { background: var(--cat-archive); }
.badge.code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }

.empty {
  position: absolute;
  inset: 40px 16px auto;
  text-align: center;
  color: var(--muted);
}

/* ---- drawer -------------------------------------------------------------- */

.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.3);
  z-index: 10;
}
.drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(560px, 100vw);
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow);
  transform: translateX(105%);
  transition: transform 0.18s ease;
  z-index: 11;
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 12px;
  overflow: hidden;
}
.drawer.open { transform: none; }
.drawer-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}
.drawer-header h2 {
  margin: 0;
  font-size: 16px;
  word-break: break-all;
}
.drawer-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.meta {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 4px 12px;
  margin: 0;
  font-size: 13px;
}
.meta dt { color: var(--muted); }
.meta dd { margin: 0; word-break: break-all; }
.meta .hash { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }
.tree-header { display: flex; align-items: center; justify-content: space-between; }
.tree-header h3 { margin: 0; font-size: 14px; }
.tree {
  flex: 1 1 auto;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  font-size: 13px;
  background: var(--bg);
}
.tree details > summary {
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: baseline;
  gap: 4px;
}
.tree details > summary::-webkit-details-marker { display: none; }
.tree details > summary::before { content: "▸"; color: var(--muted); width: 10px; flex: none; }
.tree details[open] > summary::before { content: "▾"; }
.tree details > summary > .node { flex: 1 1 auto; min-width: 0; }
.tree > .node, .tree .children > .node { padding-left: 14px; }
.tree .children { padding-left: 16px; }
.tree .node {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 1px 0;
}
.tree .node .label { overflow-wrap: anywhere; }
.tree .node .size { color: var(--muted); white-space: nowrap; }
.tree .file .label::before { content: "📄 "; }
.tree summary .label::before { content: "📁 "; }
.tree .truncated .label { color: var(--muted); font-style: italic; }

/* ---- toast & login ------------------------------------------------------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 8px 16px;
  border-radius: 8px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 20;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%); }
.toast.error { background: var(--danger); color: #fff; }

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}
.login-card {
  width: min(360px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px;
  box-shadow: var(--shadow);
}
.login-card h1 { margin: 0 0 4px; font-size: 20px; }
.login-card form { display: flex; flex-direction: column; gap: 8px; margin-top: 16px; }
.login-card button { margin-top: 8px; padding: 8px; }

@media (max-width: 640px) {
  .topbar { padding: 8px 12px; }
  .topbar h1 { font-size: 15px; white-space: nowrap; }
  #user-name { display: none; }
  .filters > * { flex: 1 1 45%; }
  .filters #q, .filters #code { flex-basis: 100%; }
}
