:root {
  --bg: #f4f6f9;
  --bg-elevated: #ffffff;
  --bg-muted: #eef1f6;
  --border: #d8dee8;
  --text: #1a2332;
  --text-muted: #5c6b7f;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #dbeafe;
  --success: #059669;
  --success-soft: #d1fae5;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --warning: #d97706;
  --warning-soft: #fef3c7;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(26, 35, 50, 0.08), 0 4px 12px rgba(26, 35, 50, 0.04);
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
  --mono: "SF Mono", "Cascadia Code", ui-monospace, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --bg-elevated: #1a2332;
    --bg-muted: #243044;
    --border: #2d3a4f;
    --text: #e8eef6;
    --text-muted: #9aabbf;
    --primary: #3b82f6;
    --primary-hover: #60a5fa;
    --primary-soft: #1e3a5f;
    --success: #34d399;
    --success-soft: #064e3b;
    --danger: #f87171;
    --danger-soft: #7f1d1d;
    --warning: #fbbf24;
    --warning-soft: #78350f;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.3), 0 4px 12px rgba(0, 0, 0, 0.2);
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

a {
  color: var(--primary);
}

/* Layout */
.app-shell {
  max-width: 960px;
  margin: 0 auto;
  padding: 0 1rem 3rem;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1.25rem;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-weight: 700;
  font-size: 1.15rem;
  letter-spacing: -0.02em;
}

.brand-mark {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--primary), #7c3aed);
  display: grid;
  place-items: center;
  color: #fff;
  font-size: 0.85rem;
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.user-email {
  font-size: 0.875rem;
  color: var(--text-muted);
}

.nav-tabs {
  display: flex;
  gap: 0.35rem;
  overflow-x: auto;
  padding-bottom: 0.25rem;
  margin-bottom: 1.25rem;
  -webkit-overflow-scrolling: touch;
}

.nav-tab {
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  padding: 0.55rem 0.9rem;
  border-radius: 999px;
  white-space: nowrap;
  font-weight: 500;
  font-size: 0.9rem;
  transition: background 0.15s, color 0.15s;
}

.nav-tab:hover {
  background: var(--bg-muted);
  color: var(--text);
}

.nav-tab.active {
  background: var(--primary-soft);
  color: var(--primary);
  border-color: transparent;
  font-weight: 600;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

/* Cards */
.card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem 1.2rem;
}

.card + .card {
  margin-top: 1rem;
}

.card-title {
  margin: 0 0 0.85rem;
  font-size: 1rem;
  font-weight: 650;
}

.grid-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
  margin-bottom: 1rem;
}

@media (max-width: 640px) {
  .grid-stats {
    grid-template-columns: 1fr;
  }
}

.stat {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}

.stat-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
  font-weight: 500;
}

.stat-value {
  font-family: var(--mono);
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  word-break: break-word;
}

.money-pos {
  color: var(--success);
}

.money-neg {
  color: var(--danger);
}

.money-neutral {
  color: var(--text);
}

/* Forms */
.form-grid {
  display: grid;
  gap: 0.85rem;
}

.form-grid.two {
  grid-template-columns: 1fr 1fr;
}

@media (max-width: 560px) {
  .form-grid.two {
    grid-template-columns: 1fr;
  }
}

.field label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.field input,
.field select,
.field textarea {
  width: 100%;
  padding: 0.65rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

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

.field textarea {
  min-height: 72px;
  resize: vertical;
}

.row-actions {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  align-items: center;
  margin-top: 0.25rem;
}

.toolbar {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  align-items: end;
  margin-bottom: 1rem;
}

.toolbar .field {
  min-width: 140px;
  flex: 1;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 0.6rem 1rem;
  font-weight: 600;
  font-size: 0.9rem;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

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

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-secondary {
  background: var(--bg-muted);
  color: var(--text);
  border-color: var(--border);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--border);
}

.btn-danger {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: transparent;
}

.btn-danger:hover:not(:disabled) {
  filter: brightness(0.95);
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border-color: transparent;
  padding: 0.45rem 0.65rem;
}

.btn-ghost:hover:not(:disabled) {
  background: var(--bg-muted);
  color: var(--text);
}

.btn-sm {
  padding: 0.35rem 0.65rem;
  font-size: 0.8rem;
}

/* Auth */
.auth-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}

.auth-card {
  width: 100%;
  max-width: 400px;
}

.auth-card h1 {
  margin: 0 0 0.35rem;
  font-size: 1.4rem;
}

.auth-card .subtitle {
  margin: 0 0 1.25rem;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.auth-heading {
  margin: 0 0 1rem;
  font-size: 1.05rem;
  font-weight: 600;
}

.auth-form {
  display: none;
}

.auth-form.active {
  display: block;
}

/* Lists */
.list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.list-item {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.85rem 0;
  border-bottom: 1px solid var(--border);
}

.list-item:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.list-item:first-child {
  padding-top: 0;
}

.list-main {
  min-width: 0;
  flex: 1;
}

.list-title {
  font-weight: 600;
  font-size: 0.95rem;
}

.list-meta {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.15rem;
}

.list-amount {
  font-family: var(--mono);
  font-weight: 700;
  font-size: 0.95rem;
  white-space: nowrap;
  text-align: right;
}

.badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 650;
  padding: 0.15rem 0.45rem;
  border-radius: 999px;
  background: var(--bg-muted);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.badge-ingreso {
  background: var(--success-soft);
  color: var(--success);
}

.badge-egreso {
  background: var(--danger-soft);
  color: var(--danger);
}

.badge-transferencia {
  background: var(--primary-soft);
  color: var(--primary);
}

.empty {
  text-align: center;
  color: var(--text-muted);
  padding: 1.5rem 0.5rem;
  font-size: 0.9rem;
}

/* Alerts / toast */
.alert {
  padding: 0.75rem 0.9rem;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  margin-bottom: 0.85rem;
  border: 1px solid transparent;
}

.alert-error {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: transparent;
}

.alert-success {
  background: var(--success-soft);
  color: var(--success);
}

.alert-info {
  background: var(--primary-soft);
  color: var(--primary);
}

.toast-host {
  position: fixed;
  bottom: 1.25rem;
  right: 1.25rem;
  left: 1.25rem;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  pointer-events: none;
  max-width: 400px;
  margin-left: auto;
}

.toast {
  pointer-events: auto;
  padding: 0.85rem 1rem;
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: 0.875rem;
  animation: slideUp 0.2s ease;
}

.toast.error {
  border-color: var(--danger);
  color: var(--danger);
}

.toast.success {
  border-color: var(--success);
  color: var(--success);
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Import preview table */
.table-wrap {
  overflow-x: auto;
  margin-top: 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

table.preview {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

table.preview th,
table.preview td {
  padding: 0.55rem 0.65rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

table.preview th {
  background: var(--bg-muted);
  font-weight: 650;
  color: var(--text-muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

table.preview tr:last-child td {
  border-bottom: none;
}

table.preview tr.dup td {
  opacity: 0.45;
  text-decoration: line-through;
}

.hidden {
  display: none !important;
}

.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 20, 25, 0.35);
  display: grid;
  place-items: center;
  z-index: 2000;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

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

.section-head h2 {
  margin: 0;
  font-size: 1.05rem;
}

.hint {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin: 0.35rem 0 0;
}

.account-type {
  text-transform: capitalize;
}
