/* LeadLine. Component classes come from js/ui.js; pages compose them. */
:root {
  --bg: #f4f5f7; --panel: #fff; --line: #e4e7eb; --line-soft: #eef0f3; --text: #1b1f24; --muted: #6b7280;
  --accent: #1d4ed8; --accent-soft: #e8efff; --warn: #b45309; --warn-soft: #fff4e0;
  --good: #047857; --good-soft: #e3f7ef; --bad: #b91c1c; --bad-soft: #fde8e8;
  --nav: #111827; --nav-text: #cbd5e1; --star: #d97706; --role: #5b21b6; --role-soft: #efe9fe;
  --radius: 8px; --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  --nav-w: 196px; --drawer-w: 560px; --pad: 20px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 var(--font); color: var(--text); background: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
h1, h2, h3 { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.err { color: var(--bad); min-height: 1.4em; }
.pad { padding: 16px var(--pad); }
.grow { flex: 1; min-width: 200px; }
[hidden] { display: none !important; }
code { font-size: 12.5px; background: var(--line-soft); padding: 1px 4px; border-radius: 4px; }
.empty { color: var(--muted); margin: 0; }
.prose { margin: 8px 0 0; }

/* ── Atoms ─────────────────────────────────────────────────────────── */
input, select, textarea { padding: 6px 9px; border: 1px solid var(--line); border-radius: 6px; background: #fff; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
textarea { width: 100%; resize: vertical; display: block; }
.btn { border: 1px solid var(--line); border-radius: 6px; padding: 6px 12px; background: #fff; display: inline-flex; align-items: center; gap: 6px; text-decoration: none; color: var(--text); line-height: 1.3; white-space: nowrap; }
.btn:hover { background: var(--line-soft); }
.btn.sm { padding: 4px 9px; font-size: 13px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.accent { background: var(--accent-soft); border-color: transparent; color: var(--accent); font-weight: 600; }
.btn.accent:hover { background: #dbe6ff; }
.btn.link { border: 0; background: none; color: var(--accent); padding: 2px 4px; }
.btn.link:hover { text-decoration: underline; background: none; }
.btn.danger { color: var(--bad); }
.btn.on { background: var(--accent-soft); border-color: var(--accent); }
.btn .ext { color: var(--muted); font-size: 11px; }
.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); vertical-align: middle; line-height: 1.5; }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.gray { background: var(--line-soft); color: var(--muted); }
.badge.role { background: var(--role-soft); color: var(--role); text-transform: capitalize; }
.score { display: inline-block; font-weight: 700; font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--line-soft); color: var(--muted); line-height: 1.5; }
.score.hi { background: var(--good-soft); color: var(--good); }
.score.mid { background: var(--warn-soft); color: var(--warn); }
.star { border: 0; background: none; color: #c4c9d1; font-size: 18px; line-height: 1; padding: 2px 6px; border-radius: 6px; flex: none; transition: color .35s ease; }
.star:hover { color: var(--star); background: var(--warn-soft); }
.star.on { color: var(--star); }
.star.big { font-size: 22px; }
.field { display: flex; flex-direction: column; gap: 3px; font-size: 13px; }
.field-label { color: var(--muted); font-size: 11.5px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 8px; }
.tb-right { display: flex; gap: 6px; align-items: center; margin-left: auto; }

/* ── Sections, facts, items ────────────────────────────────────────── */
.sec { padding: 14px var(--pad); border-bottom: 1px solid var(--line); }
.sec-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 8px; }
.sec-head h3 { font-size: 11.5px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 700; }
.sec-count { font-weight: 500; color: #9ca3af; }
.sec-hint { font-size: 12px; color: #9ca3af; }
.sec-actions { margin-left: auto; }
.sec-tools { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; align-items: flex-start; }
.sec-tools .disclosure { flex-basis: 100%; }
.facts { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px 14px; }
.fact.wide { grid-column: span 2; }
.fact-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.fact-value { font-size: 14px; font-weight: 600; margin-top: 1px; }
.fact-hint { font-size: 11.5px; color: #9ca3af; }
.item { padding: 8px 0; border-top: 1px solid var(--line-soft); font-size: 13px; }
.item:first-child { border-top: 0; padding-top: 0; }
.item-head { display: flex; align-items: center; gap: 8px; }
.item-title { font-weight: 600; flex: 1; min-width: 0; }
.item-actions { opacity: 0; }
.item:hover .item-actions { opacity: 1; }
.item-meta { color: var(--muted); font-size: 12.5px; margin-top: 1px; }
.item-body { color: var(--muted); font-size: 12.5px; margin-top: 3px; }
.disclosure { margin-top: 8px; padding: 10px 12px; background: var(--bg); border-radius: var(--radius); }
.grid-form { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
.grid-form .btn { justify-content: center; }
.import p { margin: 0 0 8px; font-size: 12.5px; }
.pick { display: flex; align-items: center; gap: 8px; padding: 4px 0; font-size: 13px; }
.note-form { margin-bottom: 8px; }

/* ── App shell: left nav + pages, drawer over the right edge ──────── */
#app { display: flex; height: 100vh; overflow: hidden; }
.sidenav { width: var(--nav-w); flex: none; background: var(--nav); color: var(--nav-text); display: flex; flex-direction: column; padding: 14px 10px; gap: 2px; }
.sidenav .brand { color: #fff; padding: 4px 10px 14px; display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 20px; letter-spacing: -0.01em; }
.sidenav a { color: var(--nav-text); text-decoration: none; padding: 8px 10px; border-radius: 6px; display: flex; align-items: center; font-weight: 500; }
.sidenav a:hover { background: rgba(255,255,255,.06); color: #fff; }
.sidenav a.on { background: rgba(255,255,255,.12); color: #fff; }
.sidenav .count { margin-left: auto; font-size: 11.5px; color: #9ca3af; }
.nav-foot { margin-top: auto; padding: 10px; font-size: 12.5px; color: #9ca3af; display: flex; flex-direction: column; gap: 6px; }
.nav-foot .user { display: flex; justify-content: space-between; align-items: center; gap: 8px; color: var(--nav-text); }
.nav-foot .btn.link { color: #93c5fd; }
.pages { flex: 1; min-width: 0; display: flex; flex-direction: column; overflow: hidden; }
.page { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.page-head { display: flex; align-items: baseline; gap: 14px; padding: 14px 22px; background: var(--panel); border-bottom: 1px solid var(--line); }
.page-head h1 { font-size: 18px; }
.page-body { overflow: auto; padding: 18px 22px; }

/* Gate */
#gate { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.gate-card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 28px; width: 100%; max-width: 380px; }
.gate-card .brand { font-weight: 700; font-size: 20px; }
.gate-card label { display: block; margin: 12px 0 4px; font-weight: 600; }
.gate-card input { width: 100%; }
.gate-card .primary { margin-top: 16px; width: 100%; background: var(--accent); color: #fff; border: 0; border-radius: 6px; padding: 9px; font-weight: 600; }
.gate-card .link { background: none; border: 0; color: var(--accent); padding: 8px 0; }

/* ── Filter bar: a fixed toolbar row, then the filter line ─────────── */
.filterbar { background: var(--panel); border-bottom: 1px solid var(--line); }
.bar-top { display: flex; align-items: center; gap: 10px; padding: 8px 18px; }
.bar-top .search { flex: 0 1 300px; min-width: 120px; }
.bar-top select { height: 30px; padding: 0 8px; font-size: 13px; }
.bar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.bar-filters { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; padding: 5px 18px; min-height: 38px; background: var(--bg); border-top: 1px solid var(--line-soft); font-size: 13px; }
.bar-filters .bar-right { font-size: 12.5px; }
.search { height: 30px; border-radius: 6px; padding: 0 12px; font-size: 13px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; background: #fff; height: 30px; }
.seg button { border: 0; background: none; padding: 0 11px; font-size: 13px; color: var(--muted); white-space: nowrap; }
.seg button:hover { color: var(--text); }
.seg button.on { background: var(--accent); color: #fff; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; flex: 1; min-width: 0; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 2px 4px 2px 10px; border: 1px solid var(--line); border-radius: 999px; background: #fff; font-size: 13px; white-space: nowrap; }
.chip-label { color: var(--muted); }
.chip input, .chip select { border: 0; background: var(--line-soft); border-radius: 999px; padding: 3px 8px; font-size: 13px; }
.chip input:focus, .chip select:focus { outline: 2px solid var(--accent-soft); }
.chip input[type=number] { width: 76px; }
.chip input[type=search] { width: 200px; }
.chip .unit { color: var(--muted); font-size: 12px; }
.chip-btn { border: 0; background: var(--line-soft); border-radius: 999px; padding: 3px 8px; font-size: 13px; }
.chip-btn .caret { color: var(--muted); font-size: 10px; }
.chip-x { border: 0; background: none; color: var(--muted); font-size: 16px; line-height: 1; padding: 0 6px; border-radius: 50%; }
.chip-x:hover { color: var(--bad); background: var(--bad-soft); }

/* Popovers and menus */
.popover { position: fixed; z-index: 1200; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,.14); min-width: 200px; max-height: 70vh; overflow: auto; }
.menu { padding: 6px; }
.menu-search { width: 100%; margin-bottom: 6px; border-radius: 6px; }
.menu-title { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: #9ca3af; padding: 8px 10px 3px; }
.menu-item { display: flex; align-items: center; justify-content: space-between; gap: 10px; width: 100%; text-align: left; border: 0; background: none; padding: 6px 10px; border-radius: 6px; font-size: 13px; cursor: pointer; }
.menu-item:hover { background: var(--accent-soft); }
.menu-hint { color: #9ca3af; font-size: 12px; }
.menu-none { padding: 8px 10px; margin: 0; }
.checks .menu-item { justify-content: flex-start; }

/* ── Leads page ────────────────────────────────────────────────────── */
.leads-body { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.map-wrap { display: none; flex: 0 0 46%; flex-direction: column; border-bottom: 1px solid var(--line); }
#app.with-map .map-wrap { display: flex; }
#map { flex: 1; min-height: 0; background: #e9ecef; position: relative; z-index: 0; isolation: isolate; }
.map-bar { display: flex; gap: 12px; align-items: center; padding: 6px 12px; background: var(--panel); font-size: 13px; }
.leaflet-container { font: inherit; }
.list { overflow: auto; flex: 1; min-height: 0; }
.list-head { display: flex; gap: 10px; align-items: center; padding: 0 12px 0 16px; height: 28px; background: var(--panel); border-bottom: 1px solid var(--line); font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.list-head .main { flex: 1; padding-left: 34px; }
.list-head .right { display: flex; gap: 14px; }
.list-head .val { min-width: 78px; text-align: right; }
.list-head .size { min-width: 130px; text-align: right; }
.list-head .when { min-width: 72px; text-align: right; }
.list-head .sortable { cursor: pointer; }
.list-head .sortable:hover, .list-head .on { color: var(--text); }
.list-head .star-gap { width: 30px; flex: none; }
.row { display: flex; gap: 10px; align-items: center; padding: 0 12px 0 16px; height: 40px; border-bottom: 1px solid var(--line-soft); cursor: pointer; background: var(--panel); font-size: 13px; }
.row:hover { background: #fafbfd; }
.row.sel { background: var(--accent-soft); }
.row .main { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; white-space: nowrap; overflow: hidden; }
.row .addr { font-weight: 600; font-size: 13.5px; }
.row .what { overflow: hidden; text-overflow: ellipsis; }
.row .right { flex: none; display: flex; gap: 14px; color: var(--muted); font-size: 12.5px; font-variant-numeric: tabular-nums; }
.row .val { color: var(--text); min-width: 78px; text-align: right; }
.row .size { min-width: 130px; text-align: right; }
.row .when { min-width: 72px; text-align: right; }

/* Work-type icon on rows */
.work { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 6px; flex: none; color: var(--accent); background: var(--accent-soft); }
.work.demolition { color: var(--bad); background: var(--bad-soft); }
.work.remodel { color: var(--role); background: var(--role-soft); }
.work.new_structure { color: var(--good); background: var(--good-soft); }
.work.permit { color: var(--muted); background: var(--line-soft); }
.work.fire { color: var(--warn); background: var(--warn-soft); }
.icon { display: block; }
.page-tools { display: flex; gap: 12px; align-items: center; margin-bottom: 12px; }
.page-tools .search { width: 300px; }
.list.inset { border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.list.inset .row { padding-left: 10px; }

/* ── Drawer ────────────────────────────────────────────────────────── */
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(var(--drawer-w), 100vw); background: var(--panel); border-left: 1px solid var(--line); box-shadow: -12px 0 32px rgba(0,0,0,.10); transform: translateX(102%); transition: transform .22s ease; z-index: 1100; display: flex; flex-direction: column; }
.drawer.open { transform: none; }
.drawer-close { position: absolute; top: 12px; right: 14px; border: 0; background: var(--line-soft); color: var(--muted); width: 28px; height: 28px; border-radius: 50%; font-size: 18px; line-height: 1; z-index: 1; }
.drawer-close:hover { background: var(--bad-soft); color: var(--bad); }
.detail { overflow: auto; flex: 1; font-size: 13px; }
.dr-head { padding: 16px 48px 12px var(--pad); border-bottom: 1px solid var(--line); }
.dr-title { display: flex; align-items: center; gap: 8px; }
.dr-title h2 { font-size: 17px; letter-spacing: -0.01em; }
.dr-sub { color: var(--muted); margin-top: 2px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.dr-actions { display: flex; gap: 6px; margin-top: 10px; flex-wrap: wrap; }
.dr-form { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; padding: 12px var(--pad); border-bottom: 1px solid var(--line); background: var(--bg); }

/* ── Dashboard, firms, runs, settings ──────────────────────────────── */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; margin-bottom: 18px; }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.tile .v { font-size: 24px; font-weight: 700; letter-spacing: -0.01em; }
.tile .l { font-weight: 600; margin-top: 2px; }
.tile .h { font-size: 11.5px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(360px, 1fr)); gap: 12px; }
.cards.one { grid-template-columns: minmax(0, 720px); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; min-width: 0; overflow: auto; }
.card h3 { margin: 0 0 8px; font-size: 11.5px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.card h3 .muted { text-transform: none; letter-spacing: 0; font-weight: 500; }
.card p { margin: 0 0 8px; }
.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
.tbl td, .tbl th { padding: 6px; border-top: 1px solid var(--line-soft); text-align: left; vertical-align: top; }
.tbl tr:first-child td { border-top: 0; }
.tbl .th th { border-top: 0; color: var(--muted); font-weight: 600; font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; white-space: nowrap; }
.tbl .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tbl .bar { width: 40%; } .tbl .bar i { display: block; height: 8px; background: var(--accent-soft); border-radius: 4px; }
.tbl.click tr { cursor: pointer; } .tbl.click tr:hover td { background: #fafbfd; }
.tbl.framed { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.tbl.framed td, .tbl.framed th { padding: 9px 12px; }

.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: #111; color: #fff; padding: 8px 14px; border-radius: 6px; font-size: 13px; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 1300; }
.toast.on { opacity: .95; }
.toast.warn { background: #ffe95c; color: #3b2f00; font-weight: 600; box-shadow: 0 6px 20px rgba(0,0,0,.18); }

@media (max-width: 900px) {
  #app { flex-direction: column; }
  .sidenav { width: auto; flex-direction: row; align-items: center; padding: 8px 10px; gap: 4px; overflow-x: auto; }
  .sidenav .brand { padding: 0 10px 0 4px; }
  .sidenav .count { margin-left: 4px; }
  .nav-foot { margin-top: 0; margin-left: auto; flex-direction: row; align-items: center; }
  .nav-foot #run-status { display: none; }
  .row .size, .list-head .size { display: none; }
  .facts { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-form { grid-template-columns: 1fr 1fr; }
}
