/* Utilitarios — móvil + botón instalar (estilo CRM) */
:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --primary: #176B87;
  --primary-hover: #12566D;
  --border: #2D3A35;
  --text-muted: #91A09B;
  --bg: #0E1412;
  --surface: #151D1A;
}

html {
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

body {
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

/* Botón instalar — visible siempre (como CRM topbar) */
.btn-install-pwa {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  min-width: 36px;
  padding: 0;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}

.btn-install-pwa:hover,
.btn-install-pwa:active,
.btn-install-pwa.pwa-install-ready {
  color: var(--primary);
  border-color: var(--primary);
}

.btn-install-pwa:disabled {
  opacity: 0.45;
  pointer-events: none;
}

body.is-standalone .btn-install-pwa,
body.is-standalone .install-bar {
  display: none !important;
}

/* Barra opcional en índice */
.install-bar {
  width: 100%;
  max-width: 460px;
  display: flex;
  justify-content: flex-end;
  margin-bottom: 4px;
}

.install-bar .btn-install-pwa {
  width: auto;
  padding: 0 12px;
  gap: 6px;
  font-size: 0.82rem;
}

.nav {
  padding-top: calc(10px + var(--safe-top));
  padding-left: calc(12px + var(--safe-left));
  padding-right: calc(12px + var(--safe-right));
  gap: 8px;
  min-height: 52px;
}

.nav-title {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 1rem;
}

.nav a {
  min-width: 40px;
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.nav-actions {
  flex-shrink: 0;
  gap: 6px;
}

.filters-row-search {
  flex: 1;
  min-width: 140px;
}

.filters-row-dates {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.filters-row-day {
  flex: 1 1 auto;
  min-width: 0;
}

.filters-row-day .fdate {
  flex: 1;
  min-width: 0;
  width: 100%;
}

.filter-panel {
  flex-shrink: 0;
  position: relative;
  z-index: 2;
  background: var(--bg);
}

.nav-btn {
  min-height: 36px;
  -webkit-tap-highlight-color: transparent;
}

.fab {
  bottom: calc(16px + var(--safe-bottom));
  right: calc(16px + var(--safe-right));
  width: 54px;
  height: 54px;
}

.overlay .modal {
  padding-bottom: calc(24px + var(--safe-bottom));
  max-height: calc(92vh - var(--safe-top));
}

@media (max-width: 640px) {
  body.page-gastos {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
  }

  body.page-gastos .top-panel {
    flex-shrink: 0;
    min-height: 0;
    overflow: visible;
  }

  body.page-gastos .top-panel:not(.expanded) .dashboard-body {
    display: none;
  }

  body.page-gastos .panel-toggle {
    display: flex;
    width: calc(100% - 24px);
    margin: 2px 12px 6px;
    padding: 8px 10px;
    min-height: 40px;
  }

  body.page-gastos .top-panel.expanded .panel-toggle i {
    transform: rotate(180deg);
  }

  body.page-gastos .filter-toolbar {
    display: flex;
  }

  body.page-gastos .filter-panel--collapsed .filter-drawer {
    display: none;
  }

  body.page-gastos .filter-panel--expanded .filter-toggle-btn {
    border-color: var(--primary);
    color: #fff;
    background: #176B8722;
  }

  body.page-gastos #filter-summary {
    display: none;
  }

  body.page-gastos .list-head {
    display: none;
  }

  body.page-gastos .filter-panel {
    flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
  }

  body.page-gastos .list-scroll {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
  }

  .income-section.open .income-body {
    max-height: min(28vh, 200px);
  }

  .income-item {
    gap: 8px;
    align-items: flex-start;
  }

  .income-item .ii-info {
    min-width: 0;
  }

  .income-item .ii-info .ii-desc {
    word-break: break-word;
  }

  .income-item .ii-amt {
    flex-shrink: 0;
    white-space: nowrap;
  }

  .income-header {
    display: grid;
    grid-template-columns: 28px minmax(0, 1fr) auto;
    gap: 8px;
    align-items: center;
  }

  .income-title {
    min-width: 0;
  }

  body.page-gastos .summary {
    margin: 0 12px 6px !important;
    padding: 12px 14px !important;
    border-radius: 12px !important;
  }

  body.page-gastos .summary h4 {
    font-size: 0.72rem !important;
    margin-bottom: 2px !important;
  }

  body.page-gastos .summary .amt {
    font-size: 1.35rem !important;
  }

  body.page-gastos .summary .progress {
    margin: 8px 0 !important;
    height: 5px !important;
  }

  body.page-gastos .summary .row {
    margin-top: 8px !important;
    gap: 6px !important;
  }

  body.page-gastos .summary .chip {
    padding: 6px 8px !important;
    border-radius: 8px !important;
  }

  body.page-gastos .summary .chip label {
    font-size: 0.68rem !important;
  }

  body.page-gastos .summary .chip span {
    font-size: 0.82rem !important;
  }

  body.page-gastos .income-section {
    margin: 0 12px 6px !important;
    border-radius: 12px !important;
  }

  body.page-gastos .income-header {
    padding: 10px 12px !important;
  }

  body.page-gastos .income-title h4 {
    font-size: 0.82rem !important;
  }

  body.page-gastos .income-title .income-total {
    font-size: 0.75rem !important;
  }

  body.page-gastos .filter-summary {
    margin: 0 12px 6px !important;
    padding: 10px 12px !important;
  }

  body.page-gastos .filters {
    padding: 4px 12px 6px !important;
    gap: 6px !important;
  }

  body.page-gastos .filters-row-search {
    width: 100%;
    flex: 1 1 100% !important;
    min-width: 0 !important;
    order: 3;
  }

  body.page-gastos .filters-row-dates {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    order: 1;
  }

  body.page-gastos .filters-row-dates .fsel {
    min-width: 0;
    min-height: 38px;
    font-size: 0.85rem;
    width: 100%;
  }

  body.page-gastos .filters-row-day {
    grid-column: 1 / -1;
    width: 100%;
    display: flex;
    order: 2;
  }

  body.page-gastos .filters-row-day .fdate {
    min-height: 38px;
    font-size: 16px;
    flex: 1;
  }

  body.page-gastos .cat-chips-scroll {
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    -webkit-overflow-scrolling: touch;
    gap: 6px !important;
    padding: 2px 12px 6px !important;
    scrollbar-width: none;
    min-height: 38px;
    align-items: center;
  }

  .cat-chips-scroll::-webkit-scrollbar {
    display: none;
  }

  .cat-chips-scroll .fc {
    flex-shrink: 0;
    white-space: nowrap;
    min-height: 36px;
    display: inline-flex;
    align-items: center;
  }

  body.page-gastos .list-scroll {
    padding: 2px 12px calc(72px + var(--safe-bottom)) !important;
    position: relative;
    z-index: 1;
  }

  .item {
    padding: 12px 14px !important;
    gap: 10px !important;
  }

  .item .val {
    font-size: 0.88rem !important;
    flex-shrink: 0;
  }

  body.page-gastos .search-inp {
    min-height: 38px;
    font-size: 16px !important; /* evita zoom en iOS */
    padding-top: 7px;
    padding-bottom: 7px;
  }

  body.page-gastos .filter-toggle-btn {
    min-height: 38px;
    padding: 6px 10px;
  }

  .nav-btn#btn-cats .lbl {
    display: none;
  }

  .nav-btn#btn-cats::after {
    content: '☰';
    font-size: 1rem;
  }

  .nav-btn#btn-clients .lbl {
    display: none;
  }

  .nav-btn#btn-clients::before {
    content: '👥 ';
  }

  body.page-home {
    padding: calc(12px + var(--safe-top)) 12px calc(16px + var(--safe-bottom));
  }

  body.page-home h1 {
    font-size: 1.4rem;
  }

  body.page-home .cards {
    max-width: none;
  }

  body.page-home .card {
    padding: 16px;
  }

  .fr {
    flex-direction: column !important;
    gap: 0 !important;
  }

  .list {
    padding: 0 12px calc(80px + var(--safe-bottom)) !important;
  }

  .card {
    margin-left: 12px !important;
    margin-right: 12px !important;
  }

  .total,
  .save {
    margin-left: 12px !important;
    margin-right: 12px !important;
    width: calc(100% - 24px) !important;
  }
}

@media (max-width: 380px) {
  .nav-title {
    font-size: 0.92rem;
  }

  .income-header .btn-add-income {
    padding: 6px 8px;
    font-size: 0.75rem;
  }
}
