/* CRM clients - tokens first, then layout, then components. */

:root {
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  --ease-in-out: cubic-bezier(0.77, 0, 0.175, 1);
  --r-sm: 6px;
  --r: 10px;
  --r-lg: 14px;
  --rail: 236px;

  /* status palette: fixed, never themed */
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110;
  --surface-1: #1a1a19;
  --surface-2: #222221;
  --surface-3: #2b2b29;
  --border: #2c2c2a;
  --border-strong: #3a3a37;
  --text-1: #f0efec;
  --text-2: #c3c2b7;
  --text-3: #8c8b81;
  --accent: #3987e5;
  --accent-soft: #16304f;
  --grid: #242422;
  --shadow: 0 1px 2px rgb(0 0 0 / .5), 0 12px 32px -12px rgb(0 0 0 / .6);
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f6f5f2;
  --surface-1: #fcfcfb;
  --surface-2: #ffffff;
  --surface-3: #f0efec;
  --border: #e5e4df;
  --border-strong: #d4d3cd;
  --text-1: #0b0b0b;
  --text-2: #52514e;
  --text-3: #85847e;
  --accent: #2a78d6;
  --accent-soft: #e4eefc;
  --grid: #ebeae5;
  --shadow: 0 1px 2px rgb(16 16 14 / .06), 0 12px 32px -14px rgb(16 16 14 / .18);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text-1);
  font: 14px/1.5 ui-sans-serif, -apple-system, "SF Pro Text", "Inter", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  font-variant-numeric: tabular-nums;
}

h1, h2, h3 { margin: 0; font-weight: 580; letter-spacing: -0.014em; }

a { color: inherit; text-decoration: none; }

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  cursor: pointer;
}

::selection { background: var(--accent); color: #fff; }

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

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

.shell {
  display: grid;
  grid-template-columns: var(--rail) minmax(0, 1fr);
  min-height: 100%;
}

.rail {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 18px 12px 12px;
  background: var(--surface-1);
  border-right: 1px solid var(--border);
}

.brand { display: flex; gap: 10px; align-items: center; padding: 0 8px 18px; }

.brand-mark {
  display: grid;
  place-items: center;
  width: 28px; height: 28px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 650;
  font-size: 15px;
}

.brand-text { display: flex; flex-direction: column; font-weight: 580; letter-spacing: -0.01em; }
.brand-text small { color: var(--text-3); font-weight: 450; font-size: 11px; letter-spacing: 0; }

#nav { display: flex; flex-direction: column; gap: 2px; }

#nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--r-sm);
  color: var(--text-2);
  transition: background 120ms ease, color 120ms ease;
}

#nav a svg {
  width: 16px; height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: .8;
}

@media (hover: hover) and (pointer: fine) {
  #nav a:hover { background: var(--surface-2); color: var(--text-1); }
}

#nav a[aria-current] { background: var(--surface-3); color: var(--text-1); }
#nav a[aria-current] svg { color: var(--accent); opacity: 1; }

.pill {
  margin-left: auto;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--surface-3);
  color: var(--text-2);
  font-size: 11px;
  font-weight: 550;
}
#nav a[aria-current] .pill { background: var(--accent); color: #fff; }
.pill:empty { display: none; }

.rail-foot { margin-top: auto; display: flex; flex-direction: column; gap: 2px; }

.ghost {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--r-sm);
  color: var(--text-3);
  font-size: 13px;
  transition: background 120ms ease, color 120ms ease, transform 140ms var(--ease-out);
}
.ghost svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; }
@media (hover: hover) and (pointer: fine) { .ghost:hover { background: var(--surface-2); color: var(--text-1); } }
.ghost:active { transform: scale(0.97); }
.ghost.spin svg { animation: spin 700ms linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

main { padding: 28px 32px 72px; max-width: 1180px; }

/* ---------- page header ---------- */

.page-head { display: flex; align-items: flex-end; gap: 16px; margin-bottom: 22px; }
.page-head h1 { font-size: 22px; }
.page-head p { margin: 2px 0 0; color: var(--text-3); font-size: 13px; }
.page-head .spacer { margin-left: auto; }

.search {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  min-width: 260px;
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--surface-1);
  color: var(--text-3);
  font-size: 13px;
  transition: border-color 140ms ease, background 140ms ease;
}
.search:hover { border-color: var(--border-strong); }
.search kbd { margin-left: auto; }

