:root {
  --brand: #4b6952;
  --brand-dark: #3b5441;
  --leaf: #90b87a;
  --cream: #fffeee;
  --page: #f6f6f2;
  --surface: #ffffff;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --line: #e1e0d9;
  --line-strong: #c3c2b7;
  --good: #006300;
  --good-bg: #e6f2e3;
  --bad: #b42323;
  --bad-bg: #fbe9e7;
  --warn-bg: #fff4d6;
  --branch-1: #2a78d6;
  --branch-2: #eb6834;
  --branch-3: #1baf7a;
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--page);
}

* { box-sizing: border-box; }
body { margin: 0; }
.hidden { display: none !important; }
h1, h2, h3 { margin: 0 0 .5rem; line-height: 1.2; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: .95rem; color: var(--ink-2); font-weight: 600; }
a { color: var(--brand); }
.num, td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* ---------- Login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 1rem; background: var(--brand); }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border-radius: 16px; overflow: hidden; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.login-brand { background: var(--brand); padding: 1.5rem 2rem .5rem; text-align: center; }
.login-brand img { width: 200px; max-width: 70%; }
.login-body { padding: 1.5rem; display: grid; gap: .9rem; }
.login-body h1 { text-align: center; font-size: 1.15rem; color: var(--ink-2); }

/* ---------- Topbar ---------- */
.topbar { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 1rem; padding: .4rem 1rem; background: var(--brand); color: var(--cream); }
.brand img { height: 44px; display: block; }
#nav { display: flex; gap: .25rem; flex: 1; overflow-x: auto; }
#nav a { color: var(--cream); text-decoration: none; padding: .5rem .8rem; border-radius: 8px; white-space: nowrap; font-size: .95rem; opacity: .85; }
#nav a:hover { background: rgba(255,255,255,.1); opacity: 1; }
#nav a.active { background: rgba(255,255,255,.18); opacity: 1; font-weight: 600; }
.user { position: relative; }
.branch-switch { width: auto; background: rgba(255,255,255,.12); color: var(--cream); border: 1px solid rgba(255,255,255,.25); font-size: .9rem; padding: .4rem .6rem; }
.branch-switch option { color: var(--ink); }
.check-list { display: flex; flex-wrap: wrap; gap: .4rem 1rem; }
.check-list label { display: inline-flex; flex-direction: row; align-items: center; gap: .35rem; font-weight: 400; color: var(--ink); }
.check-list input { width: auto; }
.user-btn { background: rgba(255,255,255,.12); color: var(--cream); border: 0; border-radius: 8px; padding: .45rem .7rem; cursor: pointer; text-align: right; line-height: 1.15; font: inherit; font-size: .85rem; }
.user-btn small { display: block; opacity: .75; font-size: .75rem; }
.menu { position: absolute; right: 0; top: calc(100% + 6px); background: var(--surface); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.15); min-width: 190px; overflow: hidden; }
.menu button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: .7rem 1rem; font: inherit; cursor: pointer; }
.menu button:hover { background: var(--page); }

main { max-width: 1240px; margin: 0 auto; padding: 1.25rem 1rem 4rem; }
.page-head { display: flex; flex-wrap: wrap; align-items: end; justify-content: space-between; gap: .75rem; margin-bottom: 1rem; }
.page-head p { margin: 0; color: var(--ink-2); }

