/* ── CSS Variables ──────────────────────────────────────────────────────── */
:root {
  --bg:         #0f1117;
  --bg-card:    #1a1f2e;
  --bg-input:   #242938;
  --bg-sidebar: #13161f;
  --accent:     #4f8ef7;
  --accent-dim: rgba(79,142,247,0.15);
  --border:     #2d3344;
  --text:       #e0e6f0;
  --text-dim:   #8892a4;
  --green:      #27ae60;
  --green-bg:   #1e4030;
  --green-dark: #1e7e34;
  --yellow:     #f39c12;
  --yellow-bg:  #3d2f0a;
  --yellow-dark:#856404;
  --red:        #e74c3c;
  --red-bg:     #3d1515;
  --red-dark:   #721c24;
  --radius:     8px;
  --sidebar-w:  320px;
}

/* ── Reset ──────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

/* ── App shell ──────────────────────────────────────────────────────────── */
#app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ── Sidebar ────────────────────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.22s ease;
}
#sidebar.collapsed { width: 0; }

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  white-space: nowrap;
}
.sidebar-title { font-weight: 700; font-size: 1rem; white-space: nowrap; }

#sidebar-body {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px 14px 24px;
  min-width: var(--sidebar-w);
}

/* Sidebar sections */
.sb-section { margin-bottom: 20px; }
.sb-section h3 {
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  margin-bottom: 10px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}

/* Collapsible expander */
.expander { margin-bottom: 8px; }
.expander-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 8px 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 600;
  text-align: left;
  transition: background 0.15s;
}
.expander-toggle:hover { background: #2c3248; }
.expander-toggle .chevron { transition: transform 0.2s; font-style: normal; }
.expander-toggle.open .chevron { transform: rotate(90deg); }
.expander-body {
  display: none;
  padding: 10px 4px 4px;
}
.expander-body.open { display: block; }

/* Label + input */
.field { margin-bottom: 10px; }
.field label {
  display: block;
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-bottom: 4px;
}
.field small {
  display: block;
  font-size: 0.7rem;
  color: var(--text-dim);
  margin-top: 2px;
}
input[type="number"],
input[type="text"],
input[type="date"],
select {
  width: 100%;
  padding: 6px 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.85rem;
  outline: none;
  transition: border-color 0.15s;
}
input[type="number"]:focus,
input[type="text"]:focus,
input[type="date"]:focus,
select:focus {
  border-color: var(--accent);
}
input[type="number"] { -moz-appearance: textfield; }
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

/* Radio group */
.radio-group { display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.radio-group label {
  display: flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  font-size: 0.82rem;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-input);
  transition: border-color 0.15s, background 0.15s;
}
.radio-group input[type="radio"] { width: auto; }
.radio-group label:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-dim);
}

/* Two-column row */
.col2 { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: end; }
.col2 .field:last-child label { text-align: center; font-size: 0.7rem; }

/* Row item (for list items like cc_cards) */
.row-item {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  margin-bottom: 8px;
}
.row-item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  font-size: 0.8rem;
  font-weight: 600;
}
.row-3 { display: grid; grid-template-columns: 1fr 1fr 80px; gap: 8px; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  cursor: pointer;
  font-size: 0.8rem;
  transition: background 0.15s, border-color 0.15s;
}
.btn:hover { background: #2c3248; border-color: var(--accent); }
.btn-sm { padding: 3px 8px; font-size: 0.75rem; }
.btn-danger { border-color: var(--red); color: var(--red); }
.btn-danger:hover { background: var(--red-bg); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: #3a78e0; }
.icon-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 1.3rem;
  line-height: 1;
  padding: 4px 6px;
  border-radius: var(--radius);
  transition: color 0.15s, background 0.15s;
}
.icon-btn:hover { color: var(--text); background: var(--bg-input); }

.add-row-btn { margin-top: 4px; }
.caption { font-size: 0.72rem; color: var(--text-dim); margin-top: 6px; }
.divider { border: none; border-top: 1px solid var(--border); margin: 12px 0; }

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

#top-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--bg);
}
#top-bar h1 { font-size: 1.2rem; font-weight: 700; }
#today-line { font-size: 0.75rem; color: var(--text-dim); margin-top: 1px; }

/* ── Tabs ───────────────────────────────────────────────────────────────── */
#tabs-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#tab-nav {
  display: flex;
  gap: 2px;
  padding: 10px 20px 0;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--bg);
}
.tab-btn {
  padding: 8px 16px;
  background: none;
  border: 1px solid transparent;
  border-bottom: none;
  border-radius: var(--radius) var(--radius) 0 0;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
  transition: color 0.15s, background 0.15s;
  position: relative;
  bottom: -1px;
}
.tab-btn:hover { color: var(--text); background: var(--bg-card); }
.tab-btn.active {
  color: var(--accent);
  background: var(--bg);
  border-color: var(--border);
  border-bottom-color: var(--bg);
}

#tab-panels { flex: 1; overflow: hidden; position: relative; }
.tab-panel {
  display: none;
  height: 100%;
  overflow-y: auto;
  padding: 20px;
}
.tab-panel.active { display: block; }

