/* ── Tokens ─────────────────────────────────────────────────────────────────── */

:root {
  color-scheme: light dark;
  --bg: #f6f6f4;
  --surface: #ffffff;
  --surface-2: #f1f1ee;
  --border: #e6e4df;
  --border-strong: #d6d3cc;
  --text: #1b1a17;
  --muted: #6b6862;
  --subtle: #9a968e;
  --accent: #3b5bdb;
  --accent-soft: #e8edfd;
  --accent-text: #2b46b5;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(20, 18, 12, 0.04), 0 12px 32px rgba(20, 18, 12, 0.10);
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e0f11;
    --surface: #16181b;
    --surface-2: #1d2024;
    --border: #272a2f;
    --border-strong: #363a41;
    --text: #ececea;
    --muted: #9a9ca3;
    --subtle: #6d7078;
    --accent: #8da2ff;
    --accent-soft: #1f2745;
    --accent-text: #a9b9ff;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 12px 32px rgba(0, 0, 0, 0.45);
  }
}

/* ── Base ───────────────────────────────────────────────────────────────────── */

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
a { color: var(--accent-text); text-decoration: none; }
a:hover { text-decoration: underline; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

html:not([data-unlocked]) #app { display: none; }
html[data-unlocked] #gate { display: none; }

/* ── Buttons ────────────────────────────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  height: 34px;
  padding: 0 13px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-weight: 500;
  transition: background 0.12s, border-color 0.12s;
}
.btn:hover { background: var(--surface-2); }
.btn-icon { width: 34px; padding: 0; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-block { width: 100%; height: 40px; }

/* ── Passcode gate ──────────────────────────────────────────────────────────── */

#gate {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg);
  z-index: 100;
}
.gate-card {
  width: 100%;
  max-width: 340px;
  padding: 32px 28px 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  text-align: center;
}
.gate-card.shake { animation: shake 0.35s; }
@keyframes shake {
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}
.gate-icon {
  width: 44px;
  height: 44px;
  margin: 0 auto 14px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: var(--accent-soft);
  color: var(--accent-text);
}
.gate-card h1 { margin: 0 0 4px; font-size: 20px; letter-spacing: -0.01em; }
.gate-sub { margin: 0 0 18px; color: var(--muted); }
.gate-card input {
  width: 100%;
  height: 40px;
  padding: 0 12px;
  margin-bottom: 10px;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  text-align: center;
  font-size: 16px;
}
.gate-error { margin: 12px 0 0; color: #c2410c; font-size: 13px; }
@media (prefers-color-scheme: dark) { .gate-error { color: #fb923c; } }

/* ── Top bar ────────────────────────────────────────────────────────────────── */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
}
.topbar-inner {
  max-width: 1240px;
  margin: 0 auto;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.brand { display: flex; align-items: baseline; gap: 12px; min-width: 0; }
.brand h1 { margin: 0; font-size: 20px; font-weight: 650; letter-spacing: -0.015em; }
.count { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.topbar-actions { display: flex; gap: 8px; flex-shrink: 0; }

/* ── Layout ─────────────────────────────────────────────────────────────────── */

.container { max-width: 1240px; margin: 0 auto; padding: 20px 24px 48px; }

.controls { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }

.search { position: relative; flex: 1 1 260px; min-width: 0; }
.search svg { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--subtle); pointer-events: none; }
.search input {
  width: 100%;
  height: 38px;
  padding: 0 12px 0 36px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  outline: none;
}
.search input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.search input::placeholder { color: var(--subtle); }

.seg {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.seg button {
  height: 30px;
  padding: 0 12px;
  background: none;
  border: 0;
  border-radius: 7px;
  color: var(--muted);
  font-weight: 500;
  white-space: nowrap;
}
.seg button:hover { color: var(--text); }
.seg button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08), 0 0 0 1px var(--border);
}

.filters { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 12px 0 16px; }

.field {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 4px 0 11px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  font-size: 13px;
}
.field select {
  max-width: 170px;
  height: 28px;
  padding: 0 6px;
  background: transparent;
  border: 0;
  color: var(--text);
  font-weight: 500;
  font-size: 13px;
  outline: none;
  cursor: pointer;
}
.field:focus-within { border-color: var(--accent); }

.chip-toggle { position: relative; display: inline-flex; }
.chip-toggle input { position: absolute; opacity: 0; inset: 0; cursor: pointer; margin: 0; }
.chip-toggle span {
  display: inline-flex;
  align-items: center;
  height: 32px;
  padding: 0 13px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
}
.chip-toggle input:checked + span { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-text); }
.chip-toggle input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ── Table ──────────────────────────────────────────────────────────────────── */

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
table { width: 100%; border-collapse: collapse; table-layout: fixed; }
thead th {
  padding: 11px 16px;
  text-align: left;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--subtle);
  white-space: nowrap;
  border-bottom: 1px solid var(--border);
}
td {
  padding: 13px 16px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  vertical-align: top;
}
.c-name { width: 20%; }
.c-firm { width: 14%; }
.c-role { width: 14%; }
.c-when { width: 136px; }
.by-firm .c-firm { display: none; }
tbody tr.row { cursor: pointer; }
tbody tr.row:hover { background: var(--surface-2); }
tbody tr.row.is-open { background: var(--accent-soft); }
tbody tr.row:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
tbody tr:last-child td { border-bottom: 0; }

