/**
 * Platform-wide mobile layout (≤1023px).
 * Complements per-section tweaks (e.g. Theme Analyzer).
 */
@media (max-width: 1023px) {
  :root {
    --platform-mobile-gutter: 1.125rem;
  }

  /* Align topbar with main content inset (safe-area aware) */
  #topbar {
    padding-top: 0.5rem !important;
    padding-bottom: 0.5rem !important;
    padding-left: max(var(--platform-mobile-gutter), env(safe-area-inset-left, 0px)) !important;
    padding-right: max(var(--platform-mobile-gutter), env(safe-area-inset-right, 0px)) !important;
  }

  /* Main column gutter — sidebar is overlay on mobile */
  main {
    padding-left: max(var(--platform-mobile-gutter), env(safe-area-inset-left, 0px));
    padding-right: max(var(--platform-mobile-gutter), env(safe-area-inset-right, 0px));
    padding-bottom: max(0.75rem, env(safe-area-inset-bottom, 0px));
    box-sizing: border-box;
  }

  /* ---- Section spacing (vertical; horizontal from main) ---- */
  main > section > .p-6,
  main section > .p-6:first-child,
  main > section.p-6 {
    padding-top: 1rem !important;
    padding-bottom: 1rem !important;
    padding-left: 0 !important;
    padding-right: 0 !important;
  }

  main section .card > .p-6,
  main section .card > .p-4,
  main section .purple-glow-card > .p-6,
  main section .card.p-4,
  main section .card.p-5 {
    padding: 1rem 1.125rem !important;
  }

  main section .card-header {
    padding: 0.875rem 1.125rem !important;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  /* Card toolbars: title above actions */
  main section .platform-mobile-toolbar-row,
  main section .card-header > .flex.items-center.justify-between,
  main section .card-header .flex.justify-between {
    flex-direction: column;
    align-items: stretch !important;
    gap: 0.65rem;
  }

  main section .platform-mobile-toolbar-row > .flex.items-center.gap-2:last-child,
  main section .card-header .flex.justify-between > div:last-child:not(:only-child) {
    width: 100%;
    flex-wrap: wrap;
    justify-content: flex-start;
    gap: 0.5rem;
  }

  main section .card-header button,
  main section .platform-mobile-card-toolbar button {
    min-height: 2.5rem;
  }

  /* ---- Grids: stack layouts without responsive prefix ---- */
  main section .grid.grid-cols-2:not(.platform-mobile-keep-cols-2):not(.analyzer-niche-row),
  main section .grid.grid-cols-3:not(.platform-mobile-keep-cols-3),
  main section .grid.grid-cols-4,
  main section .grid[class*="grid-cols-2"]:not(.platform-mobile-keep-cols-2),
  main section .grid[class*="grid-cols-3"]:not(.platform-mobile-keep-cols-3),
  main section .grid[class*="sm:grid-cols"],
  main section .grid[class*="md:grid-cols"]:not([class*="md:grid-cols-1"]) {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  /* ---- Flex rows: wrap & stack form actions ---- */
  main section .flex.gap-3:not(.flex-col):not(.flex-nowrap):not(.otn-mobile-action-btns),
  main section .flex.gap-4:not(.flex-col):not(.flex-nowrap) {
    flex-wrap: wrap;
  }

  main section .flex.gap-3:has(> input),
  main section .flex.gap-3:has(> select),
  main section .flex.gap-4:has(> input) {
    flex-direction: column;
    align-items: stretch;
  }

  main section .flex.gap-3:has(> input) > button,
  main section .flex.gap-3:has(> input) > .btn-brand,
  main section .flex.gap-4:has(> input) > button {
    width: 100%;
    justify-content: center;
  }

  /* ---- Setup step titles ---- */
  .theme-setup-step-content h2.text-2xl {
    font-size: 1.2rem !important;
    flex-wrap: wrap;
    gap: 0.5rem !important;
  }

  .theme-setup-step-content h2.text-2xl i {
    font-size: 1.35rem !important;
  }

  .theme-setup-step-content > .flex.items-center.justify-between {
    flex-direction: column;
    align-items: flex-start !important;
    gap: 0.35rem;
  }

  /* ---- Order Theme (New) stepper: horizontal scroll ---- */
  .theme-setup-step-indicator {
    flex-wrap: nowrap !important;
    justify-content: flex-start !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
    gap: 0.5rem !important;
    margin-bottom: 1.25rem !important;
    padding-bottom: 0.35rem;
    margin-left: -0.25rem;
    margin-right: -0.25rem;
    padding-left: 0.25rem;
    padding-right: 0.25rem;
  }

  .theme-setup-step-dot {
    min-width: 4.75rem !important;
    flex: 0 0 auto !important;
    padding: 0.75rem 0.45rem !important;
    border-radius: 0.75rem !important;
    scroll-snap-align: center;
  }

  .theme-setup-step-dot i {
    font-size: 1.35rem !important;
    margin-bottom: 0.3rem !important;
  }

  .theme-setup-step-dot span,
  .theme-setup-step-dot .text-sm {
    font-size: 0.62rem !important;
    text-align: center;
    line-height: 1.15;
    max-width: 4.5rem;
  }

  .theme-setup-card {
    padding: 1rem !important;
  }

  #theme-setup-section-new .otn-setup-footer,
  .otn-setup-footer {
    flex-direction: column-reverse !important;
    align-items: stretch !important;
    gap: 0.5rem !important;
    position: sticky;
    bottom: 0;
    background: linear-gradient(to top, rgba(9, 7, 31, 0.98) 70%, rgba(9, 7, 31, 0));
    padding-top: 1rem;
    margin-top: 0.5rem;
    z-index: 5;
  }

  #theme-setup-section-new .otn-setup-footer .btn-primary,
  #theme-setup-section-new .otn-setup-footer .btn-secondary,
  .otn-setup-footer .btn-primary,
  .otn-setup-footer .btn-secondary {
    width: 100%;
    justify-content: center;
    min-height: 2.75rem;
  }

  /* ---- Order Theme (New): table → cards (portrait only; landscape uses table below) ---- */
}

@media (max-width: 1023px) and (orientation: portrait) {
  #new-ordered-themes-content .otn-mobile-table {
    overflow: visible;
  }

  #new-ordered-themes-content .otn-mobile-table table {
    display: block;
    width: 100%;
  }

  #new-ordered-themes-content .otn-mobile-table thead {
    display: none;
  }

  #new-ordered-themes-content .otn-mobile-table tbody {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
  }

  #new-ordered-themes-content .otn-mobile-table tbody tr.otn-theme-row {
    display: block;
    border: 1px solid rgba(255, 255, 255, 0.12) !important;
    border-radius: 0.875rem;
    background: rgba(255, 255, 255, 0.04);
    padding: 0.75rem 1rem;
  }

  #new-ordered-themes-content .otn-mobile-table tbody tr.otn-theme-row:hover {
    background: rgba(255, 255, 255, 0.07);
  }

  #new-ordered-themes-content .otn-mobile-table tbody td {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.45rem 0 !important;
    border: none;
    text-align: right;
  }

  #new-ordered-themes-content .otn-mobile-table tbody td::before {
    content: attr(data-label);
    font-size: 0.65rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: rgba(255, 255, 255, 0.45);
    text-align: left;
    flex-shrink: 0;
    padding-top: 0.15rem;
  }

  #new-ordered-themes-content .otn-mobile-table tbody td.otn-mobile-check {
    justify-content: flex-start;
    padding-bottom: 0.65rem !important;
    margin-bottom: 0.35rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  }

  #new-ordered-themes-content .otn-mobile-table tbody td.otn-mobile-check::before {
    display: none;
  }

  #new-ordered-themes-content .otn-mobile-table tbody td.otn-mobile-title {
    flex-direction: column;
    align-items: stretch !important;
    text-align: left;
    padding-top: 0.25rem !important;
  }

  #new-ordered-themes-content .otn-mobile-table tbody td.otn-mobile-title::before {
    margin-bottom: 0.25rem;
  }

  #new-ordered-themes-content .otn-mobile-table tbody td.otn-mobile-title > div {
    justify-content: flex-start;
  }

  #new-ordered-themes-content .otn-mobile-table tbody td.otn-mobile-payment > div,
  #new-ordered-themes-content .otn-mobile-table tbody td.otn-mobile-payment > button {
    margin-left: auto;
  }

  #new-ordered-themes-content .otn-mobile-table tbody td.otn-mobile-payment .flex {
    flex-direction: column;
    align-items: stretch !important;
    width: 100%;
    gap: 0.5rem;
  }

  #new-ordered-themes-content .otn-mobile-table tbody td.otn-mobile-payment .flex > * {
    width: 100%;
    justify-content: center;
  }

  #new-ordered-themes-content .otn-mobile-table tbody td.otn-mobile-actions {
    flex-direction: column;
    align-items: stretch !important;
    text-align: left;
    padding-top: 0.65rem !important;
    margin-top: 0.35rem;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
  }

  #new-ordered-themes-content .otn-mobile-table tbody td.otn-mobile-actions::before {
    margin-bottom: 0.35rem;
  }

  #new-ordered-themes-content .otn-mobile-action-btns {
    flex-direction: column !important;
    align-items: stretch !important;
    width: 100%;
    gap: 0.5rem !important;
  }

  #new-ordered-themes-content .otn-mobile-action-btns button {
    width: 100%;
    justify-content: center;
    min-height: 2.5rem;
  }
}

