/*
 * Responsive layer for the investigation workspace.
 *
 * This file deliberately contains only additive overrides.  It is loaded after
 * app.css so the product's desktop information hierarchy stays intact while
 * narrow viewports get a single-column, touch-friendly presentation.
 */

/* Grid and flex children otherwise retain an intrinsic minimum based on long
 * filenames, UUIDs, URLs, and model identifiers.  Let content wrap instead of
 * making the document wider than the viewport. */
:where(#main, .workspace, .card, .evidence-detail-grid, .review-grid,
  .cc-grid, .atlas, .atlas__workspace, .atlas__stage, .atlas__inspector,
  .interview__columns, .companion__grid, .rs-layout-with-rail) {
  min-inline-size: 0;
}

:where(img, svg, video, audio, canvas, iframe, embed, object) {
  max-inline-size: 100%;
}

:where(.rs-mono, .sha-full, .provider-card__host, .report__mono,
  .annotation-ocr, .annotation-row, .annotation-review, .rs-pre, pre, code) {
  overflow-wrap: anywhere;
  word-break: break-word;
}

/* Controls size to their containers before a long placeholder/value can force
 * an overflow.  Checkbox/radio controls retain their compact native size. */
:where(input:not([type="checkbox"]):not([type="radio"]), select, textarea) {
  box-sizing: border-box;
  max-inline-size: 100%;
}

.rs-table-wrap {
  max-inline-size: 100%;
  overscroll-behavior-inline: contain;
  -webkit-overflow-scrolling: touch;
}

/* A couple of reports render a table without the shared wrapper.  The table
 * itself becomes the narrowly scoped horizontal scroller; page-level scrolling
 * is never suppressed. */
@media (max-width: 600px) {
  table.rs-table.report__table {
    display: block;
    inline-size: 100%;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    -webkit-overflow-scrolling: touch;
  }
}

/* The JS enhancement adds this class only after it has created a real button
 * and wired aria-expanded/hidden.  JS-off keeps app.css's wrapping navigation
 * as a complete, keyboard reachable fallback. */
.responsive-nav-toggle { display: none; }

/* The script groups the existing sibling sections here.  At desktop widths it
 * replicates the original flex-column behaviour, including the existing
 * spacer that keeps account controls at the bottom of the rail. */
.responsive-nav-drawer {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  min-block-size: 0;
}

/* Do not rely on a browser's UA [hidden] rule: the flex declaration above is
 * author CSS and can otherwise win in some cascade arrangements. */
.responsive-nav-drawer[hidden] { display: none !important; }

@media (max-width: 960px) {
  /* This slightly precedes app.css's 860px rail breakpoint.  It also covers
   * unfolded narrow foldables, where a desktop rail could otherwise straddle
   * the hinge before the layout has room for both rail and reading column. */
  .app-shell:not(.app-shell--plain) { grid-template-columns: 1fr; }
  .app-shell:not(.app-shell--plain) #main {
    padding: var(--rs-space-4) var(--rs-space-3) var(--rs-space-7);
  }

  .sidenav.sidenav--compact {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    position: static;
    height: auto;
    overflow: visible;
    padding: max(var(--rs-space-2), env(safe-area-inset-top))
      max(var(--rs-space-3), env(safe-area-inset-right))
      var(--rs-space-2) max(var(--rs-space-3), env(safe-area-inset-left));
  }

  .sidenav.sidenav--compact .sidenav__brand {
    min-inline-size: 0;
    align-self: center;
  }

  .sidenav.sidenav--compact .responsive-nav-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-block-size: 44px;
    padding-inline: var(--rs-space-3);
    border: 1px solid var(--rs-line);
    border-radius: var(--rs-radius-md);
    color: var(--rs-ink);
    background: var(--rs-surface);
    font: inherit;
    font-size: var(--rs-text-sm);
    font-weight: 600;
    cursor: pointer;
  }

  .sidenav.sidenav--compact .responsive-nav-drawer {
    display: flex;
    flex-direction: column;
    grid-column: 1 / -1;
    inline-size: 100%;
    margin: var(--rs-space-1) 0 0;
  }

  .sidenav.sidenav--compact .sidenav__group,
  .sidenav.sidenav--compact .sidenav__ctx nav {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 13rem), 1fr));
    gap: 2px;
  }

  .sidenav.sidenav--compact .sidenav__group a {
    min-block-size: 44px;
    box-sizing: border-box;
    align-items: center;
  }

  .sidenav.sidenav--compact .sidenav__ctx {
    border-top: 1px solid var(--rs-line);
    padding-top: var(--rs-space-2);
  }

  .sidenav.sidenav--compact .sidenav__foot {
    margin-left: 0;
    border-top: 1px solid var(--rs-line);
  }
}

