:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #eef1f7;
  --text: #0f172a;
  --muted: #5b6477;
  --border: #dbe1ec;
  --primary: #2453d6;
  --primary-text: #ffffff;
  --danger: #c52a2a;
  --ok: #17803d;
  --ok-bg: #e3f6ea;
  --warn: #9a5b00;
  --warn-bg: #fff2d6;
  --bad: #b42318;
  --bad-bg: #fde6e4;
  --shadow: 0 1px 2px rgb(15 23 42 / .06), 0 4px 16px rgb(15 23 42 / .06);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1120;
    --surface: #131b2e;
    --surface-2: #1b2540;
    --text: #e6eaf3;
    --muted: #97a1b6;
    --border: #283352;
    --primary: #5b86ff;
    --primary-text: #0b1120;
    --danger: #ff6b6b;
    --ok: #4ade80;
    --ok-bg: #12301f;
    --warn: #fbbf24;
    --warn-bg: #33270b;
    --bad: #ff8a80;
    --bad-bg: #3a1513;
    --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 4px 16px rgb(0 0 0 / .25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1120; --surface: #131b2e; --surface-2: #1b2540; --text: #e6eaf3; --muted: #97a1b6;
  --border: #283352; --primary: #5b86ff; --primary-text: #0b1120; --danger: #ff6b6b; --ok: #4ade80;
  --ok-bg: #12301f; --warn: #fbbf24; --warn-bg: #33270b; --bad: #ff8a80; --bad-bg: #3a1513;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  min-height: 100vh;
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; margin-bottom: 6px; }
p { margin: 0 0 12px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; letter-spacing: .03em; }
.pre { white-space: pre-wrap; }
.error { color: var(--bad); min-height: 1.2em; margin: 6px 0; font-size: .9rem; }
.error:empty { min-height: 0; margin: 0; }
.ok-text { color: var(--ok); }

/* ---------- form controls ---------- */
label { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); margin-bottom: 12px; min-width: 0; }
input, select, textarea {
  font: inherit; font-size: 16px; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 1px; border-color: transparent; }
textarea { resize: vertical; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-size: .95rem; font-weight: 600; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px;
  padding: 9px 14px; cursor: pointer; white-space: nowrap; min-height: 42px;
}
.btn:hover { filter: brightness(.97); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--primary); color: var(--primary-text); border-color: transparent; }
.btn.danger { color: var(--danger); background: transparent; border-color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.link { background: none; border: none; color: var(--primary); font-weight: 500; }
.btn.block { width: 100%; }
.btn.sm { min-height: 32px; padding: 4px 10px; font-size: .85rem; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }

/* ---------- auth screens ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.auth-card { width: 100%; max-width: 420px; padding: 28px 24px; }
.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; }
.brand.small { margin: 0; gap: 8px; font-weight: 700; }
.warn-box { background: var(--warn-bg); color: var(--warn); border-radius: 10px; padding: 10px 12px; font-size: .9rem; }
.bio-btn { margin-top: 10px; }

/* ---------- vault ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.top-actions { display: flex; gap: 6px; }
.wrap { max-width: 1100px; margin: 0 auto; padding: 16px 16px 110px; }
.toolbar { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  font: inherit; font-size: .85rem; white-space: nowrap; cursor: pointer;
  padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
}
.chip.on { background: var(--primary); color: var(--primary-text); border-color: transparent; }

.alert { background: var(--warn-bg); color: var(--text); border: 1px solid color-mix(in srgb, var(--warn) 35%, transparent); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 14px; }
.alert ul { margin: 6px 0 0; padding-left: 18px; }
.alert li { margin: 4px 0; }
.link-btn { font: inherit; background: none; border: none; padding: 0; color: var(--text); font-weight: 600; text-decoration: underline; cursor: pointer; text-align: left; }

.grid { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
@media (max-width: 380px) { .grid { grid-template-columns: 1fr; } }
.doc {
  width: 100%; display: flex; gap: 12px; align-items: stretch; text-align: left;
  font: inherit; color: inherit; cursor: pointer;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px; box-shadow: var(--shadow);
}
.doc:hover { border-color: var(--primary); }
.thumb { width: 84px; height: 64px; flex: none; border-radius: 8px; object-fit: cover; background: var(--surface-2); }
.thumb.icon { display: grid; place-items: center; font-size: 30px; }
.doc-info { min-width: 0; flex: 1; display: flex; flex-direction: column; gap: 2px; }
.doc-title { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-sub { color: var(--muted); font-size: .85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-num { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .85rem; }
.doc-meta { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 4px; }
.no-match { padding: 20px 0; }

.badge { display: inline-block; font-size: .75rem; font-weight: 700; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }

.empty { text-align: center; padding: 60px 16px; }
.empty-icon { font-size: 52px; margin-bottom: 8px; }

.fab {
  position: fixed; right: 16px; bottom: calc(18px + env(safe-area-inset-bottom)); z-index: 6;
  font: inherit; font-weight: 700; font-size: 1rem; cursor: pointer;
  padding: 14px 22px; border-radius: 999px; border: none;
  background: var(--primary); color: var(--primary-text);
  box-shadow: 0 6px 20px rgb(36 83 214 / .35);
}

/* ---------- dialogs ---------- */
dialog {
  width: min(680px, calc(100% - 32px)); max-height: calc(100vh - 48px);
  padding: 0; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgb(0 0 0 / .3);
}
dialog[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: rgb(8 12 24 / .55); }
#edit-form { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.dlg-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 12px 12px 18px; border-bottom: 1px solid var(--border); }
.dlg-head h2 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dlg-body { padding: 16px 18px; overflow-y: auto; flex: 1; min-height: 0; }
.dlg-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--border); }
.wrap-foot { flex-wrap: wrap; justify-content: flex-start; }
.spacer { flex: 1; }
@media (max-width: 600px) {
  dialog { width: 100%; max-width: 100%; height: 100%; max-height: 100%; margin: 0; border-radius: 0; border: none; }
  .wrap-foot .btn { flex: 1 1 calc(50% - 8px); }
  .wrap-foot .spacer { display: none; }
}

