/* ═══════════════════════════════════════════════════════
   BASE — reset, layout, sidebar, topbar, pantalla de carga
   Fleet Dashboard v5
   ═══════════════════════════════════════════════════════ */

*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    font-family: var(--font-sans);
    font-size: var(--t-md);
    line-height: 1.5;
    background: var(--page);
    color: var(--ink);
    min-height: 100vh;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
}

/* Todo número se alinea verticalmente */
.num, td.num, .kpi-value, .stat-value, table td, table th { font-variant-numeric: tabular-nums; }

button, select, input { font: inherit; color: inherit; }

a { color: inherit; text-decoration: none; }

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-radius: var(--r-sm);
}

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: var(--r-pill); }
::-webkit-scrollbar-thumb:hover { background: var(--ink-muted); }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ─────────────────────────────────────────────
   PANTALLA DE CARGA
   ───────────────────────────────────────────── */

#loading-screen {
    position: fixed; inset: 0; z-index: 9999;
    background: var(--page);
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    gap: var(--s-3);
    transition: opacity 0.4s var(--ease), visibility 0.4s var(--ease);
}
#loading-screen.hidden { opacity: 0; visibility: hidden; pointer-events: none; }

.loader-brand {
    font-size: var(--t-xl); font-weight: 600; letter-spacing: -0.02em;
    color: var(--ink);
}
.loader-sub {
    font-size: var(--t-xs); color: var(--ink-muted);
    letter-spacing: 0.14em; text-transform: uppercase;
    margin-bottom: var(--s-5);
}
.loader-bar-track {
    width: 220px; height: 2px; background: var(--border-strong);
    border-radius: var(--r-pill); overflow: hidden;
}
.loader-bar-fill {
    height: 100%; width: 0;
    background: var(--accent); border-radius: var(--r-pill);
    transition: width 0.3s var(--ease);
}
.loader-status {
    font-size: var(--t-xs); color: var(--ink-muted);
    font-family: var(--font-mono);
    min-height: 1.2em;
}
.loader-status.error { color: var(--status-critical); font-family: var(--font-sans); max-width: 420px; text-align: center; }

/* ─────────────────────────────────────────────
   SIDEBAR
   ───────────────────────────────────────────── */

.sidebar {
    position: fixed; left: 0; top: 0; bottom: 0;
    width: var(--sidebar-w-collapsed);
    background: var(--sidebar-bg);
    display: flex; flex-direction: column;
    z-index: 100;
    transition: width 0.24s var(--ease);
    overflow: hidden;
}
.sidebar:hover, .sidebar.pinned { width: var(--sidebar-w); }

.sidebar-header {
    display: flex; align-items: center; gap: var(--s-3);
    height: var(--topbar-h);
    padding: 0 var(--s-4);
    flex-shrink: 0;
    position: relative;
    border-bottom: 1px solid rgba(255,255,255,0.06);
}
.sidebar-mark {
    width: 26px; height: 26px; flex-shrink: 0;
    border-radius: var(--r-sm);
    background: var(--accent);
    display: grid; place-items: center;
    color: #fff;
}
.sidebar-mark svg { width: 15px; height: 15px; stroke: currentColor; fill: none; stroke-width: 2; }
.sidebar-titles { min-width: 0; opacity: 0; transition: opacity 0.15s var(--ease); }
.sidebar:hover .sidebar-titles, .sidebar.pinned .sidebar-titles { opacity: 1; }
.sidebar-logo {
    font-size: var(--t-md); font-weight: 600; color: #fff;
    white-space: nowrap; letter-spacing: -0.01em; line-height: 1.2;
}
.sidebar-company {
    font-size: var(--t-2xs); color: var(--sidebar-ink-dim);
    letter-spacing: 0.08em; text-transform: uppercase; white-space: nowrap;
}

