:root {
  --bg: #0f1115;
  --surface: #181b22;
  --surface-2: #20242e;
  --border: #2a2f3a;
  --text: #e6e9ef;
  --muted: #8b93a3;
  --primary: #4f8cff;
  --primary-hover: #3d78e6;
  --danger: #ff5c6c;
  --success: #34c98a;
  --radius: 10px;
  color-scheme: dark;
}

/* Light theme overrides (applied when <html data-theme="light">) */
:root[data-theme="light"] {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --border: #d7dce4;
  --text: #1a1d24;
  --muted: #5c6472;
  --primary: #2f6fed;
  --primary-hover: #2559c9;
  --danger: #d33448;
  --success: #1e9e6a;
  color-scheme: light;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); font-size: 0.9rem; margin: 0.25rem 0 0; }

button {
  font: inherit;
  cursor: pointer;
  border-radius: var(--radius);
  border: 1px solid transparent;
  padding: 0.55rem 0.9rem;
  transition: background 0.15s, border-color 0.15s;
}
button.primary { background: var(--primary); color: #fff; }
button.primary:hover { background: var(--primary-hover); }
button.ghost { background: transparent; color: var(--text); border-color: var(--border); }
button.ghost:hover { background: var(--surface-2); }

input, textarea {
  font: inherit;
  width: 100%;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.55rem 0.7rem;
  margin-top: 0.3rem;
}
input:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; }

label { display: block; margin-top: 0.85rem; font-size: 0.9rem; color: var(--muted); }

.brand { font-size: 1.2rem; font-weight: 600; display: flex; align-items: center; gap: 0.55rem; }
.lock-icon { font-size: 1.1rem; }
.brand-logo { height: 1.9em; width: 1.9em; display: block; flex-shrink: 0; }
.lock-card .brand .brand-logo { height: 2.4em; width: 2.4em; }

/* --- Lock screen --- */
#lock-screen { display: grid; place-items: center; min-height: 100vh; padding: 1rem; }
.lock-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 2rem;
  width: 100%;
  max-width: 400px;
  position: relative;
}
.theme-toggle {
  position: absolute;
  top: 0.9rem;
  right: 0.9rem;
  padding: 0.35rem 0.55rem;
  line-height: 1;
}
.lock-card .brand { justify-content: center; margin-bottom: 0.5rem; }
#lock-subtitle { text-align: center; margin-top: 0; }
#lock-form button { width: 100%; margin-top: 1.2rem; }
.fineprint { font-size: 0.78rem; margin-top: 1.4rem; text-align: center; line-height: 1.4; }

/* --- Vault screen --- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; padding: 0.9rem 1.4rem;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.topbar-actions { display: flex; align-items: center; gap: 0.6rem; }
.topbar-actions input[type="search"] { width: 220px; margin: 0; }
main { padding: 1.2rem 1.4rem; }

/* Folder sidebar + content layout */
.vault-body { display: flex; gap: 1.1rem; align-items: flex-start; }
.sidebar {
  flex: 0 0 210px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem;
  position: sticky; top: 76px;
}
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.2rem 0.4rem 0.5rem;
  color: var(--muted); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em;
}
.sidebar-head button { padding: 0.25rem 0.5rem; font-size: 0.8rem; }
#folder-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.folder-item {
  display: flex; align-items: center; gap: 0.4rem;
  padding: 0.45rem 0.55rem; border-radius: var(--radius);
  cursor: pointer; font-size: 0.9rem;
}
.folder-item:hover { background: var(--surface-2); }
.folder-item.active { background: var(--primary); color: #fff; }
.folder-item.drag-over { outline: 2px dashed var(--primary); outline-offset: -2px; background: var(--surface-2); }
/* Hint which folders can receive a drop while dragging */
.sidebar.drag-active .folder-item[class]:not(:first-child) { outline: 1px dashed var(--border); outline-offset: -2px; }
#entry-table tbody tr[draggable] { cursor: grab; }
#entry-table tbody tr.dragging { opacity: 0.4; }
.folder-item .folder-twisty {
  width: 0.9rem; flex-shrink: 0; text-align: center; color: var(--muted);
  font-size: 0.7rem; cursor: pointer; user-select: none;
}
.folder-item.active .folder-twisty { color: rgba(255,255,255,0.85); }
.folder-item .folder-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-item .folder-add { font-weight: 700; }
.folder-item .folder-add:hover { color: var(--primary); }
.folder-item .folder-count { color: var(--muted); font-size: 0.8rem; }
.folder-item.active .folder-count { color: rgba(255,255,255,0.8); }
.folder-item .folder-del {
  visibility: hidden; background: none; border: none; color: inherit;
  padding: 0 0.2rem; font-size: 0.85rem; opacity: 0.7;
}
.folder-item:hover .folder-del { visibility: visible; }
.folder-item .folder-del:hover { opacity: 1; color: var(--danger); }
.folder-item.active .folder-del:hover { color: #fff; }
.vault-content { flex: 1; min-width: 0; }

/* Bulk selection action bar */
.bulk-bar {
  display: flex; align-items: center; gap: 0.8rem; flex-wrap: wrap;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.55rem 0.9rem;
  margin-bottom: 0.7rem;
}
.bulk-bar #bulk-count { font-weight: 600; }
.bulk-bar .bulk-move { display: flex; align-items: center; gap: 0.4rem; color: var(--muted); font-size: 0.9rem; }
.bulk-bar select {
  margin: 0;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 0.4rem 0.5rem;
}
.bulk-bar button { padding: 0.4rem 0.9rem; }
.danger-btn { background: var(--danger); color: #fff; border: 1px solid var(--danger); }
.danger-btn:hover { filter: brightness(0.92); }

/* Users dialog */
.users-list { display: grid; gap: 0.4rem; margin-bottom: 0.4rem; }
.user-row {
  display: flex; align-items: center; justify-content: space-between; gap: 0.6rem;
  padding: 0.5rem 0.7rem; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: var(--radius);
}
.user-actions { display: flex; gap: 0.35rem; flex-shrink: 0; }
.users-subhead { margin: 1rem 0 0.2rem; font-size: 1rem; }
.folder-checks { display: flex; flex-wrap: wrap; gap: 0.6rem; margin-top: 0.3rem; }
.folder-checks label.cb { margin: 0; }
#users-dialog select { margin: 0; background: var(--surface-2); color: var(--text); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.5rem; }
#entry-table th.col-select, #entry-table td.col-select { width: 1%; text-align: center; padding-left: 0.6rem; padding-right: 0.2rem; }
#entry-table .col-select input { margin: 0; cursor: pointer; width: auto; }

.empty { text-align: center; padding: 4rem 1rem; color: var(--muted); }
.empty button { margin-top: 1rem; }

@media (max-width: 700px) {
  .vault-body { flex-direction: column; }
  .sidebar { flex-basis: auto; width: 100%; position: static; }
}

/* --- Entries table --- */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
#entry-table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.86rem;
  white-space: nowrap;
}
#entry-table thead th {
  position: sticky; top: 0;
  background: var(--surface-2);
  color: var(--muted);
  font-weight: 600;
  text-align: left;
  padding: 0.6rem 0.8rem;
  border-bottom: 1px solid var(--border);
  z-index: 1;
  white-space: nowrap;
}
#entry-table thead th.sortable { cursor: pointer; user-select: none; }
#entry-table thead th.sortable:hover { color: var(--text); }
.sort-arrow { color: var(--primary); font-size: 0.8em; }
#entry-table tbody td {
  padding: 0.55rem 0.8rem;
  border-bottom: 1px solid var(--border);
  max-width: 220px;
  overflow: hidden; text-overflow: ellipsis;
}
#entry-table tbody tr:last-child td { border-bottom: none; }
#entry-table tbody tr:hover { background: var(--surface-2); }
#entry-table td.strong { font-weight: 600; }
#entry-table td.nowrap { white-space: nowrap; color: var(--muted); }

