/**
 * Diagnostic phases 01–05 — mobile / narrow layout fixes (no redesign).
 * Load after page-local styles. Safe-area + compact chrome + content padding.
 */

@media (max-width: 960px) {
  :root {
    --diag-bottom-pad: max(28px, env(safe-area-inset-bottom, 0px));
  }

  #flow {
    padding-bottom: var(--diag-bottom-pad);
  }

  /* Legacy layout only (before diag chrome): stacked columns */
  .capture-layout:not(.capture-layout--chrome) {
    display: flex;
    flex-direction: column;
  }

  .capture-layout:not(.capture-layout--chrome) .main-col {
    order: 1;
    width: 100%;
  }

  .capture-layout:not(.capture-layout--chrome) .rail {
    order: 2;
    max-height: 4.5em;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
    white-space: nowrap;
    border-bottom: 1px solid var(--border-dim, rgba(232, 224, 212, 0.06));
    padding-bottom: 6px;
    scrollbar-width: thin;
  }

  .capture-layout:not(.capture-layout--chrome) .rail .rail-item {
    display: inline-block;
    white-space: nowrap;
    flex-shrink: 0;
  }

  .capture-layout:not(.capture-layout--chrome) .rail .rail-item::after {
    content: '·';
    margin: 0 6px;
    color: var(--border, rgba(154, 130, 96, 0.18));
  }

  .capture-layout:not(.capture-layout--chrome) .rail .rail-item:last-child::after {
    content: none;
  }

  .capture-layout:not(.capture-layout--chrome) .live-panel {
    order: 3;
    margin-top: 4px;
  }

  .capture-layout--chrome {
    display: grid !important;
  }

  .live-panel-head {
    padding: 12px 14px 8px;
  }

  .live-field {
    padding: 10px 14px;
  }
}

