:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1c2430;
  --muted: #6b7686;
  --border: #e3e7ec;
  --accent: #2563eb;
  --accent-soft: #e8efff;
  --danger: #c62828;
  --r1: #16a34a;
  --r2: #eab308;
  --r3: #f97316;
  --r4: #dc2626;
  --pending: #94a3b8;
  --radius: 10px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: flex;
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
}
h1 { font-size: 16px; margin: 0; }
h2 { font-size: 13px; margin: 0 0 8px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
h3 { font-size: 13px; margin: 18px 0 8px; }
.muted { color: var(--muted); margin: 0; font-size: 12px; }
.hint { color: var(--muted); font-size: 12px; margin: 6px 0 0; }
.hidden { display: none !important; }
.error { color: var(--danger); font-size: 13px; margin: 8px 0 0; min-height: 1em; }
code { background: #f1f3f6; padding: 1px 4px; border-radius: 4px; }

/* Layout */
#sidebar {
  width: 360px; flex-shrink: 0; height: 100%; overflow-y: auto;
  background: var(--panel); border-right: 1px solid var(--border);
  padding: 16px;
}
#map { flex: 1; min-width: 0; height: 100%; }
#results {
  width: 380px; flex-shrink: 0; height: 100%; overflow-y: auto;
  background: var(--panel); border-left: 1px solid var(--border);
  padding: 16px;
}

.brand { display: flex; gap: 10px; align-items: center; margin-bottom: 14px; }
.logo {
  width: 36px; height: 36px; border-radius: 9px; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-size: 20px;
}

