:root {
  --bg: #f5f5f7;
  --card: #ffffff;
  --text: #1d1d1f;
  --text-soft: #4a4a4d; /* lighter body text — KPI values, table content, expense names */
  --muted: #86868b;
  --border: #e5e5ea;
  --accent: #0071e3;
  --accent-soft: #e8f1fc;
  /* Elegant, low-saturation hover palette. Used for row/button/link
     hover states so the dashboard reads calm instead of "Apple system blue". */
  --hover-bg: #f4f4f6;
  --hover-border: #c9c9ce;
  --hover-text: #4a5568;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.06);
  --radius: 14px;
  --radius-sm: 8px;

  --cat-savings: #8dbad6;
  --cat-setup: #9bc2e7;
  --cat-home: #8ea9db;
  --cat-studies: #abb9d4;
  --cat-enjoy: #b9f5c4;
  --cat-losses: #fd9a9a;
  --cat-fixed: #f8ccad;
  --cat-cashout: #fef2cb;

  --green: #34c759;
  --red: #ff453a;
}

/* Always force light theme — user requested */
html {
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Segoe UI",
    Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px 32px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  backdrop-filter: saturate(180%) blur(20px);
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

/* Título + fecha apilados, alineados con el nombre/icono (no en una fila aparte). */
.topbar-title {
  display: flex;
  flex-direction: column;
  gap: 1px;
  line-height: 1.15;
  min-width: 0;
}

.topbar-title #generated-at {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- Selector de panel (Finance ⇄ Health) ---------- */

.page-switcher {
  position: relative;
  min-width: 0;
}

.switcher-btn {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  padding: 3px 7px;
  margin: -3px -7px;
  border-radius: 9px;
  cursor: pointer;
  transition: background 0.12s ease;
}

.switcher-btn:hover {
  background: var(--hover-bg);
}

.switcher-name {
  display: flex;
  align-items: center;
  gap: 5px;
}

.switcher-caret {
  flex-shrink: 0;
  color: var(--muted);
  transition: transform 0.15s ease;
}

.switcher-btn[aria-expanded="true"] .switcher-caret {
  transform: rotate(180deg);
}

.switcher-menu {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  min-width: 210px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 6px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
  z-index: 50;
}

.switcher-menu[hidden] {
  display: none;
}

.switcher-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  text-decoration: none !important;
}

.switcher-item:hover {
  background: var(--hover-bg);
  color: var(--text);
}

.switcher-item.is-current {
  background: var(--hover-bg);
}

.switcher-item svg {
  flex-shrink: 0;
}

.switcher-item.is-current::after {
  content: "✓";
  margin-left: auto;
  color: var(--muted);
  font-size: 13px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ---------- Back to Monitor ---------- */

.btn-back {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  margin-right: 2px;
  border: none;
  background: none;
  color: var(--muted);
  text-decoration: none !important;
  border-radius: 6px;
  transition: color 0.12s ease, background 0.12s ease;
}

.btn-back:hover {
  color: var(--text);
  background: var(--hover-bg);
}

.btn-back svg {
  flex-shrink: 0;
}

/* ---------- GitHub-style buttons ---------- */

.btn-sync {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 32px;
  padding: 0 14px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  text-decoration: none !important;
  background-image: none !important;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.btn-sync:hover {
  background: var(--hover-bg);
  border-color: var(--hover-border);
  color: var(--text);
}

.btn-sync:active {
  background: var(--border);
}

.btn-sync svg {
  flex-shrink: 0;
  opacity: 0.75;
}

.btn-sync.btn-icon {
  padding: 0;
  width: 32px;
}

.sync-icon-mobile { display: none; }

.topbar h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1;
  display: flex;
  align-items: center;
}

.logo {
  flex-shrink: 0;
  display: block;
  width: 24px;
  height: 24px;
}

.muted {
  color: var(--muted);
  font-size: 12px;
}

.muted.small {
  font-size: 12px;
  font-weight: 400;
}

.btn-ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
  font-family: inherit;
}

.btn-ghost:hover {
  background: var(--hover-bg);
  border-color: var(--hover-border);
  color: var(--text);
}

.btn-ghost.small {
  font-size: 12px;
  padding: 4px 10px;
}

select,
input[type="search"],
input[type="date"] {
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s ease;
}

select:focus,
input:focus {
  border-color: var(--hover-border);
  box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.04);
}

body > section,
body > footer {
  max-width: 1320px;
  margin: 0 auto 24px;
  padding-left: 32px;
  padding-right: 32px;
}

body > section.filters {
  margin-top: 24px;
}

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px 24px;
}

.filters.card {
  padding: 16px 20px;
}

.kpi-grid {
  background: transparent;
  box-shadow: none;
  padding: 0;
}

