:root {
  color-scheme: light;
  --bg: #f4f3f0;
  --surface: #fcfcfb;
  --surface-2: #f0efeb;
  --border: #e2e0da;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --exec: #2a78d6;
  --commit: #9cc2ef;
  --track: #e9e7e2;
  --good: #1f7a3a;
  --warn: #9a6400;
  --warn-bg: #fdf3dc;
  --crit: #c4302f;
  --crit-bg: #fbe6e5;
  --shadow: 0 1px 2px rgb(0 0 0 / .05), 0 4px 16px rgb(0 0 0 / .04);
  --radius: 12px;
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #33322f;
    --text: #f0efec; --text-2: #c3c2b7; --muted: #92918a;
    --accent: #3987e5; --exec: #3987e5; --commit: #2c5486; --track: #2b2a28;
    --good: #5cc27c; --warn: #e3b04b; --warn-bg: #3a2f17; --crit: #ef7b7a; --crit-bg: #3d1f1f;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #33322f;
  --text: #f0efec; --text-2: #c3c2b7; --muted: #92918a;
  --accent: #3987e5; --exec: #3987e5; --commit: #2c5486; --track: #2b2a28;
  --good: #5cc27c; --warn: #e3b04b; --warn-bg: #3a2f17; --crit: #ef7b7a; --crit-bg: #3d1f1f;
  --shadow: none;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.45; }
button, input, select, textarea { font: inherit; color: inherit; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 8px; background: var(--accent); color: var(--accent-ink); font-weight: 700; }
.brand-title { font-weight: 650; }
.brand-sub { color: var(--muted); font-size: 12px; }
.tabs { display: flex; gap: 2px; background: var(--surface-2); padding: 3px; border-radius: 10px; margin-left: 8px; overflow-x: auto; }
.tabs button { border: 0; background: none; padding: 6px 12px; border-radius: 7px; cursor: pointer; color: var(--text-2); white-space: nowrap; }
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }
.top-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.user { color: var(--muted); font-size: 12px; }

.btn { border: 1px solid var(--border); background: var(--surface); padding: 7px 14px; border-radius: 8px; cursor: pointer; font-weight: 550; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; }
.btn.icon { padding: 4px 9px; }
.btn:disabled { opacity: .55; cursor: progress; }
a { color: var(--accent); }

main { max-width: 1320px; margin: 0 auto; padding: 24px; }
.loading, .empty { color: var(--muted); padding: 40px 0; text-align: center; }
.error-box { background: var(--crit-bg); color: var(--crit); padding: 16px; border-radius: var(--radius); }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 16px; margin: 0; }
.lede { color: var(--text-2); margin: 0 0 20px; }
.section { margin-top: 28px; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.section-head .meta { color: var(--muted); font-size: 12px; }

/* KPI tiles */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.kpi .label { color: var(--text-2); font-size: 12px; }
.kpi .value { font-size: 24px; font-weight: 650; font-variant-numeric: tabular-nums; margin-top: 2px; }
.kpi .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }

/* Area cards */
.areas { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; }
.area-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); cursor: pointer; text-align: left; width: 100%; }
.area-card:hover { border-color: var(--accent); }
.area-card .top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.area-card .name { font-weight: 650; font-size: 15px; }
.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--text-2); white-space: nowrap; }
.area-card .big { font-size: 20px; font-weight: 650; font-variant-numeric: tabular-nums; margin: 10px 0 2px; }
.area-card .row { display: flex; justify-content: space-between; color: var(--text-2); font-size: 12px; font-variant-numeric: tabular-nums; margin-top: 6px; }

