/* Design tokens. Every colour, radius, space and type size lives here; nothing
   below this block uses a raw colour. Light first, dark via prefers-color-scheme.
   System fonts only, so the app looks right with no network. */
:root {
  --brand: #3B2A8C;
  --brand-ink: #FFFFFF;
  --brand-soft: #ECE8FB;
  --accent: #F2A007;
  --success: #0F7A57;
  --success-soft: #E3F5EE;
  --warn: #A55F00;
  --warn-soft: #FFF2DC;
  --danger: #C62828;
  --danger-soft: #FDECEC;
  --wa: #128C4A;
  --surface: #FFFFFF;
  --surface-2: #F4F3F8;
  --bg: #ECEAF3;
  --text: #1A1630;
  --muted: #6A6585;
  --line: #E1DEEC;
  --overlay: rgba(16, 12, 36, 0.45);
  --shadow: 0 1px 2px rgba(20, 14, 50, 0.06), 0 4px 14px rgba(20, 14, 50, 0.06);

  --radius-s: 10px;
  --radius: 14px;
  --radius-l: 22px;
  --pill: 999px;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;

  --text-xs: 12px;
  --text-s: 13.5px;
  --text-m: 15px;
  --text-l: 18px;
  --text-xl: 22px;
  --text-xxl: 30px;

  --tap: 48px;
  --shell: 520px;
  --font: system-ui, -apple-system, 'Segoe UI', Roboto, 'Noto Sans', 'Noto Sans Devanagari', sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --brand: #9C8CFF;
    --brand-ink: #120C2E;
    --brand-soft: #2A2450;
    --accent: #FFB733;
    --success: #4CD39F;
    --success-soft: #11372A;
    --warn: #FFB547;
    --warn-soft: #3A2A0E;
    --danger: #FF7A7A;
    --danger-soft: #3E1A1C;
    --wa: #3FD07F;
    --surface: #1C1A2B;
    --surface-2: #252338;
    --bg: #121120;
    --text: #F1EFFA;
    --muted: #A49FC0;
    --line: #34314C;
    --overlay: rgba(0, 0, 0, 0.6);
    --shadow: none;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* Component display rules must never un-hide something the app hid. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: var(--text-m) / 1.45 var(--font);
  overscroll-behavior-y: none;
}

#app {
  max-width: var(--shell);
  margin: 0 auto;
  min-height: 100vh;
  background: var(--bg);
  display: flex;
  flex-direction: column;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---------- Header ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  background: var(--brand); color: var(--brand-ink);
  padding: calc(env(safe-area-inset-top) + var(--space-3)) var(--space-4) var(--space-3);
}
.brandrow { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); }
.topbar h1 { margin: 0; font-size: var(--text-xl); line-height: 1.2; }
.topbar .sub { font-size: var(--text-s); opacity: 0.85; }
.brandmark {
  width: 40px; height: 40px; border-radius: var(--radius-s); flex: none;
  display: grid; place-items: center; font-weight: 800; font-size: var(--text-l);
  background: var(--accent); color: var(--text);
}
.brandmark.big { width: 64px; height: 64px; font-size: var(--text-xxl); margin: 0 auto var(--space-3); }
.offline-banner {
  background: var(--warn-soft); color: var(--warn); font-size: var(--text-s);
  padding: var(--space-2) var(--space-4); font-weight: 600;
}

main { flex: 1; padding: var(--space-3) var(--space-4) calc(96px + env(safe-area-inset-bottom)); }
.screen { display: none; }
.screen.active { display: block; }

/* ---------- Text and forms ---------- */
.section {
  font-size: var(--text-s); font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--muted); margin: var(--space-5) 0 var(--space-2);
  display: flex; justify-content: space-between; align-items: baseline;
}
.section .count { font-weight: 600; text-transform: none; letter-spacing: 0; }
.hint { font-size: var(--text-s); color: var(--muted); margin-top: var(--space-2); line-height: 1.5; }
.error-text { color: var(--danger); }

label { display: block; font-size: var(--text-s); font-weight: 600; color: var(--muted); margin: var(--space-4) 0 var(--space-1); }
input, select, textarea {
  width: 100%; min-height: var(--tap);
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--radius-s);
  padding: var(--space-3);
}
textarea { min-height: 110px; resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--brand); outline-offset: 1px;
}

