    /* Detect compact screens and touch input without relying on user-agent names. */
    @media (max-width: 768px), (pointer: coarse) {
      body { font-size: 14px; }
      #sidebar-header h1, .section-toggle, .store-section-header,
      #background-panel label, #import-msg, .footer-label,
      .store-skin-item .skin-rarity { font-size: 12px; }
      .section-toggle { min-height: 44px; }
      #sidebar-header { padding: 12px 14px; }
      #model-select, #background-select, #skin-select, #store-skins-search,
      #import-input, #animation-export-dialog select, #animation-export-name {
        min-height: 44px;
        font-size: 16px;
      }
      #import-input { min-height: 80px; }
      #import-btn, #share-btn, #copy-hex-btn, #pause-anim-btn, #record-anim-btn,
      .anim-control-btn, .call-btn, #capture-btn, .gender-btn, .export-actions button {
        min-height: 44px;
        font-size: 14px;
      }
      .gender-btn { min-width: 44px; height: 44px; font-size: 20px; }
      #background-panel .background-transparency-control,
      #animation-export-dialog .export-loop-label { min-height: 44px; font-size: 14px; }
      #background-panel input[type=checkbox],
      #animation-export-dialog input[type=checkbox] {
        width: 24px;
        height: 24px;
        flex-shrink: 0;
      }
      .animation-controls { grid-template-columns: repeat(2, minmax(0, 1fr)); }
      .call-controls > .call-btn { flex-basis: calc(50% - 6px); }
      #store-skins-section { min-height: 240px; flex-shrink: 0; }
      #store-skins-content { top: 60px; }
      .store-skin-item { min-height: 44px; font-size: 14px; }
      #zone-pickers { flex-wrap: wrap; padding: 4px 0; gap: 4px; }
      .zone-swatch { width: 44px; height: 44px; flex-shrink: 0; border-radius: 6px; }
      #viewport-readout { bottom: 64px; gap: 4px; width: max-content; }
      #skin-controls { flex-wrap: wrap; }
      #skin-controls > #skin-select { width: min(130px, 100%); }
      #capture-btn { min-width: 88px; bottom: 8px; }
      #status-bar { max-width: calc(100% - 118px); font-size: 12px; padding: 5px 8px; }
      #animation-export-dialog { max-height: calc(100dvh - 24px); }
      #animation-export-dialog p { font-size: 14px; }
      #empty-state, #no-model-notice, #loading-overlay { padding: 16px; text-align: center; }
      #no-model-notice p, #no-model-notice code { overflow-wrap: anywhere; }
    }

    @media (max-width: 768px) {
      #sidebar { width: clamp(180px, 32vw, 220px); min-width: 0; }
    }

    /* Phones in either orientation: one tab per sidebar section, support link pinned. */
    @media (max-width: 768px) and (orientation: portrait), (orientation: landscape) and (max-height: 500px) and (pointer: coarse) {
      #mobile-panel-bar {
        display: block;
        position: sticky;
        top: 0;
        z-index: 2;
        flex-shrink: 0;
        background: var(--sidebar-bg);
        border-bottom: 1px solid var(--border);
      }
      #mobile-panel-handle { display: none; }
      #mobile-panel-handle:focus-visible,
      #mobile-panel-tabs [role="tab"]:focus-visible {
        outline: 2px solid var(--accent);
        outline-offset: -2px;
      }
      #mobile-panel-tabs { display: flex; }
      #mobile-panel-tabs [role="tab"] {
        flex: 1;
        min-width: 0;
        min-height: 36px;
        padding: 0 4px;
        border: none;
        border-bottom: 2px solid transparent;
        background: transparent;
        color: var(--text-muted);
        font-family: inherit;
        font-size: 11px;
        font-weight: 600;
        letter-spacing: 0.06em;
        text-transform: uppercase;
        cursor: pointer;
      }
      #mobile-panel-tabs [role="tab"][aria-selected="true"] {
        color: var(--accent);
        border-bottom-color: var(--accent);
      }

      /* Tabs replace the section headers; only the selected section is shown. */
      #sidebar-header,
      #sidebar > .collapsible-section,
      #sidebar > .collapsible-section > .section-toggle { display: none; }
      #sidebar[data-mobile-tab="model-panel"] > #model-panel,
      #sidebar[data-mobile-tab="background-panel"] > #background-panel,
      #sidebar[data-mobile-tab="import-panel"] > #import-panel { display: block; border-top: none; }
      #sidebar[data-mobile-tab="store-skins-section"] > #store-skins-section:not(.hidden) {
        display: block;
        flex: 1 1 auto;
        min-height: 0;
        padding: 0;
        border-top: none;
      }
      #store-skins-content { inset: 0 0 6px; }

      /* Support link stays pinned to the panel bottom on every tab. */
      #footer-panel {
        position: sticky;
        bottom: 0;
        z-index: 2;
        flex-direction: row;
        align-items: center;
        justify-content: center;
        gap: 6px;
        flex-shrink: 0;
        margin-top: auto;
        padding: 6px 14px calc(6px + env(safe-area-inset-bottom));
      }
      #footer-panel .footer-link { min-height: 32px; display: inline-flex; align-items: center; }

      /* Smaller corner wordmark on phones. */
      #viewport-logo { top: 8px; left: 10px; font-size: 1rem; }

      /* One size step down from the generic touch sizes; inputs keep 16px text to avoid iOS zoom. */
      #model-select, #background-select, #skin-select, #store-skins-search,
      #import-input, #animation-export-dialog select, #animation-export-name { min-height: 36px; }
      #import-input { min-height: 64px; }
      #import-btn, #share-btn, #copy-hex-btn, #pause-anim-btn, #record-anim-btn,
      .anim-control-btn, .call-btn, #capture-btn, .export-actions button {
        min-height: 36px;
        font-size: 13px;
      }
      #capture-btn { min-width: 76px; }
      .gender-btn { min-width: 36px; height: 36px; min-height: 36px; font-size: 18px; }
      #background-panel .background-transparency-control,
      #animation-export-dialog .export-loop-label { min-height: 36px; font-size: 13px; }
      #background-panel input[type=checkbox],
      #animation-export-dialog input[type=checkbox] { width: 20px; height: 20px; }
      .store-skin-item { min-height: 36px; font-size: 13px; }
      .zone-swatch { width: 36px; height: 36px; }
    }

    /* Portrait phones: the sidebar becomes a resizable bottom panel. */
    @media (max-width: 768px) and (orientation: portrait) {
      #app-container { flex-direction: column; }
      #viewport { order: -1; }
      #sidebar {
        width: 100%;
        height: auto;
        flex: 0 0 45%;
        min-height: 0;
        border-right: none;
        border-top: 1px solid var(--border);
        border-radius: 12px 12px 0 0;
      }
      #sidebar[data-sheet="collapsed"] { flex-basis: auto; overflow: hidden; }
      #sidebar[data-sheet="collapsed"][data-mobile-tab] > :not(#mobile-panel-bar, #footer-panel) { display: none; }
      #mobile-panel-handle {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 24px;
        padding: 0;
        border: none;
        background: transparent;
        cursor: grab;
        touch-action: none;
      }
      #mobile-panel-handle span {
        width: 40px;
        height: 4px;
        border-radius: 2px;
        background: var(--text-muted);
      }

      /* Colors swipe in one row; gender + pattern sit left on the Capture line. */
      #viewport-readout { display: contents; }
      #zone-pickers {
        position: absolute;
        bottom: 50px;
        left: 0;
        right: 0;
        z-index: 10;
        max-width: none;
        flex-wrap: nowrap;
        justify-content: safe center;
        padding: 4px 10px;
        overflow-x: auto;
        overscroll-behavior-x: contain;
        scroll-snap-type: x proximity;
        scrollbar-width: none;
        touch-action: pan-x;
      }
      #zone-pickers::-webkit-scrollbar { display: none; }
      .zone-swatch { scroll-snap-align: start; }
      #skin-controls {
        position: absolute;
        left: 10px;
        bottom: 8px;
        z-index: 10;
        flex-wrap: nowrap;
        justify-content: flex-start;
        max-width: calc(100% - 118px);
      }
      #skin-controls > #skin-select { flex: 0 1 130px; min-width: 0; }
      #status-bar { bottom: 100px; }
    }

    /* Landscape phones: compact colors and skin controls tucked into the bottom-left corner. */
    @media (orientation: landscape) and (max-height: 500px) and (pointer: coarse) {
      #sidebar { width: clamp(200px, 30vw, 260px); min-width: 0; }
      #mobile-panel-tabs [role="tab"] { padding: 0 2px; font-size: 11px; letter-spacing: 0.02em; }
      #viewport-readout {
        left: 10px;
        bottom: 8px;
        transform: none;
        align-items: flex-start;
        gap: 4px;
        max-width: calc(100% - 118px);
      }
      #zone-pickers { justify-content: flex-start; padding: 0; gap: 3px; }
      .zone-swatch { width: 32px; height: 32px; }
      #skin-controls { flex-wrap: nowrap; }
      .gender-btn { min-width: 36px; height: 36px; min-height: 36px; font-size: 18px; }
      #skin-controls > #skin-select { min-height: 36px; }
      #status-bar { top: 10px; bottom: auto; left: auto; right: 10px; }
    }
