/* Shared Mobile Cleanup Layer for Admin + Registrar Dashboards */
@media (max-width: 768px) {
  .site-brand-title {
    font-size: clamp(1.05rem, 4.3vw, 1.4rem) !important;
    line-height: 1.1 !important;
  }

  .dashboard-main {
    background: #f6faf7 !important;
  }

  .dashboard-bg-card::before {
    display: none;
  }

  .dashboard-bg-card,
  #applicants-view > div,
  #students-view > div,
  #records-view > div,
  #reports-view > div,
  #sections-view > div,
  #archive-view > .space-y-6 > div,
  #account-view > div {
    box-shadow: 0 4px 12px rgba(15, 23, 42, 0.06) !important;
    border-color: #e7ece9 !important;
  }

  .top-bar-content {
    align-items: flex-start;
  }

  .top-year-button {
    box-shadow: none !important;
    transform: none !important;
  }

  .top-year-button:hover {
    transform: none !important;
  }

  .nav-button.active {
    transform: none;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25);
  }

  .action-btn::after {
    display: none;
  }

  .section-row {
    border-width: 1px;
    border-radius: 0.875rem;
    padding: 1rem;
  }

  .stats-card-container {
    padding: 1.25rem !important;
  }

  .stats-inner {
    gap: 0.875rem !important;
  }

  .stats-icon {
    padding: 0.75rem !important;
    border-radius: 1rem !important;
  }

  .stats-value {
    font-size: 2rem !important;
  }

  .mobile-card-table td {
    border-bottom: 1px solid #eef2f0;
  }

  .content-container input[type="text"],
  .content-container input[type="number"],
  .content-container input[type="date"],
  .content-container input[type="tel"],
  .content-container select,
  .content-container textarea,
  .content-container button {
    font-size: 0.9rem !important;
  }

  .content-container input[type="text"],
  .content-container input[type="number"],
  .content-container input[type="date"],
  .content-container input[type="tel"],
  .content-container select,
  .content-container textarea {
    min-height: 2.5rem;
  }

  #applicants-view > div > .flex.justify-between.items-center,
  #students-view > div > .flex.justify-between.items-center,
  #records-view > div > .flex.justify-between.items-center,
  #reports-view .bg-blue-50 .flex.items-center.justify-between,
  #sectionSelectionContainer .flex.items-center.justify-between {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 0.75rem !important;
  }

  #applicants-view .flex.space-x-2,
  #students-view .flex.space-x-2,
  #reports-view .flex.space-x-2 {
    width: 100%;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-left: 0 !important;
  }

  #applicants-view .flex.space-x-2 > *,
  #students-view .flex.space-x-2 > *,
  #reports-view .flex.space-x-2 > * {
    flex: 1 1 100%;
    min-width: 0;
    margin-left: 0 !important;
    margin-right: 0 !important;
  }

  #applicants-view .relative,
  #students-view .relative,
  #reports-view .relative {
    width: 100%;
  }

  #applicants-view input,
  #students-view input,
  #reports-view input,
  #applicants-view select,
  #students-view select,
  #reports-view select,
  #applicants-view button,
  #students-view button,
  #reports-view button,
  #sectionSelectionContainer select,
  #sectionSelectionContainer button {
    width: 100%;
  }

  /* Keep Applicants enroll button compact on mobile */
  #applicants-view #bulkEnrollBtn {
    width: auto !important;
    padding: 0.5rem 0.8rem !important;
    font-size: 0.8rem !important;
    border-radius: 0.65rem !important;
  }

  /* Keep section/program select sized to its current label text. */
  #applicants-view #bulkSectionSelect {
    width: auto !important;
    max-width: 100%;
  }

  /* Applicants toolbar: keep All Grades + Sort Students on one row */
  #applicants-view .flex.space-x-2 .relative {
    flex: 1 1 100% !important;
  }

  #applicants-view #applicantsGradeFilter,
  #applicants-view #sortStudentsBtn {
    flex: 1 1 calc(50% - 0.25rem) !important;
    width: auto !important;
    min-width: 0 !important;
    min-height: 2.2rem !important;
    height: auto !important;
    padding-top: 0.42rem !important;
    padding-bottom: 0.42rem !important;
    font-size: 0.78rem !important;
    line-height: 1 !important;
  }

  #applicants-view #sortStudentsBtn {
    display: flex !important;
    flex-direction: row !important;
    align-items: center !important;
    justify-content: flex-start !important;
    flex-wrap: nowrap !important;
    gap: 0.35rem !important;
    white-space: nowrap !important;
    text-align: left !important;
    padding-left: 0.55rem !important;
  }

  /* Override Tailwind space-x so icon/label stay in one row */
  #applicants-view #sortStudentsBtn > :not([hidden]) ~ :not([hidden]) {
    margin-left: 0 !important;
    margin-right: 0 !important;
  }

  #applicants-view #sortStudentsBtn i,
  #applicants-view #sortStudentsBtn svg {
    display: inline-flex !important;
    vertical-align: middle !important;
    margin: 0 !important;
    flex: 0 0 auto !important;
    order: 0 !important;
    margin-left: 0 !important;
    margin-right: 0.2rem !important;
  }

  #applicants-view #sortStudentsBtn span {
    display: inline-block !important;
    order: 1 !important;
    margin-left: 0 !important;
    line-height: 1 !important;
    white-space: nowrap !important;
    vertical-align: middle !important;
  }

  #viewModal .overflow-auto,
  #viewModalContent {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  #viewModalContent .flex.items-center.justify-between,
  #viewModalContent .flex.justify-between.items-center,
  #viewModalContent .flex.items-start.space-x-4,
  #viewModalContent .flex.items-center.space-x-3 {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  #viewModalContent a,
  #viewModalActions button {
    width: 100%;
    justify-content: center;
  }

  #viewModalContent .document-row {
    display: flex !important;
    align-items: center !important;
    justify-content: space-between !important;
    flex-wrap: nowrap !important;
    gap: 0.75rem !important;
  }

  #viewModalContent .document-meta {
    flex: 1 1 auto !important;
    min-width: 0 !important;
    display: flex !important;
    align-items: center !important;
    flex-wrap: nowrap !important;
    gap: 0.5rem !important;
  }

  #viewModalContent .document-meta > div:first-child {
    flex: 0 0 auto !important;
    padding: 0.45rem !important;
  }

  #viewModalContent .document-meta > div:first-child i,
  #viewModalContent .document-meta > div:first-child svg {
    width: 1rem !important;
    height: 1rem !important;
  }

  #viewModalContent .document-meta > div:last-child {
    flex: 1 1 auto !important;
    min-width: 0 !important;
    display: flex !important;
    flex-direction: column !important;
    justify-content: center !important;
  }

  #viewModalContent .document-meta > div:last-child p:first-child {
    font-size: 0.78rem !important;
    line-height: 1.05rem !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
  }

  #viewModalContent .document-meta > div:last-child p:last-child {
    font-size: 0.68rem !important;
    line-height: 0.9rem !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
  }

  #viewModalContent .document-view-btn {
    width: auto !important;
    min-width: 4.5rem !important;
    max-width: max-content !important;
    flex: 0 0 auto !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    white-space: nowrap !important;
    margin-left: auto !important;
    padding: 0.42rem 0.7rem !important;
    font-size: 0.76rem !important;
    line-height: 1 !important;
    gap: 0.3rem !important;
  }

  #viewModalContent .document-row > :not([hidden]) ~ :not([hidden]),
  #viewModalContent .document-meta > :not([hidden]) ~ :not([hidden]),
  #viewModalContent .document-view-btn > :not([hidden]) ~ :not([hidden]) {
    margin-left: 0 !important;
    margin-right: 0 !important;
  }

  #viewModalContent .document-view-btn i,
  #viewModalContent .document-view-btn svg {
    width: 0.9rem !important;
    height: 0.9rem !important;
  }

  #viewModalActions {
    display: grid !important;
    grid-template-columns: 1fr;
    gap: 0.5rem;
  }

  /* Cancel Tailwind space-x offset so stacked buttons stay aligned */
  #viewModalActions > :not([hidden]) ~ :not([hidden]) {
    margin-left: 0 !important;
    margin-right: 0 !important;
  }

  /* Smaller action buttons in archive/mobile tables */
  #archive-view .action-btn,
  #archive-view button[class*="bg-"],
  #archive-view button[class*="border-"] {
    font-size: 0.78rem !important;
    padding: 0.4rem 0.55rem !important;
    min-height: auto !important;
  }
}

