:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #273449;
  --border: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --green: #22c55e;
  --yellow: #eab308;
  --orange: #f97316;
  --red: #ef4444;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

[hidden] { display: none !important; }

/* Locked state (fail-closed): the page ships with body.locked, so until JS
   confirms an authenticated session ONLY the login gate is visible. If a script
   fails to load, the app stays hidden rather than exposed. */
body.locked #app { display: none !important; }
/* the login gate is opaque, full-screen and shown by the locked class alone */
#modal-login { display: none; background: var(--bg); z-index: 3000; }
body.locked #modal-login { display: flex; }
/* other modals must never paint over the login gate */
body.locked .modal-backdrop:not(#modal-login) { display: none !important; }

/* read-only client view (/#share=<token>): no editing, no scans, no costs */
body.readonly #projects-block,
body.readonly #btn-add-keyword,
body.readonly #actions-block,
body.readonly #btn-save-comp,
body.readonly #metric-cost,
body.readonly #export-row,
body.readonly .note-edit,
body.readonly .del { display: none !important; }

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

#app { display: flex; height: 100vh; }

/* ---------- Sidebar ---------- */
#sidebar {
  width: 320px;
  min-width: 320px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding-bottom: 24px;
}
.sb-header { padding: 16px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.sb-header h1 { font-size: 17px; letter-spacing: .3px; }

.sb-block { padding: 14px 16px 4px; }
.sb-block-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.sb-block h2 { font-size: 12px; text-transform: uppercase; color: var(--muted); letter-spacing: .8px; margin-bottom: 8px; }
.sb-block-head h2 { margin-bottom: 0; }

.item-list { list-style: none; }
.item-list li {
  padding: 7px 10px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  font-size: 14px;
}
.item-list li:hover { background: var(--panel-2); }
.item-list li.active { background: var(--panel-2); outline: 1px solid var(--accent); }
.item-list li .del {
  color: var(--muted); font-size: 12px; padding: 2px 5px; border-radius: 4px; visibility: hidden;
}
.item-list li:hover .del { visibility: visible; }
.item-list li .del:hover { color: var(--red); background: rgba(239,68,68,.15); }

.kw-grid { color: var(--accent); font-size: 11px; }

.scan-list { max-height: 220px; overflow-y: auto; margin-top: 10px; }
.scan-list li { font-size: 12.5px; color: var(--muted); }
.scan-list li.active { color: var(--text); }
.scan-list input[type=checkbox] { accent-color: var(--accent); }
.scan-badge { font-size: 11px; padding: 1px 6px; border-radius: 8px; background: var(--panel-2); }
.scan-badge.done { color: var(--green); }
.scan-badge.failed { color: var(--red); }
.scan-badge.running, .scan-badge.pending { color: var(--yellow); }

/* ---------- Buttons / inputs ---------- */
.btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 13.5px;
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #06283d; font-weight: 600; }
.btn-primary:hover { filter: brightness(1.1); }
.btn-sm { padding: 3px 9px; font-size: 12px; }
.btn-full { width: 100%; margin-bottom: 8px; }
.btn-row { display: flex; gap: 8px; }

.input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
  margin-top: 4px;
}
.input:focus { outline: 1px solid var(--accent); }

/* labelled control inside the sidebar (not a modal) */
.sb-field { display: block; font-size: 12px; color: var(--muted); margin: 10px 0 2px; }
.sb-field .input { font-size: 13px; padding: 6px 8px; }

.estimate { font-size: 12.5px; color: var(--muted); padding: 4px 2px 10px; }
.view-row { display: flex; gap: 6px; align-items: stretch; }
.view-row .input { margin-top: 0; flex: 1; min-width: 0; }
.view-row .btn { padding: 6px 10px; }
.view-row .btn.saved { color: var(--yellow); border-color: var(--yellow); }
.hint { font-size: 12px; color: var(--muted); margin: 8px 0; line-height: 1.45; }

/* ---------- Main / map ---------- */
#main { flex: 1; display: flex; flex-direction: column; position: relative; }
#map { flex: 1; background: #0b1120; }

#metrics-bar {
  display: flex;
  gap: 1px;
  background: var(--border);
  border-bottom: 1px solid var(--border);
}
.metric {
  flex: 1;
  background: var(--panel);
  padding: 10px 16px;
  display: flex;
  flex-direction: column;
}
.metric-value { font-size: 20px; font-weight: 700; }
.metric-label { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .6px; }

