:root {
  color-scheme: light dark;
  --bg: #F4EEE1; --card: #FBF7EE; --ink: #1F1B14; --ink2: #5A5245; --ink3: #6E6556; --line: #DDD3BF;
  --in: #008C72; --in-text: #006F5B; --out: #D9542B; --out-text: #AE3C18; --warn: #8A5A00; --hl: #F2B632;
  --hero: #1F1B14; --hero-ink: #FBF7EE; --hero-mute: #C9BFAB; --primary: #1F1B14; --primary-ink: #FBF7EE;
  --num: "Iowan Old Style", "Palatino Linotype", Palatino, "Noto Serif", Georgia, serif;
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --nav-h: 66px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16140F; --card: #211E18; --ink: #F3ECDD; --ink2: #C4BAA6; --ink3: #A89E88; --line: #3A352B;
    --in: #14A98F; --in-text: #54D4B8; --out: #DC683E; --out-text: #F49A78; --warn: #F2B632;
    --hero: #2B261C; --hero-ink: #FBF7EE; --hero-mute: #C9BFAB; --primary: #F2B632; --primary-ink: #1F1B14;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; overflow-x: clip; }
body { background: var(--bg); color: var(--ink); font: 16px/1.4 var(--ui); min-height: 100dvh; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
h1, h2, h3, p { margin: 0; }
button, input, select { font: inherit; color: inherit; }
button { background: none; border: 0; padding: 0; cursor: pointer; text-align: left; }
button:disabled { cursor: default; opacity: .4; }
:focus-visible { outline: 2px solid var(--in); outline-offset: 2px; }
[hidden] { display: none !important; }
small { font-size: 13px; }
.muted { color: var(--ink3); font-weight: 400; }
.small { font-size: 14px; }

/* ---------- Estructura ---------- */
#view { max-width: 520px; margin: 0 auto; padding: max(14px, env(safe-area-inset-top)) 16px calc(var(--nav-h) + 40px + env(safe-area-inset-bottom)); }
.top { display: flex; align-items: center; justify-content: space-between; min-height: 44px; }
.top h1 { font: 500 26px/1.1 var(--num); letter-spacing: -.01em; }
.icon-btn { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 50%; flex: none; }
.icon-btn:hover:not(:disabled) { background: color-mix(in srgb, var(--ink) 8%, transparent); }

#nav { position: fixed; z-index: 10; left: 0; right: 0; bottom: 0; display: grid; grid-template-columns: repeat(5, 1fr); align-items: center; height: calc(var(--nav-h) + env(safe-area-inset-bottom)); padding: 0 6px env(safe-area-inset-bottom); background: var(--card); border-top: 1px solid var(--line); }
.nav-brand { display: none; }
.nav-item { display: flex; flex-direction: column; align-items: center; gap: 3px; color: var(--ink3); padding: 8px 0; min-width: 0; }
.nav-item span { font-size: 11px; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item.on { color: var(--ink); font-weight: 600; }
.nav-add { width: 56px; height: 56px; justify-self: center; margin-top: -22px; display: grid; place-items: center; border-radius: 50%; background: var(--hl); color: #1F1B14; box-shadow: 0 3px 0 #1F1B14; }
.nav-add:active { transform: translateY(2px); box-shadow: 0 1px 0 #1F1B14; }
.nav-add svg { width: 28px; height: 28px; stroke-width: 2.4; }
.nav-add span { display: none; }

.period { display: flex; align-items: center; justify-content: center; gap: 4px; margin: 2px 0 12px; }
.period-label { min-width: 150px; min-height: 44px; text-align: center; font-weight: 600; text-transform: capitalize; }
.period-label:disabled { opacity: 1; }

/* ---------- Tarjeta principal ---------- */
.hero { background: var(--hero); color: var(--hero-ink); border-radius: 6px; padding: 20px 20px 18px; }
.hero-label { color: var(--hero-mute); font-size: 14px; }
.hero-num { font: 500 clamp(44px, 14vw, 60px)/1.1 var(--num); color: var(--hl); font-variant-numeric: lining-nums; overflow-wrap: anywhere; margin: 2px 0 6px; }
.hero-word { font: 500 34px/1.15 var(--num); color: var(--hl); margin: 2px 0 6px; }
.hero.neg .hero-num { color: #FF9C78; }
.hero-text { font-size: 15px; }
.hero-meter { height: 6px; background: rgba(255, 255, 255, .18); border-radius: 3px; margin-top: 14px; overflow: hidden; }
.hero-meter i { display: block; height: 100%; background: var(--hl); border-radius: 3px; }
.hero-foot { font-size: 13px; color: var(--hero-mute); margin-top: 6px; }
.hero-btn { background: var(--hl); color: #1F1B14; margin-top: 12px; }

.trio { display: grid; grid-template-columns: repeat(3, 1fr); border-bottom: 1px solid var(--line); margin-top: 6px; }
.trio.two { grid-template-columns: repeat(2, 1fr); border-top: 1px solid var(--line); margin-bottom: 12px; }
.trio > div { padding: 12px 8px 12px 0; min-width: 0; }
.trio > div + div { border-left: 1px solid var(--line); padding-left: 10px; }
.trio span { display: block; color: var(--ink3); font-size: 13px; }
.trio b, .kpis b, .plan-total b { font: 500 19px/1.2 var(--num); font-variant-numeric: lining-nums; overflow-wrap: anywhere; }
.in { color: var(--in-text); }
.negv { color: var(--out-text); }
.insight { font-size: 14px; color: var(--ink2); padding: 10px 0 0; }
.insight b { font-weight: 600; color: var(--ink); }

.banner { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px; margin: 6px 0; font-size: 14px; background: color-mix(in srgb, var(--hl) 28%, var(--card)); border-radius: 6px; }

/* ---------- Secciones y filas ---------- */
.sec { margin-top: 22px; }
.sec-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; padding-bottom: 6px; border-bottom: 2px solid var(--ink); }
.sec-head h2 { font-size: 14px; font-weight: 700; }
.link, .link-txt { color: var(--ink2); font-size: 14px; text-decoration: underline; text-underline-offset: 3px; }
.link { min-height: 32px; }
.empty { color: var(--ink3); padding: 18px 2px; font-size: 15px; }

.row { display: flex; align-items: center; gap: 10px; width: 100%; min-height: 58px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.row-hit { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.row.rec.done { opacity: .68; }
.ic { width: 30px; flex: none; text-align: center; font-size: 21px; }
.row-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.row-main b { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-main small { color: var(--ink3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
small.late { color: var(--out-text); font-weight: 600; }
.amt { flex: none; font: 500 17px var(--num); font-variant-numeric: lining-nums; text-align: right; white-space: nowrap; }
.amt small { display: block; font: 400 12px var(--ui); color: var(--ink3); }
.status { width: 30px; text-align: center; color: var(--in-text); font-weight: 700; }
.status.skipped { color: var(--ink3); }

.accs { display: grid; grid-template-columns: 1fr 1fr; }
.acc { padding: 10px 8px 10px 0; border-bottom: 1px solid var(--line); min-width: 0; }
.acc span { display: block; font-size: 13px; color: var(--ink2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.acc b { font: 500 18px var(--num); font-variant-numeric: lining-nums; }

.btn { display: inline-flex; align-items: center; justify-content: center; min-height: 44px; padding: 0 18px; border-radius: 6px; font-weight: 600; background: color-mix(in srgb, var(--ink) 9%, transparent); text-align: center; }
.btn.primary { background: var(--primary); color: var(--primary-ink); }
.btn.big { min-height: 52px; font-size: 17px; }
.btn.small { min-height: 38px; padding: 0 13px; font-size: 14px; flex: none; }
.btn.ghost { background: none; box-shadow: inset 0 0 0 1px var(--line); }
.btn.danger { background: var(--out); color: #fff; }
.btn.link { background: none; font-weight: 500; text-decoration: underline; text-underline-offset: 3px; width: 100%; }
.wide-btn { width: 100%; margin: 10px 0 2px; }
.danger-txt { color: var(--out-text); }
.row-end { display: flex; justify-content: flex-end; gap: 8px; }

/* ---------- Presupuestos y metas ---------- */
.budget { display: block; width: 100%; padding: 12px 0; border-bottom: 1px solid var(--line); }
.b-top { display: flex; align-items: center; gap: 10px; }
.b-top b { flex: 1; min-width: 0; font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar { position: relative; display: block; height: 8px; margin: 8px 0 4px; background: color-mix(in srgb, var(--ink) 11%, transparent); border-radius: 4px; }
.bar i { display: block; height: 100%; background: var(--in); border-radius: 4px; }
.bar em { position: absolute; top: -3px; bottom: -3px; width: 2px; margin-left: -1px; background: var(--ink); }
.budget.warn .bar i { background: var(--hl); }
.budget.over .bar i { background: var(--out); }
.b-msg { color: var(--ink3); }
.budget.warn .b-msg { color: var(--warn); font-weight: 600; }
.budget.over .b-msg { color: var(--out-text); font-weight: 600; }
.plan-total { padding: 12px 0; border-bottom: 2px solid var(--ink); }
.plan-total span { display: block; color: var(--ink3); font-size: 13px; }
.plan-total b { font-size: 28px; display: block; }
.plan-total small { color: var(--ink2); }
.goal { padding: 14px 0; border-bottom: 1px solid var(--line); }
.goal-top { display: flex; align-items: center; gap: 10px; width: 100%; }
.goal .row-main small { white-space: normal; }

/* ---------- Controles ---------- */
.seg { display: flex; gap: 2px; padding: 3px; background: color-mix(in srgb, var(--ink) 9%, transparent); border-radius: 8px; flex: 1; }
.seg button { flex: 1; min-height: 38px; padding: 0 6px; border-radius: 6px; text-align: center; font-size: 14px; font-weight: 550; color: var(--ink2); }
.seg button.on { background: var(--card); color: var(--ink); box-shadow: 0 1px 2px rgba(0, 0, 0, .18); }
.seg.wide { margin: 6px 0 12px; }
.chips { display: flex; gap: 6px; overflow-x: auto; padding: 8px 0; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; min-height: 38px; padding: 0 13px; border-radius: 19px; font-size: 14px; box-shadow: inset 0 0 0 1px var(--line); text-align: center; }
.chip.on { background: var(--primary); color: var(--primary-ink); box-shadow: none; font-weight: 600; }
.search, .form input, .form select, .note { width: 100%; min-height: 44px; padding: 8px 12px; background: var(--card); border: 1px solid var(--line); border-radius: 6px; }
.list-total { font-size: 13px; color: var(--ink3); padding: 4px 0 2px; }
.day { margin-top: 12px; }
.day-head { display: flex; justify-content: space-between; font-size: 13px; font-weight: 700; padding-bottom: 4px; border-bottom: 2px solid var(--ink); }
.day-head span:first-child::first-letter { text-transform: uppercase; }

/* ---------- Análisis ---------- */
.kpis { display: grid; grid-template-columns: 1fr 1fr; border-top: 1px solid var(--line); }
.kpis > div { padding: 12px 6px 12px 0; border-bottom: 1px solid var(--line); min-width: 0; }
.kpis > div:nth-child(even) { border-left: 1px solid var(--line); padding-left: 12px; }
.kpis span { display: block; font-size: 13px; color: var(--ink3); }
.kpis b { font-size: 22px; }
.kpis b.sm { font-size: 16px; }
.cbar { display: block; width: 100%; padding: 10px 0; border-bottom: 1px solid var(--line); }
.cbar-top { display: flex; align-items: center; gap: 10px; }
.cbar-top b { flex: 1; min-width: 0; font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cbar-track { display: block; margin: 6px 0 3px; }
.cbar-track i { display: block; height: 8px; min-width: 3px; background: var(--out); border-radius: 0 4px 4px 0; }
.cbar small { color: var(--ink3); }

.trend-read { display: flex; flex-wrap: wrap; gap: 2px 14px; align-items: center; min-height: 46px; padding: 10px 0 8px; font-size: 14px; }
.trend-read b { width: 100%; text-transform: capitalize; }
.k { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; }
.k.in, .tbars .in { background: var(--in); }
.k.out, .tbars .out { background: var(--out); }
.trend { position: relative; display: grid; grid-template-columns: repeat(6, 1fr); height: 190px; border-top: 1px dashed var(--line); padding-top: 18px; }
.tmax { position: absolute; top: 2px; left: 0; font-size: 12px; color: var(--ink3); }
.tcol { display: flex; flex-direction: column; min-width: 0; border-radius: 4px; }
.tcol.on { background: color-mix(in srgb, var(--ink) 7%, transparent); }
.tbars { flex: 1; display: flex; align-items: flex-end; justify-content: center; gap: 2px; border-bottom: 1px solid var(--ink3); min-height: 0; }
.tbars i { width: min(38%, 18px); border-radius: 4px 4px 0 0; }
.tcol small { text-align: center; color: var(--ink3); padding: 5px 0; }
.legend { display: flex; gap: 16px; font-size: 13px; color: var(--ink2); padding-top: 6px; }
.tbl { margin-top: 8px; font-size: 14px; }
.tbl summary { cursor: pointer; color: var(--ink2); min-height: 32px; }
.tbl table { width: 100%; border-collapse: collapse; }
.tbl th, .tbl td { text-align: right; padding: 5px 0; border-bottom: 1px solid var(--line); font-weight: 400; }
.tbl th:first-child { text-align: left; text-transform: capitalize; }
.tbl thead th { font-weight: 700; }

/* ---------- Bienvenida y formularios ---------- */
.onb { max-width: 420px; margin: 0 auto; padding: 26px 0 20px; }
.onb-mark { font: 600 18px var(--num); padding-bottom: 28px; }
.onb h1 { font: 500 36px/1.1 var(--num); margin-bottom: 8px; }
.form { display: flex; flex-direction: column; gap: 12px; margin-top: 16px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; font-weight: 600; }
.form label input, .form label select { font-weight: 400; font-size: 16px; }
.form small { font-weight: 400; }

/* ---------- Hojas ---------- */
dialog { color: var(--ink); background: var(--bg); border: 0; padding: 0; }
dialog::backdrop { background: rgba(10, 8, 4, .55); }
#sheet { position: fixed; inset: auto 0 0 0; width: 100%; max-width: 520px; max-height: 92dvh; margin: 0 auto; padding: 8px 16px calc(24px + env(safe-area-inset-bottom)); border-radius: 12px 12px 0 0; overflow-y: auto; overscroll-behavior: contain; }
#sheet h3 { font-size: 14px; font-weight: 700; margin-top: 18px; padding-bottom: 4px; }
#sheet > .muted { margin-bottom: 4px; }
.sh-head { display: flex; align-items: center; justify-content: space-between; gap: 6px; min-height: 48px; }
.sh-head h2 { font: 500 21px var(--num); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu { margin-top: 12px; }
.menu button { display: flex; justify-content: space-between; align-items: center; width: 100%; min-height: 50px; border-bottom: 1px solid var(--line); }
.menu svg { width: 16px; height: 16px; color: var(--ink3); }
.row > span:last-child > svg { width: 16px; height: 16px; color: var(--ink3); }

#sheet.full { inset: 0; max-width: 520px; height: 100dvh; max-height: none; border-radius: 0; display: flex; flex-direction: column; padding: max(4px, env(safe-area-inset-top)) 12px calc(10px + env(safe-area-inset-bottom)); }
#sheet.full:not([open]) { display: none; }
.draft-head .sp { width: 44px; flex: none; }
.draft-head h2 { flex: 1; text-align: center; font-size: 18px; }
.amt-big { display: block; text-align: center; font: 500 clamp(40px, 13vw, 52px)/1.2 var(--num); font-variant-numeric: lining-nums; overflow-wrap: anywhere; padding: 4px 0 8px; }
.dates { display: flex; gap: 6px; justify-content: center; }
.chip.date { background: none; border: 0; min-width: 0; width: 148px; padding: 0 8px; font-size: 14px; }
.draft-line { display: flex; gap: 8px; align-items: stretch; margin-top: 8px; }
.sel { position: relative; flex: 1 1 0; min-width: 0; display: flex; flex-direction: column; padding: 3px 10px; min-height: 46px; justify-content: center; background: var(--card); border: 1px solid var(--line); border-radius: 6px; }
.sel span { font-size: 11px; color: var(--ink3); }
.sel select { width: 100%; background: none; border: 0; padding: 0; font-size: 15px; }
.sel select option { color: #1F1B14; background: #FBF7EE; }
.note { flex: 1 1 0; min-width: 0; }
.swap { align-self: center; color: var(--ink3); }
.cats { display: grid; grid-auto-flow: column; grid-template-rows: repeat(2, 66px); grid-auto-columns: 74px; gap: 4px; overflow-x: auto; margin-top: 8px; scrollbar-width: none; flex: none; }
.cats::-webkit-scrollbar { display: none; }
.cat { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; border-radius: 6px; font-size: 21px; padding: 2px; }
.cat small { font-size: 11px; line-height: 1.1; text-align: center; color: var(--ink2); overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; line-clamp: 2; -webkit-box-orient: vertical; }
.cat.on { background: var(--primary); }
.cat.on small { color: var(--primary-ink); font-weight: 600; }
.spacer { flex: 1; }
.hint { min-height: 20px; text-align: center; color: var(--out-text); font-size: 14px; font-weight: 600; margin-top: auto; }
.shake { animation: shake .3s; }
@keyframes shake { 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }
.pad { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 52px; flex: none; }
.pad button { display: grid; place-items: center; font: 500 24px var(--num); font-variant-numeric: lining-nums; border-radius: 6px; }
.pad button:active { background: color-mix(in srgb, var(--ink) 12%, transparent); }
.save { width: 100%; min-height: 52px; flex: none; margin-top: 4px; font-size: 17px; }

@media (max-height: 700px) {
  .cats { grid-template-rows: 62px; }
  .pad { grid-auto-rows: 44px; }
  .amt-big { font-size: 38px; padding: 0 0 4px; }
  .sh-head { min-height: 44px; }
  .save { min-height: 46px; }
}

.ask { max-width: min(380px, calc(100% - 32px)); padding: 20px; border-radius: 10px; margin: auto; }
.ask h2 { font: 500 21px/1.2 var(--num); }
.ask p { font-size: 15px; color: var(--ink2); margin-top: 6px; }
.ask .row-end { margin-top: 18px; }

#toast { position: fixed; z-index: 30; left: 50%; bottom: calc(var(--nav-h) + 26px + env(safe-area-inset-bottom)); transform: translateX(-50%); width: max-content; max-width: min(92vw, 440px); display: flex; align-items: center; gap: 14px; padding: 12px 16px; background: var(--hero); color: var(--hero-ink); border-radius: 6px; font-size: 15px; box-shadow: 0 6px 20px rgba(0, 0, 0, .35); }
#toast button { color: var(--hl); font-weight: 700; flex: none; min-height: 28px; }

@media (pointer: coarse) { input, select { font-size: 16px !important; } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; } }

/* ---------- Pantallas anchas ---------- */
@media (min-width: 860px) {
  #nav { top: 0; right: auto; bottom: 0; width: 210px; height: auto; display: flex; flex-direction: column; align-items: stretch; gap: 4px; padding: 22px 14px; border-top: 0; border-right: 1px solid var(--line); }
  .nav-brand { display: block; font: 600 24px var(--num); padding: 0 12px 18px; }
  .nav-item { flex-direction: row; gap: 12px; padding: 11px 12px; border-radius: 6px; order: 2; }
  .nav-item span { font-size: 15px; }
  .nav-item.on { background: color-mix(in srgb, var(--ink) 9%, transparent); }
  .nav-add { order: 1; width: auto; height: 46px; margin: 0 0 12px; border-radius: 6px; display: flex; gap: 8px; justify-content: center; font-weight: 700; box-shadow: none; }
  .nav-add svg { width: 20px; height: 20px; }
  .nav-add span { display: inline; }
  #shell { padding-left: 210px; }
  #view { max-width: 980px; padding: 24px 32px 48px; }
  .home-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 32px; align-items: start; }
  .home-grid .sec:first-child { margin-top: 0; }
  #sheet.full { inset: 0; margin: auto; height: min(760px, 94dvh); border-radius: 12px; }
  #sheet:not(.full) { inset: 0; margin: auto; height: fit-content; border-radius: 12px; }
  #toast { bottom: 26px; }
}
