/* =========================================================
   ตรวจสอบห้องสอบปลายภาค — สไตล์กลางของทุกหน้า
   1) Tokens  2) Base  3) Components  4) Responsive
   ========================================================= */

/* ---------- 1) Tokens ---------- */
:root {
  color-scheme: light;
  --bg:            #fbfbfd;   /* พื้นหลังหน้า */
  --surface:       #ffffff;   /* การ์ด / หน้าต่าง */
  --fill:          #e8e8ed;   /* ช่องกรอก ปุ่มรอง กล่องข้อความ */
  --fill-2:        #f2f2f5;   /* กล่องย่อยในการ์ด ปุ่มไอคอน */
  --fill-hover:    #dcdce1;
  --text:          #1d1d1f;
  --muted:         #6e6e73;
  --primary:       #0071e3;
  --primary-hover: #0077ed;
  --danger:        #c4314b;
  --focus:         rgba(0, 113, 227, .35);
  --bar-bg:        rgba(251, 251, 253, .8);
  --hairline:      rgba(0, 0, 0, .08);
  --shadow:        0 2px 12px rgba(0, 0, 0, .04);

  --r-sm: 12px;  --r-md: 14px;  --r-lg: 18px;  --r-xl: 20px;  --r-pill: 980px;
  --fs-xs: 13px; --fs-sm: 15px; --fs-md: 17px;
  --gutter: 20px;
  --content: 760px;
  --ease: .2s ease;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg:            #000000;
    --surface:       #1c1c1e;
    --fill:          #1d1d1f;
    --fill-2:        #2c2c2e;
    --fill-hover:    #3a3a3c;
    --text:          #f5f5f7;
    --muted:         #a1a1a6;
    --primary:       #0a84ff;
    --primary-hover: #409cff;
    --danger:        #ff6b81;
    --focus:         rgba(10, 132, 255, .45);
    --bar-bg:        rgba(22, 22, 23, .8);
    --hairline:      rgba(255, 255, 255, .1);
    --shadow:        none;
  }
}

/* ---------- 2) Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  min-height: 100svh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Sukhumvit Set",
               "Noto Sans Thai", "Helvetica Neue", Arial, sans-serif;
  font-size: var(--fs-md);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
h1, h2, h3, p { margin: 0; }
img { display: block; user-select: none; -webkit-user-drag: none; }
button, input { font: inherit; color: inherit; }
button { -webkit-tap-highlight-color: transparent; }
:focus-visible { outline: none; box-shadow: 0 0 0 4px var(--focus); }
html.no-scroll { overflow: hidden; }
.nw { white-space: nowrap; }

/* ---------- 3) Components ---------- */

/* ปุ่ม */
.btn {
  display: block;
  padding: 16px 28px;
  border: 0;
  border-radius: var(--r-pill);
  font-size: var(--fs-md);
  font-weight: 500;
  line-height: 1.3;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  transition: background-color var(--ease), transform .1s ease;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .6; cursor: default; }
.btn-primary, .btn-primary:visited { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary, .btn-secondary:visited { background: var(--fill); color: var(--text); }
.btn-secondary:hover { background: var(--fill-hover); }

/* ปุ่มไอคอนวงกลม (แว่นขยายบนการ์ด / ปิดหน้าต่าง) */
.icon-btn {
  flex: none;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--fill-2);
  color: var(--primary);
  cursor: pointer;
  transition: background-color var(--ease), transform .1s ease;
}
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn:hover { background: var(--fill-hover); }
.icon-btn:active { transform: scale(.94); }

/* หน้าหลัก */
.hero {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: max(24px, env(safe-area-inset-top)) max(var(--gutter), env(safe-area-inset-right))
           24px max(var(--gutter), env(safe-area-inset-left));
}
.hero-logo { width: auto; height: clamp(120px, 22vh, 200px); margin-bottom: clamp(24px, 4vh, 40px); }
.hero-title { font-size: clamp(30px, 6.2vw, 56px); font-weight: 700; line-height: 1.2; letter-spacing: -.01em; }
.hero-sub { margin-top: 12px; font-size: clamp(17px, 2.6vw, 24px); color: var(--muted); }
.hero-actions {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 100%;
  max-width: 360px;
  margin-top: clamp(36px, 7vh, 64px);
}

