/* ═══════════════════════════════════════════ RESET & BASE ═══ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

:root {
  --bg:         #0f1117;
  --bg2:        #1a1d27;
  --bg3:        #22263a;
  --border:     #2e3348;
  --text:       #e8eaf0;
  --text-muted: #8891aa;
  --accent:     #6c63ff;
  --accent2:    #7c74ff;
  --green:      #2dce89;
  --red:        #f5365c;
  --amber:      #fb6340;
  --blue:       #11cdef;
  --sidebar-w:  220px;
  --topbar-h:   60px;
  --radius:     10px;
  --trans:      0.18s ease;
}

html, body { height: 100%; font-family: 'Segoe UI', system-ui, sans-serif; background: var(--bg); color: var(--text); font-size: 15px; }

a { color: var(--accent); text-decoration: none; }
button { cursor: pointer; font-family: inherit; font-size: inherit; }
input, select, textarea { font-family: inherit; font-size: inherit; }
ul { list-style: none; }

/* ════════════════════════════════════════ PASSWORD GATE ═══ */
#password-gate {
  position: fixed; inset: 0; z-index: 1000;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  transition: opacity 0.4s ease;
}
#password-gate.fade-out { opacity: 0; pointer-events: none; }

#gate-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 48px 40px;
  width: 360px;
  text-align: center;
  box-shadow: 0 24px 64px rgba(0,0,0,0.5);
  transition: transform 0.15s ease;
}
#gate-card.shake {
  animation: shake 0.4s ease;
}
@keyframes shake {
  0%,100% { transform: translateX(0); }
  20%      { transform: translateX(-8px); }
  40%      { transform: translateX(8px); }
  60%      { transform: translateX(-6px); }
  80%      { transform: translateX(6px); }
}

.gate-icon { font-size: 40px; margin-bottom: 16px; }
.gate-title { font-size: 24px; font-weight: 700; margin-bottom: 6px; }
.gate-sub   { font-size: 14px; color: var(--text-muted); margin-bottom: 28px; }

#gate-form { display: flex; flex-direction: column; gap: 12px; }
#gate-input {
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text);
  padding: 12px 16px; outline: none; transition: border-color var(--trans);
}
#gate-input:focus { border-color: var(--accent); }
#gate-form button {
  background: var(--accent); color: #fff;
  border: none; border-radius: var(--radius);
  padding: 12px; font-weight: 600;
  transition: background var(--trans);
}
#gate-form button:hover { background: var(--accent2); }

.gate-error { color: var(--red); font-size: 13px; margin-top: 10px; }

/* ══════════════════════════════════════════════ APP SHELL ═══ */
#app { display: flex; height: 100vh; }

/* ─── Sidebar ─── */
#sidebar {
  width: var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  flex-shrink: 0;
  transition: width var(--trans), transform var(--trans);
  overflow: hidden;
}
#sidebar.collapsed { width: 60px; }

.sidebar-brand {
  display: flex; align-items: center; gap: 12px;
  padding: 20px 18px; font-size: 18px; font-weight: 700;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.brand-icon { font-size: 22px; flex-shrink: 0; }
.brand-text  { transition: opacity var(--trans); }
#sidebar.collapsed .brand-text,
#sidebar.collapsed .nav-label { opacity: 0; width: 0; pointer-events: none; }

.nav-list { padding: 12px 0; flex: 1; }
.nav-item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 18px; border-radius: 8px; margin: 2px 8px;
  cursor: pointer; color: var(--text-muted);
  transition: background var(--trans), color var(--trans);
  white-space: nowrap; overflow: hidden;
}
.nav-item:hover   { background: var(--bg3); color: var(--text); }
.nav-item.active  { background: rgba(108,99,255,0.18); color: var(--accent); }
.nav-icon { font-size: 18px; flex-shrink: 0; }
.nav-label { font-size: 14px; font-weight: 500; }
.review-badge {
  margin-left: auto; min-width: 20px; height: 20px; padding: 0 6px;
  background: var(--red); color: #fff; border-radius: 10px;
  font-size: 11px; font-weight: 700; display: flex; align-items: center; justify-content: center;
}

