/* Studio mobile & tablet — shared breakpoints and drawer shell */

:root {
  --bp-phone: 640px;
  --bp-tablet: 1024px;
  --studio-dock-height: 54px;
  --studio-safe-bottom: env(safe-area-inset-bottom, 0px);
  --studio-chrome-top: 96px;
}

/* Bottom dock (hidden on desktop) */
.studio-bottom-dock {
  display: none;
}

.studio-drawer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 55;
  background: var(--gate-scrim);
  opacity: 0.55;
  border: none;
  padding: 0;
  margin: 0;
  cursor: pointer;
}

.studio-drawer-backdrop[hidden] {
  display: none !important;
}

.studio-drawer-close {
  display: none;
}

.studio-tools-sheet {
  display: contents;
}

/* Coarse pointer — larger tap targets */
@media (pointer: coarse) {
  .resize-handle,
  .tile-handle,
  .tool-btn,
  .cleanup-tool-btn,
  .btn-sm {
    min-height: 40px;
    min-width: 40px;
  }

  .extractor-result-thumb {
    min-height: 72px;
  }
}

/* Tablet — tighter header */
@media (max-width: 1024px) {
  .studio-step-label {
    display: none;
  }
}

/* Phone — canvas-first drawer shell */
@media (max-width: 640px) {
  body.studio-tool-page {
    padding-bottom: calc(var(--studio-dock-height) + var(--studio-safe-bottom));
  }

  .studio-bottom-dock {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 90;
    gap: 6px;
    padding: 6px 8px calc(6px + var(--studio-safe-bottom));
    background: var(--panel-bg);
    border-top: 1px solid var(--border);
    box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.12);
  }

  .studio-dock-btn {
    flex: 1 1 0;
    min-height: 42px;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--cream);
    font-family: var(--font-ui);
    font-size: 0.82rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
  }

  .studio-dock-btn:hover,
  .studio-dock-btn[aria-pressed="true"] {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-contrast);
  }

  .studio-drawer-backdrop:not([hidden]) {
    display: block;
  }

  .studio-drawer-close {
    display: block;
    width: 100%;
    flex-shrink: 0;
    padding: 12px 14px;
    border: none;
    border-bottom: 1px solid var(--border);
    background: var(--card-bg);
    color: var(--cream);
    font-family: var(--font-ui);
    font-size: 0.85rem;
    font-weight: 600;
    text-align: right;
    cursor: pointer;
  }

  /* Keep app shell — no full-page scroll */
  html:has(.studio-mobile-layout),
  body:has(.studio-mobile-layout) {
    height: 100%;
    max-height: 100dvh;
    overflow: hidden;
  }

  .studio-mobile-layout {
    grid-template-columns: 1fr !important;
    grid-template-rows: minmax(0, 1fr) !important;
    grid-template-areas: none !important;
    padding: 8px !important;
    padding-bottom: 8px !important;
    height: auto !important;
    max-height: none !important;
    min-height: 0 !important;
    overflow: hidden !important;
  }

  /* Side panels become slide-over drawers */
  .studio-mobile-layout > [data-studio-panel] {
    display: none !important;
    position: fixed;
    z-index: 70;
    left: 0;
    right: 0;
    bottom: calc(var(--studio-dock-height) + var(--studio-safe-bottom));
    max-height: min(72vh, calc(100dvh - var(--studio-dock-height) - var(--studio-chrome-top) - var(--studio-safe-bottom)));
    margin: 0;
    border-radius: var(--radius) var(--radius) 0 0;
    box-shadow: var(--shadow-soft);
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    flex-direction: column;
  }

  .studio-mobile-layout.studio-panel-open-upload > [data-studio-panel="upload"],
  .studio-mobile-layout.studio-panel-open-results > [data-studio-panel="results"],
  .studio-mobile-layout.studio-panel-open-sprites > [data-studio-panel="sprites"],
  .studio-mobile-layout.studio-panel-open-presets > [data-studio-panel="presets"],
  .studio-mobile-layout.studio-panel-open-repeat > [data-studio-panel="repeat"],
  .studio-mobile-layout.studio-panel-open-preview > [data-studio-panel="preview"],
  .studio-mobile-layout.studio-panel-open-help > [data-studio-panel="help"],
  .studio-mobile-layout.studio-panel-open-preview > .preview-panel,
  .studio-mobile-layout.studio-panel-open-help > .help-panel {
    display: flex !important;
  }

  /* Composer drawers — descendant selectors (tile-controls nest under .composer-pane) */
  .composer-layout.studio-mobile-layout:not(.studio-panel-open-tools) .tile-controls {
    display: none !important;
  }

  .composer-layout.studio-panel-open-tools .tile-controls,
  .composer-layout.studio-panel-open-preview .preview-panel,
  .composer-layout.studio-panel-open-help .help-panel {
    display: flex !important;
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(var(--studio-dock-height) + var(--studio-safe-bottom));
    top: auto;
    width: 100%;
    max-height: min(78vh, calc(100dvh - var(--studio-dock-height) - var(--studio-chrome-top) - var(--studio-safe-bottom)));
    transform: none !important;
    border-left: none;
    border-radius: var(--radius) var(--radius) 0 0;
    box-shadow: var(--shadow-soft);
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    z-index: 70;
  }

  .composer-layout.studio-panel-open-sprites > [data-studio-panel="sprites"] {
    display: flex !important;
  }

  /* Dock replaces header Tools/Preview switch on phone */
  .composer-layout .compose-view-switch {
    display: none !important;
  }

  /* Workspace stays visible */
  .studio-mobile-layout > .extractor-work,
  .studio-mobile-layout > .composer-work-panel,
  .studio-mobile-layout > .clean-work,
  .studio-mobile-layout > .bg-preview-panel,
  .studio-mobile-layout > .work-panel {
    display: flex !important;
    min-height: 0;
    flex: 1 1 auto;
    max-height: none;
    grid-area: auto !important;
  }

  .studio-mobile-layout > .extractor-easy-left,
  .studio-mobile-layout > .extractor-easy-right,
  .studio-mobile-layout > .sprites-panel,
  .studio-mobile-layout > .clean-side,
  .studio-mobile-layout > .bg-repeat-panel,
  .studio-mobile-layout > [data-studio-panel="presets"] {
    grid-area: auto !important;
  }

  /* Sticky zoom on extractor */
  .extractor-easy-zoombar {
    position: sticky;
    top: 0;
    z-index: 5;
    margin-top: 0;
  }

  /*
   * Tools sheet: one scrollable bottom drawer for edit + stylize bars.
   * display:contents on desktop; fixed sheet when Tools is open on phone.
   */
  .studio-mobile-layout .studio-tools-sheet {
    display: none;
  }

  .studio-mobile-layout.studio-panel-open-tools .tab-pane.active > .studio-tools-sheet,
  .studio-mobile-layout.studio-panel-open-tools .clean-cleanup-pane > .studio-tools-sheet {
    display: flex !important;
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(var(--studio-dock-height) + var(--studio-safe-bottom));
    max-height: min(70vh, calc(100dvh - var(--studio-dock-height) - var(--studio-chrome-top) - var(--studio-safe-bottom)));
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    z-index: 75;
    padding: 0 0 12px;
    background: var(--surface);
    border-top: 1px solid var(--border);
    border-radius: var(--radius) var(--radius) 0 0;
    box-shadow: var(--shadow-soft);
  }

  .studio-mobile-layout.studio-panel-open-tools .studio-tools-sheet .source-toolbar,
  .studio-mobile-layout.studio-panel-open-tools .studio-tools-sheet .cleanup-stylize-bar {
    display: flex !important;
    position: static;
    flex-wrap: wrap;
    gap: 8px;
    padding: 12px;
    max-height: none;
    overflow: visible;
    background: transparent;
    border: none;
    border-radius: 0;
    box-shadow: none;
  }

  .studio-mobile-layout:not(.studio-panel-open-tools) .clean-work .source-toolbar,
  .studio-mobile-layout:not(.studio-panel-open-tools) .clean-work .cleanup-stylize-bar,
  .studio-mobile-layout:not(.studio-panel-open-tools) #sourceToolbar,
  .studio-mobile-layout:not(.studio-panel-open-tools) #cleanupToolbar,
  .studio-mobile-layout:not(.studio-panel-open-tools) #cleanupStylizeBar {
    display: none !important;
  }

  /* Preview bottom dock */
  .preview-page .studio-bottom-dock {
    display: flex;
  }

  .preview-page #preview {
    padding-bottom: calc(var(--studio-dock-height) + var(--studio-safe-bottom));
  }

  .preview-page .preview-dock-size {
    display: none;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 91;
    align-items: center;
    gap: 8px;
    padding: 10px 12px calc(10px + var(--studio-safe-bottom));
    background: var(--panel-bg);
    border-top: 1px solid var(--border);
    box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.12);
  }

  .preview-page.studio-panel-open-size .preview-dock {
    display: none;
  }

  .preview-page.studio-panel-open-size .preview-dock-size {
    display: flex;
  }

  .preview-dock-size input[type="range"] {
    flex: 1;
    min-width: 0;
  }

  .preview-dock-size .studio-dock-btn {
    flex: 0 0 auto;
  }

  /* Background maker — drawer shell overrides legacy stack layout */
  .background-layout.studio-mobile-layout {
    grid-template-columns: 1fr !important;
    grid-template-rows: minmax(0, 1fr) !important;
    overflow: hidden !important;
  }

  /* Keep drawers above header while open; backdrop covers header for dismiss */
  body:has(.studio-drawer-active) .app-header,
  body.studio-panel-open-size .app-header {
    z-index: 40;
  }

  /* Header overflow — hide secondary controls; keep mode switch + project save/open */
  .app-header .controls .ctrl-break {
    display: none;
  }

  .app-header .controls .btn-ghost:not(.studio-help-btn):not(#openAdvancedBtn):not(#backToEasyBtn) {
    display: none;
  }

  .app-header .controls #newProjectBtn {
    display: none;
  }

  .app-header .controls #openAdvancedBtn,
  .app-header .controls #backToEasyBtn,
  .app-header .controls #saveProjectBtn,
  .app-header .controls label.btn[for="openProjectInput"] {
    display: inline-flex;
  }

  .studio-side-tool-label {
    display: none;
  }
}
