/* Tablet and phone layouts */
@media (max-width: 980px) {
  .count-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 820px) {
  .toolbar {
    position: static;
  }

  .numbered-row,
  .split-row {
    grid-template-columns: 2rem minmax(0, 1fr);
  }

  .numbered-row > .label-text,
  .numbered-row > input,
  .numbered-row > textarea,
  .numbered-row > select,
  .numbered-row > .compound-fields,
  .split-row label,
  .count-grid,
  .stacked-fields,
  .map-grid,
  .sketch-wrap,
  .signature-grid,
  .compound-fields,
  .mode-control,
  .gps-panel {
    grid-column: 2;
  }

  .map-grid,
  .compound-fields,
  .gps-grid,
  .measurement-grid,
  .water-detail-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .signature-grid,
  .samples-heading {
    grid-template-columns: 1fr;
  }

  #appShell[data-active-mode="standard"] .standard-single-row {
    grid-template-columns: 2rem minmax(0, 1fr);
  }
}

@media (max-width: 700px) {
  .mode-selector {
    place-items: start center;
    padding: 0;
    background: linear-gradient(180deg, #fffdf8 0%, #f5ecd5 100%);
  }

  .mode-panel {
    min-height: 100vh;
    min-height: 100dvh;
    padding: max(1rem, env(safe-area-inset-top)) 1rem
      max(1.5rem, env(safe-area-inset-bottom));
    border-right: 0;
    border-left: 0;
    border-radius: 0;
    box-shadow: none;
  }

  .mode-panel-header,
  .app-header {
    grid-template-columns: minmax(0, 1fr) 140px;
    gap: 0.65rem 0.85rem;
  }

  .mode-panel-header > div,
  .app-header > .header-copy {
    display: contents;
  }

  .mode-eyebrow,
  .app-eyebrow {
    grid-column: 1;
    grid-row: 1;
    align-self: center;
  }

  .brand-logo-large,
  .header-logo {
    grid-column: 2;
    grid-row: 1;
    width: 140px;
    max-width: 100%;
    justify-self: end;
    align-self: start;
  }

  .mode-panel-header h1,
  .app-header h1 {
    grid-column: 1 / -1;
    grid-row: 2;
    max-width: none;
    margin: 0.2rem 0 0;
    line-height: 1.06;
  }

  .mode-panel-header h1 {
    font-size: clamp(1.9rem, 8vw, 2.4rem);
  }

  .app-header h1 {
    font-size: clamp(1.75rem, 7.2vw, 2.15rem);
  }

  .mode-panel-header p:not(.eyebrow),
  .subtitle {
    grid-column: 1 / -1;
    grid-row: 3;
    max-width: 36rem;
    margin: 0.4rem 0 0;
    font-size: 0.98rem;
    line-height: 1.42;
  }

  .status-group {
    grid-column: 1 / -1;
    grid-row: 4;
    display: grid;
    gap: 0.5rem;
    margin-top: 0.7rem;
    padding: 0.6rem 0.65rem;
  }

  .status-context {
    width: 100%;
  }

  .status-feedback {
    width: 100%;
    justify-content: space-between;
    padding-top: 0.5rem;
    border-top: 1px solid rgba(24, 52, 135, 0.12);
  }

  .mode-grid {
    grid-template-columns: 1fr;
    gap: 0.8rem;
    margin-top: 1rem;
  }

  .mode-card {
    min-height: 0;
    gap: 0.45rem;
    padding: 1rem 1.05rem;
  }

  .mode-card strong {
    font-size: 1.45rem;
  }

  .mode-card-action {
    margin-top: 0.25rem;
  }

  .mode-privacy {
    margin-top: 0.8rem;
  }

  .app-header {
    min-height: 0;
    padding: max(1rem, env(safe-area-inset-top)) 1rem 1.05rem;
  }

  .tabbar {
    padding-top: 0.55rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .tab {
    white-space: nowrap;
  }

  /* Wide measurement tables scroll inside their own container and never
     increase the page width. */
  .water-table-scroll {
    margin-inline: 0;
    border-radius: 12px;
  }

  .water-data-table {
    display: table;
    width: 100%;
    min-width: 1120px;
  }

  .water-measurement-table {
    min-width: 1160px;
  }

  .water-summary-table {
    min-width: 1040px;
  }

  .water-data-table thead {
    display: table-header-group;
  }

  .water-data-table tbody {
    display: table-row-group;
  }

  .water-data-table tr {
    display: table-row;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    box-shadow: none;
  }

  .water-data-table th,
  .water-data-table td {
    display: table-cell;
    width: auto;
    border: 1px solid var(--line-strong);
  }

  .water-data-table td::before {
    content: none;
  }
}

@media (max-width: 620px) {
  main {
    padding: 0.75rem 0.75rem 1.5rem;
  }

  .view {
    width: 100%;
    max-width: 100%;
    border: 1px solid rgba(127, 149, 171, 0.32);
    border-radius: 16px;
  }

  .document-heading {
    padding: 1.1rem 1rem 0.45rem;
  }

  .document-heading h2 {
    font-size: 1.55rem;
    line-height: 1.12;
  }

  .document-heading .document-note {
    margin-top: 0.55rem;
    font-size: 0.86rem;
  }

  .protocol-page {
    padding: 0.5rem 1rem 1.25rem;
  }

  fieldset {
    margin: 0.85rem 0 1.5rem;
  }

  legend {
    margin-bottom: 0.8rem;
    font-size: 1.08rem;
    line-height: 1.25;
  }

  .water-form-grid,
  .measurement-grid,
  .water-detail-grid,
  .water-signature-grid,
  .sample-meta,
  .count-grid,
  .map-grid,
  .compound-fields,
  .gps-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .water-field-wide {
    grid-column: auto;
  }

  .water-field,
  .water-signature-grid label,
  .split-row label,
  .stacked-fields label,
  .count-grid label,
  .map-grid label,
  .compound-fields label {
    font-size: 0.95rem;
  }

  input,
  textarea,
  select {
    min-height: 46px;
    padding: 0.72rem 0.78rem;
    border-radius: 11px;
  }

  textarea {
    min-height: 88px;
  }

  .toolbar {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 0.55rem;
    padding: 0.65rem 0.75rem;
  }

  .toolbar-primary {
    min-height: 50px;
    padding-inline: 0.8rem;
    font-size: 0.96rem;
  }

  .toolbar-menu-toggle {
    min-width: 104px;
    min-height: 50px;
    padding-inline: 0.75rem;
  }

  .toolbar-menu-panel {
    width: min(350px, calc(100vw - 1.5rem));
  }

  .sketch-label {
    align-items: flex-start;
    flex-direction: column;
  }

  .sketch-label .button {
    flex: 0 0 auto;
    width: auto;
  }

  .table-scroll {
    padding-inline: 0.7rem;
    overflow: visible;
  }

  /* The solid sample list becomes a card list on phones. */
  #sampleTable,
  #sampleTable tbody,
  #sampleTable tr,
  #sampleTable td {
    display: block;
    width: 100%;
    min-width: 0;
  }

  #sampleTable thead {
    display: none;
  }

  #sampleTable tr {
    margin-bottom: 1rem;
    padding: 0.45rem;
    border: 1px solid var(--line-strong);
    border-radius: 14px;
    background: #fff;
    box-shadow: 0 8px 18px rgba(35, 48, 61, 0.05);
  }

  #sampleTable td {
    display: grid;
    grid-template-columns: minmax(112px, 0.7fr) minmax(0, 1fr);
    align-items: start;
    gap: 0.5rem;
    padding: 0.45rem;
    border: 0;
    border-bottom: 1px solid var(--line);
  }

  #sampleTable td::before {
    content: attr(data-label);
    padding-top: 0.55rem;
    color: var(--muted);
    font-size: 0.76rem;
    font-weight: 800;
  }

  #sampleTable td:last-child {
    border-bottom: 0;
  }

  .row-actions-column {
    width: auto;
  }

  .gps-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .gps-actions .button {
    width: 100%;
  }

  #appShell[data-active-mode="standard"] .count-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .water-detail-panel {
    padding: 0.85rem;
    border-radius: 14px;
  }

  .selector-footer {
    align-items: stretch;
    flex-direction: column;
  }

  .pdf-dialog {
    width: calc(100vw - 1rem);
    max-height: calc(100dvh - 1rem);
  }
}

