/* =========================================================================
   APIO · panel-web — estilo naturaleza (salvia, crema, cafe y durazno)
   Tema claro: bosque de dia (por defecto). Tema oscuro: bosque de noche.
   Regla de color: fuera de los estados semanticos, todo es neutro.
   ========================================================================= */

:root {
    --font-ui: "Nunito", "Segoe UI", system-ui, sans-serif;
    --font-mono: "JetBrains Mono", ui-monospace, "Cascadia Code", monospace;
    --radio-lg: 20px;
    --radio-md: 14px;
    --radio-sm: 9px;
    --topbar-h: 58px;
    --sidebar-w: 224px;
    --sidebar-w-colapsado: 68px;
}

/* ----- Bosque de noche ----- */
html[data-theme="dark"] {
    --bg: #1A241D;
    --bg-degradado: #1A241D;
    --superficie: #232F26;
    --superficie-alta: #2C3A30;
    --superficie-mas-alta: #37473B;
    --linea: #3A4B3E;
    --texto: #F2EEDF;
    --texto-tenue: #AAB6A2;
    --marca: #A9C59A;
    --marca-dim: #87A67A;
    --en-marca: #1A241D;
    --menu: #2F4A3C;
    --sombra-sm: 0 2px 10px rgba(0,0,0,.3);
    --sombra-md: 0 14px 36px rgba(0,0,0,.45);
    --overlay: rgba(26,36,29,.82);
}

/* ----- Bosque de dia ----- */
html[data-theme="light"] {
    --bg: #F7F3E8;
    --bg-degradado: #F7F3E8;
    --superficie: #FFFCF4;
    --superficie-alta: #EEF0E2;
    --superficie-mas-alta: #DCE6D3;
    --linea: #E0E4D0;
    --texto: #34402F;
    --texto-tenue: #667060;
    --marca: #4F6F52;
    --marca-dim: #5F8575;
    --en-marca: #FFFCF4;
    --menu: #587B6C;
    --sombra-sm: 0 2px 10px rgba(79,111,82,.08);
    --sombra-md: 0 16px 40px rgba(79,111,82,.16);
    --overlay: rgba(247,243,232,.86);
}

/* ----- Colores semanticos (los UNICOS con significado de color) ----- */
:root {
    --estado-registrada: #7C8674;   /* gris-azul: recien creada, sin asignar */
    --estado-asignada: #B87A2E;     /* ambar: ambulancia asignada */
    --estado-enruta: #3F7C9B;       /* azul: en camino */
    --estado-atendida: #4F8455;     /* verde: resuelta */
    --estado-cancelada: #C65F49;    /* rojo: cancelada */
    --servicio-up: #4F8455;
    --servicio-degradado: #B87A2E;
    --servicio-caido: #C65F49;
    --servicio-inferido: #7A8B80;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
    margin: 0; font-family: var(--font-ui); background: var(--bg-degradado); color: var(--texto);
    transition: background-color .2s ease, color .2s ease; -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; letter-spacing: -.01em; }
a { color: inherit; }
.num, .mono { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
:focus-visible { outline: 2px solid var(--marca); outline-offset: 2px; }

/* ================= Shell: sidebar + topbar ================= */
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
    width: var(--sidebar-w); flex: none; background: var(--superficie); border-right: 1px solid var(--linea);
    display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; z-index: 70;
    transition: width .18s ease;
}
body.sidebar-colapsada .sidebar { width: var(--sidebar-w-colapsado); }
.sidebar-logo { display: flex; align-items: center; gap: .6rem; padding: 1rem .9rem; border-bottom: 1px solid var(--linea); }
.sidebar-logo .logo { width: 34px; height: 34px; border-radius: 11px; flex: none; display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: .82rem; color: var(--en-marca); background: linear-gradient(135deg, var(--marca), var(--marca-dim)); }
.sidebar-logo .nombre { font-weight: 800; font-size: 1rem; white-space: nowrap; overflow: hidden; }
.sidebar-logo .nombre small { display: block; font-weight: 600; font-size: .64rem; color: var(--texto-tenue); text-transform: uppercase; letter-spacing: .05em; }
body.sidebar-colapsada .sidebar-logo .nombre { display: none; }

.sidebar-nav { flex: 1; display: flex; flex-direction: column; gap: .15rem; padding: .7rem; overflow-y: auto; }
.sidebar-nav a {
    display: flex; align-items: center; gap: .7rem; padding: .55rem .65rem; border-radius: var(--radio-sm);
    color: var(--texto-tenue); text-decoration: none; font-weight: 600; font-size: .86rem; white-space: nowrap;
}
.sidebar-nav a svg { width: 18px; height: 18px; flex: none; stroke: currentColor; }
.sidebar-nav a:hover { background: var(--superficie-alta); color: var(--texto); }
.sidebar-nav a.activo { background: var(--marca); color: var(--en-marca); }
body.sidebar-colapsada .sidebar-nav a span { display: none; }
.sidebar-pie { padding: .7rem; border-top: 1px solid var(--linea); }
.sidebar-pie button { width: 100%; }