.resumen-block {
  max-width: 1320px;
  margin: 24px auto 24px;
  padding: 0 32px;
}

.resumen-head {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 12px;
  padding: 0 4px;
}

.resumen-head h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.resumen-block .kpi-grid {
  padding: 0;
}

.filter-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 16px;
}

.filter-row + .filter-row {
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

.filter-spacer {
  flex: 1 1 0;
  min-width: 0;
}

.filter-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 0;
}

.filter-group label {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 500;
}

.filter-group-full {
  flex: 1 1 100%;
}

.filter-row > .btn-ghost {
  /* keep Reset aligned to the bottom of the inputs, not their labels */
  align-self: flex-end;
  height: 32px;
  display: inline-flex;
  align-items: center;
  white-space: nowrap;
}

.filters.card select,
.filters.card input[type="date"] {
  height: 32px;
  min-width: 160px;
}

.chips-row {
  width: 100%;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 4px 0;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 14px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  border: 1.5px solid transparent;
  transition: opacity 0.15s ease, transform 0.05s ease, box-shadow 0.15s ease;
  color: #1d1d1f;
  letter-spacing: 0.01em;
}

.chip:not(.off) {
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
  border-color: rgba(0, 0, 0, 0.08);
}

.chip.off {
  opacity: 0.5;
  background: var(--card) !important;
  color: var(--muted);
  border-color: var(--border);
  font-weight: 500;
}

.chip:hover:not(.off) {
  transform: translateY(-1px);
}

.chip.off:hover {
  opacity: 0.75;
}

.chip:active {
  transform: scale(0.97);
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 16px;
  padding-left: 32px;
  padding-right: 32px;
}

.kpi {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  border-left: 4px solid transparent;
}

.kpi.income { border-left-color: var(--green); }
.kpi.expense { border-left-color: var(--red); }
.kpi.net { border-left-color: var(--accent); }
.kpi.fixed { border-left-color: var(--cat-fixed); }

.kpi-label {
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 500;
}

.kpi-value {
  color: var(--text-soft);
  font-size: clamp(16px, 1.7vw, 24px);
  font-weight: 600;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.kpi-value.positive { color: var(--green); }
.kpi-value.negative { color: var(--red); }

.kpi-sub {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.section-card {
  padding: 0;
  overflow: hidden;
  /* margin-left/right come from `body > section` (auto -> centered). */
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 18px 24px 12px;
  border-bottom: 1px solid var(--border);
}

.section-head h2 {
  margin: 0;
  font-size: 19px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.section-period {
  font-size: 14px;
  font-weight: 400;
  color: var(--muted);
  letter-spacing: 0;
}

.charts-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  padding: 20px 24px;
}

.charts-grid.one {
  grid-template-columns: 1fr;
}

@media (max-width: 900px) {
  .charts-grid {
    grid-template-columns: 1fr;
  }
}

.chart-box {
  min-height: 280px;
  display: flex;
  flex-direction: column;
}

.chart-box h3 {
  margin: 0 0 12px;
  font-size: 13px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 500;
}

.chart-box canvas {
  max-height: 280px;
  flex: 1;
}

.chart-box--donut {
  height: 300px;
  max-height: 300px;
}
.chart-box--donut canvas {
  max-height: 300px;
}

.chart-box--bars {
  min-height: 320px;
}

/* Donut row: single centered column, max 560px */
.charts-grid--center-one {
  grid-template-columns: minmax(0, 560px);
  justify-content: center;
}

.table-wrap {
  overflow-x: auto;
  max-height: 360px;
  overflow-y: auto;
  border-top: 1px solid var(--border);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

thead {
  position: sticky;
  top: 0;
  background: var(--card);
  z-index: 1;
}

th {
  text-align: left;
  font-weight: 500;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

th.sorted-asc::after {
  content: " ↑";
  color: var(--accent);
}

th.sorted-desc::after {
  content: " ↓";
  color: var(--accent);
}

td {
  padding: 9px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  color: var(--text-soft);
}

tbody tr:hover {
  background: var(--hover-bg);
}

td.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.category-tag {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 400;
  color: #1d1d1f;
}

/* ---------- Links ---------- */

td a,
footer a {
  color: var(--text-soft);
  text-decoration: none;
  font-weight: 400;
  background-image: linear-gradient(to right, var(--border), var(--border));
  background-position: 0 100%;
  background-size: 100% 1px;
  background-repeat: no-repeat;
  padding-bottom: 1px;
  transition: color 0.12s ease, background-image 0.12s ease;
}

td a:hover,
footer a:hover {
  color: var(--hover-text);
  background-image: linear-gradient(to right, var(--hover-text), var(--hover-text));
}

td a:visited {
  color: var(--text-soft);
}

td a:visited:hover {
  color: var(--hover-text);
}

footer {
  text-align: center;
  padding: 24px;
}

#status {
  position: fixed;
  bottom: 16px;
  right: 16px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  box-shadow: var(--shadow);
  font-size: 13px;
  max-width: 320px;
  display: none;
}

#status.show {
  display: block;
}

#status.error {
  border-color: var(--red);
  color: var(--red);
}

/* ---------- Auth modal ---------- */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(20, 20, 22, 0.45);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
}