/* แถบบนของหน้าย่อย: โลโก้ + ชื่อหน้า (ซ้าย) / หน้าหลัก (ขวา) */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
           10px max(16px, env(safe-area-inset-left));
  background: var(--bar-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--hairline);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-logo { flex: none; width: auto; height: 52px; }
.brand-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.3; }
.brand-title { font-size: clamp(18px, 3.6vw, 22px); font-weight: 700; letter-spacing: -.01em; }
.brand-sub { font-size: clamp(var(--fs-xs), 2.4vw, var(--fs-sm)); font-weight: 400; color: var(--muted); }
.home {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin: 0 -8px 0 auto;
  padding: 8px 12px 8px 8px;
  border-radius: var(--r-pill);
  color: var(--primary);
  font-size: var(--fs-md);
  font-weight: 500;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
  transition: background-color var(--ease);
}
.home:hover { background: var(--fill); }
.home svg { flex: none; width: 22px; height: 22px; }

/* เนื้อหาหน้าย่อย */
.page {
  flex: 1;
  width: 100%;
  max-width: var(--content);
  margin: 0 auto;
  padding: 32px max(var(--gutter), env(safe-area-inset-right)) 24px max(var(--gutter), env(safe-area-inset-left));
}

/* ช่องค้นหา */
.search-label { display: block; margin: 0 0 10px 2px; font-size: var(--fs-sm); font-weight: 600; }
.search-row { display: flex; gap: 10px; }
.search-field { position: relative; flex: 1; min-width: 0; }
.search-icon {
  position: absolute;
  top: 50%;
  left: 14px;
  width: 20px;
  height: 20px;
  transform: translateY(-50%);
  color: var(--muted);
  pointer-events: none;
}
.search-field input {
  width: 100%;
  padding: 13px 16px 13px 44px;
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  background: var(--fill);
  font-size: var(--fs-md);          /* ≥16px กัน iPhone ซูมอัตโนมัติ */
  line-height: 1.4;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
  transition: background-color var(--ease), border-color var(--ease), box-shadow var(--ease);
}
.search-field input::placeholder { color: var(--muted); opacity: 1; }
.search-field input:focus { background: var(--bg); border-color: var(--primary); box-shadow: 0 0 0 4px var(--focus); }
.search-btn { flex: none; padding: 0 24px; border-radius: var(--r-sm); }
.search-hint { margin: 8px 0 0 2px; font-size: 14px; color: var(--muted); }

/* ผลลัพธ์ */
.results { margin-top: 28px; }
.msg {
  padding: 20px;
  border-radius: var(--r-md);
  background: var(--fill);
  color: var(--muted);
  font-size: 16px;
  text-align: center;
}
.msg.is-error { color: var(--danger); }
.note { margin: 0 2px 12px; font-size: var(--fs-sm); color: var(--muted); }

