:root {
  --bg: #f4f5f7; --panel: #fff; --text: #15171c; --muted: #6b7280; --line: #e3e5e9;
  --accent: #1f6feb; --accent-ink: #fff; --accent-soft: #e8f0fe;
  --red: #c62828; --amber: #a15c00; --amber-bg: #fff6e5; --green: #1a7f37;
  --radius: 12px; --nav-h: 62px; --shadow: 0 1px 2px rgba(0,0,0,.05), 0 1px 6px rgba(0,0,0,.04);
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115; --panel: #181b21; --text: #e8eaed; --muted: #9aa0a6; --line: #2a2f37;
    --accent: #5b9bff; --accent-ink: #0b1220; --accent-soft: #1b2a44;
    --red: #ff6b6b; --amber: #f0b45c; --amber-bg: #2a2112; --green: #4cc26a; --shadow: none;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
body.noscroll { overflow: hidden; }
a { color: var(--accent); text-decoration: none; }
h1 { font-size: 22px; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 22px 0 8px; font-weight: 600; }
h3 { font-size: 15px; margin: 0 0 10px; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
code.block { display: block; padding: 10px; background: var(--bg); border-radius: 8px; word-break: break-all; margin: 6px 0; }
.muted { color: var(--muted); }
.red { color: var(--red); }
.err { color: var(--red); }
.loading, .empty { padding: 48px 20px; text-align: center; color: var(--muted); }
.hint { font-size: 13px; margin-top: 10px; }

/* ---------- Forms ---------- */
label { display: block; font-size: 13px; color: var(--muted); margin: 0 0 10px; }
.lbl { display: block; font-size: 13px; color: var(--muted); margin: 0 0 6px; }
input, select, textarea {
  display: block; width: 100%; margin-top: 4px; padding: 10px 12px;
  font: inherit; font-size: 16px; color: var(--text); background: var(--panel);
  border: 1px solid var(--line); border-radius: 10px; outline: none; -webkit-appearance: none; appearance: none;
}
select { background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 55%, calc(100% - 13px) 55%; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 32px; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; min-height: 44px; }
input[type=checkbox], input[type=radio] { width: auto; display: inline; margin: 0; -webkit-appearance: auto; appearance: auto; }
label.check { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 15px; }
.grid2 { display: grid; grid-template-columns: 1fr; gap: 0 12px; }
@media (min-width: 560px) { .grid2 { grid-template-columns: 1fr 1fr; } .grid2 .full { grid-column: 1 / -1; } }
.field { margin-bottom: 10px; }
.warn-box { background: var(--amber-bg); color: var(--amber); padding: 8px 10px; border-radius: 8px; font-size: 13px; margin-bottom: 10px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 14px; min-height: 42px; font: inherit; font-weight: 600; font-size: 15px;
  color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 10px; cursor: pointer;
}
.btn:disabled { opacity: .55; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { background: var(--red); border-color: var(--red); color: #fff; }
.btn.danger.ghost { background: transparent; color: var(--red); border-color: var(--line); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 34px; padding: 6px 10px; font-size: 13.5px; }
.btn.block { display: flex; width: 100%; margin-top: 10px; }
.btn .i { width: 18px; height: 18px; }
.link { background: none; border: 0; color: var(--accent); font: inherit; padding: 10px 0 0; cursor: pointer; }
.iconbtn { background: none; border: 0; color: var(--text); padding: 6px; margin-left: -8px; cursor: pointer; }
.iconbtn .i { width: 24px; height: 24px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 12px; }
.chip { border: 1px solid var(--line); background: var(--panel); color: var(--text); border-radius: 999px; padding: 6px 11px; font: inherit; font-size: 13px; cursor: pointer; }
.chip:active { background: var(--accent-soft); }

/* ---------- Layout / nav ---------- */
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; background: var(--panel); border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b); height: calc(var(--nav-h) + var(--safe-b));
}
.nav .brand { display: none; }
.nav a { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--muted); font-size: 11px; font-weight: 600; position: relative; }
.nav a .i { width: 23px; height: 23px; }
.nav a.on { color: var(--accent); }
.badge { position: absolute; top: 6px; left: calc(50% + 6px); min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--red); color: #fff; font-size: 11px; line-height: 18px; text-align: center; }
.rt { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); margin-left: 6px; vertical-align: middle; }
.rt.on { background: var(--green); }
main { padding: 0 0 calc(var(--nav-h) + var(--safe-b) + 24px); }
.page { max-width: 780px; margin: 0 auto; padding: 16px 14px; padding-top: max(16px, env(safe-area-inset-top)); }
.page.narrow { max-width: 620px; }
.page.wide { max-width: none; }
.page-h { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; margin-bottom: 6px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin: 12px 0; box-shadow: var(--shadow); }
.panel-h { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.panel-h h3 { margin: 0; }

@media (min-width: 900px) {
  .nav { top: 0; right: auto; width: 200px; height: auto; flex-direction: column; justify-content: flex-start;
    border-top: 0; border-right: 1px solid var(--line); padding: 16px 10px; gap: 2px; }
  .nav .brand { display: block; font-weight: 800; font-size: 18px; padding: 6px 12px 16px; }
  .nav a { flex: none; flex-direction: row; justify-content: flex-start; gap: 10px; font-size: 14.5px; padding: 10px 12px; border-radius: 10px; }
  .nav a.on { background: var(--accent-soft); }
  .badge { position: static; margin-left: auto; }
  main { margin-left: 200px; padding-bottom: 40px; }
  .page { padding: 24px; }
}

/* ---------- Cards ---------- */
.sec h2 .count { background: var(--line); color: var(--text); border-radius: 999px; padding: 1px 7px; font-size: 11px; margin-left: 4px; }
.sec.warn h2 { color: var(--red); }
.card {
  display: flex; align-items: center; gap: 10px; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px 12px 12px 14px; margin-bottom: 8px; cursor: pointer; box-shadow: var(--shadow);
}
.card.warn { border-left: 4px solid var(--red); }
.card-main { flex: 1; min-width: 0; }
.card-title { font-weight: 650; font-size: 16px; }
.card-sub { font-size: 13.5px; color: var(--muted); margin-top: 2px; }
.card-meta { font-size: 13px; margin-top: 6px; display: flex; flex-wrap: wrap; gap: 4px 6px; align-items: center; }
.card-note { font-size: 13.5px; margin-top: 6px; padding: 6px 8px; background: var(--bg); border-radius: 8px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.btn-call { flex: none; width: 48px; height: 48px; border-radius: 50%; display: grid; place-items: center;
  background: var(--green); color: #fff; }
.btn-call .i { width: 22px; height: 22px; }
.stage { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
.stage.mid { background: #f3e8ff; color: #7c3aed; }
.stage.won { background: #e6f6ea; color: var(--green); }
.stage.lost { background: var(--line); color: var(--muted); }
@media (prefers-color-scheme: dark) { .stage.mid { background: #2a1f3d; color: #c4a5ff; } .stage.won { background: #15301d; } }
.tag { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--bg); border: 1px solid var(--line); color: var(--muted); }
.tag.cold { background: #e0f2fe; color: #0369a1; border-color: transparent; font-weight: 700; }
@media (prefers-color-scheme: dark) { .tag.cold { background: #0c2a3d; color: #7dd3fc; } }

/* ---------- Filters ---------- */
.filters { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; margin: 10px 0 14px; }
.filters input { grid-column: 1 / -1; margin: 0; }
.filters select { margin: 0; font-size: 14px; padding: 9px 28px 9px 10px; }
@media (min-width: 900px) { .filters { grid-template-columns: 2fr 1fr 1fr 1fr; } .filters input { grid-column: auto; } }

/* ---------- Pipeline ---------- */
.board { display: flex; gap: 10px; overflow-x: auto; scroll-snap-type: x mandatory; padding-bottom: 8px; -webkit-overflow-scrolling: touch; }
.col { flex: 0 0 82vw; max-width: 300px; scroll-snap-align: start; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); display: flex; flex-direction: column; }
.col.over { outline: 2px dashed var(--accent); }
.col-h { padding: 10px 12px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; gap: 8px; font-size: 13.5px; }
.col-h span { color: var(--muted); white-space: nowrap; }
.col.lost .col-h b { color: var(--muted); }
.col-body { padding: 8px; overflow-y: auto; max-height: calc(100vh - 210px); }
.mini { background: var(--bg); border-radius: 10px; padding: 9px 10px; margin-bottom: 6px; cursor: pointer; }
.mini-t { font-weight: 600; font-size: 14px; }
.mini-s { font-size: 12.5px; color: var(--muted); }
@media (min-width: 900px) { .col { flex-basis: 260px; } .col-body { max-height: calc(100vh - 150px); } }

/* ---------- Lead page ---------- */
.lh-top { display: flex; align-items: center; gap: 4px; }
.lh-title { min-width: 0; }
.lh-title h1 { overflow-wrap: anywhere; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.actions { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 12px 0 8px; }
.act { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 10px 4px; border-radius: 12px;
  background: var(--panel); border: 1px solid var(--line); color: var(--accent); font-size: 13px; font-weight: 600; }
.act .i { width: 22px; height: 22px; }
.act.call { background: var(--green); border-color: var(--green); color: #fff; }
.act.disabled { color: var(--muted); opacity: .5; }
.actions2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.facts { display: grid; grid-template-columns: 1fr; gap: 0; padding: 4px 14px; }
.fact { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--line); margin: 0; font-size: 14px; }
.fact:last-child { border-bottom: 0; }
.fact > span { color: var(--muted); flex: none; }
.fact b { font-weight: 600; text-align: right; }
.fact select { width: auto; max-width: 60%; margin: 0; padding: 6px 30px 6px 10px; font-weight: 600; }
.leftoff { display: block; }
.leftoff textarea { margin-top: 0; }
.pills { display: flex; flex-wrap: wrap; gap: 6px; }
.pill input, .opt input, .seg input { position: absolute; opacity: 0; pointer-events: none; }
.pill span { display: inline-block; padding: 7px 11px; border-radius: 999px; border: 1px solid var(--line); color: var(--text); font-size: 13.5px; cursor: pointer; }
.pill input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.pill, .opt, .seg { margin: 0; position: relative; }
.lead-cols { display: flex; flex-direction: column; }
@media (min-width: 1100px) {
  .page.lead-page { max-width: 1200px; }
  .lead-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; align-items: start; }
  .col-hist { order: 2; position: sticky; top: 12px; max-height: calc(100vh - 24px); overflow: auto; }
}
.hist .h-item { padding: 10px 0 10px 14px; border-left: 3px solid var(--line); margin-left: 2px; }
.hist .h-item + .h-item { margin-top: 2px; }
.h-item.h-call { border-left-color: var(--green); }
.h-item.h-call.missing { border-left-color: var(--red); }
.h-item.h-stage { border-left-color: #7c3aed; }
.h-item.h-note { border-left-color: var(--accent); }
.h-item.h-estimate { border-left-color: var(--amber); }
.h-head { font-size: 14px; }
.h-meta { font-size: 12.5px; color: var(--muted); }
.h-body { font-size: 14px; margin-top: 4px; white-space: pre-wrap; overflow-wrap: anywhere; }
.h-item .btn { margin-top: 6px; }
.photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 6px; }
.ph { padding: 0; border: 0; border-radius: 8px; overflow: hidden; aspect-ratio: 1; background: var(--bg); cursor: pointer; }
.ph img { width: 100%; height: 100%; object-fit: cover; display: block; }
.viewer { width: 100%; max-height: 65vh; object-fit: contain; border-radius: 8px; background: #000; }
.savebar {
  position: fixed; left: 0; right: 0; bottom: calc(var(--nav-h) + var(--safe-b)); z-index: 25;
  display: flex; align-items: center; gap: 8px; padding: 10px 14px; background: var(--panel); border-top: 1px solid var(--line);
  box-shadow: 0 -4px 14px rgba(0,0,0,.08);
}
.savebar span { flex: 1; font-weight: 600; }
@media (min-width: 900px) { .savebar { left: 200px; bottom: 0; } }

/* ---------- Sheet ---------- */
#sheet { position: fixed; inset: 0; z-index: 50; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.42); }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 92vh; overflow-y: auto;
  background: var(--panel); border-radius: 18px 18px 0 0; padding: 18px 16px calc(16px + var(--safe-b));
  animation: up .18s ease-out;
}
.sheet-panel h2 { font-size: 18px; text-transform: none; letter-spacing: 0; color: var(--text); margin: 0 0 2px; }
.sheet-panel > p { margin: 0 0 12px; }
@keyframes up { from { transform: translateY(30px); opacity: .6; } to { transform: none; opacity: 1; } }
@media (min-width: 700px) {
  .sheet-panel { left: 50%; top: 50%; bottom: auto; right: auto; transform: translate(-50%, -50%); width: 480px; border-radius: 16px; animation: none; }
}
.sheet-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; flex-wrap: wrap; }
.sheet-actions .btn { flex: 1; }
.outcomes { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 10px 0 14px; }
.opt span { display: flex; align-items: center; justify-content: center; text-align: center; min-height: 50px; padding: 8px;
  border: 1px solid var(--line); border-radius: 12px; font-weight: 600; color: var(--text); cursor: pointer; }
.opt input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.opt:last-child:nth-child(odd) { grid-column: 1 / -1; }
.segs { display: flex; gap: 8px; margin: 4px 0 12px; }
.seg { flex: 1; }
.seg span { display: block; text-align: center; padding: 10px; border: 1px solid var(--line); border-radius: 10px; font-weight: 600; color: var(--text); cursor: pointer; }
.seg input:checked + span { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.ofields { margin-bottom: 4px; }

/* ---------- Login + toast ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login form { width: 100%; max-width: 360px; }
.login h1 { margin-bottom: 14px; }
#toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + var(--safe-b) + 14px); transform: translate(-50%, 20px);
  background: #222; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 60;
  opacity: 0; pointer-events: none; transition: .2s; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (min-width: 900px) { #toast { bottom: 24px; left: calc(50% + 100px); } }
