:root {
  --bg: #f7f6f3;
  --bg-panel: #ffffff;
  --accent: #8A5A3D;
  --ink: #26241f;
  --muted: #6b6656;
  --line: #e2ddd0;
  --negative: #b3261e;
  --positive: #006300;
  --font-body: system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-mono: ui-monospace, 'SFMono-Regular', Consolas, monospace;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  min-height: 100vh;
  padding: 0 0 60px;
}
a { color: var(--accent); }

/* ---------- Header ---------- */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
  gap: 12px;
}
.header-brand { display: flex; align-items: center; gap: 14px; }
.header-logo-frame { display: flex; align-items: center; flex-shrink: 0; }
.header-logo-frame[hidden] { display: none; }
.header-logo { max-height: 28px; max-width: 240px; object-fit: contain; display: block; }
.logo-badge {
  width: 36px; height: 36px; border-radius: 6px;
  background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 13px; color: #fff; flex-shrink: 0;
}
.header-divider { width: 1px; height: 26px; background: var(--line); }
.header-text { display: flex; flex-direction: column; }
.title { font-size: 1.1rem; font-weight: 700; color: var(--ink); }
.subtitle { margin-top: 2px; font-size: 12px; color: var(--muted); }
.status-line { display: flex; align-items: center; gap: 12px; font-size: 12px; color: var(--muted); }
.status-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent);
  display: inline-block;
}
.status-dot.error { background: var(--negative); }
.reload-btn, .btn, nav button {
  border: 1px solid var(--line);
  background: var(--bg-panel);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 0.85rem;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  cursor: pointer;
}
.reload-btn:hover { border-color: var(--accent); color: var(--accent); }
.header-actions { display: flex; gap: 8px; margin-left: auto; }

/* ---------- Buscador ---------- */
.search-bar { padding: 20px 0 0; position: relative; }
.search-bar input {
  width: 100%; min-width: 0; background: var(--bg-panel); color: var(--ink);
  border: 1px solid var(--line); border-radius: 6px; padding: 0.6rem 0.8rem;
  font-family: var(--font-body); font-size: 14px;
}
.search-bar input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.search-hint { display: block; margin-top: 6px; font-size: 12px; color: var(--muted); }

/* ---------- Content shell ---------- */
.shell { max-width: 1100px; margin: 0 auto; padding: 0 1.5rem; }

.demo-banner {
  font-size: 12px; color: var(--accent); background: #fff8ec;
  border: 1px solid var(--line);
  border-radius: 6px; padding: 6px 14px; display: inline-block; margin: 24px 0 0;
}
.demo-banner[hidden] { display: none; }