/* ---------- Buttons ---------- */
button, .btn {
  font: inherit; font-weight: 600; cursor: pointer;
  min-height: 44px; border-radius: var(--radius-s); border: 0;
  padding: var(--space-2) var(--space-4);
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  text-decoration: none;
}
button:disabled { opacity: 0.55; cursor: default; }
.btn-primary { width: 100%; min-height: var(--tap); background: var(--brand); color: var(--brand-ink); margin-top: var(--space-4); }
.btn-ghost { background: var(--surface-2); color: var(--text); border: 1px solid var(--line); }
.btn-danger { background: var(--danger-soft); color: var(--danger); }
.btn-call { background: var(--brand-soft); color: var(--brand); }
.btn-wa { background: var(--wa); color: #FFFFFF; }
.link-btn { background: none; color: var(--brand); min-height: 44px; padding: 0 var(--space-2); text-decoration: underline; }
.actions { display: flex; gap: var(--space-2); }
.actions > * { flex: 1; }

.chip {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: var(--text-xs); font-weight: 600;
  padding: 3px 9px; border-radius: var(--pill);
  background: var(--surface-2); color: var(--muted);
}
.chip.src { background: var(--brand-soft); color: var(--brand); }
.chip.late { background: var(--danger-soft); color: var(--danger); }
.chip.money { background: var(--success-soft); color: var(--success); }

.chip-row, .reason-chips { display: flex; flex-wrap: wrap; gap: var(--space-2); margin: var(--space-3) 0; }
.chip-btn {
  min-height: 44px; padding: var(--space-2) var(--space-4); border-radius: var(--pill);
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
}
.chip-btn.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }

/* ---------- Cards and rows ---------- */
.card {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: var(--space-4); margin-bottom: var(--space-3);
}
.lead-name { font-weight: 700; font-size: var(--text-m); }
.lead-need { color: var(--muted); font-size: var(--text-s); }
.lead-top { display: flex; justify-content: space-between; gap: var(--space-3); align-items: flex-start; }
.lead-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: var(--space-2); }

