:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f0f2f7;
  --border: #e3e6ee;
  --text: #1a1f2e;
  --muted: #6b7280;
  --accent: #4f6bff;
  --accent-2: #6c8cff;
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --shadow: 0 1px 2px rgba(16,24,40,.04), 0 1px 3px rgba(16,24,40,.06);
  --radius: 10px;
  --sidebar-w: 232px;
  --chat-w: 340px;
  --topbar-h: 60px;
}

[data-theme="dark"] {
  --bg: #0f131a;
  --surface: #161c26;
  --surface-2: #1d2532;
  --border: #2a3340;
  --text: #e6e8ee;
  --muted: #8a93a4;
  --accent: #6c8cff;
  --accent-2: #8aa4ff;
  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 1px 3px rgba(0,0,0,.4);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

h1, h2, h3 { margin: 0; font-weight: 600; }
button { font-family: inherit; }

/* TOPBAR */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; flex: 1; }
.brand h1 { font-size: 16px; letter-spacing: .2px; }
.logo {
  width: 30px; height: 30px;
  display: inline-grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; border-radius: 8px;
  font-weight: 700;
}
.topbar-actions { display: flex; align-items: center; gap: 8px; }

.currency-pill {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
}
.currency-pill label { color: var(--muted); font-size: 12px; }
.currency-pill select {
  border: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  outline: none;
  cursor: pointer;
  padding: 2px 4px 2px 0;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}
.currency-pill select option {
  background: var(--surface);
  color: var(--text);
}

.icon-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  width: 36px; height: 36px;
  border-radius: 8px;
  display: inline-grid; place-items: center;
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
.icon-btn:hover { background: var(--surface-2); }
.sidebar-toggle { display: none; }

#theme-toggle .icon-moon { display: none; }
[data-theme="dark"] #theme-toggle .icon-sun { display: none; }
[data-theme="dark"] #theme-toggle .icon-moon { display: inline; }

/* LAYOUT */
.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: calc(100vh - var(--topbar-h));
}
@media (min-width: 1100px) {
  .layout { grid-template-columns: var(--sidebar-w) 1fr var(--chat-w); }
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 16px 12px;
  display: flex; flex-direction: column; justify-content: space-between;
  position: sticky; top: var(--topbar-h);
  height: calc(100vh - var(--topbar-h));
}
.sidebar nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  border: none; background: transparent;
  color: var(--muted);
  border-radius: 8px;
  cursor: pointer;
  text-align: left;
  font-size: 14px;
  width: 100%;
  transition: background .15s, color .15s;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
}
.nav-item.active svg { color: #fff; }

.sidebar-footer { display: flex; flex-direction: column; gap: 8px; }

.main {
  padding: 24px;
  max-width: 1280px;
  width: 100%;
}
.view { display: none; }
.view.active { display: block; }
.view-head { margin-bottom: 18px; }
.view-head h2 { font-size: 22px; }
.view-head p { margin: 4px 0 0; }

/* PANELS */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 18px;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 14px;
  gap: 12px;
}
.panel-head h3 { font-size: 14px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.note { margin-top: 10px; }

/* STAT CARDS */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  margin-bottom: 18px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.stat-card.income { border-left-color: var(--success); }
.stat-card.expense { border-left-color: var(--danger); }
.stat-card.savings { border-left-color: var(--accent); }
.stat-card.neutral { border-left-color: var(--muted); }
.stat-label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .4px; }
.stat-value { font-size: 22px; font-weight: 700; margin-top: 4px; }
.stat-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }

/* GRID HELPERS */
.two-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
  margin-bottom: 18px;
}
.two-col .panel { margin-bottom: 0; }

.chart-wrap { position: relative; height: 280px; }

/* TABLES */
.data-table {
  width: 100%;
  border-collapse: collapse;
}
.data-table th, .data-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  font-size: 13px;
}
.data-table th {
  color: var(--muted);
  font-weight: 500;
  background: var(--surface-2);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .3px;
}
.data-table th.num, .data-table td.num { text-align: right; }
.data-table tbody tr:hover { background: var(--surface-2); }
.cat-dot {
  display: inline-block;
  width: 10px; height: 10px;
  border-radius: 50%;
  margin-right: 8px;
  vertical-align: middle;
}
.row-action {
  border: none; background: transparent;
  color: var(--danger); cursor: pointer;
  font-size: 12px;
}
.row-action:hover { text-decoration: underline; }

