/* ------------------------------------------------------------------ */
/* ShreniUP - Universal Mobile & Tablet Responsive Stylesheet         */
/* Cross-Platform Compatibility: iOS Safari, Android, Samsung, iPad    */
/* ------------------------------------------------------------------ */

/* 1. Root & Safe-Area Inset Support */
: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);
  --mobile-bottom-nav-height: 60px;
}

/* Prevent horizontal page blowout across entire viewport without breaking position: sticky */
html, body {
  overflow-x: clip !important;
  width: 100% !important;
  max-width: 100vw !important;
  box-sizing: border-box !important;
}

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

  body {
    padding-bottom: calc(var(--mobile-bottom-nav-height) + var(--safe-bottom) + 8px);
    -webkit-tap-highlight-color: transparent;
    width: 100% !important;
    max-width: 100vw !important;
    overflow-x: clip !important;
  }

  /* When arena is active, remove body padding so test is edge-to-edge */
  body.arena-active {
    padding-bottom: 0 !important;
  }

  /* Math & KaTeX equations touch scroll container */
  .katex-display, .katex, .math-scroll-wrapper {
    max-width: 100% !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    -webkit-overflow-scrolling: touch !important;
    padding: 4px 2px;
  }

  /* Responsive Data Tables */
  .table-responsive {
    display: block;
    width: 100% !important;
    max-width: 100% !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    margin-bottom: 0.75rem;
  }

  .card {
    max-width: 100% !important;
    box-sizing: border-box;
    overflow-x: auto;
  }

  table {
    display: table !important;
    width: 100% !important;
    min-width: 520px;
    border-collapse: collapse;
  }

  /* Ensure touch targets meet minimum 44x44px */
  button, .btn, .nav-tab-btn, .filter-chip, .option-item {
    min-height: 40px;
  }
}

