:root {
  color-scheme: light;
  --bg: #f5f4f0;
  --surface: #fcfcfb;
  --surface-2: #f0efea;
  --line: #e2e0d9;
  --text: #1b1b19;
  --text-2: #52514e;
  --text-3: #6c6b66;
  --accent: #1c6b4f;
  --accent-ink: #ffffff;
  --accent-soft: #dfeee7;
  --accent-soft-ink: #14523c;
  --warn-soft: #fbeedb;
  --warn-ink: #7a4a00;
  --danger: #b3261e;
  --danger-soft: #fae3e1;
  --violet-soft: #e9e6fb;
  --violet-ink: #43359f;
  --series-balance: #2a78d6;
  --series-contributed: #eb6834;
  --grid: #e8e6df;
  --shadow: 0 1px 2px rgb(0 0 0 / 5%);
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #252523;
    --line: #33332f;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --text-3: #a3a297;
    --accent: #4eb68f;
    --accent-ink: #08261b;
    --accent-soft: #17362a;
    --accent-soft-ink: #a5e0c8;
    --warn-soft: #3a2b12;
    --warn-ink: #f0cd8f;
    --danger: #f2877f;
    --danger-soft: #3f1d1a;
    --violet-soft: #2a2650;
    --violet-ink: #c5befa;
    --series-balance: #3987e5;
    --series-contributed: #d95926;
    --grid: #2c2c29;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

h1, h2, h3, p { margin: 0; }
a { color: var(--accent); }
h1 { font-size: 1.5rem; line-height: 1.2; letter-spacing: -0.01em; }
h2 { font-size: 1.05rem; line-height: 1.3; }

[hidden] { display: none !important; }

.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--text-2); }
.small { font-size: 0.875rem; }

/* Estructura */
.layout { min-height: 100dvh; display: flex; flex-direction: column; }

.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 1.05rem; }
.user-box { margin-left: auto; display: flex; align-items: center; gap: 12px; }

