:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-2: #f8f9f7;
  --border: #e4e6e0;
  --text: #16181a;
  --text-2: #52514e;
  --muted: #7a7a74;
  --brand: #0f7a4a;
  --brand-dark: #0b6239;
  --brand-soft: #e5f3ec;
  --income: #2a78d6;
  --expense: #eb6834;
  --good: #137a3f;   --good-bg: #e3f4e8;
  --warn: #9a5b00;   --warn-bg: #fdf0d5;
  --bad: #b42323;    --bad-bg: #fbe4e4;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 16, .05), 0 1px 8px rgba(16, 24, 16, .04);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
* { box-sizing: border-box; }
body { margin: 0; font-size: 15px; line-height: 1.45; }
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h2 { font-size: 1.35rem; }
h3 { font-size: 1rem; }
a { color: var(--brand); }
.muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }

/* ---------- Buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: 8px; font: inherit; font-weight: 550; cursor: pointer;
  white-space: nowrap; transition: background .12s, border-color .12s;
}
.btn:hover { background: var(--surface-2); border-color: #cfd2ca; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); border-color: var(--brand-dark); }
.btn-danger { color: var(--bad); }
.btn-danger:hover { background: var(--bad-bg); border-color: #efc4c4; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--text-2); }
.btn-sm { padding: 4px 10px; font-size: 13px; border-radius: 7px; }
.btn-block { width: 100%; }
.icon-btn {
  border: 0; background: transparent; font-size: 20px; line-height: 1; padding: 4px 8px;
  border-radius: 6px; cursor: pointer; color: var(--text-2);
}
.icon-btn:hover { background: var(--surface-2); }

label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; font-weight: 600; color: var(--text-2); }
input, select, textarea {
  font: inherit; font-size: 15px; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 9px 11px; width: 100%;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--brand) 45%, transparent); outline-offset: 1px; border-color: var(--brand);
}
textarea { resize: vertical; min-height: 64px; }
.checkbox { flex-direction: row; align-items: center; gap: 8px; font-weight: 500; color: var(--text); }
.checkbox input { width: 18px; height: 18px; }

/* ---------- Login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px;
  background: radial-gradient(circle at 20% 10%, var(--brand-soft), var(--bg) 60%); }
.login-card { width: 100%; max-width: 360px; background: var(--surface); padding: 32px 28px; border-radius: 16px;
  box-shadow: 0 10px 40px rgba(16, 40, 24, .1); display: flex; flex-direction: column; gap: 14px; text-align: center; }
.login-card label { text-align: left; }
.login-card .logo { font-size: 40px; }
.form-error { color: var(--bad); font-size: 14px; margin: 0; min-height: 1em; }

/* ---------- Layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10; background: var(--surface); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 20px; padding: 10px 32px; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.logo-sm { font-size: 22px; }
.tabs { display: flex; gap: 4px; }
.tabs a {
  text-decoration: none; color: var(--text-2); font-weight: 600; padding: 7px 14px; border-radius: 8px;
}
.tabs a:hover { background: var(--surface-2); }
.tabs a.active { background: var(--brand-soft); color: var(--brand-dark); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.month-picker { display: flex; align-items: center; border: 1px solid var(--border); border-radius: 9px; padding: 2px; }
.month-picker input { border: 0; padding: 5px 4px; width: auto; font-weight: 600; }
.month-picker input:focus { outline: none; }

.content { max-width: 1920px; margin: 0 auto; padding: 24px 32px; display: flex; flex-direction: column; gap: 20px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 16px 18px 6px; }
.card-body { padding: 12px 18px 18px; }
.grid-2 { display: grid; grid-template-columns: 1.4fr 1fr; gap: 20px; }

/* ---------- Stat tiles ---------- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
.stat { padding: 16px 18px; }
.stat .label { font-size: 13px; color: var(--text-2); font-weight: 600; display: flex; align-items: center; gap: 6px; }
.stat .value { font-size: 1.65rem; font-weight: 700; margin-top: 6px; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.stat .sub { font-size: 13px; color: var(--muted); margin-top: 2px; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.value.neg { color: var(--bad); }

.status-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
.mini { padding: 14px 18px; display: flex; align-items: center; justify-content: space-between; text-decoration: none; color: inherit; }
a.mini:hover { border-color: #cfd2ca; }
.mini .big { font-size: 1.4rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.progress { height: 8px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 99px; overflow: hidden; margin-top: 10px; }
.progress > span { display: block; height: 100%; background: var(--brand); border-radius: 99px; }

/* ---------- Badges ---------- */
.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 12.5px; font-weight: 650;
  padding: 3px 9px; border-radius: 99px; white-space: nowrap; }
.badge .half { width: 9px; height: 9px; border-radius: 50%; border: 1.5px solid currentColor;
  background: linear-gradient(90deg, currentColor 50%, transparent 50%); display: inline-block; }
.badge.paid { background: var(--good-bg); color: var(--good); }
.badge.partial { background: var(--warn-bg); color: var(--warn); }
.badge.unpaid { background: var(--bad-bg); color: var(--bad); }
.badge.inactive, .badge.na { background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 11px 14px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 650; background: var(--surface-2); }
th.num { text-align: right; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #fafbf9; }
tr.dim td { color: var(--muted); }
td .name { font-weight: 600; }
td .sub { font-size: 12.5px; color: var(--muted); }
.actions { display: flex; gap: 6px; justify-content: flex-end; }
tfoot td { font-weight: 700; background: var(--surface-2); border-top: 1px solid var(--border); border-bottom: 0; }

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.toolbar input[type=search] { max-width: 280px; }
.toolbar select { width: auto; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--border); background: var(--surface); border-radius: 99px; padding: 5px 12px; font: inherit;
  font-size: 13px; font-weight: 600; color: var(--text-2); cursor: pointer; }