@media (max-width: 1023px) {
  /* ---- Modals & overlays ---- */
  main section .fixed.inset-0 > .card,
  main section .fixed.inset-0 > div[class*="max-w"],
  #otn-theme-details-modal > .theme-setup-card {
    max-width: calc(100vw - 2 * var(--platform-mobile-gutter)) !important;
    width: 100% !important;
    max-height: 92vh;
    margin: 0.5rem auto;
  }

  main section .fixed.inset-0.p-6,
  main section .fixed.inset-0.p-4 {
    padding: max(0.75rem, env(safe-area-inset-top, 0px)) var(--platform-mobile-gutter) !important;
  }

  /* ---- Dashboard toolbar ---- */
  .dashboard-toolbar-bar {
    padding: 0.5rem 0.65rem !important;
    gap: 0.35rem !important;
    flex-wrap: wrap;
  }

  .dashboard-toolbar-tab {
    font-size: 0.7rem !important;
    padding: 0.4rem 0.55rem !important;
    min-height: 2.25rem;
  }

  .dashboard-toolbar-bar .ml-auto {
    margin-left: 0 !important;
    width: 100%;
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  /* ---- Integrations / shop cards ---- */
  #section-integrations .grid.grid-cols-3,
  #shopify-shops .grid {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  /* ---- Photo editor toolbars ---- */
  #section-photo-editor-demo .flex.gap-2,
  #section-photo-editor-local .flex.gap-2 {
    flex-wrap: wrap;
  }

  /* ---- Tables (generic fallback; OTN uses card layout above) ---- */
  main section .overflow-x-auto:not(.otn-mobile-table) {
    -webkit-overflow-scrolling: touch;
  }

  main section pre,
  main section .overflow-x-auto {
    max-width: 100%;
  }

  /* ---- Typography ---- */
  main section .text-2xl {
    font-size: 1.25rem !important;
  }

  main section .text-xl {
    font-size: 1.1rem !important;
  }

  main section h2.text-2xl {
    font-size: 1.2rem !important;
  }

  /* ---- Explore / banner CTAs ---- */
  .explore-self-banner {
    padding: 1rem !important;
    flex-wrap: wrap;
    text-align: center;
  }

  /* ---- Sidebar drawer ---- */
  #app-sidebar .sidebar-content {
    padding: 1.25rem 0.875rem;
  }

  #app-sidebar .sidebar-item {
    min-height: 2.75rem;
  }

  /* ---- Touch-friendly controls ---- */
  main section button.text-xs,
  main section .btn-brand,
  main section .btn-secondary {
    min-height: 2.5rem;
  }

  main section input[type="text"],
  main section input[type="url"],
  main section input[type="email"],
  main section select,
  main section textarea {
    font-size: 16px; /* prevents iOS zoom on focus */
  }

  /* Floating scroll-to-top (above safe area, clear of footer) */
  .platform-scroll-top {
    position: fixed;
    right: max(1rem, env(safe-area-inset-right, 0px));
    bottom: max(5.5rem, calc(1.25rem + env(safe-area-inset-bottom, 0px)));
    z-index: 45;
    width: 3rem;
    height: 3rem;
    border-radius: 9999px;
    border: 1px solid rgba(255, 255, 255, 0.18);
    background: rgba(15, 15, 25, 0.88);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    color: #fff;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
    transition: opacity 0.2s ease, transform 0.2s ease;
  }

  .platform-scroll-top:not([hidden]) {
    opacity: 1;
    transform: translateY(0);
  }

  .platform-scroll-top[hidden] {
    display: none !important;
  }

  .platform-scroll-top:active {
    transform: scale(0.96);
  }
}