.main {
  flex: 1;
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 20px 16px calc(96px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.tabs {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 5;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 0 7px;
  min-height: 56px;
  color: var(--text-2);
  font-size: 0.6875rem;
  text-decoration: none;
}
.tab svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tab[aria-current="page"] { color: var(--accent); font-weight: 600; }
.tab:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

@media (min-width: 760px) {
  .tabs {
    position: static;
    display: flex;
    gap: 2px;
    border: 0;
    padding: 0;
    background: none;
    order: 0;
  }
  .tab {
    flex-direction: row;
    gap: 6px;
    min-height: 0;
    padding: 6px 10px;
    border-radius: 8px;
    font-size: 0.9375rem;
  }
  .tab svg { display: none; }
  .tab[aria-current="page"] { background: var(--accent-soft); color: var(--accent-soft-ink); }
  .main { padding-bottom: 40px; }
}

.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 16px;
  box-shadow: var(--shadow);
  min-width: 0;
}
.card > h2 { margin-bottom: 12px; }

.stack { display: flex; flex-direction: column; gap: 12px; }
.grid-2 { display: grid; gap: 16px; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 640px) { .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

/* Cifras */
.hero-label { color: var(--text-2); font-size: 0.9375rem; }
.hero-value { font-size: clamp(2.25rem, 10vw, 3.25rem); font-weight: 700; line-height: 1.1; letter-spacing: -0.02em; overflow-wrap: anywhere; }
.hero-note { color: var(--text-2); font-size: 0.875rem; margin-top: 4px; }
.hero-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }

.stats { display: grid; gap: 12px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (min-width: 760px) { .stats { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.stat { padding: 14px 16px; }
.stat-label { color: var(--text-2); font-size: 0.8125rem; }
.stat-value { font-size: 1.125rem; font-weight: 650; margin-top: 2px; overflow-wrap: anywhere; }

/* En un celular angosto, cada cifra en su renglón para que no se parta. */
@media (max-width: 439px) {
  .stats { grid-template-columns: minmax(0, 1fr); gap: 8px; }
  .stat { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; padding: 12px 16px; }
  .stat-value { margin-top: 0; white-space: nowrap; }
}

.lines { display: grid; gap: 6px; margin: 0; }
.lines > div { display: flex; justify-content: space-between; gap: 16px; }
.lines dt { color: var(--text-2); }
.lines dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.lines .total { border-top: 1px solid var(--line); padding-top: 8px; margin-top: 2px; font-weight: 650; }
.lines .total dt { color: var(--text); }
.person-total { font-size: 1.5rem; font-weight: 700; overflow-wrap: anywhere; margin-bottom: 10px; }

/* Botones */
button, .btn { font: inherit; }
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 8px 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font-weight: 550;
  cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn:focus-visible, input:focus-visible, select:focus-visible, .link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.danger-solid { background: var(--danger); border-color: var(--danger); color: var(--surface); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.compact { min-height: 36px; padding: 4px 12px; font-size: 0.875rem; }
.link {
  background: none;
  border: 0;
  padding: 4px 2px;
  color: var(--text-2);
  text-decoration: underline;
  cursor: pointer;
  font-size: 0.875rem;
}

/* Formularios */
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > label, .field-label { font-size: 0.875rem; font-weight: 550; color: var(--text-2); }
input, select {
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
input[aria-invalid="true"] { border-color: var(--danger); }
.hint { font-size: 0.8125rem; color: var(--text-3); }
.error { color: var(--danger); font-size: 0.875rem; }
.form-grid { display: grid; gap: 12px; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 560px) { .form-grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

.segmented { display: flex; gap: 4px; padding: 4px; background: var(--surface-2); border-radius: 12px; }
.segmented label { flex: 1; position: relative; cursor: pointer; }
.segmented input { position: absolute; inset: 0; opacity: 0; min-height: 0; cursor: pointer; }
.segmented span {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  padding: 4px 10px;
  border-radius: 9px;
  font-weight: 550;
  color: var(--text-2);
  text-align: center;
}
.segmented input:checked + span { background: var(--surface); color: var(--text); box-shadow: 0 0 0 1px var(--line); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }

.notice { padding: 10px 12px; border-radius: 10px; font-size: 0.9375rem; }
.notice.good { background: var(--accent-soft); color: var(--accent-soft-ink); }
.notice.warn { background: var(--warn-soft); color: var(--warn-ink); }

/* Tablas */
.table-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
table { border-collapse: collapse; width: 100%; font-size: 0.9375rem; }
th, td { padding: 8px 10px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--grid); font-variant-numeric: tabular-nums; }
th { color: var(--text-2); font-size: 0.8125rem; font-weight: 550; }
th:first-child, td:first-child { text-align: left; padding-left: 0; }
th:last-child, td:last-child { padding-right: 0; }
tr.highlight td { background: var(--accent-soft); color: var(--accent-soft-ink); font-weight: 600; }
tr.highlight td:first-child { padding-left: 8px; }
tr.highlight td:last-child { padding-right: 8px; }

@media (max-width: 519px) {
  .wide-only { display: none; }
  table { font-size: 0.8125rem; }
  th, td { padding: 8px 4px; }
}
@media (max-width: 399px) {
  .plan th:first-child, .plan td:first-child { display: none; }
  .plan th:nth-child(2), .plan td:nth-child(2) { text-align: left; padding-left: 0; }
}

/* Movimientos */
.filters { display: grid; gap: 8px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.move-list { list-style: none; margin: 0; padding: 0; }
.move { display: flex; gap: 12px; align-items: flex-start; padding: 12px 0; border-bottom: 1px solid var(--grid); }
.move:first-child { padding-top: 0; }
.move:last-child { border-bottom: 0; padding-bottom: 0; }
.move-main { flex: 1; min-width: 0; }
.move-title { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-weight: 550; }
.move-desc { color: var(--text-2); font-size: 0.875rem; overflow-wrap: anywhere; }
.move-meta { color: var(--text-3); font-size: 0.8125rem; }
.move-side { text-align: right; flex-shrink: 0; }
.move-amount { font-weight: 650; font-variant-numeric: tabular-nums; }
.move-amount.out { color: var(--danger); }
.move-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 2px; }

.tag { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 0.75rem; font-weight: 600; }
.tag.in { background: var(--accent-soft); color: var(--accent-soft-ink); }
.tag.out { background: var(--danger-soft); color: var(--danger); }
.tag.adj { background: var(--violet-soft); color: var(--violet-ink); }
.tag.future { background: var(--warn-soft); color: var(--warn-ink); }

/* Tasas */
.rate { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 4px 12px; padding: 10px 0; border-bottom: 1px solid var(--grid); }
.rate:last-child { border-bottom: 0; }

/* Gráfico */
.chart-box { position: relative; height: 280px; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 16px; margin-bottom: 10px; font-size: 0.875rem; color: var(--text-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.key { display: inline-block; width: 16px; height: 2px; border-radius: 1px; }
.key.balance { background: var(--series-balance); }
.key.contributed { background: var(--series-contributed); }

/* Diálogos */
dialog {
  width: min(480px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / 45%); }
.dialog-form { display: flex; flex-direction: column; gap: 14px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

/* Login */
.login { min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login .card { width: min(380px, 100%); padding: 24px; }
.login h1 { margin-bottom: 4px; }
.login p { margin-bottom: 16px; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(72px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 20;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--text);
  color: var(--surface);
  font-size: 0.9375rem;
}
#toast.bad { background: var(--danger); }
@media (min-width: 760px) { #toast { bottom: 24px; } }
