:root {
  --red: #a80000;
  --red-dark: #7a0000;
  --red-light: #ce1111;
  --gold: #d4a547;
  --cream: #fafafa;
  --bg: #f5f3ee;
  --ink: #1a1a1a;
  --ink-soft: #5a5a5a;
  --line: #e0dccf;
  --green: #2d7a3e;
  --amber: #c88a1a;
  --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.12);
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body {
  height: 100%;
  overscroll-behavior: contain;
}
body {
  font-family: 'Inter', system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  font-size: 15px;
  padding-bottom: env(safe-area-inset-bottom);
}

/* ===== HEADER ===== */
.app-header {
  background: linear-gradient(135deg, var(--red) 0%, var(--red-dark) 100%);
  color: var(--cream);
  padding: 1rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 50;
  padding-top: calc(1rem + env(safe-area-inset-top));
}
.brand { display: flex; align-items: center; gap: 0.7rem; }
.brand-mark {
  width: 42px;
  height: 42px;
  background: var(--gold);
  color: var(--ink);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Oswald', sans-serif;
  font-size: 1.5rem;
  font-weight: 700;
}
.brand-name {
  font-family: 'Oswald', sans-serif;
  font-size: 1.3rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  line-height: 1.1;
}
.brand-tag {
  font-size: 0.75rem;
  opacity: 0.8;
  letter-spacing: 0.08em;
}
.icon-btn {
  width: 42px;
  height: 42px;
  background: rgba(250,250,250,0.15);
  border: 1px solid rgba(250,250,250,0.25);
  color: var(--cream);
  border-radius: 10px;
  cursor: pointer;
  font-size: 1.3rem;
  transition: background 0.2s;
}
.icon-btn:hover { background: rgba(250,250,250,0.25); }

/* ===== TABS ===== */
.tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: var(--cream);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: calc(74px + env(safe-area-inset-top));
  z-index: 40;
}
.tab-btn {
  padding: 0.9rem 0.3rem;
  background: transparent;
  border: none;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.75rem;
  color: var(--ink-soft);
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  align-items: center;
  border-bottom: 3px solid transparent;
  transition: all 0.2s;
}
.tab-btn span { font-weight: 500; }
.tab-btn:hover { color: var(--red); }
.tab-btn.active {
  color: var(--red);
  border-bottom-color: var(--red);
  background: rgba(168, 0, 0, 0.03);
}

/* ===== MAIN ===== */
.app-main {
  padding: 1rem 1rem 5rem;
  max-width: 800px;
  margin: 0 auto;
}
.tab-panel { display: none; }
.tab-panel.active { display: block; }

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}
.panel-head h2 {
  font-family: 'Oswald', sans-serif;
  font-size: 1.4rem;
  letter-spacing: 0.03em;
}
.section-title {
  font-family: 'Oswald', sans-serif;
  font-size: 1.1rem;
  letter-spacing: 0.05em;
  color: var(--ink-soft);
  text-transform: uppercase;
  margin: 2rem 0 0.8rem;
}

/* ===== MONTH SELECTOR ===== */
.month-selector {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--cream);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.5rem;
  margin-bottom: 1.2rem;
}
.month-selector h2 {
  font-family: 'Oswald', sans-serif;
  font-size: 1.1rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.month-nav {
  width: 40px;
  height: 40px;
  background: transparent;
  border: none;
  font-size: 1.5rem;
  color: var(--red);
  cursor: pointer;
  border-radius: 6px;
  font-family: inherit;
}
.month-nav:hover { background: rgba(168,0,0,0.08); }

/* ===== SUMMARY ===== */
.summary-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.7rem;
  margin-bottom: 1rem;
}
.summary-card {
  background: var(--cream);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 1rem 0.7rem;
  text-align: center;
}
.summary-paid { border-left: 4px solid var(--green); }
.summary-pending { border-left: 4px solid var(--amber); }
.summary-total { border-left: 4px solid var(--red); }
.summary-label {
  font-size: 0.7rem;
  letter-spacing: 0.1em;
  color: var(--ink-soft);
  text-transform: uppercase;
  margin-bottom: 0.3rem;
}
.summary-value {
  font-family: 'Oswald', sans-serif;
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--ink);
  line-height: 1;
}
.summary-paid .summary-value { color: var(--green); }
.summary-pending .summary-value { color: var(--amber); }
.summary-total .summary-value { color: var(--red); }
.summary-sub {
  font-size: 0.75rem;
  color: var(--ink-soft);
  margin-top: 0.3rem;
}

