/* Leadomix — shared design tokens for the ops (/app/) and CS (/admin/) consoles.
 *
 * Single source of truth, extracted from brand/BRAND.md §7 and site/styles.css
 * so the product and leadomix.ai stop looking like two different companies.
 * Load this BEFORE app.css / admin.css; those files now only carry layout and
 * component rules, and alias their legacy variable names onto these tokens.
 *
 * Rules inherited from BRAND.md that this file enforces:
 *   - Everything Geist; every number Geist Mono with tabular-nums (the single
 *     most load-bearing rule in the system). Use .num on any numeric cell.
 *   - Structure comes from hairlines and barely-lifted surfaces, never shadows.
 *   - Lime is the default accent, spent once per screen plus the primary CTA.
 *     Optional data-accent=* remaps --lime* for the account-menu colour picker.
 *     --warn is semantic only: money being lost. Never decorative.
 *
 * Light mode: BRAND.md is dark-only, but both consoles ship light, so light is
 * rebuilt here to brand discipline instead of the old muddy grey-green — real
 * ground/surface separation and visible hairlines, so nothing needs a shadow.
 */

/* ---- primitives: identical in both themes ------------------------------- */
:root {
  --sans: "Geist", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: "Geist Mono", ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;

  /* type scale — replaces ~30 ad-hoc font-sizes */
  --fs-label: 0.6875rem; /* mono labels: uppercase, 0.16em tracking */
  --fs-xs: 0.75rem;
  --fs-sm: 0.8125rem;
  --fs-md: 0.9375rem; /* body */
  --fs-lg: 1.0625rem;
  --fs-xl: 1.375rem;
  --fs-2xl: 1.75rem;

  /* aliases used by new component rules (#989); --fs-* stay for existing ones */
  --text-xs: var(--fs-xs);
  --text-sm: var(--fs-sm);
  --text-base: var(--fs-md);
  --text-lg: var(--fs-lg);
  --text-xl: var(--fs-xl);
  --text-2xl: var(--fs-2xl);
  --text-hero: clamp(1.5rem, 1.1rem + 1.6vw, 2.1rem);
  --text-label: var(--fs-label);

  --weight-regular: 400;
  --weight-medium: 500;
  --weight-semibold: 600; /* Geist ships 400/500/600 only: no 700 */

  --lh-tight: 1.25;
  --lh-body: 1.6;
  --tr-tight: -0.02em;
  --tr-label: 0.16em;

  /* spacing ramp — replaces 25+ ad-hoc padding pairs */
  --sp-1: 0.25rem;
  --sp-2: 0.5rem;
  --sp-3: 0.75rem;
  --sp-4: 1rem;
  --sp-5: 1.5rem;
  --sp-6: 2rem;

  /* radius scale — replaces 14 competing radii */
  --r-sm: 6px;
  --r: 10px;
  --r-lg: 14px;
  --r-pill: 999px;
  --radius-sm: var(--r-sm);
  --radius-md: var(--r);
  --radius-lg: var(--r-lg);
  --radius-pill: var(--r-pill);

  --dur: 140ms;
  --ease: cubic-bezier(0.2, 0, 0.2, 1);
}