.card {
  position: relative;
  padding: 20px 76px 20px 20px;     /* เว้นขวาให้ปุ่มไอคอน */
  border: 1px solid var(--hairline);
  border-radius: var(--r-lg);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.card + .card { margin-top: 14px; }
.card-title { font-size: clamp(20px, 4.4vw, 24px); font-weight: 700; line-height: 1.35; letter-spacing: -.01em; }
.card-sub { margin-top: 2px; font-size: 16px; color: var(--muted); }
.card .icon-btn { position: absolute; top: 14px; right: 14px; }

/* หน้าต่างรายละเอียด (มือถือ: แผ่นเลื่อนจากล่าง / จอกว้าง: กลางจอ)
   --vvh = ความสูงที่มองเห็นจริง (JS ตั้งจาก visualViewport) — กันปัญหาเบราว์เซอร์ในแอป
   LINE / Messenger ที่รายงาน 100vh สูงกว่าจอจริง ทำให้หน้าต่างล้นลงไปใต้จอ */
:root { --vvh: 100vh; }
@supports (height: 100dvh) { :root { --vvh: 100dvh; } }
.sheet {
  position: fixed;
  inset: calc(var(--vvh) * .08) 0 auto 0;   /* ยึดจากด้านบน จึงไม่ล้นแม้ viewport ผิดเพี้ยน */
  display: none;
  flex-direction: column;
  width: 100%;
  max-width: 100%;
  height: calc(var(--vvh) * .92);
  max-height: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  border: 0;
  border-radius: var(--r-xl) var(--r-xl) 0 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 -8px 40px rgba(0, 0, 0, .18);
}
.sheet[open] { display: flex; animation: sheet-in .28s cubic-bezier(.2, .8, .2, 1); }
/* เบราว์เซอร์ที่ไม่รองรับ <dialog>: ทำพื้นหลังทึบเอง */
.sheet.is-fallback[open] { z-index: 100; }
.sheet-backdrop { position: fixed; inset: 0; z-index: 99; background: rgba(0, 0, 0, .38); }
.sheet::backdrop { background: rgba(0, 0, 0, .38); -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px); }
@keyframes sheet-in { from { transform: translateY(24px); opacity: 0; } }
.sheet-head {
  flex: none;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 20px var(--gutter) 14px;
  border-bottom: 1px solid var(--hairline);
}
.sheet-title { flex: 1; min-width: 0; }
.sheet-name { font-size: 20px; font-weight: 700; line-height: 1.35; }
.sheet-sub { margin-top: 2px; font-size: var(--fs-sm); color: var(--muted); }
.sheet-head .icon-btn { width: 36px; height: 36px; color: var(--muted); }
.sheet-head .icon-btn svg { width: 18px; height: 18px; }
.sheet-body {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 8px var(--gutter) max(24px, env(safe-area-inset-bottom));
}
.sheet-body .msg { margin-top: 12px; }

.day + .day { margin-top: 8px; }
.day-title {
  padding: 14px 0 10px;
  color: var(--primary);
  font-size: 16px;
  font-weight: 700;
}
.slots { margin: 0; padding: 0; list-style: none; }
.slot { padding: 14px 16px; border-radius: var(--r-md); background: var(--fill-2); }
.slot + .slot { margin-top: 10px; }
.slot-time { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }
.slot-room { margin-top: 2px; font-size: var(--fs-sm); }
.slot-with { margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--hairline); }
.slot-label { margin-bottom: 4px; font-size: var(--fs-xs); color: var(--muted); }
.person { display: flex; flex-wrap: wrap; column-gap: 8px; font-size: var(--fs-sm); }
.person + .person { margin-top: 2px; }
.person b { font-weight: 600; }
.person span { color: var(--muted); }

/* ส่วนท้าย */
.site-footer {
  flex: none;
  padding: 20px max(var(--gutter), env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom))
           max(var(--gutter), env(safe-area-inset-left));
  color: var(--muted);
  font-size: var(--fs-xs);
  line-height: 1.6;
  text-align: center;
}
.site-footer .sep { margin: 0 8px; opacity: .6; }

/* ---------- 4) Responsive ---------- */
@media (min-width: 640px) {
  .sheet {
    inset: 0;
    width: calc(100% - 48px);
    max-width: 600px;
    height: fit-content;
    max-height: calc(var(--vvh) * .86);
    margin: auto;
    border-radius: var(--r-xl);
    box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
  }
}
@media (max-width: 520px) {               /* footer: แยกหน่วยงาน/มหาวิทยาลัยคนละบรรทัด */
  .site-footer { font-size: 12px; }
  .site-footer .sep { display: none; }
  .site-footer .nw { display: block; }
}
@media (max-width: 380px) {
  .home-label { display: none; }
  .search-btn { padding: 0 16px; }
}
@media (orientation: landscape) and (max-height: 500px) {   /* มือถือแนวนอน */
  .hero-logo { height: clamp(72px, 24vh, 110px); margin-bottom: 14px; }
  .hero-title { font-size: clamp(24px, 4.2vw, 36px); }
  .hero-sub { margin-top: 6px; font-size: 16px; }
  .hero-actions { margin-top: 24px; gap: 10px; max-width: 340px; }
  .hero-actions .btn { padding: 12px 24px; font-size: 16px; }
  .topbar { padding-top: calc(6px + env(safe-area-inset-top)); padding-bottom: 6px; }
  .brand-logo { height: 40px; }
  .page { padding-top: 20px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}
