:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --text: #1b2430;
  --muted: #667085;
  --border: #e3e7ed;
  --accent: #1a73e8;
  --accent-text: #ffffff;
  --shadow: 0 6px 24px rgba(16, 24, 40, .14);
  --lvl0: #2e9e5b;
  --lvl1: #e0b100;
  --lvl2: #ef6c00;
  --lvl3: #d32f2f;
  --panel-w: 390px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151b;
    --surface: #1a1f27;
    --surface-2: #232a34;
    --text: #e8ecf1;
    --muted: #98a2b3;
    --border: #2d3540;
    --accent: #5b9dff;
    --accent-text: #0b1220;
    --shadow: 0 6px 24px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}
#map { position: fixed; inset: 0 0 0 var(--panel-w); background: var(--surface-2); }

/* ---------- Panel ---------- */
.panel {
  position: fixed; left: 0; top: 0; bottom: 0; width: var(--panel-w);
  background: var(--surface); border-right: 1px solid var(--border);
  overflow-y: auto; padding: 14px 14px 40px; z-index: 1000;
}
.panel-head h1 { font-size: 19px; margin: 2px 0 0; }
.panel-head .sub { margin: 0 0 10px; color: var(--muted); }
.sheet-toggle { display: none; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px; margin-bottom: 12px;
}
.card h2 { font-size: 14px; margin: 0 0 8px; }
.card h3 { font-size: 13px; margin: 12px 0 6px; color: var(--muted); font-weight: 600; }

