/* Mobile-specific layout for iOS, Android and PWA */
@media (max-width: 820px) {
  html,
  body {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
    overscroll-behavior-x: none;
    -webkit-text-size-adjust: 100%;
  }

  body {
    touch-action: pan-y;
  }

  .auth-screen,
  .app-shell,
  .app-header,
  .layout,
  .sidebar,
  .content,
  .view,
  .cards-list,
  .stats-grid,
  .modal-root,
  .modal-card,
  .modal-body {
    max-width: 100%;
    min-width: 0;
    overflow-x: hidden;
  }

  .auth-screen {
    align-items: start;
    padding: max(12px, env(safe-area-inset-top)) 12px max(16px, env(safe-area-inset-bottom));
  }

  .auth-card {
    width: 100%;
    padding: 16px;
    border-radius: 18px;
  }

  .auth-card .brand-lockup.centered {
    padding: 12px;
    gap: 10px;
  }

  .auth-card .brand-mark,
  .app-header .brand-mark {
    width: 52px;
    height: 52px;
    border-radius: 16px;
  }

  .auth-card .brand-mark svg,
  .app-header .brand-mark svg {
    width: 28px;
    height: 28px;
  }

  .auth-card h1,
  .app-header h1 {
    font-size: 20px;
    line-height: 1.12;
  }

  .form-grid.two,
  .stats-grid,
  .module-grid,
  .report-grid,
  .races-grid,
  .export-options-grid,
  .attendance-day-options {
    grid-template-columns: 1fr;
  }

  input,
  select,
  textarea,
  button {
    max-width: 100%;
  }

  .btn,
  .link-btn,
  .auth-tab,
  .nav-btn {
    min-height: 44px;
  }

  .app-header {
    padding: calc(env(safe-area-inset-top) + 12px) 14px 14px;
    grid-template-columns: 1fr;
    gap: 12px;
    border-radius: 0 0 22px 22px;
  }

  .app-header .brand-lockup {
    align-items: center;
    gap: 12px;
  }

  .app-header .brand-lockup > div:last-child {
    min-width: 0;
  }

  .app-header h1 {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  .user-chip {
    width: 100%;
    justify-content: center;
    padding: 9px 12px;
  }

  .user-chip span {
    max-width: min(230px, 62vw);
    font-size: 14px;
  }

  #logoutBtn {
    width: 100%;
  }

  .layout {
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
    padding: 12px;
  }

  .sidebar {
    position: static;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    padding: 10px;
    border-radius: 16px;
  }

  .sidebar .nav-btn {
    width: 100%;
    text-align: center;
    padding: 10px 8px;
    white-space: normal;
  }

  .page-title,
  .view-head,
  .stat-card,
  .data-card {
    padding: 15px;
    border-radius: 16px;
  }

  .page-title h2,
  .view-head h2 {
    font-size: 24px;
    line-height: 1.12;
  }

  .view-head {
    display: grid;
    gap: 12px;
  }

  .view-actions {
    justify-content: stretch;
    display: grid;
    grid-template-columns: 1fr;
  }

  .view-actions .btn,
  .search-input {
    width: 100%;
    max-width: none;
  }

  .stats-grid {
    gap: 10px;
  }

  .stat-card strong {
    font-size: 32px;
  }

  .competitions-grid,
  .users-grid {
    grid-template-columns: 1fr;
  }

  .competition-card-vertical,
  .user-card {
    min-height: 0;
  }

  .competition-card-vertical .card-actions,
  .user-card .button-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .competition-card-vertical .card-actions .danger,
  .user-card .button-row .danger {
    grid-column: 1 / -1;
  }

  .modal-root {
    align-items: end;
    padding: max(10px, env(safe-area-inset-top)) 10px max(10px, env(safe-area-inset-bottom));
  }

  .modal-card {
    width: 100%;
    max-height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 20px);
    border-radius: 20px 20px 16px 16px;
  }

  .modal-head {
    padding: 14px;
    gap: 10px;
  }

  .modal-head h3 {
    font-size: 18px;
    line-height: 1.2;
  }

  .modal-body {
    padding: 14px;
  }

  .close-btn {
    width: 42px;
    height: 42px;
    flex: 0 0 auto;
  }

  .form-actions {
    display: grid;
    grid-template-columns: 1fr;
  }

  .form-actions .btn,
  .presence-form > .btn.primary,
  .presence-athlete-box .btn {
    width: 100%;
  }

  .presence-form {
    gap: 22px;
  }

  .presence-form .form-grid.two {
    gap: 16px;
  }

  .presence-athlete-box {
    display: grid;
    padding: 16px;
    gap: 14px;
  }

  .attendance-summary-card {
    padding: 16px;
  }

  .attendance-summary-pills {
    display: grid;
    grid-template-columns: 1fr;
  }

  .attendance-calendar-card {
    padding: 12px;
    gap: 12px;
  }

  .attendance-calendar-head {
    display: grid;
    gap: 12px;
  }

  .attendance-calendar-head h3 {
    font-size: 20px;
  }

  .attendance-calendar-head p {
    font-size: 14px;
  }

  .attendance-calendar-controls {
    display: grid;
    grid-template-columns: 44px 1fr 44px;
    gap: 8px;
    align-items: center;
  }

  .attendance-calendar-controls strong {
    min-width: 0;
    text-align: center;
    font-size: 15px;
  }

  .attendance-calendar-controls [data-attendance-calendar="today"] {
    grid-column: 1 / -1;
    width: 100%;
  }

  .attendance-calendar-weekdays,
  .attendance-calendar-grid {
    grid-template-columns: repeat(7, minmax(0, 1fr));
    gap: 4px;
  }

  .attendance-calendar-weekdays span {
    font-size: 10px;
    letter-spacing: 0;
  }

  .attendance-calendar-cell {
    min-height: 64px;
    padding: 6px;
    border-radius: 10px;
    gap: 4px;
  }

  .attendance-day-number {
    font-size: 15px;
  }

  .attendance-day-markers {
    gap: 2px;
  }

  .attendance-session-dot {
    width: 17px;
    height: 17px;
    font-size: 9px;
  }

  .attendance-calendar-cell small {
    display: none;
  }

  .athlete-picker-card {
    width: 100%;
    max-height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 20px);
    border-radius: 20px;
  }

  .athlete-picker-head {
    padding: 14px;
    align-items: flex-start;
  }

  .picker-athlete-row {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .picker-status-options {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .picker-status-options label {
    justify-content: center;
    padding: 9px 6px;
    font-size: 12px;
  }

  .race-toolbar,
  .button-row,
  .meta {
    gap: 7px;
  }
}

@media (max-width: 390px) {
  .auth-card,
  .page-title,
  .view-head,
  .stat-card,
  .data-card {
    padding: 13px;
  }

  .app-header h1,
  .auth-card h1 {
    font-size: 18px;
  }

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

  .competition-card-vertical .card-actions,
  .user-card .button-row,
  .picker-status-options {
    grid-template-columns: 1fr;
  }

  .attendance-calendar-cell {
    min-height: 58px;
    padding: 5px;
  }

  .attendance-session-dot {
    width: 16px;
    height: 16px;
  }
}

@supports (overflow: clip) {
  @media (max-width: 820px) {
    html,
    body,
    .app-shell,
    .layout,
    .content,
    .view,
    .cards-list,
    .modal-card {
      overflow-x: clip;
    }
  }
}


/* Mobile action buttons refinement */
@media (max-width: 820px) {
  body.is-staff #view-competitions .view-actions.staff-only {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  #view-competitions #newCompetitionBtn {
    grid-column: 1 / -1;
    width: 100%;
  }

  #view-competitions #exportInscriptionsCsv,
  #view-competitions #exportInscriptionsPdf {
    width: 100%;
  }

  #view-attendance .attendance-calendar-controls {
    grid-template-columns: 46px minmax(0, 1fr) 46px;
    align-items: center;
  }

  #view-attendance .attendance-calendar-controls .compact-btn {
    width: 46px;
    min-width: 46px;
    height: 46px;
    min-height: 46px;
    padding: 0;
    border-radius: 14px;
    font-size: 24px;
    line-height: 1;
  }

  #view-attendance .attendance-calendar-controls strong {
    order: 0;
    min-width: 0;
    width: 100%;
    text-align: center;
    font-size: 17px;
    line-height: 1.15;
    white-space: nowrap;
  }

  #view-attendance .attendance-calendar-controls [data-attendance-calendar="today"] {
    grid-column: 1 / -1;
    width: 100%;
    min-width: 0;
    height: 44px;
    min-height: 44px;
    font-size: 14px;
    border-radius: 14px;
  }
}

