/* Make Tile — phone canvas-first shell (Procreate / Maps style)
   Desktop & tablet keep compose-shell.css. This file only reshapes max-width 640px. */

@media (max-width: 640px) {
  body.studio-tool-page.compose-layout-phone,
  body.studio-tool-page[data-studio-step="compose"]:has(.composer-layout.layout-phone) {
    --compose-phone-header: 44px;
    --compose-phone-dock: var(--studio-dock-height, 108px);
    padding-bottom: calc(var(--compose-phone-dock) + var(--studio-safe-bottom, env(safe-area-inset-bottom, 0px)));
  }

  /* ── Thin header: More + brand + theme — Save/Project live in Project dock ── */
  body[data-studio-step="compose"]:has(.composer-layout.layout-phone) .app-header {
    min-height: var(--compose-phone-header);
    height: var(--compose-phone-header);
    --studio-tool-menu-top: var(--compose-phone-header);
  }

  body[data-studio-step="compose"]:has(.composer-layout.layout-phone) .app-header #studioProjectChipWrap,
  body[data-studio-step="compose"]:has(.composer-layout.layout-phone) .app-header .studio-project-chip-wrap {
    display: none !important;
  }

  body[data-studio-step="compose"]:has(.composer-layout.layout-phone) #composePhoneSaveBtn {
    display: none !important;
  }

  /* ── Layout: canvas owns the viewport ───────────────────────────────────── */
  .composer-layout.layout-phone,
  .composer-layout.layout-phone.studio-mobile-layout {
    display: flex !important;
    flex-direction: column !important;
    height: calc(
      100dvh - var(--compose-phone-header, 52px) - var(--compose-phone-dock, 64px) -
        var(--studio-safe-bottom, env(safe-area-inset-bottom, 0px)) -
        env(safe-area-inset-top, 0px)
    ) !important;
    max-height: none !important;
    min-height: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
    position: relative;
  }

  .composer-layout.layout-phone > .composer-work-panel,
  .composer-layout.layout-phone > .composer-pane {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
    padding: 0 !important;
    border: none !important;
    background: transparent !important;
    position: relative !important;
  }

  .composer-layout.layout-phone .compose-canvas-chrome,
  .composer-layout.layout-phone .compose-canvas-hint,
  .composer-layout.layout-phone .tile-save-info,
  .composer-layout.layout-phone .placed-selection-bar {
    display: none !important;
  }

  .composer-layout.layout-phone #tileEditorWrap,
  .composer-layout.layout-phone .tile-editor-wrap {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    margin: 0 !important;
    border-radius: 0 !important;
    border: none !important;
  }

  /* Floating undo / redo on canvas */
  .compose-phone-float-tools {
    display: none;
  }

  .composer-layout.layout-phone .compose-phone-float-tools {
    display: flex;
    position: absolute;
    top: 10px;
    left: 10px;
    z-index: 8;
    gap: 6px;
  }

  .composer-layout.layout-phone .compose-phone-float-tools .btn {
    min-width: 44px;
    min-height: 44px;
    border-radius: 999px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.14);
    background: var(--panel-bg);
  }

  /* Hide desktop side panels until a dock opens them */
  .composer-layout.layout-phone > .sprites-panel,
  .composer-layout.layout-phone > [data-studio-panel="sprites"],
  .composer-layout.layout-phone > #composeLibraryMount,
  .composer-layout.layout-phone > .layers-panel[data-studio-panel="layers"],
  .composer-layout.layout-phone > .more-panel[data-studio-panel="more"],
  .composer-layout.layout-phone > .selected-inspector#selectedInspector,
  .composer-layout.layout-phone > .selected-inspector[data-studio-panel="adjust"],
  .composer-layout.layout-phone > [data-studio-panel="canvas"],
  .composer-layout.layout-phone > [data-studio-panel="project"],
  .composer-layout.layout-phone > .preview-panel,
  .composer-layout.layout-phone > .help-panel {
    display: none !important;
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(var(--compose-phone-dock, 64px) + var(--studio-safe-bottom, env(safe-area-inset-bottom, 0px)));
    top: auto;
    width: 100%;
    max-height: min(
      78vh,
      calc(
        100dvh - var(--compose-phone-header, 52px) - var(--compose-phone-dock, 64px) -
          var(--studio-safe-bottom, env(safe-area-inset-bottom, 0px))
      )
    );
    margin: 0;
    z-index: 70;
    flex-direction: column;
    border-radius: 18px 18px 0 0;
    border: 1px solid var(--border);
    border-bottom: none;
    box-shadow: 0 -12px 40px rgba(0, 0, 0, 0.18);
    background: var(--panel-bg);
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  /* Library sheet: chrome stays put; only the sprite grid scrolls */
  .composer-layout.layout-phone > .sprites-panel,
  .composer-layout.layout-phone > [data-studio-panel="sprites"],
  .composer-layout.layout-phone > #composeLibraryMount {
    overflow: hidden;
  }

  .composer-layout.layout-phone.studio-panel-open-sprites > .sprites-panel,
  .composer-layout.layout-phone.studio-panel-open-sprites > [data-studio-panel="sprites"],
  .composer-layout.layout-phone.studio-panel-open-sprites > #composeLibraryMount,
  .composer-layout.layout-phone.studio-panel-open-canvas > [data-studio-panel="canvas"],
  .composer-layout.layout-phone.studio-panel-open-project > [data-studio-panel="project"],
  .composer-layout.layout-phone.studio-panel-open-adjust > .selected-inspector#selectedInspector,
  .composer-layout.layout-phone.studio-panel-open-adjust > [data-studio-panel="adjust"],
  .composer-layout.layout-phone.studio-panel-open-more > .more-panel,
  .composer-layout.layout-phone.studio-panel-open-preview > .preview-panel,
  .composer-layout.layout-phone.studio-panel-open-help > .help-panel {
    display: flex !important;
  }

  .composer-layout.layout-phone.studio-panel-open-adjust > .selected-inspector#selectedInspector,
  .composer-layout.layout-phone.studio-panel-open-adjust > [data-studio-panel="adjust"] {
    overflow-x: hidden !important;
    overflow-y: auto !important;
  }

  .composer-layout.layout-phone.studio-panel-open-adjust > .selected-inspector#selectedInspector:not(.adjust-collapsed),
  .composer-layout.layout-phone.studio-panel-open-adjust > .selected-inspector[data-studio-panel="adjust"]:not(.adjust-collapsed) {
    min-height: min(
      72vh,
      calc(
        100dvh - var(--compose-phone-header, 52px) - var(--compose-phone-dock, 64px) -
          var(--studio-safe-bottom, env(safe-area-inset-bottom, 0px)) - 8px
      )
    );
  }

  .composer-layout.layout-phone.studio-panel-open-adjust > .selected-inspector.mode-arrange .adjust-handle {
    display: none;
  }

  .composer-layout.layout-phone.studio-panel-open-canvas > [data-studio-panel="canvas"],
  .composer-layout.layout-phone.studio-panel-open-project > [data-studio-panel="project"] {
    min-height: min(
      52vh,
      calc(
        100dvh - var(--compose-phone-header, 52px) - var(--compose-phone-dock, 64px) -
          var(--studio-safe-bottom, env(safe-area-inset-bottom, 0px)) - 8px
      )
    );
  }

  .composer-layout.layout-phone.studio-panel-open-adjust > .selected-inspector.mode-sprite #inspectorBody:not([hidden]),
  .composer-layout.layout-phone.studio-panel-open-adjust > .selected-inspector.mode-sprite .inspector-colour-panel:not([hidden]),
  .composer-layout.layout-phone.studio-panel-open-adjust > .selected-inspector.mode-batch #inspectorBody:not([hidden]),
  .composer-layout.layout-phone.studio-panel-open-adjust > .selected-inspector.mode-batch .inspector-colour-panel:not([hidden]),
  .composer-layout.layout-phone.studio-panel-open-adjust > .selected-inspector.mode-arrange .inspector-layer-panel:not([hidden]),
  .composer-layout.layout-phone.studio-panel-open-adjust > .selected-inspector.mode-bg #inspectorBgBody:not([hidden]) {
    flex: 1 1 auto !important;
    max-height: none !important;
    min-height: 0 !important;
    overflow: hidden !important;
    display: flex !important;
    flex-direction: column;
  }

  .composer-layout.layout-phone.studio-panel-open-adjust > .selected-inspector.mode-arrange .layer-panel-scroll {
    flex: 1 1 auto;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Drawer grab handle */
  .compose-drawer-handle {
    display: none;
  }

  .composer-layout.layout-phone [data-studio-panel] > .compose-drawer-handle {
    display: flex;
    justify-content: center;
    padding: 10px 0 4px;
    flex-shrink: 0;
  }

  .composer-layout.layout-phone [data-studio-panel] > .compose-drawer-handle::before {
    content: '';
    width: 40px;
    height: 4px;
    border-radius: 999px;
    background: color-mix(in srgb, var(--muted) 55%, transparent);
  }

  /* ── Canvas / Project hubs ──────────────────────────────────────────────── */
  .compose-hub {
    display: flex;
    flex-direction: column;
    min-height: 0;
    flex: 1 1 auto;
  }

  .compose-hub-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 4px 16px 12px;
    border-bottom: 1px solid var(--border);
  }

  .compose-hub-head h2 {
    margin: 0;
    font-size: 1.15rem;
    font-weight: 650;
  }

  .compose-hub-list {
    list-style: none;
    margin: 0;
    padding: 8px 0 20px;
  }

  .compose-hub-item {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    min-height: 52px;
    padding: 12px 16px;
    border: none;
    border-bottom: 1px solid color-mix(in srgb, var(--border) 70%, transparent);
    background: transparent;
    color: var(--cream);
    font-family: var(--font-ui);
    font-size: 1rem;
    font-weight: 550;
    text-align: left;
    cursor: pointer;
  }

  .compose-hub-item:active {
    background: color-mix(in srgb, var(--surface) 80%, var(--accent) 20%);
  }

  .compose-hub-item-meta {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--muted);
    font-size: 0.85rem;
    font-weight: 500;
  }

  .compose-hub-swatch {
    width: 22px;
    height: 22px;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--hub-swatch, #f5f0e6);
    flex-shrink: 0;
  }

  .compose-hub-chevron {
    opacity: 0.45;
  }

  .compose-hub-layers {
    padding: 4px 12px 16px;
  }

  .compose-hub-layer-row {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 48px;
    padding: 6px 8px;
    border-radius: 10px;
  }

  .compose-hub-layer-row.is-active {
    background: color-mix(in srgb, var(--surface) 70%, var(--accent) 30%);
  }

  .compose-hub-layer-row .btn {
    min-height: 40px;
  }

  .compose-hub-layer-main {
    flex: 1 1 auto;
    min-width: 0;
    text-align: left;
  }

  /* Art / Library drawer */
  .composer-layout.layout-phone .sprites-panel .panel-hint-foot {
    display: none;
  }

  .composer-layout.layout-phone .sprites-panel.library-panel,
  .composer-layout.layout-phone .sprites-panel[data-studio-panel="sprites"] {
    min-height: 0;
  }

  .composer-layout.layout-phone .sprites-panel .panel-head,
  .composer-layout.layout-phone .sprites-panel .library-sticky-chrome,
  .composer-layout.layout-phone .sprites-panel > .compose-drawer-handle,
  .composer-layout.layout-phone .sprites-panel > .drawer-close,
  .composer-layout.layout-phone .sprites-panel > .studio-drawer-close {
    flex-shrink: 0;
  }

  .composer-layout.layout-phone .sprites-panel #spritePalette {
    flex: 1 1 auto;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding: 12px 16px 20px;
  }

  .composer-layout.layout-phone .sprites-panel .library-panel-head {
    padding: 8px 16px 10px;
  }

  .composer-layout.layout-phone .sprites-panel .library-select-toggle {
    min-height: 40px;
    min-width: 72px;
    padding: 8px 14px;
    font-size: 0.78rem;
  }

  .composer-layout.layout-phone .sprites-panel .library-filter-chips {
    padding: 0 16px 10px;
    gap: 8px;
  }

  .composer-layout.layout-phone .sprites-panel .library-chip {
    min-height: 36px;
    padding: 7px 14px;
    font-size: 0.72rem;
  }

  .composer-layout.layout-phone .sprites-panel .sprite-select-bar {
    padding: 8px 16px 12px;
    gap: 8px;
  }

  .composer-layout.layout-phone .sprites-panel .sprite-select-bar .btn-sm {
    min-height: 40px;
    padding: 8px 12px;
    font-size: 0.72rem;
  }

  .composer-layout.layout-phone .sprites-panel .size-section-grid {
    grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
    grid-auto-rows: 112px;
    gap: 10px;
  }

  .composer-layout.layout-phone .sprites-panel .sprite-thumb {
    height: 112px;
    min-height: 112px;
    max-height: 112px;
    border-radius: 14px;
  }

  .composer-layout.layout-phone .sprites-panel .size-section-head {
    padding-top: 4px;
    background: linear-gradient(
      to bottom,
      var(--panel-bg, var(--surface)) 65%,
      transparent
    );
  }

  .composer-layout.layout-phone .sprite-add-selected {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-contrast);
  }

  /* ── Dock: Art · Canvas · Project (panel rail above tool tabs) ──────────── */
  body[data-studio-step="compose"]:has(.composer-layout.layout-phone) .studio-bottom-dock.composer-dock {
    display: flex !important;
    gap: 4px;
    padding: 6px 10px 6px;
    bottom: calc(var(--studio-tool-tabs-height, 56px) + var(--studio-safe-bottom, env(safe-area-inset-bottom, 0px)));
    z-index: 91;
  }

  body[data-studio-step="compose"]:has(.composer-layout.layout-phone) .studio-bottom-dock .studio-dock-btn {
    flex-direction: column;
    gap: 2px;
    min-height: 48px;
    font-size: 0.72rem;
    font-weight: 650;
    letter-spacing: 0.02em;
  }

  body[data-studio-step="compose"]:has(.composer-layout.layout-phone) .studio-dock-icon {
    font-size: 1.15rem;
    line-height: 1;
  }

  /* ── FAB ────────────────────────────────────────────────────────────────── */
  .compose-fab-wrap {
    display: none;
  }

  .composer-layout.layout-phone ~ .compose-fab-wrap,
  body[data-studio-step="compose"]:has(.composer-layout.layout-phone) .compose-fab-wrap {
    display: block;
    position: fixed;
    right: 16px;
    bottom: calc(var(--compose-phone-dock, 64px) + var(--studio-safe-bottom, env(safe-area-inset-bottom, 0px)) + 16px);
    z-index: 80;
  }

  .compose-fab {
    width: 56px;
    height: 56px;
    border-radius: 999px;
    border: none;
    background: var(--accent);
    color: var(--accent-contrast);
    font-size: 1.75rem;
    line-height: 1;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
    cursor: pointer;
  }

  .compose-fab[aria-expanded="true"] {
    transform: rotate(45deg);
  }

  .compose-fab-menu {
    position: absolute;
    right: 0;
    bottom: calc(100% + 12px);
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 8px;
    min-width: 180px;
  }

  .compose-fab-menu[hidden] {
    display: none !important;
  }

  .compose-fab-action {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    padding: 10px 16px;
    border: none;
    border-radius: 999px;
    font-family: var(--font-ui);
    font-size: 0.92rem;
    font-weight: 650;
    color: #1a1512;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.16);
    cursor: pointer;
    text-decoration: none;
    white-space: nowrap;
  }

  .compose-fab-action[data-fab="upload"] { background: #f0a8c0; }
  .compose-fab-action[data-fab="extract"] { background: #9fd4a8; }
  .compose-fab-action[data-fab="library"] { background: #c4b0e8; }

  .compose-fab-scrim {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 75;
    border: none;
    padding: 0;
    margin: 0;
    background: rgba(20, 16, 14, 0.45);
    cursor: pointer;
  }

  body.compose-fab-open .compose-fab-scrim {
    display: block;
  }

  /* Long-press toast hint sits fine; selection edit via Canvas or long-press */

  .composer-layout.layout-phone .adjust-handle {
    display: flex;
  }
}

@media (min-width: 641px) {
  .compose-phone-only,
  #composePhoneSaveBtn,
  .compose-phone-float-tools,
  .compose-fab-wrap,
  .compose-fab-scrim {
    display: none !important;
  }
}
