/**
 * RESPONSIVE CSS - Tối Ưu Toàn Diện Cho Mobile & Tablet
 * Khắc phục triệt để:
 * 1. Bảng bị ép dọc từng chữ trên mobile -> Chuyển sang dạng Thẻ Card thoáng đãng
 * 2. Navbar dài gây tràn ngang màn hình -> Thu gọn và chuyển sang Mobile Bottom Nav
 * 3. Bento vượt khung -> Căn chỉnh 100% lọt lòng màn hình điện thoại
 */

/* ====================================================================
   1. QUY TẮC HIỂN THỊ DESKTOP (Mặc định > 768px)
   ==================================================================== */
.desktop-table-container {
  display: block;
}
.mobile-card-list {
  display: none;
}
.mobile-bottom-nav {
  display: none;
}

/* ====================================================================
   2. TABLET (769px - 1024px)
   ==================================================================== */
@media (max-width: 1024px) {
  .bento-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 1rem;
  }
  .bento-card.span-4x1,
  .bento-card.span-3x1,
  .bento-card.span-2x1 {
    grid-column: span 2;
  }
  .bento-card.span-1x2,
  .bento-card.span-2x2 {
    grid-column: span 2;
    grid-row: auto;
  }
}

/* ====================================================================
   3. MOBILE CHUYÊN DỤNG (<= 768px)
   ==================================================================== */
@media (max-width: 768px) {
  /* Chặn hoàn toàn thanh cuộn ngang gây lệch màn hình */
  html, body {
    width: 100% !important;
    max-width: 100vw !important;
    overflow-x: hidden !important;
    position: relative;
    font-size: 14.5px;
  }

  .app-container {
    width: 100% !important;
    max-width: 100vw !important;
    overflow-x: hidden !important;
  }

  /* CHUYỂN ĐỔI BẢNG: Ẩn bảng table, Hiện thẻ Mobile Card */
  .desktop-table-container {
    display: none !important;
  }

  .mobile-card-list {
    display: flex !important;
    flex-direction: column !important;
    gap: 0.85rem !important;
    width: 100% !important;
    box-sizing: border-box !important;
  }

  .mobile-device-card,
  .mobile-audit-card {
    cursor: pointer !important;
    margin: 0 !important;
    width: 100% !important;
    box-sizing: border-box !important;
    box-shadow: var(--shadow-md) !important;
    border: 1px solid var(--border-light) !important;
  }

  /* MAIN CONTENT: Đệm vừa vặn, không thụt lề quá sâu */
  .main-content {
    padding: 0.75rem 0.65rem 5.5rem 0.65rem !important;
    width: 100% !important;
    max-width: 100vw !important;
    box-sizing: border-box !important;
    overflow-x: hidden !important;
  }

  /* TOP NAVBAR: Thu gọn thành thanh Header chuẩn app mobile */
  .top-navbar {
    padding: 0.55rem 0.75rem !important;
    width: 100% !important;
    max-width: 100vw !important;
    box-sizing: border-box !important;
    display: flex !important;
    align-items: center !important;
    justify-content: space-between !important;
    overflow: hidden !important;
  }

  .brand-section {
    gap: 0.5rem !important;
    max-width: calc(100% - 55px) !important;
    flex: 1 !important;
    min-width: 0 !important;
  }

  .brand-logo-badge {
    width: 34px !important;
    height: 34px !important;
    font-size: 0.9rem !important;
    border-radius: 8px !important;
    flex-shrink: 0 !important;
  }

  .brand-logo-img {
    height: 32px !important;
    max-width: 68px !important;
    object-fit: contain !important;
    flex-shrink: 0 !important;
  }

  .brand-text-container {
    min-width: 0 !important;
    flex: 1 !important;
  }

  .brand-company {
    display: none !important; /* Ẩn dòng text công ty quá dài */
  }

  .brand-station {
    font-size: 0.92rem !important;
    font-weight: 800 !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    display: block !important;
    width: 100% !important;
  }

  /* Ẩn các nút điều hướng dài ở top navbar trên mobile */
  .top-navbar .nav-actions .nav-item-btn:not(.nav-item-settings) {
    display: none !important;
  }

  .top-navbar .nav-actions {
    gap: 0.35rem !important;
    flex-shrink: 0 !important;
  }

  .top-navbar .nav-item-settings {
    padding: 0.4rem 0.55rem !important;
    font-size: 1.1rem !important;
  }

  /* BENTO GRID: 100% lọt lòng, không tràn màn hình */
  .bento-grid {
    display: grid !important;
    grid-template-columns: 100% !important; /* 1 cột duy nhất lọt lòng */
    grid-auto-rows: auto !important;
    gap: 0.85rem !important;
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
  }

  /* Ép tất cả thẻ Bento vào 1 cột */
  .bento-card,
  .bento-card.span-4x1,
  .bento-card.span-3x1,
  .bento-card.span-2x1,
  .bento-card.span-1x1,
  .bento-card.span-2x2,
  .bento-card.span-1x2 {
    grid-column: 1 / -1 !important;
    grid-row: auto !important;
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
    padding: 1.15rem 1rem !important;
    border-radius: 16px !important;
    margin: 0 !important;
    min-width: 0 !important;
    overflow: hidden !important;
  }

  /* Font chữ và tiêu đề trong thẻ Bento */
  .bento-title {
    font-size: 1.05rem !important;
    line-height: 1.35 !important;
    word-break: break-word !important;
  }

  .bento-desc {
    font-size: 0.82rem !important;
    line-height: 1.45 !important;
    word-break: break-word !important;
  }

  .bento-metric-large {
    font-size: 1.75rem !important;
    margin: 0.35rem 0 !important;
  }

  .bento-header {
    margin-bottom: 0.65rem !important;
  }

  .bento-footer {
    margin-top: 0.75rem !important;
    padding-top: 0.65rem !important;
    font-size: 0.82rem !important;
  }

  /* Page Header trên Mobile */
  .page-header {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 0.65rem !important;
    margin-bottom: 0.85rem !important;
    width: 100% !important;
    box-sizing: border-box !important;
  }

  .page-title-group h1 {
    font-size: 1.3rem !important;
    line-height: 1.25 !important;
    word-break: break-word !important;
  }

  .page-title-group p {
    font-size: 0.8rem !important;
    line-height: 1.35 !important;
  }

  .page-header-actions {
    width: 100% !important;
    display: flex !important;
    gap: 0.5rem !important;
    flex-wrap: wrap !important;
  }

  .page-header-actions .btn {
    flex: 1 !important;
    min-width: 130px !important;
    justify-content: center !important;
    padding: 0.55rem 0.65rem !important;
    font-size: 0.8rem !important;
  }

  /* Tabs cuộn ngang mượt mà */
  .tabs-header {
    display: flex !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    scrollbar-width: none !important;
    gap: 0.35rem !important;
    margin-bottom: 0.85rem !important;
    width: 100% !important;
    box-sizing: border-box !important;
  }

  .tabs-header::-webkit-scrollbar {
    display: none;
  }

  .tab-btn {
    flex-shrink: 0 !important;
    padding: 0.55rem 0.85rem !important;
    font-size: 0.82rem !important;
  }

  /* Responsive Grids trong các module */
  .responsive-grid-2,
  .responsive-grid-detail {
    display: grid !important;
    grid-template-columns: 100% !important;
    gap: 0.85rem !important;
    width: 100% !important;
    box-sizing: border-box !important;
  }

  /* Modal trên Mobile */
  #app-modal-overlay > div {
    width: 95% !important;
    max-width: 95% !important;
    padding: 1.25rem 1rem !important;
    border-radius: 18px !important;
    margin: 0 auto !important;
  }
}

