/* Compact workbench. Desktop rules and the shared simulation stay independent.
   Keep this query in sync with COMPACT_QUERY in mobile-ui.js. */
@layer layout {
  .mobile-only { display: none; }
  .guide-panel { display: contents; }

  @media (max-width: 860px), (max-width: 1100px) and (max-height: 600px) and (pointer: coarse) {
    :root { --edge: 8px; --mobile-top: calc(68px + env(safe-area-inset-top, 0px)); --mobile-bottom: calc(68px + env(safe-area-inset-bottom, 0px)); }
    body.in-workshop { overflow: hidden; min-height: 0; height: var(--mobile-viewport-height, 100dvh); }
    .mobile-only { display: flex; }
    .app-bar {
      top: calc(8px + env(safe-area-inset-top, 0px)); left: max(8px, env(safe-area-inset-left, 0px)); right: max(8px, env(safe-area-inset-right, 0px));
      height: 52px; min-height: 52px; flex-wrap: nowrap; padding: 0 6px; gap: 4px; border-radius: 18px;
    }
    .app-bar .brand { padding: 0 4px; flex: none; }
    .brand img { width: 30px; height: 30px; }
    .brand-name, .app-bar .preview-tag, .bar-link, #mode-switch { display: none !important; }
    .case-chip { flex: 1; max-width: none; min-width: 0; padding: 0 4px; border: 0; background: transparent; gap: 6px; }
    .case-chip-avatar { width: 28px; height: 28px; }
    .case-chip-text b { font-size: 14px; }
    .case-chip-text small { font-size: 11px; }
    .bar-end { flex: none; margin-left: auto !important; gap: 0; }
    #more-menu { width: min(300px, calc(100vw - 24px)); max-height: calc(100dvh - var(--mobile-top) - 16px); overflow-y: auto; }
    #more-menu button { min-height: 44px; }

    .workspace { position: fixed; inset: 0; height: var(--mobile-viewport-height, 100dvh); min-height: 0; overflow: hidden; padding: 0; }

    /* Workbench first: the 3D scene fills the space between the app bar and the
       action bar. All panels live in one bottom sheet (collapsed, half or full),
       so the workshop is never covered by stacked windows. */
    .stage {
      --sheet-head: 60px; --sheet-body: clamp(200px, 44%, 440px);
      position: absolute; inset: var(--mobile-top) max(8px, env(safe-area-inset-right, 0px)) var(--mobile-bottom) max(8px, env(safe-area-inset-left, 0px));
      display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) var(--sheet-head) var(--sheet-body);
      grid-template-areas: "scene" "head" "panel"; gap: 0; padding: 0; min-width: 0; min-height: 0; overflow: hidden;
      transition: grid-template-rows var(--t-base) var(--ease-out);
    }
    .workspace[data-sheet="collapsed"] .stage { --sheet-body: 0px; }
    .workspace[data-sheet="full"] .stage { --sheet-body: calc(100% - var(--sheet-head) - 132px); }
    @media (prefers-reduced-motion: reduce) { .stage { transition: none; } }

    .scene { grid-area: scene; position: relative; inset: auto; width: 100%; height: 100%; min-height: 0; border-radius: 18px 18px 0 0; overflow: hidden; }
    .workspace[data-sheet="collapsed"] .scene { border-radius: 18px 18px 0 0; }
    .stage-top { display: none; }

    /* Task bar floats over the top of the scene. */
    .mobile-coach {
      grid-area: scene; align-self: start; z-index: 6; margin: 6px; align-items: center; gap: 4px; min-width: 0; padding: 4px;
      border-radius: 16px; background: var(--glass-thick); border: 1px solid var(--glass-outline); box-shadow: var(--glass-shadow);
      -webkit-backdrop-filter: blur(var(--glass-blur)) saturate(var(--glass-saturate)); backdrop-filter: blur(var(--glass-blur)) saturate(var(--glass-saturate));
    }
    .mobile-task { display: grid; gap: 1px; flex: 1; min-width: 0; min-height: 44px; padding: 2px 6px; text-align: left; border: 0; border-radius: 10px; background: none; color: var(--text); }
    .mobile-task small { font: var(--type-caption); color: var(--accent-strong); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .mobile-task b { font: 600 14px/1.25 var(--font-sans); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
    .mobile-coach .btn { min-height: 44px; padding: 0 14px; font-size: 14px; flex: none; }
    .mobile-coach .icon-btn { width: 40px; height: 44px; flex: none; }

    /* Camera views: one slim row along the bottom edge of the scene. */
    .stage-tools {
      grid-area: scene; position: relative; inset: auto; align-self: end; justify-self: stretch; margin: 6px; z-index: 5;
      display: flex; flex-direction: row; align-items: center; gap: 4px; pointer-events: none;
    }
    .stage-tools > * { pointer-events: auto; }
    .camera-tabs { display: flex; flex-direction: row; flex: 1; min-width: 0; max-width: 300px; border-radius: 14px; padding: 2px; }
    .camera-tabs > button { min-width: 0; min-height: 40px; flex: 1; padding: 0 3px; font-size: 13px; }
    .stage-tools > .icon-btn { width: 44px; height: 44px; flex: none; border-radius: 14px; margin-left: auto; }

    /* 3D tools on demand: one button under the task bar opens the column. */
    .stage-bottom {
      grid-area: scene; position: relative; inset: auto; transform: none; align-self: start; justify-self: end; z-index: 5;
      margin: 66px 6px 6px; display: flex; flex-direction: row-reverse; align-items: stretch; gap: 4px; pointer-events: none;
    }
    .workspace.mobile-tools-open .stage-bottom { justify-self: stretch; }
    .stage-bottom > * { pointer-events: auto; }
    #mobile-tools-toggle { width: 44px; height: 44px; border-radius: 14px; flex: none; }
    .workspace.mobile-tools-open #mobile-tools-toggle { background: var(--accent); color: var(--on-accent); }
    .stage-bottom .tool-strip, .stage-bottom #connection-toggle { display: none; }
    /* Open tools form one row under the task bar, clear of the camera views. */
    .workspace.mobile-tools-open .stage-bottom .tool-strip { display: flex; flex-direction: row; flex: 1; min-width: 0; border-radius: 14px; padding: 2px; }
    .workspace.mobile-tools-open .stage-bottom #connection-toggle { display: flex; }
    .tool-strip > button { flex: 1; min-width: 0; min-height: 44px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; border-radius: 10px; padding: 2px; }
    .tool-strip > button span:not([data-icon]) { position: static; width: auto; height: auto; clip: auto; font-size: 10px; line-height: 1.1; }
    .tool-strip .icon { width: 16px; height: 16px; }
    #connection-toggle { width: 52px; height: auto; min-height: 44px; flex: none; flex-direction: column; justify-content: center; gap: 1px; border-radius: 14px; padding: 0; }
    #connection-toggle > span:not([data-icon]) { display: none; }
    #connection-toggle .icon { width: 16px; height: 16px; }
    #connection-toggle::after { content: "Link"; font-size: 10px; line-height: 1.1; }
    #connection-toggle[aria-pressed="true"]::after { content: "Linked"; }
    /* Screen-space desktop helpers would cover the small scene; the plane and
       Key facts controls stay reachable in Adjust and Parts. */
    #part-bubbles, #gap-label, #shelf-label { display: none !important; }
    /* Shelf picker: the button keeps to the shelf corner; open, it is one bar along
       the bottom of the workbench while the sheet stays collapsed. */
    .shelf-picker { z-index: 7; }
    .shelf-picker-toggle { min-height: 36px; padding: 0 10px 0 8px; font-size: 13px; }
    .shelf-picker-panel { width: 100%; padding: 8px 8px 8px 12px; gap: 6px; border-radius: 16px; transform-origin: bottom center; animation-name: sheet-in; }
    .picker-head { align-items: center; }
    .picker-head small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .shelf-picker-panel .btn.small { min-height: 40px; }
    .picker-item { grid-template-columns: minmax(0, 1fr) auto; align-items: center; padding-top: 6px; }
    .picker-text b { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .picker-text p, .picker-ifu, .picker-label { display: none; }
    .picker-actions { flex-wrap: nowrap; }
    .picker-empty { padding-top: 6px; min-height: 47px; display: flex; align-items: center; }
    .workspace.shelf-picking :is(.stage-tools, .stage-bottom) { display: none !important; }

    /* Bottom sheet: handle, tabs and a collapse button form its header. */
    .mobile-sheet-head {
      grid-area: head; position: relative; z-index: 4; display: grid; grid-template-columns: minmax(0, 1fr) 44px; grid-template-rows: 14px 44px;
      grid-template-areas: "handle handle" "tabs toggle"; align-items: center; column-gap: 4px; padding: 0 4px 2px;
      background: var(--surface); border: 1px solid var(--separator); border-bottom: 0; border-radius: 18px 18px 0 0; box-shadow: 0 -6px 18px rgba(17, 26, 20, .08);
      touch-action: none;
    }
    .mobile-sheet-handle { grid-area: handle; align-self: stretch; display: grid; place-items: center; border: 0; padding: 0; background: none; cursor: grab; min-height: 0; }
    .mobile-sheet-handle span { width: 40px; height: 5px; border-radius: 3px; background: var(--separator-strong); }
    .mobile-sheet-handle:focus-visible { box-shadow: var(--focus-ring); border-radius: 8px; }
    .mobile-tabs { grid-area: tabs; position: relative; inset: auto; margin: 0; z-index: 2; display: flex; padding: 2px; border-radius: 14px; min-width: 0; }
    .mobile-tabs > button { flex: 1; min-height: 40px; padding: 0 4px; font-size: 14px; }
    #mobile-scene-toggle { grid-area: toggle; width: 44px; height: 44px; border-radius: 12px; }
    .workspace[data-sheet="collapsed"] .mobile-sheet-head { border-bottom: 1px solid var(--separator); border-radius: 0 0 18px 18px; box-shadow: none; }
    .workspace[data-sheet="collapsed"] .scene { border-radius: 18px 18px 0 0; }

    .guide-panel, .shelf-panel, .inspector-panel {
      grid-area: panel; position: relative; inset: auto; width: 100% !important; min-width: 0; min-height: 0; height: 100%; max-height: none;
      overflow: auto; overscroll-behavior: contain; scroll-padding: 16px; border-radius: 0 0 18px 18px; -webkit-overflow-scrolling: touch;
      background: var(--surface); border: 1px solid var(--separator); border-top: 0; box-shadow: none;
      -webkit-backdrop-filter: none; backdrop-filter: none;
    }
    .guide-panel { display: flex; flex-direction: column; gap: 12px; padding: 8px; }
    .shelf-panel, .inspector-panel { display: block; }
    .workspace[data-sheet="collapsed"] :is(.guide-panel, .shelf-panel, .inspector-panel) { display: none !important; }
    .panel-body { overflow: visible; padding: 8px 16px 16px; }
    .panel-head { padding: 10px 16px 6px; flex-wrap: wrap; }
    .panel-head h2 { font-size: 18px; flex-wrap: wrap; min-width: 0; }
    .panel-sub { overflow-wrap: anywhere; }
    .panel-toggle { display: none; }
    .stage.shelf-collapsed .shelf-panel > :not(.panel-head), .stage.inspector-collapsed .inspector-panel > :not(.panel-head) { display: block; }
    .stage.shelf-collapsed .shelf-panel .panel-head > :not(.panel-toggle), .stage.inspector-collapsed .inspector-panel .panel-head > :not(.panel-toggle) { display: flex; }
    .workspace[data-mobile-tab="step"] .shelf-panel, .workspace[data-mobile-tab="step"] .inspector-panel,
    .workspace[data-mobile-tab="shelf"] .guide-panel, .workspace[data-mobile-tab="shelf"] .inspector-panel,
    .workspace[data-mobile-tab="inspector"] .guide-panel, .workspace[data-mobile-tab="inspector"] .shelf-panel { display: none; }
    .stage-bottom-left, .measure-card { display: block !important; position: static; width: 100%; min-width: 0; max-height: none; flex: none; }
    .measure-card { order: -1; }
    /* In the tutorial the current step comes first; the patient card follows. */
    body.guided .measure-card { order: 1; }
    .measure-card .sheet-row { display: none; }
    .measure-card > button { padding: 12px; }
    .measure-card .sheet-foot { margin-top: 2px; }
    .instructor-card { display: block; padding: 16px; box-shadow: none; }
    .instructor-picture { display: none; }
    .instructor-copy > p { font-size: 15px; line-height: 1.5; }
    .instructor-copy h2 { font-size: 21px; }
    .tutorial-actions { display: none; }
    .tutorial-actions .btn { min-height: 44px; }
    .tutorial-actions [data-action="tutorial-try"] { flex: 1; }
    .mobile-gesture-help { margin: 0 8px 8px; font: var(--type-footnote); color: var(--text-secondary); }
    .tutorial-focus :is(#shelf-panel, #inspector-panel, #measurement-card, .stage-bottom) { opacity: 1; filter: none; }
    .glow-target { scroll-margin: 16px; animation: none; box-shadow: 0 0 0 3px var(--accent) !important; }
    .shelf-items.glow-target, .tutorial-choice.glow-target, .knee-ref-choice.glow-target { box-shadow: inset 3px 0 var(--accent) !important; padding-left: 8px; }
    .shelf-pick { min-width: 0; }
    .shelf-text b { white-space: normal; }
    .shelf-panel .panel-foot { padding: 12px 16px; }
    .segmented > button, .chip-btn, .btn.small, .choice-option, .instructor-copy summary, .link-btn { min-height: 44px; }
    .link-btn { display: inline-flex; align-items: center; }
    .instructor-copy summary { display: flex; align-items: center; }
    .axis-control { grid-template-columns: minmax(0, 1fr) 156px; gap: 8px; }
    .stepper { grid-template-columns: 44px minmax(0, 1fr) 44px; }
    input, select, textarea, .stepper input { font-size: 16px !important; }
    .procedure[open] { max-height: none; overflow: visible; }
    .plane-bubbles button { min-width: 0; font-size: 13px; }
    .inspector-links { gap: 4px; }

    /* Tutorial pointer: "Tap here" above or below the highlighted control. */
    #glow-pointer { z-index: 95; font-size: 13px; padding: 6px 12px; animation: none; white-space: nowrap; }
    #glow-pointer[data-side="above"] { transform: translate(-50%, calc(-100% - 8px)); }
    #glow-pointer[data-side="below"] { transform: translate(-50%, 8px); }
    #glow-pointer[data-side="above"]::before, #glow-pointer[data-side="below"]::before {
      position: absolute; left: 50%; margin: 0; border: 7px solid transparent; transform: translateX(-50%);
    }
    #glow-pointer[data-side="above"]::before { top: 100%; border-top-color: var(--accent); border-right-color: transparent; }
    #glow-pointer[data-side="below"]::before { bottom: 100%; border-bottom-color: var(--accent); border-right-color: transparent; }
    @media (prefers-reduced-motion: no-preference) {
      #glow-pointer[data-side="above"] { animation: tap-bob-above 1.4s var(--ease-in-out) infinite; }
      #glow-pointer[data-side="below"] { animation: tap-bob-below 1.4s var(--ease-in-out) infinite; }
    }
    @keyframes tap-bob-above { 0%, 100% { translate: 0 0; } 50% { translate: 0 -4px; } }
    @keyframes tap-bob-below { 0%, 100% { translate: 0 0; } 50% { translate: 0 4px; } }
    /* Foot reference: room for the pointer above the current control, so it never covers the rule or the reading help. */
    body.tutorial-focus .foot-task.is-active :is(.foot-rule-actions, .reading-row, [data-action="set-foot-mark"]) { margin-top: 40px; }

    .action-bar {
      position: absolute; top: auto; left: max(8px, env(safe-area-inset-left, 0px)); right: max(8px, env(safe-area-inset-right, 0px)); bottom: calc(8px + env(safe-area-inset-bottom, 0px));
      height: 52px; padding: 0 4px; gap: 4px; border-radius: 18px; z-index: 40;
    }
    .scope-note, .history-controls .btn { display: none; }
    .history-controls { flex: none; }
    .history-controls .icon-btn { width: 44px; height: 44px; }
    .review-controls { flex: 1; min-width: 0; gap: 4px; }
    #hint-button { flex: none; width: auto; padding: 0 12px; gap: 6px; }
    #hint-button > span:not([data-icon]) { font-size: 13px; }
    .review-controls [data-action="evaluate"] { flex: 1; min-width: 0; padding: 0 8px; gap: 4px; font-size: 13px; }
    .toast { top: calc(var(--mobile-top) + 64px); bottom: auto; width: calc(100vw - 32px); max-width: 420px; padding: 8px 12px; border-radius: 12px; font-size: 13px; pointer-events: none; }

    /* A full sheet leaves a small strip of the scene: keep only the task bar. */
    .workspace[data-sheet="full"] :is(.stage-tools, .stage-bottom) { display: none; }
    .mobile-keyboard .stage { grid-template-rows: 0 var(--sheet-head) minmax(0, 1fr); }
    .mobile-keyboard :is(.scene, .stage-tools, .stage-bottom, .mobile-coach) { display: none; }
    .mobile-keyboard .mobile-sheet-head { border-radius: 18px 18px 0 0; }

    /* Start with a clear choice, with the tutorial in the first screenful. */
    .welcome-page { display: block; padding: calc(var(--mobile-top) + 12px) 16px 24px; }
    .welcome-visual { display: none; }
    .welcome-copy { gap: 20px; }
    .welcome-copy h1 { font-size: 32px; }
    .welcome-brand { gap: 12px; }
    .welcome-brand img { width: 56px; }
    .welcome-copy .lede { font-size: 16px; line-height: 1.5; }
    .mode-grid { grid-template-columns: 1fr; }
    .mode-card { padding: 16px; }
    .welcome-scope { margin-top: 20px; }
    .resume-card { flex-wrap: wrap; }

    /* Sheets scroll as one body on short screens; the close button stays reachable. */
    .modal-backdrop { inset: 0 0 auto; height: var(--mobile-viewport-height, 100dvh); padding: 0; align-items: end; }
    .modal, .modal.wide, .modal.narrow { width: 100%; max-height: calc(var(--mobile-viewport-height, 100dvh) - env(safe-area-inset-top, 0px) - 8px); border-radius: 22px 22px 0 0; }
    .modal-header { padding: 14px 16px 10px; gap: 8px; }
    .modal-header h2 { font-size: 22px; }
    .modal-header p { font-size: 13px; }
    .modal-header .icon-btn { flex: none; }
    .modal-content { padding: 16px; overflow-x: hidden; overflow-wrap: anywhere; }
    .modal-footer { padding: 10px 16px calc(10px + env(safe-area-inset-bottom, 0px)); }
    .modal-footer { flex-wrap: wrap; gap: 8px; }
    .modal-footer .btn { flex: 1 1 40%; min-width: 0; white-space: normal; text-align: center; }
    /* With three or more actions the main one gets its own full-width row. */
    .modal-footer:has(> .btn:nth-child(3)) .btn.primary { flex-basis: 100%; order: 2; }
    .briefing, .briefing-person { grid-template-columns: 1fr; }
    .briefing-sheet { position: static; }
    .measure-sheet.full { columns: 1; }
    .source-item { flex-direction: column; align-items: stretch; }
    .copy-row, .ifu-open { min-width: 0; }
    .assessment-matrix table, .assessment-matrix tbody { display: block; }
    .assessment-matrix caption { display: block; }
    .assessment-matrix thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
    .assessment-matrix tr { display: grid; grid-template-columns: 1fr 1fr; padding: 12px; gap: 8px; border-bottom: 1px solid var(--separator); }
    .assessment-matrix tbody th { grid-column: 1 / -1; }
    .assessment-matrix th, .assessment-matrix td { padding: 0; border: 0; white-space: normal; overflow-wrap: anywhere; min-width: 0; }
    .assessment-matrix td::before { content: attr(data-label); display: block; font: var(--type-caption); color: var(--text-secondary); margin-bottom: 4px; }
    .assessment-matrix td:last-child { grid-column: 1 / -1; }
    .assessment-matrix td:last-child::before { display: none; }
  }
  @media (max-width: 400px) {
    .picker-more { display: none; }
    #hint-button { width: 44px; padding: 0; }
    #hint-button > span:not([data-icon]) { display: none; }
  }
  @media (max-width: 860px) and (max-height: 720px) and (orientation: portrait) {
    /* Short phones: a slightly smaller sheet keeps more of the workbench visible. */
    .stage { --sheet-head: 56px; --sheet-body: clamp(168px, 42%, 360px); }
    .mobile-sheet-head { grid-template-rows: 12px 42px; }
    .mobile-tabs > button { min-height: 40px; }
    .workspace[data-sheet="full"] .stage { --sheet-body: calc(100% - var(--sheet-head) - 112px); }
  }
  @media (max-width: 860px) and (orientation: landscape) and (max-height: 600px), (max-width: 1100px) and (max-height: 600px) and (pointer: coarse) and (orientation: landscape) {
    /* Landscape phones: workbench left; task bar and panel share the right column,
       so nothing but the camera views sits on the scene. Bars are a little flatter. */
    :root { --mobile-top: calc(56px + env(safe-area-inset-top, 0px)); --mobile-bottom: calc(56px + env(safe-area-inset-bottom, 0px)); }
    .app-bar { height: 44px; min-height: 44px; }
    .action-bar { height: 44px; }
    .action-bar :is(.icon-btn, .btn) { min-height: 40px; height: 40px; }
    .stage { --sheet-head: 52px; grid-template-columns: minmax(0, 1fr) min(46vw, 420px); grid-template-rows: auto var(--sheet-head) minmax(0, 1fr); grid-template-areas: "scene coach" "scene head" "scene panel"; column-gap: 8px; transition: none; }
    .mobile-coach { grid-area: coach; margin: 0 0 6px; }
    .stage-bottom { margin-top: 6px; }
    .mobile-sheet-head { grid-template-rows: 10px 40px; }
    .mobile-tabs > button { min-height: 38px; }
    .workspace[data-sheet="full"] .stage { --sheet-body: auto; }
    .workspace[data-sheet="collapsed"] .stage { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) var(--sheet-head); grid-template-areas: "scene" "head"; }
    .workspace[data-sheet="collapsed"] .mobile-coach { grid-area: scene; margin: 6px; }
    .workspace[data-sheet="collapsed"] .stage-bottom { margin-top: 62px; }
    .scene { border-radius: 18px; }
    .mobile-sheet-head { border-radius: 18px 18px 0 0; box-shadow: none; }
    .workspace[data-sheet="collapsed"] .scene { border-radius: 18px 18px 0 0; }
    .guide-panel, .shelf-panel, .inspector-panel { border-radius: 0 0 18px 18px; }
    .workspace[data-sheet="full"] :is(.stage-tools, .stage-bottom) { display: flex; }
    .mobile-task b { -webkit-line-clamp: 1; }
    .toast, .toast.visible { left: auto; right: max(16px, env(safe-area-inset-right, 0px)); top: auto; bottom: calc(var(--mobile-bottom) + 8px); width: min(44vw, 400px); transform: none; }
    .modal { display: block; overflow-y: auto; }
    .modal-header { position: sticky; top: 0; z-index: 2; background: var(--surface); }
    .modal-header p { display: none; }
    .modal-content { overflow: visible; }
  }
}
