/* Lead Management System: single stylesheet (spec §14). Mobile first, light and dark from one file (NFR-23).
   Contrast pairs meet WCAG AA 4.5:1 (NFR-22). Interactive targets are at least 44px (NFR-18). */

:root {
  color-scheme: light dark;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #1a1d21;
  --muted: #555e6b;
  --border: #d5d9df;
  --primary: #1d5bb8;
  --primary-text: #ffffff;
  --primary-soft: #e3ecfa;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn-text: #7a4100;
  --warn-soft: #fff3e0;
  --ok-text: #1b6b35;
  --ok-soft: #e6f4ea;
  --overdue: #b42318;
  --today: #1d5bb8;
  --mark: #ffe08a;
  --mark-text: #3d2c00;
  --shadow: 0 1px 2px rgb(0 0 0 / 8%), 0 2px 8px rgb(0 0 0 / 6%);
  --radius: 10px;
  --tap: 44px;
  --nav-h: 60px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101317;
    --surface: #1a1e23;
    --surface-2: #252a31;
    --text: #e8eaed;
    --muted: #a8b0bb;
    --border: #39404a;
    --primary: #8ab4f8;
    --primary-text: #0b1a33;
    --primary-soft: #1d2a40;
    --danger: #f28b82;
    --danger-soft: #3a1f1d;
    --warn-text: #fdd8a5;
    --warn-soft: #3a2a12;
    --ok-text: #a8dab5;
    --ok-soft: #17301f;
    --overdue: #f28b82;
    --today: #8ab4f8;
    --mark: #6b5400;
    --mark-text: #fff3c4;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
  }
}

*, *::before, *::after { box-sizing: border-box; }

/* Components below set display; the hidden attribute must still win. */
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 var(--font);
}

main {
  max-width: 720px;
  margin: 0 auto;
  padding: 12px 16px 24px;
}
main.with-nav { padding-bottom: calc(var(--nav-h) + 32px + env(safe-area-inset-bottom)); }
body[data-page="lead"] main.with-nav { padding-bottom: calc(var(--nav-h) + 96px + env(safe-area-inset-bottom)); }
main.narrow { max-width: 440px; padding-top: 32px; }

h1 { font-size: 1.45rem; line-height: 1.2; margin: 8px 0 12px; }
h2 { font-size: 1.1rem; margin: 24px 0 8px; }
h3 { font-size: 1rem; margin: 16px 0 8px; }
p { margin: 0 0 8px; }
a { color: var(--primary); }
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .9em; }
pre.code { background: var(--surface-2); padding: 12px; overflow-x: auto; border-radius: var(--radius); }

.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; margin: -1px; padding: 0; border: 0;
}
.skip { position: absolute; left: -9999px; }
.skip:focus { left: 8px; top: 8px; z-index: 100; background: var(--surface); padding: 8px 12px; }

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

.icon { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; vertical-align: -4px; }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 8px 16px; min-height: 48px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--text); text-decoration: none; }
.whoami { color: var(--muted); font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Buttons and fields */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: var(--tap); min-width: var(--tap); padding: 8px 14px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover { border-color: var(--primary); }
.btn:disabled, .btn[aria-busy="true"] { opacity: .6; cursor: progress; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--primary); }
.btn.small { padding: 6px 10px; font-size: .92rem; }
.btn.block { display: flex; width: 100%; }
.btn.call { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.linklike { background: none; border: 0; padding: 0; color: var(--primary); font: inherit; cursor: pointer; text-align: left; min-height: var(--tap); }

.stack { display: grid; gap: 12px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.row.two { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.row.two > .field { min-width: 0; }

.field { display: grid; gap: 4px; font-weight: 600; font-size: .95rem; }
.field.inline { display: flex; align-items: center; gap: 8px; }
.req, .hint { color: var(--muted); font-weight: 400; font-size: .9rem; }
.check { display: flex; align-items: center; gap: 10px; min-height: var(--tap); }
.check input { width: 22px; height: 22px; flex: none; }

input, select, textarea {
  width: 100%; min-height: var(--tap); padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text);
  font: inherit; font-weight: 400;
}
textarea { resize: vertical; }
input[type="checkbox"], input[type="radio"] { width: auto; min-height: 0; accent-color: var(--primary); }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 12px 12px; margin: 0; }
legend { font-weight: 600; padding: 0 4px; }

.form-msg:empty { display: none; }
.form-msg { color: var(--danger); font-weight: 600; }
.form-msg.ok { color: var(--ok-text); }
.form-msg ul { margin: 6px 0; padding-left: 20px; }

.banner { padding: 10px 12px; border-radius: var(--radius); margin: 8px 0 12px; }
.banner.error { background: var(--danger-soft); color: var(--danger); font-weight: 600; }
.banner.warn { background: var(--warn-soft); color: var(--warn-text); }
.banner.ok { background: var(--ok-soft); color: var(--ok-text); font-weight: 600; }
#offline { margin: 0; border-radius: 0; text-align: center; }
.warn-text { color: var(--warn-text); font-weight: 600; }

.badge {
  display: inline-block; padding: 1px 8px; margin-right: 6px; border-radius: 999px;
  font-size: .8rem; font-weight: 700; background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border);
}
.stage-interested, .stage-meeting, .stage-proposal { background: var(--primary-soft); }
.stage-won { background: var(--ok-soft); color: var(--ok-text); }
.stage-lost { text-decoration: line-through; color: var(--muted); }

