:root {
  --bg: #000;
  --surface: #09090b;
  --s2: #111114;
  --s3: #18181c;
  --line: #1f1f24;
  --line2: #2c2c33;
  --text: #f2f2f4;
  --muted: #a1a1aa;
  --faint: #63636d;
  --accent: #8a5cf5;
  --accent-text: #c9b6ff;
  --accent-dim: #4b3596;
  --ok: #4ade80;
  --warn: #e0ac00;
  --danger: #ff6b6b;
  --radius: 8px;
  --radius-lg: 12px;
  --drawer: 400px;
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, system-ui, "Segoe UI", Roboto, Inter, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, monospace;
  --ease: cubic-bezier(0.2, 0, 0, 1);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.5 var(--font); -webkit-font-smoothing: antialiased; overflow: hidden; overscroll-behavior: none; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }
h1, h2, p { margin: 0; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sky { position: fixed; inset: 0; width: 100%; height: 100%; z-index: -1; display: block; }
.muted { color: var(--muted); }

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 32px; padding: 0 12px; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--s2);
  font-size: 13px; font-weight: 500; white-space: nowrap; cursor: pointer;
  transition: background 150ms var(--ease), border-color 150ms var(--ease), color 150ms var(--ease);
}
.btn:hover { background: var(--s3); border-color: var(--line2); }
.btnWhite { background: #fff; border-color: #fff; color: #000; }
.btnWhite:hover { background: #e4e4e7; border-color: #e4e4e7; }
.btnPrimary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btnPrimary:hover { background: #7b4ee8; border-color: #7b4ee8; }
.btnGhost { background: transparent; border-color: transparent; color: var(--muted); }
.btnGhost:hover { background: var(--s2); border-color: transparent; color: var(--text); }
.btnDanger { color: var(--danger); }
.btnDanger:hover { color: var(--danger); background: rgba(255, 107, 107, 0.08); }
.btnIcon { width: 32px; padding: 0; }
.linkBtn { border: 0; background: none; padding: 0; color: var(--muted); font-size: 12px; cursor: pointer; }
.linkBtn:hover { color: var(--text); }

input, textarea, select {
  width: 100%; font: inherit; font-size: 14px; color: var(--text);
  background: var(--s2); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 8px 12px; transition: border-color 150ms var(--ease);
}
input::placeholder, textarea::placeholder { color: var(--faint); }
input:hover, textarea:hover { border-color: var(--line2); }
input:focus, textarea:focus { outline: none; border-color: var(--accent); }
textarea { resize: vertical; line-height: 1.55; }

.field { display: grid; gap: 6px; font-size: 12px; color: var(--muted); }
.fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
.fieldset legend { padding: 0; margin-bottom: 6px; }
.saveState { font-size: 11.5px; color: var(--faint); }
.error { color: var(--danger); font-size: 13px; min-height: 1.2em; }

/* add-dialog status chips */
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  position: relative; display: inline-flex; align-items: center; height: 30px; padding: 0 11px;
  border-radius: var(--radius); border: 1px solid var(--line2); color: var(--muted); font-size: 13px; cursor: pointer;
}
.chip input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.chip:has(input:checked) { background: rgba(138, 92, 245, 0.15); border-color: var(--accent); color: var(--accent-text); }
.chip:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- login ---------- */
.login { height: 100%; display: grid; place-items: center; padding: 16px; }
.loginBox { width: min(320px, 100%); display: grid; gap: 10px; }
.loginBox .brand { font-size: 20px; margin-bottom: 12px; }
.loginBox .btn { height: 40px; margin-top: 6px; }

/* ---------- header ---------- */
.app, .graph { position: fixed; inset: 0; }
.graph { touch-action: none; }
.bar {
  position: fixed; top: 0; left: 0; right: 0; height: 76px; z-index: 2;
  display: flex; align-items: center; gap: 16px; padding: 0 16px 0 20px;
  background: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0));
  transition: right 200ms var(--ease);
}
body.hasCard .bar { right: var(--drawer); }
.brand { font-size: 14px; font-weight: 600; letter-spacing: -0.01em; white-space: nowrap; }

