:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-2: #f0f2f5;
  --border: #dfe3e8;
  --text: #1c2026;
  --muted: #69717d;
  --accent: #e5484d;
  --primary: #2f6fed;
  --primary-text: #ffffff;
  --hover: #f3f5f8;
  --selected: #e8f0fe;
  --ok: #1f9d55;
  --ok-bg: #e3f6eb;
  --bad: #d63b3b;
  --bad-bg: #fde8e8;
  --warn: #b7791f;
  --warn-bg: #fdf3dc;
  --mark: #ffe58a;
  --mark-text: #1c2026;
  --tag-bg: #eef1f6;
  --tag-text: #3b4452;
  --shadow: 0 8px 30px rgba(15, 23, 42, .15);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --border: #2a2f3a;
  --text: #e3e6eb;
  --muted: #8b93a1;
  --primary: #4c84ff;
  --hover: #1d212a;
  --selected: #1b2740;
  --ok: #3ecf7f;
  --ok-bg: #12301f;
  --bad: #ff6b6b;
  --bad-bg: #3a1717;
  --warn: #f0b44c;
  --warn-bg: #3a2c10;
  --mark: #7a5d00;
  --mark-text: #fff3c4;
  --tag-bg: #252a35;
  --tag-text: #c8cdd6;
  --shadow: 0 8px 30px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
}
button, input, select { font: inherit; color: inherit; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .92em; background: var(--panel-2); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 16px; white-space: nowrap; }
.search { position: relative; flex: 1; max-width: 760px; }
.search input {
  width: 100%; padding: 9px 40px 9px 36px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--panel-2); outline: none;
}
.search input:focus { border-color: var(--primary); background: var(--panel); }
.search-icon { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); }
.help-btn {
  position: absolute; right: 7px; top: 50%; transform: translateY(-50%);
  width: 24px; height: 24px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--panel); color: var(--muted); cursor: pointer; font-size: 12px;
}
.search-help {
  position: absolute; top: calc(100% + 6px); left: 0; right: 0;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: var(--shadow); padding: 12px 14px; z-index: 30;
}
.search-help td { padding: 3px 10px 3px 0; vertical-align: top; }
.search-help p { margin: 8px 0 0; color: var(--muted); }
.top-actions { display: flex; gap: 8px; margin-left: auto; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 7px 13px; border-radius: 7px;
  border: 1px solid var(--border); background: var(--panel); cursor: pointer;
  white-space: nowrap; user-select: none;
}
.btn:hover { background: var(--hover); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--hover); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.icon { padding: 7px 9px; }
:root[data-theme="dark"] .sun { display: block; }
:root[data-theme="dark"] .moon { display: none; }
:root[data-theme="light"] .sun { display: none; }

