:root {
  --bg: #0b1220;
  --rail: #0a101c;
  --panel: #0f1726;
  --panel-2: #152034;
  --line: #22304a;
  --text: #e6edf7;
  --muted: #8d9bb3;
  --accent: #38bdf8;
  --free: #22c55e;
  --owned: #38bdf8;
  --warn: #f59e0b;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }
button, input, select { font: inherit; color: inherit; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

#app { display: grid; grid-template-columns: 72px auto 1fr; grid-template-rows: 100%; height: 100dvh; }
#rail { grid-column: 1; }
#drawer { grid-column: 2; }
#viewport { grid-column: 3; }

/* ---------- Barra de iconos ---------- */
#rail {
  display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 12px 0;
  background: var(--rail); border-right: 1px solid var(--line); z-index: 3;
}
.logo { font-weight: 800; font-size: 13px; letter-spacing: 1px; color: var(--accent); margin-bottom: 10px; }
.spacer { flex: 1; }
.rail-btn {
  position: relative; width: 60px; padding: 9px 0 7px; display: flex; flex-direction: column; align-items: center; gap: 4px;
  border: 0; border-radius: 12px; background: transparent; color: var(--muted); cursor: pointer; transition: background .15s, color .15s;
}
.rail-btn .lbl { font-size: 10.5px; font-weight: 600; }
.rail-btn:hover { background: var(--panel-2); color: var(--text); }
.rail-btn[aria-pressed="true"] { background: #0c2a3d; color: var(--accent); }
.rail-btn[aria-pressed="true"]::before { content: ""; position: absolute; left: -6px; top: 10px; bottom: 10px; width: 3px; border-radius: 3px; background: var(--accent); }
.rail-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.badge {
  position: absolute; top: 4px; right: 6px; min-width: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--free); color: #04140a; font-size: 10px; font-weight: 800; line-height: 17px; text-align: center;
}
.badge:empty { display: none; }
.badge.warn { background: var(--warn); color: #241400; }

/* ---------- Panel de listado ---------- */
#drawer {
  width: 330px; display: flex; flex-direction: column; gap: 10px; min-height: 0; padding: 16px 14px 10px;
  background: var(--panel); border-right: 1px solid var(--line); z-index: 2;
}
#drawer[hidden] { display: none; }
.dr-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.dr-head h2 { margin: 0; font-size: 18px; }
.dr-head small, .muted { color: var(--muted); font-size: 12.5px; }
.icon-btn { border: 0; background: transparent; color: var(--muted); padding: 6px; border-radius: 8px; cursor: pointer; display: grid; place-items: center; }
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.icon-btn svg { width: 18px; height: 18px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line); background: var(--panel-2); color: var(--muted); border-radius: 999px;
  padding: 5px 11px; font-size: 12.5px; cursor: pointer;
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #04223a; font-weight: 700; }
.chips.small .chip, .tag { padding: 2px 8px; font-size: 11.5px; cursor: default; border-radius: 999px; border: 1px solid var(--line); }
.tag.free { background: #0f2b1a; border-color: #1d5a35; color: #86efac; }
.tag.owned { background: #0c2433; border-color: #1f4f6d; color: #7dd3fc; }
.tag.warn { background: #2e1f06; border-color: #6b4a0f; color: #fcd34d; }
.tag.custom { background: #24143a; border-color: #4c2a7a; color: #d8b4fe; }

.search { position: relative; }
.search svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); width: 16px; height: 16px; color: var(--muted); }
#dr-search { width: 100%; padding: 9px 10px 9px 34px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel-2); outline: none; }
#dr-search:focus { border-color: var(--accent); }

#dr-list { list-style: none; margin: 0 -6px; padding: 0; overflow-y: auto; flex: 1; min-height: 0; }
#dr-list li {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; margin: 0 6px 2px; border-radius: 10px; cursor: pointer;
}
#dr-list li:hover, #dr-list li:focus-visible { background: var(--panel-2); outline: none; }
#dr-list li[aria-selected="true"] { background: #0c2a3d; box-shadow: inset 3px 0 0 var(--accent); }
#dr-list .dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
#dr-list .txt { flex: 1; min-width: 0; }
#dr-list strong { display: block; font-size: 13.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#dr-list small { display: block; color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#dr-list .more { color: var(--muted); font-size: 12px; justify-content: center; cursor: default; }
#dr-list .empty { color: var(--muted); cursor: default; justify-content: center; padding: 24px 10px; text-align: center; }

#dr-layers { overflow-y: auto; min-height: 0; flex: 1; }
#dr-layers h3 { font-size: 13px; margin: 14px 0 6px; color: var(--muted); font-weight: 600; }
#lay-sources { list-style: none; margin: 0; padding: 0; }
.switch { display: flex; align-items: center; gap: 8px; padding: 6px 2px; cursor: pointer; font-size: 13px; }
.switch input { accent-color: var(--accent); width: 16px; height: 16px; }
#lay-sources .switch span { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#lay-sources small { color: var(--muted); font-variant-numeric: tabular-nums; }

/* ---------- Mapa ---------- */
#viewport { position: relative; min-width: 0; }
#view { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; }
#view:active { cursor: grabbing; }
#view.hover { cursor: pointer; }

#topbar { position: absolute; top: 12px; right: 12px; display: flex; gap: 8px; }
.pill-btn {
  display: flex; align-items: center; gap: 6px; padding: 7px 12px; border-radius: 999px; font-size: 13px; cursor: pointer;
  border: 1px solid var(--line); background: rgba(15, 23, 38, .88); backdrop-filter: blur(8px);
}
.pill-btn svg { width: 16px; height: 16px; }
.pill-btn:hover { border-color: var(--accent); }
.pill-btn select { background: transparent; border: 0; outline: none; cursor: pointer; }
.pill-btn select option { background: var(--panel); }

#tooltip {
  position: absolute; left: 0; top: 0; pointer-events: none; z-index: 4; max-width: 260px;
  padding: 6px 10px; border-radius: 8px; background: rgba(5, 10, 20, .92); border: 1px solid var(--line); font-size: 12.5px;
}
#tooltip b { display: block; font-size: 13px; }
#tooltip span { color: var(--muted); }

#card {
  position: absolute; left: 16px; bottom: 16px; width: min(360px, calc(100% - 32px)); z-index: 3;
  padding: 14px 16px; border-radius: var(--radius); background: rgba(15, 23, 38, .96); border: 1px solid var(--line);
  box-shadow: 0 12px 40px rgba(0, 0, 0, .45); backdrop-filter: blur(8px);
}
#card header { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
#card h2 { margin: 0 0 6px; font-size: 17px; }
#card dl { display: grid; grid-template-columns: auto 1fr; gap: 5px 14px; margin: 12px 0; font-size: 13px; }
#card dt { color: var(--muted); }
#card dd { margin: 0; text-align: right; word-break: break-word; }
.card-actions { display: flex; gap: 8px; }
.primary, .ghost { flex: 1; padding: 9px; border-radius: 10px; cursor: pointer; font-weight: 700; font-size: 13px; }
.primary { border: 0; background: var(--accent); color: #04223a; }
.primary:hover { filter: brightness(1.08); }
.ghost { border: 1px solid var(--line); background: transparent; }
.ghost:hover { border-color: var(--accent); }

#help {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: min(440px, calc(100% - 32px)); z-index: 5;
  padding: 20px 22px; border-radius: 16px; background: rgba(15, 23, 38, .98); border: 1px solid var(--line); box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
#help h2 { margin: 0 0 10px; font-size: 18px; }
#help ul { margin: 0 0 16px; padding-left: 18px; line-height: 1.6; font-size: 14px; color: #c8d3e4; }
#help .primary { width: 100%; }

#status { position: absolute; right: 12px; bottom: 12px; font-size: 11.5px; color: var(--muted); background: rgba(10, 16, 28, .7); padding: 4px 9px; border-radius: 8px; pointer-events: none; }

#loading {
  position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 12px; z-index: 6;
  background: rgba(11, 18, 32, .9); color: var(--muted); font-size: 14px; text-align: center; padding: 16px;
}
#loading[hidden] { display: none; }
.dl-bar { width: min(280px, 70vw); height: 6px; border-radius: 99px; background: #1e293b; overflow: hidden; }
.dl-bar > div { height: 100%; width: 0; background: var(--accent); border-radius: inherit; transition: width .25s ease-out; }
.dl-text { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; min-height: 1em; }
/* Después de la pantalla de carga: barrita discreta arriba mientras termina de bajar el resto */
#dl-progress { position: absolute; top: 12px; left: 50%; transform: translateX(-50%); z-index: 5; display: flex; align-items: center; gap: 10px;
  padding: 6px 12px; border-radius: 99px; background: rgba(10, 16, 28, .78); border: 1px solid var(--line); pointer-events: none; }
