/* ============================================================
   GradeFlow — SaaS Style Design System
   ============================================================ */

:root {
  --primary: #6366f1;
  --primary-dark: #4f46e5;
  --primary-light: #e0e7ff;
  --success: #10b981;
  --success-dark: #059669;
  --warning: #f59e0b;
  --danger: #ef4444;
  --export: #0ea5e9;
  --export-dark: #0284c7;

  --bg: #f8fafc;
  --bg-card: #ffffff;
  --bg-hover: #f1f5f9;
  --border: #e2e8f0;
  --border-light: #f1f5f9;

  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --text-white: #ffffff;

  --sidebar-bg: #0f172a;
  --sidebar-hover: rgba(255,255,255,0.08);
  --sidebar-active: rgba(99,102,241,0.25);
  --sidebar-text: #cbd5e1;
  --sidebar-active-text: #a5b4fc;

  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow: 0 4px 6px -1px rgba(0,0,0,0.07), 0 2px 4px -2px rgba(0,0,0,0.05);
  --shadow-md: 0 10px 15px -3px rgba(0,0,0,0.08), 0 4px 6px -4px rgba(0,0,0,0.05);
  --shadow-lg: 0 20px 25px -5px rgba(0,0,0,0.1), 0 8px 10px -6px rgba(0,0,0,0.05);

  --radius: 10px;
  --radius-sm: 6px;
  --radius-lg: 16px;
  --sidebar-width: 240px;
  --topbar-height: 60px;

  --font: 'Inter', 'Sarabun', sans-serif;
  --transition: 0.18s cubic-bezier(0.4, 0, 0.2, 1);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 15px; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text-primary);
  display: flex;
  min-height: 100vh;
  overflow: hidden;
}

/* ============================================================
   SIDEBAR
   ============================================================ */
.sidebar {
  width: var(--sidebar-width);
  min-height: 100vh;
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  position: fixed;
  left: 0; top: 0; bottom: 0;
  z-index: 100;
  transition: transform var(--transition);
  box-shadow: 4px 0 24px rgba(0,0,0,0.15);
}

.sidebar.collapsed { transform: translateX(-100%); }

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px 20px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
}

.logo-icon {
  width: 36px; height: 36px;
  background: linear-gradient(135deg, var(--primary), #818cf8);
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

.logo-text {
  font-size: 1.1rem;
  font-weight: 700;
  color: #fff;
  letter-spacing: -0.3px;
}

.sidebar-nav {
  flex: 1;
  padding: 16px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-section-label {
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: #475569;
  padding: 8px 10px 4px;
  text-transform: uppercase;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--sidebar-text);
  font-size: 0.875rem;
  font-weight: 500;
  transition: background var(--transition), color var(--transition);
  text-decoration: none;
  user-select: none;
}

.nav-item:hover { background: var(--sidebar-hover); color: #f1f5f9; }
.nav-item.active { background: var(--sidebar-active); color: var(--sidebar-active-text); }

.sidebar-footer {
  padding: 16px;
  border-top: 1px solid rgba(255,255,255,0.06);
}

.sidebar-user {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-avatar {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), #818cf8);
  color: white;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
  font-size: 0.875rem;
}

.user-info .user-name { color: #f1f5f9; font-size: 0.8rem; font-weight: 600; }
.user-info .user-role { color: #64748b; font-size: 0.7rem; }

/* ============================================================
   MAIN WRAPPER
   ============================================================ */
.main-wrapper {
  margin-left: var(--sidebar-width);
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  transition: margin-left var(--transition);
  overflow: hidden;
}

.main-wrapper.expanded { margin-left: 0; }

/* ============================================================
   TOPBAR
   ============================================================ */
.topbar {
  height: var(--topbar-height);
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: var(--shadow-sm);
}

.topbar-left { display: flex; align-items: center; gap: 14px; }

.sidebar-toggle {
  width: 36px; height: 36px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: transparent;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-secondary);
  transition: background var(--transition), color var(--transition);
}
.sidebar-toggle:hover { background: var(--bg-hover); color: var(--text-primary); }

.breadcrumb {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--text-secondary);
}
.breadcrumb .bc-sep { color: var(--text-muted); }
.breadcrumb .bc-link { cursor: pointer; color: var(--text-secondary); }
.breadcrumb .bc-link:hover { color: var(--primary); }
.breadcrumb .bc-current { color: var(--text-primary); font-weight: 600; }

.save-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  color: var(--success);
  font-weight: 500;
}

/* ============================================================
   CONTENT AREA
   ============================================================ */
.content {
  flex: 1;
  padding: 28px 32px;
  overflow-y: auto;
  max-height: calc(100vh - var(--topbar-height));
}

.page.hidden { display: none; }

/* ============================================================
   PAGE HEADER
   ============================================================ */
.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 28px;
  gap: 16px;
}

.page-title {
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--text-primary);
  letter-spacing: -0.5px;
}

