:root {
  --bg: oklch(0.165 0.005 70);
  --shell: oklch(0.19 0.006 70);
  --raised: oklch(0.225 0.007 70);
  --hover: oklch(0.25 0.008 72);
  --line: oklch(0.30 0.008 72);
  --line-strong: oklch(0.40 0.01 75);
  --text: oklch(0.93 0.008 80);
  --muted: oklch(0.72 0.012 78);
  --faint: oklch(0.60 0.012 78);
  --brass: oklch(0.80 0.095 82);
  --brass-dim: oklch(0.80 0.095 82 / 0.16);
  --brass-ink: oklch(0.21 0.03 80);
  --danger: oklch(0.70 0.15 28);
  --danger-dim: oklch(0.70 0.15 28 / 0.14);
  --ok: oklch(0.78 0.11 150);

  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "DejaVu Sans Mono", Menlo, monospace;
  --r: 6px;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  caret-color: var(--brass);
  scrollbar-color: var(--line-strong) transparent;
}
::selection { background: oklch(0.80 0.095 82 / 0.35); color: var(--text); }
[hidden] { display: none !important; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }

h1, h2, h3 { margin: 0; font-weight: 600; text-wrap: balance; }
h2 { font-size: 1.125rem; }
h3 { font-size: 0.9375rem; }
a { color: var(--brass); text-underline-offset: 3px; }
hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; width: 100%; }
code, .num, kbd { font-variant-numeric: tabular-nums; }
code { font-family: var(--mono); font-size: 0.8125rem; word-break: break-all; }
em { font-style: normal; color: var(--text); }

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

/* Controls ----------------------------------------------------------------- */

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

input, select, textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: 7px 10px;
  min-height: 34px;
  transition: border-color 150ms var(--ease), background-color 150ms var(--ease);
}
input:hover, select:hover, textarea:hover { border-color: var(--line-strong); }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--brass); background: oklch(0.15 0.005 70); }
input::placeholder, textarea::placeholder { color: var(--faint); }
input[type="search"]::-webkit-search-cancel-button { filter: grayscale(1); opacity: 0.5; cursor: pointer; }
textarea { resize: vertical; line-height: 1.5; }
select { appearance: none; padding-right: 26px; cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: right 13px center, right 9px center; background-size: 4px 4px; background-repeat: no-repeat; }

label { display: grid; gap: 5px; color: var(--muted); font-size: 0.8125rem; }
label > input, label > select, label > textarea { color: var(--text); font-size: 0.875rem; }
label small { color: var(--faint); font-size: 0.75rem; }

button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 32px; padding: 0 12px;
  border: 1px solid var(--line); border-radius: var(--r);
  background: var(--raised); cursor: pointer; white-space: nowrap;
  transition: background-color 150ms var(--ease), border-color 150ms var(--ease), color 150ms var(--ease);
}
button:hover { background: var(--hover); border-color: var(--line-strong); }
button:active { transform: translateY(1px); }
button:disabled { opacity: 0.5; cursor: default; transform: none; }
button[aria-busy="true"] { cursor: progress; opacity: 0.7; }
button.primary { background: var(--brass); border-color: var(--brass); color: var(--brass-ink); font-weight: 600; }
button.primary:hover { background: oklch(0.85 0.09 84); }
button.ghost { background: transparent; border-color: transparent; color: var(--muted); }
button.ghost:hover { color: var(--text); background: var(--raised); }
button.danger { background: transparent; border-color: transparent; color: var(--danger); }
button.danger:hover { background: var(--danger-dim); }
button.danger.armed { background: var(--danger); border-color: var(--danger); color: oklch(0.18 0.02 28); font-weight: 600; }
button.icon { width: 32px; padding: 0; background: transparent; border-color: transparent; color: var(--faint); flex: none; }
button.icon:hover { color: var(--text); background: var(--raised); }
svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; }