/* Due labels: always text plus an icon for overdue, never colour alone (NFR-22). */
.due { font-weight: 700; margin-right: 6px; white-space: nowrap; }
.due.overdue { color: var(--overdue); }
.due.today { color: var(--today); }
.due.later { color: var(--muted); }
.due .icon { width: 16px; height: 16px; margin-right: 3px; }

/* Header strip (FR-70) */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 4px 0 12px; }
.stats > a, .stats > span {
  display: grid; justify-items: center; align-content: center; gap: 0; padding: 8px 4px; min-height: 60px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); text-decoration: none; text-align: center;
}
.stats strong { font-size: 1.3rem; line-height: 1.1; }
.stats span span, .stats a span { font-size: .75rem; color: var(--muted); }

.search { display: flex; gap: 8px; margin-bottom: 10px; }
.search input { flex: 1; }

.filters { margin-bottom: 12px; }
.chips { display: flex; gap: 6px; border: 0; padding: 0; overflow-x: auto; }
.chips label { flex: 1; }
.chips input { position: absolute; opacity: 0; pointer-events: none; }
.chips span {
  display: flex; align-items: center; justify-content: center; min-height: var(--tap); padding: 0 12px;
  border: 1px solid var(--border); border-radius: 999px; background: var(--surface); font-weight: 600; cursor: pointer; white-space: nowrap;
}
.chips input:checked + span { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.chips input:focus-visible + span { outline: 3px solid var(--primary); outline-offset: 2px; }
.filter-more { margin-top: 8px; }
.filter-more summary, details.edit > summary, details.add > summary, details.correct > summary, .details-edit > summary {
  min-height: var(--tap); display: flex; align-items: center; gap: 8px; cursor: pointer; color: var(--primary); font-weight: 600;
}
.filter-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 8px 0; }
.result-head { color: var(--muted); }