.lead-row {
  display: flex; align-items: center; gap: var(--space-3);
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: var(--space-3); margin-bottom: var(--space-2);
}
.lead-row .open {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: var(--space-3);
  background: none; padding: 0; text-align: left; color: inherit; justify-content: flex-start;
  overflow: hidden;
}
.lead-row .info { display: block; min-width: 0; flex: 1; overflow: hidden; }
.lead-row .name { display: block; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lead-row .need { display: block; color: var(--muted); font-size: var(--text-s); font-weight: 400; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lead-row .meta { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; font-size: var(--text-xs); color: var(--muted); align-items: center; }
.lead-row .quick { display: flex; gap: var(--space-2); flex: none; }
.lead-row .quick > * { width: 48px; height: 48px; padding: 0; border-radius: var(--pill); font-size: var(--text-xs); }

.avatar {
  width: 44px; height: 44px; border-radius: var(--pill); flex: none;
  display: grid; place-items: center; font-weight: 700; font-size: var(--text-s);
  background: var(--brand-soft); color: var(--brand);
}
.avatar.big { width: 64px; height: 64px; font-size: var(--text-l); }

.stage {
  display: inline-flex; align-items: center; font-size: var(--text-xs); font-weight: 700;
  padding: 3px 10px; border-radius: var(--pill); background: var(--surface-2); color: var(--muted);
}
.stage.new { background: var(--warn-soft); color: var(--warn); }
.stage.contacted, .stage.site_visit, .stage.quoted { background: var(--brand-soft); color: var(--brand); }
.stage.won { background: var(--success-soft); color: var(--success); }
.stage.lost { background: var(--surface-2); color: var(--muted); }
button.stage { min-height: 36px; }

.empty { text-align: center; color: var(--muted); padding: var(--space-6) var(--space-4); }
.empty .big { font-size: var(--text-l); font-weight: 700; color: var(--text); margin-bottom: var(--space-1); }

/* ---------- Today ---------- */
.money-strip {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(0, 1fr)); gap: 1px;
  width: 100%; padding: 0; overflow: hidden;
  background: var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
  color: var(--text);
}
.money-strip > div { background: var(--surface); padding: var(--space-3) var(--space-2); text-align: center; }
.money-strip b { display: block; font-size: var(--text-l); }
.money-strip span { font-size: var(--text-xs); color: var(--muted); }
.money-strip .warn b { color: var(--danger); }
.money-strip .good b { color: var(--success); }
.due-line { margin: var(--space-4) 0 0; font-size: var(--text-s); color: var(--muted); }
.due-line b { font-size: var(--text-xl); color: var(--text); margin-right: 4px; }

/* ---------- Leads list ---------- */
#leadSearch { margin-bottom: var(--space-2); }
.filters { display: flex; gap: var(--space-2); overflow-x: auto; padding: var(--space-1) 0 var(--space-2); scrollbar-width: none; }
.filters button {
  flex: none; min-height: 40px; border-radius: var(--pill); padding: 0 var(--space-3);
  background: var(--surface); color: var(--text); border: 1px solid var(--line); font-size: var(--text-s);
}
.filters button.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.filters button small { opacity: 0.75; margin-left: 4px; }
.list-tools { display: flex; justify-content: space-between; align-items: center; gap: var(--space-2); font-size: var(--text-s); color: var(--muted); }
.list-tools select { width: auto; min-height: 40px; padding: 0 var(--space-2); }

/* ---------- Lead detail ---------- */
.detail-head { text-align: center; padding: var(--space-3) 0; }
.detail-head .avatar { margin: 0 auto var(--space-2); }
.detail-head h2 { margin: 0; font-size: var(--text-xl); }
.detail-head .sub { color: var(--muted); font-size: var(--text-s); margin-top: 2px; }
.detail-head .lead-meta { justify-content: center; }
.back { background: none; color: var(--brand); padding: 0; min-height: 44px; }
.big-actions { display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--space-2); margin: var(--space-3) 0; }
.big-actions > * {
  flex-direction: column; min-height: 64px; font-size: var(--text-xs); gap: 2px;
  background: var(--surface); color: var(--text); border: 1px solid var(--line); border-radius: var(--radius);
}
.big-actions .ico { font-size: var(--text-l); line-height: 1; }
.big-actions .btn-wa { background: var(--wa); color: #FFFFFF; border-color: var(--wa); }
.field-grid .field { margin: 0; }
.field-grid label { margin-top: var(--space-3); }
.field-grid input, .field-grid textarea { background: var(--surface); }
.next-action { display: flex; justify-content: space-between; align-items: center; gap: var(--space-2); }

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li {
  position: relative; padding: 0 0 var(--space-3) var(--space-5);
  border-left: 2px solid var(--line); margin-left: 6px; font-size: var(--text-s);
}
.timeline li::before {
  content: ''; position: absolute; left: -7px; top: 3px;
  width: 12px; height: 12px; border-radius: var(--pill); background: var(--brand);
}
.timeline .when { display: block; color: var(--muted); font-size: var(--text-xs); }
.timeline .label { font-weight: 600; }
.timeline .body { color: var(--muted); white-space: pre-wrap; }

/* ---------- Sheets ---------- */
.sheet-bg {
  position: fixed; inset: 0; z-index: 20; background: var(--overlay);
  display: none; align-items: flex-end; justify-content: center;
}
.sheet-bg.open { display: flex; }
.sheet {
  width: 100%; max-width: var(--shell); max-height: 92vh; overflow-y: auto;
  background: var(--surface); border-radius: var(--radius-l) var(--radius-l) 0 0;
  padding: var(--space-3) var(--space-4) calc(var(--space-5) + env(safe-area-inset-bottom));
}
.sheet .grab { width: 40px; height: 5px; border-radius: var(--pill); background: var(--line); margin: 0 auto var(--space-3); }
.sheet h3 { margin: 0 0 var(--space-2); font-size: var(--text-l); }

.outcomes { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-2); margin-top: var(--space-3); }
.outcomes button { min-height: 72px; font-size: var(--text-m); border-radius: var(--radius); }
.outcomes [data-outcome="no_answer"] { background: var(--surface-2); color: var(--text); }
.outcomes [data-outcome="interested"] { background: var(--success-soft); color: var(--success); }
.outcomes [data-outcome="callback"] { background: var(--brand-soft); color: var(--brand); }
.outcomes [data-outcome="not_interested"] { background: var(--danger-soft); color: var(--danger); }

.template-chips { display: flex; gap: var(--space-2); overflow-x: auto; padding-bottom: var(--space-2); scrollbar-width: none; }
.template-chips .chip-btn { flex: none; }

