/* =========================================================
   Life OS — finance.css
   Finance module: tabs, dashboard boxes, charts, money table,
   budgets. Chart colors are tested color-blind-safe pairs.
   ========================================================= */
:root {
  --fin-in: #2a78d6;
  --fin-out: #eb6834;
  --fin-good-text: #006300;
  --fin-bad-text: #c4302f;
  --st-good: #0ca30c;
  --st-warn: #fab219;
  --st-bad: #d03b3b;
}
:root[data-theme="dark"] {
  --fin-in: #3987e5;
  --fin-out: #d95926;
  --fin-good-text: #0ca30c;
  --fin-bad-text: #e66767;
}

/* ---------- Tabs ---------- */
.fin-tabs {
  display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin: -8px 0 20px;
  overflow-x: auto; scrollbar-width: none;
}
.fin-tabs::-webkit-scrollbar { display: none; }
.fin-tab {
  flex: 0 0 auto; padding: 8px 12px; font-size: 0.9rem; color: var(--text-muted); text-decoration: none;
  border-bottom: 2px solid transparent; margin-bottom: -1px; border-radius: var(--radius) var(--radius) 0 0;
  white-space: nowrap;
}
.fin-tab:hover { color: var(--text); background: var(--hover); }
.fin-tab.active { color: var(--text); font-weight: 600; border-bottom-color: var(--accent); }
.fin-body > .section:first-child { margin-top: 0; }

/* ---------- Month picker ---------- */
.fin-month { display: flex; align-items: center; gap: 4px; margin-bottom: 14px; }
.fin-month .icon-btn { font-size: 1.3rem; line-height: 1; }
.fin-month-label { font-weight: 600; min-width: 132px; text-align: center; }
.fin-month .btn-small { margin: 0 0 0 6px; }

/* ---------- Cards and text ---------- */
.fin-card { border: 1px solid var(--border); border-radius: 12px; background: var(--panel); padding: 14px 16px; }
.fin-card-title { font-size: 0.82rem; font-weight: 600; color: var(--text-muted); margin-bottom: 6px; }
.fin-muted { color: var(--text-muted); font-size: 0.85rem; }
.fin-big { font-size: 1.6rem; font-weight: 600; line-height: 1.2; margin-bottom: 8px; }
.fin-big .fin-muted { font-size: 0.9rem; font-weight: 400; }
.fin-link { display: inline-block; margin-top: 8px; font-size: 0.85rem; color: var(--accent); text-decoration: none; }
.fin-link:hover { text-decoration: underline; }
.fin-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; flex: 0 0 auto; }
.fin-empty { text-align: center; padding: 32px 20px; }
.fin-empty-icon { font-size: 2.4rem; }
.fin-empty p { max-width: 420px; margin: 6px auto 14px; }

/* ---------- Summary boxes ---------- */
.fin-tiles { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; margin-bottom: 12px; }
.fin-tile { border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; background: var(--panel); min-width: 0; }
.fin-tile-label { font-size: 0.8rem; color: var(--text-muted); }
.fin-tile-value { font-size: 1.35rem; font-weight: 600; line-height: 1.3; margin: 2px 0; overflow-wrap: anywhere; }
.fin-delta { font-size: 0.75rem; color: var(--text-faint); }
.fin-delta.is-good { color: var(--fin-good-text); }
.fin-delta.is-bad { color: var(--fin-bad-text); }

/* ---------- Budget meter ---------- */
.fin-meter { --st: var(--st-good); height: 8px; border-radius: 999px; overflow: hidden; margin: 4px 0 6px;
  background: color-mix(in srgb, var(--st) 18%, transparent); }
.fin-meter.st-warn { --st: var(--st-warn); }
.fin-meter.st-bad { --st: var(--st-bad); }
.fin-meter-fill { display: block; height: 100%; border-radius: 999px; background: var(--st); transition: width 0.3s var(--ease); }
.fin-warn-list { margin: 4px 0 0; padding-left: 0; list-style: none; font-size: 0.88rem; }
.fin-warn-list li { padding: 2px 0; }