@media (max-width: 768px) {
  :root {
    --diag-nav-offset: 0px;
  }

  /* Phases 02–05: horizontal + bottom safe area (no fixed global nav) */
  body:not(:has(.global-nav)) {
    padding-left: max(16px, env(safe-area-inset-left, 0px));
    padding-right: max(16px, env(safe-area-inset-right, 0px));
    padding-bottom: max(100px, calc(72px + env(safe-area-inset-bottom, 0px)));
  }

  /* —— Capture: fixed global nav + safe area —— */
  .global-nav {
    padding-left: max(16px, env(safe-area-inset-left, 0px));
    padding-right: max(16px, env(safe-area-inset-right, 0px));
    padding-top: max(12px, env(safe-area-inset-top, 0px));
    padding-bottom: 10px;
  }

  body:has(.global-nav) {
    padding-top: max(56px, calc(env(safe-area-inset-top, 0px) + 44px));
    padding-left: max(12px, env(safe-area-inset-left, 0px));
    padding-right: max(12px, env(safe-area-inset-right, 0px));
    padding-bottom: max(100px, calc(80px + env(safe-area-inset-bottom, 0px)));
  }

  #flow .step.active,
  #steps-root .step.active {
    scroll-margin-top: max(72px, calc(env(safe-area-inset-top, 0px) + 56px));
  }

  #summary {
    scroll-margin-top: max(72px, calc(env(safe-area-inset-top, 0px) + 56px));
  }

  /* —— Phases 02–05: compact sticky top strip + menu —— */
  .top-nav.diag-phase-top-nav {
    position: sticky;
    top: 0;
    z-index: 40;
    flex-wrap: nowrap;
    align-items: center;
    gap: 8px;
    padding: max(8px, env(safe-area-inset-top, 0px)) max(12px, env(safe-area-inset-right, 0px)) 8px
      max(12px, env(safe-area-inset-left, 0px));
    margin-left: max(-12px, calc(-1 * env(safe-area-inset-left, 0px)));
    margin-right: max(-12px, calc(-1 * env(safe-area-inset-right, 0px)));
  }

  .top-nav.diag-phase-top-nav .nav-brand {
    flex-shrink: 0;
  }

  .diag-top-nav-details {
    position: relative;
    margin-left: auto;
    flex: 1;
    min-width: 0;
    border: 0;
  }

  .diag-top-nav-more {
    list-style: none;
    cursor: pointer;
    font-family: var(--mono, 'DM Mono', ui-monospace, monospace);
    font-size: 8px;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    color: var(--gold, #9a8260);
    padding: 8px 10px;
    border: 1px solid var(--border-dim, rgba(232, 224, 212, 0.06));
    border-radius: var(--radius, 6px);
    text-align: center;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
  }

  .diag-top-nav-more::-webkit-details-marker {
    display: none;
  }

  .diag-top-nav-dropdown {
    position: absolute;
    top: 100%;
    right: 0;
    left: auto;
    margin-top: 6px;
    min-width: min(100vw - 24px, 260px);
    padding: 10px 12px;
    border: 1px solid var(--border-dim, rgba(232, 224, 212, 0.08));
    border-radius: var(--radius, 6px);
    background: var(--surface-raised, rgba(32, 28, 24, 0.98));
    max-height: min(55vh, 320px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    z-index: 50;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  }

  .diag-top-nav-dropdown .top-nav-links {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
    justify-content: flex-start;
  }

  .progress-bar-wrap {
    margin-bottom: 20px;
  }

  .progress-meta {
    margin-bottom: 6px;
  }

  .progress-label {
    font-size: 8px;
    letter-spacing: 0.18em;
  }

  .progress-count {
    font-size: 11px;
  }

  .instinct-block {
    margin-bottom: 24px;
    padding-bottom: 20px;
  }

  .sh-global-map-wrap {
    margin-bottom: 4px;
  }
}

@media (min-width: 769px) {
  .diag-top-nav-details > .diag-top-nav-more {
    display: none !important;
  }

  .diag-top-nav-details {
    display: contents;
  }

  .diag-top-nav-dropdown {
    display: contents;
  }
}

/* —— Capture (phase 01): single-thread mobile focus (≤768px) —— */
@media (max-width: 768px) {
  .page-capture #flow.flow--chrome {
    display: flex;
    flex-direction: column;
  }

  .page-capture #flow.flow--chrome #steps-root {
    order: 2;
    width: 100%;
  }

  .page-capture #flow.flow--chrome .diag-progress-stack {
    order: 1;
    width: 100%;
    margin-top: 0;
    padding-top: 0;
    border-top: none;
  }

  .page-capture .diag-details--steps,
  .page-capture .diag-details--phases {
    display: none !important;
  }

  .page-capture .progress-bar-wrap--sticky-mobile {
    position: sticky;
    top: max(52px, calc(env(safe-area-inset-top, 0px) + 44px));
    z-index: 35;
    margin-bottom: 12px;
    padding-bottom: 10px;
    background: linear-gradient(
      180deg,
      rgba(12, 10, 9, 0.97) 0%,
      rgba(12, 10, 9, 0.92) 70%,
      rgba(12, 10, 9, 0.78) 100%
    );
    border-bottom: 1px solid rgba(232, 224, 212, 0.06);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
  }

  .page-capture .live-read-drawer-toggle {
    display: inline-flex;
    align-self: flex-start;
    align-items: center;
    justify-content: center;
    margin: 0 0 14px;
    padding: 8px 12px;
    font-family: var(--mono, 'DM Mono', ui-monospace, monospace);
    font-size: 8px;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    color: var(--gold, #9a8260);
    background: rgba(8, 8, 7, 0.55);
    border: 1px solid rgba(232, 224, 212, 0.1);
    border-radius: var(--radius, 6px);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }

  /* Live read: off-canvas until opened (overrides chrome ≤960 bottom sheet default) */
  .page-capture:not(.capture-live-read-open) .live-panel--chrome {
    transform: translateY(110%) !important;
    visibility: hidden;
    pointer-events: none;
    transition: transform 0.4s cubic-bezier(0.22, 1, 0.36, 1), visibility 0.4s;
  }

  .page-capture.capture-live-read-open .live-panel--chrome {
    transform: translateY(0) !important;
    visibility: visible;
    pointer-events: auto;
    transition: transform 0.4s cubic-bezier(0.22, 1, 0.36, 1), visibility 0.4s;
  }

  .page-capture:not(.capture-live-read-open).diag-has-live-chrome #flow.flow--chrome {
    padding-bottom: max(28px, env(safe-area-inset-bottom, 0px));
  }

  .page-capture.capture-live-read-open.diag-live-open #flow.flow--chrome {
    padding-bottom: max(120px, calc(72px + env(safe-area-inset-bottom, 0px)));
  }

  .live-read-backdrop {
    position: fixed;
    inset: 0;
    z-index: 84;
    background: rgba(4, 3, 2, 0.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.35s ease;
  }

  .page-capture.capture-live-read-open .live-read-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  .page-capture #phase-header .sys-actions--capture {
    align-items: stretch;
    width: 100%;
    max-width: 360px;
    margin-left: auto;
    margin-right: auto;
  }

  .page-capture #phase-header .sys-actions__primary .sys-btn--pri {
    width: 100%;
    justify-content: center;
  }

  .page-capture .sys-actions-more {
    border: 1px solid rgba(232, 224, 212, 0.08);
    border-radius: var(--radius, 6px);
    background: rgba(8, 8, 7, 0.35);
    margin-top: 4px;
  }

  .page-capture .sys-actions-more__summary {
    list-style: none;
    cursor: pointer;
    padding: 10px 12px;
    font-family: var(--mono, 'DM Mono', ui-monospace, monospace);
    font-size: 8px;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    color: rgba(160, 152, 137, 0.85);
    text-align: center;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
  }

  .page-capture .sys-actions-more__summary::-webkit-details-marker {
    display: none;
  }

  .page-capture .sys-actions-more__body {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 0 12px 12px;
    align-items: stretch;
  }

  .page-capture .sys-actions-more__body .sys-btn,
  .page-capture .sys-actions-more__body a.sys-btn {
    width: 100%;
    justify-content: center;
  }

  .page-capture .step-header {
    margin-bottom: 22px;
  }

  .page-capture .step-instruction {
    margin-bottom: 8px;
  }

  .page-capture .step-micro {
    font-size: 10px;
    opacity: 0.82;
    margin-top: 8px;
    line-height: 1.55;
  }

  .page-capture .option-list {
    gap: 6px;
  }

  .page-capture .option-item {
    padding: 14px 16px;
  }

  .page-capture .nav-row {
    margin-top: 24px;
  }

  .page-capture #flow .step.active,
  .page-capture #steps-root .step.active {
    scroll-margin-top: max(140px, calc(env(safe-area-inset-top) + 120px));
  }
}

@media (min-width: 769px) {
  .page-capture .live-panel--chrome {
    transform: none !important;
    visibility: visible !important;
    pointer-events: auto !important;
  }

  .page-capture .live-read-drawer-toggle {
    display: none !important;
  }

  .page-capture .live-read-backdrop {
    display: none !important;
  }

  .page-capture #phase-header .sys-actions-more > .sys-actions-more__summary {
    display: none !important;
  }

  .page-capture #phase-header .sys-actions-more > .sys-actions-more__body {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    padding: 0;
    border: none;
    background: transparent;
  }

  .page-capture #phase-header .sys-actions--capture {
    flex-direction: column;
    align-items: center;
  }

  .page-capture #phase-header .sys-actions-more {
    border: none;
    background: transparent;
    margin: 0;
    padding: 0;
    display: block;
    width: 100%;
    max-width: none;
  }
}
