:root {
  --bg: #0b1120;
  --bg-elevated: #111a2e;
  --bg-card: #16223b;
  --bg-input: #0f172a;
  --border: rgba(148, 163, 184, 0.18);
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --accent: #0ea5e9;
  --positive: #10b981;
  --negative: #f43f5e;
  --warning: #f59e0b;
  --shadow: 0 12px 30px rgba(2, 6, 23, 0.45);
  --radius: 14px;
  --radius-sm: 9px;
}

[data-theme="light"] {
  --bg: #f1f5f9;
  --bg-elevated: #ffffff;
  --bg-card: #ffffff;
  --bg-input: #f8fafc;
  --border: rgba(15, 23, 42, 0.12);
  --text: #0f172a;
  --text-muted: #475569;
  --shadow: 0 8px 24px rgba(15, 23, 42, 0.08);
}

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

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

.muted {
  color: var(--text-muted);
}

.small {
  font-size: 0.85rem;
}

.positive {
  color: var(--positive);
}

.negative {
  color: var(--negative);
}

/* Auth Shell */
.auth-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
  background: radial-gradient(circle at top right, rgba(99, 102, 241, 0.22), transparent 55%), var(--bg);
}

.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 28px;
  box-shadow: var(--shadow);
}

.auth-brand {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 22px;
}

.auth-brand h1 {
  margin: 0;
  font-size: 1.35rem;
}

.auth-brand p {
  margin: 2px 0 0;
  font-size: 0.85rem;
}

.auth-logo {
  width: 48px;
  height: 48px;
  border-radius: 12px;
}

.auth-form h2 {
  margin: 0 0 8px;
  font-size: 1.15rem;
}

.form-error {
  color: var(--negative);
  font-size: 0.88rem;
  margin: 8px 0 0;
}

/* Fields & Inputs */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
}

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

input[type="text"],
input[type="password"],
input[type="number"],
input[type="date"],
input[type="datetime-local"],
input[type="search"],
select,
textarea {
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text);
  font-size: 0.94rem;
  font-family: inherit;
}

input:focus,
select:focus,
textarea:focus {
  outline: 2px solid var(--primary);
  border-color: transparent;
}

.input-with-addon {
  display: grid;
  grid-template-columns: 1fr 90px;
  gap: 8px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  color: var(--text);
  background: transparent;
  transition: transform 0.08s ease, background 0.15s ease, border-color 0.15s ease;
}

.btn:active {
  transform: scale(0.98);
}

.btn.small {
  padding: 6px 10px;
  font-size: 0.8rem;
}

.btn-full {
  width: 100%;
  padding: 11px 16px;
}

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

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

.btn-accent {
  background: linear-gradient(135deg, #4f46e5, #0ea5e9);
  color: #ffffff;
}

.btn-secondary {
  background: rgba(148, 163, 184, 0.14);
  border-color: var(--border);
}

.btn-secondary:hover {
  background: rgba(148, 163, 184, 0.24);
}

.btn-ghost {
  border-color: var(--border);
  color: var(--text-muted);
}

.btn-ghost:hover {
  color: var(--text);
  background: rgba(148, 163, 184, 0.12);
}

.btn-icon {
  padding: 7px 10px;
  background: rgba(148, 163, 184, 0.12);
}

/* Topbar & App Layout */
.app-shell {
  min-height: 100vh;
  padding-bottom: 72px;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 24px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-icon {
  width: 34px;
  height: 34px;
  border-radius: 8px;
}

.brand-title {
  display: block;
  font-weight: 700;
  font-size: 1.05rem;
  line-height: 1.1;
}

.brand-sub {
  display: block;
  font-size: 0.74rem;
  color: var(--text-muted);
}

.topbar-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.select-compact {
  width: auto;
  min-width: 132px;
  padding: 8px 10px;
}

.container {
  max-width: 1360px;
  margin: 0 auto;
  padding: 20px 20px 40px;
}

/* KPI Grid */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}

.kpi-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
}

.kpi-networth {
  border-top: 3px solid var(--primary);
}
.kpi-income {
  border-top: 3px solid var(--positive);
}
.kpi-expense {
  border-top: 3px solid var(--negative);
}
.kpi-cashflow {
  border-top: 3px solid var(--accent);
}

.kpi-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
}

.kpi-value {
  font-size: 1.55rem;
  font-weight: 800;
  margin: 6px 0 4px;
  letter-spacing: -0.02em;
}

.kpi-sub {
  font-size: 0.78rem;
  color: var(--text-muted);
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.curr-pill {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.14);
  font-weight: 600;
}

/* Section Headers */
.section-block {
  margin-bottom: 28px;
}

.section-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.section-header h2 {
  margin: 0;
  font-size: 1.2rem;
}

.section-header p {
  margin: 2px 0 0;
}

.section-tools {
  display: flex;
  gap: 8px;
}