/* ====================================================================
   4. BOTTOM MOBILE NAVIGATION BAR (Thanh điều hướng ngón tay cái)
   ==================================================================== */
@media (max-width: 768px) {
  .mobile-bottom-nav {
    display: flex !important;
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    z-index: 9999 !important;
    background: rgba(255, 255, 255, 0.97) !important;
    backdrop-filter: blur(20px) !important;
    -webkit-backdrop-filter: blur(20px) !important;
    border-top: 1px solid var(--border-light) !important;
    box-shadow: 0 -4px 16px rgba(15, 23, 42, 0.08) !important;
    justify-content: space-around !important;
    align-items: center !important;
    padding: 0.35rem 0.2rem !important;
    padding-bottom: calc(0.35rem + env(safe-area-inset-bottom, 0px)) !important;
    height: 56px !important;
    box-sizing: border-box !important;
  }

  .mobile-nav-item {
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    text-decoration: none !important;
    color: var(--text-secondary) !important;
    font-size: 0.65rem !important;
    font-weight: 600 !important;
    padding: 0.25rem 0.35rem !important;
    border-radius: 10px !important;
    transition: var(--transition-normal) !important;
    min-width: 54px !important;
    gap: 0.1rem !important;
  }

  .mobile-nav-item span:first-child {
    font-size: 1.2rem !important;
    line-height: 1 !important;
  }

  .mobile-nav-item.active {
    color: var(--primary) !important;
    font-weight: 800 !important;
  }

  /* Nút chụp ảnh OCR nổi bật ở giữa */
  .mobile-nav-item.highlight-btn {
    color: white !important;
    background: linear-gradient(135deg, var(--primary) 0%, #0284c7 100%) !important;
    box-shadow: 0 4px 12px rgba(26, 86, 219, 0.4) !important;
    border-radius: 14px !important;
    padding: 0.4rem 0.65rem !important;
    transform: translateY(-8px) !important;
  }

  /* ====================================================================
     MODAL XEM TÀI LIỆU & OCR RESPONSIVE (KHẮC PHỤC CHÈN TEXT TRÊN MOBILE)
     ==================================================================== */
  #app-modal-overlay {
    z-index: 100000 !important;
  }

  .doc-modal-tab-bar {
    display: flex !important;
    gap: 6px !important;
    background: #e2e8f0 !important;
    padding: 4px !important;
    border-radius: 12px !important;
    margin-bottom: 0.85rem !important;
  }

  .doc-modal-tab-btn {
    flex: 1 !important;
    padding: 8px 6px !important;
    font-size: 0.82rem !important;
    font-weight: 700 !important;
    border-radius: 8px !important;
    border: none !important;
    background: transparent !important;
    color: #475569 !important;
    cursor: pointer !important;
    text-align: center !important;
    transition: all 0.2s ease !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 4px !important;
  }

  .doc-modal-tab-btn.active {
    background: #ffffff !important;
    color: #1a56db !important;
    font-weight: 800 !important;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.12) !important;
  }

  .doc-modal-body-grid {
    display: block !important;
    width: 100% !important;
  }

  .doc-modal-body-grid .doc-col-image img {
    width: 100% !important;
    max-height: 58vh !important;
    object-fit: contain !important;
    border-radius: 12px !important;
    background: #0f172a !important;
  }

  .doc-modal-body-grid .doc-col-text {
    width: 100% !important;
  }
}