/* ---- light: rebuilt to brand discipline --------------------------------- */
:root,
[data-bs-theme="light"] {
  /* Depth comes from the ground being genuinely tinted, not from shadows.
     The first pass had ground #f4f5f3 against white surfaces — only 1.09:1,
     so cards never lifted and the whole console read as one flat white sheet
     with borders drawn on it. At 1.19:1 a white card reads as raised, which
     lets the hairline get *softer* rather than doing all the work. */
  --ground: #e9ece5; /* warm grey-green page ground */
  --surface: #ffffff; /* panels lift by tone, not by shadow */
  --surface-2: #f2f4ef; /* sits between ground and surface: rows, hovers */
  --line: #d2d8cc; /* quiet now that tone separates the planes */
  --line-soft: #e0e4da;

  --text: #121512; /* 15.4:1 on ground */
  --text-2: #454e44; /* 7.9:1 — body copy */
  --muted: #5d675c; /* 5.0:1 — AA at caption sizes */

  /* Brand lime (#AAD635 nudge) as FILL with near-black ink.
     As text on white it fails contrast, so --lime-text carries links and
     figures in light mode — same family, readable. */
  --lime: #aad635;
  --lime-ink: #0c1004;
  --lime-text: #3f5c0a; /* ≥4.5:1 on ground / white for links */
  --lime-dim: rgba(170, 214, 53, 0.16);
  --lime-line: rgba(122, 163, 25, 0.42);

  --warn: #b83a0a; /* money being lost — 4.8:1 on the tinted ground */

  /* status palette — named, theme-aware, replaces 145 loose hexes */
  --st-neutral: #6b756a;
  --st-info: #3f6089;
  --st-attention: #9a6a06; /* borders and fills; text uses --st-attention-ink */
  --st-attention-ink: #7a5200; /* 5.8:1 on ground */
  --st-ok: #2f6b3c;
  --st-closed: #98a096;

  /* chat threads (inbox, Ventas) — WhatsApp's planes; ratios measured on the bubble they sit on */
  --chat-wall: #efeae2;
  --chat-wall-dot: rgba(17, 27, 33, 0.05);
  --chat-in: #ffffff;
  --chat-out: #d9fdd3;
  --chat-ink: #111b21; /* 17.5:1 on in, 15.8:1 on out */
  --chat-meta: #5d675c; /* 5.9:1 on in */
  --chat-meta-out: #4d6553; /* 5.7:1 on out */
  --chat-day-bg: #ffffff;
  --chat-day-ink: #54656f; /* 6.1:1 */
  --chat-pill-human: #8a5d00; /* 4.8:1 on its 14% tint */
  /* avatar fills, both themes: white initials clear 5.8:1 on each */
  --chat-tone-0: #2f6b3c;
  --chat-tone-1: #3f6089;
  --chat-tone-2: #6b4a8c;
  --chat-tone-3: #8a5a0a;
  --chat-tone-4: #a03c3c;
  --chat-tone-5: #1f6f78;

  color-scheme: light;
}

/* ---- dark: the committed brand world ----------------------------------- */
[data-bs-theme="dark"] {
  --ground: #08090a; /* near-black, never pure black */
  --surface: #0e1012;
  --surface-2: #14171a;
  --line: #1e2225;
  --line-soft: #17191c;

  --text: #f2f4f3;
  --text-2: #a8afb5; /* cool-biased grey, chosen not inherited */
  --muted: #7f878f; /* 4.9:1 on surface-2, the lightest surface it sits on */

  --lime: #c7f751;
  --lime-ink: #0a0d04;
  --lime-text: #c7f751; /* passes on near-black, so text = the accent */
  --lime-dim: rgba(199, 247, 81, 0.1);
  --lime-line: rgba(199, 247, 81, 0.28);

  --warn: #ff7a5c;

  --st-neutral: #767e86;
  --st-info: #7ea6d4;
  --st-attention: #e8b45c;
  --st-attention-ink: #e8b45c;
  --st-ok: #8fc99a;
  --st-closed: #3a4046;

  --chat-wall: #0b141a;
  --chat-wall-dot: rgba(233, 237, 239, 0.035);
  --chat-in: #202c33;
  --chat-out: #005c4b;
  --chat-ink: #e9edef; /* 12.1:1 on in, 6.8:1 on out */
  --chat-meta: #93a3ad; /* 5.5:1 on in */
  --chat-meta-out: #b9d0cd; /* 4.9:1 on out */
  --chat-day-bg: #182229;
  --chat-day-ink: #93a3ad; /* 6.2:1 */
  --chat-pill-human: #e8b45c; /* 7.8:1 on its 14% tint */

  color-scheme: dark;
}

/* ---- Bootstrap bridge --------------------------------------------------- */
/* Drive Bootstrap from the tokens instead of overriding its utilities later. */
:root,
[data-bs-theme="light"],
[data-bs-theme="dark"] {
  --bs-body-bg: var(--ground);
  --bs-body-color: var(--text);
  --bs-secondary-color: var(--text-2);
  --bs-tertiary-bg: var(--surface-2);
  --bs-secondary-bg: var(--surface);
  --bs-border-color: var(--line);
  --bs-emphasis-color: var(--text);
  --bs-primary: var(--lime-text);
  --bs-link-color: var(--lime-text);
  --bs-link-hover-color: var(--text);
  --bs-body-font-family: var(--sans);
  --bs-body-font-size: var(--fs-md);
  --bs-body-line-height: var(--lh-body);
  --bs-border-radius: var(--r);
  --bs-border-radius-sm: var(--r-sm);
  --bs-border-radius-lg: var(--r-lg);
}