/* Banks & Pockets Matrix */
.banks-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}

.bank-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
}

.bank-card.active-scope {
  outline: 2px solid var(--primary);
}

.bank-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.bank-title-row {
  display: flex;
  align-items: center;
  gap: 9px;
}

.bank-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
}

.bank-name {
  font-weight: 700;
  font-size: 1.05rem;
}

.bank-total {
  text-align: right;
}

.bank-total-main {
  font-weight: 800;
  font-size: 1.05rem;
}

.pockets-list {
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
}

.pocket-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  background: rgba(148, 163, 184, 0.07);
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 0.14s ease, border-color 0.14s ease;
}

.pocket-row:hover {
  background: rgba(148, 163, 184, 0.14);
}

.pocket-row.active-scope {
  border-color: var(--primary);
  background: rgba(99, 102, 241, 0.14);
}

.pocket-info {
  min-width: 0;
}

.pocket-name {
  font-weight: 600;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pocket-meta {
  font-size: 0.74rem;
  color: var(--text-muted);
}

.pocket-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.pocket-balance {
  font-weight: 700;
  font-size: 0.92rem;
  text-align: right;
}

.bank-footer {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

/* Charts Grid */
.charts-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.chart-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 12px;
}

.chart-header h3 {
  margin: 0;
  font-size: 0.98rem;
}

.chart-body {
  min-height: 210px;
}

.chart-empty {
  height: 200px;
  display: grid;
  place-items: center;
  color: var(--text-muted);
  font-size: 0.88rem;
  text-align: center;
}

.donut-wrap {
  display: grid;
  grid-template-columns: 170px 1fr;
  gap: 14px;
  align-items: center;
}

.legend-list {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.legend-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
}

.legend-left {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
}

.legend-swatch {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  flex-shrink: 0;
}

.hbar-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-top: 6px;
}

.hbar-row-top {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  margin-bottom: 4px;
}

.hbar-track {
  height: 10px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.16);
  overflow: hidden;
}

.hbar-fill {
  height: 100%;
  border-radius: 999px;
}

