/* ── Tokens ──────────────────────────────────────────────────────────────── */
:root {
  --bg:          #ffffff;
  --bg-subtle:   #f7f7f7;
  --bg-card:     #ffffff;
  --border:      #e2e2e2;
  --border-strong: #c0c0c0;
  --text:        #0a0a0a;
  --text-muted:  #666666;
  --text-faint:  #999999;
  --accent:      #0a0a0a;
  --accent-inv:  #ffffff;
  --danger:      #cc0000;
  --success:     #007a00;
  --mono:        "JetBrains Mono", "Fira Mono", "Courier New", monospace;
  --sans:        "Inter", system-ui, -apple-system, sans-serif;
  --radius:      4px;
  --nav-h:       56px;
  --max-w:       860px;
  --transition:  0.15s ease;
}

[data-theme="dark"] {
  --bg:          #0d0d0d;
  --bg-subtle:   #161616;
  --bg-card:     #111111;
  --border:      #2a2a2a;
  --border-strong: #444444;
  --text:        #f0f0f0;
  --text-muted:  #aaaaaa;
  --text-faint:  #666666;
  --accent:      #f0f0f0;
  --accent-inv:  #0d0d0d;
  --danger:      #ff5555;
  --success:     #44bb44;
}

/* ── Reset ───────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

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

body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background var(--transition), color var(--transition);
}

main { flex: 1; }

/* ── Typography ──────────────────────────────────────────────────────────── */
h1, h2, h3, h4 {
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.2;
}
h1 { font-size: 2rem; }
h2 { font-size: 1.35rem; }
h3 { font-size: 1.1rem; }

p { color: var(--text); }
p + p { margin-top: 0.9rem; }

a { color: var(--text); text-underline-offset: 3px; }
a:hover { opacity: 0.7; }

code, kbd, pre, .mono {
  font-family: var(--mono);
  font-size: 0.875em;
}

code {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.15em 0.4em;
}

pre {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.2rem;
  overflow-x: auto;
  line-height: 1.5;
}

/* ── Nav ─────────────────────────────────────────────────────────────────── */
.nav {
  position: sticky;
  top: 0;
  z-index: 100;
  isolation: isolate;
  height: var(--nav-h);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 1.5rem;
  gap: 1.5rem;
  transition: background var(--transition);
}

.nav-brand {
  font-family: var(--sans);
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: -0.02em;
  text-decoration: none;
  color: var(--text);
  margin-right: auto;
}
.nav-brand span { opacity: 0.4; }

.nav-links {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  list-style: none;
}

.nav-links a {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-muted);
  text-decoration: none;
  padding: 0.35rem 0.65rem;
  border-radius: var(--radius);
  transition: background var(--transition), color var(--transition);
}
.nav-links a:hover { background: var(--bg-subtle); color: var(--text); opacity: 1; }
.nav-links a.active { color: var(--text); background: var(--bg-subtle); }

/* Theme toggle */
.theme-toggle {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.875rem;
  padding: 0.3rem 0.55rem;
  line-height: 1;
  transition: border-color var(--transition), color var(--transition);
}
.theme-toggle:hover { border-color: var(--border-strong); color: var(--text); }

/* Hamburger button — hidden on desktop */
.nav-hamburger {
  display: none;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text-muted);
  font-size: 1rem;
  padding: 0.3rem 0.55rem;
  line-height: 1;
  transition: border-color var(--transition), color var(--transition);
}
.nav-hamburger:hover { border-color: var(--border-strong); color: var(--text); }

@media (max-width: 640px) {
  .nav-hamburger { display: block; }

  .nav-links {
    display: none;
    position: absolute;
    top: var(--nav-h);
    left: 0;
    right: 0;
    flex-direction: column;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    padding: 0.5rem 1rem 0.75rem;
    gap: 0;
  }

  .nav-links a { display: block; padding: 0.6rem 0.5rem; }

  .nav.nav-open .nav-links { display: flex; }
}

/* ── Layout ──────────────────────────────────────────────────────────────── */
.page {
  max-width: var(--max-w);
  margin: 0 auto;
  padding: 3rem 1.5rem 5rem;
}

.page-narrow {
  max-width: 420px;
  margin: 0 auto;
  padding: 4rem 1.5rem 5rem;
}

/* ── Cards ───────────────────────────────────────────────────────────────── */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.75rem;
}

.card + .card { margin-top: 1rem; }
.card-grid > .card { margin-top: 0; }

.card-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 1.25rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

/* ── Forms ───────────────────────────────────────────────────────────────── */
.form-group { margin-bottom: 1.1rem; }

label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 0.4rem;
}

input[type="text"],
input[type="email"],
input[type="password"] {
  width: 100%;
  padding: 0.6rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--sans);
  font-size: 0.9375rem;
  transition: border-color var(--transition);
  outline: none;
}

input[type="text"]:focus,
input[type="email"]:focus,
input[type="password"]:focus {
  border-color: var(--text);
}

