/* ==========================================================
   Design tokens
   ========================================================== */
:root {
  --color-bg: #f4f6f8;
  --color-surface: #ffffff;
  --color-surface-alt: #f0f2f5;
  --color-border: #e2e5ea;
  --color-text: #1a1d23;
  --color-text-muted: #6b7280;
  --color-primary: #2563eb;
  --color-primary-hover: #1d4ed8;
  --color-primary-soft: #eaf0ff;
  --color-success: #16a34a;
  --color-success-soft: #e8f8ee;
  --color-danger: #dc2626;
  --color-danger-soft: #fdecea;
  --color-warning: #d97706;
  --color-warning-soft: #fef3e0;
  --color-info: #0891b2;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow-md: 0 4px 12px rgba(16, 24, 40, 0.08);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --sidebar-width: 220px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;

  --chart-1: #2563eb; --chart-2: #16a34a; --chart-3: #d97706; --chart-4: #dc2626;
  --chart-5: #7c3aed; --chart-6: #0891b2; --chart-7: #db2777; --chart-8: #65a30d;
  --chart-9: #ea580c; --chart-10: #4f46e5; --chart-11: #059669; --chart-12: #b45309;
  --chart-13: #9333ea; --chart-14: #64748b;
}

[data-theme="dark"] {
  --color-bg: #0f1216;
  --color-surface: #171b21;
  --color-surface-alt: #1e232b;
  --color-border: #2a2f38;
  --color-text: #e6e8eb;
  --color-text-muted: #9aa1ac;
  --color-primary: #4f7dfb;
  --color-primary-hover: #6b91fc;
  --color-primary-soft: #1a2740;
  --color-success: #34d17f;
  --color-success-soft: #123822;
  --color-danger: #f16464;
  --color-danger-soft: #3b1c1c;
  --color-warning: #f0ab4b;
  --color-warning-soft: #3a2a10;
  --color-info: #38bdf8;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--color-bg);
  color: var(--color-text);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, h4 { margin: 0 0 4px; font-weight: 600; }
p { margin: 0 0 8px; }
a { color: var(--color-primary); text-decoration: none; }
button { font-family: inherit; }
.hidden { display: none !important; }
.muted { color: var(--color-text-muted); }
.text-right { text-align: right; }
.scroll-x { overflow-x: auto; }

/* ==========================================================
   Buttons & form controls
   ========================================================== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 16px; border-radius: var(--radius-sm); border: 1px solid var(--color-border);
  background: var(--color-surface); color: var(--color-text); font-size: 14px; font-weight: 500;
  cursor: pointer; transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn:hover { background: var(--color-surface-alt); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--color-primary); border-color: var(--color-primary); color: #fff; }
.btn-primary:hover { background: var(--color-primary-hover); border-color: var(--color-primary-hover); }
.btn-danger { background: var(--color-danger); border-color: var(--color-danger); color: #fff; }
.btn-danger:hover { opacity: 0.9; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-block { width: 100%; }
.btn-icon { padding: 6px 8px; line-height: 1; }

label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 5px; color: var(--color-text-muted); }
input, select, textarea {
  width: 100%; padding: 9px 11px; border-radius: var(--radius-sm); border: 1px solid var(--color-border);
  background: var(--color-surface); color: var(--color-text); font-size: 14px; font-family: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--color-primary); outline-offset: -1px; }
.field { margin-bottom: 14px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field-hint { font-size: 12px; color: var(--color-text-muted); margin-top: 4px; }
.field-error { font-size: 12px; color: var(--color-danger); margin-top: 4px; }

/* ==========================================================
   Auth screen
   ========================================================== */
.auth-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px;
}
.auth-card {
  width: 100%; max-width: 380px; background: var(--color-surface); border: 1px solid var(--color-border);
  border-radius: var(--radius-lg); padding: 32px 28px; box-shadow: var(--shadow-md);
}
.auth-logo { font-size: 22px; font-weight: 700; margin-bottom: 4px; }
.auth-sub { color: var(--color-text-muted); font-size: 13px; margin-bottom: 22px; }
.auth-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 18px; }
.auth-links { display: flex; justify-content: space-between; margin-top: 14px; font-size: 13px; }
.otp-input {
  font-size: 26px; letter-spacing: 10px; text-align: center; font-weight: 600;
}
.alert {
  padding: 10px 12px; border-radius: var(--radius-sm); font-size: 13px; margin-bottom: 14px;
}
.alert-error { background: var(--color-danger-soft); color: var(--color-danger); }
.alert-success { background: var(--color-success-soft); color: var(--color-success); }
.alert-info { background: var(--color-primary-soft); color: var(--color-primary); }

/* ==========================================================
   App shell
   ========================================================== */