@media (max-width: 680px) {
  .app-shell:not(.app-shell--plain) #main {
    padding-inline: max(var(--rs-space-3), env(safe-area-inset-left));
    padding-inline-end: max(var(--rs-space-3), env(safe-area-inset-right));
  }

  .case-head h1, .cc-head h1 { overflow-wrap: anywhere; }

  .card { padding-inline: var(--rs-space-3); }

  .cc-case__top, .report__bar, .evidence-page-nav, .image-annotation__toolbar,
  .annotation-review__actions, .obs-form__row, .review-actions {
    align-items: stretch;
  }

  .report__bar, .evidence-page-nav, .image-annotation__toolbar,
  .annotation-review__actions, .obs-form__row {
    flex-direction: column;
  }

  .report__actions, .image-annotation__toolbar, .annotation-review__actions,
  .obs-actions, .report__inline { flex-wrap: wrap; }

  .evidence-page-nav .rs-btn, .annotation-review__actions .rs-btn,
  .obs-form__row .rs-btn { justify-content: center; }

  .tl-event { display: grid; grid-template-columns: 1fr; gap: var(--rs-space-1); }
  .tl-when { flex-basis: auto; }

  .facts-table th { white-space: normal; width: 8rem; }

  .image-annotation__coords {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--rs-space-2);
  }

  /* Keep the canvas sized by the rendered image.  A definite 100% inline
   * size combined with the existing 420px max-height can squash portrait
   * evidence, and would make normalized annotation coordinates misleading. */
  .evidence-image-canvas { max-inline-size: 100%; }
  .evidence-preview:not([width]) {
    inline-size: auto;
    max-inline-size: 100%;
    block-size: auto;
  }
  .image-annotation__overlay { max-inline-size: 100%; }

  .annotation-triage__grid { grid-template-columns: 1fr; }
  .annotation-triage__item dl { grid-template-columns: minmax(5rem, auto) minmax(0, 1fr); }

  .report__inds { grid-template-columns: 1fr; }
  .report__sec h2 { align-items: flex-start; }

  .atlas__toolbar { align-items: stretch; }
  .atlas__lenses { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .atlas__lens { min-block-size: 44px; justify-content: center; }
  .atlas__canvas { height: clamp(19rem, 62svh, 32rem); }
  .atlas__legend { position: static; max-inline-size: none; margin-top: var(--rs-space-2); }
  .atlas__activity { max-height: 14rem; }
  .atlas__rail { display: grid; grid-template-columns: 1fr; }
  .atlas__rail-title { grid-column: auto; }

  .interview-line { max-inline-size: 100%; }
}

/* Coarse-pointer devices get reliably tappable controls without changing the
 * compact desktop density for mouse/keyboard users. */
@media (pointer: coarse) {
  :where(.rs-btn, button:not(.linklike), .topbar a, .breadcrumb a) {
    min-block-size: 44px;
  }

  :where(input:not([type="checkbox"]):not([type="radio"]), select) {
    min-block-size: 44px;
  }
}

/* Foldable browsers expose a second segment only when they know its geometry.
 * Put navigation and the active work surface in distinct continuous segments,
 * with the physical hinge as the grid gap.  This is structural rather than a
 * device-name fork, and ordinary responsive layout remains the fallback. */
@supports (width: env(viewport-segment-width 0 0)) {
  @media (horizontal-viewport-segments: 2) {
    .app-shell:not(.app-shell--plain) {
      grid-template-columns:
        minmax(0, env(viewport-segment-width 0 0))
        minmax(0, env(viewport-segment-width 1 0));
      column-gap: max(0px, calc(env(viewport-segment-left 1 0) - env(viewport-segment-right 0 0)));
      align-items: stretch;
    }

    .app-shell:not(.app-shell--plain) .sidenav {
      grid-column: 1;
      grid-row: 1;
      position: sticky;
      top: 0;
      display: flex;
      flex-direction: column;
      flex-wrap: nowrap;
      align-self: start;
      block-size: 100dvh;
      min-inline-size: 0;
      overflow-y: auto;
    }

    .app-shell:not(.app-shell--plain) .app-body {
      grid-column: 2;
      grid-row: 1;
      min-inline-size: 0;
    }

    .app-shell:not(.app-shell--plain) #main {
      max-inline-size: none;
      padding: var(--rs-space-4) var(--rs-space-3) var(--rs-space-7);
    }

    /* app-body becomes the relevant measure: viewport queries alone do not
     * see that a two-segment device has only a narrow reading segment. */
    .app-shell:not(.app-shell--plain) .app-body {
      container: workpane / inline-size;
    }

    .atlas__canvas { min-inline-size: 0; }
  }
}

@container workpane (max-width: 640px) {
  .grid2, .review-grid, .evidence-detail-grid, .cc-grid,
  .interview__columns, .atlas__workspace { grid-template-columns: 1fr; }

  .tl-event { display: grid; grid-template-columns: 1fr; gap: var(--rs-space-1); }
  .tl-when { flex-basis: auto; }
  .image-annotation__coords, .annotation-triage__grid, .report__inds {
    grid-template-columns: 1fr;
  }

  .report__bar, .evidence-page-nav, .image-annotation__toolbar,
  .annotation-review__actions, .obs-form__row { flex-direction: column; }

  .atlas__lenses { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .atlas__lens { min-block-size: 44px; justify-content: center; }
  .atlas__rail { max-block-size: none; }
  .atlas__activity { position: static; inline-size: auto; margin-top: var(--rs-space-2); }
  .atlas__legend { position: static; max-inline-size: none; margin-top: var(--rs-space-2); }
  .atlas__canvas { block-size: clamp(19rem, 62svh, 32rem); }
}

@media (prefers-reduced-motion: reduce) {
  .responsive-nav-toggle { transition: none; }
}
