:root {
  --bg: #f0f9ff;
  --bg2: #f8fafc;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #0284c7;
  --primary-light: #e0f2fe;
  --pass: #059669;
  --pass-bg: #ecfdf5;
  --fail: #dc2626;
  --fail-bg: #fef2f2;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 4px 16px rgba(15, 23, 42, 0.04);
  --radius: 16px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: system-ui, -apple-system, 'Segoe UI', 'Noto Sans Thai', sans-serif;
  background: linear-gradient(180deg, var(--bg) 0%, var(--bg2) 40%);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
}

.topbar {
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

.topbar-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0.75rem 1rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.brand-icon {
  width: 2.25rem;
  height: 2.25rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--primary-light);
  color: var(--primary);
  border-radius: 12px;
  font-size: 0.9rem;
}

.brand h1 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
}

.brand-sub {
  margin: 0;
  font-size: 0.7rem;
  color: var(--muted);
}

.nav-tabs {
  display: flex;
  gap: 0.35rem;
  background: var(--bg2);
  padding: 0.25rem;
  border-radius: 12px;
  border: 1px solid var(--border);
}

.nav-tab {
  border: none;
  background: transparent;
  padding: 0.45rem 1rem;
  border-radius: 10px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}

.nav-tab.active {
  background: #fff;
  color: var(--primary);
  box-shadow: var(--shadow);
}

.main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1rem;
}

.page { display: none; }
.page.active { display: block; }

.panel {
  background: var(--card);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 1rem;
  box-shadow: var(--shadow);
}

.panel-title {
  margin: 0 0 0.75rem;
  font-size: 0.95rem;
  font-weight: 700;
}

.filter-panel .date-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.75rem;
  color: var(--muted);
  font-weight: 600;
}

.field-full { margin-bottom: 0.5rem; }

.field input {
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 0.9rem;
  color: var(--text);
  background: #fff;
}

.quick-dates {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin: 0.75rem 0;
}

.chip {
  border: 1px solid var(--border);
  background: var(--bg2);
  border-radius: 999px;
  padding: 0.3rem 0.75rem;
  font-size: 0.75rem;
  cursor: pointer;
  color: var(--muted);
}

.chip:hover { border-color: var(--primary); color: var(--primary); }

.btn {
  border: none;
  border-radius: 10px;
  padding: 0.6rem 1.2rem;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
  width: 100%;
  margin-top: 0.25rem;
}

.hint {
  font-size: 0.75rem;
  color: var(--muted);
  margin: 0 0 0.5rem;
}

.status-msg {
  font-size: 0.8rem;
  color: var(--muted);
  margin: 0.75rem 0 0.25rem;
  min-height: 1.2em;
}

.status-msg.error { color: var(--fail); }

.type-section {
  margin-top: 1.25rem;
}

.type-heading {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--primary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 0 0 0.65rem;
  padding-bottom: 0.35rem;
  border-bottom: 2px solid var(--primary-light);
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 0.75rem;
}

.summary-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  text-align: left;
  width: 100%;
}

.summary-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 24px rgba(15, 23, 42, 0.08);
}

.summary-card .code {
  font-size: 1rem;
  font-weight: 800;
  font-family: ui-monospace, monospace;
  margin-bottom: 0.35rem;
  word-break: break-all;
}

.summary-card .meta {
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 0.5rem;
}

.stat-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.stat {
  font-size: 0.75rem;
  padding: 0.2rem 0.5rem;
  border-radius: 8px;
  background: var(--bg2);
  font-weight: 600;
}

.stat.pass { background: var(--pass-bg); color: var(--pass); }
.stat.fail { background: var(--fail-bg); color: var(--fail); }

.grade-line {
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Detail overlay */
.detail-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.35);
  z-index: 200;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

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

.detail-sheet {
  background: #fff;
  width: 100%;
  max-width: 1200px;
  max-height: 92vh;
  max-height: 92dvh;
  border-radius: 20px 20px 0 0;
  display: flex;
  flex-direction: column;
  box-shadow: 0 -8px 40px rgba(0,0,0,0.12);
}

@media (min-width: 768px) {
  .detail-overlay { align-items: center; padding: 1rem; }
  .detail-sheet { border-radius: var(--radius); max-height: 88vh; }
}

.detail-head {
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
}

.btn-back {
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 10px;
  padding: 0.4rem 0.65rem;
  font-size: 0.8rem;
  cursor: pointer;
  flex-shrink: 0;
}

.detail-title-wrap { min-width: 0; }
.detail-title-wrap h2 { margin: 0; font-size: 1rem; }
.detail-sub { margin: 0.2rem 0 0; font-size: 0.75rem; color: var(--muted); }

.detail-col-picker {
  margin: 0.75rem 1rem 0;
  padding: 0.75rem;
}

.col-pick-label {
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--muted);
  margin-bottom: 0.4rem;
}

.col-pick-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 0.75rem;
}

.col-pick-grid label {
  font-size: 0.75rem;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  cursor: pointer;
}

.table-scroll-wrap {
  overflow: auto;
  flex: 1;
  margin: 0.75rem 1rem 1rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  -webkit-overflow-scrolling: touch;
}

.detail-table {
  width: max-content;
  min-width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
}

.detail-table th,
.detail-table td {
  padding: 0.45rem 0.65rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.detail-table th {
  background: var(--bg2);
  font-weight: 700;
  position: sticky;
  top: 0;
  z-index: 2;
}

.detail-table .col-sticky {
  position: sticky;
  left: 0;
  z-index: 3;
  background: #fff;
  font-family: ui-monospace, monospace;
  font-weight: 600;
  min-width: 180px;
  max-width: 220px;
  box-shadow: 2px 0 4px rgba(0,0,0,0.04);
}

.detail-table thead .col-sticky {
  background: var(--bg2);
  z-index: 4;
}

.detail-table tbody tr:nth-child(even) td { background: #fafbfc; }
.detail-table tbody tr:nth-child(even) td.col-sticky { background: #f6f8fa; }

.loading {
  position: fixed;
  inset: 0;
  background: rgba(255,255,255,0.75);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 300;
  gap: 0.75rem;
}

.loading.hidden { display: none; }

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

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

.empty-state {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--muted);
  font-size: 0.9rem;
}
