/* Shared mobile hardening for Olympia's static/PWA surfaces. */
html,
body {
  width: 100%;
  max-width: 100%;
  min-height: 100%;
}

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

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

.nav-item,
.nav-btn,
.mobile-bottom-nav button,
.race-split-open-btn,
button[type="submit"] {
  min-height: 48px !important;
  touch-action: manipulation;
}

.content,
.card,
.grid-4,
.grid-3,
.grid-2,
.grid-12,
.grid-21,
.chart-wrap {
  min-width: 0;
}

.chart-wrap {
  width: 100%;
  aspect-ratio: 16 / 9;
  min-height: 220px;
  height: auto;
}

.chart-wrap canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
  max-inline-size: 100%;
}

/* Fluid primitives: nested flex/grid children must be allowed to shrink. */
.app *,
.main *,
main *,
.content * {
  min-inline-size: 0;
}

.modal,
[role="dialog"],
.modal-overlay {
  max-width: 100%;
}

[role="dialog"] .card,
.modal .card,
.modal-card,
.bottom-sheet {
  width: min(100%, var(--dialog-max-width, 46rem)) !important;
  max-width: calc(100vw - 24px) !important;
}

.race-table-card {
  max-width: 100%;
  overflow: hidden !important;
}

.race-table-scroll {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior-inline: contain;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}

.race-scroll-hint {
  display: none;
}

.responsive-race-table {
  width: 100%;
  min-width: 760px;
}

.race-athlete-cell,
.race-competition-cell {
  max-width: 18rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

.race-row-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.race-row-actions .btn,
.race-split-open-btn {
  min-width: 48px;
  min-height: 48px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

@media (max-width: 768px) {
  html {
    -webkit-text-size-adjust: 100%;
  }

  body {
    overflow-x: hidden;
  }

  .app,
  .main,
  main,
  .content,
  .page {
    width: 100%;
    min-width: 0 !important;
    max-width: 100%;
  }

  .app,
  .main {
    overflow-x: hidden;
  }

  button,
  [role="button"],
  input,
  select,
  textarea {
    min-height: 48px;
  }

  input:not([type="checkbox"]):not([type="radio"]),
  select,
  textarea {
    width: 100%;
    min-width: 0 !important;
    font-size: 16px !important;
  }

  input[type="checkbox"],
  input[type="radio"] {
    width: 20px;
    height: 20px;
    flex: 0 0 20px;
  }

  textarea {
    resize: vertical;
  }

  main table:not(.responsive-race-table),
  .content table:not(.responsive-race-table),
  .table-wrap table:not(.responsive-race-table),
  .card > table:not(.responsive-race-table) {
    display: block;
    width: 100%;
    min-width: 0 !important;
    max-width: 100%;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    scrollbar-width: thin;
    -webkit-overflow-scrolling: touch;
  }

  main table:not(.responsive-race-table) th,
  main table:not(.responsive-race-table) td,
  .content table:not(.responsive-race-table) th,
  .content table:not(.responsive-race-table) td {
    white-space: nowrap;
  }

  .content [style*="display:flex"],
  main [style*="display:flex"] {
    max-width: 100%;
    flex-wrap: wrap;
  }

  .content [style*="grid-template-columns"],
  main [style*="grid-template-columns"] {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .content [style*="min-width:"],
  main [style*="min-width:"] {
    min-width: 0 !important;
  }

  .card[style*="width:"],
  [role="dialog"] .card {
    width: min(100%, calc(100vw - 24px)) !important;
    max-width: calc(100vw - 24px) !important;
  }

  .btn,
  .btn-sm,
  .btn-login {
    min-height: 48px;
    padding: 11px 14px;
  }

  .tabs {
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    -webkit-overflow-scrolling: touch;
  }

  .tab,
  .nav-item,
  .action-item {
    min-height: 48px;
  }

  .grid-4,
  .grid-3,
  .grid-2,
  .grid-12,
  .grid-21,
  .decision-grid {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .card,
  .action-center,
  .decision-brief {
    max-width: 100%;
  }

  .topbar {
    gap: 10px;
  }

  .topbar > .flex:last-child {
    min-width: 0;
    justify-content: flex-end;
  }

  #topbar-date {
    display: none;
  }

  .chart-wrap {
    aspect-ratio: 4 / 3;
    min-height: 200px;
  }

  .race-tabs {
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    -webkit-overflow-scrolling: touch;
  }

  .race-tabs > button {
    flex: 1 0 148px;
    min-height: 48px;
  }

  #page-races {
    padding-inline: 0;
  }

  #page-races > * {
    margin-inline: 12px;
  }

  #page-races .race-table-card {
    margin-inline: 0;
    border-inline: 0;
    border-radius: 0;
  }

  .race-toolbar,
  .race-filters,
  .race-actions {
    width: 100%;
    align-items: stretch !important;
    flex-direction: column;
  }

  .race-filters > *,
  .race-actions > *,
  #relay-type-filter {
    width: 100% !important;
  }

  .race-scroll-hint {
    display: block;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    color: var(--muted);
    background: rgba(6, 182, 212, .06);
    font-size: .76rem;
    line-height: 1.45;
  }
}