.contenido { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
    position: sticky; top: 0; z-index: 60; height: var(--topbar-h); display: flex; align-items: center; gap: .65rem;
    padding: 0 1.1rem; background: var(--superficie); border-bottom: 1px solid var(--linea);
}
.topbar .titulo-pagina { font-size: 1.02rem; font-weight: 800; margin-right: auto; }
.reloj { font-family: var(--font-mono); font-size: .82rem; color: var(--texto-tenue); }
.indicador-vivo { display: flex; align-items: center; gap: .4rem; font-size: .76rem; font-weight: 700; color: var(--servicio-up); }
.indicador-vivo .punto { width: 8px; height: 8px; border-radius: 50%; background: var(--servicio-up); box-shadow: 0 0 0 0 rgba(46,154,82,.6); animation: latido 1.8s infinite; }
.indicador-vivo.reconectando { color: var(--servicio-degradado); }
.indicador-vivo.reconectando .punto { background: var(--servicio-degradado); animation: none; }
@keyframes latido { 0% { box-shadow: 0 0 0 0 rgba(46,154,82,.55); } 70% { box-shadow: 0 0 0 7px rgba(46,154,82,0); } 100% { box-shadow: 0 0 0 0 rgba(46,154,82,0); } }

.boton-icono {
    display: inline-flex; align-items: center; justify-content: center; width: 36px; height: 36px; border-radius: 50%;
    background: var(--superficie-alta); border: 1px solid var(--linea); color: var(--texto); cursor: pointer; flex: none;
    padding: 0;
}
.boton-icono svg { width: 17px; height: 17px; stroke: currentColor; fill: none; flex: none; }
.boton-icono:hover { background: var(--superficie-mas-alta); }
.solo-movil { display: none; }

.chip-usuario { display: flex; align-items: center; gap: .5rem; padding: .25rem .4rem .25rem .7rem; border-radius: 999px; background: var(--superficie-alta); border: 1px solid var(--linea); }
.chip-usuario .nombre { font-size: .8rem; font-weight: 700; }
.chip-usuario .rol { font-size: .62rem; font-weight: 800; letter-spacing: .05em; text-transform: uppercase; padding: .16rem .5rem; border-radius: 999px; color: var(--en-marca); background: var(--marca-dim); }
.topbar form.salir { margin: 0; }
.topbar form.salir button { padding: .4rem .75rem; font-size: .8rem; }

main.area-principal { max-width: 1520px; width: 100%; margin: 0 auto; padding: 1.15rem 1.3rem 3rem; display: grid; gap: 1.05rem; }
.centro { max-width: 420px; margin: 0 auto; width: 100%; padding: 0 1rem; }

