/**
 * mobile-modals-and-forms.css
 * Comprehensive mobile responsiveness, bottom-sheet modal architecture,
 * and form control ergonomics for the AUTOGRADING platform.
 * 
 * Target viewports: Mobile 320px - 430px, Foldables, Tablets (<= 768px).
 * File length strictly maintained under 800-line architectural ceiling.
 */

/* ==========================================================================
   1. UNIVERSAL IOS SAFARI AUTO-ZOOM PREVENTION & TOUCH TARGETS
   ========================================================================== */
@media (max-width: 768px) {
  input[type="text"],
  input[type="password"],
  input[type="email"],
  input[type="number"],
  input[type="search"],
  input[type="tel"],
  input[type="url"],
  select,
  textarea,
  .form-control,
  .field {
    font-size: 16px !important;
  }

  button,
  .btn,
  .dock-item,
  a.btn {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }
}

/* ==========================================================================
   2. MOBILE BOTTOM DOCK CLEARANCE (NO OBSCURED CONTENT)
   ========================================================================== */
@media (max-width: 768px) {
  body:not(.portal-mode):not(.parent-mode) .workspace-container,
  body:not(.portal-mode):not(.parent-mode) .hub-grid,
  body:not(.portal-mode):not(.parent-mode) .exams-list,
  body:not(.portal-mode):not(.parent-mode) #exam-hub-section,
  body:not(.portal-mode):not(.parent-mode) .view-exam-detail,
  body:not(.portal-mode):not(.parent-mode) .exam-detail-content,
  body:not(.portal-mode):not(.parent-mode) .submissions-container,
  body:not(.portal-mode):not(.parent-mode) .classes-view-container,
  body:not(.portal-mode):not(.parent-mode) .tools-view-container,
  body:not(.portal-mode):not(.parent-mode) .math-visual-container {
    padding-bottom: calc(76px + env(safe-area-inset-bottom, 20px) + 1.25rem) !important;
  }
}

/* ==========================================================================
   3. "GIANT OVAL EGG" BUG ELIMINATION & SEGMENTED SWITCHER FIXES
   ========================================================================== */
@media (max-width: 768px) {
  /* Exam Hub Filter Ribbon: clean 2x2 grid on mobile so all 4 tabs fit without scrolling */
  #exam-status-filters,
  .bento-filter-ribbon#exam-status-filters,
  .bento-segmented-group#exam-status-filters {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    overflow: visible !important;
    border-radius: 12px !important;
    padding: 4px !important;
    gap: 4px !important;
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
  }

  #exam-status-filters::-webkit-scrollbar {
    display: none !important;
  }

  #exam-status-filters .bento-filter-pill,
  #exam-status-filters .hub-filter-pill,
  #exam-status-filters button {
    width: 100% !important;
    max-width: none !important;
    white-space: nowrap !important;
    padding: 0.36rem 0.5rem !important;
    border-radius: 8px !important;
    min-height: 34px !important;
    font-size: 0.78rem !important;
    text-align: center !important;
    justify-content: center !important;
  }

  /* Exam Hub Toolbar on Mobile */
  .card.exams .toolbar,
  .exam-hub-card .toolbar,
  #exam-hub-section .toolbar {
    display: flex !important;
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 8px !important;
    padding: 10px 12px !important;
    width: 100% !important;
    box-sizing: border-box !important;
  }

  .card.exams .toolbar .search,
  .exam-hub-card .toolbar .search,
  #exam-hub-section .toolbar .search,
  #exam-hub-section .toolbar .exam-hub-search-box-wrap {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    flex: 0 0 auto !important;
    margin: 0 !important;
    box-sizing: border-box !important;
  }

  /* Subscription Billing Cycle Switcher */
  .cycle-switcher-wrapper {
    display: grid !important;
    grid-template-columns: repeat(3, 1fr) !important;
    border-radius: 12px !important;
    width: 100% !important;
    box-sizing: border-box !important;
    padding: 4px !important;
    gap: 4px !important;
  }

  .cycle-switch-btn {
    border-radius: 8px !important;
    padding: 0.5rem 0.25rem !important;
    min-height: 40px !important;
    font-size: 0.76rem !important;
    width: 100% !important;
    text-align: center !important;
    justify-content: center !important;
  }

  /* Sub-Modal Tabs Nav (No clipping mask) */
  .sub-modal-tab-nav {
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    scrollbar-width: none !important;
    mask-image: none !important;
    -webkit-mask-image: none !important;
    padding: 4px !important;
    gap: 4px !important;
    border-radius: 12px !important;
    width: 100% !important;
    box-sizing: border-box !important;
  }

  .sub-modal-tab-nav::-webkit-scrollbar {
    display: none !important;
  }

  .sub-modal-tab-nav .sub-tab-btn {
    flex: 0 0 auto !important;
    white-space: nowrap !important;
    padding: 0.45rem 0.85rem !important;
    min-height: 38px !important;
    border-radius: 8px !important;
    font-size: 0.78rem !important;
  }
}

