:root {
  --bg: #0b1020;
  --panel: #121a2f;
  --panel2: #182240;
  --border: #243056;
  --text: #e8eefc;
  --muted: #8b9bb8;
  --accent: #5b8cff;
  --accent2: #3dd6c6;
  --danger: #ff5c7a;
  --ok: #3ecf8e;
  --warn: #f0b429;
  --shadow: 0 12px 40px rgba(0,0,0,.35);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background:
    radial-gradient(1200px 600px at 10% -10%, #1a2a55 0%, transparent 50%),
    radial-gradient(900px 500px at 100% 0%, #13283a 0%, transparent 45%),
    var(--bg);
  color: var(--text);
  min-height: 100vh;
}

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

/* —— R2 tab: full-bleed list view —— */
html, body { height: 100%; }
body.tab-r2-active {
  overflow: hidden; /* page doesn't scroll; only table does */
}
body.tab-r2-active .layout {
  max-width: none;
  width: 100%;
  height: 100vh;
  margin: 0;
  padding: 12px 16px 12px;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}
body.tab-r2-active .top {
  margin-bottom: 10px;
  flex-shrink: 0;
}
body.tab-r2-active #panelR2.tab-panel.active {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0; /* critical for nested scroll */
}
body.tab-r2-active .brand h1 { font-size: 1.15rem; }
body.tab-r2-active .brand .muted { display: none; }

.top {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 20px; gap: 16px; flex-wrap: wrap;
}
.header-right {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
}
.tabs {
  display: flex; background: var(--panel); border: 1px solid var(--border);
  border-radius: 12px; padding: 4px; gap: 4px;
}
.tab {
  border: 0; background: transparent; color: var(--muted);
  padding: 8px 16px; border-radius: 9px; font-weight: 600;
  cursor: pointer; font-size: .9rem;
}
.tab.active {
  background: linear-gradient(135deg, var(--accent), #7aa2ff);
  color: #061018;
}
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* Campaign tab */
.campaign-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
@media (max-width: 980px) {
  .campaign-grid { grid-template-columns: 1fr; }
}
.campaign-form .camp-intro { margin: 0 0 14px; font-size: .9rem; line-height: 1.45; }
.campaign-form code {
  background: #0a1220; padding: 1px 6px; border-radius: 4px; font-size: .85em;
}
.camp-mode-toggle {
  display: flex;
  gap: 4px;
  padding: 4px;
  margin: 0 0 14px;
  background: #0a1220;
  border: 1px solid var(--border);
  border-radius: 12px;
}
.camp-mode-btn {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 9px 12px;
  border-radius: 9px;
  font-weight: 600;
  font-size: .88rem;
  cursor: pointer;
}
.camp-mode-btn.active {
  background: linear-gradient(135deg, var(--accent), #7aa2ff);
  color: #061018;
}
.camp-mode-btn:hover:not(.active) {
  color: var(--text);
  background: rgba(91, 140, 255, .08);
}
.camp-hint {
  margin: 6px 0 0;
  font-size: .8rem;
  line-height: 1.4;
}
#campManualBlock label { margin-bottom: 10px; }
.camp-section {
  margin: 14px 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #0d1528;
  padding: 10px 12px;
}
.camp-section-head {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 8px; gap: 8px; flex-wrap: wrap;
}
.camp-linkish { display: flex; gap: 4px; }
.btn-xs { padding: 4px 10px !important; font-size: .78rem !important; }
.check-grid {
  display: flex; flex-direction: column; gap: 6px;
  max-height: 220px; overflow: auto;
  padding-right: 4px;
}
.check-item {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 6px 8px; border-radius: 8px;
  cursor: pointer; font-size: .88rem;
}
.check-item:hover { background: rgba(91,140,255,.08); }
.check-item input { margin-top: 3px; }
.subh {
  margin: 16px 0 8px; font-size: .95rem; color: var(--muted); font-weight: 600;
}
.stat-grid.compact .stat .n { font-size: 1.1rem; }
.camp-jobs-wrap { max-height: 360px; overflow: auto; }
.camp-jobs-table { width: 100%; border-collapse: collapse; font-size: .85rem; }
.camp-jobs-table th {
  text-align: left; padding: 8px 10px; color: var(--muted);
  font-size: .72rem; text-transform: uppercase; letter-spacing: .04em;
  border-bottom: 1px solid var(--border); position: sticky; top: 0;
  background: var(--panel);
}
.camp-jobs-table td {
  padding: 8px 10px; border-bottom: 1px solid #1a2540; vertical-align: top;
}
.kw-cell { max-width: 180px; color: var(--muted); font-size: .8rem; }
.job-st {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: .72rem; font-weight: 700; text-transform: uppercase;
  background: #1a2540; color: var(--muted);
}
.job-st-queued { background: #1a2a4a; color: #9eb6e8; }
.job-st-running { background: #3a2e10; color: var(--warn); }
.job-st-done { background: #0f2e22; color: var(--ok); }
.job-st-failed { background: #3a1520; color: var(--danger); }
.job-st-cancelled { background: #222; color: #888; }
.job-row.job-running { background: rgba(240,180,41,.06); }
.job-row.job-done { opacity: .85; }
.job-row.job-failed { background: rgba(255,92,122,.06); }

/* API usage dashboard */
.usage-card {
  margin-bottom: 16px;
  background: linear-gradient(180deg, #152240 0%, #101a30 100%);
  border: 1px solid #2e4a7a;
}
.usage-head {
  display: flex; justify-content: space-between; align-items: flex-start;
  gap: 12px; margin-bottom: 12px; flex-wrap: wrap;
}
.usage-head h2 { margin: 0 0 4px; color: #f0f5ff; font-size: 1.05rem; }
.usage-table-wrap {
  overflow: auto;
  border: 1px solid #2e426e;
  border-radius: 10px;
  background: #0a1220;
  max-height: 280px;
}
.usage-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .9rem;
}
.usage-table th {
  position: sticky; top: 0;
  background: #1a2d55;
  color: #dce8ff;
  text-align: left;
  padding: 10px 12px;
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  border-bottom: 2px solid #4a7ad4;
  white-space: nowrap;
}
.usage-table td {
  padding: 10px 12px;
  border-bottom: 1px solid #1c2a48;
  color: #d8e4fa;
  vertical-align: middle;
}
.usage-table tbody tr:nth-child(odd) td { background: #0c1528; }
.usage-table tbody tr:nth-child(even) td { background: #101c34; }
.usage-table .num { font-variant-numeric: tabular-nums; font-weight: 600; }
.usage-table .bar-cell { min-width: 120px; }
.usage-bar {
  height: 8px; border-radius: 99px; background: #1a2744;
  overflow: hidden; margin-top: 4px;
}
.usage-bar > i {
  display: block; height: 100%; border-radius: 99px;
  background: linear-gradient(90deg, #3dd6c6, #5b8cff);
}
.usage-bar.warn > i { background: linear-gradient(90deg, #f0b429, #ff8c42); }
.usage-bar.full > i { background: linear-gradient(90deg, #ff5c7a, #ff3b5c); }
.usage-pill {
  display: inline-block; padding: 3px 10px; border-radius: 999px;
  font-size: .72rem; font-weight: 700; text-transform: uppercase;
}
.usage-pill.ok { background: #0f3a24; color: #5dffb0; }
.usage-pill.warn { background: #4a3410; color: #ffd060; }
.usage-pill.full { background: #4a1525; color: #ff8aa0; }
.usage-details {
  margin-top: 12px;
  color: #b4c8ea;
  font-size: .9rem;
}
.usage-details summary {
  cursor: pointer;
  color: #9ec0ff;
  font-weight: 600;
  padding: 6px 0;
}
.usage-details .usage-table-wrap { margin-top: 8px; max-height: 360px; }

body.tab-r2-active .usage-card {
  flex-shrink: 0;
  margin-bottom: 10px;
  /* keep compact on R2 full-screen tab */
}
body.tab-r2-active .usage-table-wrap { max-height: 160px; }
.brand { display: flex; gap: 14px; align-items: center; }
.logo {
  width: 44px; height: 44px; border-radius: 12px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  font-size: 22px; color: #061018; font-weight: 700;
}
h1 { margin: 0; font-size: 1.35rem; letter-spacing: .02em; }
h2 { margin: 0 0 14px; font-size: 1rem; color: #c9d7f5; font-weight: 600; }
.muted { color: var(--muted); margin: 2px 0 0; font-size: .85rem; }

.status-pill {
  padding: 8px 14px; border-radius: 999px;
  background: var(--panel); border: 1px solid var(--border);
  text-transform: uppercase; font-size: .75rem; letter-spacing: .08em;
  font-weight: 700;
}
.status-pill.running { color: var(--accent2); border-color: #2a6b62; background: #102820; }
.status-pill.completed, .status-pill.completed_with_errors { color: var(--ok); }
.status-pill.failed, .status-pill.interrupted { color: var(--danger); }

.grid {
  display: grid;
  grid-template-columns: 1.05fr 1.25fr;
  grid-template-areas:
    "form stats"
    "map map"
    "places log";
  gap: 16px;
}
.form-card { grid-area: form; }
.stats-card { grid-area: stats; }
.map-card { grid-area: map; }
.places-card { grid-area: places; }
.log-card { grid-area: log; }

@media (max-width: 960px) {
  .grid {
    grid-template-columns: 1fr;
    grid-template-areas: "form" "stats" "map" "places" "log";
  }
}

.map-head {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 10px; margin-bottom: 10px;
}
.map-head h2 { margin: 0; }
.map-legend {
  display: flex; flex-wrap: wrap; gap: 12px;
  font-size: .78rem; color: var(--muted);
}
.map-legend .dot {
  display: inline-block; width: 10px; height: 10px; border-radius: 50%;
  margin-right: 5px; vertical-align: middle;
}
.map-legend .dot.center { background: #5b8cff; box-shadow: 0 0 0 2px rgba(91,140,255,.35); }
.map-legend .dot.range { background: transparent; border: 2px solid #5b8cff; width: 9px; height: 9px; }
.map-legend .dot.cell { background: rgba(61,214,198,.35); border: 1px solid #3dd6c6; }
.map-legend .sq.cell {
  display: inline-block; width: 10px; height: 10px; margin-right: 5px; vertical-align: middle;
  background: rgba(61,214,198,.3); border: 1px solid #3dd6c6; border-radius: 1px;
}
.map-legend .dot.place { background: #ff3b5c; box-shadow: 0 0 6px rgba(255,59,92,.55); }

.map {
  height: 420px;
  width: 100%;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: #0a1220;
  z-index: 1;
}
.map-foot { margin-top: 8px; font-size: .8rem; }

/* R2 browser — full viewport list + internal scroll only */
.r2-card {
  position: relative;
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
  height: 100%;
  padding: 14px 16px 12px !important;
  background: linear-gradient(180deg, #141c32 0%, #0e1528 100%);
  border: 1px solid #2a3a62;
  border-radius: 14px;
  overflow: hidden; /* clip; table area scrolls */
}
.r2-toolbar {
  display: flex; flex-wrap: wrap; justify-content: space-between;
  gap: 10px 16px; align-items: center; margin-bottom: 10px;
  flex-shrink: 0;
}
.r2-toolbar h2 { margin: 0 0 2px; color: #f0f5ff; font-size: 1.1rem; }
.r2-toolbar .muted { margin: 0; font-size: .82rem; }
.r2-actions {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
}
.r2-actions input[type="search"] {
  min-width: 280px;
  flex: 1 1 220px;
  background: #0c1426;
  border: 1px solid #3a4f7a;
  color: #f2f6ff;
  border-radius: 10px;
  padding: 10px 14px;
  font-size: .95rem;
}
.r2-actions input[type="search"]::placeholder { color: #7a8db0; }
.r2-actions select {
  background: #0c1426;
  border: 1px solid #3a4f7a;
  color: #f2f6ff;
  border-radius: 10px;
  padding: 10px 12px;
}
.r2-stats {
  margin-bottom: 8px;
  width: 100%;
  max-width: none;
  flex-shrink: 0;
  grid-template-columns: repeat(6, minmax(100px, 1fr));
  gap: 8px;
}
.r2-extra-actions {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
  margin-bottom: 10px; flex-shrink: 0;
}
.r2-extra-actions .muted { font-size: .8rem; }
.r2-stats .stat {
  background: #101a30;
  border-color: #2e426e;
  padding: 10px 8px;
}
.r2-stats .stat .n { color: #7ec8ff; font-size: 1.25rem; }
.r2-stats .stat .l { color: #9aabc8; }

/* THE list: takes ALL remaining height, scrolls freely */
.r2-table-wrap {
  flex: 1 1 auto;
  min-height: 0;              /* allow shrink inside flex */
  height: 100%;
  max-height: none !important; /* override any old cap */
  overflow: auto;
  border: 1px solid #2e426e;
  border-radius: 12px;
  background: #0a1220;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: auto;
  scrollbar-color: #5a8ad4 #0a1220;
}
.r2-table-wrap::-webkit-scrollbar { width: 14px; height: 14px; }
.r2-table-wrap::-webkit-scrollbar-track { background: #0a1220; border-radius: 8px; }
.r2-table-wrap::-webkit-scrollbar-thumb {
  background: linear-gradient(180deg, #5a8ad4, #3a5fa0);
  border-radius: 8px;
  border: 2px solid #0a1220;
}

.r2-table {
  width: 100%;
  min-width: 960px; /* horizontal scroll if narrow window */
  border-collapse: separate;
  border-spacing: 0;
  font-size: .94rem;
}
.r2-table th {
  position: sticky; top: 0; z-index: 3;
  text-align: left;
  padding: 12px 14px;
  font-size: .74rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: #dce8ff;
  background: linear-gradient(180deg, #1e3260 0%, #182848 100%);
  border-bottom: 2px solid #4a7ad4;
  box-shadow: 0 2px 8px rgba(0,0,0,.35);
  white-space: nowrap;
}
.r2-table td {
  padding: 12px 14px;
  border-bottom: 1px solid #1c2a48;
  vertical-align: middle;
  color: #d8e4fa;
  line-height: 1.45;
}
/* zebra rows */
.r2-table tbody tr:nth-child(odd) td { background: #0c1528; }
.r2-table tbody tr:nth-child(even) td { background: #101c34; }
.r2-table tbody tr:hover td {
  background: #1a2f58 !important;
  color: #ffffff;
}
.r2-table .phone {
  font-weight: 700;
  font-size: 1rem;
  color: #3dffc8;
  white-space: nowrap;
  letter-spacing: .02em;
}
.r2-table .phone a { color: inherit; text-decoration: none; }
.r2-table .phone a:hover { color: #9dffe6; text-decoration: underline; }
.r2-table .name {
  font-weight: 700;
  font-size: .98rem;
  color: #ffffff;
  min-width: 160px;
  max-width: none; /* don't clip names */
}
.r2-table .addr {
  color: #b4c8ea;
  min-width: 220px;
  max-width: none; /* full address visible, wrap */
  white-space: normal;
  word-break: break-word;
}
.r2-table .muted-cell { color: #6f82a6; }
.r2-table td:nth-child(4) { /* rating */
  color: #ffd666;
  font-weight: 600;
  white-space: nowrap;
}
.r2-extra-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin: 10px 0 14px;
}
.r2-extra-actions .inline-limit {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  font-size: .85rem;
  color: var(--muted);
}
.r2-extra-actions .inline-limit select {
  width: auto;
  min-width: 72px;
  padding: 6px 8px;
}
.backfill-box {
  margin: 0 0 14px;
  padding: 12px 14px;
  border: 1px solid #2e4a7a;
  border-radius: 12px;
  background: #0d1528;
}
.backfill-box.hidden { display: none; }
.backfill-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}
.backfill-bar {
  height: 8px;
  border-radius: 999px;
  background: #1a2540;
  overflow: hidden;
  margin-bottom: 8px;
}
.backfill-bar i {
  display: block;
  height: 100%;
  background: linear-gradient(90deg, var(--accent), #3dd6c6);
  width: 0%;
  transition: width .25s ease;
}
.backfill-recent {
  margin-top: 8px;
  max-height: 140px;
  overflow: auto;
  font-size: .8rem;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.backfill-recent .bf-line { padding: 2px 0; border-bottom: 1px solid #1a2540; }
.backfill-recent .bf-ok { color: var(--ok); }
.backfill-recent .bf-fail { color: var(--danger); }
.backfill-recent .bf-skip { color: #8b9bb8; }
.center-cell {
  text-align: center;
  padding: 48px 20px !important;
  color: #9aabc8 !important;
  font-size: 1rem;
}
.badge {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 7px;
  background: #243b6a;
  color: #c5d9ff;
  font-size: .74rem;
  margin: 2px;
  border: 1px solid #3a568c;
}
.badge.warn {
  background: #4a3410;
  color: #ffd060;
  border-color: #8a6520;
}
.badge.ok {
  background: #0f3a24;
  color: #5dffb0;
  border-color: #2a7a50;
}

.r2-detail {
  position: fixed; inset: 0; background: rgba(4, 10, 24, .72);
  backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  z-index: 50; padding: 20px;
}
.r2-detail.hidden { display: none; }
.r2-detail-inner {
  width: min(820px, 100%);
  max-height: 88vh;
  overflow: auto;
  position: relative;
  background: #121c36;
  border: 1px solid #3a568c;
}
.r2-detail-close { float: right; margin-bottom: 8px; }
.r2-detail-inner h2 { color: #fff; }
.r2-detail-inner pre {
  background: #0a1220 !important;
  border-color: #2e426e !important;
  color: #c5d9ff !important;
  max-height: 60vh;
}

/* Leaflet dark-ish tweaks */
.leaflet-container { background: #0a1220; font: inherit; }
.leaflet-popup-content-wrapper {
  background: #152038; color: var(--text); border-radius: 10px;
  border: 1px solid var(--border);
}
.leaflet-popup-tip { background: #152038; }
.leaflet-popup-content { margin: 10px 12px; font-size: .85rem; }
.leaflet-control-zoom a {
  background: #152038 !important; color: var(--text) !important;
  border-color: var(--border) !important;
}

.card {
  background: linear-gradient(180deg, var(--panel) 0%, #0f1628 100%);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 18px;
  box-shadow: var(--shadow);
}

label {
  display: flex; flex-direction: column; gap: 6px;
  font-size: .8rem; color: var(--muted); margin-bottom: 12px;
}
input, select {
  background: var(--panel2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: .95rem;
}
input[name="location"], #locationInput {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .92rem;
  letter-spacing: 0.01em;
}
input.input-error {
  border-color: var(--danger) !important;
  box-shadow: 0 0 0 3px rgba(255, 92, 122, 0.18);
}
input:focus, select:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(91,140,255,.15);
}
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.checks {
  display: flex; gap: 16px; margin: 4px 0 14px;
  color: var(--text); font-size: .9rem;
}
.checks label { flex-direction: row; align-items: center; gap: 8px; margin: 0; }

.actions { display: flex; flex-wrap: wrap; gap: 10px; }
.btn {
  border: 0; border-radius: 10px; padding: 10px 16px;
  font-weight: 600; cursor: pointer; font-size: .9rem;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary {
  background: linear-gradient(135deg, var(--accent), #7aa2ff);
  color: #061018;
}
.btn.ghost {
  background: transparent; color: var(--text);
  border: 1px solid var(--border);
}
.btn.danger { background: #3a1520; color: var(--danger); border: 1px solid #6a2438; }

.estimate {
  margin-top: 12px; padding: 12px; border-radius: 10px;
  background: #0a1222; border: 1px solid var(--border);
  color: #b8c8e8; font-size: .8rem; overflow: auto; max-height: 180px;
}
.hidden { display: none; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-bottom: 14px;
}
.stat {
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 10px;
  text-align: center;
}
.stat .n { display: block; font-size: 1.25rem; font-weight: 700; color: #f2f6ff; }
.stat .l { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

.current {
  background: #0c1426;
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 12px;
  font-size: .9rem;
  margin-bottom: 10px;
  min-height: 64px;
}
.meta { font-size: .8rem; color: var(--muted); word-break: break-all; }

.table-wrap { overflow: auto; max-height: 320px; }
table { width: 100%; border-collapse: collapse; font-size: .85rem; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 600; font-size: .75rem; text-transform: uppercase; }
td .tag {
  display: inline-block; padding: 2px 6px; border-radius: 6px;
  background: #1a2744; color: #9eb6e8; font-size: .72rem;
}
td .partial { color: var(--warn); font-size: .72rem; }

.log {
  height: 320px; overflow: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .75rem;
  background: #090f1c;
  border-radius: 12px;
  border: 1px solid var(--border);
  padding: 10px;
}
.log .line { padding: 3px 0; border-bottom: 1px solid rgba(36,48,86,.5); }
.log .ts { color: #5d6f93; margin-right: 8px; }
.log .ev { color: var(--accent2); margin-right: 8px; }
.log .err { color: var(--danger); }
.log .warn { color: var(--warn); }