/* ---------- Add lead ---------- */
.mic-wrap { position: relative; }
.mic-wrap textarea { padding-right: 64px; }
.mic {
  position: absolute; right: var(--space-2); bottom: var(--space-2);
  width: 48px; height: 48px; border-radius: var(--pill); padding: 0;
  background: var(--brand); color: var(--brand-ink); font-size: var(--text-l);
}
.mic.listening { background: var(--danger); }
.voice-language { margin-top: var(--space-2); font-size: var(--text-s); }
.parsed { display: none; margin-top: var(--space-3); }
.parsed.show { display: block; }
.parsed .row label { margin-top: var(--space-2); }
.save-row { display: grid; grid-template-columns: 1fr 1.4fr; gap: var(--space-2); margin-top: var(--space-4); }
.save-row button { min-height: var(--tap); }

/* ---------- Report ---------- */
.report-range { display: grid; gap: var(--space-2); }
.report-dates { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-2); }
.report-totals { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-2); margin-top: var(--space-3); }
.report-total { margin: 0; display: flex; flex-direction: column; gap: 2px; }
.report-total span { font-size: var(--text-xs); color: var(--muted); }
.report-total strong { font-size: var(--text-l); }
.report-table-wrap { overflow-x: auto; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.report-table { width: 100%; border-collapse: collapse; font-size: var(--text-s); }
.report-table th, .report-table td { padding: var(--space-2) var(--space-3); text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
.report-table th { color: var(--muted); font-weight: 600; font-size: var(--text-xs); }

/* ---------- More ---------- */
.more-list .card { display: block; }
.template-item { display: flex; justify-content: space-between; gap: var(--space-2); align-items: flex-start; }
.template-item .body { color: var(--muted); font-size: var(--text-s); white-space: pre-wrap; }
.stage-labels { display: grid; grid-template-columns: 1fr 1fr; gap: 0 var(--space-2); }
.banner {
  background: var(--brand-soft); color: var(--text); border-radius: var(--radius);
  padding: var(--space-3); font-size: var(--text-s); margin-bottom: var(--space-2);
}

/* ---------- Bottom navigation ---------- */
nav {
  position: fixed; left: 50%; bottom: 0; transform: translateX(-50%); z-index: 10;
  width: 100%; max-width: var(--shell);
  display: grid; grid-template-columns: repeat(5, 1fr); align-items: end;
  background: var(--surface); border-top: 1px solid var(--line);
  padding: var(--space-1) var(--space-1) calc(var(--space-1) + env(safe-area-inset-bottom));
}
nav button {
  flex-direction: column; gap: 2px; min-height: 56px; padding: var(--space-1);
  background: none; color: var(--muted); font-size: var(--text-xs); font-weight: 600;
}
nav button svg { width: 24px; height: 24px; }
nav button.on { color: var(--brand); }
nav .add-btn {
  width: 60px; height: 60px; min-height: 60px; border-radius: var(--pill); justify-self: center;
  background: var(--brand); color: var(--brand-ink); margin-top: -20px; box-shadow: var(--shadow);
}
nav .add-btn svg { width: 28px; height: 28px; }
nav .add-btn.on { color: var(--brand-ink); outline: 3px solid var(--brand-soft); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(88px + env(safe-area-inset-bottom)); z-index: 30;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--surface); font-size: var(--text-s);
  padding: var(--space-3) var(--space-4); border-radius: var(--radius-s);
  max-width: calc(100% - 32px); transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Login ---------- */
.login {
  position: fixed; inset: 0; z-index: 40; background: var(--brand);
  display: grid; place-items: center; padding: var(--space-4);
}
.login[hidden] { display: none; }
.login-card {
  width: 100%; max-width: 380px; background: var(--surface);
  border-radius: var(--radius-l); padding: var(--space-5); text-align: center;
}
.login-card h2 { margin: 0; }
.login-card .tagline { color: var(--muted); font-size: var(--text-s); margin-bottom: var(--space-3); }
.login-card .hint { margin-bottom: var(--space-3); }
.login-card input { margin-bottom: var(--space-2); text-align: center; font-size: var(--text-l); }
.login-error { color: var(--danger); font-size: var(--text-s); min-height: 20px; margin-top: var(--space-2); }
.login-link { background: none; color: var(--brand); width: 100%; margin-top: var(--space-2); }

.spend-nudge {
  width: 100%; margin: var(--space-3) 0 0; text-align: left; flex-direction: column; align-items: flex-start;
  background: var(--warn-soft); color: var(--warn); border: 1px solid var(--warn);
}
.spend-nudge .hint { margin: 0; color: var(--text); font-weight: 400; }
.lost-estimate { margin-top: var(--space-3); border: 1px solid var(--danger); }
.lost-estimate .lead-name { margin-top: var(--space-2); }
.campaign-table td .chip { margin-left: 4px; }
