/* ================= TOKENS ================= */
:root {
  --bg: #f4f8f7;
  --surface: #ffffff;
  --surface-2: #eaf3f1;
  --sidebar-bg: #0e2523;
  --sidebar-ink: #d7ece9;
  --sidebar-muted: #7fa9a4;
  --sidebar-active: #143634;

  --ink: #10221f;
  --muted: #5c716e;
  --muted-2: #85a09c;
  --border: #dde7e5;
  --border-soft: #eaf1ef;

  --accent: #0FBAB2;
  --accent-ink: #0a8f89;
  --accent-soft: #e3f6f4;
  --accent-2: #C2622E;
  --accent-2-soft: #fbe9df;
  --gold: #D9A521;

  --good: #1E8E5A;
  --warning: #D97706;
  --critical: #D6304A;
  --critical-soft: #fdecea;

  --radius: 14px;
  --radius-sm: 9px;
  --shadow-1: 0 1px 2px rgba(16, 34, 31, .06);
  --shadow-2: 0 10px 28px rgba(16, 34, 31, .10);

  --font-display: 'Manrope', system-ui, sans-serif;
  --font-body: 'IBM Plex Sans', system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1715;
    --surface: #122622;
    --surface-2: #16302b;
    --sidebar-bg: #081513;
    --sidebar-active: #102b28;

    --ink: #e7f3f1;
    --muted: #93b0ab;
    --muted-2: #6f8d88;
    --border: #1e3a35;
    --border-soft: #172b27;

    --accent: #22d1c8;
    --accent-ink: #6fe8e1;
    --accent-soft: #113330;
    --accent-2: #e08350;
    --accent-2-soft: #2c1b12;
    --gold: #eab84a;

    --critical-soft: #391b1c;
    --shadow-1: 0 1px 2px rgba(0, 0, 0, .3);
    --shadow-2: 0 14px 32px rgba(0, 0, 0, .45);
  }
}
/* Elección manual del usuario (botón claro/oscuro) — gana en cualquier dirección */
:root[data-theme="dark"] {
  --bg: #0b1715;
  --surface: #122622;
  --surface-2: #16302b;
  --sidebar-bg: #081513;
  --sidebar-active: #102b28;

  --ink: #e7f3f1;
  --muted: #93b0ab;
  --muted-2: #6f8d88;
  --border: #1e3a35;
  --border-soft: #172b27;

  --accent: #22d1c8;
  --accent-ink: #6fe8e1;
  --accent-soft: #113330;
  --accent-2: #e08350;
  --accent-2-soft: #2c1b12;
  --gold: #eab84a;

  --critical-soft: #391b1c;
  --shadow-1: 0 1px 2px rgba(0, 0, 0, .3);
  --shadow-2: 0 14px 32px rgba(0, 0, 0, .45);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  overflow-x: hidden;
  font-variant-numeric: tabular-nums;
}
h1, h2, h3, .num { font-family: var(--font-display); text-wrap: balance; }
a { color: var(--accent-ink); }