input[type="checkbox"] { accent-color: var(--text); }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 1.1rem;
  border-radius: var(--radius);
  font-family: var(--sans);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: opacity var(--transition), background var(--transition);
  border: 1px solid transparent;
  line-height: 1.4;
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-inv);
  border-color: var(--accent);
}
.btn-primary:hover { opacity: 0.85; color: var(--accent-inv); }

.btn-ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border-strong);
}
.btn-ghost:hover { background: var(--bg-subtle); opacity: 1; }

.btn-danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--danger);
}
.btn-danger:hover { background: var(--danger); color: #fff; opacity: 1; }

.btn-sm { padding: 0.3rem 0.7rem; font-size: 0.8125rem; }
.btn-full { width: 100%; justify-content: center; }

/* ── Alerts ──────────────────────────────────────────────────────────────── */
.alert {
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  font-size: 0.875rem;
  border: 1px solid;
  margin-bottom: 1.25rem;
}
.alert-error  { border-color: var(--danger);  color: var(--danger);  background: color-mix(in srgb, var(--danger) 8%, var(--bg)); }
.alert-success{ border-color: var(--success); color: var(--success); background: color-mix(in srgb, var(--success) 8%, var(--bg)); }

/* ── Table ───────────────────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

th {
  text-align: left;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 0.6rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

td {
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--bg-subtle); }

/* ── Badges ──────────────────────────────────────────────────────────────── */
.badge {
  display: inline-block;
  font-family: var(--mono);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 0.15em 0.5em;
  border-radius: 2px;
  border: 1px solid;
}
.badge-admin { border-color: var(--text); color: var(--text); }
.badge-user  { border-color: var(--border-strong); color: var(--text-muted); }

/* Duplicate detection badges */
.badge-ok   { border-color: var(--success); color: var(--success); }
.badge-warn { border-color: var(--warning, #d97706); color: var(--warning, #d97706); }

/* ── Divider ─────────────────────────────────────────────────────────────── */
.divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2rem 0;
}

/* ── Section header ──────────────────────────────────────────────────────── */
.section-label {
  font-family: var(--mono);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-faint);
  margin-bottom: 0.85rem;
}

/* ── Login page hero ─────────────────────────────────────────────────────── */
.login-hero {
  text-align: center;
  margin-bottom: 2.5rem;
}

.login-hero .wordmark {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}

.login-hero p {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-top: 0.4rem;
}

/* ── Content prose ───────────────────────────────────────────────────────── */
.prose h2 { margin: 2rem 0 0.6rem; }
.prose h3 { margin: 1.5rem 0 0.4rem; }
.prose p  { margin-bottom: 0.9rem; color: var(--text-muted); }
.prose ul { padding-left: 1.4rem; color: var(--text-muted); }
.prose ul li { margin-bottom: 0.3rem; }
.prose pre { margin: 1rem 0; }

/* ── Inline form row ─────────────────────────────────────────────────────── */
.inline-form {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  flex-wrap: wrap;
}
.inline-form .form-group { margin-bottom: 0; flex: 1; min-width: 120px; }

/* ── Utility ─────────────────────────────────────────────────────────────── */
.text-muted  { color: var(--text-muted); }
.text-faint  { color: var(--text-faint); }
.text-mono   { font-family: var(--mono); }
.text-sm     { font-size: 0.875rem; }
.text-xs     { font-size: 0.75rem; }
.mt-1 { margin-top: 0.5rem; }
.mt-2 { margin-top: 1rem; }
.mt-3 { margin-top: 1.5rem; }
.mt-4 { margin-top: 2rem; }
.mb-1 { margin-bottom: 0.5rem; }
.mb-2 { margin-bottom: 1rem; }
.flex { display: flex; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.gap-1 { gap: 0.5rem; }
.gap-2 { gap: 1rem; }

/* ── Contacts ────────────────────────────────────────────────────────────── */
.contact-row {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  padding: 0.7rem 0;
  border-bottom: 1px solid var(--border);
  text-decoration: none;
  color: var(--text);
}
.contact-row:last-child { border-bottom: none; }
.contact-row:hover { opacity: 1; background: var(--bg-subtle); }

.contact-avatar {
  flex-shrink: 0;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.contact-meta { min-width: 0; flex: 1; }
.contact-meta .name { font-weight: 600; }
.contact-meta .sub {
  color: var(--text-muted);
  font-size: 0.8125rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dynamic-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 0.5rem;
}
.dynamic-row select { width: 110px; flex-shrink: 0; }
.dynamic-row input[type="text"] { flex: 1; }

.sync-status {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.8125rem;
  color: var(--text-muted);
}
.sync-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-faint);
  display: inline-block;
  cursor: default;
  flex-shrink: 0;
}
.sync-dot.ok { background: var(--success); }
.sync-dot.error { background: var(--danger); }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  align-items: center;
  margin-bottom: 1.25rem;
}
.toolbar form { display: inline-flex; gap: 0.5rem; align-items: center; }

.toolbar-primary {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  margin-top: 1.5rem;
  margin-bottom: 0.6rem;
}

