:root {
  --sidebar-width: 230px;
  --primary: #1565c0;
  --primary-dark: #003c8f;
  --accent: #f57f17;
}

body { font-family: 'Sarabun', 'Segoe UI', sans-serif; background: #f5f6fa; }

/* === Layout === */
#app-wrapper { display: flex; min-height: 100vh; }

#sidebar {
  width: var(--sidebar-width);
  height: 100vh;
  background: var(--primary-dark);
  color: #fff;
  position: fixed;
  top: 0; left: 0;
  display: flex;
  flex-direction: column;
  z-index: 100;
  transition: transform .25s ease;
  overflow: hidden;
}
#sidebar .sidebar-brand {
  padding: .9rem 1rem;
  background: rgba(0,0,0,.2);
  font-size: .95rem;
  font-weight: 700;
  letter-spacing: .5px;
  flex-shrink: 0;
}
#sidebar .sidebar-brand small { display: block; font-size: .68rem; font-weight: 400; opacity: .75; }

/* ── Scrollable nav ─────────────────────────── */
#sidebar nav {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,.2) transparent;
}
#sidebar nav::-webkit-scrollbar { width: 4px; }
#sidebar nav::-webkit-scrollbar-thumb { background: rgba(255,255,255,.2); border-radius: 2px; }

/* ── Group header (collapsible) ─────────────── */
#sidebar .group-header {
  display: flex;
  align-items: center;
  gap: .4rem;
  padding: .45rem 1rem .3rem;
  font-size: .68rem;
  text-transform: uppercase;
  letter-spacing: .9px;
  color: rgba(255,255,255,.55);
  cursor: pointer;
  user-select: none;
  margin-top: .35rem;
  transition: color .15s;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
}
#sidebar .group-header:hover { color: rgba(255,255,255,.85); }
#sidebar .group-header .gh-arrow {
  margin-left: auto;
  font-size: .7rem;
  transition: transform .2s;
}
#sidebar .group-header.collapsed .gh-arrow { transform: rotate(-90deg); }

/* ── Group body ─────────────────────────────── */
#sidebar .sidebar-group {
  border-left: 3px solid transparent;
  transition: border-color .15s, background .15s;
}
#sidebar .sidebar-group:hover { border-left-color: var(--g-color, transparent); }
#sidebar .sidebar-group:has(.nav-link.active) {
  border-left-color: var(--g-color, transparent);
  background: rgba(255,255,255,.03);
}
#sidebar .group-body {
  overflow: hidden;
  transition: max-height .22s ease;
}
#sidebar .group-body.collapsed { max-height: 0 !important; }

/* ── Nav links ──────────────────────────────── */
#sidebar .nav-link {
  color: rgba(255,255,255,.8);
  padding: .5rem 1rem .5rem 1.5rem;
  display: flex;
  align-items: center;
  gap: .6rem;
  border-radius: 0;
  transition: background .12s;
  font-size: .85rem;
  line-height: 1.3;
}
#sidebar .nav-link:hover, #sidebar .nav-link.active {
  background: rgba(255,255,255,.15);
  color: #fff;
}
#sidebar .nav-link.active {
  border-left: 3px solid var(--g-color, rgba(255,255,255,.7));
  padding-left: calc(1.5rem - 3px);
  background: rgba(255,255,255,.15);
}
#sidebar .nav-link i { font-size: 1rem; width: 20px; flex-shrink: 0; }

/* ── Footer ─────────────────────────────────── */
#sidebar .sidebar-footer {
  flex-shrink: 0;
  padding: .6rem 1rem;
  border-top: 1px solid rgba(255,255,255,.1);
  font-size: .75rem;
  opacity: .55;
}

#main { margin-left: var(--sidebar-width); flex: 1; display: flex; flex-direction: column; }

#topbar {
  background: #fff;
  border-bottom: 1px solid #dee2e6;
  padding: .6rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
}
#topbar .page-title { font-size: 1.1rem; font-weight: 600; color: #212529; margin: 0; }
#topbar .user-info { font-size: .85rem; color: #495057; }
#topbar .user-info strong { color: var(--primary); }

#content { padding: 1.5rem; flex: 1; }

