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

:root {
  --bg: #f5f7fb;
  --panel: #ffffff;
  --line: #d8e2f0;
  --head: #eaf2ff;
  --text: #172033;
  --muted: #64748b;
  --blue: #2563eb;
  --gray: #64748b;
  --red: #dc2626;
  --ok-bg: #f0fdf4;
  --ok-line: #bbf7d0;
  --err-bg: #fff1f2;
  --err-line: #fecaca;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Sarabun", "Leelawadee UI", Tahoma, Arial, sans-serif;
  font-size: 16px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: center;
  padding: 14px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 1px 0 rgba(15, 23, 42, .03);
}

h1, h2 { margin: 0; }
h1 { font-size: 24px; }
h2 { text-align: center; font-size: 22px; margin-bottom: 12px; }
.brand { color: var(--muted); font-weight: 600; }

nav {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
}

nav a, button, .button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  border: 0;
  border-radius: 8px;
  padding: 8px 14px;
  background: var(--blue);
  color: #fff;
  text-decoration: none;
  font: 700 16px/1.2 "Sarabun", sans-serif;
  cursor: pointer;
}

nav a {
  min-height: 38px;
  border: 0;
  border-radius: 8px;
  background: #f8fbff;
  color: #28415f;
  padding: 8px 13px;
  box-shadow: 0 1px 1px rgba(15, 23, 42, .04);
}

nav a:hover,
nav a:focus {
  background: #eef5ff;
  color: #1d4ed8;
  outline: none;
}

nav a[href*="report_grade_pdf"] {
  background: #2563eb;
  color: #fff;
}

nav a[href*="report_grade_pdf"]:hover,
nav a[href*="report_grade_pdf"]:focus {
  background: #1d4ed8;
  color: #fff;
}

nav a.logout-link {
  background: #fff1f2;
  color: #991b1b;
  border: 1px solid #fecaca;
}

nav a.logout-link:hover,
nav a.logout-link:focus {
  background: #fee2e2;
  color: #7f1d1d;
}

.small {
  min-height: 32px;
  padding: 5px 10px;
  font-size: 14px;
}

button.secondary { background: var(--gray); }
button.danger { background: var(--red); }
button.light {
  background: #eef5ff;
  color: #1d4ed8;
  border: 1px solid #bfdbfe;
}
button:disabled { opacity: .6; cursor: wait; }

.page { padding: 16px 18px 24px; }

