/* =========================================================================
   Apex EduTech Client Portal — responsive / mobile-friendly layer
   Loaded on every portal page. Purely additive: no existing rules removed.
   ========================================================================= */

/* ---------- 1. Global safety ---------- */
*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html, body {
  max-width: 100%;
  overflow-x: hidden;
}

img, svg, video, canvas, iframe, table { max-width: 100%; }
img, video { height: auto; }

/* Never let a long file name / URL blow out the layout */
h1, h2, h3, h4, p, li, td, th, a, span, div { overflow-wrap: anywhere; word-break: break-word; }

/* ---------- 2. Readable type scale ---------- */
:root { --apex-fs-base: 16px; }

body { font-size: clamp(15px, 0.5vw + 14px, 17px); line-height: 1.55; }

@media (max-width: 640px) {
  h1 { font-size: 1.375rem !important; line-height: 1.3 !important; }
  h2 { font-size: 1.1875rem !important; line-height: 1.35 !important; }
  h3 { font-size: 1.0625rem !important; line-height: 1.4 !important; }
  .text-3xl { font-size: 1.5rem !important; }
  .text-2xl { font-size: 1.25rem !important; }
  .text-xl  { font-size: 1.125rem !important; }
  /* Nothing smaller than 13px anywhere — keeps labels legible */
  .text-xs  { font-size: 0.8125rem !important; }
  .text-sm  { font-size: 0.9375rem !important; }
}

/* Inputs at >=16px stop iOS from zooming the page on focus */
input, select, textarea, button {
  font-family: inherit;
  font-size: max(16px, 1em);
}
@media (min-width: 641px) {
  input, select, textarea, button { font-size: inherit; }
}

/* ---------- 3. Touch targets ---------- */
@media (max-width: 900px) {
  button,
  a.btn,
  [role="button"],
  input[type="submit"],
  input[type="button"],
  .file-actions a,
  #cp-upload-btn {
    min-height: 44px;
  }
  button, [role="button"], input[type="submit"] { touch-action: manipulation; }
}

/* ---------- 4. Site header (login page) ---------- */
@media (max-width: 900px) {
  html body.apex-has-site-header { padding-top: 0 !important; }

  .apex-site-header {
    position: sticky !important;
    top: 0;
  }
  .apex-site-header .apex-title-wrap { padding: 12px 12px 6px; }
  .apex-site-header .apex-title { font-size: clamp(16px, 4.6vw, 22px) !important; }

  /* One swipeable row instead of a cramped 3-line wrap */
  .apex-site-header .apex-nav {
    flex-wrap: nowrap !important;
    justify-content: flex-start !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding: 4px 12px 10px !important;
    gap: 4px !important;
    scroll-snap-type: x proximity;
  }
  .apex-site-header .apex-nav::-webkit-scrollbar { display: none; }
  .apex-site-header .apex-nav a {
    font-size: 15px !important;
    padding: 9px 12px !important;
    flex: 0 0 auto;
    scroll-snap-align: start;
  }
  .apex-nav-wrap { position: relative; }
}

/* ---------- 5. Login card ---------- */
@media (max-width: 1023px) {
  .apex-login-shell { min-height: auto !important; padding: 16px !important; }
}
@media (max-width: 640px) {
  .apex-login-shell { padding: 12px !important; }
  .apex-login-card { border-radius: 1rem !important; box-shadow: 0 6px 20px rgba(15,23,42,.10) !important; }
  .apex-login-card .p-8, .apex-login-card .p-12 { padding: 1.25rem !important; }
}

/* ---------- 6. Dashboard shell ---------- */
@media (max-width: 767px) {
  /* Give the drawer a proper backdrop and a comfortable width */
  #sidebar { width: min(85vw, 20rem) !important; }

  #apex-sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, .45);
    z-index: 20;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s ease;
  }
  body.apex-drawer-open #apex-sidebar-backdrop { opacity: 1; pointer-events: auto; }
  body.apex-drawer-open { overflow: hidden; }

  main { padding: 0.875rem !important; }

  header.flex { flex-wrap: wrap; gap: .5rem; padding: .75rem !important; }
  header.flex h2 { margin-left: .625rem !important; font-size: 1.0625rem !important; }

  main .bg-white.p-6 { padding: 1rem !important; }
  main .rounded-lg { border-radius: .75rem !important; }
}