/* ---------- Charts ---------- */
.fin-chart-top { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 6px; }
.fin-legend { display: flex; gap: 14px; font-size: 0.82rem; color: var(--text-muted); }
.fin-legend > span { display: inline-flex; align-items: center; gap: 6px; }
.fin-key { display: inline-block; width: 10px; height: 10px; border-radius: 3px; flex: 0 0 auto; }
.fin-key-in { background: var(--fin-in); }
.fin-key-out { background: var(--fin-out); }
.fin-toggle { font-size: 0.8rem; }
.fin-plot { position: relative; width: 100%; min-height: 220px; }
.fin-svg { display: block; overflow: visible; }
.fin-grid { stroke: var(--border); stroke-width: 1; }
.fin-axis { stroke: var(--text-faint); stroke-width: 1; opacity: 0.6; }
.fin-tick { fill: var(--text-faint); font-size: 11px; font-variant-numeric: tabular-nums; }
.fin-tick-now { fill: var(--text); font-weight: 600; }
.fin-bar-in { fill: var(--fin-in); }
.fin-bar-out { fill: var(--fin-out); }
.fin-hl { fill: transparent; }
.fin-hl.on { fill: var(--hover); }
.fin-hit { fill: transparent; cursor: default; outline: none; }
.fin-tip {
  position: absolute; top: 4px; z-index: 2; pointer-events: none; min-width: 150px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow);
  padding: 8px 10px; font-size: 0.8rem;
}
.fin-tip[hidden] { display: none; }
.fin-tip-title { font-weight: 600; margin-bottom: 4px; }
.fin-tip-row { display: flex; align-items: center; gap: 6px; color: var(--text-muted); }
.fin-tip-row b { margin-left: auto; color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.fin-tip-net { border-top: 1px solid var(--border); margin-top: 4px; padding-top: 4px; }

/* Spending by category (bars) */
.fin-cats { display: flex; flex-direction: column; gap: 2px; padding: 8px; }
.fin-cat-row {
  display: grid; grid-template-columns: minmax(150px, 1.2fr) 2fr 130px; align-items: center; gap: 12px;
  border: 0; background: transparent; text-align: left; padding: 6px 8px; border-radius: var(--radius); cursor: pointer; color: var(--text);
}
.fin-cat-row:hover { background: var(--hover); }
.fin-cat-name { display: flex; align-items: center; gap: 8px; font-size: 0.9rem; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fin-cat-track { height: 10px; display: block; }
.fin-cat-fill { display: block; height: 100%; background: var(--fin-out); border-radius: 0 4px 4px 0; }
.fin-cat-val { font-size: 0.85rem; font-variant-numeric: tabular-nums; white-space: nowrap; text-align: right; }

/* Latest entries */
.fin-recent { display: flex; flex-direction: column; }
.fin-recent-row {
  display: flex; align-items: center; gap: 12px; border: 0; background: transparent; text-align: left; cursor: pointer;
  padding: 7px 8px; border-radius: var(--radius); color: var(--text); font-size: 0.92rem;
}
.fin-recent-row:hover { background: var(--hover); }
.fin-recent-date { color: var(--text-faint); font-size: 0.8rem; width: 52px; flex: 0 0 auto; }
.fin-recent-desc { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fin-amt { font-variant-numeric: tabular-nums; white-space: nowrap; }
.fin-amt-in { color: var(--fin-good-text); }

/* ---------- Add form ---------- */
.fin-add { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }
.fin-add-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.fin-add-row > .pop-input, .fin-add-row > .pop-select { width: auto; }
.fin-add .fin-seg { flex: 0 0 auto; }
.fin-add input[type="date"] { flex: 0 0 auto; }
.fin-add-desc { flex: 2 1 220px; }
.fin-amount-box {
  display: inline-flex; align-items: center; gap: 4px; flex: 1 1 120px;
  border: 1px solid var(--border); border-radius: var(--radius); padding: 0 8px; background: var(--bg);
}
.fin-amount-box:focus-within { border-color: var(--accent); }
.fin-amount-box .pop-input { border: 0; padding-left: 0; padding-right: 0; background: transparent; outline: none; min-width: 0; flex: 1; }
.fin-sym { color: var(--text-muted); font-size: 0.9rem; }
.fin-add-row > .pop-select { flex: 1 1 160px; }
.fin-add-account { flex: 1 1 140px; }
.fin-add-btn { flex: 0 0 auto; }
.fin-seg .seg-btn { white-space: nowrap; padding: 5px 12px; }

/* ---------- Money table ---------- */
.fin-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; }
.fin-toolbar .fin-month { margin-bottom: 0; }
.fin-filters { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.fin-filters .pop-select, .fin-filters .pop-input { width: auto; }
.fin-search { width: 160px !important; }
.fin-table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 12px; background: var(--panel); }
.fin-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.fin-table th, .fin-table td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
.fin-table tbody tr:last-child td { border-bottom: 0; }
.fin-table th { font-size: 0.75rem; font-weight: 600; color: var(--text-muted); background: var(--bg-soft); }
.fin-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.fin-sort { border: 0; background: transparent; padding: 0; font: inherit; color: inherit; cursor: pointer; }
.fin-sort.on { color: var(--text); }
.fin-row { cursor: pointer; }
.fin-row:hover td { background: var(--hover); }
.fin-col-date { color: var(--text-muted); }
.fin-desc-btn {
  border: 0; background: transparent; padding: 0; text-align: left; cursor: pointer; color: var(--text);
  max-width: 280px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block;
}
.fin-cat-chip { display: inline-flex; align-items: center; gap: 6px; }
.fin-cat-label { margin-left: -2px; }
.fin-foot { display: flex; gap: 16px; flex-wrap: wrap; justify-content: flex-end; font-size: 0.82rem; color: var(--text-muted); padding: 8px 4px; }
.fin-foot b { color: var(--text); font-variant-numeric: tabular-nums; }
.fin-editor { width: 280px; }
.fin-editor .fin-seg { width: 100%; }
.fin-editor .fin-seg .seg-btn { flex: 1; }
.fin-del { margin-left: auto; width: auto; }

/* ---------- Budgets ---------- */
.fin-blist { padding: 6px 8px; display: flex; flex-direction: column; }
.fin-brow {
  display: grid; grid-template-columns: minmax(140px, 1fr) 170px minmax(140px, 1.3fr) 30px; gap: 12px; align-items: center;
  padding: 8px 4px; border-bottom: 1px solid var(--border);
}
.fin-brow-in { grid-template-columns: minmax(140px, 1fr) minmax(140px, 1fr) 30px; }
.fin-brow-name { display: flex; align-items: center; gap: 8px; min-width: 0; font-size: 0.92rem; }
.fin-brow-name span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fin-budget-box { flex: none; }
.fin-budget-input { width: 70px !important; }
.fin-budget-input.invalid { color: var(--danger); }
.fin-per { font-size: 0.75rem; white-space: nowrap; }
.fin-brow-status .fin-meter { margin: 0 0 3px; }
.fin-brow-text { font-size: 0.8rem; }
.fin-addcat { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; padding: 10px 4px 4px; }
.fin-addcat .pop-input { flex: 1; min-width: 160px; }
.fin-addcat .pop-error { flex-basis: 100%; }
.fin-icon-input { width: 56px; text-align: center; }
.fin-body .section { margin-top: 28px; }

/* ---------- Shared rows ---------- */
.fin-card-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.fin-card-row .pin-btn { flex: 0 0 auto; }
.fin-nav-row { align-items: center; margin-bottom: 14px; }
.fin-nav-row .fin-month { margin-bottom: 0; }
.fin-add-open { margin: 14px 0 4px; }
.fin-inline-label { display: inline-flex; align-items: center; gap: 6px; font-size: 0.82rem; color: var(--text-muted); flex-wrap: wrap; }
.fin-inline-label .fin-amount-box { flex: 0 1 140px; }
.fin-add .fin-icon-input { flex: 0 0 56px; }
.fin-overdue { color: var(--fin-bad-text); font-weight: 600; }
.fin-due-now { color: var(--text); font-weight: 600; }

/* ---------- Bills ---------- */
.fin-bill {
  display: grid; grid-template-columns: minmax(0, 1fr) auto auto 30px; gap: 12px; align-items: center;
  padding: 8px 4px; border-bottom: 1px solid var(--border);
}
.fin-bill:last-child, .fin-brow:last-child { border-bottom: 0; }
.fin-bill.overdue { box-shadow: inset 3px 0 0 var(--st-bad); padding-left: 10px; }
.fin-bill-name { font-size: 0.95rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fin-bill-sub { font-size: 0.78rem; color: var(--text-muted); }
.fin-bill-amt { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.fin-paid { white-space: nowrap; }

/* ---------- Goals ---------- */
.fin-goals { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; margin-top: 14px; }
.fin-goal { display: flex; flex-direction: column; gap: 4px; }
.fin-goal .btn { align-self: flex-start; margin-top: 6px; }
.fin-goal.done { border-color: color-mix(in srgb, var(--st-good) 45%, var(--border)); }
.fin-goal-head { display: flex; align-items: center; gap: 8px; }
.fin-goal-icon { font-size: 1.4rem; line-height: 1; }
.fin-goal-name { flex: 1; font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fin-meter.st-goal { --st: var(--accent); }
.fin-goal .fin-big { margin-bottom: 2px; font-size: 1.35rem; }

/* ---------- Home cards ---------- */
.fin-card-line { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.fin-card-line b { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.fin-card-line > span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fin-card-big { font-size: 1.25rem; font-weight: 600; margin: 2px 0 4px; }
.card-body .fin-meter { margin: 2px 0 6px; height: 6px; }

/* ---------- Net worth ---------- */
.fin-tiles-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.fin-line { fill: none; stroke: var(--fin-in); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.fin-area { fill: var(--fin-in); opacity: 0.1; }
.fin-end-dot { fill: var(--fin-in); stroke: var(--panel); stroke-width: 2; }
.fin-end-label { fill: var(--text); font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums; }
.fin-cross { stroke: var(--text-faint); stroke-width: 1; }
.fin-brow-acct { grid-template-columns: minmax(140px, 1fr) minmax(90px, 0.6fr) 150px 30px; }
.fin-acct-type { font-size: 0.8rem; }
.fin-past { display: flex; flex-direction: column; max-height: 150px; overflow-y: auto; }
.fin-past-row { display: grid; grid-template-columns: 1fr auto 24px; gap: 8px; align-items: center; font-size: 0.85rem; padding: 2px 0; }
.fin-past-row b { font-weight: 600; font-variant-numeric: tabular-nums; }
.fin-past-x { width: 24px; height: 24px; }
.fin-past-add input[type="month"] { width: 130px; flex: 0 0 auto; }
.fin-past-add .pop-input { min-width: 0; }
.fin-note { margin-top: 12px; }

/* ---------- Bank file import / export ---------- */
.fin-io { display: flex; gap: 8px; flex-wrap: wrap; margin: -6px 0 16px; }
.fin-io .btn-small { margin: 0; }
.overlay-box:has(.fin-import) { width: min(720px, calc(100vw - 24px)); }
.fin-import { display: flex; flex-direction: column; min-height: 0; }
.fin-import-head { padding: 12px 16px 8px; border-bottom: 1px solid var(--border); }
.fin-import-head .quick-title { padding: 0; font-size: 1rem; color: var(--text); }
.fin-import-body { overflow-y: auto; padding: 12px 16px; display: flex; flex-direction: column; gap: 12px; min-height: 0; }
.fin-import-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 14px; align-items: end; }
.fin-import-full { grid-column: 1 / -1; }
.fin-import-grid .pop-select, .fin-import-grid .pop-input { width: 100%; }
.fin-import-sum { font-size: 0.85rem; color: var(--text-muted); }
.fin-import-foot { display: flex; justify-content: flex-end; align-items: center; gap: 14px; padding: 10px 16px; border-top: 1px solid var(--border); }

/* Bills on the Calendar */
.cal-due.cal-bill { text-decoration: none; color: var(--text); box-shadow: inset 2px 0 0 var(--fin-out); }
.cal-due.cal-bill:hover { background: var(--hover); }

/* ---------- Phones ---------- */
@media (max-width: 760px) {
  .fin-tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .fin-col-account { display: none; }
  .fin-brow { grid-template-columns: 1fr auto 30px; }
  .fin-brow .fin-brow-status { grid-column: 1 / -1; grid-row: 2; }
  .fin-cat-row { grid-template-columns: minmax(0, 1fr) auto; row-gap: 4px; }
  .fin-cat-track { grid-column: 1 / -1; grid-row: 2; }
  .fin-toolbar { flex-direction: column; align-items: stretch; }
  .fin-search { width: auto !important; flex: 1; }
  .fin-bill { grid-template-columns: minmax(0, 1fr) auto 30px; }
  .fin-bill .fin-paid { grid-column: 2; grid-row: 2; }
  .fin-nav-row { flex-wrap: wrap; }
  .fin-tiles-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .fin-tiles-3 .fin-tile:last-child { grid-column: 1 / -1; }
  .fin-brow-acct { grid-template-columns: 1fr auto 30px; }
  .fin-brow-acct .fin-acct-type { grid-column: 1; grid-row: 2; margin-top: -8px; }
  .fin-import-grid { grid-template-columns: 1fr; }
}
@media (max-width: 420px) {
  .fin-tile-value { font-size: 1.15rem; }
  .fin-desc-btn { max-width: 130px; }
  .fin-cat-label, .fin-entries .fin-dot, .fin-per { display: none; }
  .fin-budget-input { width: 60px !important; }
  .fin-table th, .fin-table td { padding: 8px; }
}