.sidebar-pin {
    position: absolute; right: var(--s-3); top: 50%;
    transform: translateY(-50%);
    width: 24px; height: 24px;
    background: none; border: none; cursor: pointer;
    color: var(--sidebar-ink-dim);
    border-radius: var(--r-sm);
    display: grid; place-items: center;
    opacity: 0; transition: opacity 0.15s var(--ease), color 0.15s var(--ease);
}
.sidebar:hover .sidebar-pin, .sidebar.pinned .sidebar-pin { opacity: 1; }
.sidebar-pin:hover { background: var(--sidebar-hover); color: #fff; }
.sidebar.pinned .sidebar-pin { color: var(--accent); }
.sidebar-pin svg { width: 14px; height: 14px; stroke: currentColor; fill: none; stroke-width: 2; }

.sidebar-nav {
    flex: 1; padding: var(--s-3) var(--s-2);
    overflow-y: auto; overflow-x: hidden;
}
.sidebar-nav::-webkit-scrollbar { width: 0; }

.nav-group-label {
    font-size: var(--t-2xs); font-weight: 600;
    color: var(--sidebar-ink-dim);
    letter-spacing: 0.12em; text-transform: uppercase;
    padding: var(--s-4) var(--s-3) var(--s-2);
    white-space: nowrap;
    opacity: 0; transition: opacity 0.15s var(--ease);
}
.sidebar:hover .nav-group-label, .sidebar.pinned .nav-group-label { opacity: 1; }
.nav-group:first-child .nav-group-label { padding-top: var(--s-2); }

/* Separador visible cuando el sidebar está colapsado y las etiquetas no se leen */
.sidebar:not(:hover):not(.pinned) .nav-group + .nav-group {
    border-top: 1px solid rgba(255,255,255,0.06);
    margin-top: var(--s-2); padding-top: var(--s-2);
}

.nav-item {
    display: flex; align-items: center; gap: var(--s-3);
    height: 36px; padding: 0 var(--s-3);
    border-radius: var(--r-md);
    color: var(--sidebar-ink);
    font-size: var(--t-sm); font-weight: 500;
    cursor: pointer; white-space: nowrap;
    margin-bottom: 1px; position: relative;
    transition: background 0.15s var(--ease), color 0.15s var(--ease);
    border: none; background: none; width: 100%; text-align: left;
}
.nav-item:hover { background: var(--sidebar-hover); color: #e6ecf4; }
.nav-item.active { background: var(--sidebar-active); color: #fff; font-weight: 600; }
.nav-item.active::before {
    content: ''; position: absolute; left: 0; top: 8px; bottom: 8px;
    width: 2px; border-radius: var(--r-pill); background: var(--accent);
}
.nav-icon { width: 18px; height: 18px; flex-shrink: 0; display: grid; place-items: center; }
.nav-icon svg {
    width: 16px; height: 16px; stroke: currentColor; fill: none;
    stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round;
}
.nav-item-text { opacity: 0; transition: opacity 0.15s var(--ease); overflow: hidden; text-overflow: ellipsis; }
.sidebar:hover .nav-item-text, .sidebar.pinned .nav-item-text { opacity: 1; }

.nav-badge {
    margin-left: auto;
    background: var(--status-critical); color: #fff;
    font-size: var(--t-2xs); font-weight: 700;
    min-width: 18px; height: 17px; line-height: 17px;
    text-align: center; border-radius: var(--r-pill);
    padding: 0 5px; flex-shrink: 0;
    opacity: 0; transition: opacity 0.15s var(--ease);
}
.sidebar:hover .nav-badge, .sidebar.pinned .nav-badge { opacity: 1; }
/* Colapsado: el badge se reduce a un punto sobre el icono */
.sidebar:not(:hover):not(.pinned) .nav-badge {
    opacity: 1; position: absolute; top: 6px; left: 26px;
    min-width: 7px; width: 7px; height: 7px; padding: 0;
    font-size: 0; overflow: hidden;
}

.sidebar-footer {
    padding: var(--s-3) var(--s-4);
    border-top: 1px solid rgba(255,255,255,0.06);
    font-size: var(--t-2xs); color: var(--sidebar-ink-dim);
    white-space: nowrap; line-height: 1.5;
    opacity: 0; transition: opacity 0.15s var(--ease);
}
.sidebar:hover .sidebar-footer, .sidebar.pinned .sidebar-footer { opacity: 1; }
.sidebar-footer .cache-flag { color: var(--status-good); }

/* ─────────────────────────────────────────────
   CONTENIDO PRINCIPAL
   ───────────────────────────────────────────── */

.main {
    margin-left: var(--sidebar-w-collapsed);
    min-height: 100vh;
    transition: margin-left 0.24s var(--ease);
}
.sidebar.pinned ~ .main { margin-left: var(--sidebar-w); }

/* ─────────────────────────────────────────────
   TOPBAR
   ───────────────────────────────────────────── */

.topbar {
    position: sticky; top: 0; z-index: 50;
    min-height: var(--topbar-h);
    background: color-mix(in srgb, var(--page) 88%, transparent);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
    display: flex; align-items: center; gap: var(--s-4);
    padding: var(--s-2) var(--s-6);
}
.topbar-left { display: flex; align-items: baseline; gap: var(--s-3); min-width: 0; flex-shrink: 0; }
.topbar-title {
    font-size: var(--t-lg); font-weight: 600; letter-spacing: -0.015em;
    white-space: nowrap; color: var(--ink);
}
.topbar-context {
    font-size: var(--t-xs); color: var(--ink-muted);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: var(--s-2); flex-shrink: 0; }

/* Los filtros son una sola fila por encima del contenido */
.filters {
    display: flex; align-items: center; gap: var(--s-2);
    flex-wrap: wrap; justify-content: flex-end;
    flex: 1; min-width: 0;
}
.filter {
    display: inline-flex; align-items: center;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--r-md); height: 30px;
    box-shadow: var(--shadow-sm);
    transition: border-color 0.15s var(--ease);
}
.filter:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-ring); }
.filter.is-set { border-color: var(--accent); background: var(--accent-soft); }
.filter-label {
    font-size: var(--t-2xs); color: var(--ink-muted);
    text-transform: uppercase; letter-spacing: 0.07em; font-weight: 600;
    padding: 0 var(--s-1) 0 var(--s-3);
    white-space: nowrap; pointer-events: none;
}
.filter select {
    appearance: none; background: transparent; border: none;
    padding: 0 26px 0 var(--s-1);
    font-size: var(--t-sm); font-weight: 500; color: var(--ink);
    height: 100%; cursor: pointer; outline: none;
    max-width: 170px; text-overflow: ellipsis;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2378849a' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 7px center;
    background-size: 13px;
}
.filter select option { background: var(--surface); color: var(--ink); }