/* ---- appointment lanes + status (#987) ----------------------------------
 * One row per lane: --lane-X is the stripe/border, --lane-X-ink is text on the
 * surface or on a 12% tint of the stripe. They resolve through the --st-* palette,
 * so each pair is measured for both themes (all ink >= 4.5:1). */
:root,
[data-bs-theme="light"],
[data-bs-theme="dark"] {
  --lane-scheduled: var(--st-neutral);
  --lane-scheduled-ink: var(--text-2);
  --lane-confirmed: var(--lime);
  --lane-confirmed-ink: var(--lime-text);
  --lane-waiting: var(--st-info);
  --lane-waiting-ink: var(--st-info);
  --lane-need-reschedule: var(--st-attention);
  --lane-need-reschedule-ink: var(--st-attention-ink);
  --lane-action-required: var(--warn);
  --lane-action-required-ink: var(--warn);
  --lane-done: var(--st-ok);
  --lane-done-ink: var(--st-ok);
  --lane-closed: var(--st-closed);
  --lane-closed-ink: var(--muted);
  --lane-cancelled: var(--text-2);
  --lane-cancelled-ink: var(--text-2);
}

/* ---- base -------------------------------------------------------------- */
body {
  font-family: var(--sans);
  font-size: var(--fs-md);
  line-height: var(--lh-body);
  background: var(--ground);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

h1, h2, h3, h4, h5, h6 {
  letter-spacing: var(--tr-tight);
  line-height: var(--lh-tight);
}

/* ---- numbers: the load-bearing rule ------------------------------------ */
/* Any figure — money, counts, times, IDs. Mono + tabular so columns align. */
.num,
.tabular,
td.num,
th.num {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
}

/* Mono labels: the recurring structural device, replacing decorative eyebrows. */
.mono-label {
  font-family: var(--mono);
  font-size: var(--fs-label);
  text-transform: uppercase;
  letter-spacing: var(--tr-label);
  color: var(--muted);
}

/* ---- tables: previously zero CSS in both consoles ---------------------- */
/* Hairline structure, quiet mono header, numerics right-aligned and tabular. */
.table {
  --bs-table-bg: transparent;
  --bs-table-color: var(--text);
  --bs-table-border-color: var(--line);
  margin-bottom: 0;
  font-size: var(--fs-sm);
}

.table > thead > tr > th {
  font-family: var(--mono);
  font-size: var(--fs-label);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: var(--tr-label);
  color: var(--muted);
  background: transparent;
  border-bottom: 1px solid var(--line);
  padding: var(--sp-2) var(--sp-3);
  white-space: nowrap;
}

.table > tbody > tr > td {
  padding: var(--sp-2) var(--sp-3);
  border-bottom: 1px solid var(--line-soft);
  vertical-align: middle;
}

.table > tbody > tr:last-child > td { border-bottom: 0; }

.table > tbody > tr:hover > td { background: var(--surface-2); }

/* Numeric columns: right-aligned + tabular without needing a class per cell. */
.table th.text-end,
.table td.text-end {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.table-wrap { overflow-x: auto; }

/* ---- surfaces: structure from hairlines, never shadows ----------------- */
.card,
.tenant-card,
dialog {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r);
  box-shadow: none;
}

/* BRAND.md: "never by drop shadows". Neutralise Bootstrap's shadow utilities
   in the consoles rather than hunting all 24 call sites in the markup. */
.shadow-sm,
.shadow,
.shadow-lg {
  box-shadow: none !important;
}

/* ---- accent discipline -------------------------------------------------- */
.btn-primary {
  --bs-btn-bg: var(--lime);
  --bs-btn-border-color: var(--lime);
  --bs-btn-color: var(--lime-ink);
  --bs-btn-hover-bg: var(--lime);
  --bs-btn-hover-border-color: var(--lime);
  --bs-btn-hover-color: var(--lime-ink);
  --bs-btn-active-bg: var(--lime);
  --bs-btn-active-color: var(--lime-ink);
  --bs-btn-disabled-bg: var(--lime);
  --bs-btn-disabled-border-color: var(--lime);
  --bs-btn-disabled-color: var(--lime-ink);
  font-weight: 500;
}

/* ---- focus: one visible ring everywhere (admin.css had none) ----------- */
:where(a, button, input, select, textarea, summary, [tabindex]):focus-visible {
  outline: 2px solid var(--lime);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

/* ---- motion ------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---- stat tiles: one figure, mono + tabular, hairline structure ---------
 * Ported from the ops panel's .roi-block so the CS console stops rendering
 * money as <p> tags with colons. Dense on purpose: these sit in a dashboard
 * header, so they use hairline dividers instead of boxed padding. */
.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(7rem, 1fr));
  gap: 0;
  border: 1px solid var(--line);
  border-radius: var(--r);
  overflow: hidden;
}

