/* ============================================================
   ระบบรับสมัครสอบเข้าโรงเรียนออนไลน์
   Utilities + Device Layer — ใช้ร่วมกันทั้งหน้าบ้านและหน้าจ้าหน้าที่

   ⚠️ ไฟล์นี้ต้องถูกโหลด "เป็นไฟล์สุดท้าย" ใน <head> เสมอ
      เพราะ utility ต้องชนะสไตล์ของคอมโพเนนต์ (เช่น .card p-0)
      ลำดับ: main.css/admin.css → sweetalert2.min.css → utilities.css

   เหตุผลที่ต้องมีไฟล์นี้: เทมเพลตทั้งสองฝั่งใช้คลาสแบบ Bootstrap
   (.mt-3, .d-flex, .text-center, .p-0, .rounded, ...) แต่ไม่มีไฟล์ไหน
   นิยามไว้เลย ทำให้เลย์เอาต์เพี้ยนและพังบนจอมือถือ จึงรวมไว้ที่เดียว

   Breakpoint มาตรฐานของโปรเจกต์ (ใหญ่ → เล็ก)
     1600px  จอ ultrawide
     1280px  โน้ตบุ๊กเล็ก / แท็บเล็ตนอน
     1024px  iPad นอนตั้ง — เมนูข้างกลายเป็นลิ้นชัก
      900px  แท็บเล็ตตั้ง
      768px  มือถือจอนอน / มือถือจอใหญ่
      600px  มือถือทั่วไป
      480px  มือถือเล็ก
      380px  มือถือจิ๋ว (iPhone SE รุ่นแรก)
   ============================================================ */

/* ============================================================
   1. BASE — กันล้นแนวนอนและข้อความ/ตัวเลขยาวทะลุการ์ด
   ============================================================ */

img,
video,
canvas,
iframe {
  max-width: 100%;
}

/* ไฟล์ที่ผู้ใช้อัปโหลดมีชื่อยาวมาก (ไม่มีช่องว่าง) ต้องตัดบรรทัดได้
   ไม่งั้นจะดันการ์ดให้ล้นจอมือถือ
   ⚠️ ห้ามใส่กับ strong/em ทั่วไป เพราะจะทำให้เลขที่ใบสมัครอย่าง "0001"
   ถูกตัดกลางคำเป็น "000" + "1" (พบจริงบนจอมือถือ) */
.file-name,
.application-result-no,
.cred-value,
.account-number,
.account-val,
.doc-file {
  overflow-wrap: anywhere;
  word-break: break-word;
}

/* ชื่อไฟล์ในการ์ดเอกสาร/สลิป */
.file-name {
  margin: 0.5rem 0;
  font-size: 0.9rem;
}

/* ============================================================
   2. DISPLAY / FLEX / GAP
   ============================================================ */

.d-none { display: none !important; }
.d-block { display: block; }
.d-inline { display: inline; }
.d-inline-block { display: inline-block; }
.d-flex { display: flex; }
.d-inline-flex { display: inline-flex; }
.d-grid { display: grid; }

.flex-wrap { flex-wrap: wrap; }
.flex-nowrap { flex-wrap: nowrap; }
.flex-column { flex-direction: column; }
.flex-grow-1 { flex-grow: 1; }
.flex-shrink-0 { flex-shrink: 0; }

.justify-content-start { justify-content: flex-start; }
.justify-content-center { justify-content: center; }
.justify-content-end { justify-content: flex-end; }
.justify-content-between { justify-content: space-between; }
.justify-content-around { justify-content: space-around; }

.align-items-start { align-items: flex-start; }
.align-items-center { align-items: center; }
.align-items-end { align-items: flex-end; }
.align-items-baseline { align-items: baseline; }

.gap-1 { gap: 0.25rem; }
.gap-2 { gap: 0.5rem; }
.gap-3 { gap: 0.75rem; }
.gap-4 { gap: 1.25rem; }

