/* ============================================================
   Mi Plata — Plantilla de Organización Financiera (PWA)
   ============================================================ */

@font-face {
  font-family: 'Inter';
  src: url('assets/fonts/inter-var.woff2') format('woff2');
  font-weight: 100 900;
  font-display: swap;
}
@font-face {
  font-family: 'Poppins';
  src: url('assets/fonts/poppins-600.woff2') format('woff2');
  font-weight: 600;
  font-display: swap;
}
@font-face {
  font-family: 'Poppins';
  src: url('assets/fonts/poppins-700.woff2') format('woff2');
  font-weight: 700;
  font-display: swap;
}

:root {
  --green: #0d9668;
  --green-dark: #0b7d57;
  --green-pale: #e6f5ef;
  --amber: #f59e0b;
  --amber-dark: #92400e;
  --amber-bg: #fef3e2;
  --red: #dc2626;
  --red-bg: #fee2e2;

  --ink: #111827;
  --ink-2: #374151;
  --gray: #6b7280;
  --gray-3: #9ca3af;
  --line: #e5e7eb;
  --bg: #f6f7f9;
  --surface: #ffffff;

  --radius: 16px;
  --radius-sm: 11px;
  --shadow: 0 1px 2px rgba(16,24,40,.05), 0 4px 14px rgba(16,24,40,.06);

  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-head: 'Poppins', var(--font);

  --tabbar-h: 62px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

/* Cualquier display explícito (flex/grid/block) le gana al atributo [hidden].
   Esta regla global evita ese bug en todo el app. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

body { font-size: 15px; line-height: 1.45; }

h1, h2, h3, strong { font-family: var(--font-head); }

button, input, select { font-family: inherit; font-size: inherit; color: inherit; }

/* ============================ LAYOUT ============================ */

.app { min-height: 100dvh; display: flex; flex-direction: column; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--green);
  color: #fff;
  padding: calc(env(safe-area-inset-top, 0px) + 14px) 16px 14px;
}
.topbar-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.topbar-title { margin: 0; font-size: 21px; font-weight: 700; letter-spacing: -0.01em; }

.period-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: rgba(255,255,255,.16);
  border: 0;
  color: #fff;
  font-size: 13.5px;
  font-weight: 600;
  padding: 7px 11px;
  border-radius: 999px;
  cursor: pointer;
}
.period-btn:active { background: rgba(255,255,255,.26); }

.main {
  flex: 1;
  padding: 16px 16px calc(var(--tabbar-h) + var(--safe-b) + 84px);
}

.view { display: none; }
.view.is-active { display: block; animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ============================ CARDS ============================ */

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 14px;
}
.card-title { margin: 0 0 12px; font-size: 15.5px; font-weight: 700; }
.card-eyebrow {
  display: block;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .02em;
  text-transform: uppercase;
  color: var(--gray);
  margin-bottom: 6px;
}
.card-note { display: block; margin: 10px 0 0; font-size: 12.5px; color: var(--gray); line-height: 1.5; }
.card-note--warn {
  background: var(--amber-bg);
  color: var(--amber-dark);
  padding: 11px 12px;
  border-radius: var(--radius-sm);
  margin: 0 0 14px;
}
.card--alert { background: var(--amber-bg); box-shadow: none; border: 1px solid #fde3bd; }
.card--alert .card-eyebrow { color: var(--amber-dark); }

/* ============================ KPIs ============================ */

.kpi-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 14px;
}
.kpi {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 13px 14px;
  box-shadow: var(--shadow);
  border-top: 3px solid var(--line);
  min-width: 0;
}
.kpi--in { border-top-color: var(--green); }
.kpi--out { border-top-color: var(--amber); }
.kpi--bal { border-top-color: var(--ink); }
.kpi--debt { border-top-color: var(--red); }

.kpi-label { display: block; font-size: 12px; color: var(--gray); font-weight: 600; }
.kpi-value {
  display: block;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-top: 3px;
  overflow-wrap: anywhere;
}
.kpi--bal .kpi-value.is-neg { color: var(--red); }
.kpi-sub { display: block; font-size: 11.5px; color: var(--gray-3); margin-top: 2px; }

.fuga-cat { display: block; font-size: 18px; font-weight: 700; color: var(--amber-dark); }
.fuga-val { display: block; font-size: 13px; color: var(--amber-dark); opacity: .85; margin-top: 2px; }

/* ============================ DONUT ============================ */

.donut-wrap { display: flex; align-items: center; gap: 14px; }
.donut { flex: 0 0 132px; width: 132px; height: 132px; position: relative; }
.donut svg { display: block; width: 100%; height: 100%; }
.donut-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  pointer-events: none;
}
.donut-center small { font-size: 10.5px; color: var(--gray); font-weight: 600; }
.donut-center strong { font-size: 15px; letter-spacing: -.02em; }

.legend { list-style: none; margin: 0; padding: 0; flex: 1; min-width: 0; }
.legend li {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  padding: 2.5px 0;
  min-width: 0;
}
.legend .dot { width: 9px; height: 9px; border-radius: 3px; flex: 0 0 9px; }
.legend .lg-name { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--ink-2); }
.legend .lg-pct { font-weight: 600; font-variant-numeric: tabular-nums; color: var(--gray); }