kbd {
  padding: 1px 5px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--surface-2);
  color: var(--text-3);
  font: 500 11px/1.6 ui-monospace, SFMono-Regular, monospace;
}

/* ---------- cards & tiles ---------- */

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 16px;
}

.tile .label { color: var(--text-3); font-size: 12px; letter-spacing: .01em; }
.tile .value { margin-top: 6px; font-size: 26px; font-weight: 600; letter-spacing: -0.03em; line-height: 1.1; }
.tile .value small { font-size: 15px; font-weight: 500; color: var(--text-3); letter-spacing: -0.01em; }
.tile .detail { margin-top: 4px; color: var(--text-3); font-size: 12px; }
.tile .detail b { color: var(--text-2); font-weight: 550; }

.stagger > * { opacity: 0; animation: rise 320ms var(--ease-out) forwards; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.stagger > :nth-child(1) { animation-delay: 0ms; }
.stagger > :nth-child(2) { animation-delay: 40ms; }
.stagger > :nth-child(3) { animation-delay: 80ms; }
.stagger > :nth-child(4) { animation-delay: 120ms; }
.stagger > :nth-child(5) { animation-delay: 160ms; }
.stagger > :nth-child(n+6) { animation-delay: 200ms; }

@media (prefers-reduced-motion: reduce) {
  .stagger > * { animation: fade 200ms ease forwards; }
  @keyframes fade { from { opacity: 0; } to { opacity: 1; } }
  .ghost:active { transform: none; }
}

.section { margin-top: 26px; }
.section-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 10px; }
.section-head h2 { font-size: 14px; }
.section-head span { color: var(--text-3); font-size: 12px; }
.section-head .spacer { margin-left: auto; }

/* ---------- chart ---------- */

.chart { width: 100%; height: auto; display: block; overflow: visible; }
.chart .grid-line { stroke: var(--grid); stroke-width: 1; }
.chart .axis { fill: var(--text-3); font-size: 10.5px; }
.chart .bar { fill: var(--accent); }
.chart .bar-hit { fill: transparent; }
.chart .bar-hit:hover + .bar, .chart .bar.on { fill: var(--accent); filter: brightness(1.18); }
.chart .paid { fill: var(--good); }

.chart-tip {
  position: absolute;
  pointer-events: none;
  padding: 8px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
  background: var(--surface-2);
  box-shadow: var(--shadow);
  font-size: 12px;
  white-space: nowrap;
  opacity: 0;
  transform: translate(-50%, -100%) scale(0.97);
  transition: opacity 120ms ease, transform 120ms var(--ease-out);
  z-index: 5;
}
.chart-tip.on { opacity: 1; transform: translate(-50%, -100%) scale(1); }
.chart-tip b { display: block; margin-bottom: 3px; color: var(--text-1); }
.chart-tip span { color: var(--text-2); }
.chart-wrap { position: relative; }

.legend { display: flex; gap: 14px; margin-top: 10px; color: var(--text-2); font-size: 12px; }
.legend i { display: inline-block; width: 9px; height: 9px; border-radius: 3px; margin-right: 6px; }

/* ---------- table ---------- */

.table-wrap { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--r-lg); overflow: hidden; }

table { width: 100%; border-collapse: collapse; }

th {
  position: sticky; top: 0; z-index: 1;
  padding: 10px 14px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  color: var(--text-3);
  font-size: 11.5px;
  font-weight: 550;
  text-align: left;
  letter-spacing: .02em;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
}
th.num, td.num { text-align: right; }
th[aria-sort] { color: var(--text-1); }
th[aria-sort]::after { content: " ↓"; color: var(--accent); }
th[aria-sort="ascending"]::after { content: " ↑"; }

td { padding: 11px 14px; border-bottom: 1px solid var(--border); color: var(--text-2); }
tbody tr:last-child td { border-bottom: 0; }
tbody tr { transition: background 100ms ease; cursor: pointer; }
@media (hover: hover) and (pointer: fine) { tbody tr:hover { background: var(--surface-2); } }
tbody tr.is-test td { opacity: .55; }

td.name { color: var(--text-1); font-weight: 520; }
td.name .sub { display: block; color: var(--text-3); font-weight: 400; font-size: 12px; }
.tag {
  display: inline-block; margin-left: 6px; padding: 0 5px;
  border: 1px solid var(--border-strong); border-radius: 5px;
  color: var(--text-3); font-size: 10.5px; font-weight: 500; vertical-align: 1px;
}
.muted { color: var(--text-3); }
.dim { color: var(--text-3); }

