/* 手机主操作保留同一位置，缩放工具与聊天共用可见视口坐标。 */
.ai-agent-mobile-actions {
  display: none;
}

@media (max-width: 600px), (max-width: 1000px) and (max-height: 600px) and (pointer: coarse) {
  .ai-agent-shell.has-mobile-action-bar {
    --ai-agent-mobile-actions-height: calc(72px + env(safe-area-inset-bottom, 0px));
    --ai-agent-mobile-tools-space: 56px;
    --ai-agent-mobile-bottom-space: calc(var(--ai-agent-mobile-actions-height) + var(--ai-agent-mobile-tools-space));
  }

  .has-mobile-action-bar #aiAgentMobileActions:not([hidden]) {
    position: absolute;
    z-index: 30;
    top: calc(var(--ai-agent-mobile-viewport-top) + var(--ai-agent-mobile-viewport-height) - var(--ai-agent-mobile-actions-height));
    right: 0;
    left: 0;
    display: block;
    height: var(--ai-agent-mobile-actions-height);
    padding: 10px max(12px, env(safe-area-inset-right, 0px)) calc(12px + env(safe-area-inset-bottom, 0px)) max(12px, env(safe-area-inset-left, 0px));
    border-top: 1px solid rgba(148, 163, 184, 0.22);
    background: var(--bm-surface, #fff);
    box-shadow: 0 -4px 18px rgba(42, 34, 82, 0.05);
    isolation: isolate;
  }

  #aiAgentMobileActions .ai-agent-mobile-action-row {
    display: grid;
    grid-template-columns: 76px minmax(0, 1fr) minmax(0, 1.3fr);
    align-items: stretch;
    gap: 8px;
    height: 48px;
    min-width: 0;
  }

  #aiAgentMobileActions .ai-agent-mobile-action-row > button {
    position: relative;
    top: auto;
    right: auto;
    bottom: auto;
    left: auto;
    display: inline-flex;
    min-width: 0;
    width: 100%;
    height: 48px;
    min-height: 48px;
    align-items: center;
    justify-content: center;
    gap: 6px;
    margin: 0;
    padding: 0 8px;
    border: 1px solid rgba(132, 111, 235, 0.5);
    border-radius: 16px;
    background: #fff;
    color: var(--primary, #7364c8);
    box-shadow: none;
    transform: none;
    font: inherit;
    font-size: 14px;
    font-weight: 600;
    line-height: 1.2;
    white-space: nowrap;
    touch-action: manipulation;
  }

  #aiAgentMobileActions #aiAgentMobileToggle {
    gap: 5px;
    padding: 0 5px;
    border-color: #e4e4eb;
    color: var(--bm-text, #1f2937);
    font-size: 12px;
  }

  #aiAgentMobileActions #aiAgentMobileToggle .ai-agent-brand-mark {
    width: 24px;
    height: 24px;
    gap: 2px;
    padding: 3px;
    border-radius: 7px;
  }

  #aiAgentMobileActions #referenceAnalysisConfirm {
    border-color: #202124;
    background: #202124;
    color: #fff;
    font-size: 15px;
  }

  #aiAgentMobileActions .ai-agent-mobile-action-row > button:disabled {
    cursor: default;
    opacity: 0.45;
    box-shadow: none;
  }

  #aiAgentMobileActions #referenceAnalysisConfirm:disabled.is-submitting {
    opacity: 0.65;
  }

  #aiAgentMobileActions button:focus-visible {
    outline: 2px solid var(--bm-focus-ring, #846feb);
    outline-offset: 3px;
  }

  #aiAgentMobileActions [hidden] {
    display: none !important;
  }

  #aiAgentMobileTools {
    position: absolute;
    z-index: 3;
    right: max(16px, env(safe-area-inset-right, 0px));
    bottom: calc(100% + 8px);
    width: max-content;
  }

  .ai-agent-shell.has-mobile-action-bar:not(.is-chat-collapsed) #aiAgentMobileTools {
    visibility: hidden;
    opacity: 0;
    pointer-events: none;
  }

  #aiAgentMobileTools button {
    -webkit-tap-highlight-color: transparent;
  }

  #mobileCanvasToolsToggle {
    display: inline-flex;
    min-width: 76px;
    min-height: 44px;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 0 12px;
    border: 1px solid rgba(148, 163, 184, 0.4);
    border-radius: 22px;
    background: #fff;
    color: #4b5563;
    box-shadow: none;
    font: inherit;
    font-size: 14px;
    font-weight: 600;
    line-height: 1.2;
    touch-action: manipulation;
    transition: background-color 120ms ease;
  }

  #mobileCanvasToolsToggle:active {
    background: #f4f1fc;
  }

  #mobileCanvasToolsToggle svg,
  #mobileCanvasToolsMenu svg {
    flex: 0 0 auto;
    width: 20px;
    height: 20px;
  }

  #mobileCanvasToolsToggle .ai-agent-mobile-tools-chevron {
    width: 14px;
    height: 14px;
    transition: transform 200ms ease;
  }

  #mobileCanvasToolsToggle[aria-expanded="true"] .ai-agent-mobile-tools-chevron {
    transform: rotate(180deg);
  }

  #mobileCanvasToolsMenu {
    position: absolute;
    right: 0;
    bottom: calc(100% + 8px);
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 4px;
    width: min(292px, calc(100vw - 28px));
    padding: 7px;
    border: 1px solid #e3e1eb;
    border-radius: 18px;
    background: #fff;
    box-shadow: 0 8px 26px rgba(42, 34, 82, 0.13);
  }

  #mobileCanvasToolsMenu [data-mobile-canvas-action] {
    display: flex;
    min-width: 0;
    min-height: 52px;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 5px;
    border: 0;
    border-radius: 11px;
    background: transparent;
    color: #374151;
    font: inherit;
    font-size: 13px;
    font-weight: 500;
    white-space: nowrap;
    touch-action: manipulation;
  }

  #mobileCanvasToolsMenu [data-mobile-canvas-action="background"] {
    grid-column: 1 / -1;
    min-height: 40px;
    border-top: 1px solid #efedf4;
    border-radius: 0 0 10px 10px;
    color: #6b7280;
    font-size: 12px;
  }

  #mobileCanvasToolsMenu [data-mobile-canvas-action]:disabled {
    opacity: 0.4;
  }

  #mobileCanvasToolsMenu [data-mobile-canvas-action]:active:not(:disabled),
  #mobileCanvasToolsMenu [data-mobile-canvas-action]:focus-visible {
    background: #f4f1fc;
  }

  #mobileCanvasBackgroundSlot,
  #mobileCanvasBackgroundSlot #canvasBackgroundControl {
    position: static;
  }

  #mobileCanvasBackgroundSlot #canvasBackgroundToggle {
    display: none;
  }

  #mobileCanvasBackgroundSlot #canvasBackgroundPanel {
    right: 0;
    bottom: calc(100% + 8px);
    width: min(290px, calc(100vw - 28px));
    max-width: calc(100vw - 28px);
    /* 键盘压缩视口时允许面板滚动，不把标题及关闭按钮推出屏幕。 */
    max-height: max(1px, calc(var(--ai-agent-mobile-viewport-height) - var(--ai-agent-mobile-actions-height) - 90px));
    transform-origin: bottom right;
  }

  /* 聊天直接衔接底栏，开合共用同一几何，避免工具占位变化引发跳动。 */
  .has-mobile-action-bar .ai-agent-sidebar {
    --ai-agent-mobile-sheet-available-height: max(1px, calc(var(--ai-agent-mobile-viewport-height) - var(--ai-agent-mobile-actions-height) - 8px));
    top: calc(var(--ai-agent-mobile-viewport-top) + var(--ai-agent-mobile-viewport-height) - var(--ai-agent-mobile-actions-height) - min(var(--ai-agent-mobile-sheet-height), var(--ai-agent-mobile-sheet-available-height)));
    height: min(var(--ai-agent-mobile-sheet-height), var(--ai-agent-mobile-sheet-available-height));
    padding-bottom: 0;
    overflow: hidden;
    transition: opacity 280ms cubic-bezier(0.2, 0.8, 0.2, 1), transform 280ms cubic-bezier(0.2, 0.8, 0.2, 1), visibility 0s linear;
  }

  .ai-agent-shell.has-mobile-action-bar:not(.is-chat-collapsed) .ai-agent-sidebar {
    visibility: visible;
    opacity: 1;
    transform: translate3d(0, 0, 0);
    transition-delay: 0s;
  }

  .ai-agent-shell.has-mobile-action-bar.is-chat-collapsed .ai-agent-sidebar {
    transform: translate3d(0, calc(100% + var(--ai-agent-mobile-actions-height) + 2px), 0);
    transition-delay: 0s, 0s, 280ms;
  }

  .has-mobile-action-bar .ai-agent-chat {
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .has-mobile-action-bar .ai-agent-composer__input {
    flex: 0 1 auto;
    max-height: 4.8em;
  }

  .has-mobile-action-bar .ai-agent-sidebar__header,
  .has-mobile-action-bar .ai-agent-sidebar__footer {
    flex-shrink: 0;
  }

  .has-mobile-action-bar .ai-agent-mobile-attention {
    top: calc(var(--ai-agent-mobile-viewport-top) + var(--ai-agent-mobile-viewport-height) - var(--ai-agent-mobile-bottom-space) - 46px);
    max-width: calc(100% - 28px);
  }

  .ai-agent-shell.has-mobile-action-bar.is-mobile-keyboard-open {
    --ai-agent-mobile-actions-height: 64px;
    --ai-agent-mobile-tools-space: 52px;
    --ai-agent-mobile-bottom-space: calc(var(--ai-agent-mobile-actions-height) + var(--ai-agent-mobile-tools-space));
  }

  .has-mobile-action-bar.is-mobile-keyboard-open #aiAgentMobileActions {
    padding-top: 8px;
    padding-bottom: 10px;
  }

  .has-mobile-action-bar.is-mobile-keyboard-open #aiAgentMobileActions .ai-agent-mobile-action-row,
  .has-mobile-action-bar.is-mobile-keyboard-open #aiAgentMobileActions .ai-agent-mobile-action-row > button {
    height: 44px;
    min-height: 44px;
  }

  .has-mobile-action-bar.is-mobile-keyboard-open .ai-agent-sidebar__header {
    min-height: 40px;
    padding: 0 10px;
  }

  .has-mobile-action-bar.is-mobile-keyboard-compact .ai-agent-sidebar__header {
    display: none;
  }

  .has-mobile-action-bar.is-mobile-keyboard-open .ai-agent-sidebar__header .ai-agent-panel-toggle {
    min-height: 36px;
  }

  .has-mobile-action-bar.is-mobile-keyboard-open #agentWorkspaceStatus {
    display: none;
  }

  .has-mobile-action-bar.is-mobile-keyboard-open .ai-agent-sidebar__footer {
    padding: 4px 8px;
  }

  .has-mobile-action-bar.is-mobile-keyboard-open .ai-agent-composer {
    min-height: 72px;
    gap: 3px;
    padding: 4px 8px;
  }

  .has-mobile-action-bar.is-mobile-keyboard-open .ai-agent-composer__input {
    min-height: 1.6em;
    /* 正常高度可读三行，短视口给标题、发送与固定底栏留出空间。 */
    max-height: clamp(1.6em, calc(var(--ai-agent-mobile-viewport-height) - 194px), 4.8em);
  }

  .has-mobile-action-bar.is-mobile-keyboard-open .ai-agent-composer__main {
    min-height: 44px;
  }

  .has-mobile-action-bar.is-mobile-keyboard-open .ai-agent-composer__main > button {
    width: 44px;
    height: 44px;
  }

  .has-mobile-action-bar.is-mobile-keyboard-open .ai-agent-composer__notice {
    max-height: 16px;
  }
}