@media (prefers-reduced-motion: no-preference) {
  .fade-up { animation: fadeUp .5s cubic-bezier(.2,.7,.3,1) both; }
}
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ================= GATE ================= */
#gate {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  background:
    radial-gradient(700px 500px at 15% 10%, rgba(15,186,178,.25), transparent 60%),
    radial-gradient(700px 500px at 85% 90%, rgba(194,98,46,.18), transparent 60%),
    var(--sidebar-bg);
}
#gate.hidden { display: none; }
.gate-box {
  background: var(--surface); color: var(--ink);
  padding: 34px 38px; border-radius: var(--radius);
  width: min(360px, 90vw); box-shadow: var(--shadow-2);
  border-top: 4px solid var(--accent-2); text-align: center;
}
.gate-box h1 { font-size: 1.2rem; margin: 0 0 4px; color: var(--accent-ink); font-weight: 800; }
.gate-box p { color: var(--muted); font-size: .85rem; margin: 0 0 18px; }
.gate-box input {
  width: 100%; padding: 11px 13px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 1rem; margin-bottom: 10px; background: var(--bg); color: var(--ink); font-family: var(--font-body);
}
.gate-box button {
  width: 100%; padding: 11px 13px; border: none; border-radius: var(--radius-sm);
  background: var(--accent); color: #04302d; font-weight: 700; font-size: .95rem; cursor: pointer;
  transition: transform .15s ease, background .15s ease;
}
.gate-box button:hover { background: var(--accent-ink); color: #fff; transform: translateY(-1px); }
.gate-err { color: var(--critical); font-size: .8rem; min-height: 18px; margin-top: 8px; }

/* ================= APP SHELL ================= */
#app { display: none; height: 100vh; }
#app.ready { display: grid; grid-template-columns: 232px 1fr; }

.sidebar {
  background: var(--sidebar-bg); color: var(--sidebar-ink);
  display: flex; flex-direction: column; padding: 20px 14px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 6px 8px 22px; }
.brand .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--accent-2); flex: none; box-shadow: 0 0 0 4px rgba(194,98,46,.18); }
.brand h1 { font-size: 1.05rem; margin: 0; color: #fff; font-weight: 800; letter-spacing: .01em; }
.brand span { display: block; font-size: .7rem; color: var(--sidebar-muted); margin-top: 1px; }

nav.nav { display: flex; flex-direction: column; gap: 3px; flex: 1; }
nav.nav button {
  display: flex; align-items: center; gap: 10px;
  border: none; background: none; color: var(--sidebar-muted);
  font-family: var(--font-body); font-size: .87rem; font-weight: 500;
  padding: 10px 12px; border-radius: var(--radius-sm); cursor: pointer; text-align: left;
  transition: background .15s ease, color .15s ease;
}
nav.nav button svg { width: 17px; height: 17px; flex: none; opacity: .85; }
nav.nav button:hover { background: rgba(255,255,255,.05); color: #cfe9e5; }
nav.nav button.active { background: var(--sidebar-active); color: #fff; }
nav.nav button.active svg { opacity: 1; color: var(--accent); }

.theme-seg { display: flex; background: rgba(255,255,255,.06); border-radius: 999px; padding: 3px; margin-bottom: 14px; }
.theme-seg button {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 5px;
  border: none; background: none; color: var(--sidebar-muted); font-size: .72rem; font-weight: 500;
  padding: 7px 8px; border-radius: 999px; cursor: pointer; font-family: var(--font-body);
  transition: background .15s ease, color .15s ease;
}
.theme-seg button svg { width: 13px; height: 13px; }
.theme-seg button.active { background: var(--sidebar-active); color: #fff; }
.theme-seg button:not(.active):hover { color: #cfe9e5; }

.sidebar-foot { border-top: 1px solid rgba(255,255,255,.08); padding-top: 14px; font-size: .72rem; color: var(--sidebar-muted); }
.sidebar-foot button {
  display: flex; align-items: center; gap: 6px; background: none; border: none; color: var(--sidebar-ink);
  font-size: .76rem; cursor: pointer; padding: 6px 4px; margin-top: 4px; font-family: var(--font-body);
}
.sidebar-foot button:hover { color: var(--accent); }
.spin-icon { display: inline-block; transition: transform .5s; }
.spin-icon.spinning { animation: spin 1s linear infinite; }

.main { display: flex; flex-direction: column; min-width: 0; height: 100vh; overflow: hidden; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 14px 26px; border-bottom: 1px solid var(--border); background: var(--surface); flex: none;
  flex-wrap: wrap;
}
.topbar h2 { margin: 0; font-size: 1.05rem; font-weight: 800; }
.topbar-right { display: flex; align-items: center; gap: 12px; }

.emerg-chip {
  display: none; align-items: center; gap: 7px; background: var(--critical-soft); color: var(--critical);
  border: 1px solid rgba(214,48,74,.25); padding: 6px 12px; border-radius: 999px; font-size: .78rem; font-weight: 600;
  cursor: pointer; transition: transform .15s ease;
}
.emerg-chip:hover { transform: translateY(-1px); }
.emerg-chip.show { display: inline-flex; }
.emerg-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--critical); }
@media (prefers-reduced-motion: no-preference) {
  .emerg-dot { animation: pulse 1.6s ease-in-out infinite; }
}
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(214,48,74,.5);} 50% { box-shadow: 0 0 0 5px rgba(214,48,74,0);} }

.month-control { display: flex; align-items: center; gap: 8px; font-size: .82rem; color: var(--muted); }
.month-control select {
  padding: 7px 10px; border-radius: var(--radius-sm); border: 1px solid var(--border);
  font-size: .85rem; background: var(--surface); color: var(--ink); font-family: var(--font-body);
}

.views { flex: 1; overflow-y: auto; padding: 22px 26px 30px; }
.view { display: none; }
.view.active { display: block; }

h3.block-title {
  font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted);
  margin: 0 0 12px; font-family: var(--font-body); font-weight: 600;
}

