/* ==========================================================================
   AUTOGRADING AI - ENTERPRISE MOBILE-FIRST DESIGN SYSTEM (CSS MODULE)
   Optimized for: 320px–430px Smartphones, Foldables, Tablets (768px–1024px),
   and pristine preservation of Desktop workstations (1280px+).
   Standards: Apple HIG, Google Material Design 3, WCAG 2.1 AAA Touch Targets
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. Root Safe-Area & Touch Variables
   -------------------------------------------------------------------------- */
:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --touch-min-height: 44px;
  --touch-min-width: 44px;
  --mobile-dock-height: calc(60px + env(safe-area-inset-bottom, 0px));
}

/* --------------------------------------------------------------------------
   2. Touch Target & iOS Safari Auto-Zoom Fix
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
  /* Prevent iOS Safari from auto-zooming on focus when font-size < 16px */
  input,
  select,
  textarea {
    font-size: 16px !important;
  }

  /* Eliminate 300ms tap delay on mobile touch */
  button,
  .btn,
  .dock-item,
  .segmented-btn,
  .btn-qr-chip,
  .math-key-btn,
  .mobile-sg-tab-btn,
  .mobile-action-sheet-item {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }

  /* Minimum touch target enforcement */
  .btn-sm,
  .btn-xs {
    min-height: 38px;
    padding-left: 0.65rem;
    padding-right: 0.65rem;
  }

  .btn:not(.btn-sm):not(.btn-xs) {
    min-height: var(--touch-min-height);
  }
}

/* --------------------------------------------------------------------------
   3. Sidebar Backdrop Overlay (Click outside to close drawer on mobile)
   -------------------------------------------------------------------------- */
.sidebar-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 140;
  display: none;
  opacity: 0;
  transition: opacity 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

.sidebar-backdrop.active {
  display: block;
  opacity: 1;
}

/* --------------------------------------------------------------------------
   4. Mobile Topbar & Action Sheet Menu
   -------------------------------------------------------------------------- */
.btn-mobile-more-actions {
  display: none;
}