.viewNav { display: flex; margin: 0 auto; }
.nimbus-count { margin-left: 6px; opacity: 0.55; font-size: 12.5px; font-variant-numeric: tabular-nums; }
.nimbus-item[aria-current="page"] .nimbus-count { opacity: 0.8; }

/* the other header controls share the water pill's glass */
.bar .btn:not(.btnWhite), .bar .search input {
  height: 42px; border: 0; border-radius: 999px; background: rgba(10, 18, 40, 0.26);
  box-shadow: inset 0 1px 1px rgba(255, 255, 255, 0.34), inset 0 0 0 1px rgba(255, 255, 255, 0.14), 0 12px 32px -14px rgba(0, 40, 90, 0.5);
  -webkit-backdrop-filter: blur(10px) saturate(1.2) brightness(1.08); backdrop-filter: blur(10px) saturate(1.2) brightness(1.08);
  color: rgba(241, 247, 255, 0.85);
}
.bar .btn:not(.btnWhite):hover { background: rgba(40, 52, 80, 0.4); color: #f1f7ff; }
.bar .btn { padding: 0 16px; }
.bar .btnWhite { height: 42px; border-radius: 999px; padding: 0 18px; }
.bar .search input { padding-left: 36px; }
.bar .searchIcon { left: 14px; z-index: 1; }
.bar .kbd { z-index: 1; }
.bar .kbd { right: 12px; border-color: rgba(255, 255, 255, 0.18); }
.tools { display: flex; align-items: center; gap: 8px; }
.search { position: relative; }
.searchIcon { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--faint); pointer-events: none; }
.search input { width: 220px; height: 32px; padding: 0 48px 0 32px; font-size: 13px; }
.kbd { position: absolute; right: 8px; top: 50%; transform: translateY(-50%); font: 11px var(--mono); color: var(--muted); border: 1px solid var(--line2); border-radius: 4px; padding: 1px 5px; pointer-events: none; }
.search input:focus + .kbd { display: none; }
.searchResults {
  position: absolute; right: 0; top: calc(100% + 6px); width: 300px; margin: 0; padding: 4px; list-style: none; z-index: 5;
  background: var(--surface); border: 1px solid var(--line2); border-radius: var(--radius-lg);
}
.searchResults button {
  width: 100%; min-height: 38px; display: flex; align-items: center; gap: 10px; text-align: left;
  border: 0; background: none; padding: 8px 10px; border-radius: var(--radius); cursor: pointer; font-size: 13.5px;
}
.searchResults button:hover, .searchResults button:focus-visible { background: var(--s2); }
.searchResults i, .legend i { width: 8px; height: 8px; border-radius: 50%; flex: none; display: inline-block; }
.searchResults small { margin-left: auto; color: var(--faint); font-size: 12px; }
.waDot { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); }
.waBtn[data-state="open"] .waDot { background: var(--ok); }

/* ---------- legend / status ---------- */
.legend {
  position: fixed; left: 20px; bottom: max(16px, env(safe-area-inset-bottom));
  display: flex; flex-wrap: wrap; gap: 4px 16px; margin: 0; padding: 0; list-style: none;
  color: var(--muted); font-size: 12px; pointer-events: none;
}
.legend li { display: flex; align-items: center; gap: 6px; }
.legend i.ring { background: none; border: 1.5px dashed var(--muted); width: 9px; height: 9px; }
.live { position: fixed; left: 8px; bottom: max(20px, env(safe-area-inset-bottom)); width: 5px; height: 5px; border-radius: 50%; background: var(--faint); }
.live.on { background: var(--ok); }
.empty { position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); color: var(--muted); text-align: center; padding: 0 24px; width: min(360px, 100%); }