/* ─── Main area ─── */
#main-area { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

#topbar {
  height: var(--topbar-h); background: var(--bg2);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 16px;
  padding: 0 24px; flex-shrink: 0;
}
#sidebar-toggle {
  background: none; border: 1px solid var(--border);
  border-radius: 6px; color: var(--text); padding: 6px 10px;
  font-size: 16px; line-height: 1;
}
#view-title { font-size: 18px; font-weight: 700; flex: 1; }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.sync-label { font-size: 12px; color: var(--text-muted); }

#view-container { flex: 1; overflow-y: auto; padding: 28px; }

/* ══════════════════════════════════════════ UTILITY CLASSES ═══ */
.btn-primary {
  background: var(--accent); color: #fff; border: none;
  border-radius: var(--radius); padding: 9px 18px;
  font-weight: 600; font-size: 14px;
  display: inline-flex; align-items: center; gap: 8px;
  transition: background var(--trans);
}
.btn-primary:hover { background: var(--accent2); }

.btn-ghost {
  background: none; color: var(--text-muted);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 8px 14px; font-size: 13px;
  transition: border-color var(--trans), color var(--trans);
}
.btn-ghost:hover { border-color: var(--text-muted); color: var(--text); }

.btn-danger {
  background: none; color: var(--red);
  border: 1px solid var(--red); border-radius: var(--radius);
  padding: 8px 14px; font-size: 13px;
}

.card {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px;
}
.card-title { font-size: 13px; color: var(--text-muted); font-weight: 500; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 8px; }

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

.spinner {
  width: 16px; height: 16px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

.badge {
  display: inline-block; padding: 2px 8px;
  border-radius: 99px; font-size: 11px; font-weight: 600;
}
.badge-green  { background: rgba(45,206,137,0.15); color: var(--green); }
.badge-red    { background: rgba(245,54,92,0.15);  color: var(--red); }
.badge-amber  { background: rgba(251,99,64,0.15);  color: var(--amber); }
.badge-blue   { background: rgba(17,205,239,0.15); color: var(--blue); }

.amount-income  { color: var(--green); font-weight: 600; }
.amount-expense { color: var(--red);   font-weight: 600; }

/* ══════════════════════════════════════════════ DASHBOARD ═══ */

/* Available Balance Banner */
.balance-banner {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px 28px;
  display: flex;
  align-items: center;
  gap: 40px;
  flex-wrap: wrap;
}
.balance-banner-inner { flex: 1; min-width: 200px; }
.balance-label {
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 1px; color: var(--text-muted); margin-bottom: 6px;
}
.balance-sub { font-size: 12px; color: var(--text-muted); margin-top: 6px; }
.balance-skeleton {
  display: inline-block; width: 200px; height: 40px;
  background: var(--bg3); border-radius: 6px;
  animation: pulse 1.2s infinite;
}
.balance-breakdown {
  display: flex; flex-direction: column; gap: 10px;
  flex-shrink: 0; min-width: 220px;
}
.balance-stat {
  display: flex; align-items: center; gap: 8px; font-size: 13px;
}
.balance-stat-dot {
  width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0;
}
.balance-stat-label { flex: 1; color: var(--text-muted); }
.balance-stat-val { font-weight: 600; }

@keyframes pulse { 0%,100% { opacity:0.5; } 50% { opacity:1; } }

.kpi-value { font-size: 28px; font-weight: 700; margin: 4px 0; }
.kpi-sub   { font-size: 12px; color: var(--text-muted); }
.kpi-income  .kpi-value { color: var(--green); }
.kpi-expense .kpi-value { color: var(--red); }
.kpi-net .kpi-value     { color: var(--blue); }
.kpi-rate .kpi-value    { color: var(--accent); }

.chart-card { background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; }
.chart-card canvas { max-height: 260px; }

.budget-progress-row { display: flex; flex-direction: column; gap: 14px; }
.budget-bar-item { display: flex; flex-direction: column; gap: 6px; }
.budget-bar-header { display: flex; justify-content: space-between; font-size: 13px; }
.budget-bar-track {
  height: 8px; background: var(--bg3); border-radius: 99px; overflow: hidden;
}
.budget-bar-fill {
  height: 100%; border-radius: 99px;
  background: var(--green);
  transition: width 0.4s ease;
}
.budget-bar-fill.amber { background: var(--amber); }
.budget-bar-fill.red   { background: var(--red); }

/* ══════════════════════════════════════════════════ TABLES ═══ */
.data-table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th {
  text-align: left; padding: 10px 14px;
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.5px; color: var(--text-muted);
  border-bottom: 1px solid var(--border);
}
td {
  padding: 11px 14px; border-bottom: 1px solid rgba(46,51,72,0.6);
  vertical-align: middle;
}
tr:last-child td { border-bottom: none; }
tr:hover td { background: rgba(255,255,255,0.025); }

.conf-dot {
  display: inline-block; width: 8px; height: 8px;
  border-radius: 50%; margin-right: 4px;
}
.conf-high   { background: var(--green); }
.conf-medium { background: var(--amber); }
.conf-low    { background: var(--red); }

/* ══════════════════════════════════════════════════ FILTERS ═══ */
.filters-bar {
  display: flex; flex-wrap: wrap; gap: 10px;
  margin-bottom: 18px; align-items: flex-end;
}
.filter-group { display: flex; flex-direction: column; gap: 4px; }
.filter-group label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.4px; }
.filter-input {
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 6px; color: var(--text); padding: 8px 12px;
  outline: none; min-width: 140px;
  transition: border-color var(--trans);
}
.filter-input:focus { border-color: var(--accent); }