.modnav { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; background: #f1f3f6; padding: 4px; border-radius: var(--radius); margin: -4px 0 14px; }
.modnav a { font-size: 13px; color: var(--muted); text-decoration: none; padding: 6px 4px; border-radius: 7px; text-align: center; font-weight: 500; white-space: nowrap; }
.modnav a:hover { color: var(--text); background: rgba(255,255,255,.6); }
.modnav a.active { color: var(--accent); background: var(--panel); box-shadow: 0 1px 2px rgba(0,0,0,.08); font-weight: 600; }

.tabs { display: flex; gap: 4px; background: #f1f3f6; padding: 4px; border-radius: var(--radius); margin-bottom: 14px; }
.tab {
  flex: 1; border: 0; background: transparent; padding: 7px; border-radius: 7px;
  font: inherit; color: var(--muted); cursor: pointer;
}
.tab.active { background: var(--panel); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.08); }

.block { padding: 12px 0; border-bottom: 1px solid var(--border); }
.row { display: flex; gap: 6px; }
.stack { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
label { font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 3px; }

input, select, textarea {
  font: inherit; color: var(--text); width: 100%;
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 9px; background: #fff;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
textarea { resize: vertical; }

.btn {
  font: inherit; border: 1px solid var(--accent); background: var(--accent); color: #fff;
  border-radius: 8px; padding: 7px 12px; cursor: pointer; white-space: nowrap; text-decoration: none;
  display: inline-flex; align-items: center; justify-content: center;
}
.btn:disabled { opacity: .55; cursor: default; }
.btn.secondary { background: #fff; color: var(--accent); }
.btn.primary { font-weight: 600; padding: 10px 12px; }
.btn.wide { width: 100%; margin-top: 12px; }
.link { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 6px 0 0; font: inherit; font-size: 12px; }
.icon-btn { border: 0; background: none; font-size: 22px; line-height: 1; color: var(--muted); cursor: pointer; }

.alert { background: #fff4e5; border: 1px solid #ffd8a8; padding: 10px; border-radius: 8px; font-size: 13px; margin-bottom: 8px; }

.candidates { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.candidates li {
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; cursor: pointer;
}
.candidates li:hover { border-color: var(--accent); background: var(--accent-soft); }
.candidates b { display: block; }
.candidates small { color: var(--muted); display: block; }

.card { border: 1px solid var(--accent); background: var(--accent-soft); border-radius: 8px; padding: 10px; margin-top: 8px; }
.card b { display: block; }
.card small { display: block; color: var(--muted); }

.modes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.mode { cursor: pointer; position: relative; }
.mode input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; pointer-events: none; }
.mode span {
  display: flex; flex-direction: column; border: 1px solid var(--border); border-radius: 8px; padding: 8px;
  color: var(--text);
}
.mode small { color: var(--muted); font-size: 11px; }
.mode input:checked + span { border-color: var(--accent); background: var(--accent-soft); }

.estimate { margin-top: 12px; padding: 10px; background: #f1f3f6; border-radius: 8px; font-size: 13px; }
.estimate b { font-size: 15px; }

/* Historial */
.history { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.history li { border: 1px solid var(--border); border-radius: 8px; padding: 10px; cursor: pointer; position: relative; }
.history li:hover { border-color: var(--accent); }
.history li.active { border-color: var(--accent); background: var(--accent-soft); }
.history b { display: block; padding-right: 22px; }
.history small { display: block; color: var(--muted); }
.history .del { position: absolute; top: 6px; right: 6px; border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 16px; }
.history .del:hover { color: var(--danger); }
.badge { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 99px; background: #f1f3f6; color: var(--muted); margin-left: 4px; }
.badge.running { background: #fff4e5; color: #b45309; }
.badge.error { background: #fdecec; color: var(--danger); }

/* Resultados */
.results-head { display: flex; justify-content: space-between; gap: 8px; }
.results-head h2 { text-transform: none; letter-spacing: 0; color: var(--text); font-size: 16px; margin: 0; }
.progress { position: relative; height: 22px; background: #f1f3f6; border-radius: 99px; overflow: hidden; margin-top: 12px; }
.progress div { position: absolute; inset: 0 auto 0 0; background: var(--accent-soft); border-right: 2px solid var(--accent); transition: width .4s; }
.progress span { position: relative; display: block; text-align: center; font-size: 12px; line-height: 22px; }
.kw-tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.kw-tabs button {
  font: inherit; font-size: 12px; border: 1px solid var(--border); background: #fff; border-radius: 99px;
  padding: 4px 10px; cursor: pointer;
}
.kw-tabs button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.metrics { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.metric { border: 1px solid var(--border); border-radius: 8px; padding: 10px; }
.metric small { color: var(--muted); display: block; font-size: 11px; }
.metric b { font-size: 20px; }
.metric .delta { font-size: 12px; margin-left: 4px; }
.delta.up { color: var(--r1); }
.delta.down { color: var(--r4); }
.metric-note { grid-column: 1 / -1; font-size: 12px; color: var(--muted); }

.legend { display: flex; gap: 12px; margin-top: 12px; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.legend span { display: flex; align-items: center; gap: 4px; }
.legend .pin { width: 12px; height: 12px; border-width: 1px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 12px; }
th, td { text-align: left; padding: 6px 4px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 500; }
td.num, th.num { text-align: right; white-space: nowrap; }
tr.me td { background: var(--accent-soft); font-weight: 600; }
td small { display: block; color: var(--muted); font-weight: 400; }
.actions { display: flex; gap: 8px; margin-top: 16px; }
.actions .btn { flex: 1; }

/* Pines del mapa */
.pin {
  display: grid; place-items: 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); background: var(--pending);
}
.pin.r1 { background: var(--r1); }
.pin.r2 { background: var(--r2); color: #3b2f00; }
.pin.r3 { background: var(--r3); }
.pin.r4 { background: var(--r4); }
.pin.err { background: #475569; }
.pin.pending { background: var(--pending); animation: pulse 1.4s ease-in-out infinite; }
.pin.small { width: 22px; height: 22px; font-size: 10px; }
@keyframes pulse { 50% { opacity: .45; } }
.biz-pin { font-size: 22px; line-height: 1; text-shadow: 0 1px 3px rgba(0,0,0,.4); }

.popup b { display: block; margin-bottom: 4px; }
.popup ol { margin: 4px 0 0; padding-left: 18px; }
.popup li.me { font-weight: 700; color: var(--accent); }

@media (max-width: 900px) {
  body { flex-direction: column; overflow-y: auto; height: auto; }
  #sidebar, #results { width: 100%; height: auto; border: 0; }
  #map { height: 60vh; flex: none; }
}

/* Sesión */
.account { margin: 2px 0 0; font-size: 12px; }
.account a { color: var(--accent); text-decoration: none; }
.account a:hover { text-decoration: underline; }
.auth-dialog { border: 1px solid var(--border); border-radius: 12px; padding: 20px; width: min(360px, 92vw); }
.auth-dialog::backdrop { background: rgba(15, 23, 42, .35); }
.auth-dialog h3 { margin: 0 0 6px; }
.auth-dialog .actions { display: flex; gap: 8px; margin-top: 6px; }
