/* Shared mobile layout for the workspace, image studio and legacy batch page. */
@media (max-width: 820px) {
  html { overflow-x: hidden; }
  body, #app { width: 100%; min-width: 0; overflow-x: hidden; }
  img, video, canvas { max-width: 100%; }

  /* Shared header: compact controls on row one, scrollable navigation on row two. */
  .topbar {
    height: auto !important;
    min-height: 62px;
    display: flex;
    flex-wrap: wrap;
    align-content: center;
    gap: 0 8px;
    padding: 8px 12px 0 !important;
  }
  .topbar .brand { order: 1; min-width: 0; gap: 8px; font-size: 17px; }
  .topbar .brand-mark { width: 32px; height: 32px; flex: 0 0 32px; border-radius: 8px; }
  .topbar .brand > span:last-child { min-width: 0; white-space: nowrap; }
  .topbar .brand small { font-size: 9px; }
  .topbar .topbar-spacer { order: 2; }
  .topbar .auth-entry, .topbar .user-chip, .topbar #auth-slot { order: 3; flex: 0 0 auto; }
  .topbar .settings-button {
    order: 4;
    flex: 0 0 34px;
    width: 34px;
    height: 34px;
    padding: 0;
    justify-content: center;
    font-size: 0;
  }
  .topbar .settings-button > span { font-size: 15px; }
  .topbar .site-nav {
    order: 10;
    width: calc(100% + 24px);
    max-width: none;
    min-width: 0;
    margin: 8px -12px 0 !important;
    padding: 0 12px 7px;
    display: flex !important;
    flex: 0 0 calc(100% + 24px);
    gap: 5px !important;
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  .topbar .site-nav::-webkit-scrollbar { display: none; }
  .topbar .site-nav-link {
    flex: 0 0 auto;
    min-width: max-content;
    padding: 7px 10px !important;
    border: 1px solid transparent;
    border-radius: 7px;
    font-size: 11px !important;
    line-height: 1.2;
    white-space: nowrap;
    word-break: keep-all;
  }
  .topbar .site-nav-link.active { border-color: #d8e5fa; }

  /* Shared content rhythm. */
  .market-shell, .spaces-shell, .batch-shell, .video-shell, .retouch-shell {
    width: 100%;
    padding: 20px 14px 40px !important;
  }
  .hero-row, .batch-page-head, .video-page-head, .retouch-head { margin-bottom: 18px; }
  .hero-row h1, .batch-page-head h1, .video-page-head h1, .retouch-head h1, .spaces-heading h1 {
    font-size: clamp(24px, 8vw, 30px);
    line-height: 1.2;
  }
  .hero-copy, .batch-page-head p:not(.eyebrow), .video-page-head p:not(.eyebrow), .retouch-head p:not(.eyebrow) {
    font-size: 12px;
    line-height: 1.65;
  }
  .hero-stats { width: 100%; gap: 8px; }
  .stat { min-width: 0; padding: 10px 12px; }
  .stat strong { font-size: 17px; }
  .stat span { font-size: 10px; }
  .market-category-row { min-width: 0; margin-bottom: 10px; overflow: hidden; }
  .filter-pills { width: 100%; flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
  .filter-pills::-webkit-scrollbar { display: none; }
  .pill { flex: 0 0 auto; padding: 7px 11px; font-size: 11px; }
  .section-label { margin-bottom: 8px; }

  /* App cards and personal workspace. */
  .app-card { min-height: 235px; padding: 15px; }
  .app-head { gap: 10px; }
  .app-icon { width: 44px; height: 44px; flex-basis: 44px; }
  .card-footer { gap: 7px; padding-top: 14px; }
  .card-actions { min-width: 0; }
  .card-actions .btn { padding-inline: 10px; }
  .spaces-heading { margin: 24px 0 15px; }
  .spaces-actions { width: 100%; display: grid; grid-template-columns: 1fr 1fr; }
  .spaces-actions .btn { min-width: 0; }

  /* Chat controls and composer remain inside the viewport. */
  .chat-shell { min-height: calc(100dvh - 104px); }
  .chat-top { padding: 13px 14px; gap: 10px; }
  .chat-top > .app-icon { width: 40px; height: 40px; flex-basis: 40px; }
  .chat-top > div:nth-child(2) { min-width: 0; flex: 1; }
  .chat-top h1 { overflow: hidden; font-size: 16px; text-overflow: ellipsis; white-space: nowrap; }
  .chat-top p { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .chat-top .topbar-spacer { display: none; }
  .chat-model-control { width: 100%; margin: 2px 0 0; grid-template-columns: 42px minmax(0, 1fr); }
  .chat-top > [data-clear-chat] { margin-left: auto; }
  .chat-messages { padding: 18px 12px; }
  .chat-compose { padding: 10px 12px max(10px, env(safe-area-inset-bottom)); }
  .welcome { padding: 17px; }
  .message { gap: 7px; margin: 14px auto; }
  .bubble, .message.assistant .bubble { max-width: calc(100vw - 70px); padding: 10px 12px; font-size: 12px; }
  .composer, .composer textarea { min-width: 0; }
  .quick-prompts { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .quick-prompts::-webkit-scrollbar { display: none; }
  .quick { flex: 0 0 auto; }

  /* Admin modules become a horizontal module bar, not a narrow icon rail. */
  .admin-shell { display: block; min-height: calc(100dvh - 104px); }
  .admin-side {
    position: sticky;
    top: 102px;
    z-index: 15;
    width: 100%;
    padding: 7px 10px;
    overflow: hidden;
    border-right: 0;
    border-bottom: 1px solid #243c60;
  }
  .admin-brand, .admin-label, .admin-side-bottom { display: none; }
  .admin-nav { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
  .admin-nav::-webkit-scrollbar { display: none; }
  .admin-nav button {
    flex: 0 0 auto;
    width: auto;
    min-width: max-content;
    padding: 8px 11px;
    border-radius: 7px;
    font-size: 11px !important;
    line-height: 1.2;
    white-space: nowrap;
  }
  .admin-nav button::first-letter { font-size: inherit !important; }
  .admin-nav-group { display:flex; flex:0 0 auto; flex-direction:row; align-items:center; gap:6px; }
  .admin-nav-group-toggle { flex:0 0 auto; width:auto; min-width:max-content; gap:6px; }
  .admin-nav-group-toggle .admin-nav-chevron { margin-left:0; font-size:11px; }
  .admin-nav-group-items { flex-direction:row; flex:0 0 auto; gap:4px; margin:0; padding:0 0 0 6px; border-left:1px solid #526987; }
  .admin-nav-group-items button { flex:0 0 auto; width:auto; min-width:max-content; }
  .admin-nav-group-items[hidden] { display:none; }
  .admin-content { width: 100%; min-width: 0; padding: 18px 12px 40px; }
  .admin-top { align-items: flex-start; flex-direction: column; gap: 12px; margin-bottom: 16px; }
  .admin-top h1 { font-size: 24px; }
  .admin-top > .btn, .admin-count-badge { align-self: stretch; }
  .metrics { gap: 8px; }
  .metric { padding: 13px; }
  .panel { min-width: 0; }
  .panel-head, .workflow-admin-section > summary { padding: 13px; }
  .panel-head { align-items: flex-start; gap: 8px; }
  .table-wrap { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .table-wrap table { min-width: 680px; }
  .email-form-body { padding: 13px; }
  .email-save-bar { position: sticky; bottom: 0; z-index: 5; align-items: stretch; flex-direction: column; padding: 10px; }
  .email-save-bar .btn { width: 100%; }
  .batch-admin-field-summary { min-width: 0; }
  .batch-admin-field-summary > span:nth-child(2) { min-width: 0; }
  .batch-admin-field-summary strong, .batch-admin-field-summary small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  /* Batch, clone, retouch, replacement and video workspaces. */
  .batch-workspace, .video-layout, .retouch-layout { grid-template-columns: 1fr; gap: 12px; }
  .batch-config, .video-config, .retouch-config { order: 1; }
  .batch-main, .video-main, .retouch-main { order: 2; }
  .batch-config { grid-template-columns: 1fr 1fr; gap: 10px; }
  .batch-section, .video-section, .retouch-section { padding: 14px; }
  .batch-actionbar { gap: 12px; padding: 13px; }
  .batch-actionbar > div:last-child { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; }
  .batch-actionbar > div:last-child .btn { width: 100%; min-width: 0; padding-inline: 8px; white-space: normal; }
  .batch-result-grid, .retouch-result-grid { gap: 8px; padding: 10px; }
  .batch-history-grid, .retouch-history-grid, .video-history-grid { gap: 8px; padding: 10px; }
  .batch-result-tools, .retouch-toolbar { width: 100%; justify-content: flex-start; }
  .batch-result-tools .btn, .retouch-toolbar .btn { flex: 1 1 auto; min-width: 0; }
  .batch-dropzone, .retouch-dropzone { min-height: 92px; }
  .video-output-head, .retouch-output-head, .image-output-head { min-width: 0; gap: 8px; }
  .retouch-output-head { align-items: flex-start; flex-wrap: wrap; }
  .retouch-toolbar { order: 3; }
  .retouch-file-list { max-height: 260px; }
  .product-replace-shell .retouch-config { position: static; max-height: none; }

  /* Standalone image studio. */
  .image-workbench { min-height: 0; }
  .image-config-panel { padding: 16px 12px 18px; }
  .image-panel-heading { padding-bottom: 15px; margin-bottom: 12px; }
  .image-panel-heading .back-link { margin-bottom: 13px; }
  .image-panel-heading h1 { font-size: 21px; }
  .image-config-form { gap: 10px; }
  .config-card { padding: 13px; border-radius: 10px; }
  .config-card-title { margin-bottom: 11px; }
  .prompt-card textarea { min-height: 112px; }
  .image-output-panel { padding: 12px; gap: 12px; grid-template-rows: auto auto; }
  .image-output-card { border-radius: 11px; }
  .image-result-area { min-height: 260px; padding: 12px; }
  .image-result-content .image-grid { gap: 9px; }
  .history-output-card .history-list { padding: 10px; }

  dialog { max-width: calc(100vw - 20px); max-height: calc(100dvh - 20px); margin: auto; }
  dialog form { max-height: calc(100dvh - 20px); overflow: auto; }

  /* Legacy /batch-img preview receives the same mobile behavior. */
  .batch-topbar { height: auto; min-height: 62px; display: flex; flex-wrap: wrap; gap: 0 8px; padding: 8px 10px 0; }
  .batch-brand { order: 1; }
  .batch-spacer { order: 2; }
  .batch-auth { order: 3; width: 32px; height: 32px; padding: 0; justify-content: center; }
  .batch-settings { order: 4; width: 34px; height: 34px; padding: 0; }
  .batch-nav { order: 10; width: calc(100% + 20px); display: flex; flex: 0 0 calc(100% + 20px); gap: 5px; margin: 8px -10px 0; padding: 0 10px 7px; overflow-x: auto; scrollbar-width: none; }
  .batch-nav::-webkit-scrollbar { display: none; }
  .batch-nav a { flex: 0 0 auto; min-width: max-content; padding: 7px 10px; font-size: 11px; white-space: nowrap; }
  .batch-sidebar, .batch-workspace { order: initial; }
  .workspace-toolbar { flex-wrap: wrap; }
  .toolbar-actions { width: 100%; }
  .toolbar-actions button { flex: 1; font-size: 11px; }
  .primary-button:first-letter { font-size: inherit; }
  .pipeline { padding: 10px; }
  .pipeline-step { flex: 0 0 auto; }
}

@media (max-width: 700px) {
  .batch-editor-backdrop {
    align-items: stretch;
    padding: 0;
    overscroll-behavior: contain;
  }
  .batch-editor {
    width: 100%;
    height: 100vh;
    height: 100dvh;
    max-height: 100dvh;
    grid-template-rows: auto minmax(0, 1fr) auto;
    border: 0;
    border-radius: 0;
  }
  .batch-editor-head { min-width: 0; padding: 9px 10px; gap: 8px; }
  .batch-editor-head > div:first-child { gap: 2px; }
  .batch-editor-head small { line-height: 1.35; }
  .batch-editor-head-actions { flex: 0 0 auto; flex-wrap: nowrap; gap: 3px; }
  .batch-editor-head-actions .btn, .batch-editor-head-actions .batch-image-preview-close { width: 32px; min-width: 32px; height: 32px; padding: 0; }
  .batch-editor-head-actions output { min-width: 38px; }
  .batch-editor-body {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(96px, 32dvh) minmax(130px, 1fr);
  }
  .batch-editor-tools {
    min-height: 0;
    max-height: 32dvh;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-auto-rows: min-content;
    gap: 7px;
    padding: 8px 10px;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    border-right: 0;
    border-bottom: 1px solid #e2e8f2;
  }
  .batch-editor-tool-row, .batch-editor-command-row, .batch-editor-reference-upload { grid-column: 1 / -1; }
  .batch-editor-tools label { gap: 3px; }
  .batch-editor-tools input[type=color] { height: 27px; }
  .batch-editor-tools input[type=text] { min-width: 0; padding: 6px; }
  .batch-editor-stage { min-width: 0; min-height: 0; padding: 8px; overscroll-behavior: contain; }
  .batch-editor-canvas-wrap { min-width: 0; min-height: 0; }
  .batch-editor-foot { padding: 8px 10px max(8px, env(safe-area-inset-bottom)); }
  .batch-editor-foot > label { margin-bottom: 4px; }
  .batch-editor-foot > div { grid-template-columns: minmax(0, 1fr) auto; gap: 7px; align-items: stretch; }
  .batch-editor-foot textarea { box-sizing: border-box; min-width: 0; min-height: 42px; max-height: 20dvh; padding: 7px; }
  .batch-editor-foot .btn { min-width: 76px; padding: 8px 10px; }
  .batch-editor-foot p { min-height: 0; margin-top: 3px; }
}

@media (max-width: 560px) {
  .topbar { padding-inline: 10px !important; }
  .topbar .site-nav { width: calc(100% + 20px); flex-basis: calc(100% + 20px); margin-inline: -10px !important; padding-inline: 10px; }
  .topbar .brand small { display: none; }
  .topbar .auth-entry, .topbar .user-chip { width: 32px; height: 32px; padding: 0; justify-content: center; }
  .topbar .avatar { width: 30px; height: 30px; flex-basis: 30px; }

  .market-shell, .spaces-shell, .batch-shell, .video-shell, .retouch-shell { padding: 16px 10px 34px !important; }
  .hero-row { gap: 15px; }
  .hero-copy { margin-top: 8px; }
  .app-grid { gap: 10px; }
  .app-card { min-height: 0; }
  .app-title h3 { white-space: normal; }
  .app-desc { -webkit-line-clamp: 2; }
  .card-footer { align-items: flex-end; }
  .card-actions { gap: 5px; }
  .share-role { width: 32px; padding: 0; font-size: 0; }
  .share-role span { font-size: 14px; }
  .card-actions .btn { padding: 8px 10px; }

  .admin-side { top: 94px; }
  .admin-content { padding-inline: 10px; }
  .metrics { grid-template-columns: 1fr 1fr; }
  .workflow-admin-section > summary { align-items: flex-start; }
  .workflow-admin-section > summary > button { flex: 0 0 auto; }
  .clone-admin-limit-grid { grid-template-columns: 1fr; }
  .prompt-section textarea { height: 180px; max-height: 45vh; }

  .batch-config { grid-template-columns: 1fr; }
  .batch-config .batch-section:first-child { grid-column: auto; }
  .batch-page-head, .video-page-head, .retouch-head { gap: 10px; }
  .batch-actionbar > div:last-child { grid-template-columns: 1fr; }
  .batch-result-grid, .retouch-result-grid { grid-template-columns: 1fr; }
  .batch-history-grid, .retouch-history-grid { grid-template-columns: 1fr 1fr; }
  .video-upload-row { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .video-dropzone { min-width: 0; padding: 8px 3px; }
  .video-dropzone small { font-size: 8px; }
  .video-history-grid { grid-template-columns: 1fr; }
  .clone-plan-summary { grid-template-columns: 24px 42px minmax(0, 1fr); }
  .clone-plan-actions { grid-column: 3; width: 100%; justify-content: space-between; }
  .clone-plan-actions .btn { flex: 0 0 auto; }
  .clone-plan-body .batch-plan-fields { grid-template-columns: 1fr; }
  .batch-editor-head { align-items: flex-start; }
  .batch-editor-head-actions { flex-wrap: wrap; justify-content: flex-end; }

  .image-workbench .generation-controls { grid-template-columns: 1fr; }
  .image-workbench .generation-controls label:first-child { grid-column: auto; }
  .image-config-panel .reference-list { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .image-output-head { min-height: 56px; padding: 12px; }
  .generated-image-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .result-action { min-width: 0; padding-inline: 5px; }
  .history-output-card .history-item { grid-template-columns: 52px minmax(0, 1fr); }
  .history-output-card .history-preview { width: 52px; height: 52px; }

  .field-grid { grid-template-columns: 1fr; }
  .field.full { grid-column: auto; }
  .uploaded-list { grid-template-columns: repeat(4, 1fr); }
  .result-grid { grid-template-columns: 1fr; }
  .results-heading, .config-section { padding: 13px; }
}