/* Notched iPhones, Android cutouts and installed PWA window controls. */
@media (max-width: 900px) {
  body {
    min-height: 100dvh;
  }

  .topbar {
    padding-top: max(10px, env(safe-area-inset-top, 0px));
    padding-left: max(12px, env(safe-area-inset-left, 0px));
    padding-right: max(12px, env(safe-area-inset-right, 0px));
  }

  .content,
  main,
  .page {
    padding-left: max(10px, env(safe-area-inset-left, 0px));
    padding-right: max(10px, env(safe-area-inset-right, 0px));
  }

  .bottom-nav,
  .mobile-bottom-nav {
    padding-left: max(6px, env(safe-area-inset-left, 0px));
    padding-right: max(6px, env(safe-area-inset-right, 0px));
    padding-bottom: max(6px, env(safe-area-inset-bottom, 0px));
  }

  button,
  [role="button"],
  a.nav-item,
  .nav-btn,
  .race-split-open-btn {
    min-width: 48px;
    min-height: 48px !important;
    box-sizing: border-box;
    touch-action: manipulation;
  }
}

@media (max-width: 420px) {
  .card {
    padding: 14px;
  }

  .hero-actions,
  .form-actions,
  .modal-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .hero-actions > *,
  .form-actions > *,
  .modal-actions > * {
    width: 100%;
  }

  .content {
    padding: 10px;
  }

  .card-value {
    font-size: clamp(1.45rem, 10vw, 2rem);
  }

  .chart-wrap {
    aspect-ratio: 1 / 1;
    min-height: 188px;
  }
}

/* Fold cover screens and 320px-class phones: preserve content before decoration. */
@media (max-width: 360px) {
  .content {
    padding-inline: max(8px, env(safe-area-inset-left, 0px));
  }

  .card {
    padding: 12px;
    border-radius: 12px;
  }

  .responsive-race-table tbody {
    gap: 8px;
    padding: 8px;
  }

  .responsive-race-table td {
    grid-template-columns: minmax(72px, 32%) minmax(0, 1fr);
    padding: 9px 10px;
  }

  .bottom-nav .nav-label,
  .mobile-bottom-nav .nav-label {
    font-size: clamp(.56rem, 2.8vw, .68rem);
  }
}

/* Tablet portrait stays touch-first; landscape gains columns without fixed widths. */
@media (min-width: 769px) and (max-width: 1024px) {
  .grid-4,
  .grid-3 {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  .grid-2,
  .grid-12,
  .grid-21 {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .chart-wrap {
    min-height: clamp(220px, 34vw, 340px);
  }
}

@media (min-width: 900px) and (max-width: 1180px) and (orientation: landscape) {
  .grid-4,
  .grid-3 {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }
}

/* Includes 1024px tablets where the persistent sidebar leaves <800px content. */
@media (max-width: 1180px) {
  .responsive-race-table,
  .responsive-race-table thead,
  .responsive-race-table tbody,
  .responsive-race-table tr,
  .responsive-race-table th,
  .responsive-race-table td {
    display: block;
  }

  .responsive-race-table {
    min-width: 0 !important;
    width: 100% !important;
    border-collapse: separate;
    border-spacing: 0;
  }

  .responsive-race-table thead {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
  }

  .responsive-race-table tbody {
    display: grid;
    gap: 12px;
    padding: 12px;
  }

  .responsive-race-table tr {
    border: 1px solid var(--border);
    border-radius: 14px;
    background: rgba(15, 25, 35, .42);
    box-shadow: var(--shadow);
    overflow: hidden;
  }

  .responsive-race-table td {
    display: grid;
    grid-template-columns: minmax(82px, 34%) minmax(0, 1fr);
    gap: 10px;
    align-items: start;
    min-height: 42px;
    padding: 10px 12px;
    white-space: normal !important;
    overflow-wrap: anywhere;
    border-bottom: 1px solid rgba(45, 62, 80, .55);
  }

  .responsive-race-table td:last-child {
    border-bottom: 0;
  }

  .responsive-race-table td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: .68rem;
    font-weight: 800;
    letter-spacing: .04em;
    text-transform: uppercase;
  }

  .responsive-race-table td[colspan] {
    display: block;
    text-align: center;
  }

  .responsive-race-table td[colspan]::before {
    content: none;
  }

  .race-athlete-cell,
  .race-competition-cell {
    max-width: none !important;
    white-space: normal !important;
  }

  .race-time-cell {
    font-size: 1rem;
  }

  .race-row-actions {
    display: grid !important;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
    white-space: normal !important;
  }

  .race-row-actions::before {
    grid-column: 1 / -1;
  }

  .race-row-actions .btn {
    width: 100%;
  }
}