/* Lead cards */
.cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.card {
  display: flex; align-items: stretch; gap: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.card.is-overdue { border-left: 5px solid var(--overdue); }
.card-main { flex: 1; min-width: 0; display: grid; gap: 2px; padding: 10px 0 10px 12px; color: var(--text); text-decoration: none; }
.card-title { font-weight: 700; font-size: 1.05rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-meta, .card-last { color: var(--muted); font-size: .88rem; }
.card-next { font-size: .95rem; }
.card-actions { display: flex; align-items: center; padding: 8px; }
.card-actions .btn.call { min-width: 64px; flex-direction: column; gap: 0; font-size: .8rem; padding: 6px 8px; }
.card-actions .btn.call .icon { width: 22px; height: 22px; }
.more-row { list-style: none; }
.empty { text-align: center; padding: 32px 8px; color: var(--muted); }
.empty .btn { margin-top: 8px; }

/* Lead detail */
.crumb { margin: 0; }
.lead-head .meta { color: var(--muted); }
.stage-form { margin: 8px 0; }
.stage-form select { width: auto; min-width: 160px; }
.next-box {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.next-box.is-overdue { border-left: 5px solid var(--overdue); }
.next-box p { margin: 0; }
.next-line { font-weight: 600; }

.phones, .contacts, .timeline, .plain-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.phones > li, .contacts > li, .plain-list > li {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px;
}
.phone-line { display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: baseline; }
.phone-num { font-weight: 700; font-variant-numeric: tabular-nums; }
.phone-label { color: var(--muted); font-size: .88rem; }
.phone-actions { display: flex; gap: 8px; margin-top: 6px; }
.is-dead .phone-num { text-decoration: line-through; color: var(--muted); }
.is-inactive { opacity: .75; }
.contact-name { font-weight: 700; margin: 0; }
details.add { margin-top: 8px; }
details[open] > form, details[open] > .admin-zone { margin-top: 8px; }
.admin-zone { border-top: 1px dashed var(--border); padding-top: 8px; margin-top: 12px; }

.timeline { gap: 0; border-left: 2px solid var(--border); margin-left: 6px; }
.act { padding: 8px 0 12px 14px; position: relative; }
.act::before { content: ""; position: absolute; left: -7px; top: 14px; width: 12px; height: 12px; border-radius: 50%; background: var(--surface); border: 2px solid var(--muted); }
.act-head { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 0; }
.act-title { font-weight: 700; }
.act-notes { white-space: normal; margin: 4px 0; }
.act-meta { color: var(--muted); font-size: .85rem; margin: 2px 0 0; }
.act-correction { background: var(--warn-soft); color: var(--warn-text); padding: 6px 8px; border-radius: 8px; margin: 4px 0; }
.act-stage .act-title { font-weight: 600; color: var(--muted); }
.email-body, .mail-body { white-space: pre-wrap; word-wrap: break-word; font-family: var(--font); margin: 6px 0 0; }
.details-edit { margin-top: 24px; }

/* Bottom action bar on a lead, above the nav: primary actions in the lower half (NFR-19). */
.actionbar {
  position: fixed; left: 0; right: 0; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); z-index: 20;
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 6px; padding: 8px 12px;
  background: var(--surface); border-top: 1px solid var(--border);
}
.actionbar .btn { flex-direction: column; gap: 0; font-size: .8rem; padding: 6px 4px; }

/* Bottom navigation */
.bottomnav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--nav-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border);
}
.bottomnav > a, .bottomnav summary {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  height: var(--nav-h); color: var(--muted); text-decoration: none; font-size: .75rem; font-weight: 600; cursor: pointer; list-style: none;
}
.bottomnav summary::-webkit-details-marker { display: none; }
.bottomnav [aria-current="page"] { color: var(--primary); }
.bottomnav .nav-new .icon { color: var(--primary); }
.more { position: relative; }
.more-menu {
  position: absolute; right: 8px; bottom: calc(var(--nav-h) + 4px); min-width: 220px;
  display: grid; padding: 6px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.more-menu a, .more-menu .linklike { display: flex; align-items: center; min-height: var(--tap); padding: 0 12px; color: var(--text); text-decoration: none; width: 100%; }
.more-menu form { margin: 0; }

/* Bottom sheets (dialogs) */
dialog.sheet {
  width: 100%; max-width: 560px; margin: auto auto 0; padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  border: 0; border-radius: 16px 16px 0 0; background: var(--surface); color: var(--text);
  max-height: 92vh; overflow-y: auto;
}
dialog.sheet::backdrop { background: rgb(0 0 0 / 45%); }
dialog.sheet h2 { margin: 0 0 2px; }
dialog.sheet form { display: grid; gap: 12px; }
.sub { color: var(--muted); margin: 0 0 8px; }
.chooser-list { list-style: none; margin: 0 0 12px; padding: 0; display: grid; gap: 8px; }
.chooser-list li { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--border); }
.chooser-list .num { font-weight: 700; display: block; }
.chooser-list .lbl { color: var(--muted); font-size: .88rem; }
.seg { display: flex; gap: 6px; border: 0; padding: 0; overflow-x: auto; }
.seg label { flex: 1; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: flex; align-items: center; justify-content: center; min-height: var(--tap); padding: 0 10px; border: 1px solid var(--border); border-radius: 8px; font-weight: 600; cursor: pointer; }
.seg input:checked + span { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.seg input:focus-visible + span { outline: 3px solid var(--primary); outline-offset: 2px; }
.outcomes { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.outcome { width: 100%; }
.outcome[data-outcome="connected"] { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.quick-dates { display: flex; flex-wrap: wrap; gap: 6px; }

/* Compose */
.preview .mail { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; }
.mail p { margin: 0 0 6px; }
.mail-k { display: inline-block; min-width: 64px; color: var(--muted); font-size: .88rem; }
.mail-body { border-top: 1px solid var(--border); padding-top: 10px; }
mark.unfilled { background: var(--mark); color: var(--mark-text); padding: 0 3px; border-radius: 4px; font-weight: 700; outline: 2px dashed var(--mark-text); }
.confirm-box { margin-top: 12px; padding: 12px; background: var(--primary-soft); border-radius: var(--radius); }

/* Tables (import, users) */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table { border-collapse: collapse; width: 100%; font-size: .9rem; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { background: var(--surface-2); }
.map-table select { min-width: 140px; }
.map-table tr.is-header td { font-weight: 700; background: var(--surface-2); }
.map-table td { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.or { text-align: center; color: var(--muted); margin: 0; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 50;
  bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom));
  width: min(560px, calc(100% - 32px)); padding: 12px 14px; border-radius: var(--radius);
  background: var(--text); color: var(--bg); box-shadow: var(--shadow);
  display: flex; gap: 12px; align-items: center; justify-content: space-between;
}
.toast .btn { min-height: 36px; }
.toast.error { background: var(--danger); color: var(--surface); }

@media (min-width: 720px) {
  .card-main { padding-left: 16px; }
  .outcomes { grid-template-columns: repeat(3, 1fr); }
}

@media (prefers-reduced-motion: no-preference) {
  dialog.sheet[open] { animation: rise .16s ease-out; }
  @keyframes rise { from { transform: translateY(24px); } to { transform: none; } }
}