td.c-name { font-weight: 600; white-space: normal; overflow-wrap: anywhere; }
td.c-role, td.c-note { color: var(--muted); }
td.c-when { color: var(--muted); font-variant-numeric: tabular-nums; }
td.c-note { white-space: normal; overflow: visible; text-overflow: clip; line-height: 1.5; }

.note-text {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: var(--note-lines, 3);
  overflow: hidden;
}
#app { --note-lines: 3; --card-lines: 4; }
#app.notes-short { --note-lines: 1; --card-lines: 2; }
#app.notes-full { --note-lines: none; --card-lines: none; }
#app.notes-full .note-text,
#app.notes-full .card-note { display: block; overflow: visible; white-space: pre-wrap; }

.none { color: var(--subtle); }

.tag {
  display: inline-block;
  margin-left: 8px;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-text);
  font-size: 11px;
  font-weight: 600;
  line-height: 1.5;
  vertical-align: 1px;
  white-space: nowrap;
}
.new-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-left: 8px;
  border-radius: 50%;
  background: var(--accent);
  vertical-align: 2px;
}

.group-row th {
  padding: 0;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  border-top: 1px solid var(--border);
  text-align: left;
  font-weight: 600;
}
tbody tr.group-row:first-child th { border-top: 0; }
.group-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 16px;
  background: none;
  border: 0;
  text-align: left;
  font-weight: 600;
}
.group-toggle svg { color: var(--subtle); transition: transform 0.15s; flex-shrink: 0; }
.group-toggle[aria-expanded="false"] svg { transform: rotate(-90deg); }
.group-toggle .n { color: var(--muted); font-weight: 500; font-variant-numeric: tabular-nums; }

.empty { padding: 56px 16px; text-align: center; color: var(--muted); }
.page-note { margin-top: 20px; text-align: center; color: var(--subtle); font-size: 12.5px; }

/* ── Cards (mobile) ─────────────────────────────────────────────────────────── */

