/* Mirror Image mobile controls
   Desktop layout remains controlled by styles.css. */

.mobile-image-actions,
.mobile-clear-button,
.mobile-upload-copy {
  display: none;
}

#imageInput {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

@media (max-width: 760px) {
  html, body {
    max-width: 100%;
    overflow-x: hidden;
  }

  body {
    font-size: 16px;
  }

  .site-header {
    position: static;
    min-height: 0;
    padding: 10px 12px;
    align-items: stretch;
    flex-direction: column;
    gap: 10px;
  }

  .brand {
    gap: 10px;
  }

  .brand-mark {
    width: 40px;
    height: 40px;
  }

  .brand h1 {
    font-size: 1.15rem;
  }

  .brand-subtitle {
    font-size: .72rem;
  }

  .header-actions {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .header-actions .button,
  .header-actions a {
    min-height: 46px;
  }

  .app-shell {
    display: flex;
    flex-direction: column;
    padding: 8px;
    gap: 10px;
  }

  .panel,
  .workspace {
    margin: 0;
    box-shadow: none;
  }

  .controls-panel {
    order: 1;
  }

  .workspace {
    order: 2;
  }

  .panel-section {
    padding: 15px 13px;
  }

  .section-heading {
    margin-bottom: 12px;
  }

  .desktop-upload-copy,
  .desktop-image-actions {
    display: none !important;
  }

  .mobile-upload-copy {
    display: block;
  }

  .drop-zone {
    min-height: 136px;
    padding: 18px 14px;
    cursor: pointer;
    touch-action: manipulation;
  }

  .drop-zone strong {
    font-size: 1rem;
  }

  .mobile-image-actions {
    display: grid;
    gap: 10px;
    margin-top: 12px;
  }

  #mobileChooseButton {
    width: 100%;
    min-height: 50px;
    font-size: .82rem;
  }

  .mobile-paste-target {
    display: flex;
    min-height: 92px;
    padding: 14px;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    text-align: center;
    border: 1px dashed rgba(199,163,71,.55);
    background: rgba(199,163,71,.055);
    color: #f4efe5;
    -webkit-user-select: text;
    user-select: text;
    touch-action: manipulation;
    outline: none;
  }

  .mobile-paste-target:focus {
    border-color: #e2c66f;
    box-shadow: 0 0 0 2px rgba(226,198,111,.12);
  }

  .mobile-paste-target strong {
    color: #e2c66f;
  }

  .mobile-paste-target span {
    color: #aaa1af;
    font-size: .82rem;
    line-height: 1.4;
  }

  .mobile-clear-button {
    display: inline-flex;
    width: 100%;
    min-height: 46px;
    margin-top: 10px;
  }

  .canvas-stage {
    min-height: 360px;
    height: min(92vw,430px);
    max-height: 430px;
  }

  #tokenCanvas {
    width: min(92%,410px);
    max-height: 92%;
  }

  .workspace-toolbar,
  .status-bar {
    padding: 10px;
    align-items: flex-start;
    flex-wrap: wrap;
  }

  .workspace-toolbar .view-actions {
    flex-wrap: wrap;
  }

  .button {
    min-height: 44px;
  }

  .button-row {
    flex-direction: column;
  }

  .button-row > * {
    width: 100%;
  }

  .button-grid {
    grid-template-columns: 1fr;
  }

  .shape-picker {
    grid-template-columns: 1fr 1fr;
  }

  .color-grid,
  .two-column {
    grid-template-columns: 1fr;
  }

  .color-field {
    grid-template-columns: 1fr 46px;
    min-height: 48px;
  }

  input[type="text"],
  select {
    min-height: 46px;
  }

  input[type="range"] {
    min-height: 30px;
  }

  dialog {
    width: calc(100% - 20px);
    max-height: calc(100dvh - 24px);
    overflow: auto;
    padding: 0;
  }

  .dialog-header {
    padding: 14px;
    gap: 10px;
  }

  .dialog-content {
    padding: 14px;
  }

  .preset-row {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 430px) {
  .header-actions {
    grid-template-columns: 1fr;
  }

  .shape-picker {
    grid-template-columns: 1fr;
  }

  .canvas-stage {
    min-height: 320px;
  }

  .empty-ring {
    width: 105px;
    height: 105px;
  }
}


/* Correct mobile sequence: Steps 1, 2, 3, 4, 5, 6, then Live Preview.
   The desktop layout uses two separate side panels, so on mobile we flatten
   those wrappers and explicitly order their sections. */
@media (max-width: 760px) {
  .app-shell {
    display: flex !important;
    flex-direction: column !important;
    gap: 10px !important;
  }

  .controls-panel,
  .design-panel {
    display: contents !important;
  }

  .controls-panel > .panel-section,
  .design-panel > .panel-section {
    display: block !important;
    margin: 0 !important;
    border: 1px solid var(--line) !important;
    background: var(--panel) !important;
  }

  .controls-panel > .panel-section:nth-child(1) { order: 1 !important; }
  .controls-panel > .panel-section:nth-child(2) { order: 2 !important; }
  .controls-panel > .panel-section:nth-child(3) { order: 3 !important; }

  .design-panel > .panel-section:nth-child(1) { order: 4 !important; }
  .design-panel > .panel-section:nth-child(2) { order: 5 !important; }
  .design-panel > .panel-section:nth-child(3) { order: 6 !important; }

  /* Saved Presets follows the numbered setup steps. */
  .design-panel > .panel-section:nth-child(4) { order: 7 !important; }

  /* Live Preview is deliberately last on mobile. */
  .workspace {
    order: 8 !important;
    width: 100% !important;
  }

  .workspace-hint::after {
    content: " Touch and drag to reposition.";
  }
}