/* Sub-tabs (within savings) */
.sub-tab-nav {
  display: flex;
  gap: 2px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.sub-tab-btn {
  padding: 6px 14px;
  background: none;
  border: 1px solid transparent;
  border-bottom: none;
  border-radius: var(--radius) var(--radius) 0 0;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 600;
  transition: color 0.15s;
  position: relative;
  bottom: -1px;
}
.sub-tab-btn:hover { color: var(--text); }
.sub-tab-btn.active {
  color: var(--accent);
  border-color: var(--border);
  border-bottom-color: var(--bg-card);
  background: var(--bg-card);
}
.sub-panel { display: none; }
.sub-panel.active { display: block; }

/* ── Tab content layout ─────────────────────────────────────────────────── */
.tab-content h2 {
  font-size: 1.3rem;
  font-weight: 700;
  margin-bottom: 16px;
}
.tab-content h3 {
  font-size: 1rem;
  font-weight: 700;
  margin: 24px 0 12px;
}
.tab-content h4 {
  font-size: 0.9rem;
  font-weight: 600;
  margin: 14px 0 8px;
  color: var(--text-dim);
}

/* Input row */
.input-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}
.input-group label {
  display: block;
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-bottom: 4px;
}
.input-group small {
  display: block;
  font-size: 0.7rem;
  color: var(--text-dim);
  margin-top: 3px;
}
.input-group input, .input-group select {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.9rem;
}
.input-group input:focus, .input-group select:focus { border-color: var(--accent); outline: none; }

/* Info banner */
.info-banner {
  background: rgba(79,142,247,0.1);
  border: 1px solid rgba(79,142,247,0.3);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin-bottom: 16px;
  font-size: 0.82rem;
  color: #adc6ff;
}

/* ── Chart wrapper ──────────────────────────────────────────────────────── */
.chart-wrapper {
  position: relative;
  height: 300px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 20px;
}
.chart-wrapper.tall { height: 380px; }

/* ── Metric tiles ───────────────────────────────────────────────────────── */
.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}
.tile {
  border-radius: var(--radius);
  padding: 14px 12px 12px;
  text-align: center;
  color: #fff;
}
.tile-green  { background: var(--green-dark); }
.tile-yellow { background: var(--yellow-dark); }
.tile-red    { background: var(--red-dark); }
.tile-blue   { background: #1a437e; }
.tile-label  { font-size: 0.65rem; font-weight: 700; text-transform: uppercase;
               letter-spacing: 0.06em; opacity: 0.85; margin-bottom: 6px; }
.tile-value  { font-size: 1.35rem; font-weight: 700; line-height: 1.2; }
.tile-sub    { font-size: 0.65rem; opacity: 0.75; margin-top: 5px; }

/* ── Metrics row ────────────────────────────────────────────────────────── */
.metrics { display: flex; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.metric {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 16px;
  min-width: 130px;
  flex: 1;
}
.metric-label { font-size: 0.7rem; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.05em; }
.metric-value { font-size: 1.1rem; font-weight: 700; margin-top: 2px; }

/* ── Progress bar ───────────────────────────────────────────────────────── */
.progress-bar-wrapper {
  background: var(--bg-input);
  border-radius: 99px;
  height: 16px;
  margin: 8px 0;
  overflow: hidden;
}
.progress-bar-fill {
  height: 100%;
  border-radius: 99px;
  background: linear-gradient(90deg, var(--green) 0%, #2ecc71 100%);
  transition: width 0.4s ease;
}
.progress-label { font-size: 0.75rem; color: var(--text-dim); margin-top: 4px; }

/* ── Ledger table ───────────────────────────────────────────────────────── */
.table-wrapper {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 24px;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}
thead tr {
  background: var(--bg-input);
  border-bottom: 1px solid var(--border);
}
th {
  padding: 8px 12px;
  text-align: left;
  font-weight: 600;
  color: var(--text-dim);
  white-space: nowrap;
}
td {
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--bg-input); }
.tr-today { background: rgba(79,142,247,0.08) !important; }
.tr-today td { border-bottom-color: rgba(79,142,247,0.3); }
td.amt-green { color: var(--green); font-weight: 600; }
td.amt-red   { color: var(--red); font-weight: 600; }
td.amt-gray  { color: var(--text-dim); }
.today-badge {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  font-size: 0.6rem;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 4px;
  margin-left: 4px;
  vertical-align: middle;
}

/* ── Expander (main tab areas) ──────────────────────────────────────────── */
.main-expander { margin-bottom: 12px; }
.main-expander-toggle {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 10px 14px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
  text-align: left;
}
.main-expander-toggle:hover { background: #222840; }
.main-expander-toggle .chevron { transition: transform 0.2s; font-style: normal; }
.main-expander-toggle.open .chevron { transform: rotate(90deg); }
.main-expander-body {
  display: none;
  padding: 14px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  margin-top: -1px;
}
.main-expander-body.open { display: block; }

/* List rows in expander */
.oe-row {
  display: grid;
  grid-template-columns: 2fr 1.2fr 80px auto;
  gap: 8px;
  align-items: end;
  margin-bottom: 8px;
}
.oe-row label { font-size: 0.72rem; color: var(--text-dim); display: block; margin-bottom: 3px; }
.oe-row input { padding: 6px 8px; font-size: 0.82rem; }

/* ── Data table (amortization etc) ─────────────────────────────────────── */
.data-table-wrapper {
  overflow-x: auto;
  overflow-y: auto;
  max-height: 380px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin: 12px 0 20px;
}

/* ── Savings grid ───────────────────────────────────────────────────────── */
.savings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 20px;
}
@media (max-width: 700px) { .savings-grid { grid-template-columns: 1fr; } }

/* ── Scrollbar ──────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #3a4155; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #4f5a75; }

/* ── Range input ────────────────────────────────────────────────────────── */
input[type="range"] {
  width: 100%;
  background: transparent;
  cursor: pointer;
}