/* ซ่อน/แสดงด้วย JavaScript (ใช้ร่วมกันหลายหน้า) */
.hidden {
  display: none !important;
}

/* แถวที่เรียงเป็น flex ต้องตัดบรรทัดได้ (สำคัญมากบนมือถือ)
   ไม่งั้นช่องกรอก + ปุ่มที่อยู่แถวเดียวกันจะดันกันจนล้นจอ */
.d-flex {
  min-width: 0;
}

@media (max-width: 600px) {
  .d-flex {
    flex-wrap: wrap;
  }

  .flex-nowrap {
    flex-wrap: nowrap;
  }
}

/* ============================================================
   3. SPACING — สเกล 1=0.25rem 2=0.5rem 3=0.75rem 4=1.25rem 5=2rem
   ============================================================ */

.m-0 { margin: 0; }
.mt-0 { margin-top: 0; }
.mb-0 { margin-bottom: 0; }
.ml-0 { margin-left: 0; }
.mr-0 { margin-right: 0; }

.mt-1 { margin-top: 0.25rem; }
.mt-2 { margin-top: 0.5rem; }
.mt-3 { margin-top: 0.75rem; }
.mt-4 { margin-top: 1.25rem; }
.mt-5 { margin-top: 2rem; }

.mb-1 { margin-bottom: 0.25rem; }
.mb-2 { margin-bottom: 0.5rem; }
.mb-3 { margin-bottom: 0.75rem; }
.mb-4 { margin-bottom: 1.25rem; }
.mb-5 { margin-bottom: 2rem; }

.ml-1 { margin-left: 0.25rem; }
.ml-2 { margin-left: 0.5rem; }
.ml-3 { margin-left: 0.75rem; }
.mr-1 { margin-right: 0.25rem; }
.mr-2 { margin-right: 0.5rem; }
.mr-3 { margin-right: 0.75rem; }

.mx-2 { margin-left: 0.5rem; margin-right: 0.5rem; }
.my-2 { margin-top: 0.5rem; margin-bottom: 0.5rem; }
.my-3 { margin-top: 0.75rem; margin-bottom: 0.75rem; }
.my-4 { margin-top: 1.25rem; margin-bottom: 1.25rem; }
.my-5 { margin-top: 2.5rem; margin-bottom: 2.5rem; }
.mx-auto { margin-left: auto; margin-right: auto; }

.p-0 { padding: 0; }
.p-1 { padding: 0.25rem; }
.p-2 { padding: 0.5rem; }
.p-3 { padding: 0.85rem; }
.p-4 { padding: 1.25rem; }
.p-5 { padding: 2.25rem; }

.pt-2 { padding-top: 0.5rem; }
.pt-3 { padding-top: 0.75rem; }
.pb-2 { padding-bottom: 0.5rem; }
.pb-3 { padding-bottom: 0.75rem; }

.py-2 { padding-top: 0.5rem; padding-bottom: 0.5rem; }
.py-3 { padding-top: 0.85rem; padding-bottom: 0.85rem; }
.py-4 { padding-top: 1.5rem; padding-bottom: 1.5rem; }
.py-5 { padding-top: 2.5rem; padding-bottom: 2.5rem; }
.px-2 { padding-left: 0.5rem; padding-right: 0.5rem; }
.px-3 { padding-left: 0.85rem; padding-right: 0.85rem; }

/* ============================================================
   4. TEXT / COLOR / SIZE
   ============================================================ */

.text-left { text-align: left; }
.text-center { text-align: center; }
.text-right { text-align: right; }
.text-nowrap { white-space: nowrap; }
.text-break { overflow-wrap: anywhere; word-break: break-word; }