@media (max-width: 390px) {
  #view-attendance .attendance-calendar-controls {
    grid-template-columns: 42px minmax(0, 1fr) 42px;
  }

  #view-attendance .attendance-calendar-controls .compact-btn {
    width: 42px;
    min-width: 42px;
    height: 42px;
    min-height: 42px;
    font-size: 22px;
  }

  #view-attendance .attendance-calendar-controls strong {
    font-size: 15px;
  }
}


/* Results mobile */
@media (max-width: 820px) {
  body.is-staff #view-results .view-actions.staff-only {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }
  #view-results .view-actions.staff-only .btn { width: 100%; }
  .results-preview-head,
  .results-competition-head { display: grid; }
  .result-stat-pills { display: grid; grid-template-columns: 1fr; }
  .results-table { min-width: 620px; }
  .result-athlete-block li { display: grid; }
}

/* Results/competition mobile anti-cut */
@media (max-width: 820px) {
  .competition-card-vertical,
  .results-import-card,
  .results-preview-card,
  .results-competition-card {
    overflow: visible;
  }
  .competition-card-vertical h3,
  .results-preview-head h3,
  .results-competition-head h3 {
    overflow-wrap: anywhere;
  }
  .competition-card-vertical .card-actions .btn {
    min-width: 0;
    white-space: normal;
  }
}

