:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #e8590c;
  --accent-dark: #c2410c;
  --accent-soft: #fff1e8;
  --blue: #2563eb;
  --danger: #dc2626;
  --ok: #15803d;
  --radius: 12px;
  --shadow: 0 6px 24px rgba(15, 23, 42, .14), 0 1px 3px rgba(15, 23, 42, .08);
  --topbar-h: 56px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); overflow: hidden; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }
svg { stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

#app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; background: var(--bg); }

/* ---------- barra superior ---------- */
.topbar {
  height: var(--topbar-h); flex: none; display: flex; align-items: center; gap: 12px;
  padding: 0 12px; padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right));
  background: var(--panel); border-bottom: 1px solid var(--line); z-index: 1000; position: relative;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; flex: none; }
.brand svg { width: 30px; height: 30px; stroke: none; }
.brand svg path { stroke: #fff; }
.name-wrap { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; }
.route-name {
  flex: 1; min-width: 0; max-width: 420px; font: inherit; font-size: 16px; font-weight: 600; color: var(--text);
  border: 1px solid transparent; border-radius: 8px; padding: 7px 10px; background: transparent;
  text-overflow: ellipsis;
}
.route-name:hover { border-color: var(--line); }
.route-name:focus { outline: none; border-color: var(--accent); background: #fff; box-shadow: 0 0 0 3px var(--accent-soft); }
.dirty-badge {
  flex: none; font-size: 12px; font-weight: 600; color: #92400e; background: #fef3c7;
  padding: 3px 8px; border-radius: 99px; white-space: nowrap;
}
.actions { display: flex; align-items: center; gap: 6px; margin-left: auto; flex: none; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 38px; padding: 0 12px; border-radius: 9px; border: 1px solid var(--line);
  background: #fff; cursor: pointer; font-size: 14px; font-weight: 500; white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s;
}
.btn svg { width: 18px; height: 18px; flex: none; }
.btn:hover:not(:disabled) { background: #f3f4f6; }
.btn:focus-visible, .tool:focus-visible { outline: 3px solid rgba(232, 89, 12, .35); outline-offset: 1px; }
.btn:disabled { opacity: .45; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-dark); }
.btn-danger { background: #fff; border-color: #fecaca; color: var(--danger); }
.btn-danger:hover:not(:disabled) { background: #fef2f2; }
.btn-ghost-danger { border-color: transparent; color: var(--muted); padding: 0 8px; }
.btn-ghost-danger:hover:not(:disabled) { color: var(--danger); background: #fef2f2; }
.btn-lg { height: 46px; padding: 0 20px; font-size: 15px; }
.btn-sm { height: 32px; padding: 0 10px; font-size: 13px; }

.dropdown { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 230px; background: #fff;
  border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); padding: 6px; z-index: 1100;
}
.menu button {
  display: flex; flex-direction: column; align-items: flex-start; width: 100%; border: 0; background: none;
  padding: 9px 10px; border-radius: 7px; cursor: pointer; text-align: left;
}
.menu button:hover { background: var(--accent-soft); }
.menu small { color: var(--muted); font-size: 12px; margin-top: 1px; }

/* ---------- mapa ---------- */
.map-area { position: relative; flex: 1; min-height: 0; }
#map { position: absolute; inset: 0; background: #e8ecef; }
.leaflet-container { font-family: inherit; }
.leaflet-bar a { color: var(--text); }
.fs-btn { display: flex !important; align-items: center; justify-content: center; }
.fs-btn svg { width: 17px; height: 17px; }
#map.mode-extend { cursor: crosshair; }
#map.mode-extend .leaflet-interactive { cursor: crosshair; }

/* marcadores de edición (Geoman) */
.marker-icon {
  background: #fff !important; border: 2.5px solid var(--accent) !important; border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .35);
}
.marker-icon-middle { opacity: .55 !important; border-color: var(--accent) !important; }
.marker-icon-middle:hover { opacity: 1 !important; }
#map.mode-delete .marker-icon { border-color: var(--danger) !important; cursor: pointer; }
#map.mode-delete .marker-icon:hover { background: var(--danger) !important; }
@media (pointer: coarse) {
  .marker-icon { width: 22px !important; height: 22px !important; margin: -11px 0 0 -11px !important; }
  .marker-icon-middle { width: 18px !important; height: 18px !important; margin: -9px 0 0 -9px !important; }
}

.wpt-icon span {
  display: block; width: 18px; height: 18px; background: var(--blue); border: 2.5px solid #fff;
  border-radius: 50% 50% 50% 0; transform: rotate(-45deg); box-shadow: 0 1px 4px rgba(0, 0, 0, .4);
}

/* ---------- estado vacío ---------- */
.empty {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px;
  z-index: 500; pointer-events: none;
}
.empty-card {
  pointer-events: auto; max-width: 460px; width: 100%; text-align: center; background: rgba(255, 255, 255, .97);
  border-radius: 18px; box-shadow: var(--shadow); padding: 28px 24px 22px; border: 2px dashed #f3c3a6;
}
.empty-icon { width: 60px; height: 60px; margin: 0 auto 8px; color: var(--accent); background: var(--accent-soft); border-radius: 16px; display: grid; place-items: center; }
.empty-icon svg { width: 40px; height: 40px; stroke: none; }
.empty-icon svg path { stroke: currentColor; }
.empty h1 { font-size: 22px; margin: 8px 0 6px; }
.empty p { margin: 0 0 16px; color: #374151; line-height: 1.45; }
.empty-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.empty-note { font-size: 12.5px; color: var(--muted) !important; margin: 16px 0 0 !important; }
.empty-resume { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; align-items: stretch; font-size: 14px; }
.resume-btn {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px; text-align: left; width: 100%;
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; background: #fff; cursor: pointer;
}
.resume-btn:hover { border-color: var(--accent); background: var(--accent-soft); }
.resume-btn small { color: var(--muted); }
.linklike { border: 0; background: none; color: var(--accent-dark); cursor: pointer; font-size: 13.5px; text-decoration: underline; }

/* ---------- panel de datos ---------- */
.stats {
  position: absolute; top: 12px; left: 12px; z-index: 800; width: 280px; background: var(--panel);
  border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
}
.stats-summary {
  display: flex; align-items: center; gap: 14px; width: 100%; border: 0; background: none; cursor: pointer;
  padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--line);
}
.sum-item { display: inline-flex; align-items: baseline; gap: 3px; font-size: 15px; white-space: nowrap; }
.sum-item b { font-size: 17px; }
.sum-item small { color: var(--muted); }
.sum-item.up::before { content: "▲"; color: var(--ok); font-size: 10px; margin-right: 2px; }
.sum-item.down::before { content: "▼"; color: var(--danger); font-size: 10px; margin-right: 2px; }
.chev { width: 18px; height: 18px; margin-left: auto; color: var(--muted); transition: transform .2s; transform: rotate(180deg); }
.stats.collapsed .chev { transform: none; }
.stats.collapsed .stats-body { display: none; }
.stats.collapsed .stats-summary { border-bottom: 0; }
.stats-body { padding: 10px 14px 12px; }
.stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; margin: 0; }
.stats-grid div { min-width: 0; }
.stats-grid dt { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.stats-grid dd { margin: 1px 0 0; font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.stats-diff { margin: 10px 0 0; font-size: 12.5px; color: var(--muted); }
.stats-diff .pos { color: var(--ok); } .stats-diff .neg { color: var(--danger); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--line); font-size: 12.5px; color: #374151; }
.legend span, .sw-toggle { display: inline-flex; align-items: center; gap: 6px; }
.sw-toggle { cursor: pointer; }
.sw-toggle input { margin: 0; accent-color: var(--accent); }
.sw { display: inline-block; width: 22px; height: 0; border-top: 4px solid var(--accent); border-radius: 2px; }
.sw-mod { border-top-color: #7c3aed; }
.sw-orig { border-top: 4px dotted #475569; }

/* ---------- barra de edición ---------- */
.editbar {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: max(22px, env(safe-area-inset-bottom));
  z-index: 800; display: flex; flex-direction: column; align-items: center; gap: 8px;
  max-width: calc(100% - 24px);
}
.hint {
  background: rgba(31, 41, 55, .9); color: #fff; font-size: 13.5px; line-height: 1.4; padding: 7px 14px;
  border-radius: 10px; text-align: center; max-width: 620px; box-shadow: 0 2px 8px rgba(0, 0, 0, .2);
}
.hint:empty { display: none; }
.hint .muted { color: #d1d5db; }
.seg-mini { display: inline-flex; background: rgba(255, 255, 255, .15); border-radius: 7px; padding: 2px; margin: 0 2px; vertical-align: middle; }
.seg-mini button { border: 0; background: none; color: #fff; padding: 2px 9px; border-radius: 5px; cursor: pointer; font-size: 13px; }
.seg-mini button.on { background: #fff; color: var(--text); font-weight: 600; }
.tools {
  display: flex; align-items: center; gap: 4px; background: var(--panel); padding: 6px; border-radius: 14px; box-shadow: var(--shadow);
}
.seg { display: flex; gap: 2px; }
.sep { width: 1px; align-self: stretch; background: var(--line); margin: 4px 4px; }
.tool {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  min-width: 64px; height: 54px; padding: 4px 8px; border: 0; border-radius: 10px; background: none; cursor: pointer;
  font-size: 11.5px; font-weight: 500; color: #374151;
}
.tool svg { width: 22px; height: 22px; }
.tool svg .fill { fill: #fff; }
.tool svg .danger { stroke: var(--danger); }
.tool svg .thin { stroke-width: 1.2; opacity: .5; }
.tool:hover:not(:disabled) { background: #f3f4f6; }
.tool.active { background: var(--accent-soft); color: var(--accent-dark); box-shadow: inset 0 0 0 1.5px var(--accent); }
.tool:disabled { opacity: .35; cursor: default; }
.tool.icon-only { min-width: 56px; }

/* ---------- soltar archivo ---------- */
.drop {
  position: absolute; inset: 10px; z-index: 1500; background: rgba(232, 89, 12, .12);
  border: 3px dashed var(--accent); border-radius: 18px; display: grid; place-items: center; pointer-events: none;
}
.drop-card { background: #fff; border-radius: 16px; padding: 22px 32px; text-align: center; box-shadow: var(--shadow); color: var(--accent-dark); }
.drop-card svg { width: 42px; height: 42px; }
.drop-card p { margin: 6px 0 2px; font-size: 18px; font-weight: 700; color: var(--text); }
.drop-card small { color: var(--muted); }

/* ---------- avisos ---------- */
.toasts {
  position: fixed; top: calc(var(--topbar-h) + 12px); left: 50%; transform: translateX(-50%); z-index: 3000;
  display: flex; flex-direction: column; gap: 8px; align-items: center; width: min(520px, calc(100% - 24px)); pointer-events: none;
}
.toast {
  pointer-events: none; background: #1f2937; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px;
  line-height: 1.4; box-shadow: var(--shadow); opacity: 0; transform: translateY(-8px); transition: opacity .25s, transform .25s;
  border-left: 4px solid var(--ok);
}
.toast.show { opacity: 1; transform: none; }
.toast-error { background: #7f1d1d; border-left-color: #fca5a5; }

/* ---------- diálogos ---------- */
.modal { position: fixed; inset: 0; z-index: 4000; background: rgba(15, 23, 42, .45); display: grid; place-items: center; padding: 16px; }
.modal-card { background: #fff; border-radius: 16px; box-shadow: var(--shadow); width: min(480px, 100%); max-height: calc(100dvh - 32px); overflow: auto; padding: 20px 22px 18px; }
.modal-card h2 { margin: 0 0 10px; font-size: 18px; }
.modal-card p { margin: 0 0 12px; line-height: 1.5; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; margin-top: 16px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: #374151; }
.field select { font: inherit; font-weight: 400; font-size: 15px; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--line); background: #fff; }
.simp-result { margin-top: 12px !important; font-size: 15px; }
.lib-list { list-style: none; margin: 0 0 8px; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.lib-list li { display: flex; align-items: center; gap: 10px; border: 1px solid var(--line); border-radius: 10px; padding: 8px 8px 8px 12px; }
.lib-list li.confirm { border-color: #fecaca; background: #fef2f2; }
.lib-list li.confirm .btn-ghost-danger { color: var(--danger); font-weight: 600; }
.lib-info { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.lib-info b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lib-info small { color: var(--muted); font-size: 12.5px; }
.lib-btns { display: flex; gap: 4px; flex: none; }
.lib-btns svg { width: 17px; height: 17px; }

/* ---------- pantallas medianas ---------- */
@media (max-width: 1080px) {
  .actions .btn span { display: none; }
  .actions .btn { padding: 0 10px; }
  .actions .btn-primary span { display: inline; }
}

/* ---------- móvil ---------- */
@media (max-width: 759px) {
  :root { --topbar-h: 52px; }
  .topbar { gap: 6px; padding: 0 8px; }
  .brand-text { display: none; }
  .name-wrap { gap: 4px; }
  .route-name { font-size: 15px; padding: 6px 6px; }
  .dirty-badge { font-size: 0; width: 10px; height: 10px; padding: 0; background: #f59e0b; }
  .actions { gap: 2px; }
  .actions .btn { border-color: transparent; padding: 0 8px; height: 38px; }
  .actions .btn span, .actions .btn-primary span { display: none; }
  .actions .btn-primary { padding: 0 10px; }

  .stats { top: 8px; left: 8px; right: 56px; width: auto; }
  .stats-summary { padding: 8px 12px; gap: 12px; }
  .sum-item b { font-size: 15.5px; }
  .stats-grid dd { font-size: 15px; }

  .editbar { bottom: max(14px, env(safe-area-inset-bottom)); width: calc(100% - 16px); max-width: none; }
  .hint { font-size: 13px; padding: 6px 10px; }
  .tools { width: 100%; justify-content: space-between; padding: 4px; gap: 0; overflow-x: auto; }
  .seg { gap: 0; }
  .tool { min-width: 0; flex: 1; padding: 4px 2px; font-size: 10.5px; height: 52px; }
  .tool.icon-only { min-width: 0; }
  .sep { margin: 6px 1px; }
  .tool[data-mode="edit"] span { font-size: 0; }
  .tool[data-mode="edit"] span::after { content: "Mover"; font-size: 10.5px; }
  #btnUndo span, #btnRedo span { display: none; }
  #btnUndo, #btnRedo { flex: .7; }
  #btnSimplify span { font-size: 0; }
  #btnSimplify span::after { content: "Simplif."; font-size: 10.5px; }
  .leaflet-control-scale { display: none; }
  .leaflet-bottom.leaflet-right { margin-bottom: 0; }
  .leaflet-control-attribution { font-size: 10px; }
  .empty-card { padding: 22px 16px 18px; }
  .empty h1 { font-size: 20px; }
}
@media (max-width: 380px) {
  .tool { font-size: 0 !important; height: 46px; }
  .tool span::after { font-size: 0 !important; }
}
/* En móvil con la barra de edición visible, subimos la atribución del mapa */
@media (max-width: 759px) {
  .leaflet-bottom { bottom: 0; }
}
