/* ==========================================================================
 * 辅助工具 iframe 页面手机端适配（统一补丁）
 * 只在 <=768px 生效，避免动桌面端布局。
 * 每个工具 HTML 都在 </head> 前引这个文件。
 * ========================================================================== */
@media (max-width: 768px) {
  html,
  body {
    width: 100% !important;
    max-width: 100% !important;
    height: 100% !important;
    min-height: 100% !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  /* 工具页嵌在工作台 iframe 里，不再叠加全站悬浮菜单 */
  html body .tom-r-fab,
  html body .tom-r-panel,
  html body .tom-r-scrim {
    display: none !important;
    visibility: hidden !important;
    opacity: 0 !important;
    pointer-events: none !important;
  }

  button,
  [role="button"] {
    min-width: 40px !important;
    min-height: 36px !important;
    white-space: nowrap !important;
    box-sizing: border-box !important;
  }

  input,
  select,
  textarea {
    max-width: 100% !important;
    box-sizing: border-box !important;
  }

  img,
  video,
  canvas {
    max-width: 100% !important;
  }

  /* ---------- English：侧栏被全站脚本推到负坐标，主区被挤出屏 ---------- */
  body:has(.app-layout .sidebar-nav) .app-layout {
    display: block !important;
    width: 100% !important;
    max-width: 100% !important;
    height: auto !important;
    overflow: visible !important;
  }

  body:has(.app-layout .sidebar-nav) .sidebar-nav {
    position: static !important;
    left: auto !important;
    top: auto !important;
    transform: none !important;
    width: 100% !important;
    max-width: 100% !important;
    height: auto !important;
    max-height: none !important;
    border-right: 0 !important;
    border-bottom: 1px solid var(--border-color, #333);
  }

  body:has(.app-layout .sidebar-nav) .main-stage {
    width: 100% !important;
    max-width: 100% !important;
    margin-left: 0 !important;
    padding-left: 0 !important;
  }

  body:has(.app-layout .sidebar-nav) .resize-handle-panel,
  body:has(.app-layout .sidebar-nav) .resize-handle {
    display: none !important;
  }

  /* ---------- PsImg：三列 520px 固定栏 ---------- */
  #psimg-left-column,
  #psimg-center-column,
  #psimg-right-column,
  .psimg-col {
    flex: 0 0 auto !important;
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
  }

  .psimg-col .sidebar-panel,
  .psimg-col .comfy-panel {
    width: 100% !important;
    max-width: 100% !important;
    position: static !important;
  }

  body:has(#psimg-left-column) .app-container {
    display: flex !important;
    flex-direction: column !important;
    height: auto !important;
    overflow: visible !important;
  }

  body:has(#psimg-left-column) .sidebar-nav {
    width: 100% !important;
    height: auto !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  body:has(#psimg-left-column) .sidebar-nav .nav-item {
    flex: 0 0 auto !important;
    writing-mode: horizontal-tb !important;
    padding: 8px 12px !important;
    white-space: nowrap !important;
  }

  body:has(#psimg-left-column) .psimg-col {
    height: auto !important;
    overflow: visible !important;
  }

  body:has(#psimg-left-column) #left-panel,
  body:has(#psimg-left-column) #psimg-right-column .sidebar-panel {
    flex: 0 0 auto !important;
    height: auto !important;
    min-height: 0 !important;
    overflow: visible !important;
  }

  body:has(#psimg-left-column) #left-panel .tab-content {
    height: auto !important;
    min-height: 0 !important;
    overflow: visible !important;
  }

  .psimg-col .main-canvas-container,
  .psimg-col .canvas-wrapper {
    width: 100% !important;
    max-width: 100% !important;
    min-height: 280px !important;
  }

  /* ---------- UserStarlink：左右面板整体出屏 ---------- */
  .settings-layout {
    display: block !important;
    width: 100% !important;
    max-width: 100% !important;
  }

  .settings-layout .left-panel,
  .settings-layout .right-panel,
  .settings-layout .panel-content {
    position: static !important;
    left: auto !important;
    right: auto !important;
    transform: none !important;
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    height: auto !important;
    max-height: none !important;
    margin: 0 0 12px !important;
  }

  .settings-layout .panel-toggle {
    display: none !important;
  }

  /* 地图底部控制条：文字按钮不再竖着折行 */
  .netctrl-btn,
  .netctrl-scale {
    white-space: nowrap !important;
  }

  .netctrl-btn.netctrl-wide {
    width: auto !important;
    min-width: 52px !important;
    padding: 0 8px !important;
  }

  /* ---------- aimusic：380px 侧栏把主区推到 x=390 ---------- */
  body:has(#mainContent) aside.w-\[380px\] {
    width: 100% !important;
    max-width: 100% !important;
    height: auto !important;
    flex: 0 0 auto !important;
  }

  body:has(#mainContent) > div.flex.flex-1 {
    display: block !important;
    overflow: visible !important;
    height: auto !important;
  }

  body:has(#mainContent) #mainContent {
    width: 100% !important;
    max-width: 100% !important;
    height: auto !important;
    overflow: visible !important;
    padding: 12px !important;
  }

  /* ---------- editing：左工具条 + 画布 + 右属性栏三列挤压 ---------- */
  body:has(.properties-panel) .app-container {
    display: flex !important;
    flex-direction: column !important;
    height: auto !important;
    overflow: visible !important;
  }

  body:has(.properties-panel) .left-sidebar {
    width: 100% !important;
    max-width: 100% !important;
    height: auto !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  body:has(.properties-panel) .main-workspace,
  body:has(.properties-panel) .properties-panel,
  body:has(.properties-panel) .resources-panel {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    height: auto !important;
    max-height: none !important;
    position: static !important;
    margin: 0 0 12px !important;
  }

  body:has(.properties-panel) .properties-panel {
    border-left: 0 !important;
    border-top: 1px solid var(--border-color, #333);
  }

  /* ---------- imgTOtext：右侧 AI 面板整块出屏 ---------- */
  body:has(.ai-sidebar) .app-container {
    display: flex !important;
    flex-direction: column !important;
    height: auto !important;
    overflow: visible !important;
  }

  body:has(.ai-sidebar) .ai-sidebar,
  body:has(.ai-sidebar) .preview-sidebar,
  body:has(.ai-sidebar) .main-panel {
    position: static !important;
    left: auto !important;
    right: auto !important;
    transform: none !important;
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    height: auto !important;
    max-height: none !important;
    margin: 0 0 12px !important;
  }

  /* 结果标题/操作区：小屏先保证标题不断行，按钮自动换到下一行 */
  .result-header,
  .ai-result-header {
    flex-wrap: wrap !important;
    gap: 8px !important;
  }

  .result-title,
  .ai-result-title {
    flex: 0 0 auto !important;
    white-space: nowrap !important;
  }

  .result-actions,
  .ai-result-actions {
    flex: 1 1 auto !important;
    flex-wrap: wrap !important;
    justify-content: flex-end !important;
  }

  /* ---------- 其余工具：常见固定宽卡片/弹窗收窄 ---------- */
  [class*="modal"],
  [class*="dialog"] {
    max-width: calc(100vw - 24px) !important;
  }

  table {
    display: block;
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}
