:root {
  --bg: #f4f6fa;
  --card: #fff;
  --line: #dfe4ec;
  --ink: #1c2330;
  --muted: #5d6879;
  --brand: #1f5fa8;
  --brand-ink: #123f74;
  --brand-soft: #e8f0fb;
  --ok: #197a4b;
  --ok-soft: #e4f4ec;
  --warn: #8a5208;
  --warn-soft: #fdf1e0;
  --bad: #a72222;
  --bad-soft: #fdeaea;
  --focus: #0b63d6;
  --r-card: 14px;
  --r-ctl: 8px;
  --gap-in: 8px;
  --gap-out: 20px;
}

* { box-sizing: border-box; }

/* ------------------------------------------------------------ ล็อกอิน -- */

.gate {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 20px;
  background: linear-gradient(140deg, var(--brand-ink), var(--brand));
}
.gate[hidden] { display: none; }

.gate-box {
  width: 100%;
  max-width: 380px;
  background: var(--card);
  border-radius: var(--r-card);
  padding: 26px 24px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .3);
}
.gate-box h1 { margin: 0 0 4px; font-size: 20px; color: var(--brand-ink); }
.gate-sub { margin: 0 0 16px; font-size: 13px; color: var(--muted); }
.gate-box .field { display: block; margin-bottom: 16px; }
.gate-box input { width: 100%; font-size: 16px; }
.gate-box .btn { width: 100%; }

.err-box {
  margin: 0 0 14px;
  padding: 10px 13px;
  border-radius: var(--r-ctl);
  background: var(--bad-soft);
  border: 1px solid #f0bcbc;
  color: var(--bad);
  font-size: 13px;
  font-weight: 500;
}
.err-box:empty { display: none; }

/* ---- คลาสแทน style= เดิม ----
   CSP เป็น style-src 'self' จึงใช้ attribute style= ไม่ได้เลย
   ค่าพวกนี้ย้ายมาจากแท็กใน index.html และ app.js */
.mt0 { margin-top: 0; }
.mt12 { margin-top: 12px; }
.mt16 { margin-top: 16px; }

.w-pick { width: 44px; }
.w-scode { width: 118px; }
.w-sname { width: 220px; }
.w-smajor { width: 150px; }
.w-seq { width: 70px; }
.w-course { width: 230px; }
.w-count { width: 76px; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "IBM Plex Sans Thai", "Leelawadee UI", "Segoe UI", Tahoma, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ------------------------------------------------------------- a11y base -- */

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: 3px;
}
header :focus-visible { outline-color: #fff; }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.skip {
  position: absolute;
  left: 12px; top: -60px;
  z-index: 20;
  background: #fff;
  color: var(--brand-ink);
  padding: 9px 16px;
  border-radius: var(--r-ctl);
  box-shadow: 0 2px 10px rgba(0, 0, 0, .25);
  text-decoration: none;
  transition: top .12s ease-out;
}
.skip:focus { top: 12px; }

/* ---------------------------------------------------------------- header -- */