/* FORMS */
.form-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
}
.form-grid .wide { grid-column: 1 / -1; }
.form-grid label {
  display: flex; flex-direction: column; gap: 6px;
  font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .3px;
}
.form-grid input, .form-grid select {
  font: inherit;
  padding: 9px 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.form-grid input:focus, .form-grid select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79,107,255,.15);
}
.form-actions {
  display: flex; align-items: center; gap: 12px;
  justify-content: flex-end;
}
.check {
  display: inline-flex !important; flex-direction: row !important;
  align-items: center; gap: 8px;
  text-transform: none !important; letter-spacing: 0 !important;
  font-size: 13px !important; color: var(--text) !important;
  margin-right: auto;
}

button.primary, button.ghost, button.danger {
  font: inherit;
  padding: 9px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  transition: background .15s, border-color .15s, transform .05s;
}
button.primary {
  background: var(--accent);
  color: #fff;
  border: 1px solid var(--accent);
}
button.primary:hover { background: var(--accent-2); }
button.ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}
button.ghost:hover { background: var(--surface-2); }
button.danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--border);
}
button.danger:hover { background: rgba(220,38,38,.08); border-color: var(--danger); }
button:active { transform: translateY(1px); }

#export-pdf {
  font: inherit;
  padding: 9px 16px;
  border-radius: 8px;
  cursor: pointer;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}
#export-pdf:hover { background: var(--surface-2); }

/* FILTER ROW */
.filter-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr) auto;
  gap: 12px;
  align-items: end;
}
.filter-row label {
  display: flex; flex-direction: column; gap: 6px;
  font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .3px;
}
.filter-row input, .filter-row select {
  font: inherit;
  padding: 9px 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  outline: none;
}

.row-buttons {
  display: flex; gap: 10px; flex-wrap: wrap;
  align-items: center;
}
.file-btn {
  font: inherit;
  padding: 9px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  display: inline-block;
}
.file-btn:hover { background: var(--surface-2); }

/* ALERTS */
.alerts { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }
.alert {
  padding: 10px 14px;
  border-radius: 8px;
  border: 1px solid;
  font-size: 13px;
  display: flex; align-items: center; gap: 8px;
}
.alert.warn { background: rgba(217,119,6,.1); border-color: var(--warning); color: var(--warning); }
.alert.bad { background: rgba(220,38,38,.1); border-color: var(--danger); color: var(--danger); }

.status-pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 500;
}
.status-pill.ok { background: rgba(22,163,74,.12); color: var(--success); }
.status-pill.warn { background: rgba(217,119,6,.12); color: var(--warning); }
.status-pill.bad { background: rgba(220,38,38,.12); color: var(--danger); }
.status-pill.info { background: rgba(79,107,255,.12); color: var(--accent); }

.data-table tr.section-row td {
  background: var(--surface-2);
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .5px;
  font-weight: 600;
  padding: 8px 12px;
}
.data-table tr.section-row:hover td { background: var(--surface-2); }

/* RANGE SELECT (panel-head dropdown) */
.range-select {
  font: inherit;
  font-size: 12px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 6px;
  outline: none;
  cursor: pointer;
}
.range-select:hover { border-color: var(--accent); }

/* PLAN TOTALS */
.plan-totals {
  display: flex;
  gap: 28px;
  padding: 14px 4px 0;
  margin-top: 14px;
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}
.plan-total-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.plan-total-label {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .4px;
  font-weight: 500;
}
.plan-total-value {
  font-size: 16px;
  font-weight: 600;
}
.plan-total-value .small {
  margin-left: 6px;
  font-weight: 500;
}

/* PLAN ACTION BAR */
.plan-action-bar {
  margin-top: 14px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 12px;
}
#plan-locked {
  display: flex;
  align-items: center;
  gap: 10px;
}
#budgets-panel.locked input,
.form-grid input:disabled {
  background: var(--surface-2);
  color: var(--muted);
  cursor: not-allowed;
}

/* TOAST */
.toast {
  position: fixed;
  bottom: 24px; left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: var(--shadow);
  z-index: 100;
  transition: opacity .2s;
}
.toast.hidden { opacity: 0; pointer-events: none; }

.empty {
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  padding: 18px 0;
}
.hidden { display: none !important; }