/* ---------- Controls ---------- */
label { display: grid; gap: .3rem; font-size: .85rem; color: var(--ink-2); font-weight: 500; }
input, select, textarea { font: inherit; font-size: 1rem; color: var(--ink); padding: .55rem .7rem; border: 1px solid var(--line-strong); border-radius: 8px; background: var(--surface); width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--leaf); outline-offset: 0; border-color: var(--brand); }
input.money { text-align: right; font-variant-numeric: tabular-nums; }
textarea { min-height: 2.6rem; resize: vertical; }
.btn { font: inherit; border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink); padding: .55rem 1rem; border-radius: 8px; cursor: pointer; font-weight: 500; white-space: nowrap; text-decoration: none; display: inline-flex; align-items: center; gap: .4rem; }
.btn:hover { background: var(--page); }
.btn.primary { background: var(--brand); color: #fff; border-color: var(--brand); }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { color: var(--bad); border-color: #e6b7b2; }
.btn.small { padding: .3rem .6rem; font-size: .85rem; }
.btn.block { width: 100%; justify-content: center; padding: .75rem; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.filters { display: flex; flex-wrap: wrap; gap: .6rem; align-items: end; margin-bottom: 1rem; }
.filters label { min-width: 150px; }
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button { font: inherit; font-size: .9rem; border: 0; background: none; padding: .5rem .8rem; cursor: pointer; border-right: 1px solid var(--line); display: inline-flex; align-items: center; gap: .4rem; }
.seg button:last-child { border-right: 0; }
.seg button.active { background: var(--brand); color: #fff; }
.dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; flex: none; }
.error { color: var(--bad); margin: 0; min-height: 1.2em; font-size: .9rem; }

/* ---------- Cards ---------- */
.card { background: var(--surface); border-radius: var(--radius); border: 1px solid rgba(11,11,11,.08); padding: 1rem 1.1rem; }
.grid { display: grid; gap: 1rem; }
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(175px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.kpi { background: var(--surface); border-radius: var(--radius); border: 1px solid rgba(11,11,11,.08); padding: .85rem 1rem; }
.kpi .label { font-size: .8rem; color: var(--ink-2); }
.kpi .value { font-size: 1.45rem; font-weight: 650; margin-top: .2rem; letter-spacing: -.01em; }
.kpi .sub { font-size: .78rem; color: var(--muted); margin-top: .15rem; }
.kpi.hero { grid-column: span 2; background: var(--brand); color: #fff; border: 0; }
.kpi.hero .label, .kpi.hero .sub { color: rgba(255,255,255,.8); }
.kpi.hero .value { font-size: 2rem; }
.delta-up { color: var(--good); font-weight: 600; }
.delta-down { color: var(--bad); font-weight: 600; }
.kpi.hero .delta-up, .kpi.hero .delta-down { color: #fff; }
.cols-2 { display: grid; grid-template-columns: 2fr 1fr; gap: 1rem; margin-bottom: 1rem; }
@media (max-width: 900px) { .cols-2 { grid-template-columns: 1fr; } .kpi.hero { grid-column: span 1; } }
.chart-box { position: relative; height: 300px; }
.legend { display: flex; flex-wrap: wrap; gap: 1rem; font-size: .85rem; color: var(--ink-2); margin: .25rem 0 .5rem; }
.legend span { display: inline-flex; align-items: center; gap: .4rem; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th { text-align: left; font-weight: 600; color: var(--ink-2); font-size: .8rem; padding: .55rem .6rem; border-bottom: 1px solid var(--line-strong); white-space: nowrap; }
td { padding: .55rem .6rem; border-bottom: 1px solid var(--line); vertical-align: top; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: #f4f7f2; }
tfoot td { font-weight: 650; border-top: 1px solid var(--line-strong); border-bottom: 0; }
.pill { display: inline-flex; align-items: center; gap: .3rem; padding: .1rem .5rem; border-radius: 999px; font-size: .78rem; font-weight: 600; background: var(--page); color: var(--ink-2); white-space: nowrap; }
.pill.ok { background: var(--good-bg); color: var(--good); }
.pill.bad { background: var(--bad-bg); color: var(--bad); }
.pill.warn { background: var(--warn-bg); color: #7a5300; }
.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; }

/* ---------- Form rendición ---------- */
.shift-form { display: grid; grid-template-columns: 1fr 340px; gap: 1rem; align-items: start; }
@media (max-width: 900px) { .shift-form { grid-template-columns: 1fr; } }
.form-sections { display: grid; gap: 1rem; }
.fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: .8rem; }
.fields .wide { grid-column: 1 / -1; }
.hint { font-size: .78rem; color: var(--muted); font-weight: 400; }
.hint button { font: inherit; color: var(--brand); background: none; border: 0; padding: 0; cursor: pointer; text-decoration: underline; }
.summary { position: sticky; top: 72px; display: grid; gap: .5rem; }
.summary .row { display: flex; justify-content: space-between; gap: 1rem; font-size: .92rem; padding: .25rem 0; }
.summary .row span:last-child { font-variant-numeric: tabular-nums; }
.summary .row.total { font-weight: 650; border-top: 1px solid var(--line); padding-top: .55rem; }
.summary .diff { border-radius: 8px; padding: .8rem; text-align: center; margin-top: .25rem; }
.summary .diff .value { font-size: 1.6rem; font-weight: 700; }
.summary .diff.ok { background: var(--good-bg); color: var(--good); }
.summary .diff.bad { background: var(--bad-bg); color: var(--bad); }
.summary .diff.pending { background: var(--page); color: var(--ink-2); }

/* ---------- Import ---------- */
.dropzone { border: 2px dashed var(--line-strong); border-radius: var(--radius); padding: 2rem; text-align: center; background: var(--surface); cursor: pointer; }
.dropzone.drag { border-color: var(--brand); background: #f4f7f2; }
.warn-list { margin: 0; padding-left: 1rem; font-size: .8rem; color: #7a5300; }
.err-list { margin: 0; padding-left: 1rem; font-size: .8rem; color: var(--bad); }

/* ---------- Modal / toast ---------- */
dialog { border: 0; border-radius: 14px; padding: 0; width: min(720px, calc(100vw - 2rem)); max-height: calc(100vh - 2rem); box-shadow: 0 20px 60px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(20,30,22,.45); }
#modal-body { padding: 1.25rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1rem; flex-wrap: wrap; }
.detail-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: .1rem 1.25rem; }
.detail-grid .row { display: flex; justify-content: space-between; gap: .5rem; padding: .4rem 0; border-bottom: 1px solid var(--line); font-size: .9rem; }
.detail-grid .row span:first-child { color: var(--ink-2); }
.toast { position: fixed; bottom: 1.25rem; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: .7rem 1.1rem; border-radius: 10px; z-index: 100; box-shadow: 0 8px 24px rgba(0,0,0,.25); max-width: calc(100vw - 2rem); }
.toast.bad { background: var(--bad); }

@media (max-width: 700px) {
  .topbar { flex-wrap: wrap; gap: .4rem; }
  .brand img { height: 36px; }
  #nav { order: 3; flex-basis: 100%; }
  .user { margin-left: auto; }
  main { padding: 1rem .75rem 4rem; }
}

/* ---------- Gastos ---------- */
.gasto-line { display: grid; grid-template-columns: minmax(150px, 1fr) minmax(150px, 1.3fr) 130px auto; gap: .5rem; align-items: center; }
@media (max-width: 600px) { .gasto-line { grid-template-columns: 1fr 110px auto; } .gasto-line [data-g=descripcion] { grid-column: 1 / -1; grid-row: 2; } }
.kpi.result-pos .value { color: var(--good); }
.kpi.result-neg .value { color: var(--bad); }
.matrix td, .matrix th { font-size: .85rem; }
.cols-2 { align-items: start; }
.kpi.hero .muted { color: rgba(255,255,255,.75); }
.fields label { align-content: start; }
