/* =====================================================
   Ticket Module — Mobile Responsive (Redesign v2)
   @author เต้ย + Miracle

   ลำดับ media queries: ใหญ่ → เล็ก
   เพื่อให้ breakpoint เล็กกว่า override ได้
   ===================================================== */

/* ===================== Touch device ===================== */
@media (hover: none) and (pointer: coarse) {
  .btn-sm {
    min-height: 36px;
    min-width: 36px;
  }
  .kanban-card {
    padding: 12px !important;
  }
  .dropdown-item {
    padding: 10px 16px !important;
  }
  .kanban-card:hover {
    transform: none !important;
  }
}

/* ===================== Tablet landscape ≤ 1024px ===================== */
@media (max-width: 1024px) {
  .kanban-scroll-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}

/* ===================== Tablet ≤ 768px ===================== */
@media (max-width: 768px) {
  .row-filter > div.w-150px {
    width: 48% !important;
    min-width: 48% !important;
  }

  .kanban-columns-wrapper {
    flex-wrap: wrap !important;
  }
  .kanban-column {
    flex: 0 0 48% !important;
    width: 48% !important;
    max-width: 48% !important;
    min-width: 0 !important;
  }
  .kanban-columns-wrapper.narrow .kanban-column,
  .kanban-columns-wrapper.wide .kanban-column {
    flex: 0 0 48% !important;
    width: 48% !important;
    max-width: 48% !important;
  }

  .gantt_container {
    font-size: 0.85rem;
  }

  .table td,
  .table th {
    padding: 6px 8px !important;
    font-size: 0.85rem;
  }
}

/* Desktop: ซ่อน mobile-only elements */
@media (min-width: 577px) {
  .mobile-filter-toggle,
  .mobile-filter-chips,
  .mobile-filter-sheet-overlay,
  .mobile-filter-sheet,
  .mobile-state-tabs,
  .mobile-swipe-indicator {
    display: none !important;
  }
}

