/* ══════════════════════════════════════════════════════════════════════
   CollabHub — Responsive / Mobile layer
   ----------------------------------------------------------------------
   Hand-authored plain CSS, loaded AFTER tailwind.css (survives rebuilds).
   Targets: iPhone 17 / 16 / Pro Max / SE · Android flagship + small · iPad
            & Android tablets · both portrait & landscape.

   Strategy
     • < 1024px  → off-canvas drawer sidebar + hamburger (phones & tablet portrait)
     • >= 1024px → original fixed sidebar untouched (desktop / tablet landscape)
     • Touch targets >= 44px · notch / dynamic-island safe-area · fluid spacing
     • Tables keep their columns and scroll horizontally (auto-wrapped in JS)
   ══════════════════════════════════════════════════════════════════════ */

/* Use dynamic viewport height so the iOS/Android URL bar can't clip the shell */
.ch-shell { height: 100vh; height: 100dvh; }

/* Chrome hooks hidden on desktop */
#ch-hamburger,
#ch-mobile-scrim { display: none; }

/* ──────────────────────────────────────────────────────────────────────
   PHONES + TABLET PORTRAIT  (< 1024px) — drawer navigation
   ────────────────────────────────────────────────────────────────────── */
@media (max-width: 1023px) {

  /* — Hamburger in the topbar — */
  #ch-hamburger {
    display: grid; place-items: center;
    flex: 0 0 44px; width: 44px; height: 44px; margin-left: -6px; margin-right: 2px;
    border: 0; border-radius: 0.625rem; cursor: pointer;
    background: transparent; color: var(--ch-text-2);
    transition: background-color .15s;
  }
  #ch-hamburger:hover { background: var(--ch-surface-3); }
  #ch-hamburger:active { background: var(--ch-surface-3); }

  /* The desktop sidebar control becomes the drawer close control on touch layouts. */
  #ch-sidebar-toggle { flex: 0 0 44px; width: 44px; height: 44px; }

  /* — Sidebar becomes an off-canvas drawer — */
  #ch-sidebar {
    position: fixed; top: 0; left: 0;
    height: 100vh; height: 100dvh;
    width: 284px !important;
    max-width: 86vw;
    z-index: 80;
    transform: translateX(-100%);
    transition: transform .28s cubic-bezier(.16, 1, .3, 1);
    will-change: transform;
    box-shadow: 6px 0 32px rgba(0, 0, 0, .28);
    padding-top: env(safe-area-inset-top, 0px);
    padding-left: env(safe-area-inset-left, 0px);
  }
  html[data-mobile-nav="open"] #ch-sidebar { transform: translateX(0); }

  /* Force the expanded look while it is a drawer, even if the user left the
     desktop sidebar collapsed (data-state="collapsed"). */
  #ch-sidebar[data-state="collapsed"] { width: 284px !important; }
  #ch-sidebar[data-state="collapsed"] .sb-text,
  #ch-sidebar[data-state="collapsed"] .sb-badge,
  #ch-sidebar[data-state="collapsed"] .sb-section,
  #ch-sidebar[data-state="collapsed"] .sb-sub,
  #ch-sidebar[data-state="collapsed"] .sb-search,
  #ch-sidebar[data-state="collapsed"] .sb-foot { display: revert; opacity: 1; }
  #ch-sidebar[data-state="collapsed"] .sb-search-collapsed { display: none; }
  #ch-sidebar[data-state="collapsed"] .sb-header { justify-content: space-between; }
  #ch-sidebar[data-state="collapsed"] .nav-link,
  #ch-sidebar[data-state="collapsed"] .sb-item { justify-content: flex-start; }

  /* Bigger touch targets inside the drawer nav (>= 44px) */
  #ch-sidebar .nav-link,
  #ch-sidebar .nav-sub { min-height: 44px; }
  #ch-sidebar .nav-link { padding-top: 0.6rem; padding-bottom: 0.6rem; }
  #ch-sidebar .nav-sub  { padding-top: 0.55rem; padding-bottom: 0.55rem; }

  /* — Scrim behind the drawer — */
  #ch-mobile-scrim {
    display: block; position: fixed; inset: 0; z-index: 70;
    background: rgba(0, 0, 0, .45);
    -webkit-backdrop-filter: blur(2px); backdrop-filter: blur(2px);
    opacity: 0; pointer-events: none;
    transition: opacity .28s ease;
  }
  html[data-mobile-nav="open"] #ch-mobile-scrim { opacity: 1; pointer-events: auto; }
  html[data-mobile-nav="open"] body { overflow: hidden; }

  /* — Topbar: tighten + respect side safe-areas — */
  header.ch-topbar {
    padding-left: max(0.875rem, env(safe-area-inset-left)) !important;
    padding-right: max(0.875rem, env(safe-area-inset-right)) !important;
    gap: 0.25rem;
  }
  /* Hide the long search pill on small screens (cmd-K still on hamburger-adjacent) */
  header.ch-topbar .ch-topbar-search { display: none !important; }
  /* Topbar controls: WCAG-aligned touch target. */
  header.ch-topbar button,
  header.ch-topbar a,
  header.ch-topbar select { min-height: 44px; }

  /* — Main content: fluid padding + safe-area + bottom breathing room — */
  main.ch-main {
    padding-left: max(0.9rem, env(safe-area-inset-left));
    padding-right: max(0.9rem, env(safe-area-inset-right));
    padding-top: 1.1rem;
    padding-bottom: calc(3.5rem + env(safe-area-inset-bottom, 0px));
  }

  /* — Tables keep columns + scroll horizontally (wrapper added by JS) — */
  .table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .table-scroll > table { min-width: max-content; }
  /* Force single-line cells so the table grows and scrolls instead of the
     rows getting tall — except cells that already self-limit their width. */
  .table-scroll .data-table td,
  .table-scroll .data-table th { white-space: nowrap; }
  .table-scroll .data-table .truncate,
  .table-scroll .data-table .line-clamp-2,
  .table-scroll .data-table .refjob,
  .table-scroll .data-table .whitespace-normal { white-space: normal; }

  /* Modals already go full-screen < 640px (see input.css #6); make sure the
     tablet range (641–1023) keeps them comfortably inset. */
  #modal-panel { max-width: min(92vw, 640px); }

  /* Page section headers wrap their action buttons gracefully */
  main.ch-main .flex.justify-between.items-start.flex-wrap > .flex { row-gap: 0.5rem; }
}