.icon-btn {
    width: 30px; height: 30px; flex-shrink: 0;
    display: grid; place-items: center;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--r-md); cursor: pointer;
    color: var(--ink-secondary);
    box-shadow: var(--shadow-sm);
    transition: color 0.15s var(--ease), border-color 0.15s var(--ease), background 0.15s var(--ease);
}
.icon-btn:hover { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
.icon-btn svg { width: 15px; height: 15px; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.danger:hover { color: var(--status-critical); border-color: var(--status-critical); background: rgba(208,59,59,0.08); }

.menu-toggle { display: none; }

/* ─────────────────────────────────────────────
   CONTENIDO DE PÁGINA
   ───────────────────────────────────────────── */

.page { padding: var(--s-6); max-width: 1720px; }
.section { display: none; }
.section.active { display: block; }

/* Filas de contenido */
.row { display: grid; gap: var(--s-4); margin-bottom: var(--s-4); }
.row.c1 { grid-template-columns: 1fr; }
.row.c2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.row.c3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.row.c4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.row.split-2-1 { grid-template-columns: 2fr minmax(0, 1fr); }
.row.split-1-2 { grid-template-columns: minmax(0, 1fr) 2fr; }

/* ─────────────────────────────────────────────
   RESPONSIVE
   ───────────────────────────────────────────── */

@media (max-width: 1280px) {
    .row.c4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .row.c3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .row.split-2-1, .row.split-1-2 { grid-template-columns: 1fr; }
}

@media (max-width: 900px) {
    .sidebar { width: 0; }
    .sidebar.open { width: var(--sidebar-w); box-shadow: var(--shadow-lg); }
    .sidebar.open .sidebar-titles,
    .sidebar.open .sidebar-footer,
    .sidebar.open .nav-group-label,
    .sidebar.open .nav-item-text,
    .sidebar.open .nav-badge { opacity: 1; }
    .main, .sidebar.pinned ~ .main { margin-left: 0; }
    .menu-toggle { display: grid; }
    .topbar { flex-wrap: wrap; padding: var(--s-2) var(--s-4); }
    .filters { justify-content: flex-start; width: 100%; }
    .page { padding: var(--s-4); }
    .row.c2, .row.c3, .row.c4 { grid-template-columns: 1fr; }
}

@media (max-width: 560px) {
    .topbar-context { display: none; }
    .filter select { max-width: 120px; }
}

/* ─────────────────────────────────────────────
   ANIMACIONES DE ENTRADA
   ───────────────────────────────────────────── */

@keyframes riseIn {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: none; }
}
.rise { animation: riseIn 0.32s var(--ease) both; }
.rise-1 { animation-delay: 0.02s; }
.rise-2 { animation-delay: 0.05s; }
.rise-3 { animation-delay: 0.08s; }
.rise-4 { animation-delay: 0.11s; }
.rise-5 { animation-delay: 0.14s; }
.rise-6 { animation-delay: 0.17s; }
.rise-7 { animation-delay: 0.20s; }
.rise-8 { animation-delay: 0.23s; }

/* ─────────────────────────────────────────────
   ESQUELETOS DE CARGA
   ───────────────────────────────────────────── */

@keyframes shimmer {
    0%   { background-position: -420px 0; }
    100% { background-position: 420px 0; }
}
.skeleton {
    background: linear-gradient(90deg,
        var(--surface-sunken) 25%,
        color-mix(in srgb, var(--surface-sunken) 60%, var(--border-strong)) 50%,
        var(--surface-sunken) 75%);
    background-size: 840px 100%;
    animation: shimmer 1.4s infinite linear;
    border-radius: var(--r-lg);
}
.skeleton-kpi   { height: 104px; }
.skeleton-chart { height: 340px; }
.skeleton-table { height: 220px; }