@media (max-width: 420px) {
  .mode-panel-header,
  .app-header {
    grid-template-columns: minmax(0, 1fr) 124px;
  }

  .brand-logo-large,
  .header-logo {
    width: 124px;
  }

  .mode-panel-header h1 {
    font-size: 1.9rem;
  }

  .app-header h1 {
    font-size: 1.72rem;
  }

  .mode-panel-header p:not(.eyebrow),
  .subtitle {
    font-size: 0.94rem;
  }

  .status-context-item {
    padding: 0;
    font-size: 0.73rem;
  }

  .status-context-item + .status-context-item {
    margin-left: 0.45rem;
    padding-left: 0.5rem;
  }

  .status-feedback {
    align-items: center;
    flex-direction: row;
    gap: 0.45rem;
  }
}

@media (max-width: 700px) {
  .probe-manager {
    margin: 0 0.75rem 1rem;
    padding: 1rem;
    border-radius: 16px;
  }

  .probe-manager-heading {
    align-items: flex-start;
  }

  .probe-manager-controls {
    grid-template-columns: minmax(0, 1fr);
  }

  .probe-card-list {
    grid-template-columns: minmax(0, 1fr);
  }

  .probe-manager-actions {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
  }

  .probe-manager-actions .button {
    width: 100%;
    min-height: 52px;
  }
}

@media (max-width: 430px) {
  .probe-manager-heading {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .probe-card {
    align-items: flex-start;
  }
}