#card-list { display: none; }
.card {
  padding: 14px 16px;
  margin-bottom: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.card:active { background: var(--surface-2); }
.card-top { display: flex; align-items: baseline; gap: 8px; }
.card-name { flex: 1; min-width: 0; font-weight: 600; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-when { color: var(--muted); font-size: 12.5px; white-space: nowrap; }
.card-sub { margin-top: 2px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-note {
  margin: 8px 0 0;
  color: var(--muted);
  display: -webkit-box;
  -webkit-line-clamp: var(--card-lines, 4);
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.group-head { margin: 18px 0 10px; }
.group-head:first-child { margin-top: 4px; }
.group-head .group-toggle { padding: 4px 2px; }

/* ── Detail panel ───────────────────────────────────────────────────────────── */

#backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 14, 10, 0.32);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  z-index: 30;
}
#backdrop.open { opacity: 1; pointer-events: auto; }

#detail {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(460px, 100vw);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow);
  transform: translateX(100%);
  visibility: hidden;
  transition: transform 0.22s ease, visibility 0.22s;
  z-index: 40;
}
#detail.open { transform: none; visibility: visible; }

.detail-bar { display: flex; justify-content: flex-end; padding: 12px 14px 0; padding-top: calc(12px + env(safe-area-inset-top)); }
.detail-body { flex: 1; overflow-y: auto; padding: 4px 24px 32px; padding-bottom: calc(32px + env(safe-area-inset-bottom)); }

.detail-body h2 { margin: 0; font-size: 22px; font-weight: 650; letter-spacing: -0.015em; line-height: 1.25; }
.detail-body h2 .tag { font-size: 12px; vertical-align: 3px; }
.detail-sub { margin: 4px 0 0; color: var(--muted); font-size: 15px; }
.detail-office { margin: 2px 0 0; color: var(--subtle); }

dl.facts { display: grid; grid-template-columns: 104px 1fr; gap: 12px 16px; margin: 24px 0 0; }
dl.facts dt { color: var(--muted); font-size: 13px; padding-top: 1px; }
dl.facts dd { margin: 0; overflow-wrap: anywhere; }
dl.facts .sub { display: block; color: var(--subtle); font-size: 12.5px; }

.detail-body h3 {
  margin: 28px 0 10px;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--subtle);
}
.notes {
  padding: 14px 16px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.6;
}

/* ── Toast ──────────────────────────────────────────────────────────────────── */

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translate(-50%, 8px);
  padding: 9px 16px;
  background: var(--text);
  color: var(--bg);
  border-radius: 10px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 200;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ── Mobile ─────────────────────────────────────────────────────────────────── */

@media (max-width: 720px) {
  .topbar-inner { padding: 12px 16px; }
  .container { padding: 14px 16px 40px; }
  .brand { gap: 8px; }
  .brand h1 { font-size: 18px; }
  .btn span { display: none; }
  #export-btn { width: 34px; padding: 0; }

  .table-wrap { display: none; }
  #card-list { display: block; }

  .controls { gap: 10px; }
  .search { flex-basis: 100%; }
  .seg { width: 100%; }
  .seg button { flex: 1; padding: 0 6px; }

  .filters {
    flex-wrap: nowrap;
    overflow-x: auto;
    margin-left: -16px;
    margin-right: -16px;
    padding: 0 16px 4px;
    scrollbar-width: none;
  }
  .filters::-webkit-scrollbar { display: none; }
  .field, .chip-toggle { flex-shrink: 0; }
  .field select { max-width: 120px; }

  #detail {
    top: auto;
    width: 100%;
    height: 92dvh;
    height: 92vh;
    border-left: 0;
    border-top: 1px solid var(--border);
    border-radius: 18px 18px 0 0;
    transform: translateY(100%);
  }
  #detail.open { transform: none; }
  .detail-body { padding: 0 20px 28px; }
  dl.facts { grid-template-columns: 92px 1fr; }
}

@supports (height: 1dvh) {
  @media (max-width: 720px) { #detail { height: 92dvh; } }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

/* ── Sorting ────────────────────────────────────────────────────────────────── */

.th-sort {
  padding: 0;
  background: none;
  border: 0;
  color: inherit;
  font: inherit;
  letter-spacing: inherit;
  text-transform: inherit;
  cursor: pointer;
}
.th-sort:hover, th.sorted .th-sort { color: var(--text); }

.dir-btn {
  width: 26px;
  height: 26px;
  margin-left: -2px;
  background: transparent;
  border: 0;
  border-radius: 50%;
  color: var(--text);
  font-size: 15px;
  line-height: 1;
}
.dir-btn:hover { background: var(--surface-2); }