/* 3. Responsive Header & Top Bar (Phones <= 768px) */
@media (max-width: 768px) {
  header {
    padding: 0.45rem 0.6rem;
    padding-top: max(0.45rem, var(--safe-top));
    gap: 0.35rem;
    flex-wrap: nowrap;
    width: 100% !important;
    max-width: 100vw !important;
    box-sizing: border-box;
    overflow: visible !important;
  }

  .brand-container {
    gap: 0.35rem;
    flex-shrink: 0;
  }

  .brand-emblem-badge {
    width: 32px !important;
    height: 32px !important;
    padding: 0 3px !important;
    flex-direction: row !important;
    align-items: baseline !important;
    justify-content: center !important;
    gap: 1px !important;
  }

  .brand-emblem-badge .emblem-shreni {
    font-size: 8.5px !important;
    line-height: 32px !important;
  }

  .brand-emblem-badge .emblem-up {
    font-size: 7px !important;
    line-height: 32px !important;
  }

  .brand-container img {
    width: 32px !important;
    height: 32px !important;
  }

  .logo-badge {
    font-size: 1.05rem;
    padding: 0.2rem 0.4rem;
  }

  .brand-title,
  .brand-text h1 {
    font-size: 1.05rem !important;
    font-weight: 800 !important;
    line-height: 1.1 !important;
    display: inline-flex !important;
    white-space: nowrap !important;
    margin: 0 !important;
  }

  .brand-up-accent {
    display: inline !important;
    font-weight: 900 !important;
  }

  .brand-subtitle,
  .brand-text > span {
    display: none !important; /* Hide long subtitle on phones to preserve horizontal space */
  }

  .header-actions {
    gap: 0.3rem;
    max-width: calc(100% - 40px);
    flex-shrink: 1;
    overflow: visible !important;
  }

  .exam-selector-group {
    height: 32px;
    padding: 0 0.35rem;
    gap: 0.2rem;
    max-width: 125px;
    flex-shrink: 1;
    min-width: 0;
  }

  .exam-selector-label {
    display: none !important; /* Hide label text on mobile, icon + dropdown are self-evident */
  }

  .select-exam {
    font-size: 0.75rem !important;
    max-width: 95px;
    text-overflow: ellipsis;
  }

  /* Guest Auth buttons on mobile */
  #auth-buttons-group {
    gap: 0.25rem !important;
  }

  #auth-buttons-group .btn {
    padding: 0.3rem 0.5rem;
    font-size: 0.74rem;
  }

  /* Hide redundant upgrade & refund action buttons on mobile header; bottom nav / profile drawer has them */
  #nav-upgrade-btn,
  #header-upgrade-btn,
  #header-refund-btn {
    display: none !important;
  }

  /* User Profile Pill on mobile - compact avatar + name */
  .user-profile-pill {
    height: 32px;
    padding: 0 0.35rem 0 0.2rem;
    gap: 0.25rem;
    max-width: 110px;
    flex-shrink: 1;
    min-width: 0;
  }

  .user-avatar-initials {
    width: 22px !important;
    height: 22px !important;
    font-size: 0.65rem !important;
  }

  .user-display-name {
    max-width: 55px;
    font-size: 0.74rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Hide secondary badges specifically from the compact header pill on mobile */
  .user-profile-pill .badge-plan,
  .user-profile-pill .badge-verified-mini,
  .user-profile-pill .capsule-divider,
  .user-profile-pill .user-validity-text {
    display: none !important;
  }

  .btn-theme-toggle {
    width: 32px;
    height: 32px;
    padding: 4px;
    flex-shrink: 0;
  }

  .user-profile-dropdown {
    width: min(320px, calc(100vw - 1.25rem));
    max-width: calc(100vw - 1rem);
    right: -36px;
    z-index: 1000;
  }

  .user-profile-dropdown.open {
    display: flex !important;
  }

  .user-profile-dropdown .badge-verified-mini,
  .user-profile-dropdown .user-validity-text {
    display: inline-flex !important;
  }

  /* Desktop Nav Sidebar: Hide on phones when bottom nav is active */
  .app-layout-container {
    display: block !important;
  }

  .nav-sidebar,
  .nav-tabs-bar {
    display: none !important;
  }

  main {
    width: 100% !important;
    max-width: 100vw !important;
    margin: 0.75rem auto;
    padding: 0 0.75rem;
    box-sizing: border-box;
    overflow-x: hidden;
  }

  .tab-content {
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box;
  }
}

/* Tablets (769px to 1024px) */
@media (min-width: 769px) and (max-width: 1024px) {
  header {
    padding: 0.5rem 1rem;
  }
  .exam-selector-group {
    max-width: 220px;
  }
  .nav-sidebar {
    width: 200px;
    min-width: 200px;
    padding: 0.85rem 0.4rem;
  }
  .nav-tab-btn {
    padding: 0.6rem 0.5rem;
    font-size: 0.8rem;
    gap: 0.45rem;
  }
}

/* 4. Sticky Bottom Navigation Bar (Modern EdTech Mobile Standard) */
.mobile-bottom-nav {
  display: none; /* Hidden on desktop */
}

