/* ==========================================================================
 * IDE responsive layer
 *
 * Three layout modes driven by <html data-layout-mode="...">:
 *
 *   desktop  — viewport > 1024px (default). Full multi-pane layout.
 *   tablet   — 641px–1024px.         Compressed widths, touch-friendly sizing,
 *                                    but the same pane structure as desktop.
 *   phone    — <= 640px.             Page-based layout: prompt / assets /
 *                                    content are stacked and only one is
 *                                    visible at a time; a bottom tab bar
 *                                    switches between them.
 *
 * The mode is decided by IDE.Layout (see ide-layout.js). Users can override
 * the auto-detected mode with the Layout button in the top bar.
 *
 * Orientation is tracked via <html data-orientation="portrait|landscape">
 * and used by IDE.Panes to rotate split directions in the content editor.
 * ========================================================================== */

/* --------------------------------------------------------------------------
 * Layout toggle button + bottom tab bar (never rendered on desktop)
 * -------------------------------------------------------------------------- */
#btn-layout-toggle {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

#mobile-tab-bar {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: 56px;
  padding: 4px 6px calc(4px + env(safe-area-inset-bottom)) 6px;
  background: var(--bg-panel);
  border-top: 1px solid var(--border);
  z-index: 900;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.25);
}

#mobile-tab-bar .mobile-tab {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 500;
  border-radius: var(--radius);
  cursor: pointer;
  min-height: 44px;
  padding: 4px 2px;
}

#mobile-tab-bar .mobile-tab .mobile-tab-icon {
  font-size: 20px;
  line-height: 1;
}

#mobile-tab-bar .mobile-tab.active {
  color: var(--accent);
  background: rgba(108, 99, 255, 0.12);
}

/* --------------------------------------------------------------------------
 * Touch-friendly sizing (applied to any coarse-pointer device regardless
 * of layout mode)
 * -------------------------------------------------------------------------- */
@media (pointer: coarse) {
  .btn {
    min-height: 36px;
    padding: 6px 12px;
  }
  .btn-sm {
    min-height: 30px;
    padding: 4px 8px;
  }
  .resize-handle-v {
    width: 10px;
  }
  .resize-handle-h {
    height: 10px;
  }
  .pane-resize-v {
    width: 10px;
  }
  .pane-resize-h {
    height: 10px;
  }
  .pane-tab {
    min-height: 36px;
  }
  .tree-line {
    min-height: 36px;
  }
  .tree-eye,
  .tree-plane {
    min-width: 32px;
    min-height: 32px;
  }
}

/* --------------------------------------------------------------------------
 * Tablet mode — reduce hardcoded min-widths so the pane layout still fits
 * on a ~768px screen without horizontal scrolling.
 * -------------------------------------------------------------------------- */
html[data-layout-mode="tablet"] .workspace {
  min-width: 280px;
}
html[data-layout-mode="tablet"] .sidebar {
  min-width: 200px;
  max-width: 60%;
}
html[data-layout-mode="tablet"] .prompt-area {
  min-height: 180px;
}
html[data-layout-mode="tablet"] .prompt-panel {
  min-width: 220px;
}
html[data-layout-mode="tablet"] .ai-output-panel {
  min-width: 200px;
  max-width: 380px;
}

/* In tablet portrait, the prompt's two sub-panels (prompt + templates)
 * stack vertically so neither is squeezed. */
html[data-layout-mode="tablet"][data-orientation="portrait"] .prompt-area-body {
  flex-direction: column;
}
html[data-layout-mode="tablet"][data-orientation="portrait"]
  #prompt-template-resize {
  width: auto;
  height: 5px;
  cursor: row-resize;
}
html[data-layout-mode="tablet"][data-orientation="portrait"] .ai-output-panel {
  max-width: none;
  min-height: 140px;
}

/* Top bar: let actions wrap to a second row if they don't fit. */
html[data-layout-mode="tablet"] .top-bar,
html[data-layout-mode="phone"] .top-bar {
  flex-wrap: wrap;
  height: auto;
  min-height: var(--header-h);
  padding: 4px 8px;
  row-gap: 4px;
}
html[data-layout-mode="tablet"] .top-actions,
html[data-layout-mode="phone"] .top-actions {
  flex-wrap: wrap;
  row-gap: 4px;
}

/* --------------------------------------------------------------------------
 * Phone mode — page-based navigation
 * -------------------------------------------------------------------------- */
html[data-layout-mode="phone"] body {
  overflow: hidden;
}

html[data-layout-mode="phone"] #app {
  /* Reserve room for the bottom tab bar */
  padding-bottom: calc(56px + env(safe-area-inset-bottom));
  /* minus a practice copy's banner above it (0 everywhere else) */
  height: calc(100vh - var(--top-bands-h, 0px));
  height: calc(100dvh - var(--top-bands-h, 0px));
}

html[data-layout-mode="phone"] #mobile-tab-bar {
  display: flex;
}

/* CW-6: the fixed bottom docks (Runs ⚙ / log) sit at bottom:0 with a higher
 * z-index than the tab bar (1050 vs 900) — on a phone their collapsed bars
 * overlaid the page tabs and swallowed every tap. Lift them above the bar. */
html[data-layout-mode="phone"] .ide-process-view,
html[data-layout-mode="phone"] .ide-log-view {
  bottom: calc(56px + env(safe-area-inset-bottom));
}
html[data-layout-mode="phone"] .ide-inbox-toggle {
  bottom: calc(68px + env(safe-area-inset-bottom));
}
html[data-layout-mode="phone"] .ide-inbox-panel {
  bottom: calc(108px + env(safe-area-inset-bottom));
}