.stack { display: flex; flex-direction: column; gap: 14px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.err { margin: 0; color: var(--danger); min-height: 0; font-size: 0.8125rem; }
.err:empty { display: none; }
.hint, .lede, .meta { margin: 0; color: var(--muted); font-size: 0.8125rem; max-width: 65ch; }
.lede { font-size: 0.875rem; }
.meta { color: var(--faint); font-size: 0.75rem; }

.wordmark { font-size: 1.375rem; font-weight: 600; letter-spacing: 0.01em; }
.wordmark.small { font-size: 0.9375rem; flex: none; }

/* Gate (setup, unlock, sign-in) ---------------------------------------------- */

.gate { min-height: 100%; display: grid; place-items: center; padding: 48px 16px; }
.gate-box { width: min(100%, 360px); display: flex; flex-direction: column; gap: 24px; }
.gate form { animation: rise 260ms var(--ease) both; }
.gate button.primary { min-height: 38px; }
.qr { width: 184px; height: 184px; border-radius: var(--r); background: #fff; padding: 6px; align-self: start; }
.secret summary { color: var(--muted); font-size: 0.8125rem; cursor: pointer; }
.secret code { display: block; margin-top: 6px; padding: 8px 10px; background: var(--shell); border-radius: var(--r); user-select: all; }

@keyframes rise { from { opacity: 0; transform: translateY(6px); } }

/* App shell -------------------------------------------------------------------- */

.app { height: 100%; display: grid; grid-template-rows: auto auto 1fr; grid-template-columns: minmax(0, 1fr); }

.bar {
  display: flex; align-items: center; gap: 16px;
  padding: 10px 16px; background: var(--shell); border-bottom: 1px solid var(--line);
}
.search { position: relative; flex: 1; max-width: 520px; display: block; }
.search svg { position: absolute; left: 10px; top: 50%; translate: 0 -50%; color: var(--faint); pointer-events: none; }
.search input { padding-left: 34px; padding-right: 34px; background: var(--bg); }
.search kbd { position: absolute; right: 8px; top: 50%; translate: 0 -50%; font: 0.6875rem var(--mono); color: var(--faint);
  border: 1px solid var(--line); border-radius: 4px; padding: 1px 6px; pointer-events: none; }
.search input:focus + kbd, .search input:not(:placeholder-shown) + kbd { display: none; }
.actions { display: flex; gap: 6px; margin-left: auto; align-items: center; flex-wrap: wrap; justify-content: flex-end; min-width: 0; }
#b-me { color: var(--muted); }

.menu { position: relative; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 20; min-width: 200px;
  display: flex; flex-direction: column; padding: 4px;
  background: var(--raised); border: 1px solid var(--line-strong); border-radius: var(--r);
  box-shadow: 0 8px 24px oklch(0 0 0 / 0.45);
  animation: drop 160ms var(--ease) both; transform-origin: top right;
}
.menu-list a { color: var(--text); text-decoration: none; padding: 7px 10px; border-radius: 4px; }
.menu-list a:hover, .menu-list a:focus-visible { background: var(--hover); }
@keyframes drop { from { opacity: 0; transform: scale(0.97) translateY(-4px); } }

.filters {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 8px 16px 8px 4px; border-bottom: 1px solid var(--line); min-height: 49px;
}
.filter:first-child { margin-left: 12px; }
.filter-list { display: contents; }
.filter {
  display: flex; align-items: center; gap: 4px; padding: 3px 3px 3px 4px;
  background: var(--shell); border: 1px solid var(--line); border-radius: var(--r);
}
.filter select, .filter input { width: auto; min-height: 28px; padding-top: 3px; padding-bottom: 3px; font-size: 0.8125rem; }
.filter .ff { max-width: 160px; color: var(--brass); }
.filter .fv { width: 140px; }
.filter button.icon { width: 28px; min-height: 28px; }
.count { margin-left: auto; color: var(--faint); font-size: 0.8125rem; font-variant-numeric: tabular-nums; }

.work { display: grid; grid-template-columns: minmax(0, 1fr); min-height: 0; min-width: 0; }
.work.split { grid-template-columns: minmax(0, 1fr) min(440px, 42vw); }
.list { overflow: auto; min-height: 0; min-width: 0; }

/* Table -------------------------------------------------------------------------- */

table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 9px 16px; border-bottom: 1px solid var(--line); vertical-align: top; }
th {
  position: sticky; top: 0; z-index: 1; background: var(--bg);
  color: var(--faint); font-weight: 500; font-size: 0.75rem; white-space: nowrap;
  cursor: pointer; user-select: none; padding-top: 10px; padding-bottom: 8px;
}
th:hover { color: var(--text); }
th[aria-sort] { color: var(--text); }
th[aria-sort="ascending"]::after { content: ""; display: inline-block; margin-left: 6px; border: 4px solid transparent; border-bottom-color: currentColor; translate: 0 -2px; }
th[aria-sort="descending"]::after { content: ""; display: inline-block; margin-left: 6px; border: 4px solid transparent; border-top-color: currentColor; translate: 0 2px; }
td { max-width: 280px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
td.name { font-weight: 500; }
td.dim { color: var(--muted); }
td.faint { color: var(--faint); }
td .extra { color: var(--faint); font-size: 0.8125rem; }
tbody tr { cursor: pointer; transition: background-color 120ms var(--ease); }
tbody tr:hover { background: var(--shell); }
tbody tr[aria-selected="true"] { background: var(--raised); }
tbody tr[aria-selected="true"] td.name { color: var(--brass); }
tbody tr:focus-visible { outline-offset: -2px; }

/* The search signature: every match glints brass, in every column. */
mark { background: var(--brass-dim); color: var(--brass); border-radius: 2px; padding: 0 1px; }

.empty { padding: 56px 16px; max-width: 46ch; margin: 0 auto; color: var(--muted); display: grid; gap: 12px; justify-items: start; }
.empty h2 { color: var(--text); }
.empty p { margin: 0; }

/* Side panel ------------------------------------------------------------------- */

.panel {
  border-left: 1px solid var(--line); background: var(--shell);
  overflow: auto; min-height: 0; padding: 16px 20px 32px;
  animation: slide 220ms var(--ease) both;
}
@keyframes slide { from { opacity: 0; transform: translateX(12px); } }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 32px; }
.panel-head h2 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.view { gap: 16px; }

fieldset { border: 0; padding: 0; margin: 0; display: grid; gap: 8px; min-width: 0; }
legend { color: var(--muted); font-size: 0.8125rem; padding: 0; margin-bottom: 5px; }
.kv-list { display: grid; gap: 6px; }
.kv { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 3fr) 32px; gap: 6px; align-items: center; }
.kv .k { color: var(--brass); }
.kv-suggest { display: flex; flex-wrap: wrap; gap: 6px; }
.kv-suggest:empty { display: none; }
.kv-suggest button { min-height: 26px; padding: 0 9px; font-size: 0.75rem; color: var(--muted); background: transparent; border-style: dashed; }
.kv-suggest button:hover { color: var(--text); border-style: solid; }
.add-kv { justify-self: start; }