/* ============================ QUINCENA ============================ */

.quincena { display: flex; flex-direction: column; gap: 14px; }
.q-block { }
.q-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; }
.q-name { font-size: 13px; font-weight: 700; }
.q-bal { font-size: 12.5px; color: var(--gray); font-variant-numeric: tabular-nums; }
.q-bars { display: flex; flex-direction: column; gap: 5px; }
.q-bar { display: flex; align-items: center; gap: 8px; font-size: 11.5px; }
.q-bar .q-tag { width: 42px; color: var(--gray); flex: 0 0 42px; }
/* track y fill son <span>: sin display:block, width/height se ignoran */
.q-track { display: block; flex: 1; height: 9px; background: var(--bg); border-radius: 999px; overflow: hidden; }
.q-fill { display: block; height: 100%; min-width: 2px; border-radius: 999px; transition: width .35s ease; }
.q-fill--in { background: var(--green); }
.q-fill--out { background: var(--amber); }
.q-val { font-variant-numeric: tabular-nums; color: var(--ink-2); min-width: 62px; text-align: right; }

/* ============================ MOVIMIENTOS ============================ */

.seg {
  display: flex;
  background: #eceef1;
  padding: 3px;
  border-radius: 999px;
  margin-bottom: 14px;
}
.seg-btn {
  flex: 1;
  border: 0;
  background: transparent;
  padding: 8px 4px;
  border-radius: 999px;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--gray);
  cursor: pointer;
}
.seg-btn.is-active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(16,24,40,.1); }

.mov-day { margin-bottom: 14px; }
.mov-day-head {
  font-size: 11.5px;
  font-weight: 700;
  color: var(--gray);
  text-transform: uppercase;
  letter-spacing: .03em;
  padding: 0 4px 6px;
  display: flex;
  justify-content: space-between;
}
.mov-items { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }

.mov {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 11px 14px;
  width: 100%;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
  border-bottom: 1px solid var(--line);
}
.mov:last-child { border-bottom: 0; }
.mov:active { background: var(--bg); }
.mov-ico {
  width: 34px; height: 34px;
  flex: 0 0 34px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  font-size: 15px;
  background: var(--green-pale);
}
.mov-body { flex: 1; min-width: 0; }
.mov-desc { display: block; font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mov-meta { display: block; font-size: 11.5px; color: var(--gray); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mov-val { font-size: 14.5px; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.mov-val.is-in { color: var(--green); }

/* ============================ DEUDAS ============================ */

.card--deudatotal { background: var(--ink); color: #fff; }
.card--deudatotal .card-eyebrow { color: rgba(255,255,255,.6); }
.card--deudatotal .card-note { color: rgba(255,255,255,.55); }
.deuda-total { display: block; font-size: 27px; font-weight: 700; letter-spacing: -.02em; }

.deuda-item {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
  margin-bottom: 10px;
  width: 100%;
  border: 0;
  text-align: left;
  cursor: pointer;
  display: block;
}
.deuda-item:active { background: #fafbfc; }
.deuda-top { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.deuda-name { font-size: 14.5px; font-weight: 700; }
.deuda-saldo { font-size: 14.5px; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.deuda-sub { display: block; font-size: 12px; color: var(--gray); margin-top: 2px; }
.deuda-track { display: block; height: 7px; background: var(--bg); border-radius: 999px; overflow: hidden; margin-top: 9px; }
.deuda-fill { display: block; height: 100%; background: var(--green); border-radius: 999px; }
.deuda-done { color: var(--green); font-weight: 700; }

/* ============================ AJUSTES ============================ */

.cat-list { list-style: none; margin: 0 0 12px; padding: 0; }
.cat-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  font-size: 13.5px;
}
.cat-list li:last-child { border-bottom: 0; }
.add-row { display: flex; gap: 8px; }
.add-row input { flex: 1; min-width: 0; }

.footer-note { text-align: center; font-size: 11.5px; color: var(--gray-3); margin: 22px 0 0; }

/* ============================ FORM ELEMENTS ============================ */

input[type="text"], input[type="number"], input[type="date"], select {
  width: 100%;
  padding: 11px 12px;
  border: 1.5px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 15px;
  appearance: none;
  -webkit-appearance: none;
}
input:focus, select:focus { outline: none; border-color: var(--green); }

select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none'%3E%3Cpath d='M6 9L12 15L18 9' stroke='%236b7280' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 11px center;
  padding-right: 34px;
}

.field { display: block; margin-bottom: 13px; }
.field-label { display: block; font-size: 12.5px; font-weight: 600; color: var(--ink-2); margin-bottom: 5px; }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; min-width: 0; }

.money-input { position: relative; }
.money-prefix {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--gray);
  font-weight: 600;
  pointer-events: none;
}
.money-input input { padding-left: 26px; font-weight: 700; font-size: 17px; font-variant-numeric: tabular-nums; }

.tipo-toggle { display: flex; gap: 8px; margin-bottom: 15px; }
.tipo-opt { flex: 1; }
.tipo-opt input { position: absolute; opacity: 0; pointer-events: none; }
.tipo-opt span {
  display: block;
  text-align: center;
  padding: 10px;
  border: 1.5px solid var(--line);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  color: var(--gray);
  cursor: pointer;
}
.tipo-opt input:checked + span { border-color: var(--green); background: var(--green-pale); color: var(--green-dark); }

/* ============================ BUTTONS ============================ */

.btn {
  border: 0;
  border-radius: var(--radius-sm);
  padding: 12px 18px;
  font-family: var(--font-head);
  font-size: 14.5px;
  font-weight: 600;
  cursor: pointer;
  transition: transform .12s ease, opacity .12s ease;
}
.btn:active { transform: scale(.985); }
.btn-primary { background: var(--green); color: #fff; }
.btn-ghost { background: var(--bg); color: var(--ink-2); border: 1.5px solid var(--line); }
.btn-danger { background: var(--red-bg); color: var(--red); }
.btn-block { display: block; width: 100%; }
.btn-sm { padding: 9px 14px; font-size: 13.5px; }
.btn-row { display: flex; gap: 8px; margin-bottom: 8px; }
.btn-row .btn { flex: 1; }

.icon-btn {
  border: 0;
  background: transparent;
  color: var(--gray);
  padding: 6px;
  border-radius: 9px;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.icon-btn:active { background: var(--bg); }

.fab {
  position: fixed;
  right: 18px;
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px);
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 0;
  background: var(--green);
  color: #fff;
  box-shadow: 0 6px 18px rgba(13,150,104,.4);
  display: grid;
  place-items: center;
  cursor: pointer;
  z-index: 30;
}
.fab:active { transform: scale(.94); }

/* ============================ TABBAR ============================ */

.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 40;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b);
}
.tab {
  flex: 1;
  border: 0;
  background: none;
  padding: 9px 2px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  color: var(--gray-3);
  font-size: 10.5px;
  font-weight: 600;
  cursor: pointer;
}
.tab.is-active { color: var(--green); }

/* ============================ SHEETS ============================ */

.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(17,24,39,.45);
  z-index: 50;
  animation: fadeIn .18s ease;
}
.backdrop[hidden] { display: none !important; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 60;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  max-height: 92dvh;
  display: flex;
  flex-direction: column;
  animation: slideUp .24s cubic-bezier(.22,1,.36,1);
  padding-bottom: var(--safe-b);
}
.sheet[hidden] { display: none !important; }
@keyframes slideUp { from { transform: translateY(100%); } to { transform: none; } }

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 8px;
  flex: 0 0 auto;
}
.sheet-head h2 { margin: 0; font-size: 17px; font-weight: 700; }
.sheet-body { padding: 8px 16px 18px; overflow-y: auto; }
.sheet-actions { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.sheet-actions .btn-block { flex: 1; }

.year-row { display: flex; align-items: center; justify-content: center; gap: 22px; margin-bottom: 14px; }
.year-row strong { font-size: 17px; }
.month-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.month-btn {
  border: 1.5px solid var(--line);
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 11px 4px;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--ink-2);
  cursor: pointer;
}
.month-btn.is-active { background: var(--green); border-color: var(--green); color: #fff; }

/* ============================ MISC ============================ */

.empty {
  text-align: center;
  color: var(--gray);
  font-size: 13.5px;
  padding: 26px 16px;
  line-height: 1.6;
}
.empty[hidden] { display: none !important; }

.install-bar {
  position: fixed;
  left: 12px; right: 12px;
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 12px);
  z-index: 45;
  background: var(--ink);
  color: #fff;
  border-radius: var(--radius-sm);
  padding: 11px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 12.5px;
  box-shadow: 0 8px 24px rgba(16,24,40,.22);
}
/* [hidden] must beat display:flex — this bit the GLP-1 app before */
.install-bar[hidden] { display: none !important; }
.install-bar span { flex: 1; }
.install-actions { display: flex; align-items: center; gap: 4px; }
.install-bar .icon-btn { color: rgba(255,255,255,.65); }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 78px);
  transform: translateX(-50%);
  z-index: 70;
  background: var(--ink);
  color: #fff;
  font-size: 13px;
  font-weight: 500;
  padding: 10px 16px;
  border-radius: 999px;
  box-shadow: 0 6px 20px rgba(16,24,40,.25);
  animation: toastIn .2s ease;
  max-width: calc(100vw - 40px);
  text-align: center;
}
.toast[hidden] { display: none !important; }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ============================ DESKTOP ============================ */

@media (min-width: 640px) {
  .app { max-width: 520px; margin: 0 auto; box-shadow: 0 0 0 1px var(--line); background: var(--bg); min-height: 100dvh; }
  .tabbar, .install-bar, .fab { max-width: 520px; margin-left: auto; margin-right: auto; }
  .tabbar { left: 50%; transform: translateX(-50%); width: 520px; }
  .fab { left: 50%; margin-left: 190px; right: auto; }
  .sheet { max-width: 520px; margin: 0 auto; }
  .install-bar { left: 50%; transform: translateX(-50%); width: 496px; }
}