@media (max-width: 420px) {
  .cycle-switcher-wrapper {
    grid-template-columns: 1fr !important;
    border-radius: 12px !important;
  }
}

/* ==========================================================================
   4. UNIVERSAL MOBILE BOTTOM-SHEET MODAL ARCHITECTURE
   ========================================================================== */
@media (max-width: 768px) {
  /* All modal backdrops align bottom-sheet */
  .modal-backdrop,
  .modal-backdrop.open,
  .modal-backdrop.active,
  .modal-backdrop[style*="display: flex"],
  .modal.active {
    display: none;
    align-items: flex-end !important;
    justify-content: center !important;
    padding: 0 !important;
    margin: 0 !important;
    background: rgba(0, 0, 0, 0.55) !important;
    -webkit-backdrop-filter: blur(8px) !important;
    backdrop-filter: blur(8px) !important;
  }

  .modal-backdrop.open,
  .modal-backdrop.active,
  .modal-backdrop[style*="display: flex"],
  .modal.active {
    display: flex !important;
  }

  /* Universal Bottom-Sheet Dialog */
  .modal-content,
  .dlg,
  .nextgen-modal-dialog,
  .apple-modal-window,
  .modal-dialog,
  .modal-box {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 92dvh !important;
    max-height: 92vh !important;
    margin: 0 !important;
    border-bottom-left-radius: 0 !important;
    border-bottom-right-radius: 0 !important;
    border-top-left-radius: 20px !important;
    border-top-right-radius: 20px !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
    box-sizing: border-box !important;
    box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.28) !important;
    padding: 0 !important;
    transform: none !important;
  }

  /* Universal Form inside Dialog */
  .modal-content > form,
  .dlg > form,
  .nextgen-modal-dialog > form,
  .apple-modal-window > form,
  .apple-modal-form-flex {
    display: flex !important;
    flex-direction: column !important;
    height: 100% !important;
    min-height: 0 !important;
    flex: 1 1 auto !important;
    overflow: hidden !important;
    margin: 0 !important;
  }

  /* Rigid Zone 1: Sticky Modal Header */
  .dlg-h,
  .modal-header,
  .apple-modal-header {
    flex-shrink: 0 !important;
    padding: 14px 16px !important;
    border-bottom: 1px solid var(--border, #e2e8f0) !important;
    display: flex !important;
    justify-content: space-between !important;
    align-items: center !important;
    background: inherit !important;
  }

  /* Rigid Zone 2: Independent Scrollable Body */
  .dlg-b,
  .modal-body,
  .apple-modal-scrollable-body,
  .sub-tab-view {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    overscroll-behavior-y: contain !important;
    padding: 14px 16px !important;
    box-sizing: border-box !important;
  }

  /* Rigid Zone 3: Pinned Sticky Modal Footer */
  .dlg-f,
  .modal-footer,
  .apple-modal-footer {
    flex-shrink: 0 !important;
    padding: 10px 16px max(14px, env(safe-area-inset-bottom, 16px)) 16px !important;
    border-top: 1px solid var(--border, #e2e8f0) !important;
    display: flex !important;
    gap: 8px !important;
    width: 100% !important;
    box-sizing: border-box !important;
    background: var(--surface-2, #f8fafc) !important;
    z-index: 10 !important;
  }

  .dlg-f button,
  .modal-footer button,
  .apple-modal-footer button {
    flex: 1 1 auto !important;
    min-height: 44px !important;
    font-size: 0.88rem !important;
    font-weight: 700 !important;
    justify-content: center !important;
    display: inline-flex !important;
    align-items: center !important;
  }
}

/* ==========================================================================
   5. MODAL FORM INLINE GRIDS FALLBACK (SINGLE COLUMN ON MOBILE)
   ========================================================================== */
@media (max-width: 768px) {
  /* Override inline 2-col and 3-col grids inside modals */
  .modal-content [style*="grid-template-columns: 1fr 1fr"],
  .modal-content [style*="grid-template-columns: 2fr 1fr"],
  .modal-content [style*="grid-template-columns: 1fr 2fr"],
  .modal-content [style*="grid-template-columns: 1fr 1fr 1fr"],
  .modal-content [style*="grid-template-columns: repeat(3, 1fr)"],
  .modal-content [style*="grid-template-columns: 1fr auto"],
  .dlg [style*="grid-template-columns: 1fr 1fr"],
  .dlg [style*="grid-template-columns: 2fr 1fr"],
  .dlg [style*="grid-template-columns: 1fr 1fr 1fr"],
  .apple-modal-window [style*="grid-template-columns"],
  .form-grid-2col,
  .form-grid-3col,
  .dual-upload-grid,
  .exam-zalo-modes-grid,
  .worksheet-format-selector {
    grid-template-columns: 1fr !important;
    gap: 0.65rem !important;
  }

  /* Radio Cards in Exam Creation */
  .worksheet-format-option {
    width: 100% !important;
    box-sizing: border-box !important;
    padding: 0.75rem 0.9rem !important;
  }

  /* Specific: Class QR Modal ergonomics */
  #modal-class-qr #qr-canvas canvas,
  #modal-class-qr #qr-canvas img {
    max-width: 160px !important;
    max-height: 160px !important;
    width: auto !important;
    height: auto !important;
  }

  #modal-class-qr .dlg-b {
    padding: 12px 14px !important;
    gap: 8px !important;
  }

  #modal-class-qr #qr-direct-link {
    font-size: 11px !important;
    padding: 6px 10px !important;
  }
}