#dl-progress[hidden] { display: none; }
#dl-progress .dl-bar { width: 140px; height: 4px; }
#dl-progress .dl-text { white-space: nowrap; }
@media (max-width: 1100px) { #dl-progress { top: 60px; } }
@media (max-width: 760px) { #dl-progress .dl-bar { width: 90px; } }
.spinner { width: 34px; height: 34px; border: 3px solid #1e293b; border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Móvil: barra abajo y listado como hoja inferior ---------- */
@media (max-width: 760px) {
  #app { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  #viewport { grid-row: 1; grid-column: 1; }
  #rail { grid-row: 2; grid-column: 1; flex-direction: row; justify-content: space-around; padding: 6px 4px; border-right: 0; border-top: 1px solid var(--line); }
  .logo, .spacer { display: none; }
  .rail-btn[aria-pressed="true"]::before { left: 12px; right: 12px; top: -6px; bottom: auto; width: auto; height: 3px; }
  #drawer { position: fixed; left: 0; right: 0; bottom: 64px; width: auto; height: 55dvh; border-right: 0; border-top: 1px solid var(--line); border-radius: 16px 16px 0 0; }
  #card { bottom: 12px; }
  #topbar .pill-btn:last-child { display: none; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }


/* Nota de la ficha (robado, guardado, etc.) */
#card-note { margin: 10px 0 0; padding: 9px 11px; border-radius: 10px; font-size: 13px; line-height: 1.45; background: var(--panel-2); border: 1px solid var(--line); }
#card-note.danger { background: #2a1013; border-color: #7f1d1d; color: #fecaca; }
#card-note.info { background: #0e2131; border-color: #1f4f6d; color: #bae6fd; }
.tag.danger { background: #2a1013; border-color: #7f1d1d; color: #fca5a5; }
.tag.muted { background: var(--panel-2); color: var(--muted); }

/* Estado de "Mis autos" en el listado */
#dr-list li.st-estacionado .dot { box-shadow: 0 0 0 2px rgba(255, 255, 255, .28); }
#dr-list li.st-robado small { color: #fca5a5; font-weight: 600; }
#dr-list li.st-deposito small { color: #fcd34d; }
#dr-list li.st-guardado strong, #dr-list li.st-deposito strong, #dr-list li.st-robado strong { color: #c3cddd; }
.tag.brl { background: #24143a; border-color: #5b2f8f; color: #e9d5ff; }

/* Usuario / login */
.user-btn .avatar { display: none; width: 30px; height: 30px; border-radius: 50%; place-items: center; background: #1d3a55; color: #bfe7ff; font-weight: 800; font-size: 13px; }
.user-btn.logged .avatar { display: grid; }
.user-btn.logged .login-ico { display: none; }
.user-btn .lbl { max-width: 60px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.popover { position: absolute; left: 76px; bottom: 64px; z-index: 7; padding: 6px; border-radius: 10px; background: var(--panel); border: 1px solid var(--line); box-shadow: 0 10px 30px rgba(0,0,0,.45); }
.popover { display: flex; flex-direction: column; gap: 4px; }
.popover .ghost { white-space: nowrap; text-decoration: none; text-align: left; color: inherit; }
#rail { position: relative; }
.modal {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: min(380px, calc(100% - 32px)); z-index: 8;
  padding: 20px 22px; border-radius: 16px; background: rgba(15, 23, 38, .98); border: 1px solid var(--line); box-shadow: 0 20px 60px rgba(0,0,0,.55);
}
.modal h2 { margin: 0 0 6px; font-size: 18px; }
.modal label { display: grid; gap: 5px; margin-top: 12px; font-size: 13px; color: var(--muted); }
.modal input { padding: 9px 11px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel-2); color: var(--text); outline: none; }
.modal input:focus { border-color: var(--accent); }
.modal .card-actions { margin-top: 16px; }
.error { margin: 12px 0 0; padding: 8px 10px; border-radius: 8px; background: #2a1013; border: 1px solid #7f1d1d; color: #fecaca; font-size: 13px; }
#dr-list .empty p { margin: 0 0 12px; line-height: 1.45; }
#dr-list .empty { flex-direction: column; }
.primary:disabled { opacity: .6; cursor: wait; }

/* Banderas e idioma */
svg.flag { stroke: none; width: 26px; height: 18px; border-radius: 3px; overflow: hidden; box-shadow: 0 0 0 1px rgba(255,255,255,.15); }
.lang-options { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 16px; }
.lang-opt {
  display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 16px 8px; cursor: pointer;
  border-radius: 12px; border: 1px solid var(--line); background: var(--panel-2); font-weight: 600; font-size: 14px;
}
.lang-opt svg.flag { width: 64px; height: 42px; border-radius: 6px; }
.lang-opt:hover, .lang-opt:focus-visible { border-color: var(--accent); outline: none; background: #0c2a3d; }
.lang-opt[aria-pressed="true"] { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
#lang { width: min(460px, calc(100% - 32px)); text-align: center; }