.exam-card {
  display: grid;
  gap: 12px;
  padding: 14px;
  margin-bottom: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.exam-row {
  display: grid;
  gap: 10px;
  align-items: end;
}

.subject-row {
  grid-template-columns: 118px minmax(360px, 1fr);
}

.detail-row {
  grid-template-columns: 150px 150px 120px 150px auto;
}

.code-field input {
  text-align: center;
  font-weight: 700;
}

.subject-code-wrap {
  position: relative;
}

.subject-suggestions {
  display: none;
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 30;
  width: min(520px, 82vw);
  max-height: 280px;
  overflow: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 12px 28px rgba(15, 23, 42, .16);
}

.subject-suggestions.show {
  display: block;
}

.subject-suggestion {
  display: block;
  width: 100%;
  min-height: 0;
  padding: 9px 11px;
  border: 0;
  border-radius: 0;
  background: #fff;
  color: var(--text);
  text-align: left;
  font: 500 16px/1.35 "Sarabun", sans-serif;
}

.subject-suggestion:hover,
.subject-suggestion:focus {
  background: #eef5ff;
  color: #1d4ed8;
  outline: none;
}

.subject-suggestion .code {
  display: inline-block;
  min-width: 72px;
  font-weight: 700;
}

.subject-suggestion .name {
  color: var(--muted);
}

.empty-suggestion {
  padding: 10px 12px;
  color: var(--muted);
  font-weight: 600;
}

.subject-field select {
  font-weight: 600;
}

.command-buttons {
  display: flex;
  gap: 8px;
  align-items: end;
  justify-content: flex-end;
}

.check-status {
  align-self: center;
  display: inline-flex;
  align-items: center;
  min-height: 34px;
  border-radius: 999px;
  padding: 5px 12px;
  background: #f1f5f9;
  color: #64748b;
  font-weight: 800;
  white-space: nowrap;
}

.check-status.checked {
  background: #dcfce7;
  color: #166534;
}

.check-status.unchecked {
  background: #fff7ed;
  color: #c2410c;
}

.check-status.loading {
  background: #eef5ff;
  color: #1d4ed8;
}

.toolbar {
  display: grid;
  grid-template-columns: minmax(240px, 1fr) 150px 145px 130px 150px auto auto;
  gap: 10px;
  align-items: end;
  margin-bottom: 12px;
}
.toolbar.exam-toolbar {
  grid-template-columns: 150px minmax(260px, 1fr) 150px 145px 130px 150px auto auto;
}
.toolbar.compact { grid-template-columns: 170px auto auto; justify-content: start; }
.toolbar.report-filter { grid-template-columns: 170px 170px auto auto; }

label { display: grid; gap: 4px; color: #334155; font-weight: 700; font-size: 14px; }
input, select {
  width: 100%;
  min-height: 40px;
  border: 1px solid #b9cbe3;
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  font: 500 16px/1.3 "Sarabun", sans-serif;
  padding: 8px 10px;
}

.actions {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;
}
.exam-actions {
  padding: 10px 12px;
  background: #f8fbff;
  border: 1px solid var(--line);
  border-radius: 8px;
}
.actions input { max-width: 520px; }

.panel, .report-sheet {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: auto;
}

.form-panel {
  padding: 16px;
  margin-bottom: 14px;
}

.table-tools {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 14px 0;
}

.table-search {
  width: min(320px, 100%);
  font-weight: 700;
}

.muted-count {
  color: var(--muted);
  font-weight: 700;
  white-space: nowrap;
}

.subject-list-panel {
  overflow: hidden;
}

.subject-list-head {
  display: flex;
  align-items: stretch;
  justify-content: space-between;
  gap: 18px;
  padding: 18px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #ffffff 0%, #f8fbff 100%);
}

.subject-list-head h2 {
  margin: 0;
  text-align: left;
  font-size: 22px;
}

.subject-list-head p {
  margin: 5px 0 0;
  color: var(--muted);
  font-weight: 600;
}

.subject-stat-strip {
  display: flex;
  gap: 10px;
  align-items: stretch;
}

.subject-stat {
  min-width: 132px;
  padding: 10px 12px;
  border: 1px solid #cfe0f5;
  border-radius: 8px;
  background: #fff;
  text-align: right;
}

.subject-stat strong {
  display: block;
  color: var(--blue);
  font-size: 24px;
  line-height: 1.1;
}

.subject-stat span {
  display: block;
  margin-top: 3px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.subject-filter-card {
  display: grid;
  grid-template-columns: minmax(260px, 420px) auto 1fr;
  gap: 10px;
  align-items: end;
  padding: 14px 18px;
  background: #f8fbff;
  border-bottom: 1px solid var(--line);
}

.subject-search-field {
  display: grid;
  gap: 6px;
  font-weight: 800;
}

.subject-search-field span {
  color: #28415f;
  font-size: 14px;
}

.subject-search-field input {
  height: 42px;
  border: 1px solid #b8cff0;
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  font-weight: 800;
  box-shadow: 0 1px 2px rgba(15, 23, 42, .04);
}

.subject-search-field input:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 4px rgba(37, 99, 235, .12);
  outline: none;
}

.subject-filter-card .small {
  min-height: 42px;
  padding-inline: 16px;
}

.subject-table th:first-child,
.subject-table td:first-child {
  width: 140px;
}

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

.section-title h2 {
  margin: 0;
  text-align: left;
  font-size: 21px;
}

.section-title p {
  margin: 3px 0 0;
  color: var(--muted);
  font-weight: 600;
}

.summary-total {
  min-width: 120px;
  text-align: right;
  display: grid;
  gap: 5px;
  align-content: center;
}

.summary-total strong {
  display: block;
  color: var(--blue);
  font-size: 28px;
  line-height: 1.1;
}

.summary-total span {
  display: block;
  color: var(--muted);
  font-weight: 700;
  line-height: 1.2;
}

.admin-form {
  display: grid;
  grid-template-columns: repeat(3, minmax(180px, 1fr));
  gap: 12px;
  align-items: end;
}

.student-form {
  grid-template-columns: 170px 100px minmax(280px, 1fr) 130px 150px auto;
}

.wide-field {
  min-width: 260px;
}

.form-actions {
  display: flex;
  gap: 8px;
  align-items: end;
}

.form-actions button,
.form-actions .button {
  white-space: nowrap;
}

.secondary-link {
  background: var(--gray);
  color: #fff;
}

.room-checks {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fbff;
}

.room-checks label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 16px;
}

.room-checks input {
  width: 18px;
  min-height: 18px;
}

.room-schedule-list {
  grid-column: 1 / -1;
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fbff;
  overflow-x: auto;
}

.room-schedule-head,
.room-schedule-row {
  display: grid;
  grid-template-columns: minmax(110px, .8fr) 150px 140px 150px minmax(180px, 1fr);
  gap: 8px;
  align-items: center;
  min-width: 760px;
}

.room-schedule-head {
  color: var(--muted);
  font-size: 14px;
  font-weight: 800;
}

.room-schedule-room {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 16px;
}

.room-schedule-room input {
  width: 18px;
  min-height: 18px;
}

.help {
  color: var(--muted);
  margin: 12px 0 0;
}

.summary-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.student-summary-panel {
  background: linear-gradient(180deg, #ffffff 0%, #f8fbff 100%);
}

.student-summary-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(128px, 1fr));
  gap: 8px;
}

