/* Shared phone ergonomics. Keep browser zoom available; prevent focus zoom with readable fields. */
@media (max-width: 760px), (pointer: coarse) {
  body :is(input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]):not([type="range"]):not([type="button"]):not([type="submit"]):not([type="reset"]):not([type="color"]), textarea, select, [contenteditable="true"]) {
    font-size: 16px !important;
    scroll-margin-block: 100px 24px;
  }

  body :is(input, textarea, select) { min-inline-size: 0; max-inline-size: 100%; }
  body textarea { resize: vertical; }
}

@media (max-width: 760px) {
  html { scroll-padding-top: 84px; }
  /* A 320px layout may have less usable width when the browser shows a scrollbar. */
  body { min-width: 0; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; background-attachment: scroll; }
  /* The legacy fixed 544px blurred glows needlessly repaint behind every member page. */
  body.ac-student::before, body.ac-student::after { display: none; }
  :is(main, .view, .ac-main, .mh-page, .mb-page, .sp-page, .ms-page) { min-width: 0; }
  :is(.ml-form, .ms-form, .mh-form-grid, .ac-form-row) > * { min-width: 0; }
  :is(.ml-files, .mc-materials, .ms-post-media) audio { min-width: 0; max-width: 100%; }
  :is(.ml-file, .mc-materials article, .mh-dialog-content, .ac-dialog-body) { overflow-wrap: anywhere; }
  .ml-file { min-width: 0; flex-wrap: wrap; }
  .ml-file small { margin-left: 0; }

  /* Give phone controls a finger-sized target without increasing desktop density. */
  body :is(.mh-primary, .mh-secondary, .mb-primary, .mb-secondary, .sp-primary, .sp-secondary,
    .ms-text-button, .ms-file-button, .ms-filter, .mr-button, .ac-button-small,
    .ac-account-tools > a, .ac-account-tools > button, .ac-account-switch,
    .ac-account-exit, .ml-form > button, .ml-files button,
    .ms-post-actions button, .ms-comments .mh-secondary, .ac-n-more,
    .ac-guide-session button, .ac-welcome-back) { min-height: 44px; }

  body :is(.mh-dialog-close, .ms-icon-button, .mr-close, .sp-dialog-head > button,
    .mc-dialog-head > button, .ac-dialog-close, .ac-n-heading > button) {
    flex: 0 0 44px;
    width: 44px;
    height: 44px;
    min-height: 44px;
    padding: 0;
  }

  /* Native dialogs retain their own scrolling when browser chrome/keyboard reduces space. */
  body :is(.mh-dialog, .ms-dialog, .mr-dialog, .sp-dialog, .ac-dialog) {
    max-width: calc(100vw - 24px);
    max-height: calc(100dvh - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    overflow-y: auto;
    overscroll-behavior: contain;
    scroll-padding-block: 20px;
  }

  body .mc-dialog:not([data-popup-layout="fullscreen"]) {
    max-width: calc(100vw - 24px);
    max-height: calc(100dvh - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  }

  body.ac-admin .ac-builder-dialog {
    max-height: calc(100dvh - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  }

  :is(.mr-footer, .sp-dialog-footer, .mc-dialog-footer, .ac-dialog-footer, .ms-form-actions) {
    flex-wrap: wrap;
    gap: 10px;
  }

  body :is(.mr-footer, .sp-dialog-footer, .mc-dialog-footer, .ac-dialog-footer) > :is(button, a) {
    min-width: 0;
    min-height: 44px;
    white-space: normal;
    text-align: center;
    overflow-wrap: anywhere;
  }

  body .mc-dialog[data-popup-layout="fullscreen"] .mc-dialog-head {
    padding-top: max(20px, env(safe-area-inset-top));
    padding-right: 78px;
  }

  body .mc-dialog[data-popup-layout="fullscreen"] [data-mc-action="close"] {
    top: max(12px, env(safe-area-inset-top));
    right: max(12px, env(safe-area-inset-right));
  }

  body .mc-dialog[data-popup-layout="fullscreen"] .mc-dialog-footer {
    padding-bottom: max(20px, env(safe-area-inset-bottom));
  }

  /* Five rating targets fit inside the narrowest supported dialog without clipping. */
  .mr-dialog { width: calc(100vw - 24px); border-radius: 18px; }
  .mr-head { padding: 20px 18px; gap: 12px; }
  .mr-head > div { min-width: 0; }
  .mr-head h2 { font-size: 24px; overflow-wrap: anywhere; }
  .mr-body { padding: 20px 18px; }
  .mr-rating { min-width: 0; }
  .mr-stars { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
  .mr-stars button { width: 100%; min-width: 0; min-height: 44px; height: 48px; font-size: 28px; padding: 0; }
  .mr-footer { padding: 16px 18px max(20px, env(safe-area-inset-bottom)); }
  .mr-footer > button { flex: 1 1 110px; }

  /* Long authored titles, references and filenames must wrap, not widen the page. */
  :is(.sp-heading, .ml-page-head, .mh-heading, .ms-heading, .mb-heading, .ac-page-header) :is(h1, h2, p) {
    overflow-wrap: anywhere;
  }
  .ms-card-footer { flex-wrap: wrap; }
  .ms-card-footer .ms-text-button { white-space: normal; }
  body .ms-post-head > .ms-icon-button { flex: 0 0 44px; width: 44px; height: 44px; margin-left: auto; }
  .ms-capital > div { min-width: 0; }
  .ms-capital strong { overflow-wrap: anywhere; }
  .ms-detail-tags { flex-wrap: wrap; }
  .ms-opportunity-tabs button { min-width: 0; overflow-wrap: anywhere; }
  .ml-checks label > :not(input) { min-width: 0; overflow-wrap: anywhere; }
  .ml-checks input { flex: 0 0 18px; }
  .mc-materials article > .mc-material-copy { min-width: 0; }

  .ac-n-panel {
    right: max(12px, env(safe-area-inset-right));
    width: min(420px, calc(100% - 24px));
    max-height: calc(100dvh - 86px - env(safe-area-inset-bottom));
    overscroll-behavior: contain;
  }
  #ac-notifications { width: min(420px, calc(100% - 24px)); }
  .ac-n-heading > div { min-width: 0; }
  .ac-n-toolbar { flex-wrap: wrap; }
  .ac-n-toolbar button { min-height: 44px; }
  .ac-n-item > a { padding-right: 46px; }
  .ac-n-mark { width: 40px; height: 40px; right: 3px; bottom: 4px; }
  :is(.ac-toast, .ms-toast, .ac-session-message) {
    bottom: max(16px, env(safe-area-inset-bottom));
    overflow-wrap: anywhere;
  }

  /* Tables keep horizontal scrolling inside their panel rather than the whole screen. */
  body.ac-admin :is(.ac-table-wrap, .ac-csv-table) { max-width: 100%; overflow-x: auto; }
  .mb-table-scroll { max-width: 100%; overscroll-behavior-x: contain; }
  .ac-admin :is(.ac-student-filter, .ac-toolbar > *, .lrn-filters > *) { min-width: 0; }
  .ac-admin :is(.ac-student-pagination, .ac-section-title, .ac-page-header) { flex-wrap: wrap; }
  .ac-admin .ac-student-pagination > button { min-height: 44px; }
  body.ac-admin .ac-button-small { min-height: 44px; }
  .ac-admin .ac-plan-card > div { min-width: 0; overflow-wrap: anywhere; }
  .ac-admin .ac-builder-actions > button { min-width: 0; white-space: normal; }

  /* Login stays reachable on short screens; focusing a field never needs page scaling. */
  .login-shell {
    min-height: 100dvh;
    padding: max(28px, env(safe-area-inset-top)) 16px max(28px, env(safe-area-inset-bottom));
    gap: 28px;
  }
  .login-card { padding-right: 22px; padding-left: 22px; }
  .input-wrap { min-height: 50px; height: auto; padding-top: 2px; padding-bottom: 2px; }
  .input-wrap .eye-button { flex: 0 0 44px; width: 44px; height: 44px; margin-right: -10px; }
  .remember-row label, .forgot-row button, .login-back { min-height: 44px; }
  .forgot-row button, .login-back { padding-block: 10px; }
  .remember-row label { display: inline-flex; align-items: center; }
  .login-security { overflow-wrap: anywhere; }

  .ck-shell { padding-top: env(safe-area-inset-top); }
  .ck-footer { padding-bottom: env(safe-area-inset-bottom); }
  .ck-line { flex-wrap: wrap; }
  .ck-line > span { margin-left: auto; }
  .ck-payment-instructions dl { min-width: 0; }
  .ck-payment-instructions .ck-retry { min-height: 44px; }
}

@media (max-width: 620px) {
  /* Keep shell actions usable without stealing the title's width. */
  html[data-theme] body.ac-student .menu-toggle,
  body.ac-admin .ac-menu-toggle,
  body :is(.ac-n-bell, .ac-top-notifications),
  body.ac-student .ac-top-profile,
  body.ac-admin .ac-top-profile {
    flex: 0 0 44px;
    width: 44px;
    height: 44px;
    min-height: 44px;
  }
  body .ac-sidebar-collapse { flex: 0 0 40px; width: 40px; height: 40px; margin-right: -4px; }
  body .ac-account-dock { padding-bottom: max(12px, env(safe-area-inset-bottom)); }
  .ac-admin .ac-dialog-head > div { min-width: 0; }
  .ac-admin .ac-dialog-head h2 { overflow-wrap: anywhere; }
  .ac-admin .ac-dialog-footer > .ac-button { flex: 1 1 120px; }
  .mc-dialog-footer > :is(.mh-primary, .mh-secondary) { flex: 1 1 120px; }
  .sp-dialog-footer > button { flex: 1 1 120px; }
}

@media (max-width: 430px) {
  html[data-theme] body.ac-student .topbar .ac-top-theme,
  body.ac-admin .ac-topbar #admin-theme { display: none !important; }
  body.ac-student .ac-sidebar-footer .ac-sidebar-theme { display: inline-flex; align-items: center; min-height: 44px; }
  .ck-total { flex-wrap: wrap; }
  .ck-total strong { margin-left: auto; }
  .sp-ticket { flex-wrap: wrap; }
  .sp-ticket > span:nth-child(2) { flex-basis: calc(100% - 62px); }
  .sp-ticket > :last-child { margin-left: auto; }
}

@media (max-width: 760px) and (max-height: 540px) {
  .login-shell { justify-content: flex-start; gap: 20px; padding-top: 20px; }
  .login-card { padding-top: 48px; }
  .login-accent { top: 24px; }
  .ac-guide-dialog.ac-guide-premium,
  html[data-theme] body.ac-student .ac-guide-dialog.ac-guide-premium {
    max-height: calc(100dvh - 20px);
    overflow-y: auto;
  }
}
