:root {
  --bg: #f4f5f7;
  --card: #fff;
  --text: #1d2330;
  --muted: #6b7280;
  --line: #e5e7eb;
  --primary: #2f5bd3;
  --primary-d: #2449ad;
  --ok: #1f9d55;
  --warn: #d97706;
  --bad: #dc2626;
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { overflow-x: hidden; }
body { margin: 0; background: var(--bg); -webkit-tap-highlight-color: transparent; }
.hidden { display: none !important; }
h1, h2 { margin: 0; }
h2 { font-size: 1.05rem; }
.muted { color: var(--muted); }
.error { color: var(--bad); min-height: 1.2em; margin: .4rem 0 0; }
.mt { margin-top: 1.2rem; }

/* Topbar */
.topbar {
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
  background: #1d2330; color: #fff; padding: .6rem 1.5rem;
  padding-top: max(.6rem, env(safe-area-inset-top));
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 700; font-size: 1.1rem; }
.brand span { font-weight: 400; opacity: .6; }
nav { display: flex; gap: .2rem; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
nav::-webkit-scrollbar { display: none; }
nav button {
  background: none; border: 0; color: #cbd2e0; padding: .45rem .8rem;
  border-radius: 6px; cursor: pointer; font: inherit; white-space: nowrap;
  display: inline-flex; align-items: center; gap: .4rem;
}
nav svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
nav .lbl-short { display: none; }
nav button:hover { background: #2c3446; color: #fff; }
nav button.active { background: var(--primary); color: #fff; }
.top-actions { margin-left: auto; display: flex; gap: .4rem; }
.top-actions .btn { display: inline-flex; align-items: center; gap: .3rem; }
.ico { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
.topbar .btn.ghost { color: #cbd2e0; border-color: #3a4357; }

main { width: 100%; padding: 1.2rem 1.5rem 2rem; }

/* Cards / layout */
.card { background: var(--card); border-radius: var(--radius); padding: 1rem 1.2rem; box-shadow: 0 1px 2px rgba(0,0,0,.05); }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .8rem; }
.grid2 { display: grid; grid-template-columns: minmax(0, 1.5fr) minmax(0, 1fr); gap: 1.2rem; }
@media (max-width: 900px) { .grid2 { grid-template-columns: 1fr; } }
.toolbar { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; margin-bottom: 1rem; }
.toolbar h2 { margin-right: auto; font-size: 1.25rem; }

/* KPIs */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .8rem; margin-bottom: 1.2rem; }
.kpi { background: var(--card); border-radius: var(--radius); padding: 1rem 1.2rem; border-left: 4px solid var(--primary); }
.kpi .lbl { color: var(--muted); font-size: .85rem; }
.kpi .val { font-size: 1.45rem; font-weight: 700; margin-top: .2rem; font-variant-numeric: tabular-nums; }
.kpi .sub { color: var(--muted); font-size: .8rem; margin-top: .2rem; }
.kpi.pagado { border-color: var(--ok); }
.kpi.pendiente { border-color: var(--warn); }
.kpi.presupuesto { border-color: #8a8f99; }

/* Tables */
table { width: 100%; border-collapse: collapse; }
th, td { padding: .55rem .5rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
th { font-size: .8rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .02em; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tbody tr:hover { background: #fafbfc; }
tfoot td { font-weight: 700; border-top: 2px solid var(--line); border-bottom: 0; }
.table-wrap { overflow-x: auto; }
.empty { color: var(--muted); padding: 1.5rem; text-align: center; }
.acciones { white-space: nowrap; text-align: right; }
.sub-line { color: var(--muted); font-size: .82rem; }

.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: .45rem; vertical-align: middle; }
.badge { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .75rem; background: #eef1f6; color: #3a4357; white-space: nowrap; }
.badge.aceptado { background: #e0ecff; color: #1e4fb8; }
.badge.en_curso { background: #fff3d6; color: #92590a; }
.badge.terminado { background: #dcf5e6; color: #146c3b; }
.badge.descartado { background: #f3f4f6; color: #9ca3af; text-decoration: line-through; }
.badge.doc { background: #f1ecff; color: #5b3fb0; cursor: pointer; }

/* Progress */
.bar { height: 8px; background: #eceef2; border-radius: 4px; overflow: hidden; position: relative; min-width: 80px; }
.bar > div { height: 100%; border-radius: 4px; }
.bar.over > div { background: var(--bad) !important; }

/* Buttons & inputs */
.btn {
  border: 1px solid var(--line); background: #fff; color: var(--text);
  padding: .5rem .9rem; border-radius: 7px; cursor: pointer; font: inherit; font-size: .9rem;
}
.btn:hover { border-color: #c5cad3; }
.btn.primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn.primary:hover { background: var(--primary-d); }
.btn.ghost { background: transparent; }
.btn.small { padding: .3rem .6rem; font-size: .82rem; }
.icon-btn { background: none; border: 0; cursor: pointer; padding: .25rem .35rem; border-radius: 5px; color: var(--muted); font-size: .95rem; }
.icon-btn:hover { background: #eef1f6; color: var(--text); }
.icon-btn.del:hover { color: var(--bad); }
input, select, textarea {
  font: inherit; padding: .45rem .6rem; border: 1px solid #d3d7de; border-radius: 7px; background: #fff; color: var(--text);
}
textarea { resize: vertical; min-height: 60px; }
label.inline { display: inline-flex; align-items: center; gap: .3rem; color: var(--muted); font-size: .85rem; }

/* Modal */
dialog { border: 0; border-radius: 12px; padding: 0; width: min(560px, 94vw); box-shadow: 0 20px 50px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(20, 25, 35, .45); }
#modal-form { padding: 1.3rem 1.4rem; }
#modal-form h2 { margin-bottom: 1rem; font-size: 1.2rem; }
.field { display: flex; flex-direction: column; gap: .25rem; margin-bottom: .8rem; }
.field > span { font-size: .83rem; color: var(--muted); }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: .6rem; }
.dropzone { border: 2px dashed #c7ccd6; border-radius: 9px; padding: 1.2rem; text-align: center; color: var(--muted); cursor: pointer; }
.dropzone.over { border-color: var(--primary); background: #f2f5ff; }
.file-list { font-size: .85rem; margin-top: .4rem; }

/* Doc list inside modals */
.doc-mini { list-style: none; padding: 0; margin: .3rem 0 0; }
.doc-mini li { display: flex; align-items: center; gap: .5rem; padding: .3rem 0; border-bottom: 1px solid var(--line); font-size: .88rem; }
.doc-mini a { color: var(--primary); text-decoration: none; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.login-card { width: min(380px, 100%); display: flex; flex-direction: column; gap: .7rem; padding: 2rem; }
.login-card h1 { font-size: 1.35rem; }

/* Toast */
#toast {
  position: fixed; bottom: 1.2rem; left: 50%; transform: translateX(-50%) translateY(150%);
  background: #1d2330; color: #fff; padding: .6rem 1.1rem; border-radius: 8px; z-index: 50;
  opacity: 0; visibility: hidden; pointer-events: none; transition: transform .25s, opacity .25s, visibility .25s;
}
#toast.show { transform: translateX(-50%) translateY(0); opacity: 1; visibility: visible; }
#toast.err { background: var(--bad); }

/* Chart */
#chart svg { width: 100%; height: auto; display: block; }
#chart .axis { stroke: var(--line); }
#chart text { font-size: 11px; fill: var(--muted); }

.kpi.fondos { border-color: #1f9d55; background: #f6fcf8; }
.kpi.saldo { border-color: #0e7490; background: #f3fafc; }
.nowrap { white-space: nowrap; }

/* ---------- PWA: aviso de instalación ---------- */
.install-btn { background: #1f9d55; color: #fff; border-color: #1f9d55; }
.install-btn:hover { background: #17834a; }
.install-banner {
  position: fixed; z-index: 40; right: 1.2rem; bottom: 1.2rem; width: min(440px, calc(100vw - 1.6rem));
  display: grid; grid-template-columns: auto 1fr; gap: .4rem 1rem; align-items: center;
  background: #fff; border-radius: 14px; padding: 1.1rem 1.2rem;
  border: 2px solid var(--primary);
  box-shadow: 0 18px 45px rgba(29, 35, 48, .28), 0 0 0 6px rgba(47, 91, 211, .12);
  animation: install-in .45s cubic-bezier(.2, .9, .3, 1.2);
}
.install-banner img { border-radius: 12px; grid-row: span 2; align-self: start; }
.install-text strong { display: block; font-size: 1.02rem; }
.install-text p { margin: .25rem 0 0; color: var(--muted); font-size: .88rem; line-height: 1.35; }
.install-text p b { color: var(--text); }
.install-actions { grid-column: 2; display: flex; gap: .5rem; align-items: center; margin-top: .3rem; }
.install-close {
  position: absolute; top: .35rem; right: .5rem; background: none; border: 0; font-size: 1.4rem;
  color: var(--muted); cursor: pointer; line-height: 1;
}
@keyframes install-in { from { transform: translateY(120%); opacity: 0; } to { transform: none; opacity: 1; } }
.offline-bar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 60; background: var(--warn); color: #fff;
  text-align: center; font-size: .85rem; padding: .3rem;
}

/* =====================================================================
   RESPONSIVE
   ===================================================================== */

/* Tablet y portátil pequeño: navegación en una segunda fila a todo el ancho */
@media (max-width: 1100px) {
  .topbar { gap: .5rem 1rem; }
  nav#tabs { order: 3; width: 100%; margin: 0 -.3rem; }
}

/* Tablet: tablas compactas, ocultando columnas secundarias */
@media (max-width: 1024px) and (min-width: 641px) {
  main { padding: 1rem; }
  .hide-sm { display: none; }
  th, td { padding: .5rem .35rem; }
  .card { padding: .9rem 1rem; }
}

/* Móvil */
@media (max-width: 640px) {
  :root { font-size: 14.5px; }
  main { padding: .8rem .7rem calc(78px + env(safe-area-inset-bottom)); }

  /* Barra superior compacta */
  .topbar { padding: .5rem .8rem; padding-top: max(.5rem, env(safe-area-inset-top)); flex-wrap: nowrap; }
  .brand span { display: none; }
  .top-actions .lbl { display: none; }
  .top-actions .btn.small { padding: .45rem .55rem; }
  .top-actions .ico { width: 18px; height: 18px; }
  table.cards td { padding: .16rem 0; }

  /* Navegación inferior tipo app */
  nav#tabs {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; width: auto; margin: 0;
    background: #1d2330; justify-content: space-around; gap: 0; overflow: visible;
    padding: .3rem .2rem calc(.3rem + env(safe-area-inset-bottom));
    box-shadow: 0 -4px 16px rgba(0, 0, 0, .18);
  }
  nav#tabs button {
    flex: 1 1 0; min-width: 0; flex-direction: column; gap: .15rem;
    padding: .35rem .1rem; font-size: .68rem; border-radius: 8px;
  }
  nav#tabs svg { width: 21px; height: 21px; }
  nav#tabs button.active { background: transparent; color: #fff; }
  nav#tabs button.active svg { stroke: #7fa2ff; }
  nav .lbl-long { display: none; }
  nav .lbl-short { display: inline; }

  /* Toolbars: título arriba, filtros y botones ocupando el ancho */
  .toolbar { gap: .5rem; }
  .toolbar h2 { width: 100%; font-size: 1.15rem; margin: 0 0 .1rem; }
  .toolbar > select, .toolbar > .btn, .toolbar > label.inline { flex: 1 1 140px; }
  .toolbar > .btn.primary { flex-basis: 100%; order: 10; padding: .65rem; }
  label.inline input { flex: 1; min-width: 0; }

  /* KPIs en 2 columnas */
  .kpis { grid-template-columns: 1fr 1fr; gap: .55rem; margin-bottom: .9rem; }
  .kpi { padding: .7rem .8rem; border-left-width: 3px; }
  .kpi .lbl { font-size: .75rem; }
  .kpi .val { font-size: 1.12rem; }
  .kpi .sub { font-size: .72rem; }
  .kpi:last-child:nth-child(odd) { grid-column: span 2; }

  .card { padding: .8rem .75rem; border-radius: 9px; }
  .grid2 { gap: .8rem; }

  /* Tablas convertidas en tarjetas */
  table.cards thead { display: none; }
  table.cards, table.cards tbody, table.cards tfoot, table.cards tr, table.cards td { display: block; width: 100%; }
  table.cards tbody tr {
    border: 1px solid var(--line); border-radius: 10px; padding: .55rem .75rem;
    margin-bottom: .55rem; background: #fff;
  }
  table.cards tbody tr:hover { background: #fff; }
  table.cards td {
    display: flex; justify-content: space-between; align-items: center; gap: 1rem;
    border: 0; padding: .22rem 0; text-align: right; white-space: normal; min-height: 1.6em;
  }
  table.cards td::before {
    content: attr(data-label); color: var(--muted); font-size: .72rem; font-weight: 600;
    text-transform: uppercase; letter-spacing: .02em; text-align: left; flex-shrink: 0;
  }
  table.cards td[data-label=""]::before { display: none; }
  table.cards td:first-child {
    display: block; text-align: left; font-weight: 600; font-size: 1rem;
    padding-bottom: .4rem; margin-bottom: .2rem; border-bottom: 1px solid var(--line);
  }
  table.cards td:first-child .sub-line { font-weight: 400; }
  table.cards td:empty, table.cards td.vacia { display: none !important; }
  table.cards td .cv { min-width: 0; text-align: right; overflow-wrap: anywhere; }
  table.cards td .cv:has(.bar) { flex: 1; max-width: 60%; }
  table.cards td .cv .bar { width: 100%; }
  table.cards td.hide-sm { display: flex; }
  table.cards td.acciones {
    justify-content: flex-end; gap: .3rem; border-top: 1px solid var(--line);
    margin-top: .3rem; padding-top: .45rem;
  }
  table.cards td.acciones .icon-btn { font-size: 1.1rem; padding: .35rem .6rem; background: #f3f5f8; }
  table.cards tfoot tr {
    background: #1d2330; color: #fff; border-radius: 10px; padding: .6rem .8rem; margin-top: .3rem;
  }
  table.cards tfoot td { color: #fff; }
  table.cards tfoot td::before { color: #aab3c5; }
  table.cards tfoot td:first-child { border-bottom-color: #3a4357; }

  /* Tablas sin cabecera (últimos pagos) */
  table:not(.cards) td { padding: .5rem .2rem; }

  /* Modales tipo "hoja" desde abajo */
  dialog {
    width: 100%; max-width: 100%; margin: auto 0 0; border-radius: 16px 16px 0 0;
    max-height: 94dvh; overflow-y: auto;
  }
  #modal-form { padding: 1.1rem 1rem calc(1rem + env(safe-area-inset-bottom)); }
  .row { grid-template-columns: 1fr; gap: 0; }
  .modal-actions .btn { flex: 1; padding: .7rem; }
  input, select, textarea { font-size: 16px; } /* evita el zoom automático en iOS */

  /* Elementos flotantes por encima de la navegación inferior */
  #toast { bottom: calc(80px + env(safe-area-inset-bottom)); max-width: 92vw; }
  .install-banner { left: .6rem; right: .6rem; width: auto; bottom: calc(76px + env(safe-area-inset-bottom)); padding: 1rem; }
  .install-banner img { width: 46px; height: 46px; }
  .login-card { padding: 1.5rem 1.2rem; }
}

/* Pantallas muy estrechas */
@media (max-width: 360px) {
  nav#tabs button { font-size: .62rem; }
  .kpi .val { font-size: 1rem; }
}