.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.row3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0 12px; }
@media (max-width: 600px) { .row2, .row3 { grid-template-columns: 1fr; } }
.hint { background: var(--warn-bg); color: var(--warn); border-radius: 8px; padding: 8px 10px; font-size: .85rem; margin-top: -4px; }

.files-box { border: 1px dashed var(--border); border-radius: 12px; padding: 10px 12px 12px; margin: 0 0 8px; }
.files-box legend { font-size: .85rem; color: var(--muted); padding: 0 6px; }
.file-list { list-style: none; margin: 0 0 10px; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.file-list li { display: flex; align-items: center; gap: 8px; min-width: 0; }
.file-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.file-actions label.btn { margin: 0; flex-direction: row; color: var(--text); font-size: .95rem; }

.previews { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin-bottom: 16px; }
.previews > p { grid-column: 1 / -1; }
.pv { margin: 0; }
.pv img { width: 100%; height: 130px; object-fit: cover; border-radius: 10px; border: 1px solid var(--border); cursor: zoom-in; background: var(--surface-2); }
.pv-file { width: 100%; height: 130px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; padding: 8px; font: inherit; font-size: .8rem; color: var(--text); background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; cursor: pointer; word-break: break-all; }
.pv-file .big { font-size: 34px; }
.pv figcaption { font-size: .75rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 4px; }

.details { display: grid; grid-template-columns: max-content 1fr; gap: 8px 16px; margin: 0; }
.details dt { color: var(--muted); font-size: .85rem; padding-top: 2px; }
.details dd { margin: 0; display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center; min-width: 0; overflow-wrap: anywhere; }
.inline-actions { display: inline-flex; gap: 6px; }

.set { padding-bottom: 16px; margin-bottom: 16px; border-bottom: 1px solid var(--border); }
.set:last-child { border-bottom: none; margin-bottom: 0; }
.danger-zone h3 { color: var(--danger); }

/* ---------- toast / busy ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(86px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 100;
  max-width: calc(100% - 32px); width: max-content;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: .9rem; box-shadow: var(--shadow);
}
.busy { position: fixed; inset: 0; z-index: 99; display: grid; place-content: center; justify-items: center; gap: 12px; background: rgb(8 12 24 / .55); color: #fff; font-weight: 600; }
.spinner { width: 38px; height: 38px; border-radius: 50%; border: 4px solid rgb(255 255 255 / .3); border-top-color: #fff; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