.summary-card {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas:
    "room count"
    "unit count";
  align-items: center;
  gap: 1px 8px;
  min-height: 58px;
  padding: 9px 11px;
  border-radius: 8px;
  background: #eef5ff;
  color: #1d4ed8;
  text-decoration: none;
  font-weight: 700;
}

.summary-card span {
  grid-area: room;
  color: #1e3a5f;
}

.summary-card strong {
  grid-area: count;
  font-size: 24px;
  line-height: 1;
  color: #1d4ed8;
}

.summary-card em {
  grid-area: unit;
  color: var(--muted);
  font-style: normal;
  font-size: 13px;
}

.summary-card.total {
  background: #e8f8ee;
  color: #166534;
}

.summary-card.total strong {
  color: #166534;
}

.summary-card.empty-room {
  background: #f1f5f9;
  color: #64748b;
}

.summary-card.empty-room strong,
.summary-card.empty-room span {
  color: #64748b;
}

.summary-card:hover {
  background: #dbeafe;
}

.student-filter {
  margin-top: 4px;
  padding: 12px;
  background: #f8fbff;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.student-table-panel {
  overflow: hidden;
}

.table-title {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  padding: 11px 14px;
  background: #f8fbff;
  border-bottom: 1px solid var(--line);
}

.table-title strong {
  font-size: 18px;
}

.table-title span {
  color: #1d4ed8;
  font-weight: 800;
}

.row-actions {
  white-space: nowrap;
}

.row-actions form {
  display: inline-flex;
  margin: 0 0 0 6px;
}

.danger-zone {
  border-color: var(--err-line);
  background: #fff8f8;
}

