:root {
  --bg: #0b1220;
  --surface: #131c2e;
  --surface-2: #1b2740;
  --text: #e8eef7;
  --muted: #8b9bb4;
  --accent: #18b6c4;
  --accent-2: #0E7C86;
  --danger: #e5484d;
  --ok: #46c08a;
  --border: #243149;
  --radius: 16px;
}
[data-theme="light"] {
  --bg: #eef2f7;
  --surface: #ffffff;
  --surface-2: #f1f5fa;
  --text: #14202e;
  --muted: #5a6b80;
  --accent: #0E7C86;
  --accent-2: #0a5b63;
  --border: #d7e0ea;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.82rem; }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }

/* ---- Screens ---- */
.screen { display: none; }
.screen.active { display: block; }

/* ---- Login ---- */
#screen-login { min-height: 100vh; display: none; place-items: center; }
#screen-login.active { display: grid; }
.login-box { text-align: center; padding: 24px; width: 100%; max-width: 340px; }
.login-logo { font-size: 3rem; }
.login-box h1 { margin: 6px 0 2px; }
.pin-dots { display: flex; gap: 14px; justify-content: center; margin: 20px 0; }
.pin-dots span { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--muted); }
.pin-dots span.filled { background: var(--accent); border-color: var(--accent); }
.login-error { color: var(--danger); min-height: 20px; font-size: 0.9rem; }
.pinpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: 12px; }
.pinpad button {
  font-size: 1.5rem; padding: 18px 0; border-radius: 50%; aspect-ratio: 1;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer;
}
.pinpad button:active { background: var(--surface-2); }
.pinpad button.alt { font-size: 1.1rem; }
.pinpad button.ok { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }

/* ---- Topbar ---- */
.topbar { position: sticky; top: 0; z-index: 5; background: var(--bg); padding: 10px 14px 8px;
  border-bottom: 1px solid var(--border); }
.topbar-row { display: flex; align-items: center; justify-content: space-between; }
.app-title { font-size: 1.25rem; margin: 0; }
.topbar-actions { display: flex; gap: 8px; align-items: center; }
.search-row { display: flex; gap: 8px; margin-top: 10px; }
#search { flex: 1; padding: 11px 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: 1rem; }
.icon-btn { width: 42px; height: 42px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: 1.2rem; cursor: pointer; flex: none; }
.icon-btn.primary { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }
.chip { padding: 0 12px; height: 42px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); color: var(--muted); font-weight: 600; cursor: pointer; }
.chip.on { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }

/* ---- List ---- */
.list { padding: 6px 14px 90px; }
.list-letter { color: var(--muted); font-size: 0.8rem; font-weight: 700; margin: 14px 4px 4px; }
.row-wrap { position: relative; margin-bottom: 8px; border-radius: 12px; overflow: hidden; }
.row-actions { position: absolute; inset: 0 0 0 auto; width: 160px; display: flex; }
.row-actions button { flex: 1; border: none; color: #fff; font-size: 0.85rem; cursor: pointer; }
.row-actions .act-share { background: var(--accent-2); }
.row-actions .act-delete { background: var(--danger); }
.contact-row { position: relative; display: flex; align-items: center; gap: 12px; padding: 12px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px; cursor: pointer;
  transition: transform .2s ease; touch-action: pan-y; }
.avatar { width: 42px; height: 42px; border-radius: 50%; background: var(--surface-2); display: grid;
  place-items: center; font-weight: 700; color: var(--accent); flex: none; }
.contact-row .info { flex: 1; min-width: 0; }
.contact-row .info .nm { font-weight: 600; }
.contact-row .info .sub { color: var(--muted); font-size: 0.85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.contact-row .star { color: #f5b14c; font-size: 1.1rem; }
.empty { text-align: center; color: var(--muted); padding: 40px 20px; }

.az-index { position: fixed; right: 2px; top: 50%; transform: translateY(-50%); display: flex;
  flex-direction: column; font-size: 0.62rem; color: var(--muted); z-index: 4; }
.az-index span { padding: 1px 4px; cursor: pointer; }

/* ---- Sheets ---- */
.sheet { position: fixed; inset: 0; background: var(--bg); z-index: 10; overflow-y: auto;
  transform: translateX(100%); transition: transform .22s ease; }
.sheet.active { display: block; transform: translateX(0); }
.sheet-head { position: sticky; top: 0; background: var(--bg); display: flex; align-items: center;
  justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--border);
  padding-top: calc(12px + env(safe-area-inset-top)); }
.sheet-head .back, .sheet-head .save { background: none; border: none; color: var(--accent); font-size: 1rem; cursor: pointer; }
.sheet-head .save { font-weight: 700; }
.sheet-body { padding: 16px 16px 60px; }

/* ---- Contact detail ---- */
.detail-name { font-size: 1.5rem; font-weight: 700; }
.detail-meta { color: var(--muted); margin-bottom: 18px; }
.call-btn { display: flex; align-items: center; justify-content: space-between; width: 100%;
  padding: 16px; border-radius: 14px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); margin-bottom: 10px; cursor: pointer; text-decoration: none; }
.call-btn.primary { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }
.call-btn .lbl { font-size: 0.8rem; opacity: .85; }
.call-btn .num { font-size: 1.3rem; font-weight: 700; letter-spacing: .5px; }
.call-btn.primary .num { font-size: 1.5rem; }
.detail-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 18px; }
.detail-actions button { padding: 12px 4px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); cursor: pointer; font-size: 0.9rem; }
.history { margin-top: 24px; }
.history h4 { color: var(--muted); }
.history-item { font-size: 0.85rem; color: var(--muted); border-left: 2px solid var(--border);
  padding: 4px 10px; margin: 6px 0; }