/* One shared frame, divided by hairlines — no stacked card padding. */
.stat-tile {
  background: var(--surface);
  padding: var(--sp-2) var(--sp-3);
  min-width: 0;
  border-left: 1px solid var(--line);
}

.stat-tile:first-child { border-left: 0; }

.stat-tile.accent {
  background: linear-gradient(160deg, var(--lime-dim), var(--surface) 60%);
}

/* Compact dashboard cards: tighter body so the header does not eat the fold. */
.card-compact > .card-body { padding: var(--sp-3) var(--sp-4); }
.card-compact h2 { margin-bottom: var(--sp-2) !important; }

.stat-value {
  margin: 1px 0 0;
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-size: var(--fs-lg);
  font-weight: 500;
  letter-spacing: var(--tr-tight);
  line-height: var(--lh-tight);
  color: var(--text);
  overflow-wrap: anywhere;
}

.stat-tile.accent .stat-value { color: var(--lime-text); }
.stat-sub { margin: var(--sp-1) 0 0; color: var(--muted); font-size: var(--fs-xs); }

/* ---- skeletons: replace the literal "—" and "…" placeholders -----------
 * An element keeps .is-loading until real content arrives; admin.js clears it
 * with a MutationObserver, so no call site has to change. */
.is-loading {
  position: relative;
  color: transparent !important;
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--line-soft) 37%, var(--surface-2) 63%);
  background-size: 400% 100%;
  border-radius: var(--r-sm);
  animation: lm-shimmer 1.4s ease-in-out infinite;
  min-height: 1em;
  user-select: none;
}

.is-loading > * { visibility: hidden; }

@keyframes lm-shimmer {
  0%   { background-position: 100% 50%; }
  100% { background-position: 0 50%; }
}

@media (prefers-reduced-motion: reduce) {
  .is-loading { animation: none; }
}

/* ---- patient photos in a message thread -------------------------------- */
/* Fetched as a blob (the endpoint is authenticated), so the element gets an
   object URL rather than a token-bearing src. */
.bubble-img {
  display: block;
  max-width: min(17rem, 100%);
  max-height: 19rem;
  width: auto;
  height: auto;
  margin-bottom: var(--sp-2);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  background: var(--surface-2);
  object-fit: contain;
}

.bubble-img.is-loading {
  width: 11rem;
  height: 8rem;
}

/* ---- unified buttons (#990) -----------------------------------------------
 * .btn-lm composes with Bootstrap's .btn (or stands alone) and only sets size,
 * radius and the busy state, so existing colour variants keep working.
 *   <button class="btn btn-primary btn-lm btn-lm-md">   sm 2rem ? md 2.5rem ? lg 3rem
 * Set aria-busy="true" while a request is in flight: the label dims, a spinner
 * shows and clicks are ignored (lmBusy(btn, true) in app.js does all three). */
