:root {
  --bg:        #0d1117;
  --bg-soft:   #131a24;
  --panel:     #161d29;
  --line:      #232c3b;
  --line-soft: #1c2431;
  --text:      #e6edf6;
  --muted:     #8b98ab;
  --accent:    #2ee6a8;
  --accent-dim:#1a9e75;
  --warn:      #ffb454;
  --danger:    #ff6b6b;
  --shadow:    0 12px 32px rgba(0,0,0,.45);
  --r:         12px;
  --topbar-h:  60px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
}

body { display: flex; flex-direction: column; overflow: hidden; }

button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

/* ---------- Barra superior ---------- */

.topbar {
  height: var(--topbar-h);
  flex: none;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  background: var(--bg-soft);
  border-bottom: 1px solid var(--line);
  z-index: 1200;
}

.brand { display: flex; align-items: center; gap: 9px; text-decoration: none; color: var(--text); }
.brand-mark {
  display: grid; place-items: center;
  width: 32px; height: 32px;
  border-radius: 9px;
  background: linear-gradient(140deg, var(--accent), var(--accent-dim));
  color: #08120e;
}
.brand-text { font-size: 17px; letter-spacing: -.2px; font-weight: 500; }
.brand-text strong { font-weight: 700; }

.search {
  flex: 1;
  max-width: 440px;
  display: flex; align-items: center; gap: 8px;
  padding: 0 12px;
  height: 38px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
}
.search:focus-within { border-color: var(--accent-dim); color: var(--text); }
.search input {
  flex: 1; border: 0; background: none; outline: none; min-width: 0;
}
.search input::-webkit-search-cancel-button { filter: invert(.6); }

.topbar-actions { margin-left: auto; display: flex; gap: 8px; }

