:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #16181d;
  --muted: #697080;
  --line: #e2e5ea;
  --accent: #2f5bea;
  --accent-text: #ffffff;
  --accent-soft: #e8eefe;
  --danger: #c8322f;
  --danger-soft: #fdecec;
  --warn-soft: #fff4dc;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --card: #1e2127;
    --text: #eceef2;
    --muted: #9aa1ae;
    --line: #2e323a;
    --accent: #6d8dff;
    --accent-text: #0d1020;
    --accent-soft: #232b45;
    --danger: #ff6b67;
    --danger-soft: #3a1f1f;
    --warn-soft: #3a3020;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  -webkit-tap-highlight-color: transparent;
}

h1 { font-size: 1.4rem; margin: 0; line-height: 1.2; overflow-wrap: anywhere; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; }
p { margin: 0 0 .75rem; }
a { color: var(--accent); }

.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.grow { flex: 1; min-width: 0; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.masked { -webkit-text-security: disc; text-security: disc; }
.sr, .offscreen { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.icon { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ------------------------------------------------ Form controls */

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

input:not([type=checkbox]):not([type=range]):not([type=file]), select, textarea {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  font-size: 16px; /* 16px prevents iOS from zooming into inputs */
}
textarea { resize: vertical; line-height: 1.4; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--accent); flex: none; margin: 0; }
input[type=range] { width: 100%; accent-color: var(--accent); }

button, .button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); color: var(--text);
  cursor: pointer; font-weight: 550; text-decoration: none;
  touch-action: manipulation;
}
button:hover { filter: brightness(.97); }
button:disabled { opacity: .6; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.danger { color: var(--danger); border-color: var(--danger-soft); background: var(--danger-soft); }
button.small { min-height: 36px; padding: 0 12px; font-size: .9rem; }
button.wide { width: 100%; }
button.loading { position: relative; color: transparent !important; }
button.loading::after {
  content: ""; position: absolute; width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid var(--muted); border-top-color: transparent; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; min-height: 40px; padding: 0;
  border: 0; border-radius: 10px; background: transparent; color: var(--muted);
}
.icon-btn:hover { background: var(--accent-soft); color: var(--accent); }

.field { display: flex; flex-direction: column; gap: 6px; }
.field .label { font-size: .85rem; font-weight: 600; color: var(--muted); }
.field small { color: var(--muted); font-size: .8rem; }
.check { display: flex; align-items: center; gap: 10px; min-height: 40px; cursor: pointer; }
.input-group { display: flex; gap: 4px; align-items: center; }
.input-group input { flex: 1; min-width: 0; }

/* ------------------------------------------------ Layout */

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(12px);
  -webkit-backdrop-filter: saturate(1.6) blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--accent); }
.topbar nav { display: flex; gap: 2px; overflow-x: auto; }
.tab { border: 0; background: transparent; min-height: 40px; padding: 0 12px; color: var(--muted); }
.tab.active { color: var(--accent); background: var(--accent-soft); }
.tab.lock { padding: 0 10px; }

.container { max-width: 760px; margin: 0 auto; padding: 16px 16px 48px; display: flex; flex-direction: column; gap: 14px; }

