/* =====================================================================
   1. TEMA (claro por defecto, oscuro por preferencia o elección)
   ===================================================================== */
:root {
  --primary-color: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: rgba(79, 70, 229, 0.12);
  --header-bg: #4f46e5;
  --bg-color: #f3f4f6;
  --card-bg: #ffffff;
  --surface-2: #f3f4f6;
  --text-main: #1f2937;
  --text-muted: #6b7280;
  --border-color: #e5e7eb;

  --income-color: #10b981;
  --expense-color: #ef4444;
  --transfer-color: #3b82f6;
  --warning-color: #f59e0b;

  --radius: 14px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08), 0 4px 12px rgba(0, 0, 0, 0.05);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --primary-color: #818cf8;
    --primary-hover: #6366f1;
    --primary-soft: rgba(129, 140, 248, 0.16);
    --header-bg: #1e1b4b;
    --bg-color: #0f172a;
    --card-bg: #1e293b;
    --surface-2: #273449;
    --text-main: #e5e7eb;
    --text-muted: #94a3b8;
    --border-color: #334155;
    --income-color: #34d399;
    --expense-color: #f87171;
    --transfer-color: #60a5fa;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --primary-color: #818cf8;
  --primary-hover: #6366f1;
  --primary-soft: rgba(129, 140, 248, 0.16);
  --header-bg: #1e1b4b;
  --bg-color: #0f172a;
  --card-bg: #1e293b;
  --surface-2: #273449;
  --text-main: #e5e7eb;
  --text-muted: #94a3b8;
  --border-color: #334155;
  --income-color: #34d399;
  --expense-color: #f87171;
  --transfer-color: #60a5fa;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  color-scheme: dark;
}

/* =====================================================================
   2. BASE
   ===================================================================== */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background-color: var(--bg-color);
  color: var(--text-main);
  line-height: 1.5;
  padding-bottom: calc(96px + env(safe-area-inset-bottom, 0px));
  -webkit-tap-highlight-color: transparent;
}

.hidden { display: none !important; }

h2 {
  font-size: 1.1rem;
  margin-bottom: 0.85rem;
}

.hint {
  color: var(--text-muted);
  font-size: 0.88rem;
  margin: -0.4rem 0 1rem;
}

.empty {
  text-align: center;
  color: var(--text-muted);
  padding: 1.25rem 0.5rem;
  list-style: none;
}

.negative { color: var(--expense-color) !important; }

/* =====================================================================
   3. CONTROLES
   ===================================================================== */
input, select, button {
  font: inherit;
  font-size: 1rem; /* 16px evita el zoom automático en iOS */
}

input, select {
  width: 100%;
  padding: 0.7rem 0.85rem;
  border: 1px solid var(--border-color);
  border-radius: 10px;
  background: var(--card-bg);
  color: var(--text-main);
  outline: none;
  min-height: 46px;
}

input:focus, select:focus {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

input[type="color"] {
  padding: 4px;
  width: 56px;
  flex-shrink: 0;
  cursor: pointer;
}

button {
  cursor: pointer;
  border: none;
  border-radius: 10px;
  padding: 0.7rem 1.1rem;
  font-weight: 600;
  min-height: 46px;
  transition: background-color 0.15s ease, transform 0.1s ease;
}

button:active { transform: scale(0.97); }

.btn-primary { background: var(--primary-color); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }

.btn-secondary { background: var(--surface-2); color: var(--text-main); border: 1px solid var(--border-color); }
.btn-secondary:hover { background: var(--border-color); }

.btn-danger { background: var(--expense-color); color: #fff; }
.btn-danger:hover { filter: brightness(0.92); }

.btn-success { background: var(--income-color); color: #fff; }

.btn-small { min-height: 38px; padding: 0.45rem 0.85rem; font-size: 0.9rem; }

.icon-btn {
  background: transparent;
  color: inherit;
  min-height: 42px;
  min-width: 42px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.15rem;
}

.icon-btn:hover { background: var(--primary-soft); }
.icon-btn.small { min-height: 36px; min-width: 36px; font-size: 0.95rem; }

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}

/* Botones segmentados (tipo de movimiento / tema) */
.segmented {
  display: flex;
  background: var(--surface-2);
  border-radius: 12px;
  padding: 4px;
  gap: 4px;
  margin-bottom: 1rem;
}

.seg-btn {
  flex: 1;
  background: transparent;
  color: var(--text-muted);
  min-height: 40px;
  padding: 0.4rem 0.5rem;
  font-size: 0.92rem;
}

.seg-btn.active {
  background: var(--card-bg);
  color: var(--primary-color);
  box-shadow: var(--shadow);
}

.seg-btn.expense.active { color: var(--expense-color); }
.seg-btn.income.active { color: var(--income-color); }
.seg-btn.transfer.active { color: var(--transfer-color); }

/* Formularios */
.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  min-width: 0;
  grid-column: span 2;
}

.field > span {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
}

.field-wide { grid-column: 1 / -1; }

.form-actions {
  display: flex;
  gap: 0.75rem;
  margin-top: 0.25rem;
}

.form-actions button { flex: 1; }

.inline-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-bottom: 1rem;
}