.chip.active { background: var(--text); color: #fff; border-color: var(--text); }
.chip .n { opacity: .65; margin-left: 3px; }

.empty { text-align: center; padding: 36px 18px; color: var(--muted); }
.empty .big { font-size: 32px; }

/* ---------- Lists ---------- */
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.list li:last-child { border-bottom: 0; }
.list .meta { font-size: 12.5px; color: var(--muted); }

.bars { display: flex; flex-direction: column; gap: 10px; }
.bar-row .top { display: flex; justify-content: space-between; font-size: 14px; margin-bottom: 4px; }
.bar-track { height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--expense); border-radius: 4px; }

/* ---------- Chart ---------- */
.chart { position: relative; }
.chart svg { width: 100%; height: 240px; display: block; }
.chart .gridline { stroke: var(--border); stroke-width: 1; }
.chart .axis-label { fill: var(--muted); font-size: 11px; }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover { fill: rgba(0, 0, 0, .035); }
.chart .tip {
  position: absolute; pointer-events: none; background: var(--text); color: #fff; border-radius: 8px; padding: 8px 10px;
  font-size: 12.5px; white-space: nowrap; box-shadow: 0 4px 14px rgba(0, 0, 0, .18);
}
.chart .tip .row { display: flex; align-items: center; gap: 6px; }
.legend { display: flex; gap: 14px; font-size: 13px; color: var(--text-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
details.table-view { margin-top: 10px; font-size: 13px; }
details.table-view summary { cursor: pointer; color: var(--text-2); }
details.table-view table { margin-top: 8px; font-size: 13px; }
details.table-view td, details.table-view th { padding: 6px 10px; }

/* ---------- Modal ---------- */
dialog {
  border: 0; border-radius: 16px; padding: 0; width: min(520px, calc(100vw - 24px)); max-height: calc(100vh - 40px);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25); color: var(--text);
}
dialog::backdrop { background: rgba(20, 24, 20, .45); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 18px 22px 0; }
.modal-form { padding: 16px 22px 22px; display: flex; flex-direction: column; gap: 14px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 4px; }
.modal-actions .left { margin-right: auto; }
.hint { font-size: 12.5px; color: var(--muted); font-weight: 400; }
.summary-box { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; font-size: 14px;
  display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.history { border-top: 1px solid var(--border); padding: 14px 22px 20px; }
.history h3 { margin-bottom: 6px; }

/* ---------- Toast ---------- */
#toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px); opacity: 0;
  background: var(--text); color: #fff; padding: 10px 18px; border-radius: 10px; font-weight: 550; font-size: 14px;
  transition: opacity .2s, transform .2s; pointer-events: none; z-index: 100;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { background: var(--bad); }

/* ---------- Responsive ---------- */
@media (max-width: 960px) {
  .stats, .status-row { grid-template-columns: repeat(2, 1fr); }
  .grid-2 { grid-template-columns: 1fr; }
}
@media (max-width: 720px) {
  .topbar { position: static; padding: 10px 14px; gap: 10px; }
  .brand { flex: 1; }
  .topbar-right { margin-left: 0; }
  /* Students table becomes stacked cards */
  table.stack-sm thead { display: none; }
  table.stack-sm tbody tr { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 4px 10px; padding: 12px 14px; border-bottom: 1px solid var(--border); }
  table.stack-sm tbody td { padding: 0; border: 0; }
  table.stack-sm .cell-name { grid-row: 1; grid-column: 1 / 3; }
  table.stack-sm .cell-status { grid-row: 1; grid-column: 3; text-align: right; }
  table.stack-sm tfoot td[data-label]::before { content: attr(data-label); display: block; font-size: 11px; color: var(--muted); }
  table.stack-sm td[data-label] { text-align: left; font-size: 14px; }
  table.stack-sm td[data-label]::before { content: attr(data-label); display: block; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 650; }
  table.stack-sm .cell-actions { grid-column: 1 / -1; margin-top: 4px; }
  table.stack-sm .cell-actions .actions { justify-content: flex-start; }
  table.stack-sm tfoot tr { display: grid; grid-template-columns: 1fr 1fr 1fr; padding: 10px 14px; background: var(--surface-2); }
  table.stack-sm tfoot td { padding: 0; background: none; border: 0; text-align: left; }
  table.stack-sm tfoot td:first-child { grid-column: 1 / -1; }
  table.stack-sm tfoot td:empty { display: none; }
  .tabs { order: 3; width: 100%; }
  .tabs a { flex: 1; text-align: center; padding: 7px 6px; }
  .content { padding: 14px; gap: 14px; }
  .stat .value { font-size: 1.3rem; }
  .hide-sm { display: none; }
  .form-row { grid-template-columns: 1fr; }
  th, td { padding: 10px; }
  .toolbar input[type=search] { max-width: none; }
}
@media print {
  .topbar, .btn, .toolbar, .actions { display: none !important; }
  .card { box-shadow: none; }
}