/* ==========================================================================
   6. EXAM DETAIL ACTION RIBBON (NO TRUNCATED OR SQUISHED BUTTONS)
   ========================================================================== */
@media (max-width: 768px) {
  .assessment-actions-group {
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 !important;
    padding: 4px 0 !important;
    overflow: hidden !important;
  }

  .assessment-actions-group .actions {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: wrap !important;
    overflow: visible !important;
    gap: 6px !important;
    width: 100% !important;
    padding: 2px 0 4px 0 !important;
  }

  .assessment-actions-group .actions::-webkit-scrollbar {
    display: none !important;
  }

  .assessment-actions-group .actions .btn {
    flex: 0 0 auto !important;
    width: auto !important;
    min-height: 38px !important;
    padding: 0.4rem 0.85rem !important;
    border-radius: 999px !important;
    white-space: nowrap !important;
    font-size: 0.78rem !important;
    display: inline-flex !important;
    align-items: center !important;
    gap: 5px !important;
  }

  @media (max-width: 600px) {
    .assessment-actions-group .actions .btn-primary {
      flex: 1 1 calc(50% - 6px) !important;
      min-width: 130px !important;
      justify-content: center !important;
    }
    .assessment-actions-group .actions .btn-secondary,
    .assessment-actions-group .actions .btn-outline-indigo {
      flex: 0 0 auto !important;
    }
  }

  /* Force action button labels to stay visible */
  .assessment-actions-group .actions .btn .t {
    display: inline !important;
  }

  .assessment-actions-group .actions .btn-secondary {
    width: auto !important;
  }
}