input.budget-input {
  width: 110px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  text-align: right;
  font: inherit;
}
.budget-pct {
  display: inline-block;
  margin-left: 6px;
  font-size: 11px;
  color: var(--muted);
  min-width: 50px;
  text-align: right;
}
.cat-delete {
  margin-left: 8px;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  padding: 0 6px;
  border-radius: 4px;
  font-family: inherit;
  vertical-align: middle;
}
.cat-delete:hover,
.cat-delete:focus {
  background: rgba(220,38,38,.1);
  color: var(--danger);
  outline: none;
}
.mode-toggle {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  margin-left: 6px;
  vertical-align: middle;
}
.mode-btn {
  border: none;
  background: var(--surface-2);
  color: var(--muted);
  padding: 5px 9px;
  font-size: 11px;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  font-family: inherit;
}
.mode-btn:not(:last-child) { border-right: 1px solid var(--border); }
.mode-btn.active {
  background: var(--accent);
  color: #fff;
}
.mode-btn:hover:not(.active):not(:disabled) {
  background: var(--surface);
  color: var(--text);
}
.mode-btn:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

/* RESPONSIVE */
@media (max-width: 960px) {
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .two-col { grid-template-columns: 1fr; }
  .filter-row { grid-template-columns: 1fr 1fr; }
  .filter-row button { grid-column: 1 / -1; }
}
@media (max-width: 720px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    top: var(--topbar-h);
    left: 0;
    width: var(--sidebar-w);
    height: calc(100vh - var(--topbar-h));
    transform: translateX(-100%);
    transition: transform .2s;
    z-index: 15;
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-toggle { display: inline-grid; }
  .form-grid { grid-template-columns: 1fr; }
  .stat-grid { grid-template-columns: 1fr 1fr; }
  .main { padding: 16px; }
  .brand h1 { font-size: 14px; }
  .currency-pill label { display: none; }
}

/* CHAT PANEL — desktop right rail / mobile widget */
.chat-panel {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-left: 1px solid var(--border);
  position: sticky;
  top: var(--topbar-h);
  height: calc(100vh - var(--topbar-h));
  overflow: hidden;
}
.chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.chat-title { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.chat-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 0 3px rgba(22, 163, 74, .15);
}
.chat-head-actions { display: flex; gap: 4px; }
.chat-close { display: none; }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.chat-empty {
  color: var(--muted);
  font-size: 13px;
  padding: 8px 4px;
}
.chat-empty p { margin: 0 0 8px; }
.chat-empty ul { margin: 0; padding-left: 18px; }
.chat-empty li { margin: 4px 0; }

.chat-msg { display: flex; }
.chat-msg.user { justify-content: flex-end; }
.chat-bubble {
  max-width: 85%;
  padding: 8px 12px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.chat-msg.user .chat-bubble {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.chat-msg.assistant .chat-bubble {
  background: var(--surface-2);
  color: var(--text);
  border-bottom-left-radius: 4px;
}
.chat-msg.error .chat-bubble {
  background: rgba(220, 38, 38, .12);
  color: var(--danger);
  border: 1px solid rgba(220, 38, 38, .25);
}
.chat-msg .proposal-actions {
  margin-top: 8px;
  display: flex;
  gap: 6px;
}
.chat-msg .proposal-actions button {
  padding: 6px 10px;
  font-size: 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.chat-msg .proposal-actions button.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.chat-typing {
  display: inline-flex;
  gap: 3px;
  padding: 4px 0;
}
.chat-typing span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--muted);
  animation: chat-bounce 1.2s infinite ease-in-out;
}
.chat-typing span:nth-child(2) { animation-delay: .15s; }
.chat-typing span:nth-child(3) { animation-delay: .3s; }
@keyframes chat-bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: .5; }
  40% { transform: translateY(-4px); opacity: 1; }
}

.chat-input-row {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}
.chat-input-row textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-family: inherit;
  font-size: 13px;
  background: var(--surface);
  color: var(--text);
  max-height: 120px;
  line-height: 1.4;
}
.chat-input-row textarea:focus {
  outline: none;
  border-color: var(--accent);
}
.chat-send {
  width: 38px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}
.chat-send:disabled { opacity: .5; cursor: not-allowed; }

/* Floating action button — only shown on narrow screens */
.chat-fab {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  display: none;
  place-items: center;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(16, 24, 40, .18);
  z-index: 20;
}
.chat-fab:hover { background: var(--accent-2); }

/* Below the desktop breakpoint, chat is hidden by default and toggled via FAB */
@media (max-width: 1099px) {
  .chat-panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(420px, 100vw);
    height: 100vh;
    z-index: 25;
    transform: translateX(100%);
    transition: transform .25s ease;
    border-left: 1px solid var(--border);
    box-shadow: -4px 0 20px rgba(0, 0, 0, .12);
  }
  .chat-panel.open { transform: translateX(0); }
  .chat-close { display: inline-grid; }
  .chat-fab { display: grid; }
}
@media (max-width: 480px) {
  .chat-panel { width: 100vw; }
}