@media (max-width: 640px) {
  /* Keep tables horizontal on mobile (disable card-style stacking) */
  .mobile-card-table,
  .mobile-card-table thead,
  .mobile-card-table tbody,
  .mobile-card-table tr,
  .mobile-card-table th,
  .mobile-card-table td {
    display: table-cell !important;
    width: auto !important;
  }

  .mobile-card-table {
    display: table !important;
    width: 100% !important;
    min-width: 0 !important;
    table-layout: auto;
  }

  .mobile-card-table thead {
    display: table-header-group !important;
  }

  .mobile-card-table tbody {
    display: table-row-group !important;
  }

  .mobile-card-table tr {
    display: table-row !important;
  }

  .mobile-card-table td::before {
    content: none !important;
  }

  /* Let container scroll horizontally if needed */
  .bg-white.rounded-xl.overflow-hidden.border.border-gray-200 {
    overflow-x: auto !important;
    overflow-y: hidden !important;
    -webkit-overflow-scrolling: touch;
  }

  /* Applicants table: keep Select(1), Name(3) and Actions(7) */
  #applicants-view table.w-full th:nth-child(2),
  #applicants-view table.w-full td:nth-child(2),
  #applicants-view table.w-full th:nth-child(4),
  #applicants-view table.w-full td:nth-child(4),
  #applicants-view table.w-full th:nth-child(5),
  #applicants-view table.w-full td:nth-child(5),
  #applicants-view table.w-full th:nth-child(6),
  #applicants-view table.w-full td:nth-child(6) {
    display: none !important;
  }

  /* Students table: keep Name(2), Actions(6) only */
  #students-view table.w-full th:nth-child(1),
  #students-view table.w-full td:nth-child(1),
  #students-view table.w-full th:nth-child(3),
  #students-view table.w-full td:nth-child(3),
  #students-view table.w-full th:nth-child(4),
  #students-view table.w-full td:nth-child(4),
  #students-view table.w-full th:nth-child(5),
  #students-view table.w-full td:nth-child(5) {
    display: none !important;
  }

  /* Reports table: keep Name(2) only */
  #reports-view table.w-full th:nth-child(1),
  #reports-view table.w-full td:nth-child(1),
  #reports-view table.w-full th:nth-child(3),
  #reports-view table.w-full td:nth-child(3),
  #reports-view table.w-full th:nth-child(4),
  #reports-view table.w-full td:nth-child(4),
  #reports-view table.w-full th:nth-child(5),
  #reports-view table.w-full td:nth-child(5) {
    display: none !important;
  }

  /* Archive tables: keep Name(2) and Actions(last) only */
  #archive-view table.w-full th:not(:nth-child(2)):not(:last-child),
  #archive-view table.w-full td:not(:nth-child(2)):not(:last-child) {
    display: none !important;
  }

  /* Student Records tables: keep Name(2) and Actions(last) only */
  #records-view table.section-table th:not(:nth-child(2)):not(:last-child),
  #records-view table.section-table td:not(:nth-child(2)):not(:last-child) {
    display: none !important;
  }

  /* Keep action column compact so Name gets more space */
  #applicants-view table.w-full th:last-child,
  #applicants-view table.w-full td:last-child,
  #students-view table.w-full th:last-child,
  #students-view table.w-full td:last-child,
  #records-view table.section-table th:last-child,
  #records-view table.section-table td:last-child,
  #archive-view table.w-full th:last-child,
  #archive-view table.w-full td:last-child {
    width: 1% !important;
    white-space: nowrap !important;
  }

  #applicants-view table.w-full td:nth-child(3),
  #students-view table.w-full td:nth-child(2),
  #records-view table.section-table td:nth-child(2),
  #archive-view table.w-full td:nth-child(2),
  #reports-view table.w-full td:nth-child(2) {
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    max-width: 1px !important;
  }

  /* Applicants checkbox column: visible but very narrow */
  #applicants-view table.w-full th:first-child,
  #applicants-view table.w-full td:first-child {
    width: 1% !important;
    white-space: nowrap !important;
    padding-left: 0.25rem !important;
    padding-right: 0.25rem !important;
    text-align: center !important;
  }

  #applicants-view table.w-full input[type="checkbox"] {
    width: 0.85rem !important;
    height: 0.85rem !important;
  }

  /* Smaller table action buttons so names have more room */
  #applicants-view .action-btn,
  #students-view .action-btn,
  #archive-view .action-btn,
  #records-view .section-table .action-btn {
    padding: 0.28rem 0.42rem !important;
    min-width: 1.8rem !important;
    min-height: 1.8rem !important;
    font-size: 0.72rem !important;
  }

  #applicants-view .action-btn-group,
  #students-view .action-btn-group,
  #archive-view .action-btn-group {
    gap: 0.22rem !important;
  }

  /* Smaller mobile table text for denser single-line rows */
  #applicants-view table.w-full th,
  #applicants-view table.w-full td,
  #students-view table.w-full th,
  #students-view table.w-full td,
  #records-view table.section-table th,
  #records-view table.section-table td,
  #archive-view table.w-full th,
  #archive-view table.w-full td,
  #reports-view table.w-full th,
  #reports-view table.w-full td {
    font-size: 0.72rem !important;
    line-height: 1.1 !important;
    padding-top: 0.4rem !important;
    padding-bottom: 0.4rem !important;
  }
}