label { display: block; font-size: 12px; color: var(--muted); }
input, select {
  width: 100%; font: inherit; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; margin-top: 3px;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.grid2 { display: grid; grid-template-columns: minmax(0, 1.5fr) minmax(0, 1fr); gap: 8px; margin: 10px 0; }
.grid2 > label, .grid2 input, .grid2 select { min-width: 0; }
.row { display: flex; align-items: center; flex-wrap: wrap; }
.gap { gap: 8px; }

.btn {
  font: inherit; border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  border-radius: 8px; padding: 9px 12px; cursor: pointer;
}
.btn.primary { width: 100%; background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.btn.primary:disabled { opacity: .6; cursor: progress; }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 9px; font-size: 12px; }
.hint { color: var(--muted); font-size: 12px; margin: 8px 0 0; }
.muted { color: var(--muted); font-weight: 400; }

/* stops */
.stop { position: relative; display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.stop .dot { width: 12px; height: 12px; border-radius: 50%; flex: none; border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--muted); }
.stop.origin .dot { background: #2e9e5b; }
.stop.dest .dot { background: #d32f2f; }
.stop.mid .dot { background: #8a63d2; }
.stop input { margin: 0; }
.stop .rm { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 16px; padding: 0 4px; }
.suggest {
  position: absolute; left: 20px; right: 0; top: 100%; z-index: 20; margin-top: 2px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow);
  max-height: 260px; overflow-y: auto;
}
.suggest div { padding: 8px 10px; cursor: pointer; border-bottom: 1px solid var(--border); }
.suggest div:last-child { border-bottom: 0; }
.suggest div:hover, .suggest div.active { background: var(--surface-2); }
.suggest small { color: var(--muted); display: block; }

.status { padding: 10px 12px; border-radius: 10px; background: var(--surface-2); margin-bottom: 12px; }
.status.error { background: #fdecea; color: #8a1c13; }
@media (prefers-color-scheme: dark) { .status.error { background: #3a1714; color: #ffb4ab; } }

/* results */
.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; text-align: center; }
.summary b { display: block; font-size: 16px; }
.summary span { font-size: 11px; color: var(--muted); }
.alts { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.alts button { font-size: 12px; }
.alts button.on { border-color: var(--accent); color: var(--accent); font-weight: 600; }

.verdict { display: flex; gap: 10px; align-items: flex-start; }
.verdict .big { font-size: 30px; line-height: 1; }
.verdict p { margin: 2px 0 0; color: var(--muted); }
.badge { display: inline-block; padding: 1px 8px; border-radius: 99px; color: #fff; font-size: 11px; font-weight: 600; }
.lvl0 { background: var(--lvl0); } .lvl1 { background: var(--lvl1); color: #222; }
.lvl2 { background: var(--lvl2); } .lvl3 { background: var(--lvl3); }

.alert { border-left: 4px solid var(--lvl2); background: var(--surface-2); border-radius: 6px; padding: 7px 10px; margin-top: 6px; cursor: pointer; }
.alert.l3 { border-color: var(--lvl3); }
.alert small { color: var(--muted); }

.chart { width: 100%; height: auto; display: block; }
.chart .bar { cursor: pointer; }
.chart text { fill: var(--muted); font-size: 10px; }
.chart .grid { stroke: var(--border); }
.best { margin-top: 6px; font-size: 13px; }
.best button { margin: 4px 4px 0 0; }

table.pts { width: 100%; border-collapse: collapse; font-size: 12px; }
table.pts td, table.pts th { padding: 5px 4px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
table.pts th { color: var(--muted); font-weight: 600; }
table.pts tr { cursor: pointer; }
table.pts tr:hover td { background: var(--surface-2); }
table.pts .num { text-align: right; white-space: nowrap; }
.lv { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 4px; }
.src { font-size: 11px; color: var(--muted); }
.src a { color: inherit; }

/* ---------- Layers control ---------- */
.layers { position: fixed; top: 12px; right: 56px; z-index: 1000; max-width: calc(100vw - 72px); }
.layers-btn {
  font: inherit; font-weight: 600; background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 12px; box-shadow: var(--shadow); cursor: pointer;
}
.layers-body {
  margin-top: 6px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow); padding: 10px 12px; width: 300px; max-width: 100%;
}
.layer-group + .layer-group { border-top: 1px solid var(--border); margin-top: 10px; padding-top: 10px; }
.lg-title { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.chk { display: flex; gap: 8px; align-items: center; color: var(--text); font-size: 13px; margin: 6px 0; cursor: pointer; }
.chk input { width: auto; margin: 0; }
.seg { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { flex: 1; font: inherit; font-size: 12px; padding: 6px; border: 0; background: var(--surface-2); color: var(--text); cursor: pointer; }
.seg button.on { background: var(--accent); color: var(--accent-text); }

/* ---------- Time bar ---------- */
.timebar {
  position: fixed; left: calc(var(--panel-w) + 12px); right: 12px; bottom: 22px; z-index: 1000;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
  padding: 8px 12px; display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
}
.timebar[hidden] { display: none; }
.timebar input[type=range] { flex: 1; min-width: 140px; margin: 0; padding: 0; background: none; border: 0; }
.tlabel { font-weight: 600; min-width: 120px; font-size: 13px; }
.legend { display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--muted); width: 100%; }
.legend .ramp { height: 10px; flex: 1; border-radius: 4px; max-width: 360px; }
.legend .cat { display: inline-flex; align-items: center; gap: 3px; }
.legend .cat i { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }

/* ---------- Leaflet tweaks ---------- */
.leaflet-container { font: inherit; }
.leaflet-popup-content { margin: 10px 12px; font-size: 13px; }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--surface); color: var(--text); }
.pop-actions { display: flex; gap: 6px; margin-top: 8px; }
.pop-actions button { font: inherit; font-size: 12px; padding: 4px 8px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); cursor: pointer; }
.zones-img { image-rendering: auto; }
.wx-pin { font-size: 18px; line-height: 1; text-shadow: 0 1px 2px rgba(0,0,0,.35); }

/* ---------- Efectos y modo viaje ---------- */
.fx-canvas { position: absolute; left: 0; top: 0; pointer-events: none; }
.trip-btn {
  position: fixed; top: 12px; left: calc(var(--panel-w) + 12px); z-index: 1000;
  font: inherit; font-weight: 700; font-size: 15px; padding: 10px 16px; border-radius: 99px; cursor: pointer;
  background: var(--accent); color: var(--accent-text); border: 0; box-shadow: var(--shadow);
}
body.trip .trip-btn { background: var(--lvl3); color: #fff; }
.recenter {
  position: fixed; right: 12px; bottom: 110px; z-index: 1000; width: 46px; height: 46px; border-radius: 50%;
  font-size: 22px; background: var(--surface); color: var(--accent); border: 1px solid var(--border); box-shadow: var(--shadow); cursor: pointer;
}
.recenter[hidden] { display: none; }
.hud {
  position: fixed; top: 64px; left: calc(var(--panel-w) + 12px); z-index: 1000; width: 360px; max-width: calc(100vw - 24px);
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); padding: 10px 12px;
}
.hud[hidden] { display: none; }
.hud-row { display: grid; grid-template-columns: repeat(3, 1fr); text-align: center; }
.hud-row b { display: block; font-size: 26px; line-height: 1.1; font-variant-numeric: tabular-nums; }
.hud-row span { font-size: 11px; color: var(--muted); }
.hud-row b { white-space: nowrap; }
.hud-row b small { font-size: 12px; font-weight: 600; margin-left: 2px; color: var(--muted); }
.hud-next { margin-top: 8px; font-size: 15px; border-radius: 8px; padding: 6px 8px; background: var(--surface-2); }
.hud-next:empty { display: none; }
.hud-next small { color: var(--muted); }
.hud-next.warn { background: #fff1e0; color: #6b3200; }
.hud-next.l3 { background: #fde4e4; color: #7a1010; }
@media (prefers-color-scheme: dark) {
  .hud-next.warn { background: #3d2610; color: #ffd2a6; }
  .hud-next.l3 { background: #3d1515; color: #ffb4b4; }
}
.me-dot {
  width: 20px; height: 20px; margin: 5px; border-radius: 50%; background: #1a73e8; border: 3px solid #fff;
  box-shadow: 0 0 0 6px rgba(26,115,232,.25), 0 1px 4px rgba(0,0,0,.4);
}
.me-arrow {
  width: 30px; height: 30px;
  background: no-repeat center/contain url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'%3E%3Cpath d='M15 2 L26 27 L15 21 L4 27 Z' fill='%231a73e8' stroke='white' stroke-width='2.5' stroke-linejoin='round'/%3E%3C/svg%3E");
  filter: drop-shadow(0 1px 3px rgba(0,0,0,.45));
}

/* =========================================================================
   Teléfono: barra lateral (☰) + resumen flotante
   ========================================================================= */
.route-compact, .sum-verdict, .drawer-close, .menu-btn, .mini-summary, .scrim { display: none; }
.layers { z-index: 1100; }

@media (max-width: 760px) {
  :root {
    --sheet-h: 0px; --drawer-w: min(88vw, 390px);
    --safe-top: env(safe-area-inset-top, 0px); --safe-bottom: env(safe-area-inset-bottom, 0px);
  }
  body { overscroll-behavior: none; }
  #map { inset: 0; }

  /* Barra lateral */
  .panel {
    top: 0; bottom: 0; left: 0; width: var(--drawer-w); height: auto; z-index: 1300;
    padding: calc(10px + var(--safe-top)) 14px calc(28px + var(--safe-bottom));
    border-right: 1px solid var(--border); box-shadow: 6px 0 28px rgba(16, 24, 40, .25);
    transform: translateX(calc(-100% - 16px)); transition: transform .28s cubic-bezier(.2, .8, .2, 1);
    touch-action: pan-y; overscroll-behavior: contain;
  }
  body.drawer-open .panel { transform: translateX(0); }
  .panel.dragging { transition: none; }
  .scrim { display: block; position: fixed; inset: 0; z-index: 1250; background: rgba(10, 14, 20, .4); animation: fade-in .2s ease; }
  .scrim[hidden] { display: none; }
  @keyframes fade-in { from { opacity: 0; } }
  .drawer-close {
    display: block; position: absolute; top: calc(8px + var(--safe-top)); right: 8px; z-index: 2;
    width: 40px; height: 40px; border-radius: 50%; border: 0; background: var(--surface-2); color: var(--text);
    font-size: 17px; cursor: pointer;
  }
  .panel-head { padding-right: 44px; }
  .panel-head h1 { font-size: 18px; margin-top: 6px; }
  .panel-head .sub { margin-bottom: 10px; font-size: 13px; }

  /* Botón ☰ */
  .menu-btn {
    display: block; position: fixed; top: calc(10px + var(--safe-top)); left: 10px; z-index: 1000;
    width: 44px; height: 44px; border-radius: 12px; font-size: 21px; line-height: 1;
    background: var(--surface); color: var(--text); border: 1px solid var(--border); box-shadow: var(--shadow); cursor: pointer;
  }

  /* Ruta ya calculada: formulario compacto dentro de la barra */
  .panel.has-route:not(.editing) .panel-head .sub,
  .panel.has-route:not(.editing) .form-full { display: none; }
  .panel.has-route:not(.editing) .route-compact { display: flex; }
  .panel.has-route:not(.editing) #routeForm { padding: 10px 12px; }
  .route-compact { align-items: center; gap: 10px; }
  .rc-text { flex: 1; min-width: 0; }
  .rc-line { display: flex; align-items: center; gap: 8px; font-weight: 600; white-space: nowrap; overflow: hidden; }
  .rc-line + .rc-line { margin-top: 4px; }
  .rc-line span:last-child { overflow: hidden; text-overflow: ellipsis; }
  .rc-dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
  .rc-dot.o { background: #2e9e5b; } .rc-dot.d { background: #d32f2f; }

  /* Resumen flotante abajo */
  .mini-summary {
    display: grid; grid-template-columns: 1fr auto; gap: 3px 10px; align-items: center; text-align: left;
    position: fixed; left: 10px; right: 10px; bottom: calc(10px + var(--safe-bottom)); z-index: 1000;
    font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
    border-radius: 14px; box-shadow: var(--shadow); padding: 11px 14px; cursor: pointer;
  }
  .mini-summary[hidden] { display: none; }
  .ms-main { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
  .ms-main small { font-size: 12px; }
  .ms-wx { grid-column: 1; font-size: 14px; }
  .ms-more { grid-column: 2; grid-row: 1 / span 2; color: var(--accent); font-weight: 600; font-size: 13px; }

  /* Controles más cómodos para el dedo */
  input, select { font-size: 16px; padding: 10px 12px; }
  .btn.small { font-size: 14px; padding: 8px 12px; min-height: 38px; }
  .btn.primary { padding: 13px; font-size: 16px; }
  .stop .rm { font-size: 20px; padding: 4px 8px; }
  .suggest div { padding: 12px; }
  .summary b { font-size: 16px; white-space: nowrap; }
  table.pts { font-size: 13px; }
  table.pts td { padding: 8px 4px; }

  /* Botones superiores */
  .trip-btn { top: calc(10px + var(--safe-top)); left: 62px; font-size: 14px; padding: 0 14px; height: 44px; }
  .layers { top: calc(10px + var(--safe-top)); right: 10px; left: auto; max-width: none; display: flex; flex-direction: column; align-items: flex-end; }
  .layers-btn { height: 44px; padding: 0 14px; }
  .layers-body { width: min(320px, calc(100vw - 20px)); max-height: 65vh; overflow-y: auto; }
  .seg button { padding: 9px 6px; font-size: 13px; }
  .chk { font-size: 15px; margin: 10px 0; }
  .chk input { width: 20px; height: 20px; }

  /* Barra de tiempo arriba, compacta */
  .timebar {
    top: calc(62px + var(--safe-top)); bottom: auto; left: 10px; right: 10px;
    padding: 6px 10px; gap: 6px 8px; border-radius: 12px;
  }
  .timebar .btn.small { min-height: 32px; padding: 4px 10px; }
  .tlabel { min-width: 0; font-size: 12px; order: 3; width: 100%; }
  .legend { order: 4; font-size: 10px; flex-wrap: wrap; }

  /* Controles de Leaflet: sin +/−, por encima del resumen flotante */
  .leaflet-control-zoom, .leaflet-control-scale { display: none; }
  .leaflet-bottom { bottom: var(--sheet-h); }
  .leaflet-control-attribution { font-size: 9px; max-width: 70vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .leaflet-popup-content { font-size: 14px; }
  .pop-actions { flex-wrap: wrap; }
  .pop-actions button { padding: 8px 10px; font-size: 13px; }

  /* Modo viaje */
  .hud { top: calc(62px + var(--safe-top)); left: 10px; right: 10px; width: auto; padding: 10px; }
  .hud-row b { font-size: 26px; }
  .hud-next { font-size: 14px; margin-top: 6px; }
  body.trip .timebar { display: none; }
  .recenter { right: 10px; bottom: calc(var(--sheet-h) + 24px + var(--safe-bottom)); width: 50px; height: 50px; }
}

.off-route { background: #f1e6fd; color: #3d1466; border-radius: 8px; padding: 8px; margin: -6px -8px 6px; }
.off-route .btn { display: block; margin-top: 6px; width: 100%; }
@media (prefers-color-scheme: dark) { .off-route { background: #2c1846; color: #e3ccff; } }