.sample-links {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.light-link {
  background: #eef5ff;
  color: #1d4ed8;
  border: 1px solid #bfdbfe;
}

.report-sheet { padding: 14px; }

.landscape-report {
  min-width: 980px;
}

.report-title {
  margin-bottom: 4px;
}

.report-meta {
  margin: 0 0 12px;
  text-align: center;
  color: var(--muted);
  font-weight: 700;
}

.room-report-table th {
  min-width: 92px;
}

.room-report-table th:nth-child(1) { min-width: 90px; }
.room-report-table th:nth-child(2) { min-width: 64px; }
.room-report-table th:nth-child(3) { min-width: 240px; }

.room-report-table th span {
  color: var(--muted);
  font-size: 14px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 16px;
}

th, td {
  border: 1px solid #dbe4f0;
  padding: 7px 8px;
  vertical-align: middle;
}

th {
  background: var(--head);
  text-align: center;
  font-weight: 700;
}

.center { text-align: center; }
.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 58px;
  border-radius: 999px;
  padding: 3px 10px;
  background: #eef5ff;
  color: #1d4ed8;
  border: 1px solid #bfdbfe;
  font-weight: 700;
}
.mark { font-size: 20px; font-weight: 700; }
tr.absent { background: #fff1f2; }
tr.sum { background: #f8fbff; font-weight: 700; }

td input {
  min-height: 34px;
  border-radius: 6px;
}

td input[type="checkbox"] {
  width: 18px;
  min-height: 18px;
  accent-color: var(--blue);
}

.empty {
  padding: 28px;
  text-align: center;
  color: var(--muted);
}

.status {
  display: none;
  margin-bottom: 12px;
  padding: 10px 12px;
  border-radius: 8px;
  font-weight: 700;
}
.status.show { display: block; }
.status.ok { background: var(--ok-bg); border: 1px solid var(--ok-line); color: #166534; }
.status.error { background: var(--err-bg); border: 1px solid var(--err-line); color: #991b1b; }

.toast-stack {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 9999;
  display: grid;
  gap: 8px;
  width: min(360px, calc(100vw - 32px));
  pointer-events: none;
}

.toast {
  display: grid;
  gap: 2px;
  padding: 10px 12px;
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  box-shadow: 0 14px 32px rgba(15, 23, 42, .18);
  border-left: 4px solid var(--blue);
  font-weight: 700;
  line-height: 1.35;
  animation: toast-in .18s ease-out;
}

.toast.ok { border-left-color: #16a34a; }
.toast.error { border-left-color: var(--red); }
.toast.info { border-left-color: var(--blue); }
.toast.warn { border-left-color: #f59e0b; }
.toast.leaving {
  opacity: 0;
  transform: translateY(8px);
  transition: opacity .16s ease, transform .16s ease;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.install {
  width: min(560px, calc(100% - 32px));
  margin: 40px auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 20px;
}
.alert { padding: 10px 12px; border-radius: 8px; margin: 12px 0; }
.alert.ok { background: var(--ok-bg); border: 1px solid var(--ok-line); }
.alert.error { background: var(--err-bg); border: 1px solid var(--err-line); }

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 28px;
  background:
    radial-gradient(circle at top left, rgba(37, 99, 235, .18), transparent 34%),
    linear-gradient(135deg, #eef5ff 0%, #f8fbff 48%, #ffffff 100%);
}

.login-shell {
  width: min(980px, 100%);
  min-height: 560px;
  display: grid;
  grid-template-columns: minmax(0, 1.05fr) minmax(360px, .8fr);
  overflow: hidden;
  background: #fff;
  border: 1px solid rgba(185, 203, 227, .9);
  border-radius: 8px;
  box-shadow: 0 24px 70px rgba(15, 23, 42, .16);
}

.login-hero {
  display: grid;
  align-content: space-between;
  gap: 32px;
  padding: 44px;
  background:
    linear-gradient(135deg, rgba(23, 32, 51, .96), rgba(29, 78, 216, .88)),
    #172033;
  color: #fff;
}

.login-logo-mark {
  display: inline-flex;
  width: 64px;
  height: 64px;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: #fff;
  color: #1d4ed8;
  font-size: 22px;
  font-weight: 800;
  box-shadow: 0 18px 36px rgba(15, 23, 42, .22);
}

.login-kicker {
  display: block;
  margin-bottom: 8px;
  color: #bfdbfe;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: .08em;
  text-transform: uppercase;
}

.login-hero h1 {
  margin: 0 0 14px;
  color: #fff;
  font-size: 30px;
  line-height: 1.25;
}

.login-hero p {
  max-width: 520px;
  margin: 0;
  color: rgba(255, 255, 255, .84);
  font-size: 17px;
  font-weight: 500;
}

.login-feature-list {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.login-feature-list span {
  min-height: 54px;
  display: flex;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid rgba(255, 255, 255, .18);
  border-radius: 8px;
  background: rgba(255, 255, 255, .08);
  color: rgba(255, 255, 255, .92);
  font-size: 14px;
  font-weight: 700;
}

.login-card {
  display: grid;
  align-content: center;
  gap: 18px;
  padding: 44px;
  background: #fff;
}

.login-card-head {
  display: grid;
  gap: 4px;
}

.login-card-head .login-kicker {
  color: #2563eb;
}

.login-card h2 {
  margin: 0;
  color: var(--text);
  text-align: left;
  font-size: 26px;
}

.login-card-head p {
  margin: 0;
  color: var(--muted);
  font-weight: 700;
}

.login-form {
  display: grid;
  gap: 14px;
}

.login-form label {
  gap: 7px;
  font-size: 15px;
}

.login-form input {
  min-height: 48px;
  border-color: #c9d7ea;
  background: #f8fbff;
  font-size: 17px;
}

.login-form input:focus {
  border-color: #2563eb;
  box-shadow: 0 0 0 4px rgba(37, 99, 235, .12);
  outline: none;
}

.login-form button {
  width: 100%;
  min-height: 48px;
  margin-top: 4px;
  font-size: 17px;
  box-shadow: 0 12px 24px rgba(37, 99, 235, .22);
}

.login-help {
  display: grid;
  gap: 3px;
  margin: 2px 0 0;
  padding: 12px 14px;
  border: 1px solid #bfdbfe;
  border-radius: 8px;
  background: #eef5ff;
  color: #1e3a5f;
  font-size: 14px;
  font-weight: 700;
  line-height: 1.45;
}

.login-help span {
  color: #1d4ed8;
  font-size: 15px;
}

.login-help em {
  color: var(--muted);
  font-style: normal;
  font-weight: 600;
}

@media print {
  .topbar, .toolbar, .toast-stack, .logout-link { display: none; }
  body { background: #fff; }
  .page { padding: 0; }
  .report-sheet { border: 0; padding: 0; }
  @page { size: A4 landscape; margin: 8mm; }
}

@media (max-width: 980px) {
  .login-page { padding: 16px; }
  .login-shell {
    min-height: 0;
    grid-template-columns: 1fr;
  }
  .login-hero {
    gap: 18px;
    padding: 26px;
  }
  .login-hero h1 { font-size: 24px; }
  .login-hero p { font-size: 15px; }
  .login-feature-list { grid-template-columns: 1fr; }
  .login-feature-list span { min-height: 44px; }
  .login-card { padding: 26px; }
  .login-card h2 { font-size: 24px; }
  .toolbar { grid-template-columns: 1fr; }
  .subject-row, .detail-row { grid-template-columns: 1fr; }
  .command-buttons { justify-content: stretch; }
  .command-buttons button { flex: 1; }
  .admin-form, .student-form { grid-template-columns: 1fr; }
  .section-title, .table-title { align-items: stretch; flex-direction: column; }
  .subject-list-head {
    flex-direction: column;
  }
  .subject-stat-strip {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .subject-stat {
    min-width: 0;
    text-align: left;
  }
  .subject-filter-card {
    grid-template-columns: 1fr;
  }
  .summary-total { text-align: left; }
  .form-actions { flex-wrap: wrap; }
  .actions { flex-wrap: wrap; }
}