/* ---- KPI ---- */
.kpis { display: grid; grid-template-columns: repeat(5, 1fr); gap: 14px; margin-bottom: 22px; }
@media (max-width: 1000px) { .kpis { grid-template-columns: repeat(2, 1fr); } }
.kpi {
  background: var(--surface); border-radius: var(--radius); padding: 16px 18px;
  box-shadow: var(--shadow-1); border: 1px solid var(--border-soft);
  border-top: 3px solid var(--gold); transition: transform .18s ease, box-shadow .18s ease;
}
.kpi:hover { transform: translateY(-2px); box-shadow: var(--shadow-2); }
.kpi .label { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; font-weight: 600; }
.kpi .value { font-family: var(--font-display); font-size: 1.9rem; font-weight: 800; margin: 5px 0 3px; }
.kpi .delta { font-size: .78rem; color: var(--muted-2); }
.kpi .delta.up { color: var(--good); }
.kpi .delta.down { color: var(--critical); }

/* ---- Cards ---- */
.chart-card {
  background: var(--surface); border-radius: var(--radius); padding: 18px 20px 12px;
  box-shadow: var(--shadow-1); border: 1px solid var(--border-soft);
}
.chart-card canvas { max-height: 320px; }
.chart-card h4 { margin: 0 0 10px; font-size: .86rem; font-weight: 600; color: var(--ink); }
.chart-card h4 span { color: var(--muted); font-weight: 400; font-size: .78rem; }