/* Mobile sticky Live Preview
   Keep the preview visible at the top while the setup steps scroll below it. */
@media (max-width: 760px) {
  .workspace {
    order: 0 !important;
    position: sticky !important;
    top: 0 !important;
    z-index: 50 !important;
    width: 100% !important;
    margin: 0 0 10px !important;
    background: rgba(10,9,14,.985) !important;
    border: 1px solid rgba(199,163,71,.45) !important;
    box-shadow: 0 10px 24px rgba(0,0,0,.42) !important;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
  }

  .workspace-toolbar {
    min-height: 42px !important;
    padding: 7px 10px !important;
  }

  .workspace-toolbar h2,
  .workspace-toolbar h3,
  .workspace-title {
    margin: 0 !important;
    font-size: .82rem !important;
  }

  .workspace-toolbar .view-actions {
    gap: 6px !important;
  }

  .workspace-toolbar .button,
  .workspace-toolbar button {
    min-height: 36px !important;
    padding: 6px 9px !important;
    font-size: .68rem !important;
  }

  .canvas-stage {
    min-height: 0 !important;
    height: min(42vh, 320px) !important;
    max-height: 320px !important;
    padding: 8px !important;
    overflow: hidden !important;
    background: #0a0910 !important;
  }

  #tokenCanvas {
    width: auto !important;
    height: 100% !important;
    max-width: 100% !important;
    max-height: 100% !important;
    object-fit: contain !important;
  }

  .status-bar {
    min-height: 34px !important;
    padding: 6px 10px !important;
    font-size: .7rem !important;
  }

  /* Numbered steps follow directly beneath the sticky preview. */
  .controls-panel > .panel-section:nth-child(1) { order: 1 !important; }
  .controls-panel > .panel-section:nth-child(2) { order: 2 !important; }
  .controls-panel > .panel-section:nth-child(3) { order: 3 !important; }
  .design-panel > .panel-section:nth-child(1) { order: 4 !important; }
  .design-panel > .panel-section:nth-child(2) { order: 5 !important; }
  .design-panel > .panel-section:nth-child(3) { order: 6 !important; }
  .design-panel > .panel-section:nth-child(4) { order: 7 !important; }
}

@media (max-width: 430px) {
  .canvas-stage {
    height: min(38vh, 280px) !important;
    max-height: 280px !important;
  }

  .workspace-toolbar .view-actions {
    display: none !important;
  }
}


/* iPhone Safari: persistent preview
   position: sticky can fail when an ancestor uses overflow/layout rules.
   On mobile we use a fixed preview and reserve its exact height in the page. */
@media (max-width: 760px) {
  :root {
    --mobile-preview-height: 360px;
  }

  .app-shell {
    padding-top: calc(var(--mobile-preview-height) + 10px) !important;
  }

  .workspace {
    position: fixed !important;
    top: 0 !important;
    left: 8px !important;
    right: 8px !important;
    width: auto !important;
    height: var(--mobile-preview-height) !important;
    max-height: var(--mobile-preview-height) !important;
    margin: 0 !important;
    z-index: 9999 !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
    background: rgba(10,9,14,.99) !important;
    border: 1px solid rgba(199,163,71,.55) !important;
    box-shadow: 0 10px 28px rgba(0,0,0,.6) !important;
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
  }

  .workspace .canvas-stage {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    height: auto !important;
    max-height: none !important;
  }

  .workspace .workspace-toolbar,
  .workspace .status-bar {
    flex: 0 0 auto !important;
  }
}

@media (max-width: 430px) {
  :root {
    --mobile-preview-height: 320px;
  }
}


/* Mobile preview canvas fit fix */
@media (max-width: 760px) {
  .workspace .canvas-stage {
    position: relative !important;
    display: block !important;
    padding: 8px !important;
  }

  .workspace #tokenCanvas {
    position: absolute !important;
    top: 50% !important;
    left: 50% !important;
    width: var(--mobile-canvas-size, 220px) !important;
    height: var(--mobile-canvas-size, 220px) !important;
    max-width: none !important;
    max-height: none !important;
    margin: 0 !important;
    transform: translate(-50%, -50%) !important;
    object-fit: contain !important;
  }

  .workspace .empty-state {
    inset: 0 !important;
    place-content: center !important;
  }
}


/* iPhone Photos export */
@media (max-width: 760px) {
  .ios-save-button {
    display: inline-flex;
    margin-top: 10px;
  }

  .ios-save-help {
    display: block;
    margin: 8px 0 0;
    color: var(--muted);
    font-size: .76rem;
    line-height: 1.45;
  }

  .photo-save-dialog {
    width: calc(100% - 18px);
    max-width: 520px;
    max-height: calc(100dvh - 20px);
    padding: 0;
  }

  .photo-save-content {
    padding: 14px;
  }

  .photo-save-content img {
    max-height: 58dvh;
    object-fit: contain;
    -webkit-touch-callout: default;
    -webkit-user-select: auto;
    user-select: auto;
  }
}
