:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #475569;
  --primary: #1d4ed8;
  --primary-soft: #eff6ff;
  --gold: #b45309;
  --gold-bg: #fef3c7;
  --green: #047857;
  --green-bg: #d1fae5;
  --red: #b91c1c;
  --red-bg: #fee2e2;
  --radius: 14px;
  --shadow: 0 4px 18px rgba(15, 23, 42, 0.06);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Noto Sans Thai", "Plus Jakarta Sans", -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  background: rgba(15, 23, 42, 0.06);
  padding: 0.1rem 0.35rem;
  border-radius: 5px;
  font-size: 0.85em;
}

.container {
  width: 100%;
  max-width: 1680px;
  margin: 0 auto;
  padding: 0 22px;
}

.topbar {
  background: linear-gradient(135deg, #0f172a 0%, #1e3a8a 100%);
  color: #fff;
  padding: 18px 0;
  box-shadow: 0 4px 20px rgba(15, 23, 42, 0.2);
}

.topbar code {
  background: rgba(255, 255, 255, 0.15);
  color: #e0f2fe;
}

.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

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

.brand-badge {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  color: #fff;
  font-weight: 700;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 0.95rem;
  white-space: nowrap;
}

.topbar h1 {
  margin: 0;
  font-size: 1.22rem;
  font-weight: 700;
}

.subtitle {
  margin: 3px 0 0;
  font-size: 0.88rem;
  color: #cbd5e1;
}

.topbar-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.main-content {
  padding-top: 18px;
  padding-bottom: 40px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 20px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 8px 14px;
  font: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: #1e40af; }

.btn-gold { background: #f59e0b; color: #0f172a; }
.btn-gold:hover { background: #fbbf24; }

.btn-dark { background: #0f172a; color: #fff; }
.btn-dark:hover { background: #1e293b; }

.btn-outline {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  border-color: rgba(255, 255, 255, 0.35);
}
.btn-outline:hover { background: rgba(255, 255, 255, 0.22); }

.pill {
  display: inline-block;
  font-size: 0.78rem;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 999px;
}
.pill-gold { background: var(--gold-bg); color: var(--gold); }
.pill-blue { background: var(--primary-soft); color: var(--primary); }
.pill-green { background: var(--green-bg); color: var(--green); }
.pill-red { background: var(--red-bg); color: var(--red); }

.section-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.section-head h2 { margin: 6px 0 4px; font-size: 1.15rem; }
.muted { color: var(--muted); margin: 0; }
.small { font-size: 0.84rem; }

.best-section { border-top: 4px solid #f59e0b; }

.top5-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}

.rank-card {
  text-align: left;
  background: var(--surface-alt);
  border: 2px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  font: inherit;
  cursor: pointer;
  transition: all 0.15s ease;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.rank-card:hover {
  border-color: #f59e0b;
  background: #fffbeb;
}

.rank-card.active {
  border-color: #d97706;
  background: #fef3c7;
  box-shadow: 0 3px 10px rgba(217, 119, 6, 0.15);
}

.rank-card-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
}

.rank-num {
  font-weight: 700;
  font-size: 0.92rem;
  color: #92400e;
}

.rank-cred {
  font-size: 0.76rem;
  font-weight: 700;
  background: #fff;
  border: 1px solid #e2e8f0;
  padding: 1px 8px;
  border-radius: 999px;
  color: var(--primary);
}

.rank-desc {
  font-size: 0.8rem;
  color: var(--text);
  font-weight: 600;
}

.rank-sub {
  font-size: 0.75rem;
  color: var(--muted);
}

.metrics-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 10px;
  margin-bottom: 10px;
}

.metric-box {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 12px;
}
.metric-label { font-size: 0.78rem; color: var(--muted); }
.metric-val { font-size: 1.12rem; font-weight: 700; margin-top: 2px; }

.reasons-box {
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: 10px;
  padding: 9px 14px;
  font-size: 0.86rem;
  color: #78350f;
}
.reasons-box ul { margin: 4px 0 0; padding-left: 18px; }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.panel-head h2, .panel-head h3 { margin: 0; font-size: 1.08rem; }

.timetable-controls {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.legend {
  display: flex;
  gap: 12px;
  font-size: 0.82rem;
  color: var(--muted);
}
.legend-item { display: inline-flex; align-items: center; gap: 5px; }
.dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.dot.lec { background: #2563eb; }
.dot.lab { background: #0d9488; }

.alert-box {
  background: var(--red-bg);
  color: var(--red);
  border: 1px solid #fecaca;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.86rem;
  margin-bottom: 12px;
}

.timetable-scroll { overflow-x: auto; }

.timetable-grid {
  width: 100%;
  min-width: 960px;
  display: grid;
  /* 1 col day + 24 cols for 08:00..20:00 (each col = 30 min) */
  grid-template-columns: 96px repeat(24, minmax(32px, 1fr));
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  background: #fff;
}

.tt-corner {
  background: #f1f5f9;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  padding: 10px 8px;
  font-size: 0.8rem;
  font-weight: 700;
  text-align: center;
  color: var(--muted);
}

.tt-hour {
  grid-column: span 2;
  background: #f1f5f9;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  padding: 10px 4px;
  font-size: 0.8rem;
  font-weight: 700;
  text-align: center;
  color: var(--muted);
}

.tt-day-label {
  background: #f8fafc;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.94rem;
  padding: 10px 6px;
}

.tt-day-track {
  grid-column: 2 / span 24;
  display: grid;
  grid-template-columns: repeat(24, minmax(32px, 1fr));
  grid-auto-rows: minmax(96px, auto);
  border-bottom: 1px solid var(--border);
  position: relative;
  background-image: linear-gradient(to right, #f1f5f9 1px, transparent 1px);
  background-size: calc(100% / 12) 100%;
}

.tt-day-track.free-day { background-color: #f0fdf4; }

.free-badge {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #15803d;
  font-weight: 700;
  font-size: 0.95rem;
  padding: 16px;
}

.tt-block {
  margin: 5px 3px;
  padding: 8px 10px;
  border-radius: 10px;
  font-size: 0.8rem;
  line-height: 1.35;
  border-left: 5px solid;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
}

.tt-block.clash {
  outline: 2px solid #dc2626;
  background: #fee2e2 !important;
  color: #991b1b !important;
  border-left-color: #dc2626 !important;
}

.tt-block-title {
  font-weight: 700;
  font-size: 0.84rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tt-block-name {
  font-weight: 600;
  font-size: 0.78rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tt-block-sub {
  opacity: 0.92;
  font-size: 0.74rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Fullscreen Timetable Mode (CSS overlay + native Fullscreen API) */
.timetable-card.is-fullscreen {
  position: fixed;
  inset: 0;
  z-index: 9999;
  width: 100vw;
  height: 100vh;
  margin: 0;
  border-radius: 0;
  border: none;
  padding: 18px 24px;
  display: flex;
  flex-direction: column;
  background: #ffffff;
  overflow: auto;
}

.timetable-card.is-fullscreen .timetable-scroll {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.timetable-card.is-fullscreen .timetable-grid {
  flex: 1;
  min-width: 100%;
  grid-template-rows: auto repeat(5, 1fr);
}

.timetable-card.is-fullscreen .tt-day-track {
  grid-auto-rows: 1fr;
  min-height: 112px;
}

.timetable-card:fullscreen {
  width: 100vw;
  height: 100vh;
  padding: 18px 24px;
  display: flex;
  flex-direction: column;
  background: #ffffff;
  overflow: auto;
}

.timetable-card:fullscreen .timetable-scroll {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.timetable-card:fullscreen .timetable-grid {
  flex: 1;
  grid-template-rows: auto repeat(5, 1fr);
}

.timetable-card:fullscreen .tt-day-track {
  grid-auto-rows: 1fr;
  min-height: 112px;
}

/* Picker Grid */
.picker-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 12px;
}

.course-pick {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 13px;
  background: var(--surface-alt);
}

.course-pick-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 4px;
}

.course-code { font-weight: 700; font-size: 0.9rem; color: var(--primary); }
.course-credit { font-size: 0.78rem; color: var(--muted); }
.course-name { font-size: 0.88rem; font-weight: 600; margin-bottom: 7px; }
.course-select {
  width: 100%;
  font: inherit;
  font-size: 0.82rem;
  padding: 7px 9px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background: #fff;
}
.course-meta-line { margin-top: 6px; font-size: 0.78rem; color: var(--muted); }

.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 0.86rem; }
.data-table th, .data-table td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}
.data-table th {
  background: var(--surface-alt);
  font-weight: 700;
  color: var(--muted);
  font-size: 0.8rem;
}
.exam-clash-cell { color: var(--red); font-weight: 700; background: #fef2f2; }

.instructor-tools { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.search-input {
  font: inherit;
  font-size: 0.88rem;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  min-width: 240px;
}
.view-switch {
  display: inline-flex;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px;
}
.btn-tab {
  border: none;
  background: transparent;
  font: inherit;
  font-size: 0.84rem;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 7px;
  color: var(--muted);
  cursor: pointer;
}
.btn-tab.active {
  background: #fff;
  color: var(--primary);
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}

.summary-pills { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }

.instructors-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
  gap: 14px;
}

.inst-card {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  background: var(--surface-alt);
}
.inst-card-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 8px;
}
.inst-card h4 { margin: 0; font-size: 0.96rem; }
.inst-sec-list { display: flex; flex-direction: column; gap: 6px; }
.inst-sec-item {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 0.82rem;
}

.ep-tag {
  display: inline-block;
  background: #ede9fe;
  color: #5b21b6;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 999px;
  margin-left: 4px;
}

.dept-tag {
  display: inline-block;
  background: #dbeafe;
  color: #1e40af;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 999px;
  margin-left: 4px;
}

.footer { padding-bottom: 28px; text-align: center; font-size: 0.82rem; color: var(--muted); }

@media print {
  .topbar-actions, .best-actions, .picker-section, .instructor-tools, #btnToggleFullscreen { display: none !important; }
  body { background: #fff; }
  .card { box-shadow: none; break-inside: avoid; }
}