/* ---------- toolbar ---------- */
.toolbar {
  display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px;
  padding: 14px 20px 6px;
}
.filters { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px; flex: 1; }
.field { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.field.block { display: flex; margin-bottom: 12px; }
.field > span { font-weight: 550; }
.field select, .field input {
  font-size: 14px; color: var(--text);
  padding: 6px 9px; border-radius: 7px; border: 1px solid var(--border); background: var(--panel);
  min-width: 150px; outline: none;
}
.field input:focus, .field select:focus { border-color: var(--primary); }
.field.name-search input { min-width: 220px; }
.folder-actions {
  margin-left: auto;
  display: flex; align-items: center; gap: 6px; padding: 3px 3px 3px 10px;
  border: 1px solid var(--border); border-radius: 9px; background: var(--panel-2);
}
.folder-actions-label { font-size: 13px; color: var(--muted); max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-actions-label b { color: var(--text); }
.clear-filters { margin-bottom: 3px; color: var(--muted); }
.clear-filters:hover { color: var(--bad); }
.active-tags { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; padding-bottom: 4px; }
.global-actions { position: relative; display: flex; gap: 8px; }
.menu {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 25;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow);
  padding: 4px; display: flex; flex-direction: column; min-width: 220px;
}
.menu button { text-align: left; padding: 8px 12px; background: none; border: 0; border-radius: 6px; cursor: pointer; }
.menu button:hover { background: var(--hover); }

.statusbar {
  display: flex; flex-wrap: wrap; gap: 18px; align-items: center;
  padding: 4px 20px 10px; color: var(--muted); font-size: 13px;
}
.ocr-activity { display: inline-flex; gap: 8px; align-items: center; color: var(--warn); }
.spinner {
  width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid currentColor; border-right-color: transparent;
  animation: spin .8s linear infinite; display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

.bulkbar {
  position: sticky; top: 57px; z-index: 15;
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  margin: 0 20px 10px; padding: 8px 12px;
  background: var(--selected); border: 1px solid var(--primary); border-radius: 8px;
}
.bulkbar #bulkCount { font-weight: 600; margin-right: 6px; }

/* ---------- table ---------- */
main { padding: 0 20px 30px; }
.table-wrap {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px; overflow-x: auto;
  position: relative;
}
.table-wrap.dragover { outline: 2px dashed var(--primary); outline-offset: -6px; }
table.docs { width: 100%; border-collapse: collapse; }
.docs th, .docs td { padding: 9px 10px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
.docs th {
  font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em;
  background: var(--panel-2); white-space: nowrap; position: sticky; top: 0;
}
.docs th.sortable { cursor: pointer; user-select: none; }
.docs th.sortable:hover { color: var(--text); }
.docs th.sorted::after { content: " ▾"; }
.docs th.sorted.asc::after { content: " ▴"; }
.docs th.num, .docs td.num { text-align: right; }
.docs tbody tr:hover { background: var(--hover); }
.docs tbody tr.selected { background: var(--selected); }
.docs tbody tr:last-child td { border-bottom: 0; }
.col-check { width: 32px; }
.col-actions { width: 1%; white-space: nowrap; }
.docs td.nowrap { white-space: nowrap; }

.name-cell { min-width: 260px; max-width: 520px; }
.name-link { color: var(--text); text-decoration: none; font-weight: 550; word-break: break-word; display: inline-flex; gap: 7px; align-items: flex-start; }
.name-link:hover { color: var(--primary); }
.name-link svg { flex: none; margin-top: 2px; color: var(--accent); }
.snippet { margin-top: 4px; font-size: 12.5px; color: var(--muted); word-break: break-word; }
mark { background: var(--mark); color: var(--mark-text); border-radius: 2px; padding: 0 1px; }
.folder-link { color: var(--muted); cursor: pointer; word-break: break-word; }
.folder-link:hover { color: var(--primary); text-decoration: underline; }
.sub { font-size: 12px; color: var(--muted); }
.warn-text { color: var(--warn); }

.tag {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 1px 8px; margin: 1px 4px 1px 0; border-radius: 999px;
  background: var(--tag-bg); color: var(--tag-text); font-size: 12px; cursor: pointer; white-space: nowrap;
  border: 1px solid transparent;
}
.tag:hover { border-color: var(--primary); }
.tag .x { cursor: pointer; opacity: .6; font-weight: bold; }
.tag .x:hover { opacity: 1; }
.tag.filter { background: var(--primary); color: var(--primary-text); }

.ocr-cell { white-space: nowrap; }
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11.5px; font-weight: 700; letter-spacing: .04em;
  padding: 2px 8px; border-radius: 999px; vertical-align: middle;
}
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge.ok { color: var(--ok); background: var(--ok-bg); }
.badge.bad { color: var(--bad); background: var(--bad-bg); }
.badge.busy { color: var(--warn); background: var(--warn-bg); }
.badge.busy::before { animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .25; } }
.ocr-cell .btn { margin-left: 6px; }

.row-actions { display: flex; gap: 2px; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 30px; height: 30px; border-radius: 6px; border: 0; background: transparent;
  color: var(--muted); cursor: pointer; text-decoration: none;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.icon-btn.danger:hover { color: var(--bad); }

.empty { padding: 50px 20px; text-align: center; color: var(--muted); }
.pager { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 12px 2px; color: var(--muted); flex-wrap: wrap; }
.pager .pages { display: flex; gap: 4px; align-items: center; }
.pager select { padding: 4px 6px; border-radius: 6px; border: 1px solid var(--border); background: var(--panel); }

/* ---------- dialogs ---------- */
.dlg {
  border: 1px solid var(--border); border-radius: 12px; padding: 0;
  background: var(--panel); color: var(--text); box-shadow: var(--shadow);
  width: min(520px, calc(100vw - 30px));
}
.dlg.wide { width: min(860px, calc(100vw - 30px)); }
.dlg::backdrop { background: rgba(0, 0, 0, .45); }
.dlg-inner { padding: 20px 22px; }
.dlg h2 { margin: 0 0 16px; font-size: 18px; }
.dlg-actions { display: flex; gap: 8px; align-items: center; margin-top: 16px; }
.spacer { flex: 1; }
.row { display: flex; gap: 8px; }
.row.center { justify-content: center; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
@media (max-width: 640px) { .grid2 { grid-template-columns: 1fr; } }
.check { display: flex; gap: 7px; align-items: center; margin-bottom: 10px; }

.dropzone {
  border: 2px dashed var(--border); border-radius: 10px; padding: 22px; text-align: center; margin-bottom: 16px;
  background: var(--panel-2);
}
.dropzone p { margin: 0 0 8px; }
.dropzone.dragover { border-color: var(--primary); background: var(--selected); }
.upload-summary { font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.upload-list { max-height: 300px; overflow: auto; border: 1px solid var(--border); border-radius: 8px; }
.upload-list:empty { display: none; }
.up-item { display: grid; grid-template-columns: 1fr 90px 140px; gap: 10px; align-items: center; padding: 6px 10px; border-bottom: 1px solid var(--border); font-size: 13px; }
.up-item:last-child { border-bottom: 0; }
.up-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.up-size { color: var(--muted); text-align: right; }
.up-state { font-size: 12px; }
.up-state.ok { color: var(--ok); }
.up-state.err { color: var(--bad); }
.up-state.dup { color: var(--warn); }
.progress { height: 5px; border-radius: 3px; background: var(--panel-2); overflow: hidden; }
.progress > div { height: 100%; background: var(--primary); width: 0; transition: width .15s; }

.tag-editor {
  display: flex; flex-wrap: wrap; gap: 4px; align-items: center;
  padding: 5px 7px; border: 1px solid var(--border); border-radius: 7px; background: var(--panel);
}
.tag-editor input { border: 0 !important; flex: 1; min-width: 140px !important; padding: 3px !important; background: transparent !important; }
.doc-info { font-size: 12.5px; }

.tabs { display: flex; gap: 4px; margin-bottom: 8px; }
.tab { padding: 6px 12px; border: 1px solid var(--border); background: var(--panel); border-radius: 6px; cursor: pointer; }
.tab.active { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.textview {
  white-space: pre-wrap; word-break: break-word; max-height: 60vh; overflow: auto; margin: 0;
  padding: 12px; background: var(--panel-2); border-radius: 8px; border: 1px solid var(--border);
  font: 12.5px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

/* ---------- duplicates ---------- */
#dupDlg { max-height: calc(100vh - 30px); max-height: calc(100dvh - 30px); }
#dupDlg[open] { display: flex; flex-direction: column; }
#dupForm { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.dup-help { margin: -8px 0 10px; font-size: 13px; }
/* the list scrolls, header + buttons stay visible; groups must not shrink (they would overlap) */
.dup-list { flex: 1 1 auto; min-height: 0; overflow: auto; display: flex; flex-direction: column; gap: 10px; }
.dup-group { flex-shrink: 0; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.dup-head {
  display: flex; gap: 10px; align-items: center; padding: 6px 10px; font-size: 12.5px;
  background: var(--panel-2); color: var(--muted); border-bottom: 1px solid var(--border);
}
.dup-head label { display: inline-flex; gap: 5px; align-items: center; cursor: pointer; }
.dup-item {
  display: grid; grid-template-columns: 22px minmax(0, 1fr) auto auto; gap: 10px; align-items: center;
  padding: 6px 10px; border-bottom: 1px solid var(--border); font-size: 13px; cursor: pointer;
}
.dup-item:last-child { border-bottom: 0; }
.dup-item .name-link { display: inline-flex; gap: 6px; align-items: center; color: inherit; text-decoration: none; max-width: 100%; }
.dup-item .name-link svg { flex-shrink: 0; }
.dup-item .name-link .dup-path { overflow-wrap: anywhere; }
.dup-item .name-link .dup-dir { color: var(--muted); }
.dup-item .name-link:hover .dup-path { text-decoration: underline; }
.dup-item .dup-meta { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dup-item .dup-state { font-size: 12px; font-weight: 600; white-space: nowrap; }
.dup-item.keep .dup-state { color: var(--ok); }
.dup-item.del { opacity: .75; }
.dup-item.del .name-link .dup-path { text-decoration: line-through; }
.dup-item.del .dup-state { color: var(--bad); }
.btn.danger-fill { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn:disabled { opacity: .5; cursor: default; }

/* ---------- toasts ---------- */
.toasts { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast {
  background: var(--panel); border: 1px solid var(--border); border-left: 4px solid var(--primary);
  border-radius: 8px; padding: 10px 14px; box-shadow: var(--shadow); max-width: 380px;
  animation: slidein .2s ease-out;
}
.toast.error { border-left-color: var(--bad); }
.toast.ok { border-left-color: var(--ok); }
@keyframes slidein { from { transform: translateY(10px); opacity: 0; } }

@media (max-width: 900px) {
  .topbar { flex-wrap: wrap; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .bulkbar { top: 0; }
}
.pager .more { display: inline-flex; align-items: center; gap: 8px; }
#sentinel { height: 1px; }
.folder-actions .btn { color: var(--text); }

/* ---------- built-in PDF viewer (viewer.html) ---------- */
.viewer-page { display: flex; flex-direction: column; height: 100vh; height: 100dvh; overflow: hidden; }
.viewer-bar {
  display: flex; align-items: center; gap: 8px; padding: 6px 10px; flex-shrink: 0;
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.viewer-bar a.btn { display: inline-flex; align-items: center; gap: 6px; text-decoration: none; color: inherit; }
.viewer-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.viewer-dir { color: var(--muted); font-weight: 400; }
.viewer-frame { flex: 1; width: 100%; border: 0; display: block; background: var(--bg); }
.viewer-msg { padding: 40px 20px; text-align: center; color: var(--muted); }
@media (max-width: 560px) { .hide-narrow { display: none; } }
