:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #1d4ed8;
  --primary-ink: #ffffff;
  --primary-soft: #e0e7ff;
  --good: #047857;
  --good-soft: #d1fae5;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --bad: #b91c1c;
  --bad-soft: #fee2e2;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 1px 3px rgba(0, 0, 0, .06);
  --tabbar-h: 62px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b21;
    --surface-2: #22262e;
    --text: #f3f4f6;
    --muted: #9ca3af;
    --border: #2d323b;
    --primary: #6d8cff;
    --primary-ink: #0b1020;
    --primary-soft: #1e2647;
    --good: #34d399;
    --good-soft: #0f2e24;
    --warn: #fbbf24;
    --warn-soft: #2f2410;
    --bad: #f87171;
    --bad-soft: #3a1717;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--tabbar-h) + 90px + env(safe-area-inset-bottom));
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary); }
[hidden] { display: none !important; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 6px;
  min-height: 54px; padding: 6px 12px; padding-top: max(6px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar h1 { flex: 1; margin: 0 4px; font-size: 18px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  display: grid; place-items: center; width: 40px; height: 40px;
  border: 0; border-radius: 10px; background: transparent; cursor: pointer;
}
.icon-btn:active { background: var(--surface-2); }

main { max-width: 760px; margin: 0 auto; padding: 14px 16px; }

/* Bottom tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: 11.5px; font-weight: 550;
}
.tabbar a.active { color: var(--primary); }
.tabbar svg { width: 23px; height: 23px; }

/* Floating Collect Fees button */
.fab {
  position: fixed; z-index: 11; right: 16px;
  bottom: calc(var(--tabbar-h) + 14px + env(safe-area-inset-bottom));
  display: flex; align-items: center; gap: 8px;
  height: 52px; padding: 0 20px; border: 0; border-radius: 26px;
  background: var(--primary); color: var(--primary-ink);
  font-weight: 650; font-size: 15.5px; cursor: pointer;
  box-shadow: 0 6px 18px rgba(29, 78, 216, .35);
}
.fab:active { transform: scale(.97); }

/* Cards and stats */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px; margin-bottom: 12px;
}
.card h2, .section-title { margin: 0 0 10px; font-size: 15px; font-weight: 650; }
.section-title { margin: 18px 2px 8px; color: var(--muted); font-size: 12.5px; text-transform: uppercase; letter-spacing: .04em; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 12px 14px;
}
.stat .label { color: var(--muted); font-size: 12.5px; }
.stat .value { font-size: 21px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.stat.wide { grid-column: 1 / -1; }
.stat.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.stat.primary .label, .stat.primary .sub { color: inherit; opacity: .85; }

.bar { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; margin-top: 8px; }
.bar > span { display: block; height: 100%; background: var(--good); border-radius: 4px; }
.stat.primary .bar { background: rgba(255, 255, 255, .25); }
.stat.primary .bar > span { background: #fff; }

/* Lists */
.list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; margin-bottom: 12px; }
.row {
  display: flex; align-items: center; gap: 12px; min-height: 58px;
  padding: 10px 14px; border-bottom: 1px solid var(--border);
  color: inherit; text-decoration: none; background: none; width: 100%; text-align: left; border-left: 0; border-right: 0; border-top: 0;
}
.row:last-child { border-bottom: 0; }
a.row:active, button.row:active { background: var(--surface-2); }
.row .main { flex: 1; min-width: 0; }
.row .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .meta { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .end { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.avatar {
  flex: none; display: grid; place-items: center; width: 38px; height: 38px; border-radius: 50%;
  background: var(--primary-soft); color: var(--primary); font-weight: 700; font-size: 14px;
}
.empty { padding: 28px 16px; text-align: center; color: var(--muted); }

.pill { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 650; }
.pill.good { background: var(--good-soft); color: var(--good); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.muted { background: var(--surface-2); color: var(--muted); }
.amount-bad { color: var(--bad); font-weight: 650; }
.amount-good { color: var(--good); font-weight: 650; }
.strike { text-decoration: line-through; color: var(--muted); }

/* Forms */
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; font-weight: 550; }
input[type=text], input[type=search], input[type=tel], input[type=password], input[type=number],
input[type=date], input[type=month], select, textarea {
  width: 100%; min-height: 46px; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
  font-size: 16px; /* prevents iOS zoom */
}
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
.amount-input { font-size: 26px !important; font-weight: 700; text-align: center; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 46px; padding: 0 18px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); font-weight: 600; cursor: pointer; text-decoration: none; color: var(--text);
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.danger { color: var(--bad); }
.btn.small { min-height: 34px; padding: 0 12px; font-size: 13.5px; border-radius: 9px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; pointer-events: none; }
.btn-row { display: flex; gap: 8px; }
.btn-row > .btn { flex: 1; }

.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 10px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; min-height: 36px; padding: 0 14px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface); font-weight: 600; font-size: 14px; cursor: pointer;
}
.chip.on { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }

.toolbar { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.toolbar > input, .toolbar > select { flex: 1; }

/* Segmented control (attendance + tabs) */
.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 3px; }
.seg button {
  flex: 1; min-width: 42px; min-height: 38px; border: 0; border-radius: 8px; background: transparent;
  font-weight: 700; cursor: pointer; color: var(--muted);
}
.seg button.on-P { background: var(--good); color: #fff; }
.seg button.on-L { background: var(--warn); color: #fff; }
.seg button.on-A { background: var(--bad); color: #fff; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.tabs { margin-bottom: 12px; }

.sticky-save {
  position: sticky; bottom: calc(var(--tabbar-h) + 10px + env(safe-area-inset-bottom));
  margin-top: 12px; display: flex; gap: 8px;
}
.sticky-save .btn { box-shadow: 0 4px 14px rgba(0, 0, 0, .12); }

/* Profile header */
.profile-head { display: flex; gap: 14px; align-items: center; }
.profile-head .avatar { width: 54px; height: 54px; font-size: 19px; }
.profile-head h2 { margin: 0; font-size: 19px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin-top: 12px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

/* Bottom sheet */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, .4);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  width: 100%; max-width: 560px; max-height: 92vh; overflow-y: auto;
  background: var(--surface); border-radius: 18px 18px 0 0;
  padding: 10px 16px calc(18px + env(safe-area-inset-bottom));
  animation: up .18s ease-out;
}
@keyframes up { from { transform: translateY(30px); opacity: .6; } }
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--border); margin: 0 auto 10px; }
.sheet h2 { margin: 0 0 12px; font-size: 18px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
@media (min-width: 640px) {
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: 18px; }
}

.success-mark {
  width: 64px; height: 64px; border-radius: 50%; margin: 6px auto 10px;
  display: grid; place-items: center; background: var(--good-soft); color: var(--good);
}

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
th, td { padding: 9px 8px; border-bottom: 1px solid var(--border); text-align: left; }
th { color: var(--muted); font-weight: 600; font-size: 12.5px; }
td.num, th.num { text-align: right; }
tr:last-child td { border-bottom: 0; }

/* Toast */
.toast {
  position: fixed; z-index: 40; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar-h) + 80px + env(safe-area-inset-bottom));
  max-width: calc(100% - 32px); padding: 11px 16px; border-radius: 12px;
  background: #111827; color: #fff; font-weight: 550; box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
}
.toast.error { background: var(--bad); }

/* Login */
.login { max-width: 380px; margin: 9vh auto 0; }
.login .logo { width: 64px; height: 64px; display: block; margin: 0 auto 12px; }
.login h1 { text-align: center; margin: 0 0 4px; font-size: 22px; }
.login p { text-align: center; color: var(--muted); margin: 0 0 22px; }
.form-error { color: var(--bad); font-size: 14px; margin: -4px 0 10px; min-height: 1px; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.spacer { height: 8px; }
.loading { padding: 40px; text-align: center; color: var(--muted); }

@media (min-width: 640px) {
  .stats { grid-template-columns: repeat(4, 1fr); }
  .stat.wide { grid-column: span 2; }
}