/* Action button group at the end of the filters bar */
.filters-bar-actions {
  display: flex; flex-wrap: wrap; gap: 8px;
  align-items: center; margin-left: auto;
}

.pagination { display: flex; justify-content: center; gap: 8px; margin-top: 16px; align-items: center; }
.pagination button {
  background: var(--bg3); border: 1px solid var(--border);
  color: var(--text); border-radius: 6px; padding: 6px 12px;
  font-size: 13px;
}
.pagination button.active { background: var(--accent); border-color: var(--accent); }
.pagination button:disabled { opacity: 0.3; cursor: not-allowed; }

/* ══════════════════════════════════════════════════ BUDGETS ═══ */
.budget-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px,1fr)); gap: 18px; }
.budget-card { background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; display: flex; flex-direction: column; gap: 10px; }
.budget-card-header { display: flex; justify-content: space-between; align-items: center; }
.budget-card-title { font-weight: 600; display: flex; align-items: center; gap: 6px; }
.budget-card-amounts { font-size: 22px; font-weight: 700; }
.budget-card-amounts span { font-size: 13px; font-weight: 400; color: var(--text-muted); }
.budget-sparkline { height: 36px; }

/* ══════════════════════════════════════════════════ TRENDS ═══ */
.trends-table td:last-child, .trends-table th:last-child { text-align: right; }

/* ══════════════════════════════════════════════════ SETTINGS ═══ */
.settings-section { margin-bottom: 32px; }
.settings-section h3 { font-size: 15px; font-weight: 700; margin-bottom: 16px; padding-bottom: 8px; border-bottom: 1px solid var(--border); }
.setting-row { display: flex; justify-content: space-between; align-items: center; padding: 12px 0; border-bottom: 1px solid rgba(46,51,72,0.5); font-size: 14px; }
.setting-row:last-child { border-bottom: none; }
.setting-label { display: flex; flex-direction: column; gap: 3px; }
.setting-label small { color: var(--text-muted); font-size: 12px; }