/* ---- Forms ---- */
.form label { display: block; margin-bottom: 14px; color: var(--muted); font-size: 0.85rem; }
.form input { display: block; width: 100%; margin-top: 5px; padding: 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font-size: 1rem; }
.action { display: block; width: 100%; padding: 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: 1rem; margin-bottom: 10px; cursor: pointer; text-align: center; }
.danger { display: block; width: 100%; padding: 14px; border-radius: 12px; border: 1px solid var(--danger);
  background: transparent; color: var(--danger); font-size: 1rem; margin-top: 16px; cursor: pointer; }
.filepick { display: block; text-align: center; padding: 22px; border: 2px dashed var(--border);
  border-radius: 14px; cursor: pointer; margin: 16px 0; background: var(--surface); }
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; }
.switch-row input { width: 46px; height: 28px; }
hr { border: none; border-top: 1px solid var(--border); margin: 18px 0; }

/* ---- Import review ---- */
.review-group h4 { margin: 18px 0 8px; }
.review-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px; margin-bottom: 8px; }
.review-card .nm { font-weight: 600; }
.diff-line { font-size: 0.88rem; margin: 4px 0; }
.diff-old { color: var(--danger); text-decoration: line-through; }
.diff-new { color: var(--ok); }
.review-card .choices { display: flex; gap: 8px; margin-top: 8px; }
.review-card .choices button { flex: 1; padding: 9px; border-radius: 9px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); cursor: pointer; font-size: 0.85rem; }
.review-card .choices button.sel { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }
.lowconf { color: #f5b14c; font-size: 0.78rem; }
.spinner { display: inline-block; width: 15px; height: 15px; vertical-align: -2px;
  border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%;
  animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
#import-status { margin: 6px 0 4px; min-height: 22px; }

/* ---- Modal / toast ---- */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 20; display: grid; place-items: center; padding: 20px; }
.modal-card { background: var(--surface); border-radius: var(--radius); padding: 20px; width: 100%; max-width: 360px; text-align: center; }
.modal-card .action { margin-top: 10px; }
.modal-card .alt { margin-top: 14px; background: none; border: none; color: var(--muted); cursor: pointer; }
#qr-holder img { width: 240px; max-width: 100%; border-radius: 12px; margin: 12px auto; background:#fff; padding:8px; }
.toast { position: fixed; bottom: calc(20px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
  background: var(--surface-2); color: var(--text); padding: 12px 18px; border-radius: 10px; border: 1px solid var(--border);
  opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 30; max-width: 90%; }
.toast.show { opacity: 1; }