/* === Cards === */
.stat-card {
  background: #fff;
  border-radius: 12px;
  padding: 1.25rem;
  box-shadow: 0 2px 8px rgba(0,0,0,.06);
  display: flex;
  align-items: center;
  gap: 1rem;
}
.stat-card .stat-icon {
  width: 52px; height: 52px;
  border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.5rem;
}
.stat-card .stat-value { font-size: 1.8rem; font-weight: 700; line-height: 1; }
.stat-card .stat-label { font-size: .8rem; color: #6c757d; margin-top: 2px; }

.card { border: none; border-radius: 12px; box-shadow: 0 2px 8px rgba(0,0,0,.06); }
.card-header { background: #fff; border-bottom: 1px solid #f0f0f0; font-weight: 600; border-radius: 12px 12px 0 0 !important; }

/* === Forms === */
.form-select, .form-control { border-radius: 8px; border-color: #dee2e6; font-size: .9rem; }
.form-select:focus, .form-control:focus { border-color: var(--primary); box-shadow: 0 0 0 .2rem rgba(21,101,192,.15); }
.btn { border-radius: 8px; font-size: .9rem; }
.btn-primary { background: var(--primary); border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }

/* === Score Table === */
.score-table { width: 100%; border-collapse: collapse; font-size: .85rem; }
.score-table th {
  background: var(--primary);
  color: #fff;
  padding: .5rem .6rem;
  text-align: center;
  font-weight: 500;
  white-space: nowrap;
  position: sticky;
  top: 0;
}
.score-table th.col-student { text-align: left; min-width: 180px; }
.score-table td { padding: .35rem .5rem; border-bottom: 1px solid #f0f0f0; vertical-align: middle; }
.score-table tr:hover td { background: #f8f9fa; }
.score-table .score-input {
  width: 70px;
  text-align: center;
  border: 1px solid #dee2e6;
  border-radius: 6px;
  padding: .25rem .4rem;
  font-size: .85rem;
}
.score-table .score-input:focus { border-color: var(--primary); outline: none; background: #e8f0fe; }
.score-table .grade-cell { font-weight: 700; text-align: center; min-width: 50px; }
/* ── ระบบเกรด 8 ระดับ (0–4) + พิเศษ มส/ร ────────────────── */
.grade-4  { color: #1b5e20; font-weight: 700; }   /* 4.0 ดีเยี่ยม    */
.grade-35 { color: #2e7d32; }                      /* 3.5 ดีมาก       */
.grade-3  { color: #388e3c; }                      /* 3.0 ดี          */
.grade-25 { color: #1565c0; }                      /* 2.5 ค่อนข้างดี  */
.grade-2  { color: #1976d2; }                      /* 2.0 ปานกลาง     */
.grade-15 { color: #f57f17; }                      /* 1.5 พอใช้       */
.grade-1  { color: #e65100; }                      /* 1.0 ขั้นต่ำ     */
.grade-0  { color: #b71c1c; }                      /* 0   ต่ำกว่าเกณฑ์*/
.grade-ms { color: #6a1b9a; font-weight: 700; }    /* มส ไม่มีสิทธิ์  */
.grade-r  { color: #0277bd; font-weight: 700; }    /* ร  รอตัดสิน     */

/* === Attendance === */
.att-btn-group .btn {
  font-size: .78rem;
  padding: .25rem .5rem;
  border-radius: 20px;
  min-width: 52px;
}
.att-btn-group .btn.active { font-weight: 700; box-shadow: inset 0 2px 4px rgba(0,0,0,.2); }
.att-present.active  { background: #2e7d32; color: #fff; border-color: #2e7d32; }
.att-late.active     { background: #f57f17; color: #fff; border-color: #f57f17; }
.att-absent.active   { background: #c62828; color: #fff; border-color: #c62828; }
.att-sick.active     { background: #6a1b9a; color: #fff; border-color: #6a1b9a; }
.att-leave.active    { background: #0277bd; color: #fff; border-color: #0277bd; }

/* === Attendance pct badge === */
.pct-badge {
  display: inline-block;
  padding: .15rem .5rem;
  border-radius: 20px;
  font-size: .75rem;
  font-weight: 600;
}
.pct-ok   { background: #e8f5e9; color: #2e7d32; }
.pct-warn { background: #fff3e0; color: #e65100; }
.pct-bad  { background: #ffebee; color: #b71c1c; }

/* === Login page === */
#login-page {
  min-height: 100vh;
  background: linear-gradient(135deg, var(--primary-dark) 0%, var(--primary) 100%);
  display: flex; align-items: center; justify-content: center;
}
.login-card {
  width: 100%;
  max-width: 420px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 16px 48px rgba(0,0,0,.3);
  padding: 2.5rem;
}
.login-card .school-logo {
  width: 72px; height: 72px;
  background: var(--primary);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 2rem;
  color: #fff;
  margin: 0 auto 1rem;
}

/* === Logs === */
.log-event-badge { font-size: .75rem; padding: .2rem .55rem; border-radius: 20px; font-weight: 600; }
.ev-success  { background: #e8f5e9; color: #2e7d32; }
.ev-failed   { background: #ffebee; color: #b71c1c; }
.ev-logout   { background: #e3f2fd; color: #1565c0; }
.ev-locked   { background: #fff3e0; color: #e65100; }
.ev-pwd      { background: #f3e5f5; color: #6a1b9a; }

/* === Misc === */
.loading-overlay {
  position: fixed; inset: 0; background: rgba(255,255,255,.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 9999;
}
.table-responsive { border-radius: 8px; overflow: auto; }

@media (max-width: 767px) {
  #sidebar { transform: translateX(-100%); }
  #sidebar.show { transform: translateX(0); }
  #main { margin-left: 0; }
  #topbar { padding: .5rem 1rem; }
  #content { padding: 1rem; }
}