.page-subtitle {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.header-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

/* ============================================================
   STATS GRID
   ============================================================ */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 32px;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  box-shadow: var(--shadow-sm);
  transition: box-shadow var(--transition), transform var(--transition);
}
.stat-card:hover { box-shadow: var(--shadow); transform: translateY(-1px); }

.stat-icon {
  width: 48px; height: 48px;
  border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

.stat-number {
  font-size: 2rem;
  font-weight: 800;
  color: var(--text-primary);
  line-height: 1;
}

.stat-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 3px;
  font-weight: 500;
}

/* ============================================================
   SECTION HEADER
   ============================================================ */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.section-title {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-primary);
}

/* ============================================================
   SUBJECTS / CLASSROOMS GRID
   ============================================================ */
.subjects-grid, .classrooms-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}

.subject-card, .classroom-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  cursor: pointer;
  transition: box-shadow var(--transition), transform var(--transition), border-color var(--transition);
  position: relative;
  overflow: hidden;
}

.subject-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--primary), #818cf8);
  border-radius: var(--radius) var(--radius) 0 0;
}

.classroom-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: linear-gradient(90deg, #06b6d4, #0ea5e9);
  border-radius: var(--radius) var(--radius) 0 0;
}

.subject-card:hover, .classroom-card:hover {
  box-shadow: var(--shadow-md);
  transform: translateY(-2px);
  border-color: var(--primary);
}

.card-badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 600;
  background: var(--primary-light);
  color: var(--primary);
  margin-bottom: 10px;
}

.card-title {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 6px;
}

.card-meta {
  font-size: 0.78rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 6px;
}

.card-actions {
  display: flex;
  gap: 6px;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--border-light);
}

/* ============================================================
   BUTTONS
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
  font-weight: 600;
  font-family: var(--font);
  border: none;
  cursor: pointer;
  transition: background var(--transition), box-shadow var(--transition), transform var(--transition);
  white-space: nowrap;
  text-decoration: none;
}

.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--primary);
  color: white;
}
.btn-primary:hover { background: var(--primary-dark); box-shadow: 0 4px 12px rgba(99,102,241,0.35); }

.btn-success {
  background: var(--success);
  color: white;
}
.btn-success:hover { background: var(--success-dark); box-shadow: 0 4px 12px rgba(16,185,129,0.35); }

.btn-export {
  background: var(--export);
  color: white;
}
.btn-export:hover { background: var(--export-dark); box-shadow: 0 4px 12px rgba(14,165,233,0.35); }

.btn-outline {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
}
.btn-outline:hover { background: var(--bg-hover); color: var(--text-primary); }

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
}
.btn-ghost:hover { background: var(--bg-hover); }

.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid #fecaca;
}
.btn-danger:hover { background: #fef2f2; }

.btn-sm { padding: 5px 12px; font-size: 0.75rem; }
.btn-xs { padding: 3px 8px; font-size: 0.7rem; }
.btn-icon {
  padding: 6px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
}
.btn-icon:hover { background: var(--bg-hover); color: var(--danger); }

/* ============================================================
   TABLE
   ============================================================ */