/* ==========================================================================
   7. STUDENT PORTAL MOBILE OPTIMIZATION (/submit)
   ========================================================================== */
@media (max-width: 768px) {
  .portal-wrap {
    display: flex !important;
    flex-direction: column !important;
    padding: 8px !important;
    gap: 12px !important;
  }

  /* In mobile portal mode, place upload flow first so student can take photo right away */
  .portal-wrap .flow-right-col {
    order: 1 !important;
  }

  .portal-wrap .p-left {
    order: 2 !important;
    margin-top: 0.5rem !important;
  }

  .portal-wrap .p-left .facts {
    grid-template-columns: repeat(3, 1fr) !important;
    gap: 6px !important;
  }

  .portal-wrap .p-left .facts > div {
    padding: 6px 8px !important;
    font-size: 11px !important;
  }

  .portal-wrap .p-left .facts > div b {
    font-size: 12px !important;
  }

  /* Full-width action buttons in student upload card */
  .portal-action-btn-group {
    display: flex !important;
    flex-direction: column !important;
    gap: 8px !important;
    width: 100% !important;
  }

  .portal-action-btn-group button {
    width: 100% !important;
    min-height: 46px !important;
    font-size: 0.95rem !important;
    justify-content: center !important;
  }

  /* Post-submission receipt action buttons stacked cleanly */
  #portal-success-card div[style*="justify-content: center"] {
    display: flex !important;
    flex-direction: column !important;
    gap: 8px !important;
    width: 100% !important;
  }

  #portal-success-card button {
    width: 100% !important;
    min-height: 44px !important;
    justify-content: center !important;
  }
}

/* ==========================================================================
   8. MATH OCR MOBILE VIEW (/tools/math-ocr)
   ========================================================================== */
.ocr-grade-action-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.75rem;
  align-items: center;
}

@media (max-width: 768px) {
  .ocr-grade-action-row,
  #math-ocr-view .ocr-stage-card [style*="grid-template-columns"],
  .math-ocr-view [style*="grid-template-columns"],
  .ocr-stage-card [style*="grid-template-columns"] {
    display: flex !important;
    flex-direction: column !important;
    gap: 0.65rem !important;
    width: 100% !important;
  }

  #ocr-input-student-name {
    width: 100% !important;
    font-size: 16px !important;
    box-sizing: border-box !important;
  }

  #btn-ocr-trigger-grade {
    width: 100% !important;
    min-height: 46px !important;
    justify-content: center !important;
  }
}

/* ==========================================================================
   9. RUBRIC STUDIO MODAL FULLSCREEN ERGONOMICS (#modal-rubric-studio)
   ========================================================================== */
@media (max-width: 768px) {
  #modal-rubric-studio.modal-backdrop,
  #modal-rubric-studio.modal-backdrop.active {
    padding: 0 !important;
    margin: 0 !important;
    width: 100vw !important;
    height: 100dvh !important;
    max-height: 100dvh !important;
    align-items: stretch !important;
    justify-content: flex-start !important;
  }

  #modal-rubric-studio .rubric-studio-modal-content {
    padding: max(6px, env(safe-area-inset-top, 6px)) max(8px, env(safe-area-inset-right, 8px)) max(6px, env(safe-area-inset-bottom, 6px)) max(8px, env(safe-area-inset-left, 8px)) !important;
    display: flex !important;
    flex-direction: column !important;
    max-height: 100dvh !important;
    height: 100dvh !important;
    width: 100vw !important;
    max-width: 100vw !important;
    border-radius: 0 !important;
    margin: 0 !important;
    border: none !important;
  }

  .rubric-studio-header {
    flex-shrink: 0 !important;
    padding: 8px 12px !important;
  }

  .rubric-studio-actions {
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    scrollbar-width: none !important;
    gap: 6px !important;
    width: 100% !important;
    padding: 2px 0 4px !important;
  }

  .rubric-studio-actions .btn {
    flex: 0 0 auto !important;
    white-space: nowrap !important;
    font-size: 0.75rem !important;
    padding: 0.35rem 0.65rem !important;
    min-height: 34px !important;
  }
}

