:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f0f2f8;
  --border: #e2e6ef;
  --text: #1c2333;
  --text-dim: #5c6579;
  --primary: #3b6cff;
  --primary-ink: #ffffff;
  --danger: #d64545;
  --warn-bg: #fff6e5;
  --warn-border: #f2c977;
  --warn-text: #8a6100;
  --shadow: 0 1px 3px rgba(20, 30, 60, .08), 0 8px 24px rgba(20, 30, 60, .06);
  --radius: 14px;
  --c1: #3b6cff; --c2: #ff6b6b; --c3: #12b886; --c4: #f59f00; --c5: #7048e8;
}
:root[data-theme="dark"] {
  --bg: #0f1420;
  --surface: #182031;
  --surface-2: #1f2839;
  --border: #2b3548;
  --text: #e8ecf5;
  --text-dim: #97a1b8;
  --primary: #5b84ff;
  --warn-bg: #2e2712;
  --warn-border: #6b5623;
  --warn-text: #f0c56b;
  --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .3);
}

* { box-sizing: border-box; }
/* The [hidden] attribute must win over component display rules below. */
[hidden] { display: none !important; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px; background: var(--surface);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { font-size: 28px; }
.topbar h1 { font-size: 18px; margin: 0; }
.tagline { margin: 0; font-size: 12px; color: var(--text-dim); }
.topbar-actions { display: flex; align-items: center; gap: 10px; }

.layout { max-width: 1100px; margin: 0 auto; padding: 24px; }

/* ---------- cards & fields ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 22px;
}
.query-card { max-width: 620px; margin: 0 auto; }
.query-card h2, .results-head h2 { margin: 0 0 18px; font-size: 20px; }
.field { margin-bottom: 22px; }
.sub-field { margin-top: 12px; margin-bottom: 0; }
label { display: block; font-weight: 600; font-size: 14px; margin-bottom: 8px; }
label.sub { font-weight: 500; font-size: 13px; color: var(--text-dim); }
.hint { font-weight: 400; color: var(--text-dim); font-size: 12px; }

input[type="text"], input[type="date"], input[type="search"] {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 10px; background: var(--surface-2); color: var(--text);
  font-size: 14px; transition: border-color .15s;
}
input:focus { outline: none; border-color: var(--primary); }

.keyword-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 8px; }
.keyword-row { display: flex; gap: 8px; align-items: center; }
.keyword-row input { flex: 1; }
.kw-remove {
  border: none; background: var(--surface-2); color: var(--text-dim);
  width: 36px; height: 38px; border-radius: 10px; cursor: pointer; font-size: 16px;
  border: 1px solid var(--border);
}
.kw-remove:hover { color: var(--danger); }

/* ---------- segmented control ---------- */
.segmented { display: inline-flex; flex-wrap: wrap; gap: 4px; background: var(--surface-2);
  padding: 4px; border-radius: 12px; border: 1px solid var(--border); }
.seg {
  border: none; background: transparent; color: var(--text-dim);
  padding: 8px 16px; border-radius: 9px; cursor: pointer; font-size: 13px; font-weight: 600;
}
.seg.active { background: var(--surface); color: var(--primary); box-shadow: var(--shadow); }
.date-row { display: flex; gap: 16px; margin-top: 12px; flex-wrap: wrap; }
.date-row .sub { flex: 1; min-width: 140px; }

/* ---------- multi-select dropdown ---------- */
.ms { position: relative; }
.ms-toggle {
  width: 100%; display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface-2); color: var(--text); font-size: 14px; cursor: pointer;
}
.ms-toggle:hover { border-color: var(--primary); }
.ms-caret { color: var(--text-dim); font-size: 12px; }
.ms-panel {
  position: absolute; z-index: 30; top: calc(100% + 6px); left: 0; right: 0;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow); padding: 10px; max-height: 300px; display: flex; flex-direction: column;
}
.ms-search { margin-bottom: 8px; }
.ms-actions { display: flex; gap: 14px; padding: 0 2px 8px; border-bottom: 1px solid var(--border); margin-bottom: 6px; }
.link-btn { background: none; border: none; color: var(--primary); cursor: pointer; font-size: 12px; font-weight: 600; padding: 0; }
.link-btn:hover { text-decoration: underline; }
.ms-options { overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.ms-opt { display: flex; align-items: center; gap: 9px; padding: 7px 8px; border-radius: 8px; cursor: pointer; font-size: 14px; }
.ms-opt:hover { background: var(--surface-2); }
.ms-opt input { width: auto; margin: 0; accent-color: var(--primary); }
.ms-empty { color: var(--text-dim); font-size: 13px; padding: 10px 8px; }

/* ---------- buttons ---------- */
.btn {
  border: 1px solid transparent; border-radius: 10px; cursor: pointer;
  font-size: 14px; font-weight: 600; padding: 11px 18px; transition: filter .15s, background .15s;
}
.btn.primary { background: var(--primary); color: var(--primary-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { padding: 8px 14px; font-size: 13px; }
.btn.block { width: 100%; margin-top: 6px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.icon-btn { border: none; background: transparent; cursor: pointer; font-size: 16px; color: var(--text-dim); }
.footnote { margin: 12px 0 0; font-size: 12px; color: var(--text-dim); text-align: center; }
.pill { font-size: 12px; color: var(--text-dim); background: var(--surface-2);
  padding: 5px 10px; border-radius: 20px; border: 1px solid var(--border); }
.powered { font-size: 13px; color: var(--text-dim); }
.powered strong { color: var(--primary); font-weight: 700; }

/* provenance / methodology note under the results title */
.data-note {
  margin: 10px 0 0; font-size: 12.5px; line-height: 1.5; color: var(--text-dim);
  background: var(--surface-2); border: 1px solid var(--border);
  border-left: 3px solid var(--primary); border-radius: 8px; padding: 9px 12px; max-width: 760px;
}
.data-note strong { color: var(--text); }

.site-footer {
  text-align: center; color: var(--text-dim); font-size: 12px;
  padding: 28px 16px 8px; line-height: 1.6;
}
.site-footer strong { color: var(--primary); }

/* ---------- top nav (module switcher) ---------- */
.topnav { display: flex; gap: 6px; margin-left: auto; margin-right: 16px; }
.navlink {
  text-decoration: none; color: var(--text-dim); font-size: 13px; font-weight: 600;
  padding: 7px 14px; border-radius: 9px; border: 1px solid transparent;
}
.navlink:hover { background: var(--surface-2); }
.navlink.active { color: var(--primary); background: var(--surface-2); border-color: var(--border); }

/* ---------- QR module ---------- */
.section-title { max-width: 1100px; margin: 8px auto 14px; font-size: 18px; }
.qr-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 18px; }
.qr-card { display: flex; gap: 16px; padding: 16px; }
.qr-img { flex: 0 0 120px; }
.qr-img img { width: 120px; height: 120px; background: #fff; border-radius: 10px; padding: 6px; border: 1px solid var(--border); }
.qr-body { flex: 1; min-width: 0; }
.qr-label { font-weight: 700; font-size: 15px; margin-bottom: 2px; }
.qr-dest { display: block; font-size: 12.5px; color: var(--primary); text-decoration: none;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 8px; }
.qr-dest:hover { text-decoration: underline; }
.qr-link { display: flex; gap: 6px; margin-bottom: 8px; }
.qr-link input { font-size: 12px; padding: 6px 8px; }
.qr-stats { display: flex; gap: 14px; font-size: 12.5px; color: var(--text-dim); margin-bottom: 10px; }
.qr-stats b { color: var(--text); font-size: 14px; }
.qr-actions { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.qr-actions a.btn { text-decoration: none; display: inline-flex; align-items: center; }
.btn.danger:hover { color: var(--danger); border-color: var(--danger); }

/* scans modal */
.scans-modal { max-width: 860px; width: 94vw; max-height: 86vh; display: flex; flex-direction: column; }
.scans-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 14px; }
.scans-head h3 { margin: 0 0 6px; }
.scans-modal .table-scroll { overflow: auto; }
.device { color: var(--text-dim); font-size: 12.5px; }
.muted { color: var(--text-dim); }

/* toast */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px;
  font-size: 13px; font-weight: 600; z-index: 70; box-shadow: var(--shadow);
}

/* ---------- analytics modal ---------- */
.analytics-modal { max-width: 1000px; width: 96vw; max-height: 92vh; display: flex; flex-direction: column; }
.an-scroll { overflow-y: auto; padding-right: 4px; }
.an-block { margin-bottom: 20px; }
.an-block-title { font-weight: 700; font-size: 14px; margin-bottom: 10px; display: flex;
  justify-content: space-between; align-items: center; gap: 10px; }
.an-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 18px; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 12px; margin-bottom: 22px; }
.tile { background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; }
.tile-val { font-size: 26px; font-weight: 800; line-height: 1.1; }
.tile-key { font-size: 12px; color: var(--text-dim); margin-top: 3px; }

.map { height: 360px; border-radius: 12px; border: 1px solid var(--border); overflow: hidden; z-index: 0; }
.legend { font-size: 11.5px; font-weight: 500; color: var(--text-dim); display: inline-flex; align-items: center; gap: 6px; }
.legend .dot, .dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; margin-left: 8px; }
.dot.precise { background: var(--c1); } .dot.approx { background: var(--c4); }

.bars { display: flex; flex-direction: column; gap: 7px; }
.bar-row { display: grid; grid-template-columns: 90px 1fr 34px; align-items: center; gap: 8px; font-size: 12.5px; }
.bar-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text-dim); }
.bar-track { background: var(--surface-2); border-radius: 6px; height: 16px; overflow: hidden; }
.bar-fill { display: block; height: 100%; background: var(--primary); border-radius: 6px; }
.bar-num { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.small { font-size: 12px; } .muted.small { padding: 4px 0; }

/* leaflet popups follow the theme a little */
.leaflet-popup-content { font-size: 12.5px; line-height: 1.5; }

/* ---------- results ---------- */
.results { display: flex; flex-direction: column; gap: 20px; }
.results-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; }
.results-actions { display: flex; gap: 10px; }
.meta-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 4px; }
.tag { font-size: 12px; padding: 4px 10px; border-radius: 20px; background: var(--surface-2);
  border: 1px solid var(--border); color: var(--text-dim); }