/* ---------- drawer ---------- */
.card {
  position: fixed; top: 0; right: 0; bottom: 0; width: var(--drawer); z-index: 3;
  display: flex; flex-direction: column; overflow-y: auto;
  background: var(--surface); border-left: 1px solid var(--line);
  animation: drawerIn 220ms var(--ease);
}
@keyframes drawerIn { from { transform: translateX(16px); opacity: 0; } }
.cardGrip { display: none; }
.cardHead { display: flex; align-items: center; gap: 14px; padding: 20px 14px 18px 22px; }
.avatar { width: 48px; height: 48px; flex: none; border-radius: 12px; display: grid; place-items: center; font-weight: 600; font-size: 17px; background: #1c1530; color: #c4a5ff; }
.avatar[data-tone="red"] { background: #2a1214; color: #ff8a8a; }
.avatar[data-tone="grey"] { background: #18181b; color: #8b8b94; }
.avatar[data-tone="prospect"] { background: transparent; border: 1.5px dashed #6b5a99; color: #c4a5ff; }
.cardTitle { flex: 1; min-width: 0; }
.card h2 { font-size: 19px; line-height: 1.25; font-weight: 600; letter-spacing: -0.015em; overflow-wrap: anywhere; }
.cardSub { margin-top: 3px; font-size: 13px; color: var(--muted); }
.cardKind[data-kind="customer"] { color: var(--ok); }
.cardKind[data-kind="prospect"] { color: var(--warn); }
.phone:not(:empty)::before { content: " · "; color: var(--faint); }
.phone a { color: #5ad27f; text-decoration: none; font-variant-numeric: tabular-nums; }
.phone a:hover { text-decoration: underline; }

.section { display: grid; gap: 10px; padding: 16px 22px; border-top: 1px solid var(--line); }
.lbl { display: flex; justify-content: space-between; align-items: baseline; font-size: 12px; color: var(--faint); }

/* sales funnel: each status is a segment; earlier ones read as done */
.steps { display: grid; grid-template-columns: repeat(auto-fit, minmax(72px, 1fr)); gap: 6px; }
.step {
  display: grid; gap: 7px; align-content: start; padding: 4px 0 2px; border: 0; background: none;
  color: var(--faint); font-size: 11.5px; line-height: 1.3; text-align: left; cursor: pointer;
  min-width: 0; overflow-wrap: anywhere; hyphens: auto;
}
.step i { height: 4px; border-radius: 2px; background: #232329; transition: background 200ms var(--ease); }
.step:hover { color: var(--text); }
.step:hover i { background: #33333b; }
.step.done { color: var(--muted); }
.step.done i { background: var(--accent-dim); }
.step[aria-checked="true"] { color: #fff; font-weight: 500; }
.step[aria-checked="true"] i { background: var(--accent); }

.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 28px; cursor: pointer; font-size: 14px; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switchTrack { width: 34px; height: 20px; border-radius: 999px; background: var(--line2); position: relative; flex: none; transition: background 150ms var(--ease); }
.switchTrack::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform 150ms var(--ease); }
.switch input:checked + .switchTrack { background: #22a356; }
.switch input:checked + .switchTrack::after { transform: translateX(14px); }
.switch input:focus-visible + .switchTrack { outline: 2px solid var(--accent); outline-offset: 2px; }
.card textarea { min-height: 110px; background: var(--s2); }

.bubble { justify-self: start; max-width: 92%; background: #16161a; border: 1px solid var(--line); border-radius: 12px 12px 12px 4px; padding: 9px 12px; font-size: 13.5px; white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble time { display: block; margin-top: 4px; font-size: 11.5px; color: var(--faint); }

.meta { display: grid; grid-template-columns: 88px 1fr; align-items: center; row-gap: 6px; font-size: 13px; }
.meta > span:nth-child(odd), .meta > label { color: var(--faint); }
.metaEdit { display: flex; align-items: center; gap: 8px; }
.metaEdit input { height: 30px; padding: 0 8px; margin-left: -8px; background: transparent; border-color: transparent; font-size: 13px; }
.metaEdit input:hover { background: var(--s2); border-color: var(--line); }
.metaEdit input:focus { background: var(--s2); border-color: var(--accent); }
.facts { display: flex; flex-wrap: wrap; gap: 6px; margin: 0; padding: 0; list-style: none; font-size: 13px; color: var(--muted); }
.facts:empty { display: none; }
.facts li + li::before { content: "·"; margin-right: 6px; color: var(--faint); }
.accounts { display: flex; flex-wrap: wrap; gap: 6px; }
.accounts:empty { display: none; }
.accounts .field { width: 100%; }
.accounts code { font: 12px var(--mono); color: var(--muted); background: var(--s2); padding: 3px 7px; border-radius: 6px; }
.cardFoot { display: flex; justify-content: space-between; gap: 8px; margin-top: auto; padding: 12px 14px 14px; border-top: 1px solid var(--line); }
.cardFoot:not(:has(.btn:not([hidden]))) { display: none; }

/* ---------- dialogs ---------- */
.dialog { width: min(440px, calc(100% - 32px)); padding: 0; background: var(--surface); color: var(--text); border: 1px solid var(--line2); border-radius: var(--radius-lg); }
.dialog::backdrop { background: rgba(0, 0, 0, 0.72); }
.dialogBody { display: grid; gap: 14px; padding: 20px; }
.dialog h2 { font-size: 17px; font-weight: 600; }
.dialog .row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }
.dialogActions { display: flex; justify-content: flex-end; align-items: center; gap: 8px; margin-top: 4px; }
.dialogActions .btn { height: 36px; }
.dialogActions .btnDanger { margin-right: auto; }
.waQr { display: grid; place-items: center; min-height: 268px; margin-top: 12px; background: #fff; border-radius: var(--radius); }
.waQr img { width: 260px; height: 260px; image-rendering: pixelated; }
.waQr p { color: #333; }

/* ---------- select mode ---------- */
#selectBtn[aria-pressed="true"] { background: #fff; border-color: #fff; color: #000; box-shadow: 0 0 16px rgba(255, 255, 255, 0.35); }
.bulkBar {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 4;
  display: flex; align-items: center; gap: 12px; max-width: calc(100% - 32px); padding: 8px 10px 8px 16px;
  background: var(--surface); border: 1px solid var(--line2); border-radius: var(--radius-lg);
  animation: barIn 180ms var(--ease);
}
@keyframes barIn { from { transform: translate(-50%, 8px); opacity: 0; } }
.bulkCount { font-size: 13px; color: var(--muted); white-space: nowrap; min-width: 150px; }
.bulkCount.has { color: #fff; font-weight: 600; }
.bulkBar { border-color: rgba(255, 255, 255, 0.28); }
.bulkGroup { display: flex; align-items: center; gap: 6px; }
.bulkSel { gap: 12px; padding-left: 8px; border-left: 1px solid var(--line); }
.bulkSel .linkBtn { white-space: nowrap; font-size: 13px; }
.bulkBar .btn:disabled { opacity: 0.4; cursor: default; }
.bulkWrap { position: relative; }
.bulkMenu {
  position: absolute; bottom: calc(100% + 8px); left: 0; min-width: 200px; padding: 4px; z-index: 5;
  display: grid; background: var(--surface); border: 1px solid var(--line2); border-radius: var(--radius-lg);
}
.bulkMenu button { min-height: 36px; padding: 0 10px; border: 0; border-radius: var(--radius); background: none; text-align: left; font-size: 13.5px; cursor: pointer; }
.bulkMenu button:hover, .bulkMenu button:focus-visible { background: var(--s2); }
.bulkMenu .menuNew { color: var(--muted); border-top: 1px solid var(--line); border-radius: 0 0 var(--radius) var(--radius); }
body.selecting .legend { display: none; }
.toast {
  position: fixed; left: 50%; bottom: calc(max(20px, env(safe-area-inset-bottom)) + 64px); transform: translateX(-50%); z-index: 6;
  padding: 9px 14px; background: #1c1c21; border: 1px solid var(--line2); border-radius: var(--radius); font-size: 13px; max-width: calc(100% - 32px);
}

/* ---------- tablet / phone ---------- */
@media (min-width: 761px) and (max-width: 1280px) {
  .bar { gap: 10px; padding-left: 14px; }
  .bar .search input { width: 150px; padding-right: 14px; }
  .bar .kbd, #waLabel { display: none; }
  .waBtn { width: 42px; padding: 0 !important; }
}
/* drawer open on a mid-size screen: the header has ~60% of the width */
@media (min-width: 761px) and (max-width: 1400px) {
  body.hasCard .brand, body.hasCard .kbd, body.hasCard #waLabel, body.hasCard .addText, body.hasCard #selectBtn span { display: none; }

  body.hasCard .viewNav { margin: 0; }
  body.hasCard .nimbus-item { padding: 0 13px; }
  body.hasCard .nimbus-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  body.hasCard .tools { margin-left: auto; }
  body.hasCard .search input { width: 130px; padding-right: 12px; }
  body.hasCard .bar { gap: 10px; padding-left: 14px; }
  body.hasCard .waBtn, body.hasCard #addBtn, body.hasCard #selectBtn { width: 42px; padding: 0; }
}
@media (max-width: 1100px) {
  .brand { display: none; }
  .viewNav { margin: 0; }
  .tools { margin-left: auto; }
  .search input { width: 170px; }
}
@media (max-width: 760px) {
  .bar { height: auto; flex-wrap: wrap; gap: 8px; padding: max(10px, env(safe-area-inset-top)) 12px 10px; }
  body.hasCard .bar { right: 0; }
  .tools { order: 1; width: 100%; }
  .search { flex: 1; }
  .search input { width: 100%; height: 44px; font-size: 16px; }
  .kbd, .addText, #waLabel, #selectBtn span { display: none; }
  #selectBtn { width: 44px; padding: 0; }
  .bulkBar { left: 0; right: 0; bottom: 0; transform: none; max-width: none; flex-wrap: wrap; gap: 8px; padding: 10px 12px max(12px, env(safe-area-inset-bottom)); border-radius: var(--radius-lg) var(--radius-lg) 0 0; animation: none; }
  .bulkCount { width: 100%; min-width: 0; }
  .bulkGroup { flex-wrap: wrap; }
  .bulkSel { border-left: 0; padding-left: 0; width: 100%; }
  .bulkMenu { bottom: auto; top: auto; bottom: calc(100% + 6px); }
  .btn { height: 44px; }
  .waBtn, #addBtn { width: 44px; padding: 0; font-size: 20px; }
  .viewNav { order: 2; width: 100%; justify-content: center; }
  .bar .btn:not(.btnWhite), .bar .search input, .bar .btnWhite { height: 44px; }
  .searchResults { width: 100%; }

  .card {
    top: auto; left: 0; width: auto; max-height: 72vh; border-left: 0; border-top: 1px solid var(--line2);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0; padding-bottom: env(safe-area-inset-bottom);
    animation-name: sheetIn;
  }
  @keyframes sheetIn { from { transform: translateY(16px); opacity: 0; } }
  .cardGrip { display: block; width: 36px; height: 4px; border-radius: 4px; background: var(--line2); margin: 8px auto 0; flex: none; }
  .cardHead { padding-top: 12px; }
  .cardHead .btnIcon { width: 44px; }
  .step { min-height: 44px; }
  body.hasCard .legend, body.hasCard .live { display: none; }
}

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