/* Meter: ejecutado + comprometido sobre presupuesto */
.meter { position: relative; display: flex; gap: 2px; height: 10px; border-radius: 4px; background: var(--track); overflow: hidden; margin-top: 10px; }
.meter.sm { height: 8px; margin: 0; min-width: 120px; }
.meter > span { height: 100%; }
.meter .exec { background: var(--exec); }
.meter .commit { background: var(--commit); }
.meter.over { outline: 2px solid var(--crit); outline-offset: 1px; }
.legend { display: flex; gap: 16px; color: var(--text-2); font-size: 12px; flex-wrap: wrap; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

.status { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 600; }
.status.crit { color: var(--crit); }
.status.warn { color: var(--warn); }

/* Tables */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { font-size: 12px; font-weight: 600; color: var(--text-2); background: var(--surface-2); position: sticky; top: 0; white-space: nowrap; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
tbody tr:last-child td { border-bottom: 0; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover td { background: var(--surface-2); }
tfoot td { font-weight: 650; background: var(--surface-2); border-top: 1px solid var(--border); border-bottom: 0; }
td .sub { color: var(--muted); font-size: 12px; }
td.wrap { min-width: 220px; }
.orphan td { background: var(--warn-bg); }

.pill { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--text-2); white-space: nowrap; }
.pill.Pagada { background: color-mix(in srgb, var(--good) 15%, transparent); color: var(--good); }
.pill.Aprobada { background: color-mix(in srgb, var(--accent) 15%, transparent); color: var(--accent); }
.pill.Pendiente { background: var(--warn-bg); color: var(--warn); }
.pill.Cancelada { text-decoration: line-through; }
.pill.Recibida { background: color-mix(in srgb, var(--good) 15%, transparent); color: var(--good); }

/* Filters */
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.filters input, .filters select, .grid input, .grid select, .grid textarea {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 7px 10px; min-width: 0;
}
.filters input[type="search"] { min-width: 240px; flex: 1; max-width: 360px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 16px; }
.chip { border: 1px solid var(--border); background: var(--surface); padding: 6px 12px; border-radius: 99px; cursor: pointer; }
.chip[aria-pressed="true"] { background: var(--text); color: var(--surface); border-color: var(--text); }

/* Dialog */
dialog { border: 0; padding: 0; border-radius: 16px; width: min(760px, calc(100vw - 32px)); max-height: calc(100vh - 32px); background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgb(0 0 0 / .3); }
dialog::backdrop { background: rgb(0 0 0 / .35); }
#invoice-form { display: flex; flex-direction: column; max-height: calc(100vh - 32px); }
.dlg-head, .dlg-foot { display: flex; align-items: center; gap: 8px; padding: 14px 20px; border-bottom: 1px solid var(--border); }
.dlg-head h2 { flex: 1; }
.dlg-foot { border-bottom: 0; border-top: 1px solid var(--border); justify-content: flex-end; }
.dlg-foot .meta { margin-right: auto; color: var(--muted); font-size: 12px; }
.dlg-body { padding: 16px 20px; overflow-y: auto; }
.grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-top: 12px; }
.grid label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-2); font-weight: 550; }
.grid label small { font-weight: 400; color: var(--muted); }
.grid .span2 { grid-column: span 2; }
.grid output { padding: 7px 0; font-size: 14px; font-weight: 650; color: var(--text); font-variant-numeric: tabular-nums; }
.drop { display: block; border: 1.5px dashed var(--border); border-radius: 10px; padding: 12px 14px; cursor: pointer; color: var(--text-2); }
.drop:hover { border-color: var(--accent); }
.drop input { display: block; margin-bottom: 6px; }
.attach-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; gap: 6px; flex-wrap: wrap; }
.attach-list li { font-size: 12px; background: var(--surface-2); padding: 3px 10px; border-radius: 99px; }
.impact { margin-top: 14px; padding: 12px 14px; border-radius: 10px; background: var(--surface-2); font-size: 13px; }
.impact.over { background: var(--crit-bg); color: var(--crit); }
.form-error { color: var(--crit); margin: 12px 0 0; }

.tooltip { position: fixed; z-index: 50; pointer-events: none; background: var(--text); color: var(--surface); font-size: 12px; padding: 8px 10px; border-radius: 8px; max-width: 280px; line-height: 1.5; font-variant-numeric: tabular-nums; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--surface); padding: 10px 16px; border-radius: 10px; z-index: 60; }

@media (max-width: 720px) {
  .topbar { padding: 10px 16px; }
  .tabs { margin-left: 0; order: 3; width: 100%; }
  .user { display: none; }
  main { padding: 16px; }
  .grid { grid-template-columns: 1fr 1fr; }
  .filters input[type="search"] { min-width: 0; max-width: none; }
}
