@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');

:root {
  --bg-color: #f7f9fc;
  --panel-bg: #ffffff;
  --text-primary: #1e293b;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --border-color: #e2e8f0;
  
  --brand: #2563eb;
  --brand-hover: #1d4ed8;
  --brand-soft: #eff6ff;
  --danger: #ef4444;
  --danger-soft: #fef2f2;
  --success: #10b981;
  --warning: #f59e0b;
  
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.05), 0 2px 4px -2px rgb(0 0 0 / 0.05);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.05), 0 4px 6px -4px rgb(0 0 0 / 0.05);
  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 16px;
  
  /* Course Colors */
  --c0: #ed7d6d;
  --c1: #eebb71;
  --c2: #8fd3ad;
  --c3: #74ccbf;
  --c4: #65aadd;
  --c5: #d285b0;
}

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

body {
  background-color: var(--bg-color);
  color: var(--text-primary);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a {
  color: inherit;
  text-decoration: none;
}

button, input, select {
  font-family: inherit;
  color: inherit;
  outline: none;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* Layout */
.page-shell {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Header */
.site-header {
  background: var(--panel-bg);
  border-radius: var(--radius-lg);
  padding: 12px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: var(--shadow-sm);
}

.brand {
  display: flex;
  align-items: center;
  font-size: 1.125rem;
  font-weight: 700;
  color: var(--brand);
}

.site-nav {
  display: flex;
  gap: 16px;
  align-items: center;
}

.nav-link {
  font-weight: 500;
  font-size: 0.9rem;
  color: var(--text-secondary);
  transition: color 0.15s;
}

.nav-link:hover {
  color: var(--brand);
}

.admin-flag {
  color: var(--warning);
}

/* Components */
.panel {
  background: var(--panel-bg);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border-color);
}

/* Flash Messages */
.flash-stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.flash {
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-weight: 500;
}
.flash-success { background: #dcfce7; color: #166534; }
.flash-error { background: #fee2e2; color: #991b1b; }

/* Buttons */
button, .primary-button, .secondary-button, .ghost-button, .danger-button {
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: 0.9rem;
  padding: 10px 18px;
  border: none;
  transition: all 0.15s;
}

.primary-button {
  background: var(--brand);
  color: white;
  box-shadow: var(--shadow-sm);
}
.primary-button:hover { background: var(--brand-hover); }

.secondary-button {
  background: var(--brand-soft);
  color: var(--brand);
}
.secondary-button:hover { background: #dbeafe; }

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

.danger-button {
  background: var(--danger-soft);
  color: var(--danger);
}
.danger-button:hover { background: #fecaca; }

.small {
  padding: 6px 12px;
  font-size: 0.8rem;
  border-radius: var(--radius-sm);
}

.w-full { width: 100%; }

/* Form Elements */
input[type="text"], input[type="password"], select {
  width: 100%;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
  background: transparent;
  font-size: 0.95rem;
  transition: border 0.15s;
}
input:focus, select:focus {
  border-color: var(--brand);
}

.form-grid {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-grid label > span {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 6px;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}
.checkbox-row input { width: auto; }
.checkbox-row span { margin: 0 !important; font-size: 0.9rem !important; font-weight: 500 !important; color: var(--text-secondary) !important; }

.subtle-box {
  background: var(--bg-color);
  border-radius: var(--radius);
  padding: 16px;
}

.inline-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

/* Authentication Panel */
.auth-panel {
  max-width: 440px;
  margin: 40px auto;
}
.panel-heading {
  margin-bottom: 24px;
}
.panel-heading h1 {
  font-size: 1.5rem;
  margin-bottom: 8px;
}
.panel-heading p {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

/* Hero Card */
.hero-card {
  background: var(--panel-bg);
  border-radius: var(--radius-lg);
  padding: 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border-color);
}
.compact-hero {
  padding: 14px 20px;
}
.hero-content h1 {
  font-size: 1.25rem;
  font-weight: 700;
  margin-bottom: 4px;
}
.hero-subtitle {
  color: var(--text-secondary);
  font-size: 0.85rem;
  line-height: 1.4;
}

.desktop-only { display: inline; }
.mobile-only { display: none; }

/* Filter Panel */
.filter-panel {
  padding: 12px 16px;
}
.compact-panel {
  padding: 8px 12px;
}
.filter-toolbar {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
}
.filter-group, .pagination-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.compact-select {
  padding: 6px 28px 6px 10px;
  height: 32px;
  font-size: 0.85rem;
  border-radius: var(--radius-sm);
  background-color: var(--bg-color);
}
.wide-select { max-width: 140px; }
.short-select { max-width: 90px; }

.nav-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  background: var(--bg-color);
  border: 1px solid var(--border-color);
  transition: all 0.15s;
}
.nav-button:hover:not(.disabled) {
  background: var(--border-color);
  color: var(--text-primary);
}
.nav-button.disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.current-week-label {
  font-weight: 600;
  font-size: 0.9rem;
  padding: 0 4px;
  display: flex;
  align-items: center;
  gap: 4px;
}
.sync-error-dot {
  display: inline-flex;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--danger);
  color: white;
  font-size: 10px;
  align-items: center;
  justify-content: center;
  font-weight: bold;
}

/* Timetable Core - The CSS Grid */
.timetable-wrapper {
  background: var(--panel-bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border-color);
  overflow: hidden;
  position: relative;
}

.timetable-scroll {
  overflow-x: auto;
  overflow-y: hidden;
  /* hide scrollbar for sleek look but keep scrollable */
  scrollbar-width: thin;
  scrollbar-color: var(--border-color) transparent;
}

.timetable-scroll::-webkit-scrollbar {
  height: 4px;
  background: transparent;
}
.timetable-scroll::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 4px;
}

.timetable-grid {
  display: grid;
  /*
    Mobile first: very compact.
    1st column uses approx 36px, the remaining 7 columns split the space.
  */
  grid-template-columns: 36px repeat(7, minmax(40px, 1fr));
  grid-auto-rows: 60px; /* Base height for 1 period on mobile */
  gap: 1px;
  background-color: transparent;
  min-width: 320px;
  /* Grid border inside logic */
}

/* Headers for Grid */
.grid-head-corner, .grid-head-day {
  position: sticky;
  top: 0;
  z-index: 10;
  background: #f8fafc;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  text-align: center;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border-color);
  padding: 8px 0;
}

.grid-head-corner {
  background: var(--panel-bg);
}

.day-name {
  line-height: 1.2;
}

/* Time axis */
.time-cell {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  border-right: 1px solid var(--border-color);
  color: var(--text-muted);
}
.section-no {
  font-weight: 700;
  font-size: 0.8rem;
  color: var(--text-secondary);
}
.time-range {
  font-size: 0.55rem;
  color: var(--text-muted);
  margin-top: 2px;
  line-height: 1.2;
}

/* Background grid overlay */
.grid-bg-cell {
  border-bottom: 1px dashed var(--border-color);
  border-right: 1px dashed rgba(226, 232, 240, 0.4);
}
/* No right border on Sunday */
.grid-bg-cell:nth-child(7n) {
  border-right: none;
}

/* Courses */
.course-wrapper {
  padding: 1px;
  z-index: 5;
}

.course-card {
  height: 100%;
  border-radius: 6px;
  overflow: hidden;
  transition: transform 0.1s, filter 0.15s;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.05);
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: stretch;
  text-align: center;
  cursor: pointer;
}
.course-card:hover {
  filter: brightness(0.95);
}

.course-card-inner {
  padding: 4px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  height: 100%;
  overflow: hidden;
}

.course-title {
  font-size: 0.7rem;
  font-weight: 600;
  line-height: 1.15;
  margin: 0;
  /* Word break beautifully */
  word-break: break-all;
  overflow-wrap: break-word;
  white-space: normal;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  overflow: hidden;
}

.course-room, .course-teacher {
  font-size: 0.65rem;
  opacity: 0.95;
  margin: 0;
  /* Allow wrapping on 2 lines for room names to prevent cutting off too early */
  white-space: normal;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
  word-break: break-all;
}

.course-teacher { display: none; } /* Hide teacher on mobile by default to spare vertical space */

/* Course Category Colors */
.cache-color-0 { background-color: var(--c0); color: #fff; }
.cache-color-1 { background-color: var(--c1); color: #fff; }
.cache-color-2 { background-color: var(--c2); color: #1e293b; }
.cache-color-3 { background-color: var(--c3); color: #1e293b; }
.cache-color-4 { background-color: var(--c4); color: #fff; }
.cache-color-5 { background-color: var(--c5); color: #fff; }


/* Modal Styles */
.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  backdrop-filter: blur(2px);
}
.modal-content {
  background: var(--panel-bg);
  padding: 24px;
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 340px;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.modal-content h2 {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0;
  line-height: 1.4;
}
.modal-body p {
  font-size: 0.95rem;
  color: var(--text-secondary);
  margin-bottom: 8px;
  line-height: 1.5;
}
.modal-body strong {
  color: var(--text-primary);
}


/* Responsive Adjustments */
@media (min-width: 768px) {
  .page-shell { padding: 24px; }
  
  .hero-card {
    padding: 32px;
  }
  .compact-hero {
    padding: 20px 24px;
  }
  .hero-content h1 { font-size: 1.5rem !important; margin-bottom: 8px; }
  .hero-subtitle { font-size: 0.95rem !important; }
  
  .desktop-only { display: inline; }
  .mobile-only { display: none; }
  
  .inline-grid { grid-template-columns: 1fr 1fr; }
  
  .timetable-grid {
    grid-template-columns: 60px repeat(7, minmax(130px, 1fr));
    grid-auto-rows: 85px; /* Taller on desktop */
  }
  
  .grid-head-corner, .grid-head-day {
    font-size: 0.95rem;
    padding: 12px 0;
  }

  .time-cell {
    padding: 4px;
  }
  .section-no { font-size: 0.95rem; }
  .time-range {
    display: block;
    font-size: 0.75rem;
    margin-top: 4px;
  }
  
  .course-wrapper {
    padding: 2px 4px;
  }
  .course-card {
    border-radius: 10px;
  }
  .course-card-inner {
    padding: 8px;
    gap: 4px;
  }

  .course-title {
    font-size: 0.85rem;
    -webkit-line-clamp: 4;
    line-clamp: 4;
  }
  .course-room, .course-teacher {
    font-size: 0.75rem;
  }
  .course-teacher { display: -webkit-box; }
}

@media (max-width: 500px) {
  .site-nav { gap: 10px; }
  .nav-link { font-size: 0.8rem; }
  
  .desktop-only { display: none; }
  .mobile-only { display: block; }

  .filter-toolbar {
    flex-direction: row; /* Still keep flex horizontal but allow wrap */
    flex-wrap: nowrap;
    overflow-x: auto;
  }
  .filter-group {
    flex: 1;
    min-width: max-content;
  }
  /* make the grid super tight! */
  .timetable-grid {
    grid-template-columns: 32px repeat(7, minmax(38px, 1fr));
    grid-auto-rows: 46px; /* A bit denser row height */
  }
  .section-no { font-size: 0.65rem; }
  
  .course-title {
    font-size: 0.6rem; /* Even smaller font to fit inside tiny boxes */
    line-height: 1.1;
  }
  .course-room {
    font-size: 0.55rem;
    margin-top: 1px;
  }
  .grid-head-day {
    font-size: 0.65rem;
  }
}

/* Admin / Generic Tables */
.table-shell {
  overflow-x: auto;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
  background: var(--panel-bg);
}
table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
  min-width: 600px;
}
th, td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  font-size: 0.9rem;
  color: var(--text-primary);
}
th {
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--bg-color);
  white-space: nowrap;
}
tr:last-child td {
  border-bottom: none;
}
tr:hover td {
  background: rgba(0, 0, 0, 0.015);
}

/* Admin Dashboard Elements */
.admin-links {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}
.action-stack {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
}
.stat-card {
  background: var(--panel-bg);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.stat-card span {
  font-size: 0.9rem;
  color: var(--text-secondary);
  font-weight: 500;
}
.stat-card strong {
  font-size: 2rem;
  font-weight: 700;
  color: var(--brand);
  line-height: 1.1;
}

/* Spinner Animation */
@keyframes spin {
  to { transform: rotate(360deg); }
}

.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: currentColor;
  animation: spin 0.8s ease-in-out infinite;
  margin-right: 8px;
  vertical-align: middle;
}

/* Login Hint Styles */
.hint-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--bg-color);
  color: var(--text-muted);
  font-size: 10px;
  font-weight: bold;
  cursor: pointer;
  margin-left: 6px;
  text-decoration: none;
  transition: all 0.2s;
  border: 1px solid var(--border-color);
}
.hint-icon:hover {
  background: var(--brand);
  color: white;
  border-color: var(--brand);
}
.tip-image-container {
  display: none;
  margin: 6px 0 8px 0;
  animation: fadeIn 0.15s ease-in-out;
}
.tip-image-container img {
  width: 100%;
  border-radius: 6px;
  border: 1px solid var(--border-color);
  box-shadow: var(--shadow-sm);
}
.tip-image-container p {
  font-size: 11px;
  color: var(--text-secondary);
  margin-bottom: 6px;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Weaken the CAS checkbox */
.weak-checkbox {
  opacity: 0.65;
  transform: scale(0.95);
  transform-origin: left center;
  margin-top: -6px;
  margin-bottom: 6px;
  transition: opacity 0.2s;
}
.weak-checkbox:hover {
  opacity: 1;
}
.weak-cas-label {
  display: flex !important;
  flex-direction: column !important;
  gap: 2px !important;
}
.cas-hint-text {
  font-size: 0.75rem !important;
  color: var(--text-muted) !important;
  font-weight: normal !important;
}

/* =========================================
   Admin Panel Styles 
   ========================================= */

.admin-header {
  margin-bottom: 2rem;
  padding: 0 0.5rem;
}

.admin-title {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--text-dark);
  margin-bottom: 0.5rem;
  letter-spacing: -0.02em;
}

.admin-subtitle {
  color: var(--text-muted);
  font-size: 0.95rem;
  margin-top: 0;
}

/* Stat Grid */
.admin-stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1.25rem;
  margin-bottom: 2rem;
}

.admin-stat-card {
  background: var(--surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 1.25rem 1.5rem;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.admin-stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.04);
}

.admin-stat-card .stat-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.75rem;
}

.admin-stat-card .stat-value {
  font-size: 1.8rem;
  font-weight: 800;
  color: var(--text-dark);
  line-height: 1.1;
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
}

.stat-divider {
  font-size: 1.2rem;
  font-weight: 400;
  color: var(--border-color-dark);
}

/* Tables & Panels */
.admin-panel {
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  border: 1px solid var(--border-color);
  background: var(--surface);
  margin-bottom: 2rem;
}

.admin-panel .panel-heading {
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border-color);
  background: var(--surface);
}

.admin-panel .panel-heading h2 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
}

