:root {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1f232c;
  --border: #2a2f3a;
  --text: #e8eaef;
  --muted: #9aa2b1;
  --accent: #4f8cff;
  --in: #3ecf8e;
  --out: #ff6b6b;
  --warn: #ffb020;
  --radius: 14px;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f6f7f9; --surface: #fff; --surface-2: #f0f2f5; --border: #e2e6ec;
    --text: #1a1d23; --muted: #666f7e;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--out); min-height: 20px; }
.in { color: var(--in); }
.out { color: var(--out); }

/* ── Login ── */
.login { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card { width: 100%; max-width: 360px; text-align: center; }
.login-card h1 { margin: 0 0 4px; font-size: 22px; }

/* ── Estructura ── */
header {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 14px 16px; border-bottom: 1px solid var(--border); background: var(--surface);
  position: sticky; top: 0; z-index: 10;
}
header h1 { font-size: 17px; margin: 0; white-space: nowrap; }
nav { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
nav button {
  background: transparent; border: 0; color: var(--muted); padding: 8px 12px;
  border-radius: 8px; cursor: pointer; font-size: 14px;
}
nav button:hover { background: var(--surface-2); color: var(--text); }
nav button.active { background: var(--accent); color: #fff; }

main { padding: 16px; max-width: 1200px; margin: 0 auto; }
footer { padding: 16px; text-align: center; }

/* ── Tarjetas ── */
.grid { display: grid; gap: 14px; align-items: start; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); margin-bottom: 14px; }

/* Chart.js necesita un contenedor de altura fija; si no, el lienzo crece sin fin. */
.chart { position: relative; height: 240px; width: 100%; }
.chart canvas { position: absolute; inset: 0; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
}
.card h2 { font-size: 15px; margin: 0 0 12px; font-weight: 600; }

.kpi { display: flex; flex-direction: column; gap: 6px; }
.kpi .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.kpi strong { font-size: 24px; font-variant-numeric: tabular-nums; }

.score { display: flex; align-items: baseline; gap: 10px; margin-bottom: 12px; }
.score span { font-size: 40px; font-weight: 700; }
.bars { display: grid; gap: 8px; }
.bar-row { display: grid; grid-template-columns: 1fr 70px; gap: 8px; align-items: center; font-size: 13px; }
.bar { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); }

/* ── Listas y tablas ── */
.list { display: grid; gap: 8px; }
.list-row {
  display: flex; justify-content: space-between; gap: 10px;
  padding: 8px 10px; background: var(--surface-2); border-radius: 8px; font-size: 14px;
}
.table { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 9px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 500; font-size: 12px; text-transform: uppercase; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
tr.pendiente { background: rgba(255, 176, 32, .07); }

.pill { font-size: 11px; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); }
.pill.warn { background: rgba(255,176,32,.18); color: var(--warn); }

/* ── Formularios ── */
.form { display: grid; gap: 10px; }
.form label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
input, select, button {
  font: inherit; padding: 9px 11px; border-radius: 9px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
}
button { background: var(--accent); color: #fff; border: 0; cursor: pointer; font-weight: 500; }
button:hover { filter: brightness(1.1); }
button.ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
button.mini { padding: 4px 9px; font-size: 12px; }
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.toolbar label { font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; }

@media (max-width: 640px) {
  .kpi strong { font-size: 20px; }
  header { gap: 10px; }
}

.pill.danger { background: rgba(255,107,107,.18); color: var(--out); }

/* ── Chat con el agente ── */
.chat-fab {
  position: fixed; right: 20px; bottom: 20px; width: 56px; height: 56px; border-radius: 50%;
  font-size: 24px; padding: 0; box-shadow: 0 6px 20px rgba(0,0,0,.25); z-index: 30;
}
.chat {
  position: fixed; right: 20px; bottom: 20px; width: min(400px, calc(100vw - 32px));
  height: min(600px, calc(100vh - 40px)); display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 12px 40px rgba(0,0,0,.3); z-index: 31; overflow: hidden;
}
.chat-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.chat-mensajes { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 88%; padding: 9px 12px; border-radius: 12px; font-size: 14px; line-height: 1.45; white-space: pre-wrap; }
.msg.yo { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg.bot { align-self: flex-start; background: var(--surface-2); border-bottom-left-radius: 4px; }
.msg.pensando { color: var(--muted); font-style: italic; }
.sugerencias { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; white-space: normal; }
.chat-form { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--border); }
.chat-form input { flex: 1; min-width: 0; }
.chat-nota { margin: 0; padding: 0 12px 10px; }
@media (max-width: 640px) {
  .chat { right: 0; bottom: 0; width: 100vw; height: 100dvh; border-radius: 0; }
}

/* ── Categorías ── */
.form.fila { grid-template-columns: 1fr 90px 140px auto; align-items: end; }
.form.fila .ancho { min-width: 0; }
input.emoji { text-align: center; }
.acciones { display: flex; gap: 6px; justify-content: flex-end; }
.editor { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.editor input, .editor select { padding: 5px 8px; font-size: 13px; }
.aviso {
  display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 12px; margin-bottom: 12px; border-radius: 10px;
  background: rgba(79,140,255,.12); border: 1px solid rgba(79,140,255,.35); font-size: 14px;
}
.pill.sistema { background: rgba(127,127,127,.18); }
@media (max-width: 640px) { .form.fila { grid-template-columns: 1fr 1fr; } }

/* ── Cuentas ── */
details.agregar { margin-top: 12px; }
details.agregar summary { cursor: pointer; color: var(--accent); font-size: 14px; padding: 6px 0; }
details.agregar .form { margin-top: 10px; }
.cuenta-fila { display: grid; gap: 4px; }
.cuenta-fila .linea { display: flex; justify-content: space-between; gap: 8px; align-items: center; flex-wrap: wrap; }
.cuenta-fila .editor { margin-top: 6px; }
.resultado { margin-top: 12px; padding: 12px; border-radius: 10px; font-size: 14px; line-height: 1.5; }
.resultado.si { background: rgba(62,207,142,.12); border: 1px solid rgba(62,207,142,.4); }
.resultado.no { background: rgba(255,107,107,.12); border: 1px solid rgba(255,107,107,.4); }
.resultado ul { margin: 6px 0 0; padding-left: 18px; }
.list-row.ingreso { border-left: 3px solid var(--in); }
.list-row.egreso { border-left: 3px solid var(--out); }

/* ── Ingreso ── */
.login-card button, .login-card input { width: 100%; margin-top: 8px; }
#codigo { text-align: center; font-size: 22px; letter-spacing: 4px; }

a.boleta { text-decoration: none; margin-left: 4px; }
.mini-tabla { width: 100%; font-size: .9em; }
tr.productos > td { background: var(--bg-soft, rgba(127,127,127,.06)); }

/* ── Asistente de ayuda ── */
.ayuda-fab { right: 88px; background: var(--surface-2); color: var(--text, inherit); }
.ayuda-fab .badge { position: absolute; top: -4px; right: -4px; min-width: 20px; height: 20px; border-radius: 10px;
  background: var(--out); color: #fff; font-size: 12px; line-height: 20px; padding: 0 5px; }
#ayuda { right: 20px; }
.aviso { display: block; width: 100%; text-align: left; white-space: normal; padding: 8px 10px; margin-bottom: 6px;
  border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); font-size: 13px; cursor: pointer; }
.aviso.alta { border-left: 4px solid var(--out); }
.aviso.media { border-left: 4px solid var(--warn); }
.aviso.baja { border-left: 4px solid var(--border); }
