:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #d97706;
  --bg: #f1f5f9;
  --card-bg: #ffffff;
  --sidebar-w: 240px;
}

* { box-sizing: border-box; }

body {
  background: var(--bg);
  font-family: 'Segoe UI', system-ui, sans-serif;
  min-height: 100vh;
  overflow-x: hidden;
}

/* ===== Sidebar ===== */
.sidebar {
  width: var(--sidebar-w);
  min-height: 100vh;
  background: linear-gradient(160deg, #1e3a8a 0%, #2563eb 100%);
  position: fixed;
  top: 0; left: 0;
  z-index: 1000;
  transition: transform 0.3s ease;
  overflow-y: auto;
}

.sidebar-brand {
  padding: 1.5rem 1rem 1rem;
  border-bottom: 1px solid rgba(255,255,255,0.15);
}
.sidebar-brand h5 { color: #fff; font-weight: 700; margin: 0; font-size: 1rem; }
.sidebar-brand small { color: rgba(255,255,255,0.6); font-size: 0.75rem; }

.sidebar .nav-link {
  color: rgba(255,255,255,0.8);
  padding: 0.7rem 1rem;
  border-radius: 8px;
  margin: 2px 8px;
  transition: all 0.2s;
  font-size: 0.875rem;
  display: flex;
  align-items: center;
  gap: 0.6rem;
}
.sidebar .nav-link:hover, .sidebar .nav-link.active {
  color: #fff;
  background: rgba(255,255,255,0.15);
}
.sidebar .nav-link i { width: 18px; text-align: center; }

/* Sidebar close button (mobile only) */
.sidebar-close {
  display: none;
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: rgba(255,255,255,0.2);
  border: none;
  color: #fff;
  font-size: 1.25rem;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  cursor: pointer;
  align-items: center;
  justify-content: center;
}

/* Sidebar overlay (mobile) */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 999;
}
.sidebar-overlay.active { display: block; }

/* ===== Main content ===== */
.main-content {
  margin-left: var(--sidebar-w);
  min-height: 100vh;
}

.topbar {
  background: #fff;
  border-bottom: 1px solid #e2e8f0;
  padding: 0.75rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 50;
  gap: 0.5rem;
  flex-wrap: wrap;
}

/* Hamburger menu (mobile only) */
.menu-toggle {
  display: none;
  background: none;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 0.35rem 0.6rem;
  font-size: 1.25rem;
  color: #1e293b;
  cursor: pointer;
  flex-shrink: 0;
}

.content-area { padding: 1.5rem; }
.page-title { font-size: 1.35rem; font-weight: 700; color: #1e293b; margin-bottom: 0; }

/* ===== Cards ===== */
.stat-card {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 1.25rem 1.5rem;
  border: none;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.stat-card .stat-icon {
  width: 48px; height: 48px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.25rem;
  flex-shrink: 0;
}
.stat-card .stat-value { font-size: 1.75rem; font-weight: 700; line-height: 1; }
.stat-card .stat-label { font-size: 0.8rem; color: #64748b; margin-top: 0.2rem; }

/* ===== Responsive Tables ===== */
.table-responsive-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: 12px;
}

.data-table { background: #fff; border-radius: 12px; overflow: hidden; box-shadow: 0 1px 3px rgba(0,0,0,0.08); }
.data-table .table { margin: 0; }
.data-table .table th { background: #f8fafc; font-size: 0.75rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: #64748b; border-bottom: 1px solid #e2e8f0; padding: 0.75rem 1rem; white-space: nowrap; }
.data-table .table td { padding: 0.75rem 1rem; vertical-align: middle; border-color: #f1f5f9; font-size: 0.875rem; }
.data-table .table tr:last-child td { border-bottom: none; }

/* ===== Attendance checkboxes ===== */
.att-radio { display: flex; gap: 0.5rem; }
.att-btn { cursor: pointer; }
.att-btn input { display: none; }
.att-btn label {
  display: inline-block; padding: 0.2rem 0.65rem;
  border-radius: 20px; font-size: 0.75rem; font-weight: 600;
  cursor: pointer; border: 2px solid transparent;
  transition: all 0.15s; user-select: none;
}
.att-btn.present input:checked + label { background: #dcfce7; color: #16a34a; border-color: #16a34a; }
.att-btn.present label { background: #f0fdf4; color: #86efac; border-color: #bbf7d0; }
.att-btn.absent input:checked + label { background: #fee2e2; color: #dc2626; border-color: #dc2626; }
.att-btn.absent label { background: #fff5f5; color: #fca5a5; border-color: #fecaca; }
.att-btn.leave input:checked + label { background: #fef3c7; color: #d97706; border-color: #d97706; }
.att-btn.leave label { background: #fffbeb; color: #fcd34d; border-color: #fde68a; }

/* ===== Monthly grid ===== */
.monthly-table { font-size: 0.75rem; }
.monthly-table th, .monthly-table td { padding: 0.35rem 0.4rem; text-align: center; min-width: 30px; }
.monthly-table td.name-cell { text-align: left; min-width: 130px; font-size: 0.8rem; }
.cell-P { background: #dcfce7; color: #16a34a; font-weight: 600; border-radius: 4px; }
.cell-A { background: #fee2e2; color: #dc2626; font-weight: 600; border-radius: 4px; }
.cell-L { background: #fef3c7; color: #d97706; font-weight: 600; border-radius: 4px; }
.cell-pct { font-weight: 700; }
.pct-high { color: #16a34a; }
.pct-mid { color: #d97706; }
.pct-low { color: #dc2626; }

/* ===== Badges ===== */
.badge-school { background: #dbeafe; color: #1d4ed8; }
.badge-active { background: #dcfce7; color: #16a34a; }
.badge-inactive { background: #f1f5f9; color: #64748b; }

/* ===== Toast ===== */
#toast-container { position: fixed; bottom: 1.5rem; right: 1.5rem; z-index: 9999; display: flex; flex-direction: column; gap: 0.5rem; }
.toast-msg { padding: 0.75rem 1.25rem; border-radius: 10px; font-size: 0.875rem; font-weight: 500; color: #fff; min-width: 240px; max-width: 90vw; box-shadow: 0 4px 12px rgba(0,0,0,0.15); animation: slideIn 0.3s ease; }
.toast-msg.success { background: #16a34a; }
.toast-msg.error { background: #dc2626; }
.toast-msg.info { background: #2563eb; }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* ===== Capacitor / Mobile App specific ===== */
body.capacitor { padding-top: env(safe-area-inset-top); }

/* ========================================
   RESPONSIVE BREAKPOINTS
   ======================================== */

/* Tablet and below (≤ 991px) */
@media (max-width: 991px) {
  .content-area { padding: 1rem; }
  .topbar { padding: 0.6rem 1rem; }

  .stat-card { padding: 1rem; }
  .stat-card .stat-value { font-size: 1.4rem; }
  .stat-card .stat-icon { width: 40px; height: 40px; font-size: 1rem; }
}

/* Mobile (≤ 768px) */
@media (max-width: 768px) {
  /* Sidebar: hidden by default, slide-in on toggle */
  .sidebar {
    transform: translateX(-100%);
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .sidebar-close {
    display: flex;
  }

  /* Main content: full width */
  .main-content {
    margin-left: 0;
  }

  /* Show hamburger */
  .menu-toggle {
    display: block;
  }

  /* Topbar adjustments */
  .topbar {
    padding: 0.5rem 0.75rem;
  }
  .page-title {
    font-size: 1.1rem;
  }
  .topbar .btn {
    font-size: 0.8rem;
    padding: 0.3rem 0.6rem;
  }
  .topbar .d-flex.gap-2 {
    gap: 0.35rem !important;
  }

  /* Content area */
  .content-area {
    padding: 0.75rem;
  }

  /* Stat cards */
  .stat-card {
    padding: 0.75rem 1rem;
  }
  .stat-card .stat-value { font-size: 1.25rem; }
  .stat-card .stat-icon { width: 36px; height: 36px; font-size: 0.9rem; }
  .stat-card .stat-label { font-size: 0.7rem; }
  .stat-card .d-flex.gap-3 { gap: 0.5rem !important; }

  /* Tables: horizontal scroll */
  .data-table {
    border-radius: 8px;
  }
  .data-table .table th,
  .data-table .table td {
    padding: 0.5rem 0.65rem;
    font-size: 0.8rem;
  }
  .card-body.p-0 {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .table {
    min-width: 500px;
  }

  /* Attendance radio buttons: larger touch targets */
  .att-btn label {
    padding: 0.35rem 0.75rem;
    font-size: 0.8rem;
  }

  /* Monthly table */
  .monthly-table th,
  .monthly-table td {
    padding: 0.25rem 0.3rem;
    font-size: 0.65rem;
    min-width: 26px;
  }
  .monthly-table td.name-cell {
    min-width: 100px;
    font-size: 0.7rem;
  }

  /* Modal adjustments */
  .modal-dialog {
    margin: 0.5rem;
  }

  /* Filter cards */
  .card .row .col-md-3,
  .card .row .col-md-4 {
    margin-bottom: 0.25rem;
  }

  /* Buttons full-width on mobile */
  .topbar .btn-primary,
  .topbar .btn-outline-primary {
    font-size: 0.75rem;
    white-space: nowrap;
  }

  /* Toast position */
  #toast-container {
    bottom: 1rem;
    right: 0.5rem;
    left: 0.5rem;
  }
  .toast-msg {
    min-width: auto;
    width: 100%;
  }
}

/* Small phones (≤ 480px) */
@media (max-width: 480px) {
  .page-title { font-size: 1rem; }

  .stat-card .stat-value { font-size: 1.1rem; }
  .stat-card .stat-icon { width: 32px; height: 32px; font-size: 0.85rem; }

  .content-area { padding: 0.5rem; }

  .data-table .table th,
  .data-table .table td {
    padding: 0.4rem 0.5rem;
    font-size: 0.75rem;
  }

  .att-btn label {
    padding: 0.3rem 0.55rem;
    font-size: 0.7rem;
  }
  .att-radio { gap: 0.3rem; }

  /* Stack topbar buttons below title */
  .topbar {
    flex-wrap: wrap;
  }
}

/* ===== Print ===== */
@media print {
  .sidebar, .sidebar-overlay, .topbar, .no-print, .menu-toggle { display: none !important; }
  .main-content { margin-left: 0 !important; }
  .monthly-table { font-size: 0.65rem; }
  .content-area { padding: 0 !important; }
}