/* Results upload mobile flow */
@media (max-width: 820px) {
  .results-upload-row { grid-template-columns: 1fr; }
  .results-upload-row .btn { width: 100%; }
}

/* Compact results mobile summaries */
@media (max-width: 520px) {
  .result-summary-grid { grid-template-columns: 1fr 1fr; }
  .result-summary-grid strong { font-size: 22px; }
  .results-modal-tools { display: grid; }
}


@media (max-width: 820px) {
  .is-staff .stats-grid .staff-dashboard-only { display: block; }
}


/* PWA/mobile base hardening */
@media (max-width: 820px) {
  html {
    width: 100%;
    min-width: 0;
    overflow-x: hidden;
    background: var(--bg);
  }

  body {
    width: 100%;
    min-width: 0;
    overflow-x: hidden;
    position: relative;
  }

  .app-shell {
    width: 100%;
    min-width: 0;
    overflow-x: hidden;
  }

  .app-header {
    padding-top: max(14px, env(safe-area-inset-top));
    padding-left: max(14px, env(safe-area-inset-left));
    padding-right: max(14px, env(safe-area-inset-right));
  }

  .app-header .brand-lockup {
    width: 100%;
    min-width: 0;
  }

  .app-header .brand-lockup h1 {
    font-size: clamp(18px, 5.2vw, 22px);
    overflow-wrap: anywhere;
  }

  .layout,
  .content,
  .view,
  .cards-list {
    width: 100%;
    min-width: 0;
  }

  .page-title,
  .view-head,
  .data-card,
  .stat-card,
  .module-card,
  .report-card,
  .competition-card-vertical,
  .user-card {
    min-width: 0;
    overflow-wrap: anywhere;
  }

  .sidebar {
    width: 100%;
  }

  .modal-root {
    width: 100%;
    overflow-x: hidden;
  }

  .modal-card {
    max-width: calc(100vw - 20px);
  }
}

@media (display-mode: standalone) and (max-width: 820px) {
  .app-header {
    padding-top: max(16px, env(safe-area-inset-top));
  }
}


/* PWA standalone refinements */
@media (max-width: 820px) {
  .app-header {
    min-height: auto;
  }

  .app-header #logoutBtn {
    justify-self: stretch;
  }

  .auth-screen {
    min-height: 100dvh;
  }

  .modal-card,
  .pwa-install-banner {
    overscroll-behavior: contain;
  }
}

@media (display-mode: standalone) and (max-width: 820px) {
  body {
    min-height: 100dvh;
  }

  .app-shell {
    min-height: 100dvh;
  }
}

/* Mobile/PWA v1 final hardening */
@media (max-width: 820px) {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

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

  body {
    min-height: 100dvh;
  }

  .app-shell,
  .layout,
  .content,
  .view,
  .cards-list,
  .stats-grid,
  .competitions-grid,
  .users-grid,
  .results-modal-grid,
  .progression-grid,
  .modal-body,
  .modal-card {
    max-width: 100%;
    min-width: 0;
  }

  .app-header {
    width: 100%;
    max-width: 100vw;
    padding-top: max(12px, env(safe-area-inset-top));
    padding-bottom: 14px;
  }

  .app-header .brand-lockup,
  .user-chip {
    min-width: 0;
  }

  .app-header h1,
  .user-chip span,
  .page-title h2,
  .view-head h2,
  .competition-card-vertical h3,
  .data-card h3 {
    overflow-wrap: anywhere;
    word-break: normal;
  }

  .user-chip span {
    text-align: center;
    text-transform: none;
  }

  .sidebar {
    margin-top: 0;
  }

  .modal-root {
    width: 100vw;
    max-width: 100vw;
    left: 0;
    right: 0;
  }

  .modal-card {
    width: min(100%, calc(100vw - 20px));
  }

  table,
  .table-wrap,
  .results-table {
    max-width: 100%;
  }
}

@supports (overflow: clip) {
  @media (max-width: 820px) {
    html,
    body,
    .app-shell,
    .layout,
    .content,
    .modal-root {
      overflow-x: clip;
    }
  }
}

@media (display-mode: standalone) and (max-width: 820px) {
  .app-header {
    padding-top: max(12px, env(safe-area-inset-top));
  }
}

/* Clear filters mobile */
@media (max-width: 820px) {
  .filter-tools,
  .results-modal-tools,
  .staff-progression-filters {
    width: 100%;
  }

  .clear-filter-btn {
    width: 100%;
  }
}

/* Swim identity mark mobile */
@media (max-width: 820px){.auth-card .brand-mark img,.app-header .brand-mark img{width:30px;height:30px}}
