:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-2: #0ea5e9;
  --good: #22c55e;
  --warn: #f59e0b;
  --bad: #ef4444;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
  border-bottom: 1px solid #1e293b;
}

.topbar h1 { margin: 0; font-size: 20px; font-weight: 600; }
.who { color: var(--muted); font-size: 13px; }

.tabs {
  display: flex;
  gap: 4px;
  padding: 0 24px;
  border-bottom: 1px solid #1e293b;
}

.tab {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 12px 18px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: color .2s;
}

.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

main { padding: 16px 24px; }
.tabs { padding: 0 24px; }
.topbar { padding: 16px 24px; }

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

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  background: var(--panel);
  border-radius: var(--radius);
  overflow: hidden;
}

th, td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid #0f172a;
}

th { background: var(--panel-2); font-weight: 600; }
thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  /* Make sure the sticky header has an opaque background so content underneath doesn't bleed through */
  background: var(--panel-2);
  box-shadow: 0 1px 0 #0f172a;
}
tr:last-child td { border-bottom: none; }

/* Default link color inside tables (was unreadable browser default) */
table a { color: var(--accent); text-decoration: none; }
table a:hover { text-decoration: underline; }

.num { text-align: right; font-variant-numeric: tabular-nums; }

.pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
.pill.paid    { background: rgba(34,197,94,.15);  color: var(--good); }
.pill.unpaid  { background: rgba(239,68,68,.15);  color: var(--bad); }

.filters {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 14px;
  padding: 12px 14px;
  background: var(--panel);
  border-radius: var(--radius);
}
.filters label {
  display: flex;
  flex-direction: column;
  font-size: 11px;
  color: var(--muted);
  gap: 3px;
}
.filters select, .filters input {
  background: var(--panel-2);
  border: 1px solid var(--panel-2);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 13px;
  min-width: 130px;
}
.filters button {
  align-self: flex-end;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--panel-2);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 12px;
  cursor: pointer;
}
.filters button:hover { color: var(--text); border-color: var(--accent); }
.filter-summary {
  margin-left: auto;
  color: var(--muted);
  font-size: 12px;
}
.summary-table {
  border-collapse: separate;
  border-spacing: 0;
  overflow: visible;        /* allow sticky thead to anchor to page-scroll viewport */
  border-radius: 0;
}
.summary-table th, .summary-table td { border-bottom: 1px solid #0f172a; }
.summary-table tfoot td {
  position: sticky;
  bottom: 0;
  background: var(--panel-2);
  font-size: 14px;
  box-shadow: 0 -1px 0 #0f172a;
  z-index: 2;
}
.scroll-x { overflow-x: auto; }

.btn-link {
  display: inline-block;
  margin-right: 8px;
  padding: 4px 10px;
  border: 1px solid var(--accent-2);
  border-radius: 6px;
  font-size: 12px;
  color: var(--accent);
  text-decoration: none;
}
.btn-link:hover { background: var(--accent-2); color: white; }
.pivot-table {
  font-size: 12px;
  min-width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  overflow: visible;        /* override base `table { overflow:hidden }` so sticky cells anchor to scroll-x container */
  border-radius: 0;
}
.pivot-table th, .pivot-table td { border-bottom: 1px solid #0f172a; }
.pivot-table th, .pivot-table td { padding: 8px 10px; }
.pivot-table th.line-col,
.pivot-table td.line-col {
  position: sticky; left: 0;
  background: var(--panel-2);
  z-index: 1;
  text-align: left;
  white-space: nowrap;
}
.pivot-table thead th:last-child,
.pivot-table tbody td:last-child,
.pivot-table tfoot td:last-child {
  position: sticky; right: 0;
  background: var(--panel-2);
  z-index: 1;
  box-shadow: -4px 0 8px -4px rgba(0,0,0,.4);
}
.pivot-table thead th.line-col,
.pivot-table thead th:last-child {
  z-index: 3;       /* sticky-corner cells need to win over both row and col stickies */
}
.pivot-table tfoot td.line-col,
.pivot-table tfoot td:last-child {
  z-index: 3;
}
.pivot-table thead th { background: var(--panel-2); white-space: nowrap; }
.pivot-table tbody tr:hover td { background: rgba(56,189,248,.05); }
.pivot-table tbody tr:hover td.line-col,
.pivot-table tbody tr:hover td:last-child {
  background: var(--panel-2);   /* keep sticky cells opaque on hover */
}
.pivot-table .cell-total {
  display: flex; flex-direction: column; align-items: flex-end; gap: 2px;
  font-variant-numeric: tabular-nums;
}
.pivot-table .cell-total > span:first-child {
  font-size: 15px;
  font-weight: 600;
}
.pivot-table .cell-total .delta { font-size: 11px; }
.pivot-table tfoot td {
  background: var(--panel-2);
  font-weight: 600;
}

.delta {
  display: inline-block;
  margin-left: 6px;
  font-size: 11px;
  font-weight: 600;
  padding: 1px 5px;
  border-radius: 4px;
  vertical-align: middle;
}
.delta.up   { color: #fca5a5; background: rgba(239,68,68,.12); }   /* increase = bad */
.delta.down { color: #86efac; background: rgba(34,197,94,.12); }   /* decrease = good */
.delta.flat { color: var(--muted); background: transparent; padding: 0; }

.summary-table .adj {
  color: var(--muted);
  font-size: 11px;
  white-space: pre-line;
  max-width: 240px;
}
.month-block { margin-bottom: 28px; }
.month-block h2 {
  margin: 0 0 8px;
  font-size: 15px;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}
.month-block .total { color: var(--accent); font-weight: 600; }

/* Chat */
.chat-window {
  background: var(--panel);
  border-radius: var(--radius);
  height: 60vh;
  overflow-y: auto;
  padding: 16px;
  margin-bottom: 12px;
}
.msg {
  margin-bottom: 12px;
  display: flex;
  flex-direction: column;
  max-width: 80%;
}
.msg.user      { margin-left: auto;  align-items: flex-end; }
.msg.assistant { margin-right: auto; align-items: flex-start; }

.bubble {
  background: var(--panel-2);
  padding: 10px 14px;
  border-radius: 14px;
  white-space: pre-wrap;
  font-size: 14px;
  line-height: 1.5;
}
.msg.user .bubble { background: var(--accent-2); color: white; }

.chat-form { display: flex; gap: 8px; }
.chat-form input {
  flex: 1;
  padding: 10px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--panel-2);
  background: var(--panel);
  color: var(--text);
  font-size: 14px;
}
.chat-form button {
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--bg);
  font-weight: 600;
  cursor: pointer;
}
.chat-form button:disabled { opacity: 0.5; cursor: not-allowed; }
