/* ---------- Responsive polish ---------- */
@media (min-width: 861px) and (max-width: 1200px) {
  .topbar { flex-wrap: wrap; gap: .75rem; }
  .topbar__start { flex: 1 0 14rem; }
  .topbar__end { gap: .5rem; margin-inline-start: auto; }
  .topbar__userinfo { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; }
  .topbar-search { width: 38px; height: 38px; padding: 0; justify-content: center; }
  .topbar-search kbd { display: none; }
  .topbar-search__label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; }
}

@media (max-width: 860px) {
  .mobile-nav {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
  }

  .shell__content::before,
  .shell--collapsed .shell__content::before {
    inset-inline-start: 0;
  }

  .topbar {
    align-items: flex-start;
    padding: .75rem 1rem;
  }

  .topbar__start {
    min-width: 0;
    flex: 1;
  }

  .topbar__title {
    overflow: hidden;
    max-width: 46vw;
    font-size: var(--text-lead);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .topbar__subtitle {
    overflow: hidden;
    max-width: 46vw;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .topbar__end {
    gap: .5rem;
    justify-content: flex-end;
    min-width: 0;
  }

  .account-menu {
    display: none;
  }

  .topbar__menu,
  .notif-bell {
    width: 36px;
    height: 36px;
  }

  .shell__content {
    /* Clears the fixed .mobile-nav. The feedback button ends the page in
       the flow on phones (03-shell.css), so it needs no room of its own. */
    padding: 1rem .9rem calc(6.25rem + env(safe-area-inset-bottom));
    max-width: 100%;
  }

  .page__header {
    align-items: stretch;
    flex-direction: column;
    /* A wrapping column container sizes the action row at its narrowest
       (wrapped) height, leaving the stretched buttons far too tall. */
    flex-wrap: nowrap;
    gap: .75rem;
    margin-bottom: 1rem;
    padding-bottom: .75rem;
  }

  .page__header > div:has(.btn) {
    justify-content: stretch;
  }

  .page__title {
    font-size: var(--text-subtitle);
    line-height: 1.25;
  }

  .page__subtitle {
    font-size: var(--text-small);
  }

  .page-actions,
  .page__actions,
  .row-actions,
  .form-actions,
  .bulk-bar {
    justify-content: stretch;
  }

  .page-actions > *,
  .page__actions > *,
  .row-actions > *,
  .form-actions > *,
  .bulk-bar > * {
    flex: 1 1 150px;
    min-width: 0;
  }

  /* Status badges sit beside the stretched buttons at their natural size. */
  .row-actions > .badge {
    flex: none;
  }

  /* The autosave message takes its own line, and while empty it takes no
     room, so it never leaves the main button sharing a row with a blank. */
  .form-actions > .draft-status { flex-basis: 100%; }
  .form-actions > .draft-status:empty { position: absolute; }

  /* A form's Cancel already leads back, so the header's Back (marked
     page__back only where the form has one) would push the form down a row. */
  .page:has(.form-actions) .page__back { display: none; }

  .page-actions .btn,
  .page__actions .btn,
  .row-actions .btn,
  .form-actions .btn,
  .bulk-bar .btn {
    width: 100%;
  }

  .card,
  .planner-panel,
  .planner-note,
  .toolbar,
  .notifications-card,
  .notif-live {
    border-radius: var(--radius);
  }

  .card {
    padding: 1rem;
  }

  .toolbar {
    padding: .75rem;
  }

  .ward-grid,
  .missing-grid,
  .certificate-grid,
  .schedule-card {
    grid-template-columns: 1fr;
  }

  /* Counts pair up, two per row, rather than one tall card each. */
  .planner-summary,
  .stat-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: .5rem;
  }

  .stat-card {
    flex-direction: column;
    align-items: flex-start;
    gap: .5rem;
    padding: .75rem;
  }

  .stat-card__icon {
    width: 34px;
    height: 34px;
    font-size: var(--text-lead);
  }

  .stat-card__label {
    font-size: var(--text-label);
  }

  .stat-card__value {
    font-size: var(--text-subtitle);
  }

  .stat-card--link {
    position: relative;
  }

  .stat-card__arrow {
    position: absolute;
    inset-inline-end: .75rem;
    inset-block-start: .75rem;
  }

  /* Stacked, each place is one row (medal, name over halaqah, points)
     instead of a tall centered card, so the ranking starts above the fold. */
  .podium {
    grid-template-columns: 1fr;
    gap: .5rem;
  }

  .podium__place {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-areas: "medal name points" "medal meta points";
    align-items: center;
    column-gap: .75rem;
    padding: .75rem 1rem;
    text-align: start;
  }

  .podium__medal { grid-area: medal; }
  .podium__name { grid-area: name; margin-top: 0; }
  .podium__meta { grid-area: meta; margin-top: 0; }
  .podium__points { grid-area: points; margin-top: 0; }

  .podium__place--1 {
    transform: none;
  }

  .certificate-card {
    min-height: 0;
    padding: 1.25rem 1rem;
  }

  .certificate__border {
    padding: 1.5rem 1rem;
  }

  .certificate__footer {
    align-items: center;
    flex-direction: column;
    gap: .375rem;
    margin-top: 1.25rem;
  }

  .pagination {
    justify-content: flex-start;
    overflow-x: auto;
    padding-bottom: .125rem;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
  }

  .pagination__btn,
  .pagination__page {
    min-width: 44px;
    min-height: 44px;
    flex: 0 0 auto;
  }

  .form-actions--sticky {
    bottom: calc(5.25rem + env(safe-area-inset-bottom));
    margin-inline: -.9rem;
    padding: .75rem 1rem;
  }

  .assignment-list li,
  .config-list li,
  .top-students li {
    align-items: flex-start;
    flex-wrap: wrap;
  }

  .assignment-list li .btn {
    margin-inline-start: auto;
  }

  .notifications-toolbar {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .notifications-toolbar .field--grow,
  .notifications-toolbar .field:last-child {
    grid-column: auto;
  }

  .notif {
    grid-template-areas:
      "icon body"
      ". action";
    grid-template-columns: 42px minmax(0, 1fr);
  }

  .notif--selectable {
    grid-template-areas:
      "select icon body"
      ". . action";
    grid-template-columns: 44px 42px minmax(0, 1fr);
  }

  .notif__action .button_to,
  .notif__action .btn {
    width: 100%;
  }

  .notifications-bulk > * {
    flex: 0 1 auto;
  }

  .notifications-bulk__count {
    flex: 1 1 8rem;
  }
}

@media (max-width: 640px) {
  .topbar {
    display: flex;
    align-items: center;
    position: sticky;
    gap: .375rem;
  }

  .topbar__end {
    width: auto;
    flex: 0 0 auto;
    flex-wrap: nowrap;
    justify-content: flex-end;
    padding-inline-start: 0;
  }

  .topbar-search,
  .notif-bell {
    width: 42px;
    height: 42px;
    min-height: 42px;
    padding: 0;
    justify-content: center;
  }

  .topbar__title,
  .topbar__subtitle {
    max-width: min(52vw, 16rem);
  }

  .count-pill {
    margin-inline-start: .25rem;
    vertical-align: .1rem;
  }

  .table {
    font-size: var(--text-small);
  }

  .table th,
  .table td {
    padding: .5rem .75rem;
  }

  .notif {
    grid-template-areas:
      "icon body"
      ". action";
    grid-template-columns: 36px minmax(0, 1fr);
    gap: .75rem;
    padding: 1rem;
  }

  .notif--selectable {
    grid-template-areas:
      "select icon body"
      ". . action";
    grid-template-columns: 44px 36px minmax(0, 1fr);
  }

  .notif__icon {
    width: 36px;
    height: 36px;
    flex-basis: 36px;
    font-size: var(--text-lead);
  }

  .notif__action {
    width: 100%;
    margin-inline-start: 0;
  }

  .notif__action .btn {
    width: 100%;
  }

  /* Search spans the row; the two short selects share the next one. */
  .notifications-toolbar {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .notifications-toolbar .field--grow,
  .notifications-toolbar .field:last-child {
    grid-column: 1 / -1;
  }

  .notifications-bulk {
    align-items: stretch;
  }

  .notifications-bulk__select,
  .notifications-bulk__count,
  .notifications-bulk .btn {
    flex-basis: 100%;
  }

  .ward-grid {
    grid-template-columns: 1fr;
  }

  .ward-card {
    padding: 1rem;
  }

  .ward-card__metrics {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .ward-card__footer .btn {
    width: 100%;
  }

  .today-grid,
  .today-mini-list--columns {
    grid-template-columns: 1fr;
  }

  .today-row {
    grid-template-columns: 3.75rem minmax(0, 1fr);
    align-items: start;
  }

  .today-row__actions {
    grid-column: 1 / -1;
  }

  .today-row__actions {
    justify-content: stretch;
  }

  .today-row__actions .btn {
    flex: 1 1 150px;
  }

  /* A student's one action (Set assignment) stays beside the name, as in
     the class workspace, so the list isn't a column of full-width buttons. */
  .today-mini-list li > .btn {
    flex: none;
  }

  .assignment-list li {
    align-items: stretch;
    flex-direction: column;
    gap: .5rem;
  }

  .assignment-list li .btn {
    width: 100%;
    margin-inline-start: 0;
  }
}

@media (max-width: 420px) {
  .notif {
    grid-template-areas:
      "body"
      "action";
    grid-template-columns: minmax(0, 1fr);
  }

  .notif--selectable {
    grid-template-areas:
      "select body"
      "action action";
    grid-template-columns: 44px minmax(0, 1fr);
  }

  .notif__icon {
    display: none;
  }
}

@media (max-width: 480px) {
  .topbar {
    padding: .75rem .75rem;
  }

  .shell__content {
    padding-inline: .75rem;
  }

  .form-actions--sticky {
    margin-inline: -.7rem;
    padding-inline: .75rem;
  }

  .mobile-nav {
    inset-inline: .5rem;
    padding: .25rem;
    border-radius: var(--radius-lg);
  }

  .mobile-nav__item {
    min-height: 48px;
    border-radius: var(--radius);
  }

  .mobile-nav__label {
    font-size: var(--text-micro);
  }

  .btn {
    font-size: var(--text-body);
  }

  .planner-summary__item {
    min-height: 0;
    padding: .75rem .75rem;
  }

  .stat-card {
    align-items: flex-start;
    padding: 1rem;
  }

  .stat-card__icon {
    width: 38px;
    height: 38px;
  }

  .stat-card__label {
    line-height: 1.25;
  }

  /* From and to ayah are short numbers, so they share a row. */
  .portion-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .portion-row__surah,
  .portion-row__type,
  .portion-row__grade {
    grid-column: 1 / -1;
  }

  /* Its own short row, at the end: a full-width red bar read as an alarm. */
  .portion-remove {
    grid-column: 1 / -1;
    justify-self: end;
  }

  /* A surah range puts Remove beside the surah it removes, rather than
     alone on a last row. Unit and per-portion-grade rows keep the layout above. */
  .portion-row:not(.portion-row--unit):not(:has(.grade-choice)) {
    grid-template-columns: repeat(2, minmax(0, 1fr)) auto;
  }
  .portion-row:not(.portion-row--unit):not(:has(.grade-choice)) .portion-row__surah { grid-column: 1 / 3; }
  .portion-row:not(.portion-row--unit):not(:has(.grade-choice)) .portion-row__ayah + .portion-row__ayah { grid-column: 2 / -1; }
  .portion-row:not(.portion-row--unit):not(:has(.grade-choice)) .portion-remove { grid-column: 3; grid-row: 1; }
}

/* Touch layouts retain the compact desktop rhythm while meeting the 44px
   target-size requirement for controls that are intentionally dense. */
@media (max-width: 860px), (pointer: coarse) {
  .btn--sm,
  :is(.row-actions, .attention-item__actions) .action-menu__trigger,
  .toolbar__input,
  .topbar__menu,
  .topbar-search,
  .notif-bell,
  .sidebar__logout,
  .lang-switch__link,
  .theme-switch__option,
  .feedback-ratings span,
  .grade-choice__option span,
  .check-chip,
  .schedule-day,
  .switch-row,
  .topbar__privacy,
  .filter-chip,
  .filter-chips__clear,
  .saved-view-chip__delete,
  .segmented__option > span,
  .acs__input.input,
  .acs__result,
  .toolbar .btn,
  .progress-target-form .btn,
  .btn-add-row {
    min-height: 44px;
  }

  /* Text-sized links and chips grow to a full target without changing their look. */
  a.analytics__meta,
  .settings-toc a,
  .timeline-filter,
  .landing-footer__links a,
  .auth__footer a,
  .guide-breadcrumb a,
  .guardian-chip__contact,
  .podium__halaqah,
  .table__check {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
  }

  /* Grown targets keep their rows compact. */
  .landing-footer__links { flex-wrap: wrap; row-gap: 0; }
  .guide-breadcrumb ol { align-items: center; row-gap: 0; }
  .guardian-chip { padding-block: 0; }
  /* Cancels the cell padding (.75rem in the phone card layout, .5rem in a table row). */
  .table__check { justify-content: flex-start; margin-block: -.5rem; cursor: pointer; }
  .faq-item summary { min-height: 44px; }
  .settings-toc { row-gap: 0; }

  .btn--sm,
  .topbar__menu,
  .topbar-search,
  .notif-bell,
  .topbar__privacy,
  .lang-switch__link,
  .theme-switch__option,
  .saved-view-chip__delete {
    min-width: 44px;
  }

  .feedback-trigger,
  .portion-remove,
  .saved-view-chip > a,
  .chip--link {
    min-width: 44px;
    min-height: 44px;
  }

  .saved-view-chip > a,
  .chip--link {
    display: inline-flex;
    align-items: center;
  }

  .command-palette__head button,
  .feedback-dialog__head button,
  .flash__dismiss,
  .msel__tag button {
    min-width: 44px;
    min-height: 44px;
  }
}