.status { display: inline-flex; align-items: center; gap: 6px; font-weight: 520; font-size: 12.5px; white-space: nowrap; }
.status i { width: 8px; height: 8px; border-radius: 999px; flex: none; }
.status.paid { color: var(--good); } .status.paid i { background: var(--good); }
.status.sent { color: var(--warning); } .status.sent i { background: var(--warning); }
.status.generated { color: var(--text-3); } .status.generated i { background: var(--text-3); }
.status.overdue { color: var(--critical); } .status.overdue i { background: var(--critical); }

.bar-mini { height: 4px; border-radius: 999px; background: var(--surface-3); overflow: hidden; }
.bar-mini > i { display: block; height: 100%; border-radius: 999px; background: var(--accent); }

/* ---------- client detail ---------- */

.back { display: inline-flex; align-items: center; gap: 6px; color: var(--text-3); font-size: 13px; margin-bottom: 14px; }
.back:hover { color: var(--text-1); }

.detail-head { display: flex; gap: 18px; align-items: flex-start; flex-wrap: wrap; }
.avatar {
  display: grid; place-items: center;
  width: 46px; height: 46px; flex: none;
  border-radius: 13px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 18px; font-weight: 600;
}
.detail-head h1 { font-size: 21px; }
.ident { display: flex; flex-wrap: wrap; gap: 8px 16px; margin-top: 8px; color: var(--text-2); font-size: 13px; }

.field { display: inline-flex; align-items: center; gap: 7px; }
.field svg { width: 14px; height: 14px; fill: none; stroke: var(--text-3); stroke-width: 1.5; stroke-linecap: round; }
.field input {
  width: 15ch;
  padding: 4px 7px;
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  background: transparent;
  color: var(--text-1);
  font: inherit;
  transition: border-color 140ms ease, background 140ms ease;
}
.field input::placeholder { color: var(--text-3); }
.field input:hover { border-color: var(--border); }
.field input:focus { border-color: var(--accent); background: var(--surface-2); outline: none; }
.field input.wide { width: 24ch; }
.field.saved input { border-color: var(--good); }

textarea.notes {
  width: 100%;
  min-height: 72px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--surface-1);
  color: var(--text-1);
  font: inherit;
  resize: vertical;
  transition: border-color 140ms ease;
}
textarea.notes:focus { border-color: var(--accent); outline: none; }

.two-col { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); gap: 16px; align-items: start; }
@media (max-width: 920px) { .two-col { grid-template-columns: 1fr; } }

.feed { display: flex; flex-direction: column; }
.feed li { display: flex; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--border); list-style: none; }
.feed li:last-child { border-bottom: 0; }
.feed .when { flex: none; width: 76px; color: var(--text-3); font-size: 12px; padding-top: 1px; }
.feed .what { min-width: 0; }
.feed .what b { display: block; color: var(--text-1); font-weight: 500; font-size: 13px; }
.feed .what span { color: var(--text-3); font-size: 12px; }
.feed .dur { margin-left: auto; flex: none; color: var(--text-2); font-size: 12.5px; }
ul { margin: 0; padding: 0; }

/* ---------- phones view ---------- */

.phone-row { display: grid; grid-template-columns: minmax(0, 1fr) 20ch 24ch; gap: 12px; align-items: center; padding: 9px 14px; border-bottom: 1px solid var(--border); }
.phone-row:last-child { border-bottom: 0; }
.phone-row input {
  padding: 6px 9px;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--surface-2);
  color: var(--text-1);
  font: inherit;
  width: 100%;
  transition: border-color 140ms ease;
}
.phone-row input:focus { border-color: var(--accent); outline: none; }
.phone-row input.ok { border-color: var(--good); }
@media (max-width: 760px) { .phone-row { grid-template-columns: 1fr; } }

/* ---------- lookup view ---------- */

.lookup-form { display: flex; gap: 10px; flex-wrap: wrap; }
.lookup-form input {
  flex: 1 1 260px;
  padding: 11px 14px;
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--surface-1);
  color: var(--text-1);
  font: 500 16px/1.4 ui-monospace, SFMono-Regular, monospace;
  transition: border-color 140ms ease;
}
.lookup-form input:focus { border-color: var(--accent); outline: none; }