.tag.live, .tag.searchapi { color: #12b886; border-color: #12b88655; }
.tag.demo { color: var(--warn-text); border-color: var(--warn-border); background: var(--warn-bg); }
.tag.cache { color: var(--primary); border-color: var(--primary); }

.card-title { font-weight: 700; font-size: 15px; margin-bottom: 14px; }
.card-title-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.card-title-row .card-title { margin-bottom: 0; }
.search { max-width: 240px; }
.chart-wrap { position: relative; height: 340px; }

/* ---------- table ---------- */
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
thead th {
  text-align: left; padding: 10px 12px; border-bottom: 2px solid var(--border);
  color: var(--text-dim); font-size: 12px; text-transform: uppercase; letter-spacing: .04em;
  cursor: pointer; user-select: none; white-space: nowrap; position: sticky; top: 0; background: var(--surface);
}
thead th.num { text-align: right; }
thead th .arrow { opacity: .5; font-size: 10px; }
tbody td { padding: 10px 12px; border-bottom: 1px solid var(--border); }
tbody td.num { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
tbody tr:hover { background: var(--surface-2); }
.rank { color: var(--text-dim); width: 30px; }
.bar-cell { position: relative; }
.bar { position: absolute; left: 0; top: 0; bottom: 0; opacity: .12; border-radius: 0 6px 6px 0; }

.empty-state { color: var(--text-dim); text-align: center; padding: 24px; }

/* ---------- warnings ---------- */
.warnings {
  background: var(--warn-bg); border: 1px solid var(--warn-border); color: var(--warn-text);
  border-radius: 10px; padding: 12px 16px; font-size: 13px;
}
.warnings ul { margin: 6px 0 0; padding-left: 18px; }

/* ---------- overlay / loading ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(10, 14, 25, .55);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
.loader-box { background: var(--surface); padding: 34px 40px; border-radius: var(--radius);
  text-align: center; box-shadow: var(--shadow); max-width: 360px; }
.loader-box p { margin: 6px 0 0; }
.loading-sub { color: var(--text-dim); font-size: 13px; }
.spinner {
  width: 42px; height: 42px; margin: 0 auto 14px; border-radius: 50%;
  border: 4px solid var(--surface-2); border-top-color: var(--primary);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- error banner ---------- */
.banner {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 14px; padding: 12px 18px;
  border-radius: 12px; box-shadow: var(--shadow); z-index: 60; max-width: 90vw;
}
.banner.error { background: var(--danger); color: #fff; }
.banner .btn.ghost { border-color: rgba(255,255,255,.5); color: #fff; }
.banner .icon-btn { color: #fff; }
.error-text { color: var(--danger); font-size: 13px; margin: 8px 0 0; }

@media (max-width: 640px) {
  .layout { padding: 16px; }
  .chart-wrap { height: 260px; }
}