/* Material request modals reuse the Job Assignment touch-target pattern without
   widening shared button styles outside the small-screen dialog. */
@media (max-width: 1023px) {
  .mr-modal-wide .btn,
  .mr-modal-wide .btn-close { min-height: 44px; min-width: 44px; }
  .mr-modal-wide .mr-del-line { min-width: 44px; }
}

/* Date picker (`.ch-fp`, templates/base.html initDatePickers) — touch targets >= 44px.
   code review 2026-07-27: grep แล้วไม่มีกฎไหนในไฟล์นี้เคยแตะ .ch-fp/flatpickr เลย ทั้งที่ไฟล์นี้
   ประกาศมาตรฐาน 44px ไว้ตั้งแต่หัวไฟล์ (บรรทัด 11) แล้วบังคับจริงอีก 5 จุด — วัดจริงที่ 390×844:
   .flatpickr-day 39×36 · .flatpickr-prev/next-month 30×34 · .ch-fp-btn (Today/Clear) 140×33.6
   ไม่มี branch แยกตาม touch เลยสักจุด
   ที่อยู่: อยู่ไฟล์นี้ (ไม่ใช่ assets/css/input.css ที่ .ch-fp ถูกนิยาม) เพราะไฟล์นี้เป็นเจ้าของ
   มาตรฐาน 44px อยู่แล้วแต่เดียว (ดูกฎ 5 จุดข้างบน) — ทุกกฎ 44px กระจุกที่เดียว หาด้วย grep คำเดียว
   ได้ครบ ไม่ต้องแยกไปอีกไฟล์ที่ไม่มีใครนึกถึง เหมือนกับ `header.ch-topbar select`/`.mr-modal-wide`
   ด้านบนที่ override component ซึ่งนิยามอยู่ไฟล์อื่นอยู่แล้วเป็น pattern เดิม (ไม่ใช่การ duplicate
   กฎ — override เฉพาะ property ขนาด ไม่แตะ color/theme ที่ยังเป็นเจ้าของโดย input.css)
   เงื่อนไข: ใช้ `@media (pointer: coarse)` ไม่ใช่ความกว้างจอ (ต่างจาก 5 จุดข้างบนที่ใช้
   max-width:1023px) เพราะต้องตรงกับเงื่อนไขเดียวกับที่ JS ใช้ตัดสิน readOnly ของช่องวันที่
   (`chDatePickerAllowsTyping()` ใน base.html เช็ค `matchMedia('(pointer: coarse)')`) — ถ้าใช้
   max-width จะพลาด 2 ทาง: แท็บเล็ต/จอกว้างที่เป็นทัชสกรีนจะไม่ได้ปุ่มใหญ่ทั้งที่ readOnly ผ่านทาง
   pointer:coarse เหมือนกัน และเดสก์ท็อปจอแคบที่ใช้เมาส์จะโดนขยายปุ่มเกินจำเป็น
   ⚠️ ยังไม่มีใครยืนยันบนอุปกรณ์ touch จริง — ตรวจได้แค่ผ่าน DevTools device emulation (ดู
   docs handoff รอบนี้ว่า emulation จำลอง pointer:coarse ได้จริงแค่ไหน) */