/* ===================== Mobile ≤ 576px ===================== */
/* ต้องมาเป็น block สุดท้ายเพื่อ override tablet rules ทั้งหมด */
@media (max-width: 576px) {

  /* ==========================================================
     Task 1: ซ่อน Summary Section
     ========================================================== */
  #summary-section {
    display: none !important;
  }

  /* ==========================================================
     Task 2: Filter Bar → Compact + Bottom Sheet
     ========================================================== */

  /* ซ่อน filter ทั้งหมดเดิม — แสดงเฉพาะปุ่ม toggle */
  .row-filter {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 0 !important;
    position: relative;
  }

  /* ซ่อน filter dropdowns ทั้งหมด */
  .row-filter > div.me-2.w-150px {
    display: none !important;
  }

  /* ซ่อน date range + buttons row — แสดงใน bottom sheet */
  .row-filter > .ms-auto {
    display: none !important;
  }

  /* Mobile filter toggle button — inject ผ่าน JS */
  .mobile-filter-toggle {
    display: flex !important;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 0.5rem 0;
    gap: 0.5rem;
  }

  .mobile-filter-toggle .btn-filter-open {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    border: 1px solid #e4e6ea;
    border-radius: 8px;
    background: #f5f8fa;
    color: #5e6278;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
  }

  .mobile-filter-toggle .btn-filter-open:active {
    background: #e4e6ea;
  }

  .mobile-filter-toggle .btn-filter-open .filter-count {
    background: #009ef7;
    color: #fff;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    font-weight: 700;
  }

  .mobile-filter-toggle .mobile-action-btns {
    display: flex;
    gap: 0.5rem;
  }

  .mobile-filter-toggle .mobile-action-btns .btn {
    padding: 0.5rem 0.75rem !important;
    font-size: 0.8rem !important;
  }

  /* Active filter chips */
  .mobile-filter-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem;
    padding: 0.25rem 0 0.5rem;
  }

  .mobile-filter-chips:empty {
    display: none;
  }

  .mobile-filter-chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 10px;
    border-radius: 16px;
    background: #e8f4fd;
    color: #0095e8;
    font-size: 0.75rem;
    font-weight: 500;
    white-space: nowrap;
    max-width: 160px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .mobile-filter-chip .chip-remove {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: rgba(0, 149, 232, 0.2);
    color: #0095e8;
    font-size: 0.6rem;
    cursor: pointer;
    flex-shrink: 0;
  }

  /* Bottom sheet overlay */
  .mobile-filter-sheet-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1050;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s, visibility 0.3s;
  }

  .mobile-filter-sheet-overlay.active {
    opacity: 1;
    visibility: visible;
  }

  /* Bottom sheet */
  .mobile-filter-sheet {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: #fff;
    border-radius: 16px 16px 0 0;
    z-index: 1051;
    max-height: 85vh;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
    display: flex;
    flex-direction: column;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
  }

  .mobile-filter-sheet.active {
    transform: translateY(0);
  }

  .mobile-filter-sheet-handle {
    display: flex;
    justify-content: center;
    padding: 12px 0 8px;
    flex-shrink: 0;
  }

  .mobile-filter-sheet-handle::after {
    content: '';
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: #d1d5db;
  }

  .mobile-filter-sheet-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px 12px;
    border-bottom: 1px solid #f1f1f4;
    flex-shrink: 0;
  }

  .mobile-filter-sheet-header h6 {
    margin: 0;
    font-weight: 700;
    font-size: 1rem;
    color: #181c32;
  }

  .mobile-filter-sheet-body {
    padding: 16px;
    overflow-y: auto;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 1rem;
  }

  .mobile-filter-sheet-body .filter-group label {
    font-size: 0.8rem;
    font-weight: 600;
    color: #5e6278;
    margin-bottom: 0.375rem;
    display: block;
  }

  .mobile-filter-sheet-footer {
    padding: 12px 16px;
    border-top: 1px solid #f1f1f4;
    display: flex;
    gap: 0.75rem;
    flex-shrink: 0;
    padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  }

  .mobile-filter-sheet-footer .btn {
    flex: 1;
    padding: 0.625rem !important;
    font-weight: 600;
    border-radius: 8px;
  }

  /* ==========================================================
     Task 3: Kanban → Horizontal Swipe (ทีละ column)
     ========================================================== */

  /* State navigation tabs */
  .mobile-state-tabs {
    display: flex !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 6px;
    padding: 8px 0 12px;
    margin: 0 -4px;
  }

  .mobile-state-tabs::-webkit-scrollbar {
    display: none;
  }

  .mobile-state-tab {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 14px;
    border-radius: 20px;
    border: 1.5px solid #e4e6ea;
    background: #fff;
    color: #5e6278;
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
  }

  .mobile-state-tab .state-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
  }

  .mobile-state-tab .state-count {
    font-size: 0.7rem;
    font-weight: 700;
    background: #f1f1f4;
    color: #7e8299;
    border-radius: 10px;
    padding: 1px 7px;
    min-width: 20px;
    text-align: center;
  }

  .mobile-state-tab.active {
    border-color: var(--state-color, #009ef7);
    background: var(--state-color, #009ef7);
    color: #fff;
  }

  .mobile-state-tab.active .state-count {
    background: rgba(255, 255, 255, 0.3);
    color: #fff;
  }

  /* Kanban: horizontal snap scroll — 1 column per view
     ใช้ #kanban-view เพิ่ม specificity เพื่อชนะ inline styles ใน index.php */
  #kanban-view .kanban-scroll-container {
    overflow-x: auto !important;
    overflow-y: hidden !important;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    scroll-behavior: smooth;
  }

  #kanban-view .kanban-scroll-container::-webkit-scrollbar {
    display: none;
  }

  #kanban-view .kanban-columns-wrapper,
  #kanban-view .kanban-columns-wrapper.narrow,
  #kanban-view .kanban-columns-wrapper.wide {
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    width: max-content !important;
    min-width: max-content !important;
    gap: 0 !important;
    padding: 0 !important;
  }

  #kanban-view .kanban-column,
  #kanban-view .kanban-columns-wrapper.narrow .kanban-column,
  #kanban-view .kanban-columns-wrapper.wide .kanban-column {
    scroll-snap-align: start;
    flex: 0 0 calc(100vw - 48px) !important;
    width: calc(100vw - 48px) !important;
    max-width: calc(100vw - 48px) !important;
    min-width: calc(100vw - 48px) !important;
    height: calc(100vh - 320px) !important;
    max-height: calc(100vh - 320px) !important;
    margin: 0 8px !important;
    border-radius: 12px !important;
  }

  /* Collapsed columns: hide on mobile swipe mode */
  #kanban-view .kanban-column.collapsed {
    display: none !important;
  }

  /* Swipe indicator */
  .mobile-swipe-indicator {
    display: flex !important;
    justify-content: center;
    align-items: center;
    gap: 4px;
    padding: 8px 0;
  }

  .mobile-swipe-indicator .swipe-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #d1d5db;
    transition: all 0.3s;
  }

  .mobile-swipe-indicator .swipe-dot.active {
    width: 20px;
    border-radius: 3px;
    background: #009ef7;
  }

  /* ==========================================================
     Task 4: ซ่อน Kanban Controls
     ========================================================== */
  #kanban-controls {
    display: none !important;
  }

  /* ==========================================================
     Existing mobile styles (preserved)
     ========================================================== */

  /* --- Card header --- */
  .card-header {
    flex-direction: column !important;
    align-items: flex-start !important;
  }
  .card-header .card-toolbar {
    margin-top: 0.5rem;
    width: 100%;
  }

  /* --- View toggle + btn-group --- */
  .btn-group {
    flex-wrap: wrap !important;
  }
  .btn-group .btn {
    font-size: 0.8rem !important;
    padding: 0.35rem 0.5rem !important;
  }

  /* --- Modal: full screen --- */
  .modal-dialog {
    max-width: 100vw !important;
    margin: 0 !important;
    height: 100vh;
  }
  .modal-content {
    height: 100vh;
    border-radius: 0 !important;
  }
  .modal-body {
    overflow-y: auto;
    max-height: calc(100vh - 120px);
  }

  /* --- Tab navigation: horizontal scroll --- */
  .nav-tabs,
  #ticket-form-tabs {
    flex-wrap: nowrap !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .nav-tabs::-webkit-scrollbar,
  #ticket-form-tabs::-webkit-scrollbar {
    display: none;
  }
  .nav-tabs .nav-item,
  #ticket-form-tabs .nav-item {
    flex-shrink: 0;
  }

  /* --- Comment input: sticky bottom --- */
  .comment-input-container {
    position: sticky;
    bottom: 0;
    background: var(--kt-card-bg, #fff);
    padding: 8px;
    border-top: 1px solid var(--kt-border-color, #eff2f5);
    z-index: 10;
  }

  /* --- Gantt: horizontal scroll --- */
  .gantt_container {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  /* --- Performance: 2 per row --- */
  .performance-stats .col-md-3,
  .performance-stats .col-lg-3 {
    flex: 0 0 50% !important;
    max-width: 50% !important;
  }

  /* --- Ticket form: full width --- */
  .ticket-form .row > [class*="col-"] {
    flex: 0 0 100% !important;
    max-width: 100% !important;
  }

  /* --- Header name --- */
  .header-name {
    font-size: 0.85rem !important;
  }

  /* --- Row header --- */
  .row-header {
    margin-bottom: 0.5rem;
  }
}
