:root {
  --primary: #142B6B;
  --primary-dark: #0A1638;
  --primary-light: #F1E6C8;
  --accent: #1E5A6B;
  --gold: #C79A3D;
  --gold-light: #E8C877;
  --green: #16a34a;
  --red: #dc2626;
  --amber: #b3791f;
  --ink: #2B211A;
  --muted: #5B4B3C;
  --border: #E4D8B8;
  --bg: #F7EFDD;
  --card: #ffffff;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(62,20,10,.07), 0 4px 16px rgba(62,20,10,.06);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: 'Work Sans', system-ui, -apple-system, Arial, sans-serif; background: var(--bg); color: var(--ink); font-size: 15px; }
button, input, select, textarea { font-family: inherit; font-size: inherit; }
h1, h2, h3, .brand-name, .page-head h2, .modal-head h3 { font-family: 'Cinzel', Georgia, serif; }

/* ---------- Login ---------- */
#loginView[hidden], #mainView[hidden] { display: none !important; }
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: linear-gradient(160deg, #142B6B 0%, #1B2F70 55%, #0A1638 100%); padding: 20px; position: relative; }
.back-link { position: absolute; top: 24px; left: 28px; color: #E8C877; font-size: 13.5px; text-decoration: none; }
.back-link:hover { text-decoration: underline; }
.login-card { background: #fff; border-radius: var(--radius); padding: 36px 34px; width: 100%; max-width: 400px; box-shadow: 0 24px 60px rgba(0,0,0,.3); text-align: center; }
.logo-badge { width: 64px; height: 64px; border-radius: 50%; background: linear-gradient(135deg, #142B6B, #0A1638); color: #fff; font-size: 32px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; margin-bottom: 14px; overflow: hidden; }
.logo-badge.small { width: 42px; height: 42px; font-size: 22px; margin: 0; }
.crest-badge { padding: 0; }
.login-card h1 { font-size: 21px; color: var(--primary-dark); }
.tagline { color: var(--muted); margin: 6px 0 22px; font-size: 13.5px; }
.login-card label { display: block; text-align: left; font-weight: 600; font-size: 13px; margin: 12px 0 6px; color: var(--ink); }
.login-card input { width: 100%; padding: 12px 14px; border: 1.5px solid var(--border); border-radius: 6px; transition: .15s; background: var(--bg); }
.login-card input:focus { outline: none; border-color: var(--gold); box-shadow: 0 0 0 3px rgba(199,154,61,.2); background: #fff; }
.error { color: var(--red); font-size: 13px; margin-top: 10px; }
.demo-box { margin-top: 22px; background: var(--primary-light); border-radius: 8px; padding: 14px; font-size: 12.5px; display: flex; flex-direction: column; gap: 6px; text-align: left; border: 1px solid var(--border); }
.demo-box strong { margin-bottom: 2px; color: var(--primary-dark); }
code { background: #EADFC0; padding: 1px 7px; border-radius: 6px; font-size: 12px; }

/* ---------- Topbar ---------- */
.topbar { background: var(--primary); border-bottom: 1px solid var(--primary-dark); padding: 12px 22px; display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; z-index: 40; box-shadow: 0 1px 2px rgba(0,0,0,.15); }
.brand { display: flex; align-items: center; gap: 12px; }
.brand .logo-badge.small { background: none; }
.brand-name { font-weight: 600; font-size: 16px; color: #F7EFDD; }
.brand-sub { font-size: 12px; color: var(--gold-light); }
.topbar-right { display: flex; align-items: center; gap: 14px; }
.chip { background: rgba(232,200,119,.18); color: var(--gold-light); font-weight: 600; font-size: 12px; padding: 5px 12px; border-radius: 20px; text-transform: capitalize; }
.user-box { display: flex; align-items: center; gap: 10px; }
.avatar { width: 38px; height: 38px; border-radius: 50%; background: linear-gradient(135deg, var(--gold), var(--gold-light)); color: var(--primary-dark); display: flex; align-items: center; justify-content: center; font-weight: 700; }
.username { font-weight: 600; font-size: 13.5px; color: #F7EFDD; }
.muted { color: var(--muted); font-size: 12px; }
.topbar .muted { color: #D8BDB4; }
#topEmail { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .btn-ghost { color: #F7EFDD; border-color: rgba(247,239,221,.35); }
.topbar .btn-ghost:hover { background: rgba(247,239,221,.12); }

/* ---------- Layout ---------- */
.layout { display: flex; min-height: calc(100vh - 65px); }
.sidebar { width: 230px; min-width: 230px; background: #fff; border-right: 1px solid var(--border); padding: 16px 12px; }
.nav-item { display: flex; align-items: center; gap: 11px; padding: 11px 14px; border-radius: 6px; color: var(--ink); font-weight: 500; font-size: 14px; cursor: pointer; margin-bottom: 3px; transition: .12s; border: none; background: none; width: 100%; text-align: left; }
.nav-item:hover { background: var(--bg); }
.nav-item.active { background: var(--primary); color: #fff; box-shadow: 0 4px 12px rgba(110,15,31,.28); }
.nav-ico { font-size: 17px; width: 22px; text-align: center; }
.nav-group { font-size: 11px; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); font-weight: 700; padding: 14px 12px 6px; }
.content { flex: 1; padding: 26px 30px; max-width: 1280px; }

/* ---------- Cards & grids ---------- */
.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; flex-wrap: wrap; gap: 12px; }
.page-head h2 { font-size: 20px; color: var(--primary-dark); font-weight: 600; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; margin-bottom: 16px; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 16px; margin-bottom: 20px; }
.stat { background: var(--card); border-radius: var(--radius); border: 1px solid var(--border); padding: 18px 20px; box-shadow: var(--shadow); }
.stat .stat-label { font-size: 12.5px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .4px; }
.stat .stat-value { font-size: 30px; font-weight: 800; margin-top: 6px; font-family: 'Cinzel', serif; color: var(--primary-dark); }
.stat .stat-hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.stat.grad-1 { background: linear-gradient(135deg, #142B6B, #2748A0); color: #fff; }
.stat.grad-1 .stat-label, .stat.grad-1 .stat-hint, .stat.grad-1 .stat-value { color: #F7EFDD; }
.stat.grad-2 { background: linear-gradient(135deg, #1E5A6B, #2C7A8F); color: #fff; }
.stat.grad-2 .stat-label, .stat.grad-2 .stat-hint, .stat.grad-2 .stat-value { color: #E4F3F3; }
.stat.grad-3 { background: linear-gradient(135deg, var(--gold), var(--gold-light)); color: var(--primary-dark); }
.stat.grad-3 .stat-label, .stat.grad-3 .stat-hint, .stat.grad-3 .stat-value { color: var(--primary-dark); }
.stat.grad-4 { background: linear-gradient(135deg, #0A1638, #142B6B); color: #fff; }
.stat.grad-4 .stat-label, .stat.grad-4 .stat-hint, .stat.grad-4 .stat-value { color: #F1D9D0; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; padding: 10px 12px; color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .4px; border-bottom: 1.5px solid var(--border); white-space: nowrap; }
td { padding: 11px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:hover td { background: #f8fafc; }
tr:last-child td { border-bottom: none; }
.tbl-actions { display: flex; gap: 6px; }

/* ---------- Buttons & inputs ---------- */
.btn { padding: 9px 16px; border-radius: 6px; border: 1px solid transparent; font-weight: 600; font-size: 13.5px; cursor: pointer; transition: .12s; display: inline-flex; align-items: center; gap: 7px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: transparent; color: var(--ink); border-color: var(--border); }
.btn-ghost:hover { background: var(--bg); }
.btn-green { background: var(--green); color: #fff; }
.btn-green:hover { filter: brightness(1.08); }
.btn-red { background: #fbeaea; color: var(--red); }
.btn-red:hover { background: #f5d4d1; }
.btn-amber { background: #F1E6C8; color: var(--amber); }
.btn-amber:hover { background: #E8D9AC; }
.btn-sm { padding: 6px 11px; font-size: 12.5px; border-radius: 5px; }
.btn-block { width: 100%; padding: 12px; font-size: 15px; border-radius: 6px; margin-top: 18px; }

form .field { margin-bottom: 13px; }
form label { display: block; font-size: 12.5px; font-weight: 600; margin-bottom: 5px; color: var(--ink); }
form input, form select, form textarea { width: 100%; padding: 10px 12px; border: 1.5px solid var(--border); border-radius: 6px; background: #fff; transition: .15s; }
form input:focus, form select:focus, form textarea:focus { outline: none; border-color: var(--gold); box-shadow: 0 0 0 3px rgba(199,154,61,.15); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* ---------- Badges / pills ---------- */
.badge { font-size: 12px; font-weight: 600; padding: 4px 10px; border-radius: 20px; white-space: nowrap; }
.badge.grade { background: var(--primary-light); color: var(--primary-dark); }
.badge.present { background: #dcfce7; color: var(--green); }
.badge.absent { background: #fbeaea; color: var(--red); }
.badge.late { background: #F1E6C8; color: var(--amber); }
.badge.paid { background: #dcfce7; color: var(--green); }
.badge.due { background: #fbeaea; color: var(--red); }
.badge.all, .badge.student, .badge.parent { background: var(--primary-light); color: var(--primary-dark); }
.badge.new { background: #E4F3F3; color: var(--accent); }
.badge.contacted { background: #F1E6C8; color: var(--amber); }
.badge.visited { background: var(--primary-light); color: var(--primary-dark); }
.badge.enrolled { background: #dcfce7; color: var(--green); }
.badge.declined { background: #fbeaea; color: var(--red); }
.badge.excellent, .badge.good { background: #dcfce7; color: var(--green); }
.badge.satisfactory { background: #F1E6C8; color: var(--amber); }
.badge.needsrevision { background: #fbeaea; color: var(--red); }

/* ---------- Misc ---------- */
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.filters .field { margin-bottom: 0; }
.loader { padding: 60px; text-align: center; color: var(--muted); }
.empty { padding: 40px; text-align: center; color: var(--muted); }
.pill-row { display: flex; gap: 8px; flex-wrap: wrap; }
.avg-big { font-size: 34px; font-weight: 800; }
.ring { width: 84px; height: 84px; border-radius: 50%; display: flex; align-items: center; justify-content: center; background: conic-gradient(var(--gold) calc(var(--p)*1%), var(--border) 0); }
.ring > div { width: 64px; height: 64px; border-radius: 50%; background: #fff; display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 17px; color: var(--primary-dark); }
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; }
.kpi { display: flex; align-items: center; gap: 14px; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.kpi .k-ico { font-size: 26px; }
.kpi .k-val { font-size: 20px; font-weight: 800; }
.kpi .k-lab { font-size: 12px; color: var(--muted); }
.type-line { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.sec-head { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); margin: 20px 0 10px; }
.timetable td, .timetable th { text-align: center; }
.timetable td { font-size: 13px; }

/* ---------- Modal & toast ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15,23,42,.5); display: flex; align-items: center; justify-content: center; z-index: 100; padding: 20px; }
.modal { background: #fff; border-radius: 16px; width: 100%; max-width: 540px; max-height: 92vh; overflow-y: auto; box-shadow: 0 30px 70px rgba(0,0,0,.3); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 18px 22px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: #fff; }
.modal-head h3 { font-size: 17px; }
.modal-body { padding: 20px 22px; }
.modal-foot { padding: 14px 22px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; position: sticky; bottom: 0; background: #fff; }
.modal-close { border: none; background: var(--bg); width: 32px; height: 32px; border-radius: 8px; font-size: 16px; cursor: pointer; color: var(--muted); }
#toastRoot { position: fixed; top: 20px; right: 20px; z-index: 200; display: flex; flex-direction: column; gap: 10px; }
.toast { background: #1e293b; color: #fff; padding: 12px 18px; border-radius: 10px; font-size: 13.5px; box-shadow: 0 10px 30px rgba(0,0,0,.3); animation: slideIn .25s ease; }
.toast.ok { background: var(--green); }
.toast.err { background: var(--red); }
@keyframes slideIn { from { transform: translateX(40px); opacity: 0; } to { transform: none; opacity: 1; } }

@media (max-width: 860px) {
  .layout { flex-direction: column; }
  .sidebar { width: 100%; min-width: 0; display: flex; overflow-x: auto; padding: 10px; }
  .nav-group { display: none; }
  .content { padding: 18px; }
  .field-row { grid-template-columns: 1fr; }
  .topbar { flex-wrap: wrap; gap: 8px; }
  #topEmail { display: none; }
}