header {
  background: linear-gradient(100deg, var(--brand-ink), var(--brand));
  color: #fff;
  padding: 16px 24px;
}
header h1 { margin: 0; font-size: 20px; font-weight: 600; text-wrap: balance; }
header .sub { color: #d7e3f4; font-size: 13px; margin-top: 2px; font-variant-numeric: tabular-nums; }
header .bar { display: flex; align-items: center; gap: var(--gap-out); flex-wrap: wrap; justify-content: space-between; }
header .bar > div { min-width: 0; }
header .autobox { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
header .switch { display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; padding: 4px 0; }
header .switch input { width: 18px; height: 18px; cursor: pointer; accent-color: #fff; }
header .btn.ghost { background: rgba(255, 255, 255, .14); color: #fff; border-color: rgba(255, 255, 255, .6); }
header .btn.ghost:hover { background: rgba(255, 255, 255, .24); }

code {
  background: #eef1f6;
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 13px;
  white-space: nowrap;
}

main { max-width: 1220px; margin: 0 auto; padding: 18px 20px 72px; }

/* ------------------------------------------------------------------ tabs -- */

.tabs { display: flex; gap: 6px; margin: 18px 0 14px; flex-wrap: wrap; }
.tabs button {
  border: 1px solid var(--line);
  background: #fff;
  padding: 8px 16px;
  border-radius: 999px;
  cursor: pointer;
  font: inherit;
  color: var(--muted);
  transition: background-color .12s ease-out, color .12s ease-out, border-color .12s ease-out;
}
.tabs button:hover { border-color: var(--brand); color: var(--brand); }
.tabs button[aria-selected="true"] { background: var(--brand); border-color: var(--brand); color: #fff; }
[role="tabpanel"]:focus { outline: none; }

/* ----------------------------------------------------------------- cards -- */

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--r-card);
  padding: 16px;
  margin-bottom: 14px;
}
.card > h2, .card > h3 { margin-top: 0; }

.intro {
  background: linear-gradient(100deg, var(--brand-soft), #f2f7fd);
  border: 1px solid #cfe0f5;
  border-radius: var(--r-card);
  padding: 14px 16px;
  margin-bottom: 14px;
}
.intro h2 { margin: 0 0 4px; font-size: 17px; font-weight: 600; text-wrap: balance; }
.intro p { margin: 0; color: var(--brand-ink); text-wrap: pretty; }
.intro p + p { margin-top: 6px; }

/* --------------------------------------------------------------- stepper -- */

.stepper { display: flex; flex-wrap: wrap; gap: var(--gap-in); list-style: none; margin: 0 0 var(--gap-out); padding: 0; }
.stepper li { display: flex; align-items: center; gap: var(--gap-in); }
.stepper li::after { content: ""; width: 14px; height: 1px; background: var(--line); }
.stepper li:last-child::after { display: none; }
.stepper button {
  display: flex;
  align-items: center;
  gap: 8px;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  background: #fff;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 14px 6px 7px;
  transition: background-color .12s ease-out, border-color .12s ease-out, color .12s ease-out;
}
.stepper button:hover { border-color: var(--brand); color: var(--brand); }
.stepper .no {
  display: grid;
  place-items: center;
  width: 24px; height: 24px;
  border-radius: 50%;
  background: var(--line);
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.stepper .now button { border-color: var(--brand); color: var(--brand-ink); background: var(--brand-soft); font-weight: 600; }
.stepper .now .no { background: var(--brand); color: #fff; }
.stepper .done button { border-color: #b6dfc9; color: var(--ok); background: var(--ok-soft); }
.stepper .done .no { background: var(--ok); color: #fff; }

/* ------------------------------------------------------------ step cards -- */

.step { scroll-margin-top: 14px; }
.step-head {
  display: flex;
  align-items: baseline;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: space-between;
  margin-bottom: 4px;
}
.step-head h3 { margin: 0; font-size: 17px; font-weight: 600; display: flex; align-items: center; gap: 9px; }
.step-no {
  display: grid;
  place-items: center;
  width: 26px; height: 26px;
  flex: none;
  border-radius: 50%;
  background: var(--brand);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.step-hint { margin: 0 0 12px; color: var(--muted); font-size: 13px; text-wrap: pretty; }
.step-hint + .step-hint { margin-top: -6px; }

.status {
  font-size: 13px;
  border-radius: 999px;
  padding: 2px 11px;
  border: 1px solid var(--line);
  color: var(--muted);
  background: #f7f9fc;
  white-space: nowrap;
}
.status.done { border-color: #b6dfc9; background: var(--ok-soft); color: var(--ok); }
.status.warn { border-color: #f0d5a8; background: var(--warn-soft); color: var(--warn); }
.status.bad { border-color: #f2c4c4; background: var(--bad-soft); color: var(--bad); }

/* -------------------------------------------------------------- controls -- */

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.toolbar .spacer { flex: 1 1 12px; }

input[type="text"], input[type="search"], select, textarea {
  font: inherit;
  padding: 7px 11px;
  border: 1px solid var(--line);
  border-radius: var(--r-ctl);
  background: #fff;
  color: inherit;
  min-height: 36px;
}
input[type="search"] { min-width: 240px; flex: 1 1 240px; max-width: 420px; }
textarea { width: 100%; resize: vertical; }
input::placeholder { color: #97a1b0; }

button.btn {
  font: inherit;
  border: 1px solid var(--brand);
  background: var(--brand);
  color: #fff;
  padding: 7px 14px;
  min-height: 36px;
  border-radius: var(--r-ctl);
  cursor: pointer;
  transition: background-color .12s ease-out, border-color .12s ease-out, color .12s ease-out;
}
button.btn:hover { background: var(--brand-ink); border-color: var(--brand-ink); }
button.btn.ghost { background: #fff; color: var(--brand); }
button.btn.ghost:hover { background: var(--brand-soft); }
button.btn.grey { background: #fff; color: var(--muted); border-color: var(--line); }
button.btn.grey:hover { background: #f7f9fc; color: var(--ink); border-color: #c3cddc; }
button.btn.big { font-size: 16px; padding: 11px 22px; min-height: 46px; }
button.btn:disabled { opacity: .5; cursor: not-allowed; }
button.btn:not(:disabled):active { scale: .96; }

.linkish {
  font: inherit;
  background: none;
  border: 0;
  padding: 0;
  color: var(--brand);
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
  text-decoration-thickness: from-font;
}

/* ---------------------------------------------------------------- tables -- */

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { border-bottom: 1px solid var(--line); padding: 7px 9px; text-align: left; vertical-align: top; }
th { background: #f7f9fc; font-weight: 600; position: sticky; top: 0; z-index: 1; }
tbody tr:hover { background: #fafcff; }
td.c, th.c { text-align: center; }
.mono { font-variant-numeric: tabular-nums; font-family: "Cascadia Mono", Consolas, monospace; }
.num { font-variant-numeric: tabular-nums; }

/* ช่องติ๊กกินพื้นที่ทั้งเซลล์ เพื่อให้กดโดนง่าย */
.pickcell { padding: 0; }
.pickcell label { display: block; padding: 8px 9px; cursor: pointer; }
.pickcell input { width: 18px; height: 18px; cursor: pointer; accent-color: var(--brand); }

.scroll { max-height: 62vh; overflow: auto; border: 1px solid var(--line); border-radius: 10px; }
/* ในโหมดทีละขั้น ตารางต้องไม่สูงจนขั้นถัดไปหลุดหน้าจอ */
#step1 .scroll { max-height: min(46vh, 430px); }

/* ตารางสรุปเอกสารกว้างตามรายชื่อ ต้องเลื่อนในกล่องตัวเอง ไม่ใช่ดันทั้งหน้าให้กว้างเกินจอ */
#selList { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; }
#selList table { min-width: 620px; }
#selList:empty { display: none; }

.empty { padding: 22px 16px; text-align: center; }
.empty b { display: block; font-weight: 600; margin-bottom: 2px; }
.empty p { margin: 0 0 10px; color: var(--muted); font-size: 13px; }

/* --------------------------------------------------------- ขั้นที่ 2: วิชา -- */

.bulk {
  display: flex;
  gap: var(--gap-in);
  flex-wrap: wrap;
  align-items: flex-end;
  background: #f7f9fc;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  margin-bottom: var(--gap-out);
}
.bulk[hidden] { display: none; }
.bulk label.field { flex: 1 1 260px; }

.crows { display: grid; gap: var(--gap-in); }
.crow {
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(200px, .9fr) minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
}
.crow.miss { border-color: #f0d5a8; background: #fffdf8; }
.crow-name { font-size: 14px; cursor: pointer; }
.crow input { width: 100%; }
.crow-out { font-size: 13px; color: var(--muted); min-width: 0; }
.crow-out .chip { margin: 0 4px 0 0; }

/* ---------------------------------------------------------------- chips -- */

.chip {
  display: inline-block;
  background: var(--brand-soft);
  color: var(--brand-ink);
  border-radius: 6px;
  padding: 1px 8px;
  margin: 2px 3px 2px 0;
  font-size: 12.5px;
}
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.bad { background: var(--bad-soft); color: var(--bad); }
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.notes { display: grid; gap: 6px; margin: 0 0 12px; }
.note { display: flex; gap: 8px; align-items: baseline; font-size: 13.5px; border-radius: 8px; padding: 8px 11px; }
.note.warn { background: var(--warn-soft); color: var(--warn); }
.note.bad { background: var(--bad-soft); color: var(--bad); }
.note.ok { background: var(--ok-soft); color: var(--ok); }
.note b { flex: none; }

/* ----------------------------------------------------------------- stats -- */

.stats { display: flex; gap: 10px; flex-wrap: wrap; }
.stat { background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 10px 16px; flex: 1 1 132px; max-width: 200px; }
.stat b { display: block; font-size: 22px; color: var(--brand); font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- fields -- */

.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 12px; }
label.field { display: block; font-size: 13px; color: var(--muted); }
label.field input, label.field select { width: 100%; margin-top: 3px; }
label.check { display: flex; gap: 8px; align-items: center; cursor: pointer; padding: 4px 0; }
label.check input { width: 18px; height: 18px; cursor: pointer; accent-color: var(--brand); }

details.card summary { cursor: pointer; font-size: 14px; }
details.card[open] summary { margin-bottom: 10px; }

#toast {
  position: fixed; right: 16px; bottom: 16px; max-width: min(420px, calc(100vw - 32px));
  background: #1c2330; color: #fff;
  padding: 10px 16px; border-radius: var(--r-ctl);
  opacity: 0; transition: opacity .2s ease-out; pointer-events: none;
}
#toast.show { opacity: .96; }

/* ------------------------------------------------------------ responsive -- */

@media (max-width: 720px) {
  main { padding: 14px 14px 60px; }
  .crow { grid-template-columns: 1fr; }
  .crow-name { font-weight: 600; }

  /* ข้อความไทยไม่มีช่องว่างระหว่างคำ ถ้าไม่ยอมให้ตัดคำ บรรทัดเดียวจะดันหน้าเว็บให้กว้างเกินจอ */
  header { padding: 14px 16px; }
  header .bar { display: block; }
  header h1 { font-size: 18px; }
  header .autobox { margin-top: 10px; }
  header h1, .intro h2, .intro p, .step-head h3, .step-hint, .empty b { overflow-wrap: anywhere; }
  input[type="search"] { min-width: 0; }
  .toolbar > * { max-width: 100%; }
}

/* iOS Safari ซูมทั้งหน้าถ้าตัวอักษรในช่องกรอกเล็กกว่า 16px */
@media (max-width: 640px) {
  input[type="text"], input[type="search"], select, textarea { font-size: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: .01ms !important; animation-duration: .01ms !important; }
  button.btn:not(:disabled):active { scale: 1; }
}
