:root {
  --bg: #0f1115;
  --bg-elev: #161a22;
  --bg-elev-2: #1d2330;
  --border: #2a3142;
  --text: #e6e9ef;
  --muted: #8a93a6;
  --primary: #6c8cff;
  --primary-hover: #587bff;
  --danger: #ff6b6b;
  --danger-hover: #ff4f4f;
  --success: #56d364;
  --radius: 10px;
  --radius-lg: 14px;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

body {
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
    env(safe-area-inset-bottom) env(safe-area-inset-left);
}

button {
  font: inherit;
  cursor: pointer;
  border-radius: var(--radius);
  border: 1px solid transparent;
  padding: 9px 14px;
  transition: background-color 120ms ease, border-color 120ms ease, color 120ms ease;
}

button.primary {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}
button.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }

button.ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}
button.ghost:hover { background: var(--bg-elev-2); }

button.danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--danger);
}
button.danger:hover { background: var(--danger); color: white; }

button.small { padding: 6px 10px; font-size: 13px; }

button.link {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 6px 0;
  text-decoration: underline;
  font-size: 13px;
}
button.link.danger { color: var(--danger); }

input,
textarea,
select {
  font: inherit;
  color: var(--text);
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 12px;
  width: 100%;
  outline: none;
}
input:focus,
textarea:focus,
select:focus {
  border-color: var(--primary);
}

textarea {
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
}

label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 12px;
}
label > input,
label > textarea,
label > select {
  margin-top: 4px;
  color: var(--text);
}
label.checkbox {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text);
  margin: 0 12px 0 0;
}
label.checkbox input { width: auto; margin: 0; }

/* Ensure the HTML `hidden` attribute always wins over element-level
   display rules like `label { display: block }`. Without this, JS toggles
   such as `unlock-confirm-row.hidden = true` have no visual effect. */
[hidden] { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); min-height: 1.2em; margin: 4px 0 0; font-size: 13px; }

/* Screens */
.screen { display: none; }
.screen.active { display: block; }

#login-screen.active,
#unlock-screen.active {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px;
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 420px;
}

.auth-switch {
  margin: 16px 0 0;
  text-align: center;
}

.auth-switch .link {
  display: inline;
  padding: 0;
  margin-left: 4px;
}

.auth-wrap {
  width: 100%;
  max-width: 420px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.auth-help {
  margin: 0 0 8px;
  text-align: center;
  width: 100%;
}

.text-link {
  color: var(--primary);
  text-decoration: none;
}

.text-link:hover {
  text-decoration: underline;
}

a.help-link {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
}

.card h1 { margin: 0 0 4px; font-size: 22px; }
.card h2 { margin: 0 0 12px; font-size: 16px; }

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { font-weight: 700; letter-spacing: 0.3px; }
.tabs { display: flex; gap: 4px; flex: 1; }
.tab {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 8px 12px;
  border-radius: var(--radius);
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); background: var(--bg-elev-2); }
.topbar-actions { display: flex; gap: 8px; }

/* Tab panels */
.tab-panel { display: none; padding: 16px; }
.tab-panel.active { display: block; }

/* Vault layout */
.vault-layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 16px;
  min-height: calc(100vh - 110px);
}

.entries {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 12px;
  display: flex;
  flex-direction: column;
  min-height: 200px;
}
.entries-header {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}
.entries-header input { flex: 1; }
.entries-list {
  list-style: none;
  padding: 0;
  margin: 0;
  overflow-y: auto;
  flex: 1;
}
.entries-list li {
  padding: 10px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  border: 1px solid transparent;
}
.entries-list li:hover { background: var(--bg-elev-2); }
.entries-list li.active {
  background: var(--bg-elev-2);
  border-color: var(--primary);
}
.entries-list .entry-label { font-weight: 600; }
.entries-list .entry-sub { color: var(--muted); font-size: 12px; }

.editor {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
}

.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  min-height: 200px;
}

