/* Mobile-only Homepage Workspace (Plan A) — Zero Desktop Impact */
/* Strictly hidden on desktop (> 768px) so PC layout & scripts stay 100% untouched. */
.m-home-app,
.m-sheet-backdrop,
.m-toast {
  display: none !important;
}

@media (max-width: 768px) {
  body.home-page {
    background: #f8fafc;
    overflow-x: hidden;
  }

  /* Hide legacy desktop stacked editor on mobile */
  body.home-page #grid-tool {
    display: none !important;
  }

  /* Compact single-row site header on mobile homepage */
  body.home-page .header-container {
    position: relative;
    z-index: 1500;
    width: 100% !important;
    max-width: 100% !important;
    padding: 6px 10px !important;
    background: #ffffff;
    border-bottom: 1px solid #e2e8f0;
  }

  body.home-page .header-container .navbar.has-header-ad {
    display: flex !important;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    height: 40px !important;
    min-height: 40px !important;
    padding: 0 !important;
  }

  body.home-page .header-ad-slot {
    display: none !important;
  }

  body.home-page .nav-left {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    display: flex;
    align-items: center;
    gap: 6px;
  }

  body.home-page .site-logo {
    width: 30px !important;
    height: 30px !important;
    object-fit: contain !important;
    flex-shrink: 0;
  }

  body.home-page .nav-items {
    flex: 1 1 auto;
    min-width: 0;
    margin-left: 4px !important;
    padding-left: 0 !important;
    padding-right: 16px !important;
    border-left: 0 !important;
    height: 34px !important;
    gap: 4px !important;
    flex-wrap: nowrap !important;
    overflow-x: auto;
    scrollbar-width: none;
    -ms-overflow-style: none;
    -webkit-mask-image: linear-gradient(90deg, #000 88%, transparent 100%);
    mask-image: linear-gradient(90deg, #000 88%, transparent 100%);
  }

  body.home-page .nav-items::-webkit-scrollbar {
    display: none;
  }

  body.home-page .nav-link {
    flex: 0 0 auto;
    height: 30px !important;
    padding: 0 9px !important;
    font-size: 12px !important;
    border-radius: 999px !important;
  }

  body.home-page .language-selector {
    position: relative;
    z-index: 1501;
    flex-shrink: 0;
  }

  body.home-page .language-menu {
    z-index: 1502 !important;
  }

  body.home-page .language-btn {
    margin: 0 !important;
    padding: 5px 8px !important;
    height: 32px;
  }

  /* Avoid feedback FAB overlapping bottom Save button while in top editor */
  body.home-page:not(.m-scrolled-past-editor) #feedbackFab,
  body.home-page:not(.m-scrolled-past-editor) .feedback-fab {
    opacity: 0 !important;
    visibility: hidden !important;
    pointer-events: none !important;
  }

  /* Hero section stays below mobile editor for SEO & first-screen focus */
  body.home-page .hero-section--home {
    width: 100% !important;
    padding: 24px 16px 8px !important;
  }

  body.home-page .hero-section--home .hero-title-elegant h1 {
    font-size: clamp(22px, 6vw, 30px) !important;
    line-height: 1.25 !important;
  }

  body.home-page .hero-section--home .hero-title-elegant .subtitle {
    font-size: 14px !important;
    line-height: 1.55 !important;
  }

  /* =====================================================================
     Mobile Workspace (Plan A Finalized)
     ===================================================================== */
  .m-home-app {
    display: flex !important;
    flex-direction: column;
    width: 100%;
    height: calc(100dvh - 53px);
    min-height: 540px;
    max-height: 820px;
    background: #ffffff;
    overflow: hidden;
    position: relative;
    border-bottom: 1px solid #e2e8f0;
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "PingFang SC", "Helvetica Neue", sans-serif;
    -webkit-tap-highlight-color: transparent;
  }

  /* Neutralize legacy global rules from main.css inside mobile workspace */
  .m-home-app *,
  .m-sheet-backdrop * {
    box-sizing: border-box;
  }

  .m-home-app button,
  .m-home-app input,
  .m-home-app select,
  .m-sheet-backdrop button,
  .m-sheet-backdrop input {
    margin: 0 !important;
  }

  /* Dark Photo Editor Canvas Stage */
  .m-stage-wrapper {
    flex: 1 1 auto;
    min-height: 0;
    position: relative;
    background: #0f172a;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .m-edit-stage {
    flex: 1 1 auto;
    min-height: 0;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 12px 12px 44px;
    overflow: hidden;
    user-select: none;
  }

  .m-home-app #mCropCanvas {
    display: block;
    width: auto !important;
    height: auto !important;
    max-width: 100% !important;
    max-height: 100% !important;
    border: 0 !important;
    margin: 0 !important;
    border-radius: 6px;
    touch-action: none;
    cursor: grab;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  }

  .m-home-app #mCropCanvas:active {
    cursor: grabbing;
  }

  .m-stage-hint {
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(15, 23, 42, 0.72);
    color: #e2e8f0;
    border: 1px solid rgba(255, 255, 255, 0.12);
    padding: 4px 10px;
    border-radius: 999px;
    font-size: 11px;
    white-space: nowrap;
    max-width: calc(100% - 24px);
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
    z-index: 4;
    transition: opacity 0.35s ease;
  }

  .m-stage-hint.is-faded {
    opacity: 0;
  }

  .m-empty-upload {
    position: absolute;
    inset: 16px;
    border: 2px dashed rgba(148, 163, 184, 0.5);
    border-radius: 16px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    color: #e2e8f0;
    background: rgba(30, 41, 59, 0.65);
    cursor: pointer;
    text-align: center;
    padding: 20px;
    z-index: 6;
  }

  .m-empty-upload.is-hidden {
    display: none;
  }

  /* Floating Zoom Bar */
  .m-zoom-bar {
    position: absolute;
    left: 12px;
    right: 12px;
    bottom: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 5px 10px;
    border-radius: 999px;
    background: rgba(15, 23, 42, 0.8);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.12);
    color: #e2e8f0;
    z-index: 5;
  }

  .m-zoom-icon-btn {
    width: 26px;
    height: 26px;
    border: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.12);
    color: #ffffff;
    font-size: 16px;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
  }

  .m-zoom-slider {
    flex: 1;
    height: 4px;
    accent-color: #60a5fa;
    cursor: pointer;
  }

  .m-zoom-pct {
    font-size: 11px;
    font-weight: 600;
    min-width: 34px;
    text-align: right;
    color: #cbd5e1;
    font-variant-numeric: tabular-nums;
  }

  .m-reset-btn {
    border: 0;
    background: rgba(255, 255, 255, 0.14);
    color: #ffffff;
    font-size: 11px;
    font-weight: 600;
    padding: 4px 9px;
    border-radius: 999px;
    cursor: pointer;
    flex-shrink: 0;
  }

  /* Adaptive Full-Width Controls Deck */
  .m-controls-deck {
    flex: 0 0 auto;
    background: #ffffff;
    border-top: 1px solid #e2e8f0;
    padding: 8px 12px 6px;
    display: flex;
    flex-direction: column;
    gap: 7px;
    z-index: 9;
  }

  .m-control-row {
    display: flex;
    align-items: center;
    gap: 6px;
    overflow-x: auto;
    scrollbar-width: none;
    -ms-overflow-style: none;
    padding-bottom: 1px;
  }

  .m-control-row::-webkit-scrollbar {
    display: none;
  }

  .m-row-icon {
    width: 20px;
    height: 20px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: #64748b;
    flex-shrink: 0;
  }

  .m-chip {
    flex: 1 0 auto;
    height: 30px;
    line-height: 1;
    border: 1px solid #e2e8f0;
    background: #f8fafc;
    color: #475569;
    font-size: 12px;
    font-weight: 600;
    padding: 0 11px;
    border-radius: 999px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    white-space: nowrap;
    transition: all 0.15s ease;
  }

  .m-chip.active {
    background: #2563eb;
    border-color: #2563eb;
    color: #ffffff;
    box-shadow: 0 2px 6px rgba(37, 99, 235, 0.25);
  }

  .m-chip.m-platform-tiktok.active {
    background: #0f172a;
    border-color: #0f172a;
    color: #ffffff;
    box-shadow: 0 2px 6px rgba(15, 23, 42, 0.25);
  }

  .m-chip.m-platform-ig.active {
    background: linear-gradient(135deg, #a855f7, #ec4899);
    border-color: transparent;
    color: #ffffff;
    box-shadow: 0 2px 6px rgba(168, 85, 247, 0.28);
  }

  .m-chip-divider {
    width: 1px;
    height: 18px;
    background: #e2e8f0;
    flex-shrink: 0;
    margin: 0 1px;
  }

  /* Keep Instagram's 6 platform+ratio chips (Instagram, TikTok, 3:4, 9:16, 4:5, 1:1) 100% visible without clipping */
  #mRatioRow {
    gap: 4px;
  }

  #mRatioRow .m-chip {
    padding: 0 7px;
    font-size: 11.5px;
  }

  /* Custom Rows/Cols Stepper (No keyboard popup) */
  .m-stepper-bar {
    display: none;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 7px 10px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
  }

  .m-stepper-bar.is-open {
    display: flex;
  }

  .m-stepper-group {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .m-stepper-title {
    font-size: 12px;
    font-weight: 600;
    color: #475569;
  }

  .m-stepper {
    display: inline-flex;
    align-items: center;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-radius: 9px;
    overflow: hidden;
  }

  .m-stepper button {
    width: 30px;
    height: 28px;
    border: 0;
    background: transparent;
    font-size: 16px;
    font-weight: 700;
    color: #0f172a;
    cursor: pointer;
  }

  .m-stepper button:active {
    background: #e2e8f0;
  }

  .m-stepper span {
    min-width: 24px;
    text-align: center;
    font-size: 13px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
  }

  /* Balanced Bottom Action Bar (Thumb Zone) */
  .m-bottom-bar {
    flex: 0 0 auto;
    padding: 8px 12px calc(10px + env(safe-area-inset-bottom, 0px));
    background: #ffffff;
    border-top: 1px solid #e2e8f0;
    display: flex;
    align-items: center;
    gap: 8px;
    z-index: 10;
  }

  .m-btn-label {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .m-btn-upload {
    flex: 0.9 1 auto;
    min-width: 0;
    height: 44px;
    padding: 0 clamp(10px, 2.8vw, 14px);
    border-radius: 12px;
    border: 1px solid #cbd5e1;
    background: #ffffff;
    color: #0f172a;
    font-size: clamp(12px, 3.3vw, 13.5px);
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    white-space: nowrap;
  }

  .m-btn-upload:active {
    background: #f1f5f9;
  }

  .m-btn-joy-preview {
    flex: 1.05 1 auto;
    min-width: 0;
    position: relative;
    overflow: hidden;
    height: 44px;
    padding: 0 clamp(10px, 3vw, 14px);
    border: 0;
    border-radius: 12px;
    background: linear-gradient(135deg, #ff5789 0%, #a855f7 56%, #6366f1 100%);
    color: #ffffff;
    font-size: clamp(12px, 3.3vw, 13.5px);
    font-weight: 700;
    letter-spacing: 0.01em;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    white-space: nowrap;
    box-shadow: 0 6px 18px rgba(168, 85, 247, 0.34), inset 0 1px 0 rgba(255, 255, 255, 0.35);
    transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
  }

  .m-btn-joy-preview::after {
    content: "";
    position: absolute;
    top: -30%;
    left: -75%;
    width: 48%;
    height: 160%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.36), transparent);
    transform: skewX(-24deg);
    animation: mJoySheen 4.2s ease-in-out infinite;
    pointer-events: none;
  }

  .m-btn-joy-preview:active {
    transform: scale(0.95);
  }

  .m-joy-sparkle {
    flex-shrink: 0;
    animation: mJoyTwinkle 2.6s ease-in-out infinite;
  }

  @keyframes mJoySheen {
    0%, 62% { left: -75%; opacity: 0; }
    68% { opacity: 1; }
    88% { left: 135%; opacity: 1; }
    100% { left: 135%; opacity: 0; }
  }

  @keyframes mJoyTwinkle {
    0%, 100% { transform: scale(1) rotate(0deg); }
    50% { transform: scale(1.16) rotate(8deg); }
  }

  @media (prefers-reduced-motion: reduce) {
    .m-btn-joy-preview::after,
    .m-joy-sparkle {
      animation: none;
    }
  }

  .m-btn-save {
    flex: 1.25 1 auto;
    min-width: 0;
    overflow: hidden;
    height: 44px;
    padding: 0 clamp(10px, 3vw, 16px);
    border-radius: 12px;
    border: 0;
    background: linear-gradient(135deg, #2563eb, #1d4ed8);
    color: #ffffff;
    font-size: clamp(12px, 3.4vw, 14px);
    font-weight: 700;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    box-shadow: 0 6px 16px rgba(37, 99, 235, 0.28);
    white-space: nowrap;
  }

  .m-btn-save:active {
    transform: scale(0.97);
  }

  .m-btn-save:disabled {
    opacity: 0.65;
    cursor: wait;
  }

  @media (max-width: 340px) {
    .m-btn-upload {
      flex: 0 0 44px;
      padding: 0;
    }
    .m-btn-upload span {
      display: none;
    }
  }

  .m-sr-input {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
  }

  /* Bottom Sheets (Preview & Export) */
  .m-sheet-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.56);
    backdrop-filter: blur(4px);
    z-index: 2147482000;
    display: none !important;
    align-items: flex-end;
    justify-content: center;
  }

  .m-sheet-backdrop.is-open {
    display: flex !important;
  }

  .m-sheet-panel {
    width: 100%;
    max-height: 88dvh;
    background: #ffffff;
    color: #0f172a;
    border-radius: 22px 22px 0 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: 0 -12px 40px rgba(15, 23, 42, 0.28);
    animation: mSheetUp 0.22s cubic-bezier(0.22, 1, 0.36, 1);
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "PingFang SC", sans-serif;
  }

  .m-sheet-panel.is-dark {
    background: #0b0b0f;
    color: #ffffff;
  }

  @keyframes mSheetUp {
    from { transform: translateY(28px); opacity: 0.7; }
    to { transform: translateY(0); opacity: 1; }
  }

  .m-sheet-handle {
    width: 36px;
    height: 4px;
    border-radius: 999px;
    background: #cbd5e1;
    margin: 8px auto 2px;
    flex-shrink: 0;
  }

  .m-sheet-panel.is-dark .m-sheet-handle {
    background: rgba(255, 255, 255, 0.25);
  }

  .m-sheet-header {
    padding: 8px 16px 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #e2e8f0;
    flex-shrink: 0;
  }

  .m-sheet-panel.is-dark .m-sheet-header {
    border-bottom-color: rgba(255, 255, 255, 0.1);
  }

  .m-sheet-title {
    margin: 0;
    font-size: 16px;
    font-weight: 700;
  }

  .m-sheet-close {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 0;
    background: #f1f5f9;
    color: #334155;
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .m-sheet-panel.is-dark .m-sheet-close {
    background: rgba(255, 255, 255, 0.14);
    color: #ffffff;
  }

  .m-sheet-body {
    padding: 14px 16px calc(18px + env(safe-area-inset-bottom, 0px));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .m-sheet-body.no-pad {
    padding: 0 0 calc(16px + env(safe-area-inset-bottom, 0px));
  }

  /* Pure Profile Preview Simulation inside Bottom Sheet */
  .m-profile-sim {
    width: 100%;
    display: flex;
    flex-direction: column;
  }

  .m-profile-header img {
    display: block;
    width: 100%;
    height: auto;
  }

  .m-ig-tabs {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    border-top: 1px solid #efefef;
    border-bottom: 1px solid #efefef;
    background: #ffffff;
  }

  .m-ig-tab {
    border: 0;
    border-bottom: 2px solid transparent;
    background: transparent;
    padding: 9px 0;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    opacity: 0.45;
  }

  .m-ig-tab.active {
    border-bottom-color: #111827;
    opacity: 1;
  }

  .m-ig-tab:disabled {
    cursor: default;
    opacity: 0.3;
  }

  .m-ig-tab img {
    width: 20px;
    height: 20px;
    object-fit: contain;
  }

  .m-profile-grid {
    display: grid;
    gap: 2px;
    padding: 2px 0 18px;
    width: 100%;
  }

  .m-profile-tile {
    position: relative;
    overflow: hidden;
    background: #1f2937;
  }

  .m-profile-tile canvas {
    display: block !important;
    width: 100% !important;
    height: 100% !important;
    border: 0 !important;
    margin: 0 !important;
  }

  /* Export Sheet Content */
  .m-order-banner {
    background: #eff6ff;
    border: 1px solid #bfdbfe;
    color: #1e40af;
    border-radius: 12px;
    padding: 10px 12px;
    font-size: 12px;
    line-height: 1.5;
    margin-bottom: 12px;
  }

  .m-export-grid {
    display: grid;
    gap: 8px;
    margin-bottom: 14px;
  }

  .m-export-card {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 10px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  .m-export-media {
    position: relative;
    background: #0f172a;
  }

  .m-export-media img {
    display: block;
    width: 100%;
    height: auto;
  }

  .m-export-pill {
    position: absolute;
    top: 5px;
    left: 5px;
    background: rgba(15, 23, 42, 0.85);
    color: #ffffff;
    font-size: 10px;
    font-weight: 700;
    padding: 2px 6px;
    border-radius: 6px;
  }

  .m-export-dl {
    border-top: 1px solid #e2e8f0;
    background: #ffffff;
    color: #2563eb;
    font-size: 11px;
    font-weight: 700;
    padding: 7px 4px;
    text-decoration: none;
    text-align: center;
  }

  .m-export-actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .m-export-share-btn {
    width: 100%;
    height: 46px;
    border: 0;
    border-radius: 12px;
    background: linear-gradient(135deg, #2563eb, #1d4ed8);
    color: #ffffff;
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    box-shadow: 0 8px 20px rgba(37, 99, 235, 0.28);
  }

  .m-export-zip-btn {
    width: 100%;
    height: 42px;
    border: 1px solid #cbd5e1;
    border-radius: 12px;
    background: #ffffff;
    color: #0f172a;
    font-size: 13px;
    font-weight: 600;
    text-decoration: none;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
  }

  .m-export-tip {
    margin: 10px 0 0;
    font-size: 11px;
    color: #64748b;
    text-align: center;
    line-height: 1.5;
  }

  /* Toast */
  .m-toast {
    display: block !important;
    position: fixed;
    left: 50%;
    bottom: 74px;
    transform: translateX(-50%) translateY(10px);
    background: rgba(15, 23, 42, 0.92);
    color: #ffffff;
    padding: 9px 14px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
    max-width: 90vw;
    overflow: hidden;
    text-overflow: ellipsis;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease, transform 0.2s ease;
    z-index: 2147482500;
  }

  .m-toast.is-show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}