.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: space-between; }
.toolbar h1.grow + * { flex: none; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.form { display: flex; flex-direction: column; gap: 14px; }
.actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.actions.wrap { justify-content: flex-start; margin-top: 10px; }
.section-title { margin: 8px 0 -4px; }

.notice { background: var(--warn-soft); padding: 10px 12px; border-radius: 10px; font-size: .9rem; margin: 0; }
.banner {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  background: var(--warn-soft); padding: 10px 12px; border-radius: 10px; font-size: .9rem;
}

/* ------------------------------------------------ Auth screens */

.auth { max-width: 420px; margin: 0 auto; padding: 48px 16px; display: flex; flex-direction: column; gap: 14px; }
.auth h1 { text-align: center; }
.auth-logo {
  width: 64px; height: 64px; margin: 0 auto; border-radius: 18px;
  background: var(--accent); color: #fff; display: grid; place-items: center;
}
.auth-logo .icon { width: 32px; height: 32px; }

/* ------------------------------------------------ Vault list */

.searchbox { position: relative; }
.searchbox .icon { position: absolute; left: 12px; top: 13px; color: var(--muted); }
.searchbox input { padding-left: 40px !important; }

.list { display: flex; flex-direction: column; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.row {
  display: flex; align-items: center; gap: 12px; width: 100%;
  min-height: 64px; padding: 10px 14px;
  border: 0; border-radius: 0; border-bottom: 1px solid var(--line);
  background: transparent; text-align: left; font-weight: 400;
}
.row:last-child { border-bottom: 0; }
.row:hover { background: var(--accent-soft); filter: none; }
.row-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.row-title { font-weight: 600; display: flex; align-items: center; gap: 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { color: var(--muted); font-size: .88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-badges { display: flex; gap: 4px; flex: none; }
.icon.fav { width: 14px; height: 14px; fill: #f5b301; stroke: #f5b301; }
.empty { padding: 24px; text-align: center; color: var(--muted); margin: 0; }

.avatar {
  --hue: 220;
  width: 40px; height: 40px; flex: none; border-radius: 10px;
  display: grid; place-items: center; font-weight: 700;
  background: hsl(var(--hue) 70% 92%); color: hsl(var(--hue) 55% 35%);
}
@media (prefers-color-scheme: dark) {
  .avatar { background: hsl(var(--hue) 35% 25%); color: hsl(var(--hue) 80% 85%); }
}

.badge {
  display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent); font-size: .75rem; font-weight: 600; white-space: nowrap;
}
.badge.warn { background: var(--danger-soft); color: var(--danger); }

/* ------------------------------------------------ Item detail */

.kv { padding: 10px 0; border-bottom: 1px solid var(--line); }
.kv:first-child { padding-top: 0; }
.kv:last-child { border-bottom: 0; padding-bottom: 0; }
.kv > .label { display: block; font-size: .8rem; font-weight: 600; color: var(--muted); margin-bottom: 2px; }
.kv-line { display: flex; align-items: center; gap: 6px; min-height: 40px; }
.kv-line .value { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.kv-actions { display: flex; flex: none; }
.value.notes { white-space: pre-wrap; }
.value.totp { font-size: 1.6rem; font-weight: 600; letter-spacing: .05em; flex: none; }
.countdown { color: var(--muted); font-size: .85rem; font-variant-numeric: tabular-nums; flex: 1; }
.countdown.low { color: var(--danger); }

.codes { list-style: none; padding: 0; margin: 4px 0 6px; display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 2px 12px; }
.codes li { display: flex; align-items: center; justify-content: space-between; }
.code-check { display: flex; align-items: center; gap: 10px; min-height: 40px; cursor: pointer; }
.codes li.used .mono { text-decoration: line-through; color: var(--muted); }

/* ------------------------------------------------ Dialogs, generator, toast */

.overlay {
  position: fixed; inset: 0; z-index: 50; background: rgba(10, 12, 16, .45);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.dialog {
  width: 100%; max-width: 440px; max-height: 90vh; overflow: auto;
  background: var(--card); border-radius: var(--radius); padding: 20px;
  display: flex; flex-direction: column; gap: 12px; box-shadow: 0 20px 50px rgba(0, 0, 0, .3);
}
.dialog h2 { margin: 0; }
.dialog p { margin: 0; }

.gen-out { display: flex; align-items: center; gap: 6px; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 6px 6px 6px 12px; }
.generated { flex: 1; min-width: 0; overflow-wrap: anywhere; font-size: 1.05rem; }
.generated.small { font-size: .85rem; }
.generator { display: flex; flex-direction: column; gap: 6px; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); z-index: 100;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: #16181d; color: #fff; padding: 10px 16px; border-radius: 999px;
  font-size: .9rem; transition: opacity .2s, transform .2s; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); }

/* ------------------------------------------------ Account / admin */

.log { list-style: none; margin: 0; padding: 0; }
.log li { display: flex; justify-content: space-between; align-items: center; gap: 4px 12px; flex-wrap: wrap; padding: 8px 0; border-bottom: 1px solid var(--line); }
.log li:last-child { border-bottom: 0; }
.log li.warn { color: var(--danger); }

.inline-form { display: flex; gap: 8px; flex-wrap: wrap; }
.inline-form input { flex: 2 1 180px; }
.inline-form select { flex: 1 1 140px; width: auto; }
.inline-form button { flex: 1 1 160px; }

.users { display: flex; flex-direction: column; gap: 10px; }
.user-head { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-bottom: 2px; }
.file { width: 100%; }

@media (max-width: 480px) {
  .brand { display: none; }
  .topbar nav { flex: 1; justify-content: space-between; }
  .tab { padding: 0 10px; }
  .container { padding-top: 12px; }
  .codes { grid-template-columns: 1fr; }
}