@media (max-width: 480px) {
  .top-school-name {
    display: none;
  }

  #dashboard-view h2,
  #applicants-view h2,
  #students-view h2,
  #records-view h2,
  #reports-view h2,
  #sections-view h2,
  #archive-view h2,
  #account-view h2 {
    font-size: 1.25rem !important;
    margin-bottom: 0.25rem !important;
  }

  #dashboard-view p.text-gray-600,
  #applicants-view p.text-gray-600,
  #students-view p.text-gray-600,
  #records-view p.text-gray-600,
  #reports-view p.text-gray-600,
  #sections-view p.text-gray-600,
  #archive-view p.text-gray-600,
  #account-view p.text-gray-600 {
    font-size: 0.8rem !important;
  }

  .stats-card-container {
    padding: 1rem !important;
    border-radius: 1rem !important;
  }

  .stats-label {
    font-size: 0.82rem;
  }

  .stats-value {
    font-size: 1.55rem !important;
  }

  .stats-desc {
    font-size: 0.76rem;
  }

  .grade-breakdown-card {
    border-radius: 0.75rem;
  }

  .grade-breakdown-header,
  .grade-breakdown-body {
    padding-left: 0.625rem !important;
    padding-right: 0.625rem !important;
  }

  .mobile-card-table td::before {
    letter-spacing: 0.02em;
    text-transform: none;
  }

  #viewModal .modal-content {
    margin: 0 !important;
  }

  #viewModal .modal-content > div:first-child {
    align-items: center !important;
  }

  #viewModal .overflow-auto {
    max-height: calc(100vh - 8rem) !important;
    padding-right: 0 !important;
  }

  #viewModalTitle {
    font-size: 1.25rem !important;
    line-height: 1.5rem;
  }

  #viewModalContent .grid {
    gap: 0.625rem !important;
  }

  /* View modal resize for small phones */
  #viewModal .modal-content {
    width: 100% !important;
    max-width: 100% !important;
    padding: 0.75rem !important;
    border-radius: 0.75rem !important;
  }

  #viewModal .modal-content > div:first-child {
    align-items: center !important;
  }

  #viewModal .overflow-auto {
    max-height: calc(100vh - 7.25rem) !important;
  }

  #viewModalActions button {
    font-size: 0.82rem !important;
    padding: 0.45rem 0.6rem !important;
  }

  #filePreviewModal {
    padding: 0 !important;
  }

  #filePreviewModal .modal-content {
    width: 100% !important;
    max-width: 100% !important;
    height: 100vh !important;
    max-height: 100vh !important;
    margin: 0 !important;
    padding: 0 !important;
    border-radius: 0 !important;
  }

  #filePreviewModal .modal-content > div:first-child {
    padding: calc(2.5rem + env(safe-area-inset-top, 0px)) 0.25rem 0.75rem 1rem !important;
  }

  #filePreviewModal #filePreviewOpenLink {
    padding: 0.5rem 0.75rem !important;
    font-size: 0.78rem !important;
  }

  #filePreviewModal #filePreviewFrame,
  #filePreviewModal #filePreviewImage {
    height: calc(100vh - 64px) !important;
  }
}