/* ================= Componentes base ================= */
.tarjeta { background: var(--superficie); border: 1px solid var(--linea); border-radius: var(--radio-lg); padding: 1.05rem 1.2rem; box-shadow: var(--sombra-sm); }
.tarjeta h2 { font-size: .98rem; margin-bottom: .75rem; }
label { display: block; font-size: .77rem; font-weight: 600; color: var(--texto-tenue); margin-bottom: .3rem; }
input, textarea, select { width: 100%; padding: .55rem .7rem; border: 1px solid var(--linea); border-radius: var(--radio-sm); font: inherit; background: var(--superficie-alta); color: var(--texto); }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--marca-dim); outline-offset: 1px; }
button { background: var(--marca); color: var(--en-marca); border: 0; border-radius: var(--radio-sm); padding: .58rem 1rem; font: inherit; font-weight: 700; cursor: pointer; transition: transform .08s ease, background-color .15s ease; }
button:hover { background: var(--marca-dim); }
button:active { transform: scale(.97); }
button:disabled { opacity: .55; cursor: progress; }
button.secundario { background: var(--superficie-alta); color: var(--texto); border: 1px solid var(--linea); }
button.secundario:hover { background: var(--superficie-mas-alta); }
button.peligro { background: var(--estado-cancelada); color: #fff; }
button.chico { padding: .32rem .7rem; font-size: .74rem; }

.fila { display: grid; grid-template-columns: 1fr 1fr 2fr auto; gap: .7rem; align-items: end; }
.pie { font-size: .8rem; color: var(--texto-tenue); margin: .5rem 0 0; }
.mensaje { padding: .7rem 1rem; border-radius: var(--radio-md); font-size: .87rem; font-weight: 600; }
.mensaje.ok { background: color-mix(in srgb, var(--servicio-up) 16%, var(--superficie)); color: var(--servicio-up); }
.mensaje.aviso { background: color-mix(in srgb, var(--servicio-degradado) 16%, var(--superficie)); color: var(--servicio-degradado); }
.mensaje.error { background: color-mix(in srgb, var(--servicio-caido) 16%, var(--superficie)); color: var(--servicio-caido); }
pre { background: var(--superficie-alta); padding: .5rem; border-radius: var(--radio-sm); overflow: auto; font-size: .74rem; font-family: var(--font-mono); white-space: pre-wrap; }

/* chips de estado de emergencia (unico lugar con color fuerte) */
.chip-estado { display: inline-flex; align-items: center; gap: .35rem; font-size: .71rem; font-weight: 800; letter-spacing: .02em; padding: .22rem .6rem; border-radius: 999px; }
.chip-estado.REGISTRADA { background: color-mix(in srgb, var(--estado-registrada) 20%, var(--superficie)); color: var(--estado-registrada); }
.chip-estado.ASIGNADA { background: color-mix(in srgb, var(--estado-asignada) 20%, var(--superficie)); color: var(--estado-asignada); }
.chip-estado.EN_RUTA { background: color-mix(in srgb, var(--estado-enruta) 20%, var(--superficie)); color: var(--estado-enruta); }
.chip-estado.ATENDIDA { background: color-mix(in srgb, var(--estado-atendida) 20%, var(--superficie)); color: var(--estado-atendida); }
.chip-estado.CANCELADA { background: color-mix(in srgb, var(--estado-cancelada) 20%, var(--superficie)); color: var(--estado-cancelada); }

/* chip de estado de servicio (arquitectura) */
.chip-servicio { display: inline-flex; align-items: center; gap: .3rem; font-size: .68rem; font-weight: 800; padding: .18rem .55rem; border-radius: 999px; }
.chip-servicio.UP { background: color-mix(in srgb, var(--servicio-up) 20%, var(--superficie)); color: var(--servicio-up); }
.chip-servicio.DEGRADADO { background: color-mix(in srgb, var(--servicio-degradado) 20%, var(--superficie)); color: var(--servicio-degradado); }
.chip-servicio.CAIDO { background: color-mix(in srgb, var(--servicio-caido) 20%, var(--superficie)); color: var(--servicio-caido); }
.chip-servicio.INFERIDO { background: color-mix(in srgb, var(--servicio-inferido) 20%, var(--superficie)); color: var(--servicio-inferido); }

/* ================= KPIs con sparkline ================= */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(168px, 1fr)); gap: .9rem; }
.kpi { background: var(--superficie); border: 1px solid var(--linea); border-radius: var(--radio-lg); padding: .9rem 1rem; box-shadow: var(--sombra-sm); position: relative; overflow: hidden; }
.kpi::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 3px; background: var(--acento, var(--marca)); }
.kpi .cabecera-kpi { display: flex; align-items: center; gap: .5rem; margin-bottom: .3rem; }
.kpi .icono { width: 30px; height: 30px; border-radius: 9px; display: flex; align-items: center; justify-content: center; background: color-mix(in srgb, var(--acento, var(--marca)) 18%, var(--superficie)); flex: none; }
.kpi .icono svg { width: 16px; height: 16px; stroke: var(--acento, var(--marca)); }
.kpi .etiqueta { font-size: .72rem; color: var(--texto-tenue); font-weight: 700; }
.kpi .valor { font-family: var(--font-mono); font-size: 1.55rem; font-weight: 700; line-height: 1; }
.kpi svg.spark { width: 100%; height: 26px; margin-top: .3rem; display: block; }
.kpi.activas { --acento: var(--estado-asignada); }
.kpi.disponibles { --acento: var(--servicio-up); }
.kpi.eta { --acento: var(--estado-enruta); }
.kpi.atendidas { --acento: var(--marca); }
.kpi.conductores { --acento: var(--secundario, #1C9476); }

/* ================= Tablero: mapa + columna derecha con tabs ================= */
.tablero { display: grid; grid-template-columns: 1.7fr 1fr; gap: 1.05rem; align-items: start; }
.panel-mapa { position: relative; padding: 0; overflow: hidden; }
.mapa-grande, .mapa-chico { background: var(--superficie-alta); }
.mapa-grande { height: 620px; width: 100%; }
.mapa-chico { height: 140px; width: 100%; border-radius: var(--radio-md); }
.leyenda-mapa { position: absolute; left: .8rem; bottom: .8rem; z-index: 400; background: var(--overlay); backdrop-filter: blur(6px); border: 1px solid var(--linea); border-radius: var(--radio-md); padding: .55rem .75rem; font-size: .72rem; display: flex; flex-direction: column; gap: .28rem; }
.leyenda-mapa .item { display: flex; align-items: center; gap: .4rem; }
.leyenda-mapa .punto { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.controles-mapa { position: absolute; right: .8rem; top: .8rem; z-index: 400; display: flex; gap: .35rem; }
.controles-mapa button { padding: .3rem .6rem; font-size: .7rem; background: var(--overlay); color: var(--texto); border: 1px solid var(--linea); }
.controles-mapa button.activo { background: var(--marca); color: var(--en-marca); }

.panel-lateral { display: flex; flex-direction: column; gap: .7rem; }
.tabs { display: flex; gap: .3rem; background: var(--superficie); border: 1px solid var(--linea); border-radius: 999px; padding: .25rem; }
.tabs button { flex: 1; background: transparent; color: var(--texto-tenue); border-radius: 999px; padding: .4rem .5rem; font-size: .78rem; }
.tabs button.activo { background: var(--marca); color: var(--en-marca); }
.panel-tab { display: none; flex-direction: column; gap: .7rem; max-height: 560px; }
.panel-tab.activo { display: flex; }

.filtros-estado { display: flex; flex-wrap: wrap; gap: .35rem; }
.filtro-chip { background: var(--superficie-alta); border: 1px solid var(--linea); color: var(--texto-tenue); border-radius: 999px; padding: .3rem .7rem; font-size: .74rem; font-weight: 700; cursor: pointer; }
.filtro-chip.activo { background: var(--marca); border-color: var(--marca); color: var(--en-marca); }

.lista-scroll { display: flex; flex-direction: column; gap: .6rem; overflow-y: auto; padding-right: .15rem; }
.lista-scroll::-webkit-scrollbar { width: 6px; }
.lista-scroll::-webkit-scrollbar-thumb { background: var(--linea); border-radius: 10px; }

.tarjeta-emergencia { background: var(--superficie); border: 1px solid var(--linea); border-left: 4px solid var(--acento, var(--texto-tenue)); border-radius: var(--radio-md); padding: .8rem .9rem; cursor: pointer; box-shadow: var(--sombra-sm); transition: transform .12s ease, box-shadow .12s ease; }
.tarjeta-emergencia:hover { transform: translateY(-2px); box-shadow: var(--sombra-md); }
.tarjeta-emergencia.nueva { animation: resaltar 2.4s ease; }
@keyframes resaltar { 0% { box-shadow: 0 0 0 3px var(--marca); } 100% { box-shadow: var(--sombra-sm); } }
.tarjeta-emergencia[data-estado="REGISTRADA"] { --acento: var(--estado-registrada); }
.tarjeta-emergencia[data-estado="ASIGNADA"] { --acento: var(--estado-asignada); }
.tarjeta-emergencia[data-estado="EN_RUTA"] { --acento: var(--estado-enruta); }
.tarjeta-emergencia[data-estado="ATENDIDA"] { --acento: var(--estado-atendida); }
.tarjeta-emergencia[data-estado="CANCELADA"] { --acento: var(--estado-cancelada); }
.cabecera-tarjeta { display: flex; align-items: center; justify-content: space-between; gap: .5rem; margin-bottom: .35rem; }
.id-emergencia { font-family: var(--font-mono); font-size: .72rem; color: var(--texto-tenue); font-weight: 700; }
.descripcion-emergencia { font-size: .88rem; font-weight: 600; margin: .15rem 0 .5rem; }
.detalle-emergencia { display: flex; flex-wrap: wrap; gap: .4rem; font-size: .74rem; color: var(--texto-tenue); margin-bottom: .5rem; }
.detalle-emergencia span { display: inline-flex; align-items: center; gap: .28rem; background: var(--superficie-alta); border-radius: 999px; padding: .18rem .5rem; font-family: var(--font-mono); }
.acciones-tarjeta { display: flex; flex-wrap: wrap; gap: .35rem; }
.acciones-tarjeta form { margin: 0; }
.acciones-tarjeta button { padding: .3rem .65rem; font-size: .72rem; }

.tarjeta-ambulancia { background: var(--superficie); border: 1px solid var(--linea); border-radius: var(--radio-md); padding: .8rem .9rem; display: flex; flex-direction: column; gap: .5rem; }
.tarjeta-ambulancia .placa { font-family: var(--font-mono); font-weight: 800; font-size: .95rem; }
.badge-senal { display: inline-flex; align-items: center; gap: .3rem; font-size: .68rem; font-weight: 800; padding: .16rem .5rem; border-radius: 999px; }
.badge-senal.conectada { background: color-mix(in srgb, var(--servicio-up) 18%, var(--superficie)); color: var(--servicio-up); }
.badge-senal.sinsenal { background: color-mix(in srgb, var(--texto-tenue) 18%, var(--superficie)); color: var(--texto-tenue); }

.estado-vacio, .estado-carga { text-align: center; padding: 1.8rem 1rem; color: var(--texto-tenue); font-size: .84rem; border: 1px dashed var(--linea); border-radius: var(--radio-md); }
.esqueleto { background: linear-gradient(90deg, var(--superficie-alta) 25%, var(--superficie-mas-alta) 37%, var(--superficie-alta) 63%); background-size: 400% 100%; animation: brillo 1.4s ease infinite; border-radius: var(--radio-md); height: 80px; }
@keyframes brillo { 0% { background-position: 100% 50%; } 100% { background-position: 0 50%; } }

/* ================= Feed de actividad ================= */
.linea-tiempo { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.linea-tiempo li { position: relative; padding: 0 0 .9rem 1.3rem; border-left: 2px solid var(--linea); font-size: .82rem; }
.linea-tiempo li:last-child { border-color: transparent; }
.linea-tiempo li::before { content: ""; position: absolute; left: -5px; top: 2px; width: 8px; height: 8px; border-radius: 50%; background: var(--acento, var(--marca)); }
.linea-tiempo .hora { font-family: var(--font-mono); color: var(--texto-tenue); font-size: .7rem; }
.linea-tiempo .app-vinculada { color: var(--texto-tenue); font-size: .72rem; }

/* ================= Toasts ================= */
#toasts { position: fixed; top: calc(var(--topbar-h) + 12px); right: 16px; z-index: 950; display: flex; flex-direction: column; gap: .5rem; max-width: 320px; }
.toast { background: var(--superficie-mas-alta); border: 1px solid var(--linea); border-left: 4px solid var(--marca); border-radius: var(--radio-md); padding: .65rem .85rem; font-size: .81rem; box-shadow: var(--sombra-md); animation: entra-toast .22s ease; }
.toast.error { border-left-color: var(--estado-cancelada); }
@keyframes entra-toast { from { opacity: 0; transform: translateX(14px); } to { opacity: 1; transform: none; } }

/* ================= Paleta de comandos (Ctrl+K) ================= */
.cmdk-fondo { position: fixed; inset: 0; background: var(--overlay); z-index: 980; display: none; align-items: flex-start; justify-content: center; padding-top: 12vh; }
.cmdk-fondo.abierto { display: flex; }
.cmdk-caja { width: 100%; max-width: 560px; background: var(--superficie); border: 1px solid var(--linea); border-radius: var(--radio-lg); box-shadow: var(--sombra-md); overflow: hidden; }
.cmdk-caja input { border: 0; border-bottom: 1px solid var(--linea); border-radius: 0; font-size: .95rem; padding: .9rem 1.1rem; }
.cmdk-lista { max-height: 320px; overflow-y: auto; padding: .4rem; }
.cmdk-item { display: flex; align-items: center; gap: .6rem; padding: .55rem .7rem; border-radius: var(--radio-sm); cursor: pointer; font-size: .86rem; }
.cmdk-item svg { width: 16px; height: 16px; flex: none; opacity: .7; }
.cmdk-item.activo, .cmdk-item:hover { background: var(--superficie-alta); }
.cmdk-item .etq { margin-left: auto; font-size: .68rem; color: var(--texto-tenue); }

/* ================= Modal generico ================= */
.modal-fondo { position: fixed; inset: 0; background: var(--overlay); z-index: 900; display: none; align-items: center; justify-content: center; padding: 1rem; }
.modal-fondo.abierto { display: flex; }
.modal-caja { width: 100%; max-width: 640px; max-height: 88vh; overflow-y: auto; background: var(--superficie); border: 1px solid var(--linea); border-radius: var(--radio-lg); box-shadow: var(--sombra-md); padding: 1.2rem 1.3rem; }
.modal-caja .cabecera-modal { display: flex; align-items: center; justify-content: space-between; margin-bottom: .8rem; }
.barra-herramientas { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; }
.barra-herramientas input[type="text"] { max-width: 240px; }
.barra-herramientas select { width: auto; }
.grupo-vista { display: flex; gap: .2rem; background: var(--superficie-alta); border: 1px solid var(--linea); border-radius: 999px; padding: .2rem; }
.grupo-vista button { background: transparent; color: var(--texto-tenue); padding: .32rem .7rem; font-size: .76rem; border-radius: 999px; }
.grupo-vista button.activo { background: var(--marca); color: var(--en-marca); }

/* ================= Chat flotante del agente ================= */
.fab-chat { position: fixed; right: 22px; bottom: 22px; z-index: 800; width: 56px; height: 56px; border-radius: 50%; background: linear-gradient(135deg, var(--marca), var(--marca-dim)); color: var(--en-marca); border: 0; box-shadow: var(--sombra-md); display: flex; align-items: center; justify-content: center; cursor: pointer; }
.fab-chat svg { width: 24px; height: 24px; }
.panel-chat { position: fixed; right: 22px; bottom: 88px; z-index: 850; width: 370px; max-width: calc(100vw - 32px); height: 520px; max-height: 70vh; background: var(--superficie); border: 1px solid var(--linea); border-radius: var(--radio-lg); box-shadow: var(--sombra-md); display: none; flex-direction: column; overflow: hidden; }
.panel-chat.abierto { display: flex; }
.chat-cabecera { display: flex; align-items: center; gap: .55rem; padding: .75rem 1rem; background: var(--superficie-alta); border-bottom: 1px solid var(--linea); }
.chat-cabecera .punto-ia { width: 9px; height: 9px; border-radius: 50%; background: var(--servicio-up); box-shadow: 0 0 0 3px color-mix(in srgb, var(--servicio-up) 25%, transparent); }
.chat-cabecera .titulo { font-weight: 800; font-size: .86rem; flex: 1; }
.chat-cerrar { background: none; border: 0; color: var(--texto-tenue); font-size: 1rem; padding: 0; width: 24px; height: 24px; border-radius: 50%; }
.chat-cuerpo { flex: 1; overflow-y: auto; padding: .85rem; display: flex; flex-direction: column; gap: .55rem; }
.burbuja { max-width: 86%; padding: .5rem .72rem; border-radius: var(--radio-md); font-size: .83rem; line-height: 1.35; white-space: pre-wrap; }
.burbuja.usuario { align-self: flex-end; background: var(--marca); color: var(--en-marca); border-bottom-right-radius: 4px; }
.burbuja.agente { align-self: flex-start; background: var(--superficie-alta); border-bottom-left-radius: 4px; }
.burbuja.agente.error { background: color-mix(in srgb, var(--estado-cancelada) 16%, var(--superficie)); color: var(--estado-cancelada); }
.burbuja .traza-agente summary { cursor: pointer; font-size: .72rem; font-weight: 700; color: var(--estado-enruta); margin-top: .4rem; }
.burbuja .traza-agente .paso { font-size: .71rem; margin-top: .35rem; padding: .35rem .5rem; background: var(--superficie); border-radius: var(--radio-sm); font-family: var(--font-mono); }
.burbuja .traza-agente .paso b { color: var(--marca); font-family: var(--font-ui); }
.escribiendo { align-self: flex-start; display: flex; gap: .25rem; padding: .55rem .75rem; background: var(--superficie-alta); border-radius: var(--radio-md); }
.escribiendo span { width: 6px; height: 6px; border-radius: 50%; background: var(--texto-tenue); animation: rebote 1s infinite ease-in-out; }
.escribiendo span:nth-child(2) { animation-delay: .15s; }
.escribiendo span:nth-child(3) { animation-delay: .3s; }
@keyframes rebote { 0%, 80%, 100% { transform: translateY(0); opacity: .5; } 40% { transform: translateY(-4px); opacity: 1; } }
.chat-sugerencias { display: flex; flex-wrap: wrap; gap: .35rem; padding: 0 .85rem .55rem; }
.chat-sugerencias button { background: var(--superficie-alta); color: var(--texto); border: 1px solid var(--linea); font-weight: 600; font-size: .72rem; padding: .3rem .6rem; border-radius: 999px; }
.chat-form { display: flex; gap: .5rem; padding: .65rem; border-top: 1px solid var(--linea); }
.chat-form textarea { resize: none; height: 40px; padding-top: .5rem; }
.chat-form button { flex: none; width: 40px; height: 40px; border-radius: 50%; padding: 0; font-size: 1rem; }

/* ================= Login ================= */
.login-envoltorio { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 1.5rem; }
.login-tarjeta { width: 100%; max-width: 390px; background: var(--superficie); border: 1px solid var(--linea); border-radius: var(--radio-lg); box-shadow: var(--sombra-md); overflow: hidden; }
.login-marca { background: linear-gradient(135deg, var(--marca), var(--marca-dim)); color: var(--en-marca); padding: 1.6rem 1.5rem 1.3rem; }
.login-marca .logo { width: 44px; height: 44px; border-radius: 13px; background: rgba(0,0,0,.14); display: flex; align-items: center; justify-content: center; font-weight: 900; margin-bottom: .6rem; }
.login-marca h2 { font-size: 1.2rem; }
.login-marca p { margin: .2rem 0 0; font-size: .8rem; opacity: .85; }
.login-cuerpo { padding: 1.35rem 1.5rem 1.6rem; }
.login-cuerpo label { margin-top: .85rem; }
.login-cuerpo label:first-child { margin-top: 0; }
.boton-cargando { display: inline-flex; align-items: center; gap: .5rem; justify-content: center; width: 100%; }
.giro { width: 14px; height: 14px; border-radius: 50%; border: 2px solid rgba(255,255,255,.4); border-top-color: #fff; animation: girar .7s linear infinite; display: none; }
.giro.visible { display: inline-block; }
@keyframes girar { to { transform: rotate(360deg); } }
.demo-usuarios { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: 1rem; }
.demo-usuarios button { flex: 1; min-width: 100px; background: var(--superficie-alta); color: var(--texto); border: 1px solid var(--linea); font-size: .72rem; padding: .4rem .3rem; }

/* ================= App movil: maqueta de telefono ================= */
.maqueta-telefono { width: 280px; margin: 0 auto; border: 10px solid var(--superficie-mas-alta); border-radius: 34px; background: var(--bg); box-shadow: var(--sombra-md); overflow: hidden; }
.maqueta-pantalla { padding: 1rem .9rem; min-height: 480px; font-size: .84rem; }
.maqueta-pantalla h3 { font-size: 1rem; margin-bottom: .7rem; }
.maqueta-tarjeta { background: var(--superficie-alta); border-radius: var(--radio-md); padding: .7rem .8rem; margin-bottom: .7rem; }
.maqueta-tarjeta .fila-dato { display: flex; justify-content: space-between; font-size: .78rem; margin-top: .3rem; }
.maqueta-boton { background: var(--marca); color: var(--en-marca); border-radius: var(--radio-sm); padding: .5rem; text-align: center; font-weight: 700; font-size: .8rem; }

.flujo-integracion { display: flex; align-items: center; flex-wrap: wrap; gap: .4rem; justify-content: center; padding: .6rem 0; }
.flujo-nodo { background: var(--superficie-alta); border: 1px solid var(--linea); border-radius: var(--radio-md); padding: .5rem .8rem; font-size: .76rem; font-weight: 700; text-align: center; }
.flujo-flecha { color: var(--texto-tenue); font-size: 1rem; position: relative; width: 28px; text-align: center; }
.flujo-flecha .paquete { position: absolute; top: -3px; left: 0; width: 6px; height: 6px; border-radius: 50%; background: var(--marca); opacity: 0; }
.flujo-flecha.activo .paquete { animation: viajar 1s ease; }
@keyframes viajar { 0% { opacity: 1; left: 0; } 100% { opacity: 0; left: 26px; } }

/* ================= Arquitectura ================= */
.diagrama-arquitectura { width: 100%; height: auto; }
.nodo-servicio { cursor: pointer; }
.nodo-servicio rect { fill: var(--superficie); stroke: var(--linea); }
.nodo-servicio:hover rect { stroke: var(--marca); }
.nodo-servicio text { fill: var(--texto); font-family: var(--font-ui); }
.nodo-servicio .puerto { fill: var(--texto-tenue); font-family: var(--font-mono); }
.ficha-servicio { margin-top: .8rem; }

/* ================= Modo exposicion / sala de control ================= */
.insignia-rubrica { display: none; position: absolute; top: -8px; right: -8px; background: var(--marca); color: var(--en-marca); font-size: .62rem; font-weight: 800; padding: .15rem .5rem; border-radius: 999px; box-shadow: var(--sombra-sm); z-index: 5; }
body.modo-exposicion .insignia-rubrica { display: inline-flex; }
.tarjeta, .kpi, .panel-mapa { position: relative; }
body.modo-sala .sidebar, body.modo-sala .panel-lateral, body.modo-sala #toasts { display: none; }
body.modo-sala .tablero { grid-template-columns: 1fr; }
body.modo-sala .mapa-grande { height: 82vh; }
body.modo-sala .kpis .valor { font-size: 2.1rem; }
.boton-sala { position: fixed; left: 18px; bottom: 18px; z-index: 800; }

/* ================= Responsive ================= */
@media (max-width: 1180px) {
    .tablero { gap: .9rem; }
}
@media (max-width: 980px) {
    .tablero { grid-template-columns: 1fr; }
    .mapa-grande { height: 460px; }
    .boton-sala { display: none !important; }
    .sidebar { position: fixed; left: -100%; top: 0; height: 100vh; transition: left .2s ease; box-shadow: var(--sombra-md); }
    body.sidebar-movil-abierta .sidebar { left: 0; }
    .solo-movil { display: inline-flex; }
}
@media (max-width: 640px) {
    .topbar { padding: 0 .75rem; gap: .5rem; }
    .reloj { display: none; }
    .chip-usuario .nombre { display: none; }
    main.area-principal { padding: 1rem .75rem 3rem; }
    .fila { grid-template-columns: 1fr; }
    .kpis { grid-template-columns: 1fr 1fr; }
    .panel-chat { right: 10px; left: 10px; width: auto; bottom: 82px; }
    .fab-chat { right: 16px; bottom: 16px; }
    .maqueta-telefono { width: 100%; max-width: 280px; }
}

/* ----- Mapa en tema oscuro: mosaicos de OpenStreetMap invertidos (sin API key) ----- */
html[data-theme="dark"] .leaflet-tile-pane {
    filter: invert(1) hue-rotate(180deg) brightness(.95) contrast(.9);
}

/* ----- Marcadores del mapa ----- */
.marcador-apio { background: none; border: 0; }
.pin { width: 34px; height: 34px; border-radius: 50%; display: flex; align-items: center; justify-content: center; background: var(--pin); color: #fff; border: 2px solid #fff; box-shadow: 0 3px 10px rgba(0,0,0,.45); }
.pin svg { width: 19px; height: 19px; }
.pin-hospital { border-radius: 11px; }
.pin.pulso { animation: pulso-pin 1.4s infinite; }
@keyframes pulso-pin { 0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--pin) 70%, transparent); } 100% { box-shadow: 0 0 0 14px transparent; } }
.pin-etiqueta { position: absolute; top: 36px; left: 50%; transform: translateX(-50%); white-space: nowrap; font-family: var(--font-mono); font-size: .62rem; font-weight: 700; padding: .05rem .35rem; border-radius: 6px; background: var(--superficie); color: var(--texto); border: 1px solid var(--linea); }
.pin-grupo { position: relative; width: 40px; height: 40px; border-radius: 50%; display: flex; align-items: center; justify-content: center; background: var(--superficie); color: var(--marca); border: 2px solid var(--marca); box-shadow: var(--sombra-md); }
.pin-grupo svg { width: 20px; height: 20px; }
.pin-grupo b { position: absolute; top: -6px; right: -6px; min-width: 18px; height: 18px; border-radius: 999px; display: flex; align-items: center; justify-content: center; font-size: .66rem; background: var(--marca); color: var(--en-marca); }
.icono-leyenda { width: 20px; height: 20px; border-radius: 50%; flex: none; display: flex; align-items: center; justify-content: center; background: var(--pin); color: #fff; }
.icono-leyenda svg { width: 12px; height: 12px; }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--superficie); color: var(--texto); box-shadow: var(--sombra-md); }
.leaflet-bar a, .leaflet-bar a:hover { background: var(--superficie); color: var(--texto); border-color: var(--linea); }
.leaflet-container .leaflet-control-attribution { background: var(--overlay); color: var(--texto-tenue); }

/* ----- Tarjetas de indicadores ----- */
.kpi { background: linear-gradient(150deg, color-mix(in srgb, var(--acento, var(--marca)) 10%, var(--superficie)), var(--superficie) 60%); transition: transform .15s ease, box-shadow .15s ease; }
.kpi:hover { transform: translateY(-2px); box-shadow: var(--sombra-md); }
.kpi .valor { font-size: 1.9rem; margin-top: .2rem; }
.kpi svg.spark { color: var(--acento, var(--marca)); }
.kpi svg.spark.plano { opacity: .35; }

/* ----- El mapa no debe tapar el menu ni la barra superior ----- */
.leaflet-container { isolation: isolate; z-index: 0; }

/* ----- Pie del menu lateral y boton de sala ----- */
.sidebar-pie button.boton-icono { border-radius: var(--radio-sm); }
.boton-sala { left: calc(var(--sidebar-w) + 14px); }
body.sidebar-colapsada .boton-sala { left: calc(var(--sidebar-w-colapsado) + 14px); }
body.modo-sala .boton-sala { left: 18px; }
@media (max-width: 980px) {
    body.sidebar-movil-abierta .sidebar { box-shadow: 0 0 0 100vmax var(--overlay); }
}

/* leyenda y controles del mapa quedan por debajo del menu */
.panel-mapa { isolation: isolate; z-index: 0; }

/* la ultima tarjeta de indicadores ocupa el ancho sobrante */
.kpis { display: flex; flex-wrap: wrap; }
.kpis > .kpi { flex: 1 1 168px; }

/* ----- Emergencias ----- */
.filtro-chip b { margin-left: .4rem; font-family: var(--font-mono); opacity: .7; }
.rejilla-emergencias { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: .9rem; }
.rejilla-emergencias .tarjeta-emergencia { padding: 1rem 1.1rem; display: flex; flex-direction: column; gap: .35rem; }
.rejilla-emergencias .descripcion-emergencia { font-size: 1rem; }
.progreso { display: grid; grid-template-columns: repeat(4, 1fr); gap: .3rem; margin: .2rem 0 .6rem; }
.progreso .paso i { display: block; height: 5px; border-radius: 999px; background: var(--superficie-mas-alta); }
.progreso .paso.hecho i { background: var(--acento, var(--marca)); }
.progreso .paso span { display: block; margin-top: .25rem; font-size: .66rem; color: var(--texto-tenue); text-transform: capitalize; }
.progreso .paso.hecho span { color: var(--texto); font-weight: 700; }
.tabla-envoltorio { overflow-x: auto; padding: .4rem 1rem; }
.tabla { width: 100%; border-collapse: collapse; font-size: .85rem; text-align: left; }
.tabla th { color: var(--texto-tenue); font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; }
.tabla th, .tabla td { padding: .65rem .5rem; }
.tabla tbody tr { border-top: 1px solid var(--linea); }
.tabla tbody tr:hover { background: var(--superficie-alta); }

/* ----- Hospitales ----- */
.tarjeta-hospital { display: flex; gap: .8rem; align-items: flex-start; }
.tarjeta-hospital .datos strong { display: block; line-height: 1.25; }
.tarjeta-hospital .pie { margin: .25rem 0 .5rem; }
.icono-leyenda.grande { width: 40px; height: 40px; border-radius: 12px; }
.icono-leyenda.grande svg { width: 22px; height: 22px; }

/* ----- App movil ----- */
.rejilla-movil { display: grid; grid-template-columns: auto 1fr; gap: 1.2rem; align-items: start; }
.maqueta-tarjeta .fila-dato { gap: .8rem; }
.maqueta-tarjeta .fila-dato span:first-child { color: var(--texto-tenue); flex: none; }
.maqueta-tarjeta .fila-dato span:last-child { text-align: right; }
@media (max-width: 760px) { .rejilla-movil { grid-template-columns: 1fr; } }

/* ----- Agente ----- */
.ejemplos-agente { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .6rem; }
.pasos-agente { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: .9rem; }
.pasos-agente b { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; border-radius: 9px; margin-bottom: .5rem; background: var(--marca); color: var(--en-marca); }
.pasos-agente h2 { margin-bottom: .2rem; }

/* =================== Estilo naturaleza =================== */
:root { --hoja: #6F9283; --durazno: #EBA57A; --arena: #F1DFBE; --radio-sm: 12px; --radio-md: 16px; --radio-lg: 22px; }

/* ----- Menu lateral con bosque ----- */
.sidebar { background: var(--menu) url("img/bosque.svg") bottom center / 100% auto no-repeat; border-right: 0; }
.sidebar-logo { border-bottom: 0; color: #FFFCF4; }
.sidebar-logo .logo { background: #FFFCF4; color: #4F6F52; border-radius: 14px; width: 40px; height: 40px; }
.sidebar-logo .logo svg { width: 25px; height: 25px; }
.sidebar-logo .nombre small { color: #DCE6D3; }
.sidebar-nav a { color: #FFFCF4; border-radius: 14px; padding: .65rem .75rem; }
.sidebar-nav a:hover { background: rgba(255,252,244,.16); color: #FFFCF4; }
.sidebar-nav a.activo { background: #F7F3E8; color: #3F5C42; }
.sidebar-pie { border-top: 0; }
.sidebar-pie button.boton-icono { background: #FFFCF4; color: #3F5C42; border: 0; opacity: .92; }

/* ----- Banner con paisaje ----- */
.banner { position: relative; height: 190px; border-radius: var(--radio-lg); overflow: hidden; isolation: isolate; padding: 1.5rem 1.7rem; }
.banner::before { content: ""; position: absolute; inset: 0; z-index: -1; background: #DCEBE4 url("img/paisaje.svg") right bottom / cover no-repeat; }
.banner h2 { font-size: 1.6rem; font-weight: 800; color: #34402F; max-width: 60%; }
.banner p { margin: .35rem 0 0; font-weight: 700; font-size: .9rem; color: #3F5C42; max-width: 48%; }
html[data-theme="dark"] .banner::before { filter: brightness(.55) saturate(.8); }
html[data-theme="dark"] .banner h2, html[data-theme="dark"] .banner p { color: #FFFCF4; text-shadow: 0 1px 6px rgba(0,0,0,.6); }
@media (max-width: 760px) {
    .banner { height: 170px; padding: 1rem 1.1rem; }
    .banner h2 { font-size: 1.2rem; max-width: 100%; }
    .banner p { max-width: 100%; }
}

/* ----- Tarjetas, iconos y botones redondeados ----- */
.kpi::before { display: none; }
.kpi .icono { width: 42px; height: 42px; border-radius: 50%; background: color-mix(in srgb, var(--acento, var(--marca)) 22%, var(--superficie)); }
.kpi .icono svg { width: 22px; height: 22px; stroke: var(--acento, var(--marca)); fill: none; }
.kpi .valor, .reloj { font-family: var(--font-ui); font-weight: 800; }
.estado-vacio::before { content: ""; display: block; width: 140px; height: 90px; margin: 0 auto .5rem; background: url("img/brote.svg") center / contain no-repeat; }
.estado-vacio { text-align: center; }
.fab-chat { background: var(--durazno); color: #34402F; border: 3px solid var(--superficie); }

/* ----- Mapa suave de dia ----- */
html[data-theme="light"] .leaflet-tile-pane { filter: sepia(.22) saturate(.8) hue-rotate(8deg); }

/* ----- Login con paisaje ----- */
.login-envoltorio { background: #DCEBE4 url("img/paisaje.svg") center bottom / max(100%, 1156px) auto no-repeat; }
html[data-theme="dark"] .login-envoltorio { background-color: #1A241D; }
.login-marca { background: var(--menu); color: #FFFCF4; }
.login-marca .logo { background: #FFFCF4; color: #4F6F52; }
.login-marca .logo svg { width: 28px; height: 28px; }

/* el modo sala deja todo el alto al mapa */
body.modo-sala .banner { display: none; }

/* en pantallas angostas la barra solo deja lo necesario (que Salir no se corte) */
@media (max-width: 560px) {
    .reloj, .indicador-vivo, .chip-usuario { display: none; }
    .topbar .titulo-pagina { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}

/* ----- Centros de salud ----- */
.form-ambulancia { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: .8rem; align-items: end; }
.form-ambulancia input, .form-ambulancia select { width: 100%; }
.tarjeta-hospital.propio { border: 2px solid var(--marca); }