/* Toggle switch */
.toggle { position: relative; display: inline-block; width: 42px; height: 24px; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute; inset: 0;
  background: var(--bg3); border-radius: 99px;
  transition: background var(--trans); cursor: pointer;
}
.toggle-slider::before {
  content: ''; position: absolute;
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--text-muted);
  left: 3px; top: 3px;
  transition: transform var(--trans), background var(--trans);
}
.toggle input:checked + .toggle-slider { background: rgba(108,99,255,0.3); }
.toggle input:checked + .toggle-slider::before { transform: translateX(18px); background: var(--accent); }

.category-list { display: flex; flex-direction: column; gap: 8px; }
.category-row {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px; background: var(--bg3);
  border-radius: 8px; font-size: 14px;
}
.category-row .cat-swatch {
  width: 14px; height: 14px; border-radius: 3px; flex-shrink: 0;
}
.category-row .cat-name { flex: 1; }
.category-row .cat-type { font-size: 11px; color: var(--text-muted); min-width: 60px; }

/* Sync history */
.sync-log-table { font-size: 13px; }

/* ═══════════════════════════════════════════════════ MODAL ═══ */
#modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  z-index: 500; display: flex; align-items: center; justify-content: center;
}
#modal-box {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 14px; padding: 32px; width: 480px; max-width: 95vw;
  position: relative; max-height: 85vh; overflow-y: auto;
}
#modal-close {
  position: absolute; top: 16px; right: 16px;
  background: none; border: none; color: var(--text-muted);
  font-size: 18px; line-height: 1; cursor: pointer;
}
#modal-close:hover { color: var(--text); }
#modal-content { margin-top: 4px; }
.modal-title { font-size: 18px; font-weight: 700; margin-bottom: 20px; }

.form-group { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.form-group label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.4px; }
.form-input {
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); padding: 10px 14px;
  outline: none; width: 100%;
  transition: border-color var(--trans);
}
.form-input:focus { border-color: var(--accent); }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 8px; }

/* ════════════════════════════════════════════ EMPTY STATE ═══ */
.empty-state {
  text-align: center; padding: 60px 20px; color: var(--text-muted);
}
.empty-state .empty-icon { font-size: 48px; margin-bottom: 16px; }
.empty-state h3 { font-size: 18px; color: var(--text); margin-bottom: 8px; }
.empty-state p  { font-size: 14px; }

/* Mobile sidebar dim overlay — hidden on desktop, activated at ≤900px */
#sidebar-overlay { display: none; }

/* ═══════════════════════════════════════════════ RESPONSIVE ═══ */

/* Tablet & mobile — overlay sidebar */
@media (max-width: 900px) {
  #sidebar {
    position: fixed; top: 0; bottom: 0; left: 0;
    z-index: 200;
    transform: translateX(-100%);
    width: var(--sidebar-w) !important;
    box-shadow: 4px 0 24px rgba(0,0,0,0.5);
  }
  #sidebar.mobile-open { transform: translateX(0); }

  /* Dim overlay behind open sidebar */
  #sidebar-overlay {
    display: block;
    position: fixed; inset: 0; z-index: 199;
    background: rgba(0,0,0,0.5);
    opacity: 0; pointer-events: none;
    transition: opacity 0.25s ease;
  }
  #sidebar-overlay.visible { opacity: 1; pointer-events: auto; }

  .grid-4 { grid-template-columns: 1fr 1fr; }

  /* Touch-friendly nav items */
  .nav-item { padding: 14px 18px; }

  /* Topbar: hide long sync label, keep button */
  .sync-label { display: none; }
  #topbar { padding: 0 14px; gap: 10px; }

  /* Ensure topbar right section doesn't overflow */
  .topbar-right { flex-shrink: 0; }

  /* Bigger sidebar toggle touch target */
  #sidebar-toggle { padding: 10px 14px; min-height: 44px; min-width: 44px; }
}