@media (max-width: 768px) {
  .mobile-bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: calc(var(--mobile-bottom-nav-height) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    background: var(--bg-header, #ffffff);
    border-top: 1px solid var(--border-color, #e2e8f0);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    z-index: 100;
    align-items: center;
    justify-content: space-around;
    box-shadow: 0 -3px 12px rgba(0, 0, 0, 0.06);
    transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.2s ease;
  }

  /* Automatically hide bottom nav when active mock test is running or modal is open */
  body.arena-active .mobile-bottom-nav,
  body.modal-open .mobile-bottom-nav,
  .mobile-bottom-nav.nav-hidden {
    transform: translateY(100%);
    opacity: 0;
    pointer-events: none;
  }

  .mobile-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    height: 100%;
    color: var(--text-muted, #64748b);
    text-decoration: none;
    background: none;
    border: none;
    padding: 4px 0;
    cursor: pointer;
    transition: color 0.15s ease, transform 0.1s ease;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-nav-item svg {
    stroke: var(--text-muted, #64748b);
    transition: stroke 0.15s ease, transform 0.15s ease;
  }

  .mobile-nav-label {
    font-size: 0.68rem;
    font-weight: 600;
    margin-top: 3px;
    letter-spacing: -0.2px;
  }

  .mobile-nav-item:active {
    transform: scale(0.92);
  }

  .mobile-nav-item.active {
    color: var(--brand-blue, #2563eb) !important;
  }

  .mobile-nav-item.active svg {
    stroke: var(--brand-blue, #2563eb) !important;
    transform: translateY(-1px);
  }
}

/* 5. Test Series Library Toolbar & Filter Controls on Mobile */
@media (max-width: 768px) {
  .filter-toolbar {
    margin-bottom: 1rem;
  }

  .filter-dropdowns-row {
    display: flex;
    flex-direction: row;
    gap: 0.5rem;
    align-items: center;
    width: 100%;
  }

  .search-input-wrap {
    flex: 1;
    min-width: 0;
  }

  #library-search-input {
    width: 100%;
    height: 42px;
    padding-left: 36px;
    font-size: 0.88rem;
    border-radius: 8px;
  }

  /* Desktop inline filter selects: Hide inline on mobile, accessed via Mobile Filter Button */
  #library-exam-filter,
  #library-type-filter,
  #library-diff-filter {
    display: none !important;
  }

  /* Mobile Filter Drawer Button */
  .btn-mobile-filter-trigger {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    gap: 5px;
    height: 42px;
    padding: 0 0.85rem;
    background: var(--bg-card, #ffffff);
    border: 1px solid var(--border-color, #cbd5e1);
    border-radius: 8px;
    color: var(--text-main, #1e293b);
    font-size: 0.82rem;
    font-weight: 700;
    cursor: pointer;
    flex-shrink: 0;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
  }

  .btn-mobile-filter-trigger.has-filters {
    border-color: var(--brand-blue);
    color: var(--brand-blue);
    background: rgba(37, 99, 235, 0.06);
  }

  /* Quick status filter chips row: Smooth horizontal carousel */
  .filter-chips-row {
    display: flex !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    padding: 0.5rem 0 0.25rem 0 !important;
    gap: 0.45rem !important;
    scrollbar-width: none;
    -ms-overflow-style: none;
  }

  .filter-chips-row::-webkit-scrollbar {
    display: none;
  }

  .filter-chip {
    flex-shrink: 0;
    padding: 0.4rem 0.75rem;
    font-size: 0.78rem;
    border-radius: 9999px;
    white-space: nowrap;
  }

  /* All Tab Card Grids: 1 Column & 100% Width on Mobile */
  #test-series-cards-grid,
  #live-shifts-container,
  .diagnostic-grid,
  .pricing-grid {
    display: grid !important;
    grid-template-columns: 1fr !important;
    gap: 0.85rem !important;
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box;
  }

  .test-card,
  .test-paper-card,
  .plan-card {
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box;
    padding: 1rem !important;
  }

  .filter-toolbar {
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box;
  }

  .filter-chips-row {
    max-width: 100% !important;
    box-sizing: border-box;
  }
}

/* Hide Mobile Filter button on desktop */
.btn-mobile-filter-trigger {
  display: none;
}

/* 6. Mobile Filter Slide-Up Sheet Modal */
.mobile-filter-sheet-backdrop {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(15, 23, 42, 0.55);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 200;
  display: none;
  align-items: flex-end;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.mobile-filter-sheet-backdrop.open {
  display: flex;
  opacity: 1;
}

.mobile-filter-sheet {
  background: var(--bg-card, #ffffff);
  width: 100%;
  max-height: 85vh;
  border-radius: 16px 16px 0 0;
  border-top: 1px solid var(--border-color, #cbd5e1);
  box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.15);
  padding: 1.25rem 1.25rem calc(1.25rem + var(--safe-bottom)) 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  overflow-y: auto;
  transform: translateY(100%);
  transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

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

.sheet-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--border-color, #e2e8f0);
}

.sheet-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text-main);
  display: flex;
  align-items: center;
  gap: 6px;
}

.sheet-close-btn {
  background: none;
  border: none;
  font-size: 1.3rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
}

.sheet-filter-group {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.sheet-filter-label {
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--text-main);
}

.sheet-select {
  width: 100%;
  height: 44px;
  padding: 0 0.85rem;
  border: 1.5px solid var(--border-color, #cbd5e1);
  border-radius: 8px;
  background: var(--bg-card, #ffffff);
  color: var(--text-main);
  font-size: 16px !important; /* iOS zoom fix */
  font-weight: 600;
  outline: none;
}

.sheet-actions-row {
  display: flex;
  gap: 0.75rem;
  margin-top: 0.5rem;
}

.sheet-actions-row .btn {
  flex: 1;
  height: 44px;
  font-weight: 700;
  border-radius: 8px;
}

/* 7. Live Exam Arena Mobile Architecture (Sticky Top/Bottom + Palette Drawer) */
@media (max-width: 860px) {
  #exam-arena-view {
    position: relative;
    padding-bottom: calc(68px + var(--safe-bottom)) !important;
  }

  .simulator-container {
    grid-template-columns: 1fr !important;
    gap: 0 !important;
  }

  /* Top sticky arena bar: Timer, Section pill, Language toggle, Palette trigger */
  .arena-header-sticky-mobile {
    position: sticky;
    top: 0;
    z-index: 45;
    background: var(--bg-card, #ffffff);
    border-bottom: 1.5px solid var(--border-color, #cbd5e1);
    padding: 0.6rem 0.75rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  }

  .arena-mobile-timer {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: #0f172a;
    color: #38bdf8;
    padding: 5px 10px;
    border-radius: 6px;
    font-family: monospace;
    font-weight: 800;
    font-size: 0.95rem;
  }

  .arena-mobile-actions-top {
    display: flex;
    align-items: center;
    gap: 0.4rem;
  }

  .btn-mobile-palette-toggle {
    display: inline-flex !important;
    align-items: center;
    gap: 5px;
    padding: 5px 10px;
    background: rgba(37, 99, 235, 0.08);
    border: 1.5px solid var(--brand-blue);
    border-radius: 6px;
    color: var(--brand-blue);
    font-weight: 700;
    font-size: 0.8rem;
    cursor: pointer;
  }

  /* Question Pane on mobile */
  .question-pane {
    border: none !important;
    border-radius: 0 !important;
    background: transparent !important;
    padding: 0.85rem 0.25rem !important;
    min-height: calc(100vh - 200px);
  }

  .q-header-meta {
    flex-wrap: wrap;
    gap: 0.45rem;
    margin-bottom: 0.85rem;
  }

  .question-body {
    font-size: 1rem;
    line-height: 1.6;
    margin-bottom: 1.25rem;
  }

  .option-item {
    padding: 0.85rem 1rem;
    font-size: 0.95rem;
    border-radius: 8px;
  }

  /* Bottom sticky thumb controls */
  .q-actions-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 60;
    background: var(--bg-card, #ffffff);
    border-top: 1.5px solid var(--border-color, #cbd5e1);
    padding: 0.6rem 0.75rem calc(0.6rem + var(--safe-bottom)) 0.75rem;
    margin: 0 !important;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.4rem;
    box-shadow: 0 -4px 14px rgba(0, 0, 0, 0.08);
  }

  .q-actions-left {
    display: flex;
    gap: 0.35rem;
    align-items: center;
    flex-shrink: 1;
    overflow-x: auto;
  }

  .btn-sim-blue-outline,
  .btn-sim-flag-outline,
  .btn-sim-flag-active {
    padding: 0.5rem 0.65rem !important;
    font-size: 0.76rem !important;
    white-space: nowrap;
  }

  .btn-sim-blue-primary {
    padding: 0.5rem 0.95rem !important;
    font-size: 0.82rem !important;
    flex-shrink: 0;
  }

  /* Desktop inline palette pane: Transform into bottom sheet drawer */
  .palette-pane {
    display: none; /* Managed by mobile sheet drawer */
  }

  /* Mobile Question Palette Bottom Sheet Drawer */
  .mobile-palette-drawer-backdrop {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(15, 23, 42, 0.65);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 250;
    display: none;
    align-items: flex-end;
    opacity: 0;
    transition: opacity 0.2s ease;
  }

  .mobile-palette-drawer-backdrop.open {
    display: flex;
    opacity: 1;
  }

  .mobile-palette-drawer {
    background: var(--bg-card, #ffffff);
    width: 100%;
    max-height: 82vh;
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.2);
    padding: 1rem 1rem calc(1rem + var(--safe-bottom)) 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
    overflow-y: auto;
    transform: translateY(100%);
    transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1);
  }

  .mobile-palette-drawer-backdrop.open .mobile-palette-drawer {
    transform: translateY(0);
  }

  .palette-drawer-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-color, #e2e8f0);
  }

  .palette-grid-mobile {
    display: grid !important;
    grid-template-columns: repeat(5, 1fr) !important;
    gap: 8px !important;
    max-height: 240px !important;
    overflow-y: auto !important;
    padding: 4px;
  }

  .palette-btn {
    height: 40px !important;
    font-size: 0.88rem !important;
    font-weight: 700 !important;
    border-radius: 8px !important;
  }
}

/* Hide mobile palette button on desktop */
.btn-mobile-palette-toggle {
  display: none;
}

/* 8. Responsive Modals & Auth Dialogs */
@media (max-width: 640px) {
  .modal-backdrop {
    align-items: flex-start !important;
    padding: 0.5rem 0.5rem calc(1.5rem + env(safe-area-inset-bottom, 16px)) 0.5rem !important;
    z-index: 1050 !important;
  }

  .modal-dialog {
    width: 96% !important;
    max-width: 520px !important;
    margin: 0.5rem auto !important;
    max-height: calc(100dvh - 1.25rem - env(safe-area-inset-bottom, 16px)) !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
    border-radius: 14px !important;
  }

  .modal-header {
    flex-shrink: 0 !important;
    padding: 0.9rem 1.1rem !important;
  }

  .modal-body {
    flex: 1 1 auto !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    overscroll-behavior: contain !important;
    padding: 1rem !important;
    padding-bottom: calc(2.25rem + env(safe-area-inset-bottom, 16px)) !important;
    min-height: 0 !important;
  }

  .verification-grid-cols {
    display: grid !important;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem !important;
  }

  .pricing-grid {
    grid-template-columns: 1fr !important;
    gap: 1rem !important;
  }

  .plan-card {
    padding: 1.25rem !important;
  }
}

@media (max-width: 380px) {
  .verification-grid-cols {
    grid-template-columns: 1fr !important;
  }
}

/* 9. Admin Suite Responsive Styles (admin.html) */
@media (max-width: 768px) {
  .admin-container {
    padding: 12px !important;
  }

  .admin-card {
    padding: 16px !important;
    margin-bottom: 16px !important;
  }

  .admin-header {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 12px !important;
  }

  .grid-2, .grid-4 {
    grid-template-columns: 1fr !important;
    gap: 12px !important;
  }

  .admin-actions-bar {
    display: flex;
    flex-direction: column;
    width: 100%;
    gap: 8px;
  }

  .admin-actions-bar .btn {
    width: 100%;
    justify-content: center;
  }
}

@media (min-width: 769px) and (max-width: 1024px) {
  .grid-4 {
    grid-template-columns: 1fr 1fr !important;
  }
}
