/* WAFA QR Patch v1.1.7 - Mobile Responsive Scan Fix
   Fokus: mencegah overlap header, hero scanner, dan card meja scan pada HP. */

@media (max-width: 640px) {
  html,
  body,
  #root {
    width: 100% !important;
    max-width: 100% !important;
    overflow-x: hidden !important;
  }

  body {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
  }

  #root * {
    box-sizing: border-box;
    min-width: 0;
  }

  /* Header utama: paksa stack agar logo, judul, jam, dan tombol tidak saling tindih. */
  header {
    position: sticky !important;
    top: 0 !important;
    z-index: 50 !important;
    overflow: hidden !important;
  }

  header > div {
    width: 100% !important;
    max-width: 100% !important;
    padding: 0.85rem 1rem !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 0.75rem !important;
  }

  header > div > div {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
  }

  header > div > div:first-child {
    display: grid !important;
    grid-template-columns: auto 1fr !important;
    align-items: center !important;
    gap: 0.75rem !important;
  }

  header img {
    width: 3rem !important;
    height: 3rem !important;
    object-fit: contain !important;
    flex-shrink: 0 !important;
  }

  header h1,
  header [class*="text-base"][class*="font-black"] {
    font-size: 1.05rem !important;
    line-height: 1.15 !important;
    letter-spacing: -0.015em !important;
    max-width: calc(100vw - 6rem) !important;
    white-space: normal !important;
    overflow-wrap: anywhere !important;
  }

  header p {
    max-width: calc(100vw - 6rem) !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
  }

  /* Bar tombol Meja Scan / Panel Admin */
  header > div > div:nth-child(2) {
    display: flex !important;
    flex-wrap: wrap !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 0.5rem !important;
  }

  header > div > div:nth-child(2) > div {
    max-width: 100% !important;
  }

  header button {
    white-space: nowrap !important;
  }

  header [class*="rounded-xl"][class*="bg-slate-950"] {
    width: 100% !important;
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 0.35rem !important;
    padding: 0.35rem !important;
  }

  header [class*="rounded-xl"][class*="bg-slate-950"] button {
    justify-content: center !important;
    padding: 0.75rem 0.5rem !important;
    font-size: 0.92rem !important;
  }

  /* Kontainer utama tidak boleh lebih lebar dari layar HP. */
  main,
  main > div,
  section,
  aside,
  .max-w-7xl,
  .max-w-5xl,
  .max-w-3xl {
    max-width: 100% !important;
  }

  main {
    padding-left: 1rem !important;
    padding-right: 1rem !important;
    padding-top: 1.25rem !important;
    padding-bottom: 1.5rem !important;
  }

  main > div:first-child {
    padding-left: 0 !important;
    padding-right: 0 !important;
  }

  /* Judul hero scanner agar tidak terlalu besar di HP. */
  main h2 {
    font-size: clamp(1.45rem, 7vw, 2rem) !important;
    line-height: 1.18 !important;
    letter-spacing: -0.02em !important;
    max-width: 100% !important;
    overflow-wrap: normal !important;
    word-break: normal !important;
    hyphens: none !important;
  }

  main h2 + p,
  main .text-center p {
    font-size: 0.86rem !important;
    line-height: 1.55 !important;
  }

  /* Card scanner dan dashboard: kurangi padding dan radius agar muat di layar kecil. */
  .rounded-3xl,
  .rounded-\[2rem\] {
    border-radius: 1.15rem !important;
  }

  .p-6 {
    padding: 1rem !important;
  }

  .p-8,
  .p-10 {
    padding: 1.15rem !important;
  }

  .gap-8 {
    gap: 1rem !important;
  }

  .space-y-8 > :not([hidden]) ~ :not([hidden]) {
    margin-top: 1rem !important;
  }

  .space-y-6 > :not([hidden]) ~ :not([hidden]) {
    margin-top: 1rem !important;
  }

  /* Grid dalam QR scanner harus benar-benar 1 kolom pada HP. */
  .grid {
    min-width: 0 !important;
  }

  .grid.grid-cols-1,
  .grid[class*="grid-cols-1"] {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  /* Panel mode otomatis: tombol Siswa/Guru dibuat stack agar tidak sempit. */
  .grid.grid-cols-2.gap-2.text-\[10px\],
  .grid.grid-cols-2.gap-1\.5 {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  /* Field, select, tombol, input tidak boleh melebar keluar layar. */
  input,
  select,
  textarea,
  button {
    max-width: 100% !important;
  }

  input,
  select,
  textarea {
    font-size: 16px !important; /* cegah zoom otomatis mobile */
  }

  /* Kamera scanner tetap proporsional dan tidak memotong layar kiri/kanan. */
  #wafa-qr-scanner-viewport {
    width: 100% !important;
    max-width: calc(100vw - 3.5rem) !important;
    min-height: min(330px, calc(100vw - 3.5rem)) !important;
    aspect-ratio: 1 / 1 !important;
    margin-left: auto !important;
    margin-right: auto !important;
  }

  #wafa-qr-scanner-viewport video,
  #wafa-qr-scanner-viewport canvas {
    max-width: 100% !important;
    min-height: min(330px, calc(100vw - 3.5rem)) !important;
  }

  /* Kartu ringkasan di meja scan tampil 1 kolom agar tidak overflow. */
  main .grid.grid-cols-2 {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  /* Sidebar admin di HP lebih aman dan tidak mendorong konten keluar layar. */
  aside {
    width: 100% !important;
    max-width: 100% !important;
    position: relative !important;
  }

  aside nav {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) !important;
  }

  /* Tabel master data di HP bisa digeser horizontal, tidak merusak layout. */
  table {
    min-width: 760px !important;
  }

  table th,
  table td {
    white-space: nowrap !important;
  }

  /* Hindari teks panjang merusak layout. */
  .truncate,
  [class*="truncate"] {
    min-width: 0 !important;
  }

  /* Alert/toast atas dibuat full aman di layar HP. */
  [role="alert"],
  .fixed,
  .absolute {
    max-width: calc(100vw - 1.5rem) !important;
  }
}

@media (min-width: 641px) and (max-width: 900px) {
  header > div {
    flex-wrap: wrap !important;
    gap: 0.75rem !important;
  }

  header > div > div:first-child {
    min-width: 0 !important;
    flex: 1 1 100% !important;
  }

  main h2 {
    font-size: 2rem !important;
    line-height: 1.15 !important;
  }

  #wafa-qr-scanner-viewport {
    max-width: 420px !important;
  }
}