/* ====================================================================
   4. MODAL VĂN BẢN SỐ HÓA & DOCUMENT SHEET (ÁP DỤNG CHUNG DESKTOP & MOBILE)
   ==================================================================== */
#app-modal-overlay {
  z-index: 100000 !important;
}

.doc-modal-tab-bar {
  display: flex;
  gap: 8px;
  background: #f1f5f9;
  padding: 5px;
  border-radius: 12px;
  margin-bottom: 1rem;
  border: 1px solid #e2e8f0;
}

.doc-modal-tab-btn {
  flex: 1;
  padding: 8px 14px;
  font-size: 0.88rem;
  font-weight: 700;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: #64748b;
  cursor: pointer;
  text-align: center;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.doc-modal-tab-btn:hover {
  background: rgba(255, 255, 255, 0.6);
  color: #1e293b;
}

.doc-modal-tab-btn.active {
  background: #ffffff !important;
  color: #1a56db !important;
  font-weight: 800 !important;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12) !important;
}

.doc-modal-body-grid {
  display: block;
  width: 100%;
}

/* KHUNG VĂN BẢN CHUẨN A4 / DOCUMENT SHEET SANG TRỌNG */
.doc-sheet-paper {
  background: #ffffff;
  border: 1px solid #cbd5e1;
  box-shadow: 0 4px 20px rgba(15, 23, 42, 0.06);
  border-radius: 12px;
  padding: 1.5rem 1.25rem;
  font-family: 'Be Vietnam Pro', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  color: #1e293b;
}

.doc-sheet-title {
  text-align: center;
  font-weight: 900;
  font-size: 1.12rem;
  color: #1e3a8a;
  text-transform: uppercase;
  margin: 0.85rem 0 0.35rem;
  letter-spacing: 0.3px;
  line-height: 1.4;
}

.doc-sheet-number {
  text-align: center;
  font-size: 0.85rem;
  font-style: italic;
  color: #475569;
  margin-bottom: 1.15rem;
  font-weight: 600;
}

.doc-info-row {
  display: flex;
  margin-bottom: 0.5rem;
  font-size: 0.88rem;
  line-height: 1.55;
  border-bottom: 1px dashed #f1f5f9;
  padding-bottom: 0.35rem;
}

.doc-info-label {
  font-weight: 700;
  color: #475569;
  min-width: 140px;
  flex-shrink: 0;
}

.doc-info-value {
  color: #0f172a;
  font-weight: 600;
}

.doc-section-heading {
  font-size: 0.92rem;
  font-weight: 800;
  color: #0f172a;
  text-transform: uppercase;
  letter-spacing: 0.2px;
  margin: 1.15rem 0 0.65rem;
  padding-bottom: 0.25rem;
  border-bottom: 1.5px solid #cbd5e1;
}

.doc-list-item {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  margin-bottom: 0.45rem;
  font-size: 0.88rem;
  line-height: 1.6;
  color: #334155;
}

.doc-list-bullet {
  font-weight: 800;
  color: #1a56db;
  flex-shrink: 0;
}

.doc-conclusion-box {
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  border-left: 4px solid #16a34a;
  padding: 0.85rem 1rem;
  border-radius: 8px;
  margin: 1.15rem 0;
  font-weight: 600;
  color: #14532d;
  line-height: 1.6;
  font-size: 0.9rem;
}

.doc-signatures-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-top: 1.5rem;
  padding-top: 1rem;
  border-top: 1px solid #e2e8f0;
  text-align: center;
}

.doc-sig-title {
  font-size: 0.76rem;
  font-weight: 800;
  text-transform: uppercase;
  color: #475569;
  margin-bottom: 2.5rem;
}

.doc-sig-name {
  font-size: 0.85rem;
  font-weight: 800;
  color: #0f172a;
}

/* ====================================================================
   5. IN ẤN (A4 PRINTING)
   ==================================================================== */
@media print {
  body {
    background: white !important;
    color: black !important;
  }
  .top-navbar,
  .mobile-bottom-nav,
  .app-footer,
  .btn,
  .no-print {
    display: none !important;
  }
  .main-content {
    padding: 0 !important;
    max-width: 100% !important;
  }
  .bento-card {
    box-shadow: none !important;
    border: 1px solid #ccc !important;
    break-inside: avoid;
  }
}