@media (max-width: 768px) {
  .studio-topbar {
    padding-left: max(0.65rem, var(--safe-left)) !important;
    padding-right: max(0.65rem, var(--safe-right)) !important;
    height: 56px !important;
  }

  /* Hide long action text buttons on mobile */
  .topbar-right .teacher-action {
    display: none !important;
  }

  /* Show dedicated More Actions (⋮) button on mobile */
  .btn-mobile-more-actions {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    min-width: 40px;
    min-height: 40px;
    border-radius: var(--radius-md, 8px);
    background: #f1f5f9;
    border: 1px solid var(--border-color, #e2e8f0);
    color: var(--text-main, #0f172a);
    font-size: 1.25rem;
    cursor: pointer;
    transition: background 0.15s ease;
  }

  .btn-mobile-more-actions:active {
    background: #e2e8f0;
  }
}

/* Mobile Action Sheet Drawer */
.mobile-action-sheet-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(15, 23, 42, 0.5);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 10080;
  display: none;
  opacity: 0;
  transition: opacity 0.25s ease;
}

.mobile-action-sheet-backdrop.active {
  display: flex;
  align-items: flex-end;
  opacity: 1;
}

.mobile-action-sheet-card {
  width: 100%;
  background: #ffffff;
  border-radius: 20px 20px 0 0;
  padding: 1.25rem 1rem calc(1.5rem + var(--safe-bottom)) 1rem;
  box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.18);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-height: 82vh;
  overflow-y: auto;
  animation: mobileSheetSlideUp 0.28s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.mobile-sheet-drag-pill {
  width: 40px;
  height: 4px;
  background: #cbd5e1;
  border-radius: 999px;
  margin: -0.25rem auto 0.75rem auto;
}

.mobile-action-sheet-title {
  font-size: 1.05rem;
  font-weight: 800;
  color: #1e1b4b;
  margin-bottom: 0.25rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.mobile-action-sheet-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.85rem 1rem;
  min-height: 52px;
  border-radius: var(--radius-md, 10px);
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--text-main, #1e293b);
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  cursor: pointer;
  transition: all 0.15s ease;
  text-align: left;
  width: 100%;
}

.mobile-action-sheet-item:active {
  background: #eff6ff;
  border-color: #bfdbfe;
  transform: scale(0.99);
}

.mobile-action-sheet-item .sheet-icon {
  font-size: 1.25rem;
  line-height: 1;
}

@keyframes mobileSheetSlideUp {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

/* --------------------------------------------------------------------------
   5. Mobile Bottom Dock & FAB Safe-Area Inset
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
  .mobile-bottom-dock {
    height: var(--mobile-dock-height) !important;
    padding-bottom: var(--safe-bottom) !important;
    display: flex !important;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
  }

  .workspace-container {
    padding-bottom: calc(var(--mobile-dock-height) + 1.25rem) !important;
  }

  .mobile-fab {
    bottom: calc(var(--mobile-dock-height) + 14px) !important;
    right: max(16px, var(--safe-right)) !important;
    width: 52px !important;
    height: 52px !important;
    box-shadow: 0 6px 18px rgba(37, 99, 235, 0.35) !important;
  }
}

/* --------------------------------------------------------------------------
   6. Submissions Adaptive Cards (Replaces 7-Column Table on Mobile)
   -------------------------------------------------------------------------- */
.mobile-submissions-card-view {
  display: none;
}

@media (max-width: 768px) {
  /* Toggle visibility between desktop table and mobile cards */
  .submissions-table-container,
  #table-submissions {
    display: none !important;
  }

  .mobile-submissions-card-view {
    display: flex !important;
    flex-direction: column;
    gap: 0.65rem;
    padding-bottom: calc(var(--mobile-dock-height, 64px) + 3rem) !important;
  }

  .mobile-submission-card {
    background: #ffffff;
    border: 1.5px solid #e2e8f0;
    border-radius: var(--radius-lg, 12px);
    padding: 0.9rem 1rem;
    box-shadow: var(--shadow-sm, 0 1px 3px rgba(0, 0, 0, 0.04));
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    transition: transform 0.15s ease, border-color 0.15s ease;
  }

  .mobile-submission-card:active {
    border-color: var(--primary, #2563eb);
    transform: scale(0.995);
  }

  .mobile-sub-card-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .mobile-sub-student-name {
    font-size: 0.95rem;
    font-weight: 800;
    color: #0f172a;
    display: flex;
    align-items: center;
    gap: 0.4rem;
  }

  .mobile-sub-card-middle {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 0.8rem;
    color: #64748b;
    border-top: 1px dashed #f1f5f9;
    padding-top: 0.4rem;
  }

  .mobile-sub-card-actions {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.25rem;
  }

  .mobile-sub-card-actions button {
    flex: 1;
    min-height: 42px;
    font-weight: 700;
    font-size: 0.85rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    border-radius: var(--radius-md, 8px);
  }
}

/* --------------------------------------------------------------------------
   7. Teacher Usage History Dual-Mode Responsive Views
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
  .desktop-usage-view {
    display: none !important;
  }
  .mobile-usage-view {
    display: flex !important;
  }
  .usage-stats-grid {
    grid-template-columns: 1fr !important;
  }
  .usage-toolbar {
    padding: 0.5rem !important;
  }
}

/* --------------------------------------------------------------------------
   8. Desktop Preservation Guard (≥ 769px)
   Guarantees that mobile overlays, bottom docks, and mobile tabs never leak onto desktop
   -------------------------------------------------------------------------- */
@media (min-width: 769px) {
  #mobile-action-sheet-backdrop,
  #mobile-submissions-card-view,
  #mobile-sg-tab-bar,
  #mobile-sg-sticky-bar,
  .mobile-bottom-dock,
  .btn-mobile-more-actions,
  #sidebar-backdrop {
    display: none !important;
  }
  .speedgrader-studio-layout {
    display: grid !important;
    grid-template-columns: 200px 1fr 240px !important;
  }
  .desktop-usage-view {
    display: block !important;
  }
  .mobile-usage-view {
    display: none !important;
  }
}

@media (min-width: 1025px) {
  .speedgrader-studio-layout {
    grid-template-columns: 280px 1fr 340px !important;
  }
}

/* --------------------------------------------------------------------------
   9. Mobile Topbar & Profile Capsule Optimization (≤ 768px & ≤ 540px)
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
  #assessment-studio-view .studio-split-grid {
    grid-template-columns: 1fr !important;
  }
  .user-profile-trigger .user-info-text,
  .user-profile-trigger .badge-role {
    display: none !important;
  }
  .user-profile-trigger {
    padding: 0.2rem 0.35rem !important;
    gap: 0.2rem !important;
  }
}

@media (max-width: 540px) {
  .user-profile-trigger .user-info-text,
  .user-profile-trigger .badge-role {
    display: none !important;
  }
  .user-profile-trigger {
    padding: 0.2rem 0.35rem !important;
    gap: 0.2rem !important;
  }
  .btn-quota-pill {
    padding: 0.2rem 0.5rem !important;
    font-size: 0.76rem !important;
  }
  .btn-quota-pill .quota-text {
    max-width: 80px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .topbar-right {
    gap: 0.35rem !important;
  }
}

@media (max-width: 360px) {
  .context-hero-quick-actions {
    grid-template-columns: 1fr 1fr !important;
  }
}

/* --------------------------------------------------------------------------
   10. Exam Hub Mobile Streamlining: Bento 2x2 & Horizontal Filter Pills
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
  .classroom-context-hero {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 0.65rem !important;
    padding: 0.75rem 0.85rem !important;
  }
  .context-hero-quick-actions {
    display: grid !important;
    grid-template-columns: 1fr 1fr 1fr !important;
    gap: 0.35rem !important;
    width: 100% !important;
  }
  .btn-hero-action {
    justify-content: center !important;
    padding: 0.4rem 0.25rem !important;
    font-size: 0.74rem !important;
  }
  .copilot-intent-chips {
    display: flex !important;
    overflow-x: auto !important;
    white-space: nowrap !important;
    flex-wrap: nowrap !important;
    scrollbar-width: none !important;
    -webkit-overflow-scrolling: touch !important;
    gap: 0.35rem !important;
    padding-bottom: 2px !important;
  }
  .copilot-intent-chips .intent-chip {
    flex-shrink: 0 !important;
    padding: 0.3rem 0.65rem !important;
    font-size: 0.75rem !important;
    min-height: 36px !important;
  }
  .bento-hud-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 0.5rem !important;
    margin-bottom: 0.85rem !important;
  }
  .bento-card {
    padding: 0.65rem 0.75rem !important;
  }
  .bento-header {
    font-size: 0.72rem !important;
    margin-bottom: 0.2rem !important;
  }
  .squircle-icon-box {
    width: 26px !important;
    height: 26px !important;
  }
  .squircle-icon-box svg {
    width: 13px !important;
    height: 13px !important;
  }
  .bento-val {
    font-size: 1.3rem !important;
    line-height: 1.15 !important;
  }
  .bento-subtext {
    font-size: 0.68rem !important;
    line-height: 1.2 !important;
    margin-top: 0.2rem !important;
  }
  .exam-hub-header {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 0.65rem !important;
  }
  .exam-hub-header-actions {
    display: flex !important;
    align-items: center !important;
    justify-content: space-between !important;
    width: 100% !important;
    margin-left: 0 !important;
    gap: 0.5rem !important;
  }
  .exam-hub-view-modes {
    flex: 1 !important;
  }
  .exam-hub-filter-ribbon {
    display: flex !important;
    overflow-x: auto !important;
    white-space: nowrap !important;
    flex-wrap: nowrap !important;
    scrollbar-width: none !important;
    -webkit-overflow-scrolling: touch !important;
    gap: 0.4rem !important;
    padding-bottom: 4px !important;
  }
  .filter-pill-group {
    display: flex !important;
    flex-shrink: 0 !important;
    gap: 0.35rem !important;
  }
  .hub-filter-pill {
    flex-shrink: 0 !important;
    white-space: nowrap !important;
    padding: 0.35rem 0.75rem !important;
    font-size: 0.78rem !important;
    min-height: 36px !important;
  }
  .filter-pill-divider {
    flex-shrink: 0 !important;
  }
}

/* --------------------------------------------------------------------------
   11. Exam Detail Mobile Action Ribbon & Variant Selector
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
  .exam-detail-nav-bar {
    padding: 0.45rem 0.65rem !important;
    gap: 0.4rem !important;
    margin-bottom: 0.65rem !important;
  }
  .btn-back-to-exams {
    padding: 0.35rem 0.6rem !important;
    font-size: 0.78rem !important;
  }
  .exam-hero-bar {
    padding: 0.75rem 0.85rem !important;
  }
  .exam-hero-action-buttons {
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    scrollbar-width: none !important;
    -webkit-overflow-scrolling: touch !important;
    gap: 0.4rem !important;
    padding-bottom: 4px !important;
    width: 100% !important;
  }
  .exam-hero-action-buttons button,
  .exam-hero-action-buttons .btn {
    flex-shrink: 0 !important;
    white-space: nowrap !important;
    min-height: 38px !important;
    padding: 0.35rem 0.75rem !important;
    font-size: 0.78rem !important;
  }
  .exam-hero-action-buttons #btn-submit-exam-work {
    order: -1 !important;
    min-height: 38px !important;
    padding: 0.35rem 0.9rem !important;
    font-weight: 700 !important;
    background: var(--primary, #2563eb) !important;
    color: #ffffff !important;
  }
  .studio-view-header {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 0.5rem !important;
    padding: 0.5rem !important;
  }
  .studio-mode-pills {
    width: 100% !important;
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
  }
  .btn-mode-toggle {
    justify-content: center !important;
    padding: 0.45rem 0.5rem !important;
    font-size: 0.8rem !important;
  }
  .variant-tab-group {
    overflow-x: visible !important;
    flex-wrap: wrap !important;
    gap: 0.35rem !important;
  }
  .variant-tab-group .v-tab {
    flex-shrink: 0 !important;
    white-space: nowrap !important;
    padding: 0.35rem 0.65rem !important;
    min-height: 34px !important;
  }
  .variant-hub-card {
    padding: 0.5rem 0.6rem !important;
  }
  .variant-hub-header {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 0.5rem !important;
  }
  .variant-hub-actions {
    width: 100% !important;
    justify-content: flex-start !important;
  }
  .variant-hub-actions .btn {
    min-height: 36px !important;
    font-size: 0.75rem !important;
  }
  .variant-hub-grid {
    flex-wrap: wrap !important;
    overflow-x: visible !important;
  }
  .variant-hub-grid .variant-chip {
    min-height: 36px !important;
  }
}

/* --------------------------------------------------------------------------
   12. Student Portal Zero-FOUC & Teacher Chrome Suppression
   -------------------------------------------------------------------------- */
body.portal-mode .mobile-bottom-dock,
body.portal-mode .mobile-fab,
html.portal-mode .mobile-bottom-dock,
html.portal-mode .mobile-fab,
body.unauthenticated .mobile-bottom-dock,
body.unauthenticated .mobile-fab,
.modal-backdrop.active ~ .mobile-fab,
body:has(.modal-backdrop.active) .mobile-fab {
  display: none !important;
}

@media (max-width: 480px) {
  .portal-manual-entry-row {
    flex-direction: column !important;
    gap: 0.5rem !important;
  }
  .portal-manual-entry-row button {
    width: 100% !important;
    min-height: 44px !important;
  }
}

/* --------------------------------------------------------------------------
   13. Auth Gate Mobile Optimization (≤ 768px)
   Eliminates redundant logo, title, and slogan on mobile since topbar already provides brand identity
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
  .auth-gate-brand {
    display: none !important;
  }
  .auth-gate-hero {
    padding-top: 0 !important;
    gap: 0.5rem !important;
  }
  .auth-gate-container {
    padding-top: 0.25rem !important;
    gap: 0.85rem !important;
  }
}

/* --------------------------------------------------------------------------
   14. Native Edge-to-Edge Mobile Camera Scanner & Grading Screen (≤ 768px)
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
  #modal-camera-scanner {
    position: fixed !important;
    inset: 0 !important;
    width: 100vw !important;
    height: 100dvh !important;
    height: 100vh !important;
    max-width: 100vw !important;
    max-height: 100dvh !important;
    padding: 0 !important;
    margin: 0 !important;
    background: #000000 !important;
    z-index: 99999 !important;
    align-items: stretch !important;
    justify-content: stretch !important;
  }

  #modal-camera-scanner:not(.active) {
    display: none !important;
  }

  #modal-camera-scanner.active {
    display: flex !important;
    flex-direction: column !important;
  }

  #modal-camera-scanner .camera-modal-container {
    position: fixed !important;
    inset: 0 !important;
    width: 100vw !important;
    max-width: 100vw !important;
    min-width: 100vw !important;
    height: 100dvh !important;
    height: 100vh !important;
    max-height: 100dvh !important;
    max-height: 100vh !important;
    margin: 0 !important;
    border-radius: 0 !important;
    border: none !important;
    background: #000000 !important;
    padding: max(0.5rem, env(safe-area-inset-top, 10px)) 0.75rem max(1.2rem, env(safe-area-inset-bottom, 24px)) 0.75rem !important;
    display: flex !important;
    flex-direction: column !important;
    justify-content: space-between !important;
    box-sizing: border-box !important;
    overflow: hidden !important;
  }

  #modal-camera-scanner .camera-viewfinder-box {
    flex: 1 1 auto !important;
    height: auto !important;
    min-height: 0 !important;
    border-radius: var(--radius-md, 8px) !important;
  }

  #modal-camera-scanner .camera-viewfinder-box video#camera-video-stream {
    width: 100% !important;
    height: 100% !important;
    max-height: 100% !important;
    object-fit: cover !important;
  }

  #modal-camera-scanner .camera-bottom-bar {
    padding-bottom: 0.25rem !important;
    margin-top: 0.5rem !important;
    gap: 0.5rem !important;
  }

  #modal-camera-scanner .camera-shutter-trigger {
    width: 66px !important;
    height: 66px !important;
  }

  #modal-camera-scanner .camera-shutter-trigger .shutter-inner-circle {
    width: 50px !important;
    height: 50px !important;
  }

  #modal-camera-scanner .camera-finish-btn {
    padding: 0.55rem 0.85rem !important;
    font-size: 0.82rem !important;
    white-space: nowrap !important;
  }

  #modal-camera-scanner .camera-status-indicator {
    font-size: 0.76rem !important;
    padding: 0.35rem 0.55rem !important;
    white-space: nowrap !important;
  }

  /* Submission Viewer & SpeedGrader Mobile Optimization */
  .submission-viewer-modal-content { padding: 0.5rem !important; height: 100vh !important; max-height: 100vh !important; border-radius: 0 !important; }
  .mvs-toolbar { padding: 0.4rem !important; gap: 0.35rem !important; overflow-x: auto !important; white-space: nowrap !important; }
  .mvs-toolbar .btn { padding: 0.25rem 0.45rem !important; font-size: 0.75rem !important; }
  .btn-sg-back-pill { display: inline-flex; align-items: center; gap: 4px; padding: 0 8px; height: 34px; border-radius: 8px; border: 1px solid var(--border-strong, #cbd5e1); font-size: 13px; font-weight: 600; color: var(--text); flex-shrink: 0; }
  .btn-sg-back-pill:hover { background: var(--surface-3, #f1f5f9); }
  @media (max-width: 480px) {
    .btn-sg-back-lbl { display: none !important; }
    .btn-sg-back-pill { width: 34px !important; padding: 0 !important; justify-content: center !important; }
    #btn-sg-quick-edit-sub span { display: none !important; }
    #btn-sg-quick-edit-sub { width: 32px !important; padding: 0 !important; justify-content: center !important; }
  }
  @media (max-width: 640px) {
    #btn-canvas-edit-student { display: none !important; }
    .p-form { padding-right: 72px !important; }
    .stamp { width: 56px !important; height: 56px !important; top: 8px !important; right: 8px !important; }
    .stamp b { font-size: 16px !important; }
    .stamp small { font-size: 10px !important; }
  }
}