.modal[hidden] {
  display: none;
}

.modal-card {
  background: var(--card);
  border-radius: 14px;
  padding: 28px 28px 24px;
  max-width: 380px;
  width: 100%;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.25);
  text-align: center;
}

.modal-icon {
  font-size: 32px;
  line-height: 1;
  margin-bottom: 14px;
}

.modal-card h2 {
  margin: 0 0 6px;
  font-size: 18px;
  font-weight: 600;
}

.modal-card p {
  margin: 0;
  font-size: 13px;
}

.modal-card form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 20px;
  text-align: left;
}

#passphrase-input {
  padding: 10px 12px;
  font-size: 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: inherit;
  color: var(--text);
  background: var(--card);
}

#passphrase-input:focus {
  border-color: var(--hover-border);
  box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.04);
  outline: none;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-soft);
  cursor: pointer;
  user-select: none;
}

.checkbox-row input[type="checkbox"] {
  margin: 0;
  cursor: pointer;
}

.btn-primary {
  padding: 10px 16px;
  background: var(--text);
  color: #ffffff;
  border: 0;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: opacity 0.15s ease;
}

.btn-primary:hover:not(:disabled) {
  opacity: 0.85;
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: progress;
}

.modal-error {
  font-size: 12px;
  color: var(--red);
  min-height: 16px;
  text-align: center;
}

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
  body > section,
  body > footer {
    padding-left: 20px;
    padding-right: 20px;
  }

  .filters.card {
    padding: 16px 16px;
  }
  .filter-row {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 8px;
  }
  .filter-row + .filter-row {
    margin-top: 14px;
    padding-top: 14px;
  }
  .filter-group {
    flex: 1 1 auto;
  }
  .filter-spacer {
    display: none;
  }
  .filters.card select {
    width: 100%;
    min-width: 0;
  }
  .filter-row > .btn-ghost {
    align-self: flex-end;
    height: 32px;
  }
}

@media (max-width: 720px) {
  body > section,
  body > footer {
    padding-left: 14px;
    padding-right: 14px;
  }

  .topbar {
    padding: 14px 16px;
    flex-wrap: nowrap;
    gap: 10px;
  }
  .topbar-left {
    flex-wrap: nowrap;
    gap: 10px;
  }
  .topbar h1 {
    font-size: 18px;
  }
  #generated-at {
    font-size: 11px;
  }
  .sync-label        { display: none; }
  .sync-icon-desktop { display: none; }
  .sync-icon-mobile  { display: block; }
  #sync-trigger {
    padding: 0;
    width: 32px;
  }

  .resumen-block {
    padding: 0 14px;
    margin-top: 16px;
  }

  .chip-row {
    gap: 5px;
  }
  .chip {
    font-size: 11px;
    padding: 4px 11px;
  }

  .kpi-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
  }
  .kpi {
    padding: 12px 14px;
  }
  .kpi-label {
    font-size: 10px;
  }

  .section-card {
    border-radius: 12px;
  }
  .section-head {
    padding: 14px 16px 10px;
  }
  .section-head h2 {
    font-size: 15px;
  }
  .charts-grid {
    grid-template-columns: 1fr;
    gap: 12px;
    padding: 12px 8px;
  }
  .chart-box {
    min-height: 220px;
    width: 100%;
    overflow: hidden;
  }
  .chart-box canvas {
    max-height: 220px;
    max-width: 100% !important;
    width: 100% !important;
  }
  .chart-box--donut {
    height: auto;
    min-height: unset;
    max-height: unset;
  }
  .chart-box--donut canvas {
    max-height: 280px;
  }
  .chart-box--bars {
    min-height: 200px;
  }
  .chart-box--bars canvas {
    max-height: 200px;
  }

  th,
  td {
    padding: 8px 12px;
    font-size: 12px;
  }
  .table-wrap {
    max-height: 320px;
  }

  #status {
    left: 12px;
    right: 12px;
    bottom: 12px;
    max-width: none;
  }
}

@media (max-width: 420px) {
  .kpi-grid {
    grid-template-columns: 1fr;
  }
  .topbar h1 {
    font-size: 16px;
  }
}