.notes { display: grid; gap: 10px; padding-top: 16px; border-top: 1px solid var(--line); }
.note-new { display: grid; gap: 8px; }
.note-new button { justify-self: start; }
.note-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.note-list li { padding: 10px 0; border-bottom: 1px solid var(--line); display: grid; gap: 4px; }
.note-list li:last-child { border-bottom: 0; }
.note-body { white-space: pre-wrap; overflow-wrap: anywhere; }
.note-meta { display: flex; align-items: center; justify-content: space-between; color: var(--faint); font-size: 0.75rem; }
.note-meta button { min-height: 24px; padding: 0 6px; font-size: 0.75rem; }

.field-list, .people-list { list-style: none; margin: 0; padding: 0; display: grid; }
.field-list li, .people-list li { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.field-list input.rename { flex: 1; min-height: 30px; }
.field-list .used { color: var(--faint); font-size: 0.75rem; width: 64px; text-align: right; font-variant-numeric: tabular-nums; flex: none; }
.toggle { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 0.75rem; cursor: pointer; flex: none; }
.toggle input { width: 14px; height: 14px; min-height: 0; padding: 0; accent-color: var(--brass); }
.people-list .who { flex: 1; display: grid; }
.people-list .who small { color: var(--faint); font-size: 0.75rem; }
.field-new input { flex: 1; width: auto; }

/* Toast ---------------------------------------------------------------------------- */

.toast {
  position: fixed; left: 50%; bottom: 20px; translate: -50% 0; z-index: 50;
  padding: 9px 14px; border-radius: var(--r);
  background: var(--raised); border: 1px solid var(--line-strong); color: var(--text);
  box-shadow: 0 8px 24px oklch(0 0 0 / 0.45);
  animation: rise 200ms var(--ease) both;
}

/* Narrow screens ------------------------------------------------------------------------ */

@media (min-width: 701px) and (max-width: 960px) {
  .work.split { grid-template-columns: minmax(0, 1fr) min(400px, 50vw); }
  .bar { flex-wrap: wrap; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .actions { flex-wrap: wrap; }
}
}

/* Pieces that only phones use -------------------------------------------------------------- */

.count-m, .close-m, .save-m { display: none; }
.mbar { display: none !important; }

.sheet {
  margin: auto auto 0; width: 100%; max-width: 520px; padding: 8px 12px calc(12px + env(safe-area-inset-bottom));
  border: 0; border-top: 1px solid var(--line-strong); border-radius: 14px 14px 0 0;
  background: var(--raised); color: var(--text);
}
.sheet[open] { animation: sheet-up 240ms var(--ease) both; }
.sheet::backdrop { background: oklch(0 0 0 / 0.55); animation: fade 240ms var(--ease) both; }
.sheet-list { display: grid; margin-bottom: 8px; }
.sheet-list > * {
  display: flex; align-items: center; justify-content: flex-start; min-height: 52px; padding: 0 12px;
  border: 0; border-bottom: 1px solid var(--line); border-radius: 0; background: transparent;
  color: var(--text); text-decoration: none; font-size: 1rem;
}
.sheet-list > *:last-child { border-bottom: 0; }
.sheet-list .danger-text { color: var(--danger); }
.sheet-close { width: 100%; min-height: 50px; font-size: 1rem; font-weight: 600; background: var(--hover); }
@keyframes sheet-up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }

