:root {
  --bg: #0f1419;
  --panel: #1a212b;
  --panel-2: #222c38;
  --line: #2c3947;
  --text: #e6edf3;
  --muted: #8b9bb0;
  --accent: #4da3ff;
  --accent-2: #56d4a0;
  --warn: #f0a868;
  --violet: #b78cff;
  --on-accent: #04101f;   /* text/icon color drawn on top of an --accent fill */
  --radius: 10px;
}
/* Light theme — toggled via data-theme on <html> (see common.js). Only the tokens
   change; every component reads them, so the whole UI re-themes. */
[data-theme="light"] {
  --bg: #eef1f5;
  --panel: #ffffff;
  --panel-2: #e7ecf2;
  --line: #d4dbe3;
  --text: #1b2733;
  --muted: #57687a;
  --accent: #2563eb;
  --accent-2: #15a35a;
  --warn: #c2660a;
  --violet: #7c4dff;
  --on-accent: #ffffff;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}
h2, h3, h4 { margin: 0 0 .6rem; font-weight: 600; }
h3 { font-size: .95rem; color: var(--text); }
h4 { font-size: .85rem; color: var(--muted); margin-top: 1rem; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: .7rem 1.2rem; background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand { font-weight: 700; font-size: 1.05rem; display: flex; align-items: center; gap: .7rem; }
.brand span { color: var(--muted); font-weight: 400; font-size: .85rem; }
.brand .logo { height: 42px; width: auto; display: block; }
.brand .sep { color: var(--line); font-weight: 300; }
.topbar-right { display: flex; align-items: center; gap: .6rem; margin-left: auto; }
.user { color: var(--muted); font-size: .8rem; }

.topnav { display: flex; gap: .4rem; margin-left: 1.4rem; }
.topnav a {
  color: var(--muted); text-decoration: none; font-size: .85rem; font-weight: 600;
  padding: .35rem .7rem; border-radius: 7px;
}
.topnav a:hover { background: var(--panel-2); color: var(--text); }
.topnav a.active { background: var(--panel-2); color: var(--accent); }
.toggle { color: var(--muted); font-size: .8rem; display: flex; align-items: center; gap: .35rem; cursor: pointer; }
.logout-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 30px; height: 30px; border: 1px solid var(--line); border-radius: 8px;
  color: var(--muted); text-decoration: none; font-size: 1.05rem; line-height: 1;
}
.logout-btn:hover { background: var(--warn); color: #2a1500; border-color: var(--warn); }

.btn {
  background: var(--accent); color: var(--on-accent); border: 0; border-radius: 8px;
  padding: .45rem .8rem; font-weight: 600; cursor: pointer; font-size: .85rem;
}
.btn:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; color: var(--muted); border: 1px solid var(--line); }

select, input[type="search"], input[type="file"], input[type="date"] {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: .4rem .55rem; font-size: .85rem; outline: none;
}
input[type="date"] { color-scheme: dark; }
select:hover, input[type="search"]:hover, input[type="date"]:hover { border-color: var(--accent); }
select:focus, input[type="search"]:focus, input[type="date"]:focus { border-color: var(--accent); }
/* Dropdown option list (and multiselect rows) — force dark theme so text is legible. */
select option, select optgroup {
  background: #222c38; color: var(--text);
}
select option:checked, select option:hover {
  background: #2f4055; color: #fff;
}
select[multiple] { min-width: 140px; padding: .2rem; }
select[multiple] option { padding: .25rem .4rem; border-radius: 4px; }
input[type="search"]::placeholder { color: #6c7c8e; }
/* Firefox honors these for the option popup */
select { color-scheme: dark; }
/* Light theme: native controls + dropdown popups follow the light scheme. */
[data-theme="light"] select,
[data-theme="light"] input[type="date"] { color-scheme: light; }
[data-theme="light"] select option, [data-theme="light"] select optgroup {
  background: #ffffff; color: var(--text);
}
[data-theme="light"] select option:checked, [data-theme="light"] select option:hover {
  background: #dbe6fb; color: #0b1c3a;
}
/* The LogeX wordmark art is white-on-transparent (made for the dark header). In light
   mode give it a dark backing pill so the white "eX / Supply Chain" stays legible. */
[data-theme="light"] .brand .logo {
  background: #0f1419; padding: 3px 8px; border-radius: 7px;
}

.filters {
  display: flex; gap: .8rem; align-items: flex-end; flex-wrap: wrap;
  padding: .9rem 1.2rem; background: var(--panel); border-bottom: 1px solid var(--line);
}
.filter { display: flex; flex-direction: column; gap: .25rem; }
.filter[hidden] { display: none; }  /* author rule beats .filter's display:flex over UA [hidden] */
.filter label { font-size: .7rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }

/* Segmented Mes/Día view toggle (Resumen filter bar) */
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg-btn {
  background: var(--panel-2); color: var(--muted); border: 0; border-right: 1px solid var(--line);
  padding: .4rem .75rem; font-size: .8rem; font-weight: 600; cursor: pointer; line-height: 1;
}
.seg-btn:last-child { border-right: 0; }
.seg-btn:hover { color: var(--text); }
.seg-btn.active { background: var(--accent); color: var(--on-accent); }

main { padding: 1.2rem; max-width: 1500px; margin: 0 auto; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: .8rem; margin-bottom: 1.2rem; }
.kpi {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: .9rem 1rem;
}
.kpi .label { color: var(--muted); font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; }
.kpi .value { font-size: 1.5rem; font-weight: 700; margin-top: .25rem; }
.kpi .delta { font-size: .75rem; margin-top: .2rem; }

/* Insight cards (MoM narrative) */
.insights { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: .8rem; margin-bottom: 1.2rem; }
.insight { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem; border-left: 3px solid var(--muted); }
.insight.up { border-left-color: var(--accent-2); }
.insight.down { border-left-color: var(--warn); }
.insight.neutral { border-left-color: var(--accent); }
.insight-big { font-size: 1.15rem; font-weight: 700; }
.insight.up .insight-big { color: var(--accent-2); }
.insight.down .insight-big { color: var(--warn); }
.insight-label { color: var(--muted); font-size: .76rem; margin-top: .25rem; }

/* Sparkline + delta coloring in the project-intel table */
svg.spark { display: block; }
td.delta.up { color: var(--accent-2); }
td.delta.down { color: var(--warn); }
td.delta.flat { color: var(--muted); }
#projIntelTable tbody tr { cursor: pointer; }
#projIntelTable tbody tr:hover td { background: var(--panel-2); }
.delta.up { color: var(--accent-2); }
.delta.down { color: var(--warn); }
.delta.flat { color: var(--muted); }

.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: .9rem; margin-bottom: 1rem; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 1rem;
}
.card.wide { grid-column: 1 / -1; }
@media (min-width: 1100px) {
  .grid .card.wide { grid-column: span 2; }
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.chart-controls { display: flex; align-items: center; gap: .5rem; }
.chart-controls select { font-size: .8rem; padding: .3rem .45rem; }
.icon-btn {
  background: transparent; border: 1px solid var(--line); border-radius: 7px;
  color: var(--muted); cursor: pointer; font-size: 1rem; line-height: 1; padding: .3rem .5rem;
}
.icon-btn:hover { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }

/* Fixed-height chart container. Chart.js with maintainAspectRatio:false sizes the
   canvas to its parent; without a height-constrained parent the canvas grows on every
   resize (infinite-growth bug). The relative box + absolute canvas pins the height. */
.chart-box { position: relative; height: 240px; width: 100%; }
.chart-box.tall { height: 340px; }
.chart-box > canvas { position: absolute; inset: 0; width: 100% !important; height: 100% !important; }

.table-wrap { overflow: auto; max-height: 540px; border-radius: 8px; }
.table-wrap.small { max-height: 300px; }
table { width: 100%; border-collapse: collapse; font-size: .82rem; }
th, td { padding: .45rem .6rem; text-align: left; white-space: nowrap; }
thead th {
  position: sticky; top: 0; background: var(--panel-2); color: var(--muted);
  font-weight: 600; cursor: pointer; user-select: none; border-bottom: 1px solid var(--line);
}
tbody tr { border-bottom: 1px solid var(--line); }
tbody tr:hover { background: var(--panel-2); cursor: pointer; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
.table-foot { color: var(--muted); font-size: .78rem; padding: .5rem .2rem 0; }

.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex;
  align-items: center; justify-content: center; z-index: 50; padding: 1rem;
}
.modal[hidden] { display: none; }
.modal-box {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 1.4rem; width: min(960px, 96vw); max-height: 92vh; overflow: auto; position: relative;
}
.modal-box.small-box { width: min(460px, 96vw); }
.modal-close { position: absolute; top: .7rem; right: .9rem; background: none; border: 0; color: var(--muted); font-size: 1.6rem; cursor: pointer; }
.meta { color: var(--muted); margin-bottom: .8rem; font-size: .85rem; }
.modal-actions { margin-top: 1rem; }
.hint { color: var(--muted); font-size: .82rem; }
.status { margin-top: .8rem; font-size: .85rem; }

.toast {
  position: fixed; bottom: 1.2rem; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  padding: .7rem 1.1rem; border-radius: 10px; z-index: 60; font-size: .85rem;
}
.toast[hidden] { display: none; }
.empty { color: var(--muted); padding: 2rem; text-align: center; }

/* ---- Resumen (pivot) page ---- */
.pivot-month { margin-bottom: 1.6rem; }
.pivot-month-label {
  font-weight: 700; font-size: 1rem; color: var(--accent);
  margin: .2rem 0 .5rem; letter-spacing: .02em;
}
/* Three panels per month (Resumen / Variación $ / Variación %), stacked and all the
   same width. table-layout:fixed + width:100% makes every table and its columns line
   up identically regardless of cell content. */
.pivot-grids { display: flex; flex-direction: column; gap: 1rem; align-items: stretch; }
.pivot-grid { width: 100%; overflow-x: auto; }
table.pivot {
  border-collapse: collapse; font-size: .8rem; background: #fff; color: #1f2d3d;
  border-radius: 6px; overflow: hidden; width: 100%; table-layout: fixed;
}
table.pivot th, table.pivot td {
  padding: .3rem .5rem; border: 1px solid #e3e8ee; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
table.pivot thead th { font-weight: 700; font-size: .78rem; text-align: right; }
table.pivot thead th.rowhead { text-align: left; width: 150px; }
table.pivot thead th.orange { background: #f0a868; color: #2a1500; }
table.pivot thead th.gray { background: #8a99a8; color: #0d141b; }
table.pivot thead th.orange.num, table.pivot thead .orange ~ th { }
.resumen-tbl thead th { background: #fbe2c8; color: #5a3410; }
.resumen-tbl thead th.rowhead { background: #f0a868; color: #2a1500; }
.variation-tbl thead th { background: #d7dde3; color: #2a3742; }
.variation-tbl thead th.rowhead { background: #8a99a8; color: #0d141b; }
table.pivot td.rowhead { text-align: left; font-weight: 500; }
table.pivot td.num, table.pivot th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.pivot tr.totrow td { font-weight: 700; background: #f4f6f8; border-top: 2px solid #c7d0d9; }
table.pivot .totcol { font-weight: 700; background: rgba(0,0,0,.03); }
table.pivot tbody tr:hover td { background: #fff8ef; }
table.pivot tbody tr.totrow:hover td { background: #eef1f4; }

/* ---- Empleados (per-employee summary) page ---- */
.filter.grow { flex: 1; min-width: 180px; }
.filter.grow input[type="search"] { width: 100%; }
table.emp-summary { width: 100%; }
table.emp-summary thead th.sorted { color: var(--accent); }
table.emp-summary td .sub { color: var(--muted); font-size: .72rem; margin-top: 1px; }
table.emp-summary tfoot td {
  position: sticky; bottom: 0; background: var(--panel-2);
  font-weight: 700; border-top: 2px solid var(--line); white-space: nowrap;
}
/* View-detail icon button on each Empleados row */
th.viewcol, td.viewcell { width: 34px; text-align: center; }
.view-btn {
  background: transparent; border: 1px solid var(--line); border-radius: 6px;
  color: var(--accent); cursor: pointer; font-size: .95rem; line-height: 1;
  padding: .2rem .35rem;
}
.view-btn:hover { background: var(--accent); color: var(--on-accent); }
/* Wide modal for the trip-level detail (TD Det 1) */
.modal-box.wide-box { width: min(1400px, 98vw); }
#tripTable { font-size: .78rem; }
#tripTable th, #tripTable td { white-space: nowrap; }

/* ---------------- cn-kpis additions ---------------- */
/* KPI sub-label + delta coloring */
.kpi .value.good { color: var(--accent-2); }
.kpi .value.bad { color: var(--warn); }
.kpi .sub { color: var(--muted); font-size: .72rem; margin-top: .35rem; }
.kpi .delta.up { color: var(--accent-2); }
.kpi .delta.down { color: var(--warn); }

/* Light/dark theme toggle (topbar) — fixed width so ☀/🌙 don't shift the header */
.theme-toggle { min-width: 34px; text-align: center; padding: .3rem 0; }
.theme-toggle:hover { filter: none; }

/* "last updated" badge + refresh button in the topbar */
.refresh-badge { color: var(--muted); font-size: .76rem; white-space: nowrap; }
.refresh-badge b { color: var(--text); font-weight: 600; }
.btn.busy { opacity: .6; pointer-events: none; }
.spin { display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Inicio landing */
.hero { display: grid; grid-template-columns: 1.3fr 1fr; gap: 1rem; margin-bottom: 1.2rem; }
@media (max-width: 980px) { .hero { grid-template-columns: 1fr; } }
#cdMap { height: 460px; width: 100%; border-radius: var(--radius); border: 1px solid var(--line); background: var(--panel-2); }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--panel); color: var(--text); }
.leaflet-popup-content { font-size: .82rem; }
.leaflet-popup-content b { color: var(--accent); }
.map-legend { color: var(--muted); font-size: .76rem; margin-top: .4rem; }

/* Admin (reference CRUD) */
.tabs { display: flex; gap: .4rem; margin-bottom: 1rem; }
.tabs button {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--muted);
  border-radius: 8px; padding: .4rem .8rem; cursor: pointer; font-weight: 600; font-size: .85rem;
}
.tabs button.active { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.tabpane[hidden] { display: none; }
.form-row { display: flex; gap: .6rem; flex-wrap: wrap; align-items: flex-end; margin-bottom: .9rem; }
.form-row .filter input { background: var(--panel-2); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: .4rem .55rem; font-size: .85rem; }
.row-actions button { margin-right: .3rem; }
td .lnk { color: var(--accent); cursor: pointer; }
td .lnk.danger { color: var(--warn); }

/* CD chip strip in the Resumen filter bar — one horizontally-scrollable row, so it
   never adds vertical height. Stretches to fill the remaining bar width. */
.filter.cd-grow { flex: 1 1 320px; min-width: 0; }
.cd-strip {
  display: flex; gap: .3rem; overflow-x: auto; overflow-y: hidden;
  padding-bottom: 3px; scrollbar-width: thin; scrollbar-color: var(--line) transparent;
}
.cd-strip::-webkit-scrollbar { height: 6px; }
.cd-strip::-webkit-scrollbar-thumb { background: var(--line); border-radius: 3px; }
.cd-chip {
  flex: 0 0 auto; background: var(--panel-2); color: var(--muted);
  border: 1px solid var(--line); border-radius: 999px;
  padding: .38rem .7rem; font-size: .8rem; line-height: 1; cursor: pointer;
  white-space: nowrap; user-select: none;
}
.cd-chip:hover { border-color: var(--accent); color: var(--text); }
.cd-chip.active { background: var(--accent); color: var(--on-accent); border-color: var(--accent); font-weight: 600; }

/* ---------------- Login page ---------------- */
.login-body { min-height: 100vh; margin: 0; }
.login-shell { min-height: 100vh; display: grid; grid-template-columns: 1.05fr .95fr; }

/* Brand hero — intentionally dark in both themes (the logo art is white-on-transparent),
   with layered radial glows over a navy gradient for depth. */
.login-hero {
  position: relative; overflow: hidden; color: #e6edf3;
  padding: 3.4rem 3.4rem; display: flex; flex-direction: column; justify-content: space-between;
  background:
    radial-gradient(1100px 480px at 12% -12%, rgba(77, 163, 255, .20), transparent 60%),
    radial-gradient(900px 600px at 105% 115%, rgba(86, 212, 160, .12), transparent 55%),
    linear-gradient(160deg, #0d1622 0%, #0f1d30 55%, #0b1320 100%);
}
.login-hero-inner { margin: auto 0; max-width: 460px; position: relative; z-index: 1; }
.login-logo { height: 54px; width: auto; display: block; margin-bottom: 2.4rem; }
.login-hero-title { font-size: 2.05rem; line-height: 1.15; font-weight: 700; letter-spacing: -.01em; margin: 0 0 1rem; }
.login-hero-sub { color: #9fb1c6; font-size: 1rem; line-height: 1.6; margin: 0 0 1.9rem; max-width: 42ch; }
.login-feats { list-style: none; padding: 0; margin: 0; display: grid; gap: .75rem; }
.login-feats li { position: relative; padding-left: 1.9rem; color: #c4d2e2; font-size: .92rem; line-height: 1.4; }
.login-feats li::before {
  content: "✓"; position: absolute; left: 0; top: -1px;
  display: inline-flex; align-items: center; justify-content: center;
  width: 1.25rem; height: 1.25rem; border-radius: 50%;
  background: rgba(86, 212, 160, .16); color: #56d4a0; font-size: .68rem; font-weight: 700;
}
.login-hero-foot { position: relative; z-index: 1; color: #5d728c; font-size: .74rem; letter-spacing: .12em; text-transform: uppercase; }

/* Sign-in card — uses the active theme tokens (light or dark). */
.login-card {
  position: relative; background: var(--bg);
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 2.5rem;
}
.login-card-inner { width: 100%; max-width: 360px; }
.login-badge {
  display: inline-block; font-weight: 800; letter-spacing: .03em; font-size: .78rem;
  color: var(--accent); background: var(--panel); border: 1px solid var(--line);
  border-radius: 999px; padding: .34rem .85rem; margin-bottom: 1.7rem;
}
.login-title { font-size: 1.75rem; margin: 0 0 .45rem; }
.login-lead { color: var(--muted); font-size: .95rem; line-height: 1.5; margin: 0 0 1.9rem; }
.login-btn {
  display: flex; align-items: center; justify-content: center; gap: .6rem; width: 100%;
  background: var(--accent); color: var(--on-accent); border: 0; border-radius: 11px;
  padding: .85rem 1rem; font-weight: 700; font-size: .95rem; text-decoration: none; cursor: pointer;
  box-shadow: 0 10px 24px -12px var(--accent); transition: filter .15s ease, transform .05s ease;
}
.login-btn:hover { filter: brightness(1.08); }
.login-btn:active { transform: translateY(1px); }
.login-btn svg { width: 18px; height: 18px; }
.login-error {
  background: color-mix(in srgb, var(--warn) 14%, transparent);
  border: 1px solid var(--warn); color: var(--warn);
  border-radius: 9px; padding: .7rem .9rem; font-size: .85rem; margin-bottom: 1.3rem;
}
.login-help { color: var(--muted); font-size: .8rem; line-height: 1.5; margin: 1.3rem 0 0; }
.login-foot { position: absolute; bottom: 1.4rem; color: var(--muted); font-size: .76rem; }

.login-theme {
  position: fixed; top: 1.1rem; right: 1.2rem; z-index: 10;
  width: 36px; height: 36px; border-radius: 9px; cursor: pointer; font-size: 1.05rem; line-height: 1;
  background: rgba(255, 255, 255, .06); border: 1px solid rgba(255, 255, 255, .18); color: #e6edf3;
  backdrop-filter: blur(4px);
}
.login-theme:hover { background: rgba(255, 255, 255, .12); }

@media (max-width: 860px) {
  .login-shell { grid-template-columns: 1fr; }
  .login-hero { display: none; }
}