/* ===== DASH GROUPS ===== */
.dash-groups { display: flex; flex-direction: column; gap: 0.6rem; }
.dash-group-row {
  background: var(--cream);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.8rem 1rem;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.5rem;
  align-items: center;
}
.dash-group-name { font-weight: 600; }
.dash-group-stats {
  font-size: 0.85rem;
  color: var(--ink-soft);
}
.dash-group-progress {
  grid-column: 1 / -1;
  height: 6px;
  background: var(--line);
  border-radius: 3px;
  overflow: hidden;
  margin-top: 0.3rem;
}
.dash-group-progress-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--green), var(--gold));
  transition: width 0.3s;
}

/* ===== UNPAID LIST ===== */
.unpaid-list { display: flex; flex-direction: column; gap: 0.5rem; }
.unpaid-row {
  background: var(--cream);
  border: 1px solid var(--line);
  border-left: 4px solid var(--amber);
  border-radius: 8px;
  padding: 0.7rem 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}
.unpaid-row-name { font-weight: 500; }
.unpaid-row-group { font-size: 0.8rem; color: var(--ink-soft); }
.unpaid-amount {
  font-family: 'Oswald', sans-serif;
  font-weight: 600;
  color: var(--amber);
}

/* ===== CARDS LIST (groups + students) ===== */
.cards-list { display: flex; flex-direction: column; gap: 0.7rem; }
.card-row {
  background: var(--cream);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 1rem 1.1rem;
  cursor: pointer;
  transition: all 0.2s;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.3rem;
  align-items: center;
}
.card-row:hover {
  border-color: var(--red);
  box-shadow: var(--shadow);
  transform: translateX(2px);
}
.card-title {
  font-weight: 600;
  font-size: 1.05rem;
}
.card-sub {
  font-size: 0.85rem;
  color: var(--ink-soft);
}
.card-meta {
  font-family: 'Oswald', sans-serif;
  color: var(--red);
  font-weight: 600;
  font-size: 1.1rem;
}

/* ===== FILTER ROW ===== */
.filter-row { margin-bottom: 1rem; }
.filter-row select {
  width: 100%;
  padding: 0.7rem 1rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--cream);
  font-family: inherit;
  font-size: 0.95rem;
}

/* ===== PAYMENTS MATRIX ===== */
.matrix-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 0.8rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
.matrix-head h2 {
  font-family: 'Oswald', sans-serif;
  font-size: 1.4rem;
  letter-spacing: 0.03em;
}
.matrix-sub {
  font-size: 0.8rem;
  color: var(--ink-soft);
  margin-top: 0.15rem;
}
.matrix-range-controls {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  background: var(--cream);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.2rem 0.3rem;
}
.matrix-range-label {
  font-family: 'Oswald', sans-serif;
  font-size: 0.85rem;
  letter-spacing: 0.05em;
  padding: 0 0.6rem;
  min-width: 150px;
  text-align: center;
}
.matrix-legend {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.8rem;
  flex-wrap: wrap;
}
.legend-chip {
  font-size: 0.75rem;
  padding: 0.3rem 0.7rem;
  border-radius: 20px;
  background: var(--cream);
  border: 1px solid var(--line);
  color: var(--ink-soft);
}
.legend-chip.legend-paid { color: var(--green); border-color: var(--green); }
.legend-chip.legend-unpaid { color: var(--amber); border-color: var(--amber); }

