:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --primary: #1d4ed8;
  --primary-soft: #e0e7ff;
  --green: #059669;
  --green-soft: #d1fae5;
  --red: #dc2626;
  --red-soft: #fee2e2;
  --amber: #b45309;
  --amber-soft: #fef3c7;
  --gray-soft: #f3f4f6;
  --sidebar: #0f172a;
  --input: #ffffff;
  --thead: #f8f9fb;
  --hover: #f5f7fa;
  --neutral-badge: #374151;
  --chart-a: #c7d2fe;
  --violet: #7c3aed;
  --violet-soft: #ede9fe;
  --wa: #15803d;
  --wa-soft: #dcfce7;
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1020; --card: #131a2c; --text: #e5e9f2; --muted: #94a0b8; --line: #263049;
    --primary: #5b8cff; --primary-soft: #1c2a4d; --green: #34d399; --green-soft: #0f3328;
    --red: #f87171; --red-soft: #3a1717; --amber: #fbbf24; --amber-soft: #3a2a0d; --gray-soft: #1d2538;
    --sidebar: #070b16; --input: #0f1526; --thead: #182038; --hover: #1a2236; --neutral-badge: #cbd5e1; --chart-a: #3b4f86; --violet: #a78bfa; --violet-soft: #2a1f4d; --wa: #4ade80; --wa-soft: #0f2e1c;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1020; --card: #131a2c; --text: #e5e9f2; --muted: #94a0b8; --line: #263049;
  --primary: #5b8cff; --primary-soft: #1c2a4d; --green: #34d399; --green-soft: #0f3328;
  --red: #f87171; --red-soft: #3a1717; --amber: #fbbf24; --amber-soft: #3a2a0d; --gray-soft: #1d2538;
  --sidebar: #070b16; --input: #0f1526; --thead: #182038; --hover: #1a2236; --neutral-badge: #cbd5e1; --chart-a: #3b4f86; --violet: #a78bfa; --violet-soft: #2a1f4d; --wa: #4ade80; --wa-soft: #0f2e1c;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font-family: "Plus Jakarta Sans", system-ui, sans-serif; background: var(--bg); color: var(--text); font-size: 14px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
h1, h2, h3 { margin: 0; }
input, select, textarea, button { font: inherit; }