.small { font-size: 0.85rem; }
.lead {
  font-size: clamp(1rem, 0.95rem + 0.3vw, 1.15rem);
  color: var(--text-muted, #64748b);
  margin-bottom: 0.5rem;
}
.font-weight-bold { font-weight: 700; }

.text-muted { color: var(--text-muted, #64748b); }
.text-primary { color: var(--primary-color, var(--admin-primary, #1a56db)); }
.text-success { color: var(--success-color, #0e9f6e); }
.text-danger { color: var(--danger-color, #e02424); }
.text-warning { color: var(--warning-color, #d97706); }
.text-info { color: #0891b2; }
.text-white { color: #ffffff; }

.bg-light { background-color: #f8fafc; }
.bg-white { background-color: #ffffff; }
.rounded { border-radius: 10px; }
.rounded-sm { border-radius: 6px; }
.border { border: 1px solid var(--border-color, #e2e8f0); }
.border-top { border-top: 1px solid var(--border-color, #e2e8f0); }
.border-bottom { border-bottom: 1px solid var(--border-color, #e2e8f0); }
.w-100 { width: 100%; }
.h-100 { height: 100%; }

/* ซ่อนเฉพาะสายตาผู้ใช้ แต่ยังอ่านออกด้วยโปรแกรมอ่านหน้าจอ */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ============================================================
   5. MICRO-COMPONENTS ที่ใช้ทั้งสองฝั่ง
   ============================================================ */

/* ข้อความช่วยใต้ช่องกรอก */
.form-text {
  display: block;
  margin-top: 0.3rem;
  font-size: 0.82rem;
  line-height: 1.5;
}

/* กล่องแจ้งเตือนแบบมีไอคอน + เนื้อหา (flash_alerts, login, step1/4) */
.alert {
  /* ต้อง wrap ได้ และเนื้อหาที่ไม่ถูกห่อด้วย .alert-content (บางเทมเพลตเขียนข้อความ
     และ <ul> ตรง ๆ) จะกลายเป็น flex item ที่ถูกบีบจนเหลือคำละบรรทัดบนมือถือ */
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 0.4rem 0.6rem;
}

.alert > * {
  min-width: 0;
}

/* ย่อหน้าหรือรายการในการแจ้งเตือน ให้ขึ้นบรรทัดใหม่เต็มความกว้างเสมอ */
.alert > p,
.alert > ul,
.alert > ol,
.alert > hr,
.alert > h2,
.alert > h3,
.alert > h4,
.alert > .btn {
  flex: 1 1 100%;
}

.alert > p:first-child,
.alert > ul:first-child,
.alert > ol:first-child {
  margin-top: 0;
}

.alert-icon {
  flex: 0 0 auto;
  font-size: 1.05rem;
  line-height: 1.5;
}

.alert-content {
  flex: 1 1 auto;
  min-width: 0;
}

.alert-content ul,
.alert-content ol {
  margin: 0.4rem 0 0;
  padding-left: 1.25rem;
}

.alert-content li + li {
  margin-top: 0.25rem;
}

.alert .alert-heading {
  margin-bottom: 0.5rem;
  font-size: 1.05rem;
  line-height: 1.5;
}

.alert hr {
  margin: 0.85rem 0;
  border: 0;
  border-top: 1px solid currentColor;
  opacity: 0.25;
}

.alert .btn {
  margin-top: 0.5rem;
}

/* ป้ายขนาดใหญ่ (หน้าตรวจสอบสถานะ/รายละเอียดใบสมัครของเจ้าหน้าที่) */
.badge-lg {
  padding: 0.45rem 0.9rem;
  font-size: 0.9rem;
  line-height: 1.5;
}

.badge-info { background-color: #cffafe; color: #155e75; }
.badge-secondary { background-color: #e2e8f0; color: #334155; }

/* กล่อง "ยังไม่มีข้อมูล" */
.empty-state-box {
  padding: 2rem 1rem;
  border: 1px dashed var(--border-color, #e2e8f0);
  border-radius: 12px;
  background-color: #f8fafc;
}

.empty-state-box h3 {
  margin: 0.5rem 0 0.35rem;
  font-size: 1.1rem;
}

/* เส้นทางนำทาง (breadcrumbs) ใช้ทั้งหน้าบ้านและรายละเอียดใบสมัครของเจ้าหน้าที่ */
.header-breadcrumbs {
  font-size: 0.85rem;
  color: var(--text-muted, #64748b);
  margin-bottom: 0.5rem;
  overflow-wrap: anywhere;
}

.header-breadcrumbs a {
  color: var(--text-muted, #64748b);
  text-decoration: none;
}

.header-breadcrumbs a:hover {
  color: var(--primary-color, var(--admin-primary, #2563eb));
  text-decoration: underline;
}

/* ปุ่มที่กำลังส่งข้อมูล — กันกดซ้ำโดยไม่ใช้ disabled
   (เพราะ disabled จะทำให้ค่า name/value ของปุ่มหายไป) */
.btn.is-loading {
  position: relative;
  pointer-events: none;
  opacity: 0.75;
}

.btn.is-loading::after {
  content: '';
  width: 0.9em;
  height: 0.9em;
  margin-left: 0.5rem;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  display: inline-block;
  vertical-align: -0.1em;
  animation: btn-spin 0.6s linear infinite;
}

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

/* ============================================================
   6. REVIEW BLOCK — สรุปข้อมูลใบสมัคร
   ใช้ทั้งหน้าบ้าน (ขั้นตอนที่ 4 / ตรวจสอบสถานะ) และหน้าหลังบ้าน
   (รายละเอียดใบสมัครของเจ้าหน้าที่) จึงต้องอยู่ในไฟล์ที่โหลดทั้งสองฝั่ง
   ============================================================ */

.review-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
}

.review-item {
  min-width: 0;
  padding: 0.35rem 0;
  border-bottom: 1px dashed var(--border-color, #e5e7eb);
}

.review-item:last-child {
  border-bottom: none;
}

.review-item .label {
  display: block;
  font-size: 0.82rem;
  color: var(--text-muted, #6b7280);
}

.review-item .value {
  font-size: 1rem;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.review-block {
  border: 1px solid var(--border-color, #e5e7eb);
  border-radius: 12px;
  overflow: hidden;
}

.review-block-header {
  background-color: #f8fafc;
  padding: 0.7rem 1rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  border-bottom: 1px solid var(--border-color, #e5e7eb);
}

.review-block-header h3 {
  font-size: 1rem;
}

.review-content {
  background-color: #ffffff;
}

.document-summary-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.doc-summary-item {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem 0.5rem;
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--border-color, #e5e7eb);
  border-radius: 8px;
  background-color: #f8fafc;
}

.doc-title {
  font-weight: 600;
  flex: 0 0 auto;
}

.doc-file {
  min-width: 0;
}

@media (min-width: 600px) {
  .review-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.5rem 1.25rem;
    padding: 1rem 1.5rem;
  }

  .review-item {
    border-bottom: none;
  }
}

@media (min-width: 900px) {
  .review-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

/* ============================================================
   7. TABLES — เลื่อนแนวนอนได้ พร้อมเงาบอกขอบ (ใช้ร่วมกันทั้งสองฝั่ง)
   ============================================================ */

.table-responsive {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 900px) {
  .table-responsive {
    overscroll-behavior-x: contain;
    background:
      linear-gradient(to right, #fff 30%, rgba(255, 255, 255, 0)) left center / 24px 100% no-repeat,
      linear-gradient(to left, #fff 30%, rgba(255, 255, 255, 0)) right center / 24px 100% no-repeat,
      radial-gradient(farthest-side at 0 50%, rgba(15, 23, 42, 0.12), transparent) left center / 12px 100% no-repeat,
      radial-gradient(farthest-side at 100% 50%, rgba(15, 23, 42, 0.12), transparent) right center / 12px 100% no-repeat;
    background-attachment: local, local, scroll, scroll;
  }
}

/* ============================================================
   8. DEVICE LAYER — จริงจังกับ "มือถือทุกรุ่น / iPad / PC"
   ============================================================ */

/* 7.1 ปุ่มและช่องกรอกต้องสูงพอให้นิ้วแตะ (มาตรฐาน 44px ของ Apple/Google) */
@media (pointer: coarse) {
  .btn {
    min-height: 44px;
  }

  .btn-sm {
    min-height: 40px;
  }

  .form-control,
  select.form-control,
  input.form-control,
  textarea.form-control {
    min-height: 44px;
  }

  .nav-link,
  .sidebar-link {
    min-height: 44px;
    display: flex;
    align-items: center;
  }

  .header-breadcrumbs a,
  .alert-content a,
  .footer-links a,
  .styled-list a,
  .faq-answer a {
    display: inline-block;
    min-height: 32px;
    padding: 0.1rem 0;
  }
}

/* 7.2 iOS ซูมเข้าอัตโนมัติเมื่อโฟกัสช่องกรอกที่ตัวอักษรเล็กกว่า 16px
   ทำให้หน้าจอกระโดดทุกครั้ง — บังคับ 16px บนจอเล็กทั้งหมด */
@media (max-width: 768px) {
  .form-control,
  select.form-control,
  input.form-control,
  textarea.form-control,
  .swal2-input,
  .swal2-textarea,
  .swal2-select,
  .custom-dialog .form-control {
    font-size: 16px;
  }
}

/* 7.3 ร่องจอ (notch) และขอบล่างแบบ home indicator ของ iPhone
   ใช้ padding แบบ max() เพื่อไม่ให้ทับ content เดิม */
@supports (padding: env(safe-area-inset-left)) {
  .site-header .header-container,
  .site-footer .footer-container {
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
  }

  .site-footer {
    padding-bottom: max(1.5rem, env(safe-area-inset-bottom));
  }

  .admin-topbar {
    padding-left: max(1.25rem, env(safe-area-inset-left));
    padding-right: max(1.25rem, env(safe-area-inset-right));
  }

  .admin-sidebar {
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
}

/* 7.4 มือถือจอเล็กมาก: ลด padding ของ utility ให้เนื้อหาไม่เหลือพื้นที่แคบ */
@media (max-width: 480px) {
  .p-4 { padding: 1rem; }
  .p-5 { padding: 1.25rem; }
  .py-4 { padding-top: 1rem; padding-bottom: 1rem; }
  .py-5 { padding-top: 1.5rem; padding-bottom: 1.5rem; }
  .my-5 { margin-top: 1.25rem; margin-bottom: 1.25rem; }
  .mb-4 { margin-bottom: 1rem; }
  .empty-state-box { padding: 1.5rem 0.85rem; }
}

/* 7.5 มือถือจอเตี้ย (iphone จอนอน): ลดระยะแนวตั้งของส่วนหัว */
@media (max-height: 480px) and (orientation: landscape) {
  .hero-section {
    padding-top: 1.25rem;
    padding-bottom: 1.25rem;
  }

  .wizard-progress {
    margin-bottom: 1rem;
  }
}

/* 7.6 เคารพผู้ใช้ที่ปิดอนิเมชัน */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* 7.7 พิมพ์ใบสมัคร (ปุ่ม 🖨️ ในหน้าตรวจสอบสถานะ) ให้ได้กระดาษที่อ่านง่าย */
@media print {
  .site-header,
  .site-footer,
  .admin-topbar,
  .admin-sidebar,
  .mobile-menu-toggle,
  .form-actions,
  .no-print,
  .btn {
    display: none !important;
  }

  body {
    background: #ffffff !important;
  }

  .main-container,
  .admin-content {
    max-width: none;
    margin: 0;
    padding: 0;
  }

  .card,
  .form-card,
  .status-card-box,
  .review-block {
    border: 1px solid #94a3b8 !important;
    box-shadow: none !important;
    break-inside: avoid;
  }

  a[href^="http"]::after {
    content: " (" attr(href) ")";
    font-size: 0.75rem;
    color: #475569;
  }
}