.btn-lm {
  --lm-btn-h: 2.5rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  min-height: var(--lm-btn-h);
  padding: 0 var(--sp-4);
  border-radius: var(--radius-md);
  font-family: var(--sans);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  line-height: 1;
  position: relative;
}
.btn-lm-sm { --lm-btn-h: 2rem; padding: 0 var(--sp-3); font-size: var(--text-xs); border-radius: var(--radius-sm); }
.btn-lm-md { --lm-btn-h: 2.5rem; }
.btn-lm-lg { --lm-btn-h: 3rem; padding: 0 var(--sp-5); font-size: var(--text-base); border-radius: var(--radius-lg); }

.btn-lm[aria-busy="true"],
.btn[aria-busy="true"] {
  pointer-events: none;
  cursor: progress;
}
.btn-lm[aria-busy="true"]::after,
.btn[aria-busy="true"]::after {
  content: "";
  flex: 0 0 auto;
  width: 0.9em;
  height: 0.9em;
  margin-left: var(--sp-2);
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: var(--radius-pill);
  animation: lm-spin 0.7s linear infinite;
}
@keyframes lm-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  /* the global rule shortens animations to 0.01ms, which would freeze the spinner as a ring */
  .btn-lm[aria-busy="true"]::after,
  .btn[aria-busy="true"]::after { animation: none; border-right-color: currentColor; opacity: 0.5; }
}

/* ---- optional accent remaps (account-menu colour picker) -----------------
 * Keys match theme-boot.js / app.js; "forest" is the default slot and carries
 * the brand lime. Light-mode --lime-text values clear AA on ground/surface.
 * Measured (#1160), worst case per accent, light --lime-text / dark --lime on
 * ground, surface, surface-2 and a --lime-dim pill over each: ocean 6.8 / 6.2,
 * coral 5.2 / 6.0, slate 6.2 / 5.4, sand 4.75 / 6.3, amber 5.0 / 6.2.
 * tests/test_console_sweep3.py re-measures them. */
[data-accent="ocean"]  { --lime: #7eb8d4; --lime-text: #1f4f63; --lime-dim: rgba(126,184,212,.16); --lime-line: rgba(31,79,99,.42); }
[data-accent="coral"]  { --lime: #e8a090; --lime-text: #9c3a29; --lime-dim: rgba(232,160,144,.18); --lime-line: rgba(156,58,41,.42); }
[data-accent="slate"]  { --lime: #9aa8b5; --lime-text: #43505c; --lime-dim: rgba(154,168,181,.18); --lime-line: rgba(67,80,92,.42); }
[data-accent="sand"]   { --lime: #d4b896; --lime-text: #7a5c33; --lime-dim: rgba(212,184,150,.20); --lime-line: rgba(122,92,51,.42); }
[data-accent="amber"]  { --lime: #e0b45c; --lime-text: #7d5605; --lime-dim: rgba(224,180,92,.20); --lime-line: rgba(125,86,5,.42); }

/* In dark mode the accent is legible as text, so --lime-text follows --lime. */
[data-bs-theme="dark"][data-accent="ocean"]  { --lime-text: #7eb8d4; }
[data-bs-theme="dark"][data-accent="coral"]  { --lime-text: #e8a090; }
[data-bs-theme="dark"][data-accent="slate"]  { --lime-text: #9aa8b5; }
[data-bs-theme="dark"][data-accent="sand"]   { --lime-text: #d4b896; }
[data-bs-theme="dark"][data-accent="amber"]  { --lime-text: #e0b45c; }

/* Swatches in the picker show the colour as it will actually appear. */
.theme-swatch {
  display: inline-block;
  width: 0.75rem;
  height: 0.75rem;
  border-radius: var(--r-pill);
  border: 1px solid var(--line);
  background: var(--lime);
}
.theme-swatch[data-accent="forest"] { background: #c7f751; }
.theme-swatch[data-accent="ocean"]  { background: #7eb8d4; }
.theme-swatch[data-accent="coral"]  { background: #e8a090; }
.theme-swatch[data-accent="slate"]  { background: #9aa8b5; }
.theme-swatch[data-accent="sand"]   { background: #d4b896; }
.theme-swatch[data-accent="amber"]  { background: #e0b45c; }

/* ---- Tablet / touch (#2155, #2156): shared buttons grow to the 44 px target on coarse pointers */
@media (pointer: coarse) {
  .btn-lm,
  .btn-lm-sm,
  .btn-lm-md { --lm-btn-h: 2.75rem; }
}