/* ---------- Grid markers ---------- */
.rank-marker {
  display: flex; align-items: center; justify-content: center;
  border-radius: 50%;
  color: #fff; font-weight: 700; font-size: 13px;
  width: 34px; height: 34px;
  border: 2px solid rgba(255,255,255,.85);
  box-shadow: 0 1px 6px rgba(0,0,0,.55);
  font-family: "Segoe UI", sans-serif;
}
.rank-green  { background: var(--green); }
.rank-yellow { background: var(--yellow); color: #1c1917; }
.rank-orange { background: var(--orange); }
.rank-red    { background: var(--red); }
/* not found anywhere in the top-100 — visually distinct from a deep rank */
.rank-none   { background: #475569; font-size: 10px; }
/* NO DATA collected at this point (API error / empty answer). Deliberately
   unlike any rank colour: a collection failure must never read as a position. */
.rank-fail {
  background: repeating-linear-gradient(45deg, #7c2d12, #7c2d12 4px, #431407 4px, #431407 8px);
  border-color: #fb923c; color: #fed7aa; font-size: 15px;
}
.rank-preview { background: rgba(56,189,248,.35); border-style: dashed; }
.rank-delta-up   { background: var(--green); }
.rank-delta-down { background: var(--red); }
.rank-delta-same { background: #64748b; }
.rank-marker.delta { font-size: 11px; }

/* ---------- Point history chips ---------- */
.point-history { margin-bottom: 8px; }
.point-history h4 { font-size: 13px; margin-bottom: 5px; }
.hist-chip {
  display: inline-flex; flex-direction: column; align-items: center;
  margin: 0 4px 4px 0; gap: 1px;
}
.hist-chip small { font-size: 9.5px; color: var(--muted); }
.hist-chip b {
  min-width: 26px; height: 22px; padding: 0 4px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 5px; color: #fff; font-size: 11.5px;
}
.hist-chip b.rank-yellow { color: #1c1917; }

.point-err { color: #fdba74; font-size: 12px; margin-bottom: 6px; word-break: break-word; }
.point-err-hint { color: var(--muted); font-size: 11px; }

/* ---------- Popup ---------- */
.leaflet-popup-content-wrapper { background: var(--panel); color: var(--text); border-radius: 8px; }
.leaflet-popup-tip { background: var(--panel); }
.leaflet-popup-content { margin: 10px 12px; }
.popup-top20 { max-height: 260px; overflow-y: auto; min-width: 250px; }
.popup-top20 h4 { font-size: 13px; margin-bottom: 6px; }
.popup-top20 table { border-collapse: collapse; width: 100%; font-size: 12px; }
.popup-top20 td { padding: 3px 6px 3px 0; border-bottom: 1px solid var(--border); }
.popup-top20 tr.us td { color: var(--accent); font-weight: 700; }
.popup-top20 .r { color: var(--muted); width: 22px; }
.popup-top20 .stars { color: var(--muted); white-space: nowrap; }

/* ---------- Modals ---------- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(2,6,23,.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 2000;
}
.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  width: 440px;
  max-width: 94vw;
  max-height: 88vh;
  overflow-y: auto;
}
.modal h3 { margin-bottom: 12px; font-size: 16px; }
.modal label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.search-row { display: flex; gap: 8px; margin-top: 4px; }
.search-row .input { margin-top: 0; }

.candidates { margin-bottom: 10px; max-height: 220px; overflow-y: auto; }
.candidates li { flex-direction: column; align-items: flex-start; border: 1px solid var(--border); margin-bottom: 6px; gap: 2px; }
.candidates li small { color: var(--muted); word-break: break-all; }
.candidates li.active { border-color: var(--accent); }
.candidates .maps-link { color: var(--accent); font-size: 12px; text-decoration: none; }
.candidates .maps-link:hover { text-decoration: underline; }
#np-quota { font-size: 11.5px; color: var(--muted); margin: -4px 0 8px; }
#np-quota.warn { color: var(--orange); font-weight: 600; }

/* ---------- Business profile / reviews ---------- */
.biz-grid { display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; font-size: 13px; margin-bottom: 12px; }
.biz-grid dt { color: var(--muted); }
.biz-grid dd { margin: 0; word-break: break-word; }
.biz-grid a { color: var(--accent); }
.biz-reviews h4 { font-size: 13px; margin: 8px 0 6px; }
.review { border: 1px solid var(--border); border-radius: 6px; padding: 8px 10px; margin-bottom: 8px; }
.review .rhead { display: flex; justify-content: space-between; font-size: 12.5px; }
.review .rhead b { color: var(--text); }
.review .rstars { color: var(--yellow); }
.review .rtext { font-size: 13px; color: var(--muted); margin-top: 4px; }
.review .rtime { font-size: 11px; color: var(--muted); }
.biz-note { font-size: 11px; color: var(--muted); margin-top: 6px; }
.selected-biz {
  border: 1px solid var(--green); border-radius: 6px;
  padding: 8px 10px; font-size: 13px; margin-bottom: 12px;
}

/* ---------- Data tables (competitors / summary) ---------- */
.modal-wide { width: 640px; }
.table-wrap { max-height: 60vh; overflow: auto; }
.data-table { border-collapse: collapse; width: 100%; font-size: 13px; }
.data-table th {
  text-align: left; color: var(--muted); font-size: 11px; text-transform: uppercase;
  letter-spacing: .5px; padding: 6px 8px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--panel);
}
.data-table td { padding: 6px 8px; border-bottom: 1px solid var(--border); }
.data-table tr.click { cursor: pointer; }
.data-table tr.click:hover td { background: var(--panel-2); }
.data-table tr.us td { color: var(--accent); font-weight: 700; }
.trend-up { color: var(--green); font-weight: 600; }
.trend-down { color: var(--red); font-weight: 600; }
.trend-same { color: var(--muted); }

/* ---------- Chart mode toggle / notes ---------- */
.chart-toggle .btn.active { border-color: var(--accent); color: var(--accent); }
#scan-note {
  background: var(--panel); border-bottom: 1px solid var(--border);
  padding: 7px 16px; font-size: 13px; color: var(--yellow);
}
#scan-note::before { content: "📝 "; }
.note-icon { cursor: pointer; opacity: .35; }
.note-icon.has { opacity: 1; }

/* ---------- Toast ---------- */
#toast {
  position: absolute; bottom: 18px; left: 50%; transform: translateX(-50%);
  background: var(--panel); border: 1px solid var(--accent);
  padding: 10px 18px; border-radius: 8px; z-index: 1500; font-size: 14px;
  box-shadow: 0 4px 20px rgba(0,0,0,.5);
}