@media (min-width: 1024px) {
  .platform-scroll-top {
    display: none !important;
  }
}

/* Order Theme — keep toolbars on one row (title left, actions right) */
@media (max-width: 1023px) {
  #new-ordered-themes-section .otn-ordered-themes-toolbar,
  #theme-setup-section-new .otn-setup-header-bar {
    flex-direction: row !important;
    align-items: center !important;
    flex-wrap: nowrap !important;
  }

  #new-ordered-themes-section .otn-ordered-themes-toolbar-actions {
    margin-left: auto !important;
    width: auto !important;
    justify-content: flex-end !important;
    flex-wrap: nowrap !important;
  }
}

/* Order Theme — phone landscape: aligned table + setup wizard */
@media (max-width: 1023px) and (orientation: landscape) {
  #otn-order-theme-root .card > .p-6 {
    padding: 0.5rem 0.65rem !important;
  }

  #new-ordered-themes-section.card,
  #theme-setup-section-new.card {
    padding: 0.65rem 0.75rem !important;
  }

  #new-ordered-themes-content .otn-mobile-table {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  #new-ordered-themes-content .otn-mobile-table table {
    display: table !important;
    width: 100%;
    min-width: 100%;
    table-layout: fixed;
    border-collapse: collapse;
  }

  #new-ordered-themes-content .otn-mobile-table thead th:nth-child(1) {
    width: 2.25rem;
  }
  #new-ordered-themes-content .otn-mobile-table thead th:nth-child(2) {
    width: 16%;
  }
  #new-ordered-themes-content .otn-mobile-table thead th:nth-child(3) {
    width: 20%;
  }
  #new-ordered-themes-content .otn-mobile-table thead th:nth-child(4) {
    width: 18%;
  }
  #new-ordered-themes-content .otn-mobile-table thead th:nth-child(5) {
    width: 18%;
  }
  #new-ordered-themes-content .otn-mobile-table thead th:nth-child(6) {
    width: 28%;
  }

  #new-ordered-themes-content .otn-mobile-table thead {
    display: table-header-group !important;
  }

  #new-ordered-themes-content .otn-mobile-table tbody {
    display: table-row-group !important;
  }

  #new-ordered-themes-content .otn-mobile-table tbody tr.otn-theme-row {
    display: table-row !important;
    border: none !important;
    border-radius: 0 !important;
    background: transparent !important;
    padding: 0 !important;
  }

  #new-ordered-themes-content .otn-mobile-table tbody td {
    display: table-cell !important;
    vertical-align: middle !important;
    padding: 0.5rem 0.4rem !important;
    text-align: left !important;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  }

  #new-ordered-themes-content .otn-mobile-table tbody td::before {
    display: none !important;
    content: none !important;
  }

  #new-ordered-themes-content .otn-mobile-table thead th {
    padding: 0.5rem 0.35rem !important;
    font-size: 0.58rem !important;
    white-space: nowrap;
    vertical-align: bottom;
    text-align: left;
  }

  #new-ordered-themes-content .otn-mobile-table thead th:last-child {
    text-align: right;
  }

  #new-ordered-themes-content .otn-mobile-table tbody td.otn-mobile-actions {
    text-align: right !important;
  }

  #new-ordered-themes-content .otn-fulfill-payment,
  #new-ordered-themes-content .otn-mobile-table tbody td.otn-mobile-payment button {
    padding: 0.35rem 0.55rem !important;
    min-height: 0 !important;
    line-height: 1.2 !important;
    font-size: 0.62rem !important;
    white-space: nowrap;
  }

  #new-ordered-themes-content .otn-mobile-action-btns {
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    justify-content: flex-end !important;
    gap: 0.3rem !important;
  }

  #new-ordered-themes-content .otn-mobile-action-btns button {
    width: auto !important;
    min-height: 1.85rem !important;
    padding: 0.3rem 0.45rem !important;
    font-size: 0.62rem !important;
  }

  #new-ordered-themes-content .otn-mobile-table tbody td.otn-mobile-payment .flex {
    flex-direction: row !important;
    align-items: center !important;
    justify-content: flex-start !important;
    width: 100% !important;
    gap: 0.3rem !important;
  }

  #new-ordered-themes-content .otn-mobile-title .flex {
    align-items: center !important;
  }

  #new-ordered-themes-content .otn-landscape-cards {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
  }

  #theme-setup-section-new.otn-landscape-ready #theme-setup-content-new.otn-setup-visible {
    display: grid !important;
    grid-template-rows: auto minmax(9rem, 1fr) auto;
    gap: 0.5rem;
    max-height: none !important;
  }

  #theme-setup-section-new.otn-landscape-ready .otn-setup-steps-panel {
    min-height: 9rem;
    overflow-y: auto;
  }

  #theme-setup-section-new.otn-landscape-ready .theme-setup-step-content.active {
    display: block !important;
    flex: none !important;
    min-height: unset !important;
  }

  #theme-setup-section-new.otn-landscape-ready .otn-setup-footer {
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    align-items: center !important;
    position: relative !important;
    background: transparent !important;
    padding-top: 0.65rem !important;
    margin-top: 0.5rem !important;
  }

  #theme-setup-section-new.otn-landscape-ready .otn-setup-footer > .ml-auto {
    margin-left: auto !important;
    width: auto !important;
    flex: 0 0 auto;
  }

  #theme-setup-section-new.otn-landscape-ready .otn-setup-footer .btn-primary,
  #theme-setup-section-new.otn-landscape-ready .otn-setup-footer .btn-secondary {
    width: auto !important;
    min-width: 6.5rem;
    min-height: 2.35rem !important;
  }

  #theme-setup-section-new.otn-landscape-ready #theme-setup-new-step-indicator {
    display: block !important;
    width: 100%;
  }

  #theme-setup-section-new.otn-landscape-ready .theme-setup-step-indicator {
    justify-content: center !important;
    align-items: stretch !important;
    margin: 0 auto 0.85rem !important;
    gap: 0.65rem !important;
    overflow: visible !important;
    flex-wrap: wrap !important;
    max-width: 100%;
    padding: 0 0.25rem;
  }

  #theme-setup-section-new.otn-landscape-ready .theme-setup-step-dot {
    min-width: 4.75rem !important;
    min-height: 4.25rem !important;
    padding: 0.85rem 0.55rem !important;
    flex: 0 1 auto !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
  }

  #theme-setup-section-new.otn-landscape-ready .theme-setup-step-dot i {
    font-size: 1.5rem !important;
    margin-bottom: 0.35rem !important;
  }

  #theme-setup-section-new.otn-landscape-ready .theme-setup-step-dot .text-sm {
    font-size: 0.62rem !important;
    text-align: center;
  }

  #theme-setup-section-new.otn-landscape-ready .theme-setup-card {
    padding: 0.75rem 0.85rem !important;
  }
}

/* Extra-small phones */
@media (max-width: 380px) {
  :root {
    --platform-mobile-gutter: 0.875rem;
  }

  .theme-setup-step-dot {
    min-width: 4.25rem !important;
  }
}