.table-wrapper {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.table-scroll {
  overflow-x: auto;
  max-height: calc(100vh - 220px);
  overflow-y: auto;
}

.grade-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.grade-table thead {
  position: sticky;
  top: 0;
  z-index: 10;
}

.grade-table thead tr:first-child th {
  background: #1e293b;
  color: #f1f5f9;
  font-weight: 600;
  font-size: 0.75rem;
  letter-spacing: 0.03em;
  padding: 12px 14px;
  text-align: center;
  border-bottom: 1px solid #334155;
  white-space: nowrap;
}

.grade-table thead tr:nth-child(2) th {
  background: #f8fafc;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.7rem;
  padding: 8px 14px;
  border-bottom: 2px solid var(--border);
  text-align: center;
}

.grade-table tbody tr {
  border-bottom: 1px solid var(--border-light);
  transition: background var(--transition);
}

.grade-table tbody tr:hover { background: #f8fafc; }
.grade-table tbody tr:last-child { border-bottom: none; }

.grade-table td {
  padding: 8px 14px;
  text-align: center;
  color: var(--text-primary);
  vertical-align: middle;
}

.td-order {
  width: 40px;
  color: var(--text-muted);
  font-weight: 600;
}

.td-name {
  text-align: left;
  white-space: nowrap;
  font-weight: 500;
}

.td-id {
  color: var(--text-secondary);
  font-size: 0.75rem;
  font-family: monospace;
}

.score-input {
  width: 64px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
  text-align: center;
  background: #f8fafc;
  color: var(--text-primary);
  font-family: var(--font);
  transition: border-color var(--transition), background var(--transition);
  outline: none;
}

.score-input:focus {
  border-color: var(--primary);
  background: white;
  box-shadow: 0 0 0 3px rgba(99,102,241,0.12);
}

.td-total {
  font-weight: 700;
  color: var(--text-primary);
}

.td-percent {
  font-weight: 600;
  color: var(--text-secondary);
}

/* Grade badges */
.grade-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 36px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.03em;
}

.grade-A  { background: #d1fae5; color: #065f46; }
.grade-Bp { background: #dbeafe; color: #1e40af; }
.grade-B  { background: #e0e7ff; color: #3730a3; }
.grade-Cp { background: #fef9c3; color: #92400e; }
.grade-C  { background: #fef3c7; color: #92400e; }
.grade-Dp { background: #ffedd5; color: #9a3412; }
.grade-D  { background: #fee2e2; color: #991b1b; }
.grade-F  { background: #f1f5f9; color: #64748b; }

.th-assignment { min-width: 90px; }
.th-max { font-size: 0.65rem; color: var(--text-muted); font-weight: 400; }

.col-sticky {
  position: sticky;
  left: 0;
  background: white;
  z-index: 5;
  box-shadow: 2px 0 4px rgba(0,0,0,0.04);
}

.grade-table thead th.col-sticky {
  background: #1e293b;
  z-index: 15;
}
.grade-table thead tr:nth-child(2) th.col-sticky {
  background: #f8fafc;
  z-index: 15;
}

.grade-table tbody tr:hover .col-sticky { background: #f8fafc; }

/* ============================================================
   MODAL
   ============================================================ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15,23,42,0.55);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 16px;
  animation: fadeIn 0.15s ease;
}

.modal-overlay.hidden { display: none; }

@keyframes fadeIn { from { opacity:0 } to { opacity:1 } }
@keyframes slideUp { from { opacity:0; transform: translateY(20px) } to { opacity:1; transform: translateY(0) } }

.modal {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 460px;
  box-shadow: var(--shadow-lg);
  animation: slideUp 0.2s ease;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px 16px;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-primary);
}

.modal-close {
  width: 28px; height: 28px;
  border-radius: 6px;
  border: none;
  background: var(--bg-hover);
  color: var(--text-muted);
  font-size: 1.2rem;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background var(--transition);
}
.modal-close:hover { background: #fee2e2; color: var(--danger); }

.modal-body { padding: 20px 24px; }
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 16px 24px 20px;
  border-top: 1px solid var(--border);
}

/* ============================================================
   FORMS
   ============================================================ */
.form-group { margin-bottom: 16px; }
.form-row { display: flex; gap: 12px; }

.form-label {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.required { color: var(--danger); }

.form-input {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-family: var(--font);
  color: var(--text-primary);
  background: var(--bg);
  transition: border-color var(--transition), box-shadow var(--transition);
  outline: none;
}

.form-input:focus {
  border-color: var(--primary);
  background: white;
  box-shadow: 0 0 0 3px rgba(99,102,241,0.12);
}

/* ============================================================
   TOAST
   ============================================================ */
.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: #0f172a;
  color: #f1f5f9;
  padding: 12px 18px;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  font-weight: 500;
  z-index: 2000;
  box-shadow: var(--shadow-lg);
  animation: slideUp 0.2s ease;
}

.toast.hidden { display: none; }
.toast svg { color: var(--success); }

/* ============================================================
   LOADING
   ============================================================ */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(255,255,255,0.7);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 3000;
}
.loading-overlay.hidden { display: none; }

.spinner {
  width: 40px; height: 40px;
  border: 3px solid var(--primary-light);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ============================================================
   EMPTY STATE
   ============================================================ */
.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.empty-state svg { opacity: 0.4; }
.empty-state p { font-size: 0.9rem; }

/* ============================================================
   ACTION CONFIRM ICON
   ============================================================ */
.del-btn {
  padding: 4px;
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  border-radius: 4px;
  display: inline-flex;
  align-items: center;
  transition: color var(--transition), background var(--transition);
}
.del-btn:hover { color: var(--danger); background: #fee2e2; }

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .main-wrapper { margin-left: 0; }
  .content { padding: 16px; }
  .page-header { flex-direction: column; }
  .header-actions { width: 100%; }
}

/* ============================================================
   IMPORT MODAL
   ============================================================ */
.modal-lg { max-width: 640px; }

.import-tabs {
  display: flex; gap: 4px; margin-bottom: 16px;
  background: var(--bg); padding: 4px;
  border-radius: var(--radius-sm); border: 1px solid var(--border);
}

.import-tab {
  flex: 1; display: flex; align-items: center; justify-content: center;
  gap: 7px; padding: 8px 14px; border-radius: 5px; border: none;
  background: transparent; color: var(--text-muted);
  font-size: 0.82rem; font-weight: 600; font-family: var(--font);
  cursor: pointer; transition: background var(--transition), color var(--transition);
}
.import-tab:hover { background: var(--bg-hover); color: var(--text-secondary); }
.import-tab.active { background: white; color: var(--primary); box-shadow: var(--shadow-sm); }

.import-hint {
  background: #eff6ff; border: 1px solid #bfdbfe;
  border-radius: var(--radius-sm); padding: 10px 14px;
  font-size: 0.8rem; color: #1e40af; margin-bottom: 12px;
  display: flex; align-items: flex-start; gap: 8px; line-height: 1.6;
}
.import-hint svg { flex-shrink: 0; margin-top: 2px; }
.hint-example { color: #3b82f6; font-size: 0.75rem; font-family: monospace; }

.import-textarea {
  font-family: monospace; font-size: 0.82rem;
  resize: vertical; min-height: 160px; line-height: 1.7;
}

.file-drop-zone {
  border: 2px dashed var(--border); border-radius: var(--radius);
  padding: 32px; text-align: center; cursor: pointer;
  transition: border-color var(--transition), background var(--transition);
  display: flex; flex-direction: column; align-items: center;
  gap: 8px; margin-bottom: 16px;
}
.file-drop-zone:hover, .file-drop-zone.drag-over { border-color: var(--primary); background: var(--primary-light); }
.file-drop-zone p { font-size: 0.9rem; color: var(--text-secondary); font-weight: 500; }
.file-drop-hint { font-size: 0.72rem; color: var(--text-muted); }

.excel-col-map {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 14px; margin-bottom: 14px;
}

.import-preview {
  margin-top: 14px; max-height: 220px; overflow-y: auto;
  border-radius: var(--radius-sm); border: 1px solid var(--border); display: none;
}
.import-preview.visible { display: block; }
.import-preview table { width: 100%; border-collapse: collapse; font-size: 0.78rem; }
.import-preview th { background: #1e293b; color: #f1f5f9; padding: 7px 12px; text-align: left; font-weight: 600; white-space: nowrap; }
.import-preview td { padding: 6px 12px; border-bottom: 1px solid var(--border-light); color: var(--text-primary); }
.import-preview tr:last-child td { border-bottom: none; }
.import-preview tr:nth-child(even) { background: #f8fafc; }

.import-count-label { font-size: 0.8rem; color: var(--text-muted); font-weight: 500; }
.import-count-label strong { color: var(--primary); }