.inline-form input[type="text"] { flex: 1 1 180px; }
.inline-form select { flex: 0 1 130px; }
.inline-form button { flex: 0 0 auto; }

/* =====================================================================
   4. ENCABEZADO, SELECTOR DE MES Y MENÚ
   ===================================================================== */
.app-header {
  position: sticky;
  top: 0;
  z-index: 500;
  background: var(--header-bg);
  color: #fff;
  padding: calc(0.55rem + env(safe-area-inset-top, 0px)) 0.75rem 0.55rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  box-shadow: var(--shadow);
}

.app-header h1 {
  flex: 1;
  font-size: 1.15rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-header .icon-btn:hover { background: rgba(255, 255, 255, 0.15); }

.menu-toggle {
  flex-direction: column;
  gap: 5px;
  background: rgba(255, 255, 255, 0.12);
}

.menu-toggle span {
  display: block;
  width: 20px;
  height: 2px;
  background: #fff;
  border-radius: 2px;
}

.month-bar {
  position: sticky;
  top: calc(58px + env(safe-area-inset-top, 0px));
  z-index: 400;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  max-width: 1100px;
  margin: 0 auto;
  padding: 0.5rem 0.75rem;
  background: var(--bg-color);
}

.month-bar .icon-btn {
  background: var(--card-bg);
  color: var(--primary-color);
  box-shadow: var(--shadow);
  font-size: 0.9rem;
}

.month-label {
  flex: 1;
  background: var(--card-bg);
  color: var(--text-main);
  box-shadow: var(--shadow);
  font-size: 1rem;
}

.month-label:not(.is-current)::after {
  content: ' · volver a hoy';
  color: var(--primary-color);
  font-size: 0.8rem;
  font-weight: 500;
}

.side-menu {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 280px;
  max-width: 85vw;
  background: var(--card-bg);
  z-index: 900;
  transform: translateX(-105%);
  transition: transform 0.25s ease;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.side-menu.open { transform: translateX(0); box-shadow: 4px 0 24px rgba(0, 0, 0, 0.2); }

.side-menu-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: calc(0.75rem + env(safe-area-inset-top, 0px)) 0.75rem 0.75rem 1.1rem;
  background: var(--header-bg);
  color: #fff;
}

.side-menu-title { font-weight: 700; }
.side-menu-header .icon-btn { font-size: 1.6rem; }

.menu-list {
  list-style: none;
  padding: 0.5rem 0.6rem calc(1rem + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.menu-group {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  padding: 0.85rem 0.75rem 0.3rem;
  font-weight: 700;
}

.menu-item {
  width: 100%;
  text-align: left;
  background: none;
  color: var(--text-main);
  font-weight: 500;
}

.menu-item:hover { background: var(--surface-2); }

.menu-item.active {
  background: var(--primary-soft);
  color: var(--primary-color);
  font-weight: 700;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 800;
}

/* =====================================================================
   5. LAYOUT Y PANELES
   ===================================================================== */
.container {
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 0.5rem 0.75rem 1rem;
}

.view { display: none; flex-direction: column; gap: 1rem; }
.view.active { display: flex; }

.panel {
  background: var(--card-bg);
  padding: 1.1rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-bottom: 0.85rem;
}

.section-header h2 { margin: 0; }

/* Tarjetas de resumen */
.cards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

.card {
  background: var(--card-bg);
  padding: 0.9rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border-top: 4px solid var(--primary-color);
  min-width: 0;
}

.card.income { border-top-color: var(--income-color); }
.card.expense { border-top-color: var(--expense-color); }
.card.total { border-top-color: var(--warning-color); }

.card h3 {
  font-size: 0.72rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 0.25rem;
}

.card p {
  font-size: 1.2rem;
  font-weight: 800;
  overflow-wrap: anywhere;
}

.card.income p { color: var(--income-color); }
.card.expense p { color: var(--expense-color); }

/* Alertas */
.alerts { display: flex; flex-direction: column; gap: 0.5rem; padding: 0.75rem; }

.alert {
  padding: 0.6rem 0.8rem;
  border-radius: 10px;
  font-size: 0.92rem;
}

.alert-warn { background: rgba(245, 158, 11, 0.15); }
.alert-over { background: rgba(239, 68, 68, 0.15); }

/* Gráficos */
.chart-box { position: relative; width: 100%; }
.chart-box-doughnut { height: 300px; max-width: 420px; margin: 0 auto; }
.chart-box-bar { height: 260px; }

/* =====================================================================
   6. LISTAS (historial, fijos, categorías)
   ===================================================================== */
.filters {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.85rem;
}

.filters input { flex: 1; }
.filters select { flex: 0 0 auto; width: auto; }

.item-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.list-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 0.5rem 0.65rem 0.85rem;
  background: var(--surface-2);
  border-radius: 10px;
  border-left: 4px solid transparent;
}

.list-item.tx-income { border-left-color: var(--income-color); }
.list-item.tx-expense { border-left-color: var(--expense-color); }
.list-item.tx-transfer { border-left-color: var(--transfer-color); }
.list-item.inactive { opacity: 0.55; }

.item-main { display: flex; flex-direction: column; min-width: 0; }

.item-title {
  font-weight: 600;
  overflow-wrap: anywhere;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.item-meta {
  font-size: 0.8rem;
  color: var(--text-muted);
  display: block;
}

.item-meta .dot { vertical-align: 0; margin-right: 0.3rem; width: 8px; height: 8px; }

.item-side {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  flex-shrink: 0;
}

.item-actions { display: flex; gap: 2px; flex-shrink: 0; }

.amount { font-weight: 700; white-space: nowrap; }
.amount-income { color: var(--income-color); }
.amount-expense { color: var(--expense-color); }
.amount-transfer { color: var(--transfer-color); }

.dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.badge {
  font-size: 0.68rem;
  font-weight: 600;
  background: var(--primary-soft);
  color: var(--primary-color);
  padding: 1px 7px;
  border-radius: 999px;
}

/* =====================================================================
   7. CUENTAS
   ===================================================================== */
.accounts-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.6rem;
}

.account-card {
  background: var(--surface-2);
  padding: 0.85rem 0.6rem 0.85rem 1rem;
  border-radius: 10px;
  border: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}

.account-card h4 { font-size: 0.92rem; color: var(--text-muted); font-weight: 600; }
.account-card p { font-size: 1.15rem; font-weight: 800; color: var(--primary-color); }

/* =====================================================================
   8. PRESUPUESTOS Y METAS
   ===================================================================== */
.budget-list, .goal-list { display: flex; flex-direction: column; gap: 0.75rem; }

.budget-item, .goal-card {
  background: var(--surface-2);
  border-radius: 10px;
  padding: 0.8rem 0.9rem;
}

.budget-head, .goal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.5rem;
}

.goal-head > div:first-child { display: flex; flex-direction: column; min-width: 0; }

.budget-input { width: 150px; flex-shrink: 0; text-align: right; min-height: 40px; padding: 0.4rem 0.6rem; }

.progress {
  height: 10px;
  background: var(--border-color);
  border-radius: 999px;
  overflow: hidden;
}

.progress.big { height: 14px; }

.progress-bar {
  height: 100%;
  border-radius: 999px;
  transition: width 0.4s ease;
}

.progress-bar.ok { background: var(--primary-color); }
.progress-bar.warn { background: var(--warning-color); }
.progress-bar.over { background: var(--expense-color); }
.progress-bar.done { background: var(--income-color); }

.budget-meta {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-top: 0.35rem;
  flex-wrap: wrap;
}

.budget-meta .warn { color: var(--warning-color); font-weight: 600; }
.budget-meta .over { color: var(--expense-color); font-weight: 600; }

.goal-plan {
  margin-top: 0.5rem;
  font-size: 0.88rem;
  font-weight: 500;
}

/* =====================================================================
   9. AJUSTES
   ===================================================================== */
.danger-zone { border: 1px solid rgba(239, 68, 68, 0.35); }

.app-version {
  text-align: center;
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* =====================================================================
   10. BOTÓN FLOTANTE, MODAL Y NOTIFICACIONES
   ===================================================================== */
.fab {
  position: fixed;
  right: 1.1rem;
  bottom: calc(1.1rem + env(safe-area-inset-bottom, 0px));
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: var(--primary-color);
  color: #fff;
  font-size: 2rem;
  font-weight: 400;
  line-height: 1;
  padding: 0;
  box-shadow: 0 6px 18px rgba(79, 70, 229, 0.45);
  z-index: 600;
}

.fab:hover { background: var(--primary-hover); }

.modal-overlay {
  z-index: 1000;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  padding: 0;
}

.modal-box {
  background: var(--card-bg);
  width: 100%;
  max-width: 440px;
  max-height: 90vh;
  overflow-y: auto;
  border-radius: 18px 18px 0 0;
  padding: 1.25rem 1.1rem calc(1.1rem + env(safe-area-inset-bottom, 0px));
  box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.25);
  animation: slide-up 0.2s ease;
}

@keyframes slide-up {
  from { transform: translateY(30px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.modal-box h3 { font-size: 1.15rem; margin-bottom: 0.4rem; }
#modal-message { color: var(--text-muted); font-size: 0.93rem; margin-bottom: 0.9rem; }
#modal-body { display: flex; flex-direction: column; gap: 0.7rem; }
#modal-body:empty { display: none; }

.radio {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-weight: 500;
  cursor: pointer;
}

.radio input { width: 20px; height: 20px; min-height: 0; accent-color: var(--primary-color); }
.modal-select { margin-left: 1.9rem; width: calc(100% - 1.9rem); }

.modal-actions {
  display: flex;
  gap: 0.75rem;
  margin-top: 1.2rem;
}

.modal-actions button { flex: 1; }

.toast-container {
  position: fixed;
  left: 50%;
  top: calc(70px + env(safe-area-inset-top, 0px));
  transform: translateX(-50%);
  z-index: 1100;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  width: min(92vw, 420px);
  pointer-events: none;
}

.toast {
  background: #1f2937;
  color: #fff;
  padding: 0.75rem 1rem;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  font-size: 0.93rem;
  opacity: 0;
  transform: translateY(-10px);
  transition: opacity 0.25s ease, transform 0.25s ease;
}

.toast.show { opacity: 1; transform: translateY(0); }
.toast-success { background: #047857; }
.toast-error { background: #b91c1c; }
.toast-warn { background: #b45309; }

/* =====================================================================
   11. PANTALLAS MEDIANAS Y GRANDES (tablet / computadora)
   ===================================================================== */
@media (min-width: 640px) {
  .field { grid-column: span 1; }
  .field-wide { grid-column: 1 / -1; }
  .accounts-grid { grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }

  .modal-overlay { align-items: center; padding: 1rem; }
  .modal-box { border-radius: 18px; padding-bottom: 1.25rem; }
}

@media (min-width: 900px) {
  .app-header { padding-left: 1.25rem; padding-right: 1.25rem; }
  .app-header h1 { font-size: 1.3rem; }
  .container { padding: 0.5rem 1.25rem 1.5rem; }
  .month-bar { padding: 0.75rem 1.25rem; max-width: 520px; }

  .cards { grid-template-columns: repeat(4, 1fr); gap: 1rem; }
  .card { padding: 1.1rem; }
  .card p { font-size: 1.45rem; }

  /* Resumen: gráficos lado a lado */
  .view[data-view="resumen"] {
    display: none;
    grid-template-columns: 1fr 1.4fr;
    gap: 1rem;
  }
  .view[data-view="resumen"].active { display: grid; }
  .view[data-view="resumen"] .cards,
  .view[data-view="resumen"] .alerts { grid-column: 1 / -1; }
  .chart-box-doughnut, .chart-box-bar { height: 320px; }

  .form-grid { grid-template-columns: repeat(3, 1fr); }
  .view[data-view="movimiento"] .panel { max-width: 760px; width: 100%; margin: 0 auto; }

  .panel { padding: 1.4rem; }
}