#app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-width); flex-shrink: 0; background: var(--color-surface);
  border-right: 1px solid var(--color-border); padding: 18px 12px; position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column;
}
.sidebar-brand { font-weight: 700; font-size: 17px; padding: 6px 10px 18px; }
.nav-list { list-style: none; margin: 0; padding: 0; flex: 1; }
.nav-item a {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: var(--radius-sm);
  color: var(--color-text); font-size: 14px; font-weight: 500; margin-bottom: 2px;
}
.nav-item a:hover { background: var(--color-surface-alt); }
.nav-item a.active { background: var(--color-primary-soft); color: var(--color-primary); }
.nav-icon { width: 18px; text-align: center; }
.sidebar-footer { border-top: 1px solid var(--color-border); padding-top: 12px; font-size: 13px; }

.main {
  flex: 1; min-width: 0; display: flex; flex-direction: column;
}
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 22px; border-bottom: 1px solid var(--color-border); background: var(--color-surface);
  position: sticky; top: 0; z-index: 5;
}
.topbar-title { font-size: 18px; font-weight: 600; }
.hamburger { display: none; }
.content { padding: 20px; flex: 1; }

.view { display: none; }
.view.active { display: block; }

/* ==========================================================
   Cards / grid
   ========================================================== */
.card {
  background: var(--color-surface); border: 1px solid var(--color-border); border-radius: var(--radius-md);
  padding: 16px 18px; box-shadow: var(--shadow-sm);
}
.card-title { font-size: 13px; color: var(--color-text-muted); margin-bottom: 6px; font-weight: 500; }
.card-value { font-size: 22px; font-weight: 700; }
.card-sub { font-size: 12px; margin-top: 4px; }
.up { color: var(--color-success); }
.down { color: var(--color-danger); }

.grid { display: grid; gap: 14px; }
.grid-cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid-cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid-cols-4 { grid-template-columns: repeat(4, 1fr); }
.grid-cols-6 { grid-template-columns: repeat(6, 1fr); }

.section-title { display: flex; align-items: center; justify-content: space-between; margin: 26px 0 12px; }
.section-title:first-child { margin-top: 0; }
.section-title h2 { font-size: 16px; }

.chart-card { padding: 16px 18px; }
.chart-card h3 { font-size: 14px; margin-bottom: 12px; }
.chart-wrap { position: relative; height: 260px; }
.chart-wrap.tall { height: 320px; }

/* ==========================================================
   Tables
   ========================================================== */
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--color-border); white-space: nowrap; }
th { color: var(--color-text-muted); font-weight: 600; font-size: 12.5px; text-transform: uppercase; letter-spacing: 0.3px; }
tbody tr:hover { background: var(--color-surface-alt); }
.table-card { background: var(--color-surface); border: 1px solid var(--color-border); border-radius: var(--radius-md); overflow: hidden; }
.table-toolbar { display: flex; flex-wrap: wrap; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--color-border); align-items: end; }
.table-toolbar .field { margin-bottom: 0; min-width: 130px; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 600; }
.badge-income { background: var(--color-success-soft); color: var(--color-success); }
.badge-expense { background: var(--color-danger-soft); color: var(--color-danger); }
.badge-investment { background: var(--color-primary-soft); color: var(--color-primary); }
.badge-transfer { background: var(--color-surface-alt); color: var(--color-text-muted); }
.badge-loan { background: var(--color-warning-soft); color: var(--color-warning); }
.badge-refund { background: var(--color-primary-soft); color: var(--color-info); }
.pagination { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; font-size: 13px; }
.pagination-controls { display: flex; gap: 6px; }