.matrix-wrap {
  background: var(--cream);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.matrix-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  min-width: 560px;
}
.matrix-table th {
  background: var(--bg);
  padding: 0.7rem 0.5rem;
  text-align: center;
  font-family: 'Oswald', sans-serif;
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  color: var(--ink-soft);
  border-bottom: 2px solid var(--line);
}
.matrix-table th.current {
  background: var(--red);
  color: var(--cream);
}
.matrix-table th.mx-student-col { text-align: left; padding-left: 1rem; }
.matrix-table td {
  padding: 0.6rem 0.5rem;
  border-bottom: 1px solid var(--line);
  text-align: center;
}
.matrix-table tr:last-child td { border-bottom: none; }
.matrix-table td.mx-student-col {
  text-align: left;
  padding-left: 1rem;
  min-width: 160px;
  background: var(--cream);
  position: sticky;
  left: 0;
  z-index: 1;
}
.mx-student-name { font-weight: 600; color: var(--ink); }
.mx-student-group { font-size: 0.72rem; color: var(--ink-soft); margin-top: 0.1rem; }
.mx-cell {
  font-size: 1.1rem;
  font-weight: 700;
  width: 50px;
  user-select: none;
}
.mx-cell.mx-clickable { cursor: pointer; transition: background 0.15s; }
.mx-cell.mx-paid { color: var(--green); background: rgba(45, 122, 62, 0.08); }
.mx-cell.mx-paid:hover { background: rgba(45, 122, 62, 0.18); }
.mx-cell.mx-unpaid { color: var(--amber); background: rgba(200, 138, 26, 0.05); }
.mx-cell.mx-unpaid:hover { background: rgba(200, 138, 26, 0.15); }
.mx-cell.mx-before { color: var(--line); }
.mx-due-col {
  font-family: 'Oswald', sans-serif;
  font-weight: 600;
  font-size: 0.95rem;
  min-width: 70px;
}
.mx-due-col.mx-due-has { color: var(--red); }
.mx-due-col.mx-due-ok { color: var(--green); }

/* ===== FINANCE ===== */
.finance-h2 {
  font-family: 'Oswald', sans-serif;
  font-size: 1.4rem;
  letter-spacing: 0.03em;
  margin-bottom: 1rem;
}
.finance-totals { margin-bottom: 1rem; }
.big-stat {
  background: linear-gradient(135deg, var(--red) 0%, var(--red-dark) 100%);
  color: var(--cream);
  padding: 1.5rem;
  border-radius: 12px;
  margin-bottom: 0.8rem;
  text-align: center;
  box-shadow: var(--shadow);
}
.big-stat-label {
  font-family: 'Oswald', sans-serif;
  font-size: 0.8rem;
  letter-spacing: 0.15em;
  opacity: 0.85;
  margin-bottom: 0.4rem;
}
.big-stat-value {
  font-family: 'Oswald', sans-serif;
  font-size: 2.8rem;
  font-weight: 700;
  color: var(--gold);
  line-height: 1;
}
.big-stat-sub {
  font-size: 0.85rem;
  opacity: 0.8;
  margin-top: 0.4rem;
}
.finance-totals-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.6rem;
}
.mini-stat {
  background: var(--cream);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.9rem 0.5rem;
  text-align: center;
}
.mini-stat-label {
  font-size: 0.68rem;
  color: var(--ink-soft);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  margin-bottom: 0.3rem;
}
.mini-stat-value {
  font-family: 'Oswald', sans-serif;
  font-size: 1.2rem;
  font-weight: 600;
  color: var(--red);
}
.chart-card {
  background: var(--cream);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 1rem 0.5rem;
  margin-bottom: 0.8rem;
}
.chart-svg {
  width: 100%;
  height: 240px;
  display: block;
}
.chart-svg-small { height: 160px; }
.chart-labels {
  display: grid;
  gap: 0.3rem;
  margin-top: 0.4rem;
  padding-left: 40px;
  padding-right: 10px;
  font-size: 0.7rem;
  color: var(--ink-soft);
  text-align: center;
}
.debtors-list { display: flex; flex-direction: column; gap: 0.5rem; }
.debtor-row {
  background: var(--cream);
  border: 1px solid var(--line);
  border-left: 4px solid var(--red);
  border-radius: 8px;
  padding: 0.8rem 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.debtor-name { font-weight: 600; }
.debtor-group { font-size: 0.8rem; color: var(--ink-soft); }
.debtor-amount {
  font-family: 'Oswald', sans-serif;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--red);
}

