:root {
  --bg: #f4f6f5;
  --panel: #ffffff;
  --texto: #17201e;
  --muted: #64716e;
  --borde: #e2e7e5;
  --acento: #0f766e;
  --acento-suave: #e6f3f1;
  --ingreso: #0f766e;
  --gasto: #c2410c;
  --resultado: #4338ca;
  --peligro: #b91c1c;
  --aviso: #b45309;
  --aviso-bg: #fef6e7;
  --sombra: 0 1px 2px rgba(16, 24, 22, .05), 0 2px 8px rgba(16, 24, 22, .04);
  --radio: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1413; --panel: #171e1c; --texto: #e6ecea; --muted: #93a19d; --borde: #2a3532;
    --acento: #2dd4bf; --acento-suave: #133330; --ingreso: #2dd4bf; --gasto: #fb923c;
    --resultado: #a5b4fc; --peligro: #f87171; --aviso: #fbbf24; --aviso-bg: #2b2414;
    --sombra: none; color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { font-family: Inter, system-ui, -apple-system, "Segoe UI", sans-serif; background: var(--bg); color: var(--texto); font-size: 15px; line-height: 1.45; }
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; letter-spacing: -.01em; }
h1 { font-size: 1.5rem; } h2 { font-size: 1.15rem; } h3 { font-size: .95rem; font-weight: 600; }
.muted { color: var(--muted); }
.flex1 { flex: 1; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }

/* ---------- Controles ---------- */
input, select, textarea {
  font: inherit; color: inherit; background: var(--panel); border: 1px solid var(--borde);
  border-radius: 8px; padding: 8px 10px; width: 100%; min-height: 38px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--acento); outline-offset: -1px; border-color: transparent; }
label { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); font-weight: 500; }
label > input, label > select, label > textarea { color: var(--texto); font-weight: 400; font-size: .95rem; }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--texto); }
label.check input { width: auto; min-height: 0; }

