:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --border: #e2e5ea;
  --text: #1a1d23;
  --text-muted: #6b7280;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --paid-bg: #dcfce7;
  --paid-text: #15803d;
  --unpaid-bg: #fee2e2;
  --unpaid-text: #b91c1c;
  --na-bg: #eef0f3;
  --na-text: #8a919c;
  --shadow: 0 8px 24px rgba(20, 24, 32, 0.12);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1c1f26;
    --border: #2b2f38;
    --text: #eceef1;
    --text-muted: #9aa1ac;
    --primary: #3b82f6;
    --primary-hover: #60a5fa;
    --danger: #f87171;
    --paid-bg: #12301f;
    --paid-text: #4ade80;
    --unpaid-bg: #3a1414;
    --unpaid-text: #f87171;
    --na-bg: #23262d;
    --na-text: #767d88;
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.topbar h1 {
  font-size: 18px;
  margin: 0;
}

.tabs {
  display: flex;
  gap: 4px;
  background: var(--bg);
  padding: 4px;
  border-radius: 10px;
}

.tab {
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
}

.tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

main {
  max-width: 1000px;
  margin: 0 auto;
  padding: 24px;
}

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

.toolbar {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

input[type="search"], input[type="text"], input[type="number"], select, textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 14px;
  font-family: inherit;
}

#search, #history-search {
  flex: 1;
  min-width: 200px;
}

textarea { resize: vertical; }

.btn {
  border: none;
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-hover); }

.btn-secondary {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-danger-outline {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
}

.summary {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.summary .stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 16px;
  min-width: 120px;
}

.summary .stat .value {
  font-size: 20px;
  font-weight: 600;
}

.summary .stat .label {
  color: var(--text-muted);
  font-size: 12px;
}

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}

table {
  width: 100%;
  border-collapse: collapse;
}

thead th {
  text-align: left;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

tbody td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

tbody tr:last-child td { border-bottom: none; }

tbody tr.member-row:hover { background: rgba(37, 99, 235, 0.04); }

.expand-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 14px;
  width: 24px;
}

.badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}

.badge-paid { background: var(--paid-bg); color: var(--paid-text); }
.badge-unpaid { background: var(--unpaid-bg); color: var(--unpaid-text); }
.badge-na { background: var(--na-bg); color: var(--na-text); }

.months-row td {
  background: var(--bg);
  padding: 12px 16px 16px 48px;
}

.months-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 8px;
}

.month-cell {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  text-align: center;
  cursor: pointer;
  background: var(--surface);
}

.month-cell .m-label {
  font-size: 11px;
  color: var(--text-muted);
  display: block;
  margin-bottom: 4px;
}

.month-cell.paid { border-color: var(--paid-text); }
.month-cell.unpaid { border-color: var(--unpaid-text); }

.notes {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 6px;
}

.empty-state {
  padding: 32px;
  text-align: center;
  color: var(--text-muted);
}

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-list li {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 16px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: baseline;
}

.history-list .desc { flex: 1; }
.history-list .time {
  color: var(--text-muted);
  font-size: 12px;
  white-space: nowrap;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 16, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}

.overlay[hidden] { display: none; }

.modal {
  background: var(--surface);
  border-radius: 14px;
  padding: 24px;
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow);
  position: relative;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
}

.modal h2 {
  margin: 0 0 16px;
  font-size: 16px;
}

.field {
  margin-bottom: 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  font-size: 12px;
  color: var(--text-muted);
}

.field input, .field select, .field textarea {
  width: 100%;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
}

.form-error {
  color: var(--danger);
  font-size: 13px;
  margin-top: 8px;
  min-height: 18px;
}

.pay-status-line {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: var(--shadow);
  z-index: 100;
}

.toast.error {
  background: var(--unpaid-text);
  color: #fff;
}

@media (max-width: 640px) {
  .months-grid { grid-template-columns: repeat(auto-fill, minmax(60px, 1fr)); }
  main { padding: 16px; }
}