/* ---------- Loading / error ---------- */
.state-box {
  margin: 60px auto; max-width: 340px; text-align: center;
  padding: 2rem; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg-panel);
}
.state-box h3 { font-size: 1.05rem; margin-bottom: 10px; color: var(--ink); }
.state-box p { font-size: 13px; color: var(--muted); line-height: 1.6; margin-bottom: 6px; }
.spinner {
  width: 24px; height: 24px; margin: 0 auto 18px;
  border: 2px solid var(--line); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.fallback-area { margin-top: 18px; text-align: left; }
.fallback-area label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.fallback-area textarea {
  width: 100%; height: 100px; background: var(--bg); color: var(--ink);
  border: 1px solid var(--line); border-radius: 6px; padding: 10px;
  font-family: var(--font-mono); font-size: 11px; resize: vertical; margin-top: 6px;
}
.btn.primario, .btn:not(.ghost) { background: var(--accent); color: #fff; border-color: var(--accent); font-weight: 600; }
.btn.primario:hover, .btn:not(.ghost):hover { background: #6E4630; }
.btn { margin-top: 10px; }
.btn.ghost { color: var(--muted); }
.btn.ghost:hover { color: var(--accent); border-color: var(--accent); }

/* ---------- Filters ---------- */
.filters {
  display: flex; flex-wrap: wrap; gap: 14px; align-items: flex-end;
  padding: 1.25rem 0; border-bottom: 1px solid var(--line);
}
.field { display: flex; flex-direction: column; gap: 6px; min-width: 160px; }
.date-range-group { display: flex; gap: 14px; flex-shrink: 0; }
.field label { font-size: 11px; color: var(--muted); }
select, input[type="date"], input[type="text"] {
  background: var(--bg-panel); color: var(--ink);
  border: 1px solid var(--line); border-radius: 6px;
  padding: 0.5rem 0.6rem; font-family: var(--font-body); font-size: 13px;
  min-width: 170px;
}
select:focus, input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.field-hint { font-size: 11px; color: var(--negative); min-height: 14px; display: block; }
.clear-link {
  font-size: 12px; color: var(--muted);
  background: none; border: none; cursor: pointer; text-decoration: underline;
  padding-bottom: 10px;
}
.clear-link:hover { color: var(--accent); }

/* ---------- Tabs ---------- */
.tabbar { display: flex; gap: 0.5rem; margin-top: 1.25rem; flex-wrap: wrap; }
.tab-btn {
  font-family: var(--font-body); font-size: 0.85rem;
  color: var(--ink); background: var(--bg-panel); border: 1px solid var(--line);
  border-radius: 6px; padding: 0.5rem 1rem; cursor: pointer;
}
.tab-btn:hover { border-color: var(--accent); }
.tab-btn[aria-selected="true"] { background: var(--accent); color: #fff; border-color: var(--accent); font-weight: 600; }
.tab-panel[hidden] { display: none; }
.tab-panel { padding-top: 1.25rem; }

/* ---------- Stat cards (KPI) ---------- */
.stats, .budget-stats {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 0.75rem; margin-bottom: 1.25rem;
}
.stat-card {
  background: var(--bg-panel); border: 1px solid var(--line); border-radius: 8px;
  padding: 0.9rem 1rem;
}
.stat-label { font-size: 0.78rem; color: var(--muted); margin-bottom: 6px; }
.stat-value { font-size: 1.6rem; font-weight: 700; color: var(--ink); line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat-sub { margin-top: 6px; font-size: 12px; color: var(--muted); }
.stat-delta { margin-top: 6px; font-size: 0.75rem; font-weight: 600; color: var(--accent); }
.stat-delta[hidden] { display: none; }

/* ---------- Gasto por obra (clic para detalle) ---------- */
.obra-panel { margin-bottom: 1.25rem; }
.obra-row {
  display: flex; align-items: center; gap: 10px; padding: 10px 4px;
  border-bottom: 1px solid var(--line); font-size: 13.5px; cursor: pointer; border-radius: 4px;
}
.obra-row:hover { background: var(--bg); }
.obra-row .arrow { color: var(--muted); font-size: 11px; width: 10px; flex-shrink: 0; transition: transform .15s; }
.obra-row.abierta .arrow { transform: rotate(90deg); }
.obra-row .nombre { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.obra-row .amt { font-variant-numeric: tabular-nums; color: var(--accent); font-weight: 600; font-size: 12.5px; }
.obra-detalle { padding: 6px 4px 12px 20px; margin: 0 0 4px 4px; border-left: 2px solid var(--line); }
.obra-detalle .wbs-row { display: flex; font-size: 12px; padding: 5px 0; color: var(--muted); cursor: pointer; }
.obra-detalle .wbs-row:hover { color: var(--accent); }
.obra-detalle .wbs-row .amt { margin-left: auto; font-variant-numeric: tabular-nums; }
.obra-note { font-size: 12px; color: var(--muted); margin-top: 8px; }
#movimientosFiltroActivo {
  font-size: 12px; color: var(--accent); background: #fff8ec; border: 1px solid var(--line);
  border-radius: 6px; padding: 8px 12px; margin: 0 0 14px;
}
#movimientosFiltroActivo a { color: var(--accent); font-weight: 600; }
#movimientosFiltroActivo[hidden] { display: none; }

/* ---------- Quick zone: ultimos movimientos + top proveedores ---------- */
.quick-grid { display: grid; grid-template-columns: 1.3fr 1fr; gap: 0.75rem; margin-bottom: 0.75rem; }
@media (max-width: 860px) { .quick-grid { grid-template-columns: 1fr; } }

.ledger-row {
  display: grid; grid-template-columns: 1fr auto auto; gap: 12px; align-items: baseline;
  font-size: 12.5px; padding: 9px 0; border-bottom: 1px solid var(--line);
}
.ledger-row:last-child { border-bottom: none; }
.ledger-row .lr-quien { color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ledger-row .lr-tag { font-size: 11px; color: var(--muted); white-space: nowrap; }
.ledger-row .amt { text-align: right; font-variant-numeric: tabular-nums; color: var(--ink); white-space: nowrap; }

.row-list { display: flex; flex-direction: column; }
.row-item { display: flex; align-items: center; gap: 10px; font-size: 13px; padding: 9px 0; border-bottom: 1px solid var(--line); }
.row-item:last-child { border-bottom: none; }
.row-item .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); flex-shrink: 0; }
.row-item .ri-nombre { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-item .amt { margin-left: auto; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 12px; }

/* ---------- Panels ---------- */
.breakdowns { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.75rem; margin-bottom: 0.75rem; }
.breakdowns-2col { grid-template-columns: repeat(2, 1fr); margin-bottom: 1.25rem; }
.panel { background: var(--bg-panel); border: 1px solid var(--line); border-radius: 8px; padding: 1rem 1.1rem; }
.panel h4 { font-size: 0.85rem; font-weight: 600; color: var(--muted); margin-bottom: 16px; }
.empty-note { font-size: 12px; color: var(--muted); font-style: italic; }
@media (max-width: 860px) { .breakdowns, .breakdowns-2col { grid-template-columns: 1fr; } }

/* ---------- Barras horizontales (CSS puro, sin libreria) ---------- */
.explorer-panel { margin-bottom: 0.75rem; }
.explorer-controls { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 18px; margin-bottom: 20px; }
.explorer-note { font-size: 12px; color: var(--muted); padding-bottom: 10px; }
.tendencia-header {
  display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between;
  gap: 16px; margin: 0 0 18px; padding-top: 22px; border-top: 1px solid var(--line);
}

.barra-fila { display: flex; align-items: center; gap: 0.6rem; margin-bottom: 0.55rem; }
.barra-fila:last-child { margin-bottom: 0; }
.barra-etiqueta { flex: 0 0 130px; font-size: 0.8rem; color: var(--ink); text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.barra-pista { flex: 1; background: var(--bg); border-radius: 3px; height: 20px; overflow: hidden; }
.barra-relleno { height: 100%; border-radius: 0 3px 3px 0; min-width: 3px; background: var(--accent); }
.barra-valor { flex: 0 0 auto; font-size: 0.78rem; font-weight: 600; color: var(--ink); font-variant-numeric: tabular-nums; }
.barra-clicable { cursor: pointer; }
.barra-clicable:hover .barra-etiqueta { color: var(--accent); }

/* ---------- Barras verticales (tendencia mensual) ---------- */
.bars-vertical { display: flex; align-items: flex-end; gap: 6px; height: 200px; padding-top: 20px; }
.bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; min-width: 0; }
.bar-col .bar-valor { font-size: 10px; color: var(--muted); margin-bottom: 4px; white-space: nowrap; }
.bar-col .bar-rect { width: 100%; max-width: 34px; border-radius: 3px 3px 0 0; background: #D8C3AE; min-height: 2px; }
.bar-col.actual .bar-rect { background: var(--accent); }
.bar-col.actual .bar-valor { color: var(--accent); font-weight: 600; }
.bar-col .bar-etiqueta { margin-top: 8px; font-size: 10px; color: var(--muted); text-align: center; white-space: nowrap; }
@media (max-width: 860px) { .bars-vertical { overflow-x: auto; justify-content: flex-start; } .bar-col { min-width: 30px; } }

/* ---------- Dona (CSS conic-gradient, sin libreria) ---------- */
.pie-wrap { display: flex; flex-direction: column; align-items: center; gap: 16px; }
.donut-wrap { display: flex; flex-direction: column; align-items: center; gap: 16px; width: 100%; }
.donut-ring { width: 150px; height: 150px; border-radius: 50%; position: relative; flex-shrink: 0; }
.donut-hole {
  position: absolute; inset: 22%; border-radius: 50%; background: var(--bg-panel);
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
}
.donut-hole .pcl-value { font-weight: 700; font-size: 15px; color: var(--ink); }
.donut-hole .pcl-label { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.pie-legend, .donut-leyenda { list-style: none; width: 100%; }
.pie-legend-row {
  display: flex; align-items: center; gap: 8px; font-size: 12px; padding: 6px 4px;
  border-bottom: 1px solid var(--line); cursor: pointer; border-radius: 4px;
}
.pie-legend-row:last-child { border-bottom: none; }
.pie-legend-row:hover { background: var(--bg); }
.pie-legend-row:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.pie-legend-row.active .pl-label { color: var(--accent); }
.pl-swatch { width: 9px; height: 9px; border-radius: 2px; flex-shrink: 0; }
.pl-label { flex: 1; color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pl-amount { color: var(--muted); font-size: 11px; }
.pl-pct { color: var(--accent); font-size: 11px; width: 40px; text-align: right; }

/* ---------- Table ---------- */
.table-wrap { background: var(--bg-panel); border: 1px solid var(--line); border-radius: 8px; overflow: auto; max-height: 560px; }
table { width: 100%; border-collapse: collapse; font-size: 12.5px; min-width: 720px; }
thead th {
  position: sticky; top: 0; background: var(--bg-panel);
  text-align: left; padding: 12px 14px; font-weight: 600;
  font-size: 11px; color: var(--muted); border-bottom: 1px solid var(--line); cursor: pointer; white-space: nowrap;
}
thead th:hover { color: var(--accent); }
thead th.sorted { color: var(--accent); }
tbody td { padding: 11px 14px; border-bottom: 1px solid var(--line); color: var(--ink); white-space: nowrap; }
tbody tr:hover { background: var(--bg); }
tbody td.num { text-align: right; color: var(--ink); font-variant-numeric: tabular-nums; }
.table-footer {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 14px; font-size: 11px; color: var(--muted); border-top: 1px solid var(--line);
}
.section-title { font-size: 11px; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); margin: 4px 0 12px; }
.footer-links { margin-top: 28px; padding-top: 16px; border-top: 1px solid var(--line); font-size: 11px; color: var(--muted); }
.footer-links a { color: var(--muted); text-decoration: underline; }
.footer-links a:hover { color: var(--accent); }

@media (max-width: 860px) {
  .header { padding: 0.9rem 1rem; }
  .shell { padding: 0 1rem; }
  .stats, .breakdowns, .breakdowns-2col, .budget-stats { grid-template-columns: 1fr; }
  .header-brand { gap: 10px; }
  .header-logo { max-height: 20px; }
  .logo-badge { width: 30px; height: 30px; }
  .header-divider { height: 26px; }
  .date-range-group { flex-wrap: wrap; }
  .status-line { flex-wrap: wrap; }
  .header-actions { margin-left: 0; }
  .tabbar { overflow-x: auto; flex-wrap: nowrap; }
}

/* ---------- Login ---------- */
.login-shell { max-width: 340px; margin: 15vh auto 0; padding: 0 1rem; }
.login-box { background: var(--bg-panel); border: 1px solid var(--line); border-radius: 10px; padding: 2rem; }
.login-logo { display: block; height: 60px; width: auto; margin: 0 auto 1.25rem; }
.login-badge-fallback { width: 60px; height: 60px; border-radius: 10px; background: var(--accent); display: flex; align-items: center; justify-content: center; font-weight: 600; font-size: 20px; color: #fff; margin: 0 auto 1.25rem; }
.login-box h2 { font-size: 1.15rem; margin-bottom: 16px; }
.login-box input { width: 100%; margin-bottom: 12px; }
.login-box button { width: 100%; }
.login-error { color: var(--negative); font-size: 12px; margin-top: 8px; min-height: 16px; }
.login-password-row { position: relative; }
.login-password-row input { padding-right: 68px; }
.login-password-row button.login-toggle-pw {
  position: absolute; right: 4px; top: 50%; transform: translateY(calc(-50% - 6px));
  width: auto; background: none; border: none; color: var(--muted); font-size: 11px; cursor: pointer; padding: 4px 8px;
}
.login-toggle-pw:hover { color: var(--accent); }

/* ---------- Impresion / exportar a PDF ---------- */
@media print {
  body { background: #fff; color: #111; padding: 0; }
  .header { border-bottom: 2px solid #ccc; }
  .status-line, .search-bar, .filters, .clear-link, .footer-links, .reload-btn, .tabbar { display: none !important; }
  .tab-panel[hidden] { display: block !important; }
  .panel, .stat-card, .table-wrap { break-inside: avoid; border-color: #ccc; background: #fafafa; }
  .table-wrap { max-height: none; overflow: visible; }
  a[href]::after { content: ""; }
}
