:root {
  --navy: #0f2a4a;
  --navy-2: #173a63;
  --accent: #0284c7;
  --accent-soft: #e0f2fe;
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --muted: #64748b;
  --danger: #dc2626;
  --warn-bg: #fef3c7;
  --warn-text: #92400e;

  /* rank colors */
  --r-top3: #16a34a;
  --r-top10: #eab308;
  --r-top20: #f97316;
  --r-none: #dc2626;
  --r-pending: #94a3b8;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 1px 3px rgba(15, 23, 42, .08);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 22px; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.boot { padding: 48px; text-align: center; color: var(--muted); }
.hidden { display: none !important; }

/* ---------- shell ---------- */
.topbar {
  display: flex; align-items: center; gap: 24px;
  height: 56px; padding: 0 20px;
  background: var(--navy); color: #fff;
  position: sticky; top: 0; z-index: 1000;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 15px; color: #fff; white-space: nowrap; }
.brand img { width: 26px; height: 26px; }
.brand span { font-weight: 400; opacity: .75; }
.brand:hover { text-decoration: none; }
.nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.nav a { color: #cbd5e1; padding: 8px 12px; border-radius: 8px; font-weight: 500; white-space: nowrap; }
.nav a:hover { background: var(--navy-2); color: #fff; text-decoration: none; }
.nav a.active { background: var(--navy-2); color: #fff; }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.icon-btn {
  position: relative; background: transparent; border: 0; color: #cbd5e1; cursor: pointer;
  width: 36px; height: 36px; border-radius: 8px; display: grid; place-items: center;
}
.icon-btn:hover { background: var(--navy-2); color: #fff; }
.badge-dot {
  position: absolute; top: 3px; right: 3px; min-width: 16px; height: 16px; padding: 0 4px;
  border-radius: 8px; background: #ef4444; color: #fff; font-size: 10px; font-weight: 700; line-height: 16px;
}
.user-chip { color: #cbd5e1; font-size: 13px; padding: 0 6px; white-space: nowrap; }

.dropdown {
  position: absolute; right: 12px; top: 52px; width: 360px; max-height: 420px; overflow-y: auto;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(15, 23, 42, .18); z-index: 1100;
}
.dropdown .item { display: block; padding: 10px 14px; border-bottom: 1px solid var(--border); color: var(--text); }
.dropdown .item:hover { background: var(--surface-2); text-decoration: none; }
.dropdown .item.unread { background: var(--accent-soft); }
.dropdown .empty { padding: 18px; color: var(--muted); text-align: center; }

.banner { padding: 8px 20px; font-size: 13px; background: var(--warn-bg); color: var(--warn-text); border-bottom: 1px solid #fde68a; }

.page { max-width: 1400px; margin: 0 auto; padding: 20px; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head .sub { margin-top: 4px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #0369a1; }
.btn.danger { color: var(--danger); }
.btn.sm { height: 28px; padding: 0 10px; font-size: 13px; }
.btn.link { border: 0; background: none; color: var(--accent); padding: 0 4px; height: auto; }

label { display: block; font-weight: 500; font-size: 13px; margin-bottom: 4px; }
.field { margin-bottom: 14px; }
.field .hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=search], select, textarea {
  width: 100%; height: 36px; padding: 0 10px; border: 1px solid var(--border-strong); border-radius: 8px;
  font: inherit; color: var(--text); background: #fff;
}
textarea { height: auto; min-height: 120px; padding: 8px 10px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid #7dd3fc; outline-offset: 0; border-color: var(--accent); }
.check { display: flex; align-items: center; gap: 8px; font-weight: 400; }
.row { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
.inline { display: flex; gap: 8px; align-items: center; }
.inline > input { flex: 1; }

/* ---------- cards / tables ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 16px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.stack > * + * { margin-top: 16px; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { padding: 14px 16px; }
.stat .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat .value { font-size: 24px; font-weight: 700; margin-top: 4px; }
.stat .foot { font-size: 12px; color: var(--muted); margin-top: 2px; }

table.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; background: var(--surface-2); }
.table tbody tr:hover { background: var(--surface-2); }
.table tr.clickable { cursor: pointer; }
.table td.num, .table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; }

.pill { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); white-space: nowrap; }
.pill.ok { background: #dcfce7; color: #166534; border-color: #bbf7d0; }
.pill.run { background: var(--accent-soft); color: #075985; border-color: #bae6fd; }
.pill.warn { background: var(--warn-bg); color: var(--warn-text); border-color: #fde68a; }
.pill.err { background: #fee2e2; color: #991b1b; border-color: #fecaca; }

.rank-badge { display: inline-block; min-width: 38px; padding: 2px 8px; border-radius: 6px; color: #fff; font-weight: 700; text-align: center; font-variant-numeric: tabular-nums; }
.r-top3 { background: var(--r-top3); }
.r-top10 { background: var(--r-top10); color: #3f2d00; }
.r-top20 { background: var(--r-top20); }
.r-none { background: var(--r-none); }
.r-pending { background: var(--r-pending); }
.r-error { background: #475569; }
.r-empty { background: var(--surface-2); color: var(--muted); border: 1px dashed var(--border-strong); }

.delta { font-size: 12px; font-weight: 600; margin-left: 6px; }
.delta.up { color: #16a34a; }
.delta.down { color: #dc2626; }
.delta.flat { color: var(--muted); }

.progress { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; min-width: 80px; }
.progress > div { height: 100%; background: var(--accent); transition: width .4s; }

.empty-state { padding: 40px 20px; text-align: center; color: var(--muted); }
.empty-state h2 { color: var(--text); margin-bottom: 6px; }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(160deg, var(--navy) 0%, #1e4b7a 100%); }
.login-card { width: 100%; max-width: 380px; padding: 28px; }
.login-card .brand { color: var(--navy); margin-bottom: 18px; font-size: 17px; }
.login-card .brand span { color: var(--muted); opacity: 1; }
.form-error { color: var(--danger); font-size: 13px; margin-bottom: 12px; }

/* ---------- client view ---------- */
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.tabs button { background: none; border: 0; border-bottom: 2px solid transparent; padding: 10px 14px; font: inherit; font-weight: 600; color: var(--muted); cursor: pointer; }
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }

.client-layout { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 16px; align-items: start; }
.kw-list { max-height: calc(100vh - 220px); overflow-y: auto; }
.kw-item { display: flex; align-items: center; gap: 8px; padding: 10px 14px; border-bottom: 1px solid var(--border); cursor: pointer; }
.kw-item:hover { background: var(--surface-2); }
.kw-item.active { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.kw-item .kw { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kw-item.paused .kw { color: var(--muted); text-decoration: line-through; }

.scan-toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.scan-toolbar select { width: auto; min-width: 220px; }
.metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); border-bottom: 1px solid var(--border); }
.metric { padding: 12px 16px; border-right: 1px solid var(--border); }
.metric:last-child { border-right: 0; }
.metric .label { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.metric .value { font-size: 22px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.metric .help { font-size: 11px; color: var(--muted); }

.maps { display: grid; grid-template-columns: 1fr; }
.maps.compare { grid-template-columns: 1fr 1fr; }
.map-box { position: relative; height: 560px; border-right: 1px solid var(--border); }
.map-box:last-child { border-right: 0; }
.map-box .map { position: absolute; inset: 0; }
.map-label { position: absolute; top: 0; left: 0; right: 0; height: 34px; line-height: 34px; padding: 0 12px; background: var(--surface-2); border-bottom: 1px solid var(--border); font-weight: 600; font-size: 13px; }
.map-box.labeled .map { top: 34px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; align-items: center; padding: 10px 16px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 50%; margin-right: 5px; vertical-align: -2px; }

/* map pins */
.pin-icon { background: none; border: 0; }
.pin {
  width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center;
  color: #fff; font-weight: 800; font-size: 12px; border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,.4); font-variant-numeric: tabular-nums; cursor: pointer;
}
.pin.r-pending { animation: pulse 1.4s ease-in-out infinite; }
.pin.preview { width: 12px; height: 12px; background: var(--accent); border-width: 2px; }
@keyframes pulse { 50% { opacity: .45; } }
.biz-pin { width: 22px; height: 22px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg); background: var(--navy); border: 3px solid #fff; box-shadow: 0 1px 5px rgba(0,0,0,.5); }

.leaflet-popup-content { margin: 12px 14px; min-width: 260px; }
.pop-head { font-weight: 700; margin-bottom: 6px; }
.pop-list { margin: 0; padding: 0; list-style: none; max-height: 260px; overflow-y: auto; }
.pop-list li { display: flex; gap: 8px; padding: 4px 0; border-bottom: 1px solid var(--border); font-size: 12px; }
.pop-list li.me { font-weight: 700; color: var(--accent); }
.pop-list .n { width: 22px; color: var(--muted); text-align: right; flex: none; }

.chart-box { position: relative; height: 280px; padding: 12px 16px; }

.heat td.cell { text-align: center; cursor: pointer; padding: 6px; }
.heat .rank-badge { min-width: 46px; }
.heat .cell-sub { display: block; font-size: 10px; color: var(--muted); margin-top: 2px; }
.heat th.kw-col, .heat td.kw-col { position: sticky; left: 0; background: var(--surface); z-index: 1; min-width: 200px; }

/* ---------- client form ---------- */
.form-layout { display: grid; grid-template-columns: minmax(0, 520px) minmax(0, 1fr); gap: 16px; align-items: start; }
.form-map { position: sticky; top: 72px; height: calc(100vh - 100px); min-height: 420px; }
.form-map .map { position: absolute; inset: 0; border-radius: var(--radius); }
.lookup-results { margin-top: 10px; }
.lookup-item { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 6px; }
.lookup-item .t { font-weight: 600; }
.section-title { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 700; margin: 4px 0 12px; }
.estimate { background: var(--accent-soft); color: #075985; border-radius: 8px; padding: 10px 12px; font-size: 13px; }

/* ---------- modal / toast ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15, 23, 42, .5); z-index: 2000; display: grid; place-items: center; padding: 16px; }
.modal { width: 100%; max-width: 520px; max-height: calc(100vh - 32px); overflow-y: auto; }
.modal.wide { max-width: 760px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); }
#toasts { position: fixed; bottom: 16px; right: 16px; z-index: 3000; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--navy); color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.25); max-width: 380px; font-size: 13px; }
.toast.error { background: #991b1b; }

/* ---------- responsive ---------- */
@media (max-width: 1000px) {
  .client-layout, .form-layout { grid-template-columns: 1fr; }
  .kw-list { max-height: 260px; }
  .form-map { position: relative; top: 0; height: 360px; }
  .maps.compare { grid-template-columns: 1fr; }
  .map-box { height: 440px; border-right: 0; border-bottom: 1px solid var(--border); }
  .metrics { grid-template-columns: repeat(2, 1fr); }
  .user-chip { display: none; }
}
@media (max-width: 640px) {
  .page { padding: 12px; }
  .topbar { gap: 10px; padding: 0 12px; }
  .brand span { display: none; }
}

/* ---------- print (client report) ---------- */
@media print {
  .topbar, .banner, .no-print, .tabs, .kw-col-panel, .leaflet-control-zoom { display: none !important; }
  body, html { background: #fff; }
  .page { max-width: none; padding: 0; }
  .client-layout { grid-template-columns: 1fr; }
  .card { box-shadow: none; break-inside: avoid; }
  .map-box { height: 520px; }
}