/* Kill all main-axis splitters — each pane is a full-bleed page now. */
html[data-layout-mode="phone"] #main-resize,
html[data-layout-mode="phone"] #prompt-resize,
html[data-layout-mode="phone"] #prompt-template-resize {
  display: none;
}

/* Main layout becomes a page stack. */
html[data-layout-mode="phone"] .main-layout {
  flex-direction: column;
  overflow: hidden;
}

/* By default, none of the three main panes are visible. Then the active
 * phone page is revealed by matching [data-phone-page]. */
html[data-layout-mode="phone"] #workspace,
html[data-layout-mode="phone"] #asset-panel {
  display: none;
  flex: 1 1 auto;
  width: 100%;
  max-width: none;
  min-width: 0;
  min-height: 0;
  height: 100%;
}
html[data-layout-mode="phone"] #workspace #bottom-area {
  display: none;
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
  max-height: none;
}

/* "Assets" page → the tree, full height (the Tools half hides — it has its
 * own page now). CW-6: the former "prompt" page targeted the retired
 * #prompt-area and is gone. */
html[data-layout-mode="phone"][data-phone-page="assets"] #asset-panel {
  display: flex;
}
html[data-layout-mode="phone"][data-phone-page="assets"] #ide-utility-section,
html[data-layout-mode="phone"][data-phone-page="assets"] #ide-utility-resize-handle {
  display: none;
}

/* "Tools" page → the sidebar with ONLY the Tools pane, full height. */
html[data-layout-mode="phone"][data-phone-page="tools"] #asset-panel {
  display: flex;
  flex-direction: column; /* the bare `flex` reveal is a ROW — children can't fill */
}
html[data-layout-mode="phone"][data-phone-page="tools"] #asset-selection-bar,
html[data-layout-mode="phone"][data-phone-page="tools"] #action-bar,
html[data-layout-mode="phone"][data-phone-page="tools"] #action-bar-moved,
html[data-layout-mode="phone"][data-phone-page="tools"] #section-assets,
html[data-layout-mode="phone"][data-phone-page="tools"] #ide-utility-resize-handle,
html[data-layout-mode="phone"][data-phone-page="tools"]
  #asset-panel > .sidebar-section:not(#ide-utility-section) {
  display: none;
}
html[data-layout-mode="phone"][data-phone-page="tools"] #ide-utility-section {
  display: flex;
  flex-direction: column;
  /* the pane's own height-drag writes an inline `flex: 0 0 40%` — a page
   * must fill regardless of the persisted desktop split */
  flex: 1 1 auto !important;
  min-height: 0;
}
html[data-layout-mode="phone"][data-phone-page="tools"]
  #ide-utility-section .ide-utility-body {
  height: auto !important; /* the persisted desktop height must not cap a page */
  flex: 1 1 auto;
  min-height: 0;
}

/* "Content" page → the pane manager. */
html[data-layout-mode="phone"][data-phone-page="content"] #workspace {
  display: flex;
}
html[data-layout-mode="phone"][data-phone-page="content"]
  #workspace
  #bottom-area {
  display: flex;
  flex: 1 1 auto;
}

/* Phone tweaks for the sidebar: spread out the action row and let the
 * action buttons wrap. */
html[data-layout-mode="phone"] .sidebar-section-actions {
  flex-wrap: wrap;
  row-gap: 4px;
}
html[data-layout-mode="phone"] .action-bar {
  flex-wrap: wrap;
  row-gap: 4px;
}

/* Modals: responsive fallback for any small viewport regardless of layout mode.
 * The base .modal uses min() so this mostly tightens padding + max-height. */
@media (max-width: 640px) {
  .modal {
    max-height: calc(100dvh - 80px);
    padding: 16px;
  }
  .modal-form input,
  .modal-form select {
    min-height: 40px;
    font-size: 16px; /* Prevents iOS auto-zoom on focus */
  }
}

/* Modals: make them fill most of the phone screen with safe margins. */
html[data-layout-mode="phone"] .modal {
  width: calc(100vw - 16px);
  max-width: calc(100vw - 16px);
  max-height: calc(100dvh - 80px);
  padding: 16px;
}
html[data-layout-mode="phone"] .workspace-list {
  max-height: 40dvh;
}
html[data-layout-mode="phone"] .modal-form input,
html[data-layout-mode="phone"] .modal-form select {
  min-height: 40px;
  font-size: 16px; /* Prevents iOS auto-zoom on focus */
}

/* Larger base font size on phone for readability. */
html[data-layout-mode="phone"] {
  font-size: 15px;
}

/* --------------------------------------------------------------------------
 * Orientation-aware pane layout (content editor)
 *
 * IDE.Panes.applyOrientation() flips the split-tree's direction attributes
 * between portrait and landscape. CSS below is the fallback styling that
 * matches the computed DOM classes.
 * -------------------------------------------------------------------------- */

/* In portrait phone/tablet mode, let pane tabs wrap instead of forcing
 * horizontal scroll — the tab strip is usually narrow. */
html[data-orientation="portrait"][data-layout-mode="phone"] .pane-tabs,
html[data-orientation="portrait"][data-layout-mode="tablet"] .pane-tabs {
  flex-wrap: wrap;
  max-height: 72px;
  overflow-y: auto;
}

/* Prevent pane tabs from eating the entire viewport when a viewer is tall. */
html[data-layout-mode="phone"] .pane-tab {
  max-width: 140px;
}

/* --------------------------------------------------------------------------
 * Safety: hide anything marked phone-only or desktop-only when not
 * applicable. Components can opt in with these classes.
 * -------------------------------------------------------------------------- */
.phone-only {
  display: none;
}
html[data-layout-mode="phone"] .phone-only {
  display: initial;
}
html[data-layout-mode="phone"] .desktop-only {
  display: none !important;
}