/* Studio Grade Pill Select & Batch Global Bar (Zero Clipping & Dark Mode Compatible) */
.studio-batch-global-bar {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
  transition: background-color 0.2s ease, border-color 0.2s ease;
}
.studio-grade-pill-select {
  display: inline-flex;
  align-items: center;
  background-color: #f1f5f9;
  color: #334155;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 600;
  line-height: 1.3;
  padding: 4px 26px 4px 10px;
  min-width: 105px;
  width: auto;
  max-width: 160px;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23475569' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 12px 12px;
  cursor: pointer;
  box-sizing: border-box;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  transition: all 0.15s ease-in-out;
}
.studio-grade-pill-select:hover {
  background-color: #e2e8f0;
  border-color: #94a3b8;
}
.studio-grade-pill-select:focus {
  outline: none;
  border-color: #6366f1;
  box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}
.studio-bundle-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-bottom: 0.5rem;
}
.bundle-header-primary {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex: 1;
  min-width: 220px;
}
@media (max-width: 640px) {
  .studio-bundle-card-header {
    flex-direction: column;
    align-items: stretch;
    gap: 0.4rem;
  }
  .bundle-header-primary {
    width: 100%;
    min-width: 0;
    flex-wrap: wrap;
  }
  .bundle-header-primary .bundle-title-input {
    width: 100% !important;
    flex: 1 1 100% !important;
    order: 1;
  }
  .bundle-header-primary .bundle-index-tag {
    order: 0;
  }
  .bundle-header-primary .studio-grade-pill-select {
    order: 2;
    min-width: 110px;
    flex-shrink: 0;
  }
  .bundle-header-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    order: 3;
  }
}

/* Dark Mode Overrides for Studio Batch Audit Components */
[data-theme="dark"] .studio-batch-global-bar {
  background: #1e293b !important;
  border-color: #334155 !important;
  color: #f1f5f9 !important;
}
[data-theme="dark"] .studio-batch-global-bar label {
  color: #cbd5e1 !important;
}
[data-theme="dark"] .studio-batch-global-bar select {
  background-color: #0f172a !important;
  border-color: #475569 !important;
  color: #f8fafc !important;
}
[data-theme="dark"] .studio-grade-pill-select {
  background-color: #1e293b !important;
  color: #f1f5f9 !important;
  border-color: #475569 !important;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E") !important;
}
[data-theme="dark"] .studio-grade-pill-select:hover {
  background-color: #334155 !important;
  border-color: #64748b !important;
}
[data-theme="dark"] .studio-grade-pill-select:focus {
  border-color: #818cf8 !important;
  box-shadow: 0 0 0 2px rgba(129, 140, 248, 0.3) !important;
}
[data-theme="dark"] .studio-bundle-card {
  background-color: #1e293b !important;
  border-color: #334155 !important;
  color: #f1f5f9 !important;
}
[data-theme="dark"] .studio-bundle-card.active-viewing {
  background-color: #1e1b4b !important;
  border-color: #6366f1 !important;
}
[data-theme="dark"] .studio-bundle-card .bundle-title-input {
  background-color: #0f172a !important;
  border-color: #475569 !important;
  color: #f8fafc !important;
}

/* Mobile Bottom-Sheet Optimization for Zalo Modals */
@media (max-width: 640px) {
  .exam-zalo-modal-container,
  .student-zalo-modal-container {
    width: 100% !important;
    max-width: 100vw !important;
    margin: auto 0 0 0 !important;
    border-radius: 20px 20px 0 0 !important;
    max-height: 90vh !important;
    display: flex !important;
    flex-direction: column !important;
  }

  .exam-zalo-modes-grid {
    grid-template-columns: 1fr !important;
    gap: 0.5rem !important;
  }
}