/* Phone ------------------------------------------------------------------------------------------- */

@media (max-width: 700px) {
  body { font-size: 15px; -webkit-tap-highlight-color: transparent; }
  /* 16px keeps iOS Safari from zooming into a focused field. */
  input, select, textarea { font-size: 16px; min-height: 44px; }
  label > input, label > select, label > textarea { font-size: 16px; }
  button { min-height: 44px; }
  button:active { transform: none; background: var(--hover); }
  label { font-size: 0.875rem; }

  .gate { padding: calc(32px + env(safe-area-inset-top)) 20px calc(32px + env(safe-area-inset-bottom)); align-items: start; }
  .gate-box { gap: 20px; }
  .gate button.primary { min-height: 48px; }

  .app { display: block; height: auto; min-height: 100dvh; padding-bottom: calc(76px + env(safe-area-inset-bottom)); }

  .bar {
    position: sticky; top: 0; z-index: 10; flex-wrap: wrap; gap: 10px 12px;
    padding: calc(10px + env(safe-area-inset-top)) 16px 12px;
  }
  .wordmark.small { font-size: 1.0625rem; }
  .count-m { display: block; margin-left: auto; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .search input { min-height: 44px; }
  .search kbd { display: none; }
  .actions { display: none; }

  .filters { padding: 12px 16px; min-height: 0; gap: 8px; }
  .filters:not(:has(.filter)) { display: none; }
  .filters > #b-filter, .filters > .count { display: none; }
  .filter { width: 100%; margin-left: 0 !important; padding: 4px; }
  .filter .ff { flex: 1 1 0; max-width: none; min-width: 0; }
  .filter .fo { flex: 1 1 0; min-width: 0; }
  .filter .fv { flex: 1 1 100%; width: auto; order: 3; }
  .filter { flex-wrap: wrap; }
  .filter select, .filter input { min-height: 40px; font-size: 16px; }
  .filter button.icon { width: 40px; min-height: 40px; }

  .work, .work.split { display: block; }
  .work.split .list { display: block; }
  .list { overflow: visible; }

  /* The table becomes a list of contacts. */
  #grid thead { display: none; }
  #grid, #grid tbody, #grid tr, #grid td { display: block; }
  #grid tr {
    display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 14px;
    padding: 13px 16px 14px; border-bottom: 1px solid var(--line);
  }
  #grid tr:active { background: var(--shell); }
  #grid td { padding: 0; border: 0; max-width: 100%; white-space: normal; overflow: visible; font-size: 0.875rem; color: var(--muted); }
  #grid td:empty, #grid td.c-updated { display: none; }
  #grid td.c-name { flex: 1 1 100%; font-size: 1.0625rem; color: var(--text); }
  #grid td.c-name .extra { margin-top: 2px; }
  #grid td.c-notes::after { content: attr(data-unit); }
  #grid td[class*=" c-f"]::before { content: attr(data-label) " "; color: var(--faint); }

  .empty { padding: 40px 20px; }

  /* Editors take the whole screen, with Save in reach at the top. */
  /* In normal flow, not position: fixed: iOS Safari shifts fixed overlays
     when the keyboard opens, so the editor replaces the list instead. */
  .panel {
    border: 0; min-height: 100dvh; overflow: visible;
    padding: 0 16px calc(32px + env(safe-area-inset-bottom));
    animation: fade 160ms var(--ease) both;
  }
  body.sheet-open .bar, body.sheet-open .filters, body.sheet-open .work .list { display: none; }
  body.sheet-open .app { padding-bottom: 0; }
  .panel-head {
    position: sticky; top: 0; z-index: 2; margin: 0 -16px; padding: calc(8px + env(safe-area-inset-top)) 8px 8px;
    background: var(--shell); border-bottom: 1px solid var(--line);
  }
  .panel-head h2 { flex: 1; text-align: center; font-size: 1rem; }
  #v-contact .close { display: none; }
  #v-contact .close-m, #v-contact .save-m { display: inline-flex; min-width: 76px; }
  #v-contact .row button[type=submit], #v-contact .row [data-close] { display: none; }
  #v-contact .row { justify-content: center; padding-top: 4px; }
  .view { gap: 18px; padding-top: 4px; }

  .kv { grid-template-columns: minmax(0, 1fr) 44px; grid-template-areas: "k rm" "v v"; gap: 6px; padding-bottom: 10px; }
  .kv .k { grid-area: k; min-height: 40px; font-size: 15px; background: transparent; border-color: transparent; padding-left: 2px; }
  .kv .k:focus { border-color: var(--brass); }
  .kv .v { grid-area: v; }
  .kv .rm { grid-area: rm; width: 44px; min-height: 40px; }
  .kv-suggest button { min-height: 36px; padding: 0 12px; font-size: 0.875rem; }
  .add-kv { min-height: 44px; }

  .note-new button { min-height: 44px; }
  .note-meta button { min-height: 36px; padding: 0 10px; }

  .field-list li { flex-wrap: wrap; gap: 8px 12px; padding: 12px 0; }
  .field-list input.rename { flex: 1 1 100%; }
  .field-list .used { width: auto; text-align: left; flex: 1; }
  .field-new input { min-width: 0; }
  .people-list li { flex-wrap: wrap; }
  .people-list .who { flex: 1 1 100%; }

  .mbar {
    display: grid !important; grid-template-columns: 1fr 1.6fr 1fr; gap: 8px;
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
    background: var(--shell); border-top: 1px solid var(--line);
  }
  .mbar[hidden], body.sheet-open .mbar { display: none !important; }
  .mbar button { min-height: 48px; font-size: 0.9375rem; }

  .toast { bottom: calc(84px + env(safe-area-inset-bottom)); max-width: calc(100% - 32px); }
}

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