#entry-table td.editable { cursor: cell; }
#entry-table td.editable:hover { outline: 1px dashed var(--border); outline-offset: -1px; }
.inline-edit {
  width: 100%;
  margin: 0;
  padding: 0.3rem 0.4rem;
  font: inherit;
  box-sizing: border-box;
}

.pw-cell { display: flex; align-items: center; gap: 0.4rem; }
.pw-mask {
  font-family: ui-monospace, monospace;
  cursor: pointer;
  color: var(--muted);
  user-select: none;
}
.pw-mask.revealed { color: var(--text); letter-spacing: 0; user-select: text; }

.col-actions { position: sticky; right: 0; background: var(--surface); }
#entry-table tbody tr:hover .col-actions { background: var(--surface-2); }
.col-actions { white-space: nowrap; }
.col-actions .icon-btn { margin-left: 0.3rem; }

.icon-btn {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  padding: 0.3rem 0.55rem; font-size: 0.82rem;
}
.icon-btn:hover { background: var(--border); }
.icon-btn.danger:hover { background: var(--danger); border-color: var(--danger); color: #fff; }

/* --- Dialog --- */
dialog {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.6rem;
  width: min(680px, 94vw);
  max-height: 90vh;
}
dialog::backdrop { background: rgba(0,0,0,0.6); }
dialog h2 { margin: 0 0 0.5rem; font-size: 1.2rem; }

/* Two-column field grid inside the add/edit dialog */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 1.1rem; }
.form-grid .field-full { grid-column: 1 / -1; }
.form-grid .field label { margin-top: 0.8rem; }
@media (max-width: 560px) { .form-grid { grid-template-columns: 1fr; } }
.password-row { display: flex; gap: 0.5rem; align-items: stretch; }
.password-row input { margin-top: 0.3rem; }
.password-row button { margin-top: 0.3rem; white-space: nowrap; }
#gen-options { margin-top: 0.6rem; font-size: 0.9rem; }
#gen-options summary { cursor: pointer; color: var(--muted); }
.gen-grid { display: flex; flex-wrap: wrap; gap: 0.9rem; align-items: center; margin-top: 0.6rem; }
.gen-grid label { margin-top: 0; color: var(--text); }
.gen-grid input[type="range"] { width: 140px; }
label.cb { display: flex; align-items: center; gap: 0.35rem; }
label.cb input { width: auto; margin: 0; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 1.4rem; }

/* Columns manager rows */
#columns-list { margin-bottom: 0.7rem; }
.col-row { display: flex; gap: 0.4rem; align-items: center; margin-bottom: 0.45rem; }
.col-row input[type="text"] { flex: 1; margin: 0; }
.col-row select {
  margin: 0;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.45rem 0.5rem;
}
.col-row .icon-btn { padding: 0.35rem 0.5rem; }
#add-column { margin-bottom: 0.4rem; }

/* --- Toast --- */
.toast {
  position: fixed; bottom: 1.5rem; left: 50%; transform: translateX(-50%);
  background: var(--success); color: #04160e; font-weight: 600;
  padding: 0.6rem 1.1rem; border-radius: var(--radius);
  box-shadow: 0 6px 20px rgba(0,0,0,0.4); z-index: 20;
}
.toast.danger { background: var(--danger); color: #fff; }

@media (max-width: 560px) {
  .topbar { flex-direction: column; align-items: stretch; }
  .topbar-actions { flex-wrap: wrap; }
  .topbar-actions input[type="search"] { flex: 1; width: auto; }
}