.btn {
  padding: 11px 18px;
  border-radius: var(--r);
  background: var(--accent);
  color: #fff;
  font-weight: 550;
  transition: transform 150ms var(--ease-out), filter 150ms ease;
}
@media (hover: hover) and (pointer: fine) { .btn:hover { filter: brightness(1.08); } }
.btn:active { transform: scale(0.97); }
.btn.secondary { background: var(--surface-2); color: var(--text-1); border: 1px solid var(--border); }

pre.json {
  margin: 0;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--surface-1);
  color: var(--text-2);
  font: 12.5px/1.65 ui-monospace, SFMono-Regular, monospace;
  overflow: auto;
  max-height: 420px;
}
pre.json .k { color: var(--accent); }
pre.json .s { color: var(--good); }
pre.json .n { color: var(--warning); }

.quote {
  padding: 16px 18px;
  border-left: 2px solid var(--accent);
  border-radius: 0 var(--r) var(--r) 0;
  background: var(--surface-1);
  color: var(--text-1);
  font-size: 15px;
  line-height: 1.6;
}

.empty { padding: 36px 16px; text-align: center; color: var(--text-3); }

/* ---------- command palette ---------- */

.palette-backdrop[hidden] { display: none; }

.palette-backdrop {
  position: fixed; inset: 0; z-index: 40;
  display: grid; place-items: start center;
  padding-top: 14vh;
  background: rgb(0 0 0 / .42);
  backdrop-filter: blur(3px);
}

.palette {
  width: min(560px, calc(100vw - 32px));
  background: var(--surface-1);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.palette input {
  width: 100%;
  padding: 15px 18px;
  border: 0;
  border-bottom: 1px solid var(--border);
  background: transparent;
  color: var(--text-1);
  font: 15px/1.4 inherit;
}
.palette input:focus { outline: none; }
.palette input::placeholder { color: var(--text-3); }

#palette-results { max-height: 46vh; overflow: auto; }
#palette-results li {
  display: flex; gap: 10px; align-items: center;
  padding: 9px 18px;
  list-style: none;
  cursor: pointer;
}
#palette-results li b { color: var(--text-1); font-weight: 500; }
#palette-results li span { color: var(--text-3); font-size: 12px; }
#palette-results li .right { margin-left: auto; color: var(--text-3); font-size: 12px; }
#palette-results li[aria-selected="true"] { background: var(--surface-3); }

.palette footer { display: flex; gap: 12px; align-items: center; padding: 9px 18px; border-top: 1px solid var(--border); color: var(--text-3); font-size: 11.5px; }
.palette footer kbd { margin-right: 4px; }

/* ---------- toasts ---------- */

.toasts { position: fixed; bottom: 20px; right: 20px; z-index: 60; display: flex; flex-direction: column; gap: 8px; }
.toast {
  display: flex; align-items: center; gap: 9px;
  padding: 11px 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--r);
  background: var(--surface-2);
  box-shadow: var(--shadow);
  font-size: 13px;
  transition: opacity 260ms ease, transform 260ms var(--ease-out);
}
.toast i { width: 8px; height: 8px; border-radius: 999px; background: var(--good); flex: none; }
.toast.bad i { background: var(--critical); }
@starting-style { .toast { opacity: 0; transform: translateY(8px); } }
.toast.out { opacity: 0; transform: translateY(6px); }

/* ---------- skeleton ---------- */

.skeleton-page { height: 60vh; border-radius: var(--r-lg); background: linear-gradient(100deg, var(--surface-1) 30%, var(--surface-2) 50%, var(--surface-1) 70%); background-size: 240% 100%; animation: sweep 1.3s linear infinite; }
@keyframes sweep { to { background-position: -140% 0; } }

/* ---------- small screens ---------- */

@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .rail { position: static; height: auto; flex-direction: row; align-items: center; gap: 8px; overflow-x: auto; padding: 10px 12px; border-right: 0; border-bottom: 1px solid var(--border); }
  .brand { padding: 0 6px 0 0; }
  .brand-text { display: none; }
  #nav { flex-direction: row; }
  #nav a span.pill { display: none; }
  .rail-foot { margin: 0 0 0 auto; flex-direction: row; }
  .ghost { gap: 0; }
  .ghost svg { margin: 0; }
  .ghost { font-size: 0; }
  main { padding: 18px 16px 60px; }
}