/* Filter Bar & Ledger Table */
.filter-bar {
  display: grid;
  grid-template-columns: 2fr repeat(5, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.table-wrap {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
  box-shadow: var(--shadow);
}

.ledger-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

.ledger-table th,
.ledger-table td {
  padding: 11px 12px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: middle;
}

.ledger-table th {
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  background: rgba(148, 163, 184, 0.06);
  white-space: nowrap;
}

.ledger-table tbody tr:hover {
  background: rgba(148, 163, 184, 0.06);
}

.ledger-table th.num,
.ledger-table td.num {
  text-align: right;
  white-space: nowrap;
}

.tx-merchant {
  font-weight: 600;
}

.tx-date {
  font-size: 0.76rem;
  color: var(--text-muted);
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 0.76rem;
  font-weight: 600;
  background: rgba(148, 163, 184, 0.14);
  white-space: nowrap;
}

.badge-note-src {
  display: inline-block;
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  padding: 1px 5px;
  border-radius: 4px;
  margin-right: 5px;
  vertical-align: middle;
}

.badge-auto {
  background: rgba(14, 165, 233, 0.18);
  color: #38bdf8;
}

.badge-manual {
  background: rgba(99, 102, 241, 0.2);
  color: #a5b4fc;
}

.badge-import {
  background: rgba(245, 158, 11, 0.18);
  color: #fbbf24;
}

.badge-historical {
  display: inline-block;
  font-size: 0.68rem;
  padding: 1px 5px;
  border-radius: 4px;
  background: rgba(148, 163, 184, 0.2);
  color: var(--text-muted);
  margin-left: 5px;
}

.badge-type {
  display: inline-block;
  font-size: 0.66rem;
  font-weight: 800;
  padding: 1px 6px;
  border-radius: 4px;
  margin-right: 6px;
  letter-spacing: 0.03em;
  vertical-align: middle;
}

.badge-type-in {
  background: rgba(16, 185, 129, 0.18);
  color: #34d399;
}

.badge-type-out {
  background: rgba(244, 63, 94, 0.18);
  color: #fb7185;
}

.badge-type-transfer {
  background: rgba(59, 130, 246, 0.2);
  color: #60a5fa;
}

.badge-type-sync {
  background: rgba(148, 163, 184, 0.22);
  color: #cbd5e1;
}

.bal-after-pill {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 7px;
  background: rgba(16, 185, 129, 0.1);
  border: 1px solid rgba(16, 185, 129, 0.24);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.account-after-text {
  font-weight: 600;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.note-cell {
  max-width: 280px;
}

.note-clickable {
  cursor: pointer;
  border-bottom: 1px dashed rgba(148, 163, 184, 0.35);
}

.row-actions {
  display: flex;
  gap: 5px;
  justify-content: flex-end;
}

.empty-state {
  padding: 36px 16px;
  text-align: center;
}

/* Modals */
dialog.modal {
  border: none;
  padding: 0;
  background: transparent;
  max-width: 96vw;
  color: var(--text);
}

dialog.modal::backdrop {
  background: rgba(2, 6, 23, 0.72);
  backdrop-filter: blur(4px);
}

.modal-card {
  width: 520px;
  max-width: 95vw;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.modal-wide {
  width: 860px;
}

.modal-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  margin: 0;
  font-size: 1.05rem;
}

.modal-body {
  padding: 18px;
  overflow-y: auto;
}

.modal-footer {
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.modal-footer-inline {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
}

.form-grid-2 {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.form-grid-3 {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.type-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  margin-bottom: 14px;
  padding: 4px;
  border-radius: 10px;
  background: var(--bg-input);
}

.type-tab {
  padding: 8px;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: var(--text-muted);
  font-weight: 700;
  cursor: pointer;
}

.type-tab.active {
  background: var(--primary);
  color: #ffffff;
}

/* Receipt Scanner */
.receipt-controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  align-items: center;
  margin-bottom: 12px;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.88rem;
  cursor: pointer;
}

.toggle-row input {
  width: 18px;
  height: 18px;
}

.dropzone {
  display: block;
  border: 2px dashed rgba(99, 102, 241, 0.5);
  border-radius: 14px;
  padding: 26px 16px;
  text-align: center;
  cursor: pointer;
  background: rgba(99, 102, 241, 0.06);
  transition: background 0.15s ease, border-color 0.15s ease;
}

.dropzone:hover,
.dropzone.dragover {
  background: rgba(99, 102, 241, 0.14);
  border-color: var(--primary);
}

.dropzone-icon {
  font-size: 2rem;
  margin-bottom: 6px;
}

.status-banner {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: rgba(14, 165, 233, 0.15);
  border: 1px solid rgba(14, 165, 233, 0.35);
  font-size: 0.88rem;
}

.status-banner.warn {
  background: rgba(245, 158, 11, 0.15);
  border-color: rgba(245, 158, 11, 0.35);
}

.status-banner.err {
  background: rgba(244, 63, 94, 0.15);
  border-color: rgba(244, 63, 94, 0.35);
}

.receipt-split {
  display: grid;
  grid-template-columns: 260px 1fr;
  gap: 16px;
  margin-top: 14px;
}

.receipt-img-box {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px;
  display: grid;
  place-items: center;
}

.receipt-img-box img {
  max-width: 100%;
  max-height: 360px;
  border-radius: 6px;
}

.rc-items-box {
  font-size: 0.82rem;
  color: var(--text-muted);
  background: var(--bg-input);
  border-radius: 8px;
  padding: 8px 10px;
  max-height: 110px;
  overflow-y: auto;
}

.items-list {
  padding-left: 18px;
  margin: 6px 0 0;
  font-size: 0.88rem;
}

/* Importer */
.import-upload-row {
  margin-bottom: 14px;
}

.mapping-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}

.table-preview {
  max-height: 240px;
  overflow: auto;
}

.small-table th,
.small-table td {
  padding: 6px 9px;
  font-size: 0.8rem;
}

/* Settings */
.settings-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.settings-panel {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
}

.settings-panel h4 {
  margin: 0 0 8px;
}

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

/* Toast */
#toast-container {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: 0.88rem;
  font-weight: 600;
  max-width: 340px;
}

.toast.ok {
  border-left: 4px solid var(--positive);
}

.toast.err {
  border-left: 4px solid var(--negative);
}

/* Mobile Nav */
.mobile-nav {
  display: none;
}

@media (max-width: 1080px) {
  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .banks-grid {
    grid-template-columns: 1fr;
  }
  .charts-grid {
    grid-template-columns: 1fr;
  }
  .filter-bar {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 720px) {
  .topbar {
    padding: 10px 14px;
  }
  .topbar-actions .btn-accent,
  .topbar-actions #btn-add-tx,
  .topbar-actions #btn-transfer,
  .topbar-actions #btn-open-import {
    display: none;
  }
  .container {
    padding: 14px 12px 30px;
  }
  .kpi-grid {
    grid-template-columns: 1fr;
  }
  .donut-wrap,
  .receipt-split,
  .receipt-controls,
  .settings-grid,
  .form-grid-2,
  .form-grid-3,
  .mapping-grid {
    grid-template-columns: 1fr;
  }
  .filter-bar {
    grid-template-columns: 1fr;
  }
  .mobile-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 40;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    background: var(--bg-elevated);
    border-top: 1px solid var(--border);
    padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
    gap: 4px;
  }
  .mnav-btn {
    border: none;
    background: transparent;
    color: var(--text);
    padding: 8px 4px;
    border-radius: 9px;
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
  }
  .mnav-primary {
    background: var(--primary);
    color: #fff;
  }
}
