:root {
  --bg: #f3f4f6;
  --card: #ffffff;
  --ink: #1f2937;
  --muted: #6b7280;
  --line: #e5e7eb;
  --primary: #2563eb;
  --primary-d: #1d4ed8;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #d97706;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  -webkit-text-size-adjust: 100%;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
code { background: #eef2ff; padding: 1px 5px; border-radius: 5px; font-size: .9em; }

/* ---------- buttons ---------- */
.btn {
  font: inherit; border: 1px solid var(--line); background: var(--card);
  color: var(--ink); padding: 8px 12px; border-radius: 9px; cursor: pointer;
  min-height: 38px;
}
.btn:hover { background: #f9fafb; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-d); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.danger { color: var(--danger); border-color: #fecaca; background: #fef2f2; }
.btn.sm { padding: 4px 9px; min-height: 30px; font-size: 13px; }

/* ---------- gate ---------- */
.gate { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.gate-card {
  background: var(--card); padding: 28px; border-radius: var(--radius);
  box-shadow: var(--shadow); max-width: 420px; text-align: center;
}
.gate-card h1 { margin-top: 0; }

/* ---------- topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 12px;
  background: var(--card); border-bottom: 1px solid var(--line);
  padding: 8px 12px; box-shadow: var(--shadow);
}
.brand { font-weight: 700; white-space: nowrap; }
.brand span { display: inline; }
.tabs { display: flex; gap: 4px; margin-left: 8px; }
.tab {
  border: none; background: transparent; font: inherit; cursor: pointer;
  padding: 8px 12px; border-radius: 9px; color: var(--muted); min-height: 38px;
}
.tab.active { background: #eff6ff; color: var(--primary); font-weight: 600; }
.topbar-right { margin-left: auto; display: flex; gap: 6px; }

.menu {
  position: absolute; right: 12px; top: 54px; z-index: 40;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: var(--shadow); display: flex; flex-direction: column; min-width: 220px; overflow: hidden;
}
.menu button {
  text-align: left; border: none; background: transparent; font: inherit;
  padding: 11px 14px; cursor: pointer;
}
.menu button:hover { background: #f3f4f6; }

main { max-width: 1100px; margin: 0 auto; padding: 14px; }
.view { animation: fade .15s ease; }
@keyframes fade { from { opacity: .4; } to { opacity: 1; } }

/* ---------- calendar ---------- */
.cal-controls {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 10px; margin-bottom: 10px;
}
.cal-nav { display: flex; align-items: center; gap: 6px; }
.cal-nav h2 { margin: 0; font-size: 18px; min-width: 150px; text-align: center; }
.legend { display: flex; flex-wrap: wrap; gap: 8px; }
.legend .chip {
  display: inline-flex; align-items: center; gap: 6px; cursor: pointer;
  padding: 4px 9px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--card); font-size: 13px; user-select: none;
}
.legend .chip.off { opacity: .4; }
.legend .dot { width: 11px; height: 11px; border-radius: 50%; flex: none; }

.cal { display: flex; flex-direction: column; gap: 2px; }
.cal-dow { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 2px; }
.cal-dow div { text-align: center; font-size: 12px; color: var(--muted); padding: 4px 0; font-weight: 600; }
.cal-week {
  display: grid; grid-template-columns: repeat(7, minmax(0, 1fr));
  grid-auto-rows: minmax(22px, auto); gap: 2px;
  background: var(--card); border-radius: 8px; padding: 2px; box-shadow: var(--shadow);
}
.cal-day {
  grid-row: 1; min-height: 64px; padding: 3px 4px; border-radius: 6px;
}
.cal-day.dim { background: #fafafa; color: #b6bbc2; }
.cal-day .num { font-size: 12px; font-weight: 600; }
.cal-day.today .num {
  background: var(--primary); color: #fff; border-radius: 50%;
  width: 22px; height: 22px; display: inline-grid; place-items: center;
}
.cal-bar {
  margin: 1px 0; padding: 2px 6px; border-radius: 5px; color: #fff;
  font-size: 11.5px; line-height: 1.35; cursor: pointer; overflow: hidden;
  white-space: nowrap; text-overflow: ellipsis; border: none; text-align: left;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.05);
}
.cal-bar.cont-l { border-top-left-radius: 0; border-bottom-left-radius: 0; }
.cal-bar.cont-r { border-top-right-radius: 0; border-bottom-right-radius: 0; }
.cal-bar .pin { opacity: .85; }

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

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { padding: 9px 10px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
tbody tr:hover { background: #f9fafb; cursor: pointer; }
td.right, th.right { text-align: right; }

.kidtag { display: inline-flex; align-items: center; gap: 6px; }
.kidtag .dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }

.badge { font-size: 12px; padding: 2px 8px; border-radius: 999px; font-weight: 600; white-space: nowrap; }
.badge.unpaid { background: #fef2f2; color: var(--danger); }
.badge.partial { background: #fffbeb; color: var(--warn); }
.badge.paid { background: #f0fdf4; color: var(--ok); }

.list-controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 10px; }
.list-controls select { font: inherit; padding: 7px 9px; border-radius: 9px; border: 1px solid var(--line); background: var(--card); min-height: 38px; }
.list-total { margin-left: auto; font-weight: 600; }

/* ---------- dashboard cards ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 16px; }
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; }
.card .k { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.card .v { font-size: 24px; font-weight: 700; margin-top: 4px; }
.card .v.bal-pos { color: var(--danger); }
.view h3 { margin: 18px 4px 8px; }

/* ---------- modal ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 50; display: grid; place-items: center; padding: 14px; }
.modal {
  background: var(--card); border-radius: var(--radius); box-shadow: 0 10px 40px rgba(0,0,0,.25);
  width: 100%; max-width: 520px; max-height: 90vh; overflow-y: auto; padding: 18px 18px 20px;
}
.modal h2 { margin: 0 0 12px; font-size: 19px; }
.modal h3 { margin: 16px 0 8px; font-size: 15px; }
.field { margin-bottom: 11px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.field input, .field select, .field textarea {
  width: 100%; font: inherit; padding: 9px 10px; border: 1px solid var(--line);
  border-radius: 9px; background: #fff; min-height: 40px;
}
.field textarea { min-height: 64px; resize: vertical; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.modal-actions { display: flex; gap: 8px; margin-top: 16px; }
.modal-actions .spacer { flex: 1; }
.err { color: var(--danger); font-size: 13px; margin-top: 6px; min-height: 16px; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; font-size: 14px; margin: 4px 0 6px; }
.kv .k { color: var(--muted); }
.money-line { display: flex; gap: 14px; flex-wrap: wrap; align-items: baseline; margin: 8px 0; }
.money-line .big { font-size: 22px; font-weight: 700; }

.pay-row { display: flex; align-items: center; gap: 8px; padding: 7px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.pay-row .amt { font-weight: 600; }
.pay-row .when { color: var(--muted); font-size: 13px; }
.pay-row .note { color: var(--muted); font-size: 13px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pay-row .acts { display: flex; gap: 4px; margin-left: auto; }

.toast {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%);
  background: #111827; color: #fff; padding: 10px 16px; border-radius: 10px;
  z-index: 60; box-shadow: var(--shadow); font-size: 14px; max-width: 90vw;
}
.toast.err { background: var(--danger); }
.kid-manage-row { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.kid-manage-row input[type=color] { width: 38px; min-height: 38px; padding: 2px; }
.kid-manage-row input[type=text] { flex: 1; }

/* ---------- mobile ---------- */
@media (max-width: 640px) {
  .brand span { display: none; }
  .topbar { gap: 6px; padding: 6px 8px; }
  .tab { padding: 8px 9px; }
  #addBtn { font-size: 0; padding: 8px 12px; }
  #addBtn::before { content: "+"; font-size: 18px; }
  main { padding: 8px; }
  .cal-day { min-height: 52px; }
  .cal-bar { font-size: 10.5px; padding: 2px 4px; }
  .cal-nav h2 { font-size: 16px; min-width: 120px; }
  th, td { padding: 8px 8px; }
  .modal { padding: 14px; }
}
