:root {
  --bg: #f5f6f8; --card: #ffffff; --ink: #111827; --muted: #6b7280; --line: #e5e7eb;
  --brand: #2563eb; --brand-ink: #ffffff; --dark: #111827;
  --r1: #16a34a; --r2: #84cc16; --r3: #f59e0b; --r4: #ef4444; --r5: #7f1d1d;
  --radius: 10px;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
a { color: var(--brand); text-decoration: none; }
h1, h2, h3 { margin: 0 0 8px; line-height: 1.25; }
h1 { font-size: 20px; } h2 { font-size: 16px; } h3 { font-size: 15px; }
.muted { color: var(--muted); }
.hidden { display: none !important; }

/* header */
.top { position: sticky; top: 0; z-index: 1000; display: flex; align-items: center; gap: 18px; padding: 10px 18px; background: var(--dark); color: #fff; }
.brand { display: flex; align-items: center; gap: 8px; color: #fff; font-size: 16px; }
.brand img { width: 26px; height: 26px; border-radius: 6px; }
.top nav { display: flex; gap: 4px; }
.top nav a { color: #d1d5db; padding: 6px 10px; border-radius: 6px; }
.top nav a.active, .top nav a:hover { background: #1f2937; color: #fff; }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.pill { font-size: 12px; padding: 4px 10px; border-radius: 999px; background: #374151; color: #fff; white-space: nowrap; }
.pill.ok { background: #166534; } .pill.bad { background: #991b1b; } .pill.subtle { background: #1f2937; color: #9ca3af; }
.pill.run { background: var(--brand); color: #fff; }
.icon-btn { background: none; border: 0; color: #fff; font-size: 18px; cursor: pointer; padding: 4px 6px; }

.banner { margin: 12px 18px 0; padding: 12px 14px; border-radius: var(--radius); background: #fef3c7; border: 1px solid #fcd34d; color: #78350f; }
.banner.err { background: #fee2e2; border-color: #fca5a5; color: #7f1d1d; }
.banner.info { background: #dbeafe; border-color: #93c5fd; color: #1e3a8a; }
.banner .btn { margin-left: 8px; }
.banner ol { margin: 6px 0 0 18px; padding: 0; }

main { padding: 16px 18px 40px; }

/* cards + forms */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.layout { display: grid; grid-template-columns: 400px 1fr; gap: 16px; align-items: start; }
.layout-scan { display: grid; grid-template-columns: 1fr 420px; gap: 16px; align-items: start; }
label { display: block; font-size: 12px; font-weight: 600; color: #374151; margin-bottom: 10px; }
label.block { margin-top: 4px; }
input, select, textarea { display: block; width: 100%; margin-top: 4px; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 8px; font: inherit; background: #fff; color: var(--ink); }
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid #bfdbfe; border-color: var(--brand); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0 10px; }
.row { display: flex; gap: 8px; align-items: flex-end; }
.row > * { flex: 1; }
.row > .btn { flex: 0 0 auto; margin-bottom: 10px; }
.section-title { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 14px 0 8px; }
details summary { cursor: pointer; font-weight: 600; color: #374151; margin: 8px 0; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 0; border-radius: 8px; padding: 8px 14px; font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap; }
.btn.primary { background: var(--brand); color: var(--brand-ink); }
.btn.primary:disabled { background: #93c5fd; cursor: not-allowed; }
.btn.ghost { background: #eef0f3; color: var(--ink); }
.btn.danger { background: #fee2e2; color: #991b1b; }
.btn.sm { padding: 5px 10px; font-size: 12px; }
.btn.block { width: 100%; padding: 11px; font-size: 15px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* business picker */
.biz-results { max-height: 260px; overflow: auto; border: 1px solid var(--line); border-radius: 8px; margin-bottom: 10px; }
.biz-item { display: flex; gap: 8px; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.biz-item:last-child { border-bottom: 0; }
.biz-item .meta { flex: 1; min-width: 0; }
.biz-item .name { font-weight: 600; }
.biz-item .sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.biz-selected { border: 1px solid #bbf7d0; background: #f0fdf4; border-radius: 8px; padding: 10px; margin-bottom: 10px; }
.biz-selected .name { font-weight: 700; }
.estimate { font-size: 12px; color: var(--muted); margin: 8px 0 10px; }

/* map */
.map { height: calc(100vh - 140px); min-height: 520px; border-radius: var(--radius); border: 1px solid var(--line); z-index: 1; }
.map.short { height: 560px; }
.rank-pin { display: flex; align-items: center; justify-content: center; width: 30px; height: 30px; border-radius: 50%; color: #fff; font-weight: 700; font-size: 12px; border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.35); }
.rank-pin.pending { background: #fff; color: #9ca3af; border: 2px dashed #9ca3af; box-shadow: none; }
.rank-pin.scanning { background: var(--brand); animation: pulse 1s infinite; }
.rank-pin.error { background: #111827; }
.rank-pin.sel { outline: 3px solid #111827; outline-offset: 1px; }
@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.15); } }
.biz-pin { font-size: 22px; line-height: 1; filter: drop-shadow(0 1px 2px rgba(0,0,0,.4)); }

/* scan view */
.scan-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.scan-head .kw { font-size: 20px; font-weight: 700; }
.scan-head .sub { color: var(--muted); font-size: 13px; }
.metrics { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; margin-bottom: 12px; }
.metric { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; }
.metric .v { font-size: 22px; font-weight: 800; }
.metric .l { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 700; }
.metric .d { font-size: 12px; font-weight: 700; }
.d.up { color: var(--r1); } .d.down { color: var(--r4); }
.progress { height: 8px; background: #e5e7eb; border-radius: 999px; overflow: hidden; margin: 6px 0 4px; }
.progress > div { height: 100%; background: var(--brand); width: 0; transition: width .3s; }
.legend { display: flex; gap: 10px; flex-wrap: wrap; font-size: 12px; color: var(--muted); margin: 8px 0 0; }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 50%; margin-right: 4px; vertical-align: -2px; }
.viewing { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.viewing select { margin: 0; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: -4px -4px 10px; }
.tabs button { background: none; border: 0; padding: 8px 12px; font: inherit; font-weight: 600; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; }
.tabs button.active { color: var(--ink); border-bottom-color: var(--brand); }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { padding: 7px 8px; border-bottom: 1px solid #f1f2f4; vertical-align: top; }
tr.target td { background: #eff6ff; font-weight: 600; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: #f9fafb; }
.tbl-wrap { max-height: calc(100vh - 330px); overflow: auto; }
.rank-chip { display: inline-block; min-width: 26px; text-align: center; padding: 1px 6px; border-radius: 999px; color: #fff; font-weight: 700; font-size: 12px; }
.tag { display: inline-block; font-size: 10px; padding: 1px 6px; border-radius: 4px; background: #fef3c7; color: #92400e; font-weight: 700; margin-left: 4px; }
.tag.you { background: #dbeafe; color: #1e40af; }
.small { font-size: 12px; }
.empty { text-align: center; padding: 50px 20px; color: var(--muted); }

dialog { border: 0; border-radius: 12px; padding: 0; width: min(480px, 92vw); box-shadow: 0 20px 50px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(17,24,39,.45); }
.dlg { padding: 18px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }

.print-head { display: none; }

@media (max-width: 1100px) {
  .layout, .layout-scan { grid-template-columns: 1fr; }
  .metrics { grid-template-columns: repeat(3, 1fr); }
  .map { height: 480px; min-height: 0; }
  .tbl-wrap { max-height: none; }
}
@media (max-width: 640px) {
  .top { flex-wrap: wrap; gap: 8px; }
  .top-right { width: 100%; margin-left: 0; flex-wrap: wrap; }
  .metrics { grid-template-columns: repeat(2, 1fr); }
  main { padding: 12px; }
}

@media print {
  .top, .banner, .no-print, .tabs, .leaflet-control-container { display: none !important; }
  body, main { background: #fff; padding: 0; }
  .print-head { display: flex; justify-content: space-between; align-items: center; border-bottom: 2px solid #111827; padding-bottom: 8px; margin-bottom: 12px; }
  .layout-scan { grid-template-columns: 1fr; }
  .map { height: 640px; page-break-inside: avoid; }
  .tbl-wrap { max-height: none; overflow: visible; }
  .card { border: 0; padding: 0; }
  .rank-pin, .rank-chip, .legend i { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  #pointPanel { display: none; }
}