.btn {
  font: inherit; font-weight: 500; border: 1px solid var(--borde); background: var(--panel); color: var(--texto);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; min-height: 38px; display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover { border-color: var(--muted); }
.btn.primario { background: var(--acento); border-color: var(--acento); color: #fff; }
@media (prefers-color-scheme: dark) { .btn.primario { color: #062b27; } }
.btn.primario:hover { filter: brightness(1.08); }
.btn.peligro { color: var(--peligro); }
.btn.enlace { border: none; background: none; padding: 4px 0; color: var(--muted); min-height: 0; }
.btn.enlace:hover { color: var(--texto); }
.btn.icono { padding: 6px 10px; }
.btn.ancho { width: 100%; justify-content: center; }
.btn:disabled { opacity: .5; cursor: default; }

.segmentado { display: inline-flex; background: var(--panel); border: 1px solid var(--borde); border-radius: 9px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.segmentado button { font: inherit; font-size: .88rem; border: none; background: none; color: var(--muted); padding: 5px 12px; border-radius: 6px; cursor: pointer; }
.segmentado button.activo { background: var(--acento-suave); color: var(--acento); font-weight: 600; }

/* ---------- Login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--panel); border: 1px solid var(--borde); box-shadow: var(--sombra); border-radius: 16px; padding: 32px 28px; width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 14px; }
.login-card .logo { margin-bottom: 4px; }
.logo { width: 44px; height: 44px; border-radius: 12px; background: var(--acento); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 1.3rem; }
.logo.pequeño { width: 30px; height: 30px; font-size: .95rem; border-radius: 8px; }
.error { color: var(--peligro); font-size: .88rem; margin: 0; }
.aviso-demo { font-size: .82rem; color: var(--aviso); background: var(--aviso-bg); padding: 10px 12px; border-radius: 8px; margin: 0; }

/* ---------- Estructura ---------- */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.menu { background: var(--panel); border-right: 1px solid var(--borde); padding: 18px 12px; display: flex; flex-direction: column; gap: 2px; position: sticky; top: 0; height: 100vh; }
.marca { display: flex; align-items: center; gap: 10px; font-weight: 700; padding: 0 8px 18px; }
.menu a { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px; color: var(--muted); text-decoration: none; font-weight: 500; }
.menu a svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.menu a:hover { background: var(--bg); color: var(--texto); }
.menu a.activo { background: var(--acento-suave); color: var(--acento); }
.menu-pie { margin-top: auto; display: flex; flex-direction: column; gap: 4px; padding: 8px; font-size: .82rem; }
.usuario { color: var(--muted); overflow: hidden; text-overflow: ellipsis; }
.etiqueta-demo { align-self: flex-start; font-size: .7rem; font-weight: 700; color: var(--aviso); background: var(--aviso-bg); padding: 2px 8px; border-radius: 99px; }

main { padding: 24px 28px 48px; max-width: 1280px; width: 100%; min-width: 0; }
.cabecera { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.cabecera h1 { margin-right: auto; }

.tarjeta { background: var(--panel); border: 1px solid var(--borde); border-radius: var(--radio); box-shadow: var(--sombra); padding: 18px; min-width: 0; }
.tarjeta > h3 { margin-bottom: 12px; }
.tarjeta-cab { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.tarjeta-cab h3 { margin-right: auto; }

/* ---------- Panel ---------- */
.barra-periodo { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 18px; }
.nav-fecha { display: inline-flex; align-items: center; gap: 4px; }
.nav-fecha .etiqueta { font-weight: 600; min-width: 170px; text-align: center; }
.rango-pers { display: inline-flex; gap: 6px; align-items: center; }
.rango-pers input { width: auto; }

.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; margin-bottom: 14px; }
.kpi .etq { font-size: .82rem; color: var(--muted); font-weight: 500; display: flex; align-items: center; gap: 6px; }
.kpi .etq i { width: 8px; height: 8px; border-radius: 2px; display: inline-block; }
.kpi .valor { font-size: 1.55rem; font-weight: 700; margin: 4px 0 2px; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.kpi .delta { font-size: .8rem; color: var(--muted); }
.delta .sube { color: var(--ingreso); font-weight: 600; } .delta .baja { color: var(--gasto); font-weight: 600; }

.rejilla { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 14px; }
.c12 { grid-column: span 12; } .c8 { grid-column: span 8; } .c7 { grid-column: span 7; } .c6 { grid-column: span 6; } .c5 { grid-column: span 5; } .c4 { grid-column: span 4; }
.grafico { position: relative; height: 290px; }
.grafico.bajo { height: 240px; }
.vacio { color: var(--muted); text-align: center; padding: 40px 0; font-size: .9rem; }

.alerta { display: flex; gap: 12px; align-items: center; background: var(--aviso-bg); color: var(--aviso); border-radius: var(--radio); padding: 12px 16px; margin-bottom: 14px; font-size: .9rem; flex-wrap: wrap; }
.alerta strong { color: inherit; }
.alerta .btn { margin-left: auto; }

.mini-tabla { width: 100%; border-collapse: collapse; font-size: .88rem; }
.mini-tabla td, .mini-tabla th { padding: 7px 4px; border-bottom: 1px solid var(--borde); }
.mini-tabla th { text-align: left; font-weight: 500; color: var(--muted); font-size: .8rem; }
.mini-tabla tr:last-child td { border-bottom: none; }
.mini-tabla tfoot td { font-weight: 700; border-top: 2px solid var(--borde); }

/* ---------- Caja ---------- */
.caja { display: grid; grid-template-columns: minmax(0, 360px) minmax(0, 1fr); gap: 14px; align-items: start; }
.linea-pago { display: grid; grid-template-columns: 1fr 150px; align-items: center; gap: 10px; padding: 6px 0; }
.linea-pago span { font-weight: 500; }
.linea-pago input { text-align: right; font-variant-numeric: tabular-nums; font-size: 1.05rem; }
.total-caja { display: flex; justify-content: space-between; align-items: baseline; padding: 14px 0 6px; margin-top: 8px; border-top: 2px solid var(--borde); }
.total-caja strong { font-size: 1.5rem; font-variant-numeric: tabular-nums; }
.aviso-edicion { display: flex; gap: 10px; align-items: flex-start; background: var(--acento-suave); color: var(--texto); border-radius: 8px; padding: 10px 12px; margin-bottom: 12px; font-size: .86rem; }
.aviso-edicion .muted { font-size: .8rem; margin-top: 2px; }
.col-editar { width: 32px; text-align: center; }
.ico-editar { width: 16px; height: 16px; fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; opacity: .55; vertical-align: middle; }
.fila-click:hover .ico-editar { opacity: 1; stroke: var(--acento); }
.fila-click { cursor: pointer; } .fila-click:hover td { background: var(--bg); }
.fila-sel td { background: var(--acento-suave); }

/* ---------- Tablas ---------- */
.tabla-wrap { overflow-x: auto; }
.tabla { width: 100%; border-collapse: collapse; font-size: .9rem; }
.tabla th { text-align: left; font-weight: 500; color: var(--muted); font-size: .8rem; padding: 8px 10px; border-bottom: 1px solid var(--borde); white-space: nowrap; }
.tabla td { padding: 10px; border-bottom: 1px solid var(--borde); }
.tabla tfoot td { font-weight: 700; border-bottom: none; }
.tabla.compacta { font-size: .85rem; }
.tabla.compacta td, .tabla.compacta th { padding: 8px 6px; }
.tabla.compacta th { white-space: normal; }
.chip { font-size: .75rem; padding: 2px 8px; border-radius: 99px; background: var(--bg); color: var(--muted); white-space: nowrap; }
.chip.pend { background: var(--aviso-bg); color: var(--aviso); font-weight: 600; }
.chip.venc { background: color-mix(in srgb, var(--peligro) 14%, transparent); color: var(--peligro); font-weight: 600; }
.filtros { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; align-items: end; }
.filtros label { min-width: 140px; }
.filtros .buscar { flex: 1; min-width: 180px; }

/* ---------- Ajustes ---------- */
.lista-edit { display: flex; flex-direction: column; gap: 6px; }
.lista-edit .fila { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; }
.lista-edit .inactiva input { color: var(--muted); text-decoration: line-through; }
.nueva-fila { display: flex; gap: 8px; margin-top: 10px; }

/* ---------- Diálogo ---------- */
dialog { border: none; border-radius: 16px; padding: 0; width: min(620px, calc(100vw - 24px)); background: var(--panel); color: var(--texto); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(10, 20, 18, .45); }
.dlg header, .dlg footer { display: flex; align-items: center; gap: 8px; padding: 16px 20px; }
.dlg header { border-bottom: 1px solid var(--borde); justify-content: space-between; }
.dlg footer { border-top: 1px solid var(--borde); }
.rejilla-form { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; padding: 18px 20px; max-height: 68vh; overflow-y: auto; }
.col2 { grid-column: span 2; }
.ayuda { margin: -4px 0 0; font-size: .82rem; color: var(--muted); }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--texto); color: var(--panel); padding: 10px 18px; border-radius: 10px; font-size: .9rem; z-index: 50; box-shadow: 0 6px 20px rgba(0,0,0,.2); }
.toast.error { background: var(--peligro); color: #fff; }

/* ---------- Responsive ---------- */
@media (max-width: 1000px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .c8, .c7, .c6, .c5, .c4 { grid-column: span 12; }
  .caja { grid-template-columns: 1fr; }
}
@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .menu { position: fixed; bottom: 0; top: auto; left: 0; right: 0; height: auto; flex-direction: row; justify-content: space-around; padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); border-right: none; border-top: 1px solid var(--borde); z-index: 20; }
  .marca, .menu-pie { display: none; }
  .menu a { flex-direction: column; gap: 2px; font-size: .7rem; padding: 6px 8px; }
  main { padding: 16px 16px 90px; }
  .nav-fecha .etiqueta { min-width: 0; }
  .kpi .valor { font-size: 1.25rem; }
  .rejilla-form { grid-template-columns: 1fr; }
  .col2 { grid-column: span 1; }
  .ocultar-movil { display: none; }
  .toast { bottom: calc(80px + env(safe-area-inset-bottom)); }
}