.btn {
  text-decoration: none;
  display: inline-flex; align-items: center; gap: 7px;
  height: 38px; padding: 0 14px;
  border-radius: 9px; border: 1px solid transparent;
  background: none; cursor: pointer; white-space: nowrap;
  transition: background .15s, border-color .15s, transform .1s;
}
.btn:active { transform: translateY(1px); }
.btn-sm { height: 30px; padding: 0 10px; font-size: 13px; }
.btn-primary { background: var(--accent); color: #06110d; font-weight: 600; }
.btn-primary:hover { background: #4ef0ba; }
.btn-primary:disabled { background: var(--line); color: var(--muted); cursor: not-allowed; }
.btn-ghost { border-color: var(--line); color: var(--text); background: var(--panel); }
.btn-ghost:hover { border-color: #33405a; }
.btn-danger { border-color: var(--line); color: var(--danger); background: var(--panel); }

.icon-btn {
  border: 0; background: none; cursor: pointer; color: var(--muted);
  width: 32px; height: 32px; border-radius: 8px; font-size: 15px;
}
.icon-btn:hover { background: var(--line-soft); color: var(--text); }

/* ---------- Estructura ---------- */

.layout { flex: 1; display: flex; min-height: 0; }

.panel {
  width: 390px; flex: none;
  display: flex; flex-direction: column;
  background: var(--bg-soft);
  border-right: 1px solid var(--line);
  min-height: 0;
}

.map-wrap { flex: 1; position: relative; min-width: 0; }
#map { position: absolute; inset: 0; background: #0a0e14; }

.map-hint {
  position: absolute; left: 50%; top: 16px; transform: translateX(-50%);
  z-index: 900;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px;
  background: var(--panel);
  border: 1px solid var(--accent-dim);
  border-radius: 999px;
  box-shadow: var(--shadow);
  font-size: 14px;
}

/* ---------- Filtros ---------- */

.filters { padding: 12px 14px; border-bottom: 1px solid var(--line); display: grid; gap: 10px; }

.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  border: 1px solid var(--line); background: var(--panel);
  color: var(--muted); border-radius: 999px;
  padding: 6px 12px; font-size: 13px; cursor: pointer; white-space: nowrap;
}
.chip:hover { color: var(--text); }
.chip.is-active { background: rgba(46,230,168,.12); border-color: var(--accent-dim); color: var(--accent); }

.selects { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.selects select {
  flex: 1; min-width: 130px; height: 34px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; padding: 0 8px; outline: none; cursor: pointer;
  font-size: 13px;
}
.toggle { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); cursor: pointer; }
.toggle input { accent-color: var(--accent); }

.list-head {
  display: flex; justify-content: space-between; align-items: baseline; gap: 8px;
  padding: 10px 14px; font-size: 13px; color: var(--muted);
  border-bottom: 1px solid var(--line-soft);
}

/* ---------- Lista ---------- */

.list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; min-height: 0; }

.card {
  display: grid; gap: 8px;
  padding: 12px;
  border-radius: var(--r);
  border: 1px solid transparent;
  cursor: pointer;
}
.card:hover { background: var(--panel); border-color: var(--line); }
.card.is-active { background: var(--panel); border-color: var(--accent-dim); }

.card-top { display: flex; align-items: flex-start; gap: 10px; }
.card-title { font-weight: 600; line-height: 1.3; }
.card-sub { font-size: 12.5px; color: var(--muted); margin-top: 2px; }

.rank {
  flex: none; width: 42px; height: 42px; border-radius: 10px;
  display: grid; place-items: center; text-align: center;
  background: var(--line-soft); border: 1px solid var(--line);
  line-height: 1;
}
.rank b { font-size: 15px; }
.rank small { font-size: 9px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.rank.tier-fast   { background: rgba(46,230,168,.14); border-color: var(--accent-dim); color: var(--accent); }
.rank.tier-mid    { background: rgba(255,180,84,.12); border-color: #6b5326; color: var(--warn); }
.rank.tier-slow   { background: rgba(255,107,107,.10); border-color: #6b2f2f; color: var(--danger); }
.rank.tier-none   { color: var(--muted); }

.metrics { display: flex; gap: 14px; font-size: 12.5px; color: var(--muted); flex-wrap: wrap; }
.metrics b { color: var(--text); font-weight: 600; }

.badges { display: flex; gap: 6px; flex-wrap: wrap; }
.badge {
  font-size: 11px; padding: 2px 8px; border-radius: 999px;
  background: var(--line-soft); color: var(--muted); border: 1px solid var(--line);
}
.badge.free   { color: var(--accent); border-color: var(--accent-dim); background: rgba(46,230,168,.10); }
.badge.seed   { color: var(--warn); border-color: #6b5326; background: rgba(255,180,84,.10); }
.badge.oficial{ color: #7cc4ff; border-color: #2c4a66; background: rgba(124,196,255,.10); }

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

.panel-foot {
  flex: none;
  padding: 10px 14px;
  border-top: 1px solid var(--line-soft);
  font-size: 11.5px; line-height: 1.45; color: var(--muted);
}
.panel-foot a { color: var(--muted); }

/* ---------- Panel de detalle ---------- */

.sheet {
  position: fixed; right: 16px; bottom: 16px; width: 380px; max-width: calc(100vw - 32px);
  max-height: calc(100vh - var(--topbar-h) - 32px);
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 16px;
  z-index: 1100;
}
.sheet h3 { margin: 0; font-size: 17px; }
.sheet-head { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; }
.sheet-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }

.stat-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 14px 0;
}
.stat {
  background: var(--bg-soft); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px; text-align: center;
}
.stat b { display: block; font-size: 17px; }
.stat span { font-size: 11px; color: var(--muted); }

.meter { height: 5px; border-radius: 3px; background: var(--line-soft); overflow: hidden; }
.meter i { display: block; height: 100%; background: linear-gradient(90deg, var(--accent-dim), var(--accent)); }

.history { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 6px; font-size: 12.5px; }
.history li { display: flex; justify-content: space-between; gap: 8px; color: var(--muted); }
.history b { color: var(--text); }

/* ---------- Diálogos ---------- */

.dialog {
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel);
  color: var(--text);
  padding: 0;
  width: min(520px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(4,7,12,.7); backdrop-filter: blur(2px); }
.dialog-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--line);
}
.dialog-head h2 { margin: 0; font-size: 16px; }
.dialog-body { padding: 16px; display: grid; gap: 12px; max-height: 70vh; overflow-y: auto; }
.dialog-foot {
  display: flex; justify-content: flex-end; gap: 8px;
  padding: 12px 16px; border-top: 1px solid var(--line);
}

.dialog label { display: grid; gap: 5px; font-size: 12.5px; color: var(--muted); }
.dialog input, .dialog select, .dialog textarea {
  background: var(--bg-soft); border: 1px solid var(--line); border-radius: 9px;
  padding: 9px 11px; outline: none; color: var(--text); font-size: 14px; width: 100%;
  resize: vertical;
}
.dialog input:focus, .dialog select:focus, .dialog textarea:focus { border-color: var(--accent-dim); }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.coord-note {
  margin: 0; padding: 9px 12px; font-size: 12.5px; color: var(--muted);
  background: var(--bg-soft); border: 1px solid var(--line); border-radius: 9px;
}
.form-error { margin: 0; color: var(--danger); font-size: 13px; }
.hint { margin: 0; font-size: 12px; color: var(--muted); text-align: center; }

/* ---------- Test de velocidad ---------- */

.test-body { text-align: center; }
.test-target { margin: 0; font-size: 13px; color: var(--muted); }

.gauge { position: relative; margin: 6px auto 0; width: 240px; }
.gauge svg { width: 100%; display: block; }
.gauge-track { fill: none; stroke: var(--line-soft); stroke-width: 12; stroke-linecap: round; }
.gauge-fill {
  fill: none; stroke: var(--accent); stroke-width: 12; stroke-linecap: round;
  stroke-dasharray: 251.3; stroke-dashoffset: 251.3;
  transition: stroke-dashoffset .25s ease-out;
}
.gauge-value { position: absolute; left: 0; right: 0; top: 52%; }
.gauge-value span { font-size: 34px; font-weight: 700; letter-spacing: -1px; }
.gauge-value small { display: block; font-size: 11px; color: var(--muted); }
.gauge-phase { margin: 6px 0 0; font-size: 13px; color: var(--muted); min-height: 20px; }

.test-results { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 4px; }
.test-results > div {
  background: var(--bg-soft); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 6px;
}
.test-results span { display: block; font-size: 11px; }
.test-results strong { font-size: 16px; }

/* ---------- Toast ---------- */

.toast {
  position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%);
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 16px; box-shadow: var(--shadow);
  z-index: 2000; font-size: 14px; max-width: 90vw;
}
.toast.error { border-color: var(--danger); color: var(--danger); }

/* ---------- Marcadores del mapa ---------- */

.pin {
  width: 30px; height: 30px; border-radius: 50% 50% 50% 4px;
  transform: rotate(-45deg);
  display: grid; place-items: center;
  border: 2px solid rgba(0,0,0,.35);
  box-shadow: 0 3px 10px rgba(0,0,0,.5);
  font-size: 11px; font-weight: 700; color: #06110d;
}
.pin span { transform: rotate(45deg); }
.pin.tier-fast { background: var(--accent); }
.pin.tier-mid  { background: var(--warn); }
.pin.tier-slow { background: var(--danger); color: #fff; }
.pin.tier-none { background: #55627a; color: #e6edf6; }
.pin.is-active { outline: 3px solid rgba(46,230,168,.5); }

.leaflet-container { background: #0a0e14; font: inherit; }
.leaflet-control-attribution { background: rgba(13,17,23,.8) !important; color: var(--muted) !important; }
.leaflet-control-attribution a { color: var(--muted) !important; }
.leaflet-bar a { background: var(--panel) !important; color: var(--text) !important; border-color: var(--line) !important; }
.leaflet-bar a:hover { background: var(--line-soft) !important; }
/* OSM no tiene basemap oscuro gratuito sin clave: lo oscurecemos por CSS. */
.tiles-dark { filter: invert(1) hue-rotate(180deg) brightness(.9) contrast(.9) saturate(.6); }

/* ---------- Móvil ---------- */

.mobile-tabs { display: none; }

@media (max-width: 860px) {
  .layout { position: relative; }
  .panel { position: absolute; inset: 0; width: 100%; border-right: 0; z-index: 500; }
  .map-wrap { position: absolute; inset: 0; }
  body.view-map .panel { display: none; }
  body.view-list .map-wrap { visibility: hidden; }

  .mobile-tabs {
    display: flex; flex: none;
    border-top: 1px solid var(--line); background: var(--bg-soft);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .mobile-tabs button {
    flex: 1; border: 0; background: none; padding: 12px; cursor: pointer;
    color: var(--muted); font-size: 14px;
  }
  .mobile-tabs button.is-active { color: var(--accent); box-shadow: inset 0 -2px 0 var(--accent); }

  .brand-text, .topbar-actions .btn span { display: none; }
  .topbar { gap: 10px; padding: 0 10px; }
  .btn { padding: 0 11px; }

  .sheet {
    left: 8px; right: 8px; bottom: 8px; width: auto;
    max-height: 62vh;
  }
  .row { grid-template-columns: 1fr; }
}