@media (pointer: coarse) {
  /* .dayContainer/.flatpickr-days ของ vendor เป็น px ตายตัว (307.875px, ไม่ผูก viewport เลย) และ
     .flatpickr-days{overflow:hidden} ด้วย — ขยาย .flatpickr-day แล้วไม่ขยายกล่องแม่ทั้งคู่ตาม จะโดน
     ตัดขอบวันสุดท้ายของแถวทิ้งไปเงียบๆ ขยับเป็น 308px (44×7 พอดี, ต่างจากเดิมแค่ 0.125px) กัน
     ปัญหานี้โดยแทบไม่กระทบความกว้างปฏิทินที่เห็น */
  .flatpickr-calendar.ch-fp,
  .ch-fp .flatpickr-days,
  .ch-fp .dayContainer {
    width: 308px;
    min-width: 308px;
    max-width: 308px;
  }
  .ch-fp .flatpickr-day {
    width: 44px;
    max-width: 44px;
    height: 44px;
    line-height: 44px;
  }
  /* ลูกศรเปลี่ยนเดือน: vendor เดิม height:34px เท่ากับความสูงทั้งแถบเดือน (.flatpickr-month มี
     overflow:hidden ด้วย) — ขยายปุ่มเฉยๆ โดยไม่ขยายแถบจะโดนตัดพื้นที่คลิกส่วนเกินทิ้งเงียบๆ เหมือนกัน
     ต้องขยายทั้งแถบไปด้วยเสมอ */
  .ch-fp .flatpickr-months,
  .ch-fp .flatpickr-months .flatpickr-month {
    height: 44px;
  }
  /* ป้ายเดือน/ปี (ตำแหน่ง absolute, top ตายตัวจาก vendor) เลื่อนลงมาให้อยู่กึ่งกลางแนวตั้งของแถบ
     44px ใหม่ — เดิมอยู่กึ่งกลางของแถบ 34px (padding-top:7.48px ใน 34px) ค่า top ใหม่ = ครึ่งหนึ่งของ
     ส่วนสูงที่เพิ่ม ((44-34)/2 = 5px) บวกเข้ากับตำแหน่งเดิม (top:auto เดิม = 0) */
  .ch-fp .flatpickr-current-month {
    top: 5px;
  }
  .ch-fp .flatpickr-months .flatpickr-prev-month,
  .ch-fp .flatpickr-months .flatpickr-next-month {
    height: 44px;
    width: 44px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  /* ปุ่ม Today/Clear: ความสูงเดิมมาจาก padding ของ .ch-fp-btn เท่านั้น (ไม่ใช่ height ตรงๆ) —
     min-height ปลอดภัยกว่าเพราะ .ch-fp-btn เป็น flex item (flex:1) ของ .ch-fp-footer อยู่แล้ว
     ความกว้างพอเกิน 44px อยู่แล้วจากการวัดจริง (140px) ไม่ต้องแตะ
     🔴 code review ก่อน push (2026-07-27): เดิมกฎนี้ไม่ scope ใต้ `.ch-fp` เหมือนกฎอื่นทุกจุดใน
     block นี้ — Filter Preset (templates/base.html #ch-fp-bar) ก็ใช้ prefix `ch-fp-*` เหมือนกัน
     (`.ch-fp-name`/`.ch-fp-label`/`.ch-fp-empty`/`.ch-fp-freq`/`.ch-fp-btns` — ปุ่มจริงของ Filter
     Preset คือ `.ch-fp-btns` ผสม s ต่อท้าย ไม่ใช่ `.ch-fp-btn` ตัวเดียวกัน ตอนนี้เลยยังไม่ชนกันจริง
     แต่ห่างกันตัวอักษรเดียว ปลอดภัยกว่าถ้า scope ไว้ตั้งแต่ตอนนี้ — Filter Preset เปราะ ห้ามแตะโค้ด
     ส่วนนั้นเด็ดขาด นี่คือการแก้ฝั่งกฎ CSS ของปฏิทินเท่านั้น) — `.ch-fp .ch-fp-btn` ปลอดภัยเพราะ
     footer (containing .ch-fp-btn) ถูก appendChild เข้า fp.calendarContainer ตรงๆ (ดู
     templates/base.html บรรทัด ~1116-1132 onReady) ซึ่งมีคลาส .ch-fp อยู่แล้วเสมอ — เป็น
     descendant จริง ไม่ใช่แค่ sibling */
  .ch-fp .ch-fp-btn {
    min-height: 44px;
  }
}

/* ──────────────────────────────────────────────────────────────────────
   SMALL PHONES  (<= 430px)  — iPhone Pro Max down to 360px Android
   ────────────────────────────────────────────────────────────────────── */
@media (max-width: 430px) {
  main.ch-main {
    padding-left: max(0.8rem, env(safe-area-inset-left));
    padding-right: max(0.8rem, env(safe-area-inset-right));
  }
  /* KPI / stat grids: 2-up stays readable; oversized hero numbers trimmed */
  main.ch-main .text-\[26px\] { font-size: 22px; }
  /* Action-button rows in page headers: let primary buttons fill the row */
  main.ch-main header .btn,
  main.ch-main .page-actions .btn { flex: 1 1 auto; justify-content: center; }
  /* Charts that are fixed-height shrink a touch on tiny screens */
  #ch-pipeline { height: 150px !important; }
}

/* ──────────────────────────────────────────────────────────────────────
   VERY SMALL  (<= 360px)  — compact Android / iPhone SE
   ────────────────────────────────────────────────────────────────────── */
@media (max-width: 360px) {
  /* Force single-column for 2-up KPI grids that would crowd at 320–360px */
  main.ch-main .grid.grid-cols-2:not(.keep-2col) { grid-template-columns: 1fr; }
}

/* ──────────────────────────────────────────────────────────────────────
   AUTH PAGES (login / force-password) — hero hides, form centers
   The templates already use lg:grid-cols-2 + hidden lg:flex, so nothing to
   override; we only guarantee the form panel can scroll on short landscapes.
   ────────────────────────────────────────────────────────────────────── */
@media (max-height: 560px) and (max-width: 1023px) {
  .ch-auth-scroll { overflow-y: auto; }
}

/* ──────────────────────────────────────────────────────────────────────
   Restore desktop drawer transition cleanup (>=1024) — make sure no stray
   transform lingers if the viewport grows past the breakpoint.
   ────────────────────────────────────────────────────────────────────── */
@media (min-width: 1024px) {
  #ch-sidebar { transform: none !important; }
}