@media (max-width: 480px) {
  header.flex > div:first-child { flex: 1 1 100%; }
  header.flex > a[href="logout.php"] { flex: 0 0 auto; margin-left: auto; }
}

/* Breadcrumbs: scroll instead of wrapping into a tall stack */
#breadcrumb-container {
  overflow-x: auto;
  white-space: nowrap;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  max-width: 100%;
}
#breadcrumb-container::-webkit-scrollbar { display: none; }

/* ---------- 7. File list ---------- */
@media (max-width: 640px) {
  #fileDisplay li {
    flex-direction: column;
    align-items: stretch !important;
    gap: .625rem;
    padding: .875rem !important;
  }
  #fileDisplay .file-info { margin-right: 0 !important; }
  #fileDisplay .file-name { font-size: 1rem; line-height: 1.35; }
  #fileDisplay .file-actions {
    margin-top: 0 !important;
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    gap: .5rem;
  }
  #fileDisplay .file-actions a {
    margin-left: 0 !important;
    flex: 1 1 auto;
    justify-content: center;
    padding: .625rem .875rem !important;
    font-size: .9375rem !important;
  }
}

/* ---------- 8. Tables -> cards on small screens ---------- */
.apex-table-scroll {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 700px) {
  .apex-table-scroll { overflow-x: visible; }

  table.apex-stack,
  table.apex-stack thead,
  table.apex-stack tbody,
  table.apex-stack tr,
  table.apex-stack th,
  table.apex-stack td { display: block; width: auto; }

  table.apex-stack thead { position: absolute; left: -9999px; top: -9999px; }

  table.apex-stack tr {
    background: #fff;
    border: 1px solid #e2e8f0;
    border-radius: .75rem;
    margin-bottom: .75rem;
    padding: .25rem .25rem;
    box-shadow: 0 1px 2px rgba(15,23,42,.06);
  }

  table.apex-stack td {
    display: flex;
    gap: .75rem;
    align-items: flex-start;
    justify-content: space-between;
    padding: .5rem .75rem !important;
    border: 0 !important;
    white-space: normal !important;
    text-align: right;
    font-size: .9375rem !important;
  }
  table.apex-stack td + td { border-top: 1px solid #f1f5f9 !important; }

  table.apex-stack td::before {
    content: attr(data-label);
    flex: 0 0 40%;
    text-align: left;
    font-weight: 600;
    color: #475569;
    font-size: .8125rem;
    text-transform: uppercase;
    letter-spacing: .02em;
    padding-top: .1rem;
  }
  table.apex-stack td:empty { display: none; }
  table.apex-stack td[colspan] { text-align: center; }
  table.apex-stack td[colspan]::before { content: ""; display: none; }

  table.apex-stack td .flex,
  table.apex-stack td .space-x-2 { flex-wrap: wrap; gap: .5rem; justify-content: flex-end; }
  table.apex-stack td a, table.apex-stack td button { min-height: 40px; display: inline-flex; align-items: center; }
}

/* ---------- 9. Modals / overlays ---------- */
@media (max-width: 767px) {
  #excel-viewer-modal > div,
  .apex-modal-panel {
    width: 100% !important;
    max-width: 100% !important;
    height: 100% !important;
    max-height: 100% !important;
    border-radius: 0 !important;
    margin: 0 !important;
  }
  #cp-toast { left: .75rem; right: .75rem; max-width: none !important; bottom: .75rem; }
  #cp-drop-hint > div { padding: 1.25rem 1.5rem !important; text-align: center; }
}

/* ---------- 10. Generic layout softening ---------- */
@media (max-width: 640px) {
  .px-6 { padding-left: 1rem !important; padding-right: 1rem !important; }
  .p-6  { padding: 1rem !important; }
  .p-8  { padding: 1.25rem !important; }
  .p-12 { padding: 1.5rem !important; }
  .space-x-2 > * + *, .space-x-3 > * + *, .space-x-4 > * + * { margin-left: .5rem !important; }
  .max-w-md, .max-w-lg, .max-w-xl, .max-w-2xl, .max-w-3xl, .max-w-4xl, .max-w-5xl, .max-w-6xl, .max-w-7xl { max-width: 100% !important; }
  form .flex:not(.apex-keep-row) { flex-wrap: wrap; }
  form input[type="text"], form input[type="password"], form input[type="email"],
  form input[type="file"], form select, form textarea { width: 100% !important; }
}

/* Fade hint added by apex-responsive.js on horizontal scrollers */
.apex-scroll-hint::after {
  content: "";
  position: absolute;
  right: 0; top: 0; bottom: 0; width: 28px;
  pointer-events: none;
  background: linear-gradient(to right, rgba(255,255,255,0), rgba(255,255,255,.95));
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* ---------- 11. Print ---------- */
@media print {
  #sidebar, header, #cp-upload-btn, .file-actions { display: none !important; }
  main { padding: 0 !important; }
}

/* ---------- 12. Refinements ---------- */
@media (max-width: 640px) {
  header.flex { flex-wrap: nowrap !important; align-items: center; }
  header.flex > div:first-child { flex: 1 1 auto !important; min-width: 0; }
  header.flex h2 { white-space: normal; font-size: 1rem !important; margin-left: .5rem !important; }
  header.flex > a[href="logout.php"] { flex: 0 0 auto !important; margin-left: .5rem; padding: .5rem .75rem !important; white-space: nowrap; }

  main > .flex.items-center.justify-between { flex-wrap: wrap; gap: .5rem; }
  #breadcrumb-container { flex: 1 1 100%; }
  #back-button, #cp-upload-btn { flex: 0 0 auto; white-space: nowrap; }
}

/* Back button ships without a display class; keep icon + label on one line */
#back-button:not(.hidden) { display: inline-flex !important; align-items: center; white-space: nowrap; }

/* ---------- 13. Admin pages ---------- */
@media (max-width: 640px) {
  /* Button labels should never break mid-word */
  button, a.inline-flex, input[type="submit"], input[type="button"] { white-space: nowrap; }

  /* Action rows wrap onto a second line instead of crushing their buttons */
  .flex.justify-between:not(.h-16) { flex-wrap: wrap; gap: .5rem; }
  .flex.justify-between:not(.h-16) > * { min-width: 0; }

  /* Fixed-height top bars grow instead of clipping */
  .h-16 { height: auto !important; min-height: 3.5rem; padding: .5rem 0; gap: .5rem; }
  .h-16 > :first-child { min-width: 0; flex: 1 1 auto; }
  .h-16 h1 { font-size: .9375rem !important; line-height: 1.25; }

  /* Search forms: input takes the row, button sits beside it */
  form .flex input[type="text"], form .flex input[type="search"] { flex: 1 1 auto; min-width: 0; }
}

/* Stacked-table cells that hold two or more buttons */
@media (max-width: 700px) {
  table.apex-stack td.apex-cell-actions { display: block; text-align: left; }
  table.apex-stack td.apex-cell-actions::before { display: block; flex: none; margin-bottom: .4rem; }
  table.apex-stack td.apex-cell-actions > a,
  table.apex-stack td.apex-cell-actions > button {
    display: inline-flex; justify-content: center; align-items: center;
    margin: 0 .4rem .4rem 0; white-space: nowrap;
  }
}

/* ---------- 14. Inline forms (folder create, search, upload) ---------- */
@media (max-width: 640px) {
  form.flex, form .flex:not(.apex-keep-row) { flex-wrap: wrap; gap: .5rem; }
  form.flex > input[type="text"],
  form.flex > input[type="search"],
  form .flex > input[type="text"],
  form .flex > input[type="search"] { flex: 1 1 10rem; min-width: 0; width: auto !important; }
  form.flex > button, form.flex > input[type="submit"],
  form .flex > button, form .flex > input[type="submit"] { flex: 0 0 auto; }
  form input[type="file"] { flex: 1 1 100%; min-width: 0; max-width: 100%; }
}