.table-responsive {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.admin-table {
  width: 100%;
  min-width: 700px;
  border-collapse: collapse;
  text-align: left;
}

.admin-table th,
.admin-table td {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border-color);
  vertical-align: middle;
}

.admin-table th {
  background: var(--bg-body);
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.admin-table tbody tr:hover {
  background-color: rgba(0, 0, 0, 0.015);
}

.admin-table tbody tr:last-child td {
  border-bottom: none;
}

/* Utilities */
.text-center { text-align: center; }
.font-medium { font-weight: 500; color: var(--text-dark); }
.font-mono { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; }
.text-sm { font-size: 0.875rem; }
.text-muted { color: var(--text-muted); }
.text-danger { color: var(--danger); font-weight: 500; }

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.25rem 0.6rem;
  font-size: 0.75rem;
  font-weight: 600;
  border-radius: 9999px;
  line-height: 1.2;
  white-space: nowrap;
}

.badge-outline {
  border: 1px solid var(--border-color-dark);
  color: var(--text-muted);
  background: transparent;
}

.badge-success {
  background: rgba(16, 185, 129, 0.1);
  color: #059669;
}

.badge-danger {
  background: rgba(239, 68, 68, 0.1);
  color: #dc2626;
}

.badge-warning {
  background: rgba(245, 158, 11, 0.1);
  color: #d97706;
}

.badge-blue {
  background: rgba(37, 99, 235, 0.1);
  color: #1d4ed8;
}

/* Table Actions */
.table-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.inline-form {
  margin: 0;
}

.btn-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.35rem 0.8rem;
  font-size: 0.75rem;
  font-weight: 600;
  border-radius: 6px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: all 0.2s;
  background: transparent;
}

.btn-sync {
  background: rgba(37, 99, 235, 0.05);
  color: var(--brand);
  border-color: rgba(37, 99, 235, 0.1);
}

.btn-sync:hover {
  background: rgba(37, 99, 235, 0.1);
  border-color: rgba(37, 99, 235, 0.2);
}

.btn-toggle {
  color: var(--text-muted);
  border-color: var(--border-color);
}

.btn-toggle:hover {
  color: var(--text-dark);
  background: var(--bg-body);
  border-color: var(--border-color-dark);
}