/* ---- botones e inputs ---- */
.btn { border: 1px solid var(--line); background: var(--card); color: var(--text); padding: 9px 14px; border-radius: 8px; cursor: pointer; font-weight: 600; }
.btn:hover { filter: brightness(.97); }
.btn:disabled { opacity: .6; cursor: wait; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-danger { background: var(--red-soft); color: var(--red); border-color: transparent; }
.btn-ghost { background: transparent; border-color: rgba(255,255,255,.2); color: #cbd5e1; }
.btn-sm { padding: 6px 10px; font-size: 13px; }
.btn-block { width: 100%; }
label { display: flex; flex-direction: column; gap: 4px; font-weight: 600; font-size: 12px; color: var(--muted); }
input, select, textarea { border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; color: var(--text); background: var(--input); font-weight: 500; font-size: 14px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
.form-error { color: var(--red); min-height: 18px; margin: 8px 0 0; font-size: 13px; }

/* ---- acceso ---- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(135deg, #0f172a, #1e3a8a); }
.auth-card { background: var(--card); padding: 28px; border-radius: 16px; width: 100%; max-width: 380px; }
.auth-card form { display: flex; flex-direction: column; gap: 12px; }
.auth-card h1 { font-size: 20px; margin-top: 14px; }
.brand { font-weight: 800; font-size: 17px; display: flex; align-items: center; gap: 8px; }
.brand-mark { width: 30px; height: 30px; border-radius: 8px; background: var(--primary); color: #fff; display: grid; place-items: center; font-weight: 800; }
.brand-mark svg { width: 17px; height: 17px; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.btn svg { width: 16px; height: 16px; }

/* ---- layout ---- */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar { background: var(--sidebar); color: #e2e8f0; padding: 20px 14px; display: flex; flex-direction: column; gap: 22px; position: sticky; top: 0; height: 100vh; }
.sidebar .brand { color: #fff; padding: 0 6px; }
#nav { display: flex; flex-direction: column; gap: 2px; }
#nav a { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px; cursor: pointer; color: #94a3b8; font-weight: 600; text-decoration: none; }
#nav a svg { width: 18px; height: 18px; flex: none; stroke-width: 2; }
#nav a:hover { background: rgba(255,255,255,.06); color: #e2e8f0; }
#nav a.active { background: rgba(255,255,255,.1); color: #fff; }
#nav a.active svg { color: #7aa2ff; }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 8px; padding: 0 6px; }
.yo { font-size: 12px; color: #94a3b8; }
.main { padding: 20px 24px 40px; min-width: 0; }
.topbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.topbar h2 { font-size: 22px; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }

/* ---- tarjetas ---- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.card h3 { font-size: 15px; margin-bottom: 12px; }
.grid { display: grid; gap: 12px; }
.kpis { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.kpi .k-label { color: var(--muted); font-size: 12px; font-weight: 600; }
.kpi .k-val { font-size: 22px; font-weight: 800; margin-top: 4px; }
.kpi .k-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.kpi.pos .k-val { color: var(--green); }
.kpi.neg .k-val { color: var(--red); }
.kpi.destacado { background: var(--sidebar); color: #fff; border-color: var(--sidebar); }
.kpi.destacado .k-label, .kpi.destacado .k-sub { color: #94a3b8; }
.section-title { font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 22px 0 10px; }
.two-col { grid-template-columns: 2fr 1fr; }
.aviso { background: var(--amber-soft); color: var(--amber); padding: 10px 12px; border-radius: 8px; font-weight: 600; font-size: 13px; margin-bottom: 12px; }
.aviso.info { background: var(--primary-soft); color: var(--primary); }

/* ---- filtros ---- */
.filtros { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.chip { border: 1px solid var(--line); background: var(--card); padding: 6px 12px; border-radius: 999px; cursor: pointer; font-weight: 600; font-size: 13px; }
.chip.active { background: var(--text); color: var(--card); border-color: var(--text); }
.chip .n { opacity: .6; margin-left: 4px; }
.filtros input[type=search] { min-width: 220px; flex: 1; }

/* ---- tablas ---- */
.tabla-wrap { overflow-x: auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { font-size: 12px; color: var(--muted); font-weight: 700; background: var(--thead); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.wrap { white-space: normal; min-width: 180px; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover { background: var(--hover); }
tfoot td { font-weight: 800; background: var(--thead); }
.vacio { padding: 30px; text-align: center; color: var(--muted); }

.badge { display: inline-block; padding: 3px 8px; border-radius: 999px; font-size: 12px; font-weight: 700; }
.b-por_despachar { background: var(--gray-soft); color: var(--neutral-badge); }
.b-en_camino { background: var(--primary-soft); color: var(--primary); }
.b-esperando { background: var(--violet-soft); color: var(--violet); }
.b-novedad { background: var(--amber-soft); color: var(--amber); }
.b-entregado { background: var(--green-soft); color: var(--green); }
.b-no_entregado { background: var(--red-soft); color: var(--red); }
.b-cancelado { background: var(--red-soft); color: var(--red); }

/* ---- formularios en línea ---- */
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; align-items: end; }

/* ---- modal ---- */
.modal { position: fixed; inset: 0; background: rgba(15,23,42,.5); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal-card { background: var(--card); border-radius: 16px; padding: 20px; width: 100%; max-width: 620px; max-height: 90vh; overflow-y: auto; }
.modal-head { display: flex; justify-content: space-between; align-items: start; gap: 10px; margin-bottom: 14px; }
.dl { display: grid; grid-template-columns: 140px 1fr; gap: 6px 12px; margin-bottom: 16px; }
.dl dt { color: var(--muted); font-weight: 600; }
.dl dd { margin: 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
pre.raw { background: var(--gray-soft); padding: 10px; border-radius: 8px; font-size: 11px; max-height: 220px; overflow: auto; }

.toast { position: fixed; bottom: calc(20px + env(safe-area-inset-bottom, 0px)); left: 50%; transform: translateX(-50%); background: var(--text); color: var(--card); padding: 10px 16px; border-radius: 10px; z-index: 60; font-weight: 600; max-width: calc(100% - 32px); }
.toast.err { background: var(--red); }

.chart-box { position: relative; height: 260px; }

/* ---- móvil ---- */
@media (max-width: 820px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar, #nav, .main, .topbar { min-width: 0; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi .k-val { font-size: 19px; }
  .filtros { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 2px; }
  .filtros .chip { white-space: nowrap; }
  .form-row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-row > button { grid-column: 1 / -1; }
  .sidebar { position: static; height: auto; padding: 12px 16px; gap: 10px; }
  #nav { flex-direction: row; overflow-x: auto; gap: 4px; }
  #nav a { white-space: nowrap; flex: none; }
  .sidebar-foot { flex-direction: row; align-items: center; justify-content: space-between; margin: 0; }
  .main { padding-block: 16px; padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px)); }
  .sidebar { padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px)); }
  .two-col { grid-template-columns: 1fr; }
  .dl { grid-template-columns: 1fr; }
  .filtros input[type=search] { min-width: 0; width: 100%; }
}

#nav a:focus-visible, .btn:focus-visible, .chip:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
a { color: var(--primary); }

/* ---- seguimiento ---- */
.contador { margin-left: auto; background: var(--red); color: #fff; font-size: 11px; font-weight: 800; border-radius: 999px; padding: 1px 7px; line-height: 18px; }
.kpi.alerta .k-val { color: var(--violet); }
.dias { display: inline-grid; place-items: center; min-width: 30px; height: 26px; border-radius: 8px; font-weight: 800; font-variant-numeric: tabular-nums; background: var(--gray-soft); color: var(--neutral-badge); }
.dias.dias-medio { background: var(--amber-soft); color: var(--amber); }
.dias.dias-alto { background: var(--red-soft); color: var(--red); }
.acciones { display: inline-flex; gap: 6px; justify-content: flex-end; }
.btn-wa { background: var(--wa-soft); color: var(--wa); border-color: transparent; text-decoration: none; }
a.btn { text-decoration: none; }
.plantilla { display: grid; gap: 6px; margin-bottom: 12px; }
.plantilla textarea { min-height: 70px; }
.link-tel { background: none; border: 0; padding: 0; color: inherit; font: inherit; cursor: copy; text-decoration: underline dotted; }

.link-btn { background: none; border: 0; padding: 0; color: var(--primary); font: inherit; font-size: 13px; font-weight: 600; cursor: pointer; align-self: center; }
.link-btn:hover { text-decoration: underline; }
.form-ok { color: var(--green); font-size: 13px; margin: 0; }
.form-ok:empty { display: none; }
.desglose-saldo { display: flex; flex-direction: column; gap: 2px; margin: 6px 0 4px; padding-top: 6px; border-top: 1px solid var(--line); }
.desglose-saldo b { color: var(--text); }
.kpi.destacado .desglose-saldo { border-color: rgba(255,255,255,.15); }
.kpi.destacado .desglose-saldo b { color: #fff; }