.grid-2 { display: grid; grid-template-columns: 1.5fr 1fr; gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.grid-2-5 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
@media (max-width: 1100px) { .grid-2, .grid-3, .grid-2-5 { grid-template-columns: 1fr; } }

.chart-foot { display: flex; justify-content: flex-end; margin-top: 4px; }
.link-btn { background: none; border: none; color: var(--accent-ink); font-size: .78rem; cursor: pointer; padding: 0; font-family: var(--font-body); }
.link-btn:hover { text-decoration: underline; }
table.data-table { width: 100%; border-collapse: collapse; font-size: .82rem; margin-top: 8px; }
table.data-table th, table.data-table td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
table.data-table th { color: var(--muted); font-weight: 600; }

/* ---- donut ---- */
.donut-wrap { position: relative; height: 210px; display: flex; align-items: center; justify-content: center; }
.donut-wrap canvas { max-height: 210px; }
.donut-center { position: absolute; text-align: center; pointer-events: none; }
.donut-center .n { font-family: var(--font-display); font-size: 1.5rem; font-weight: 800; display: block; }
.donut-center .l { font-size: .68rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.donut-legend { display: flex; flex-wrap: wrap; gap: 8px 14px; margin-top: 12px; font-size: .78rem; }
.donut-legend .item { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.donut-legend .sw { width: 9px; height: 9px; border-radius: 3px; flex: none; }

/* ---- period seg control ---- */
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; }
.seg button { border: none; background: var(--surface); padding: 7px 15px; font-size: .82rem; cursor: pointer; color: var(--muted); font-family: var(--font-body); transition: background .15s, color .15s; }
.seg button.active { background: var(--accent); color: #04302d; font-weight: 600; }
.view-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; flex-wrap: wrap; gap: 10px; }

/* ---- CRM ---- */
.tabs { display: flex; gap: 6px; margin-bottom: 0; flex-wrap: wrap; }
.tabs button {
  padding: 9px 16px; border-radius: var(--radius-sm) var(--radius-sm) 0 0; border: 1px solid var(--border); border-bottom: none;
  background: var(--surface-2); color: var(--muted); font-size: .84rem; cursor: pointer; font-family: var(--font-body);
  transition: background .15s, color .15s;
}
.tabs button.active { background: var(--surface); color: var(--ink); font-weight: 600; border-color: var(--accent); }
.crm-card { background: var(--surface); border-radius: 0 var(--radius) var(--radius) var(--radius); padding: 16px; box-shadow: var(--shadow-1); border: 1px solid var(--border-soft); overflow-x: auto; }

.filters { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.filters label { font-size: .8rem; color: var(--muted); margin-right: 4px; }
.filters select, .filters input[type=text] {
  padding: 8px 11px; border-radius: var(--radius-sm); border: 1px solid var(--border);
  font-size: .85rem; background: var(--bg); color: var(--ink); font-family: var(--font-body);
}
.filters input[type=text] { min-width: 240px; }

.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: .72rem; font-weight: 600; }
.badge.frio { background: var(--accent-2-soft); color: #a8531a; opacity:.55 }
.badge.tibio { background: var(--accent-2-soft); color: #a8531a; opacity: .8 }
.badge.caliente { background: var(--accent-2-soft); color: #a8531a; }
.badge.convertido { background: var(--accent-soft); color: var(--good); }
.badge.estado { background: var(--surface-2); color: var(--muted); }
.badge.urgente { background: var(--critical-soft); color: var(--critical); }

.crm-table { width: 100%; border-collapse: collapse; font-size: .84rem; }
.crm-table th {
  text-align: left; padding: 10px; font-size: .71rem; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); border-bottom: 2px solid var(--border); position: sticky; top: 0; background: var(--surface);
}
.crm-table td { padding: 10px; border-bottom: 1px solid var(--border-soft); vertical-align: top; }
.crm-table tr { transition: background .1s; }
.crm-table tr:hover td { background: var(--surface-2); }
.crm-wrap { max-height: calc(100vh - 340px); min-height: 260px; overflow-y: auto; border: 1px solid var(--border); border-radius: var(--radius-sm); }

.pagination { display: flex; justify-content: space-between; align-items: center; margin-top: 10px; font-size: .8rem; color: var(--muted); }
.pagination button { border: 1px solid var(--border); background: var(--surface); border-radius: 7px; padding: 6px 11px; cursor: pointer; font-family: var(--font-body); }
.pagination button:disabled { opacity: .4; cursor: default; }

.empty-msg, .error-msg { padding: 30px; text-align: center; color: var(--muted); font-size: .9rem; }
.error-msg { color: var(--critical); }

/* scrollbar (webkit) */
.views::-webkit-scrollbar, .crm-wrap::-webkit-scrollbar { width: 9px; }
.views::-webkit-scrollbar-thumb, .crm-wrap::-webkit-scrollbar-thumb { background: var(--border); border-radius: 6px; }

@media (max-width: 860px) {
  #app.ready { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .sidebar { flex-direction: row; align-items: center; padding: 12px 16px; overflow-x: auto; }
  .brand { padding: 0 14px 0 0; }
  nav.nav { flex-direction: row; }
  .sidebar-foot { display: none; }
}