.toolbar-search {
  flex: 1;
  min-width: 0;
}

/* ── Dialog ───────────────────────────────────────────────────────────────── */
.app-dialog {
  background: var(--bg-card);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.75rem;
  width: min(460px, calc(100vw - 2rem));
  box-shadow: 0 8px 32px rgba(0,0,0,0.18);
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
}

.app-dialog::backdrop {
  background: rgba(0,0,0,0.45);
  backdrop-filter: blur(2px);
}

[data-theme="dark"] .app-dialog {
  box-shadow: 0 8px 32px rgba(0,0,0,0.55);
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
}

.dialog-title {
  margin: 0;
}

.dialog-close {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-faint);
  font-size: 0.9rem;
  padding: 0.2rem 0.4rem;
  border-radius: var(--radius);
  line-height: 1;
  transition: color var(--transition), background var(--transition);
}
.dialog-close:hover { color: var(--text); background: var(--bg-subtle); }

.dialog-section-label {
  font-family: var(--mono);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-faint);
  margin-bottom: 0.5rem;
}

.dialog-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
}

.app-dialog input[type="file"] {
  width: 100%;
  font-size: 0.875rem;
  color: var(--text-muted);
  padding: 0.4rem 0;
}

.ab-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}
.ab-tab {
  padding: 0.35rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-muted);
  text-decoration: none;
  transition: background var(--transition), color var(--transition), border-color var(--transition);
}
.ab-tab:hover { background: var(--bg-subtle); opacity: 1; color: var(--text); }
.ab-tab.active {
  background: var(--text);
  color: var(--accent-inv);
  border-color: var(--text);
}
.ab-tab-manage { border-style: dashed; }

/* ── Select ──────────────────────────────────────────────────────────────── */
select {
  padding: 0.6rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--sans);
  font-size: 0.9375rem;
  outline: none;
  cursor: pointer;
  transition: border-color var(--transition);
}
select:focus { border-color: var(--text); }

.select-sm {
  padding: 0.3rem 0.5rem;
  font-size: 0.8125rem;
}

/* ── Contact entry (expandable list row) ──────────────────────────────────── */
.contact-entry {
  border-bottom: 1px solid var(--border);
}
.contact-entry:last-child { border-bottom: none; }

.contact-row-header {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  padding: 0.7rem 0;
  cursor: pointer;
  color: var(--text);
  text-decoration: none;
  user-select: none;
  border-radius: var(--radius);
  transition: background var(--transition);
}
.contact-row-header:hover { background: var(--bg-subtle); }
.contact-entry.is-open .contact-row-header { background: var(--bg-subtle); border-radius: var(--radius) var(--radius) 0 0; }

.contact-chevron {
  font-size: 0.65rem;
  color: var(--text-faint);
  margin-left: auto;
  flex-shrink: 0;
  padding-right: 0.25rem;
}

/* ── Detail panel ─────────────────────────────────────────────────────────── */
.contact-detail-panel {
  padding: 0.75rem 0 1rem 3.15rem;
}

.detail-row {
  display: flex;
  gap: 1rem;
  padding: 0.3rem 0;
  font-size: 0.875rem;
  align-items: baseline;
}

.detail-label {
  flex-shrink: 0;
  width: 120px;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.detail-value {
  flex: 1;
  color: var(--text);
  line-height: 1.5;
  word-break: break-word;
}
.detail-value a { color: var(--text); }

.detail-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.85rem;
  flex-wrap: wrap;
}

/* ── Inline edit panel ────────────────────────────────────────────────────── */
.contact-edit-panel {
  padding: 0.75rem 0 1.1rem 3.15rem;
}

.edit-name-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.edit-section {
  margin-bottom: 0.85rem;
}
.edit-section .detail-label {
  margin-bottom: 0.35rem;
  display: block;
}

.two-col-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  margin-top: 0.35rem;
}

.edit-textarea {
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.45rem 0.65rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 0.875rem;
  resize: vertical;
  outline: none;
  transition: border-color var(--transition);
}
.edit-textarea:focus { border-color: var(--text); }

/* ── Duplicate detection ─────────────────────────────────────────────────── */
.dup-card {
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-subtle);
}

@media (max-width: 560px) {
  .edit-name-grid  { grid-template-columns: 1fr; }
  .two-col-grid    { grid-template-columns: 1fr; }
  .detail-label    { width: 90px; }
  .contact-detail-panel,
  .contact-edit-panel { padding-left: 0; }
}

/* ── Toolbar dropdown menu ───────────────────────────────────────────────── */
.toolbar-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  min-width: 160px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
  z-index: 200;
  padding: 0.25rem 0;
}

[data-theme="dark"] .toolbar-dropdown {
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}

.toolbar-menu-item {
  display: block;
  width: 100%;
  padding: 0.5rem 0.9rem;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text);
  text-decoration: none;
  background: none;
  border: none;
  cursor: pointer;
  text-align: left;
  font-family: var(--sans);
  transition: background var(--transition);
}
.toolbar-menu-item:hover { background: var(--bg-subtle); opacity: 1; }