/* Phone */
@media (max-width: 600px) {
  /* Gate card fills screen on tiny phones */
  #gate-card { width: calc(100vw - 40px); padding: 36px 24px; }

  #view-container { padding: 14px; }
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }

  /* Filters bar: stack everything in one column */
  .filters-bar { flex-direction: column; align-items: stretch; }
  .filters-bar .filter-group { width: 100%; }
  .filter-input { min-width: unset; width: 100%; }

  /* Filter action buttons: full-width row */
  .filters-bar-actions {
    display: flex; flex-wrap: wrap; gap: 8px; width: 100%;
  }
  .filters-bar-actions .btn-primary,
  .filters-bar-actions .btn-ghost { flex: 1; justify-content: center; }

  /* KPI values slightly smaller */
  .kpi-value { font-size: 22px; }

  /* Balance banner: stack vertically */
  .balance-banner { flex-direction: column; align-items: flex-start; gap: 16px; padding: 20px; }
  .balance-breakdown { width: 100%; }

  /* Setting rows: allow wrapping on very small screens */
  .setting-row { flex-wrap: wrap; gap: 10px; }
  .setting-row > * { flex-shrink: 0; }

  /* Setting row action buttons: full width when wrapping */
  .setting-row .btn-primary,
  .setting-row .btn-ghost,
  .setting-row .btn-danger { width: 100%; justify-content: center; }

  /* Multi-button wrappers in setting rows */
  .setting-row-btns { display: flex; flex-wrap: wrap; gap: 8px; width: 100%; }
  .setting-row-btns .btn-primary,
  .setting-row-btns .btn-ghost { flex: 1; min-width: 120px; justify-content: center; }

  /* Toast at bottom center on mobile */
  #toast-container { right: 16px; left: 16px; bottom: 16px; align-items: stretch; }
  .toast { text-align: center; }

  /* Ensure modals don't overflow and form-actions stack */
  #modal-box { padding: 24px 18px; }
  .form-actions { flex-wrap: wrap; }
  .form-actions .btn-primary,
  .form-actions .btn-ghost,
  .form-actions .btn-danger { flex: 1; min-width: 100px; justify-content: center; }

  /* Topbar view title truncation */
  #view-title { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

  /* Touch targets: minimum 44px height for all interactive elements */
  .btn-primary, .btn-ghost, .btn-danger,
  #sidebar-toggle, .pagination button,
  .cat-select, .filter-input, .form-input,
  .toggle { min-height: 44px; }

  #gate-form button { padding: 14px; min-height: 44px; }
  #gate-input { padding: 14px 16px; }

  /* Sync button in topbar stays compact */
  #sync-btn { padding: 8px 14px; font-size: 13px; white-space: nowrap; }

  /* Budget card action buttons full-width */
  .budget-card .btn-ghost { width: 100%; text-align: center; justify-content: center; }

  /* Transaction row actions: ensure they're tappable */
  .note-btn, .delete-tx-btn { min-height: 44px; min-width: 44px; }

  /* Category select in transaction table */
  .cat-select { max-width: 100%; width: 100%; }

  /* Keyword rule table action buttons */
  .data-table-wrap td:last-child { white-space: nowrap; }
}

/* ══════════════════════════════════════════════ TOAST (optional) ═══ */
#toast-container { position: fixed; bottom: 24px; right: 24px; display: flex; flex-direction: column; gap: 8px; z-index: 900; }
.toast {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px 18px; font-size: 14px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  animation: toastIn 0.2s ease;
}
.toast.success { border-left: 3px solid var(--green); }
.toast.error   { border-left: 3px solid var(--red); }
.toast.info    { border-left: 3px solid var(--accent); }
@keyframes toastIn { from { opacity:0; transform: translateY(10px); } to { opacity:1; transform: translateY(0); } }