/* ==========================================================
   Forms / modal
   ========================================================== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15, 18, 22, 0.5); display: flex; align-items: center;
  justify-content: center; z-index: 50; padding: 16px;
}
.modal {
  background: var(--color-surface); border-radius: var(--radius-lg); padding: 22px; width: 100%;
  max-width: 480px; max-height: 90vh; overflow-y: auto; box-shadow: var(--shadow-md);
}
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

.empty-state { text-align: center; padding: 50px 20px; color: var(--color-text-muted); }
.empty-state .btn { margin-top: 12px; }

.progress-bar { height: 8px; border-radius: 999px; background: var(--color-surface-alt); overflow: hidden; }
.progress-bar-fill { height: 100%; background: var(--color-primary); border-radius: 999px; transition: width 0.3s; }
.progress-bar-fill.low { background: var(--color-success); }
.progress-bar-fill.warn, .progress-bar-fill.moderate { background: var(--color-warning); }
.progress-bar-fill.danger, .progress-bar-fill.high { background: var(--color-danger); }
.progress-bar-fill.over-limit { background: var(--color-danger); }

.cc-card { display: flex; flex-direction: column; gap: 10px; }
.cc-card-head { display: flex; justify-content: space-between; align-items: flex-start; }
.cc-card-name { font-weight: 600; font-size: 15px; }
.cc-card-sub { font-size: 12.5px; color: var(--color-text-muted); }
.cc-card-amounts { display: flex; justify-content: space-between; font-size: 13px; }
.cc-over-limit-badge { background: var(--color-danger-soft); color: var(--color-danger); padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; }
.cc-status-badge { padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; }
.cc-status-active { background: var(--color-success-soft); color: var(--color-success); }
.cc-status-blocked { background: var(--color-warning-soft); color: var(--color-warning); }
.cc-status-closed { background: var(--color-surface-alt); color: var(--color-text-muted); }
.cc-mini-tile { padding: 12px; border: 1px solid var(--color-border); border-radius: var(--radius-sm); cursor: pointer; }
.cc-mini-tile:hover { background: var(--color-surface-alt); }
.cc-filter-tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.cc-filter-tabs .btn.active { background: var(--color-primary); color: #fff; border-color: var(--color-primary); }
.statement-line { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--color-border); font-size: 13.5px; }
.statement-line:last-child { border-bottom: none; font-weight: 700; padding-top: 12px; }

.budget-row { padding: 14px 0; border-bottom: 1px solid var(--color-border); }
.budget-row:last-child { border-bottom: none; }
.budget-row-head { display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 13.5px; }

.category-tree { list-style: none; margin: 0; padding: 0; }
.category-tree > li { margin-bottom: 4px; }
.category-row { display: flex; align-items: center; justify-content: space-between; padding: 8px 10px; border-radius: var(--radius-sm); }
.category-row:hover { background: var(--color-surface-alt); }
.category-sub-list { list-style: none; margin: 2px 0 4px 20px; padding: 0; }
.category-actions { display: flex; gap: 4px; }
.category-type-block { margin-bottom: 22px; }

.toast-container { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast {
  background: var(--color-text); color: var(--color-bg); padding: 10px 16px; border-radius: var(--radius-sm);
  font-size: 13.5px; box-shadow: var(--shadow-md); max-width: 320px; animation: toast-in 0.15s ease-out;
}
.toast-error { background: var(--color-danger); color: #fff; }
.toast-success { background: var(--color-success); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

.fab {
  position: fixed; bottom: 22px; right: 22px; width: 52px; height: 52px; border-radius: 50%;
  background: var(--color-primary); color: #fff; border: none; font-size: 26px; box-shadow: var(--shadow-md);
  cursor: pointer; display: none; align-items: center; justify-content: center; z-index: 20;
}

.insight-list { list-style: none; margin: 0; padding: 0; }
.insight-list li { padding: 10px 0; border-bottom: 1px solid var(--color-border); font-size: 13.5px; }
.insight-list li:last-child { border-bottom: none; }

.mobile-bottom-nav { display: none; }

/* ==========================================================
   Responsive
   ========================================================== */
@media (max-width: 960px) {
  .grid-cols-4 { grid-template-columns: repeat(2, 1fr); }
  .grid-cols-3 { grid-template-columns: repeat(2, 1fr); }
  .grid-cols-6 { grid-template-columns: repeat(3, 1fr); }
  .field-row { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  .sidebar {
    position: fixed; left: 0; top: 0; z-index: 40; transform: translateX(-100%);
    transition: transform 0.2s ease; box-shadow: var(--shadow-md);
  }
  .sidebar.open { transform: translateX(0); }
  .hamburger { display: inline-flex; }
  .grid-cols-2, .grid-cols-3, .grid-cols-4, .grid-cols-6 { grid-template-columns: repeat(2, 1fr); }
  .content { padding: 14px; padding-bottom: 78px; }
  table.responsive-stack thead { display: none; }
  .table-card { overflow-x: auto; }
  .fab { display: flex; }
  .mobile-bottom-nav {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0; background: var(--color-surface);
    border-top: 1px solid var(--color-border); z-index: 30; padding: 6px 4px;
  }
  .mobile-bottom-nav a {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 2px;
    font-size: 10.5px; color: var(--color-text-muted); border-radius: var(--radius-sm);
  }
  .mobile-bottom-nav a.active { color: var(--color-primary); }
  .mobile-bottom-nav .nav-icon { font-size: 17px; }
}

@media (max-width: 480px) {
  .grid-cols-2, .grid-cols-3, .grid-cols-4, .grid-cols-6 { grid-template-columns: 1fr; }
  .auth-card { padding: 24px 18px; }
}

/* Sidebar backdrop on mobile */
.sidebar-backdrop {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 35;
}
.sidebar-backdrop.open { display: block; }