.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.input-row {
  display: flex;
  gap: 6px;
  margin-top: 4px;
}
.input-row input { flex: 1; }

.form-actions {
  display: flex;
  gap: 8px;
  margin: 12px 0;
}

.generator {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 12px;
}
.generator summary { cursor: pointer; color: var(--muted); font-size: 13px; }

/* Crypto layout */
.crypto-layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
.crypto-layout .card { max-width: none; }

/* Backup */
[data-panel="backup"] {
  display: none;
  gap: 16px;
}
[data-panel="backup"].active {
  display: grid;
  grid-template-columns: 1fr 1fr;
}
[data-panel="backup"] .card { max-width: none; }

/* Documents tab */
.docs-container { max-width: 1100px; margin: 0 auto; }
.docs-lead { margin: 0 0 12px; }
.docs-footnote { margin-top: 12px; }

.mode-selector {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 16px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elev);
}
.mode-selector label {
  margin: 0;
  white-space: nowrap;
  color: var(--text);
  font-weight: 600;
  font-size: 14px;
}
.mode-selector select { flex: 1; max-width: 360px; font-weight: 600; }

body.mode-doc .mode-msg-only { display: none !important; }
body.mode-msg .mode-doc-only { display: none !important; }

.docs-grid {
  display: grid;
  gap: 16px;
  grid-template-columns: 1fr 1fr;
}
.docs-card { max-width: none; }

.hidden-file-input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.file-picker {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
  padding: 8px 10px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
}
.file-picker-name { overflow-wrap: anywhere; font-size: 13px; flex: 1; }

.dropzone {
  margin: 0 0 12px;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 16px;
  text-align: center;
  color: var(--muted);
  background: rgba(31, 41, 55, 0.25);
  transition: border-color 120ms ease, background 120ms ease;
  cursor: pointer;
  font-size: 13px;
}
.dropzone strong { color: var(--text); }
.dropzone.dragover {
  border-color: var(--primary);
  background: rgba(108, 140, 255, 0.1);
}

.note-input-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
  margin-top: 4px;
}
.note-input-row input[type="text"] { flex: 1; }

.note-list {
  width: 100%;
  padding: 6px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  min-height: 110px;
  max-height: 260px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.note-list:empty::before {
  content: "No notes yet.";
  color: var(--muted);
  font-style: italic;
  padding: 6px;
  font-size: 12px;
}

.note-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 8px;
  border-radius: var(--radius);
  background: rgba(31, 41, 55, 0.6);
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 120ms ease, border-color 120ms ease;
}
.note-item:hover { background: rgba(108, 140, 255, 0.1); }
.note-item.selected {
  border-color: var(--primary);
  background: rgba(108, 140, 255, 0.15);
}
.note-item-text {
  flex: 1;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  line-height: 1.4;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-all;
  overflow-wrap: anywhere;
}
.note-item-copy {
  flex-shrink: 0;
  padding: 4px 10px;
  font-size: 12px;
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-weight: 600;
  cursor: pointer;
}
.note-item-copy.copied {
  background: var(--success);
  color: #04250b;
  border-color: var(--success);
}

.status {
  margin-top: 10px;
  min-height: 21px;
  font-size: 13px;
  color: var(--muted);
}
.status.error { color: var(--danger); }
.status.ok { color: var(--success); }

.input-error {
  border-color: var(--danger) !important;
  box-shadow: 0 0 0 3px rgba(255, 107, 107, 0.18);
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 16px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
  z-index: 100;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Responsive */
@media (max-width: 820px) {
  .vault-layout { grid-template-columns: 1fr; }
  .crypto-layout { grid-template-columns: 1fr; }
  .docs-grid { grid-template-columns: 1fr; }
  [data-panel="backup"].active { grid-template-columns: 1fr; }
  .row { grid-template-columns: 1fr; }
  .topbar { flex-wrap: wrap; }
  .tabs { order: 3; flex-basis: 100%; overflow-x: auto; }
}