@media (max-width: 640px) {
  .tabs { grid-template-columns: repeat(5, 1fr); }
  .tab-btn { padding: 0.7rem 0.2rem; }
  .tab-btn span { font-size: 0.65rem; }
  .finance-totals-grid { grid-template-columns: 1fr 1fr; }
  .finance-totals-grid .mini-stat:nth-child(3) { grid-column: 1 / -1; }
  .big-stat-value { font-size: 2.2rem; }
  .matrix-table { font-size: 0.85rem; }
  .mx-cell { width: 42px; }
  .matrix-range-label { min-width: 110px; font-size: 0.75rem; }
}

/* ===== (old per-row payments list removed) ===== */
.payments-list { display: flex; flex-direction: column; gap: 0.5rem; }
.pay-row {
  background: var(--cream);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.8rem 1rem;
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 0.8rem;
  align-items: center;
}
.pay-row.paid {
  border-left: 4px solid var(--green);
  background: rgba(45, 122, 62, 0.04);
}
.pay-row:not(.paid) { border-left: 4px solid var(--amber); }
.pay-check {
  width: 28px;
  height: 28px;
  border: 2px solid var(--line);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-weight: bold;
  color: transparent;
  transition: all 0.15s;
}
.pay-check.paid {
  background: var(--green);
  border-color: var(--green);
  color: white;
}
.pay-info { display: flex; flex-direction: column; gap: 0.1rem; }
.pay-name { font-weight: 500; }
.pay-group-name { font-size: 0.8rem; color: var(--ink-soft); }
.pay-amount {
  font-family: 'Oswald', sans-serif;
  font-size: 1.1rem;
  font-weight: 600;
}
.pay-row.paid .pay-amount { color: var(--green); }
.pay-row:not(.paid) .pay-amount { color: var(--ink-soft); }

/* ===== EMPTY STATE ===== */
.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--ink-soft);
  background: var(--cream);
  border: 2px dashed var(--line);
  border-radius: 10px;
}

/* ===== BUTTONS ===== */
.btn-primary, .btn-ghost, .btn-danger {
  padding: 0.7rem 1.2rem;
  border-radius: 8px;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: all 0.2s;
}
.btn-primary {
  background: var(--red);
  color: var(--cream);
}
.btn-primary:hover { background: var(--red-dark); }
.btn-ghost {
  background: transparent;
  color: var(--ink-soft);
  border: 1px solid var(--line);
}
.btn-ghost:hover { background: var(--bg); }
.btn-danger {
  background: transparent;
  color: var(--red);
  border: 1px solid var(--red);
}
.btn-danger:hover { background: var(--red); color: var(--cream); }

/* ===== MODAL ===== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  align-items: flex-end;
  justify-content: center;
}
.modal.open { display: flex; }
.modal-bg {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
}
.modal-panel {
  position: relative;
  background: var(--cream);
  width: 100%;
  max-width: 500px;
  border-radius: 16px 16px 0 0;
  padding: 1.5rem 1.3rem calc(1.5rem + env(safe-area-inset-bottom));
  box-shadow: var(--shadow-lg);
  max-height: 92vh;
  overflow-y: auto;
  animation: slideUp 0.25s ease-out;
}
@keyframes slideUp {
  from { transform: translateY(30px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
.modal-panel h3 {
  font-family: 'Oswald', sans-serif;
  font-size: 1.3rem;
  letter-spacing: 0.03em;
  margin-bottom: 1.2rem;
  color: var(--red);
}
.modal-panel label {
  display: block;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--ink-soft);
  margin: 0.9rem 0 0.3rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.modal-panel input, .modal-panel select, .modal-panel textarea {
  width: 100%;
  padding: 0.7rem 0.9rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-family: inherit;
  font-size: 1rem;
  background: white;
}
.modal-panel input:focus,
.modal-panel select:focus,
.modal-panel textarea:focus {
  outline: none;
  border-color: var(--red);
}
.modal-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  margin-top: 1.5rem;
  flex-wrap: wrap;
}
.modal-actions .btn-danger { margin-right: auto; }

/* ===== TOAST ===== */
.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(60px);
  background: var(--ink);
  color: var(--cream);
  padding: 0.8rem 1.3rem;
  border-radius: 30px;
  font-size: 0.9rem;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transition: all 0.3s;
  z-index: 200;
  pointer-events: none;
}
.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

@media (min-width: 640px) {
  .tab-btn span { font-size: 0.85rem; }
  .summary-grid { gap: 1rem; }
  .summary-value { font-size: 1.8rem; }
}