@media (max-width: 359px) {
  #aiAgentMobileActions .ai-agent-mobile-action-row {
    grid-template-columns: 72px minmax(0, 1fr) minmax(0, 1.22fr);
    gap: 6px;
  }

  #aiAgentMobileActions .ai-agent-mobile-action-row > button {
    padding-right: 5px;
    padding-left: 5px;
    font-size: 13px;
  }

  #aiAgentMobileActions #referenceAnalysisConfirm {
    font-size: 14px;
  }
}

@media (prefers-reduced-motion: reduce) {
  #mobileCanvasToolsToggle .ai-agent-mobile-tools-chevron {
    transition: none;
  }
}

@media (prefers-reduced-motion: reduce) and (max-width: 600px), (prefers-reduced-motion: reduce) and (max-width: 1000px) and (max-height: 600px) and (pointer: coarse) {
  .ai-agent-shell.has-mobile-action-bar .ai-agent-sidebar,
  #mobileCanvasToolsToggle {
    transition: none;
    transition-delay: 0s;
  }
}

@media (max-width: 600px), (max-width: 1000px) and (max-height: 600px) and (pointer: coarse) {
  .has-mobile-action-bar #canvasZoomControl,
  .has-mobile-action-bar #referenceAnalysisZoomControl {
    display: none !important;
  }

  /* 原图直接在画布上选择，手机不再保留独立缩略图条和常驻教学。 */
  .has-mobile-action-bar #referenceAnalysisFloating,
  .has-mobile-action-bar .ai-agent-analysis-crop-guidance {
    display: none !important;
  }

  .has-mobile-action-bar #cropExample {
    width: max-content;
  }

  .has-mobile-action-bar .ai-agent-canvas-topbar {
    pointer-events: none;
  }

  .has-mobile-action-bar .ai-agent-canvas-topbar > * {
    pointer-events: auto;
  }

  .has-mobile-action-bar #cropExamplePanel {
    width: 300px;
    max-width: 100%;
  }

  .has-mobile-action-bar #referenceAnalysisSources {
    gap: 32px;
  }
}
