/* Compact studio: the canvas, working tools and step action share the viewport. */
@media (max-width: 899px), (max-width: 1199px) and (max-height: 560px) {
  :root {
    --muted: #5f6d62;
    --mobile-footer: calc(58px + max(12px, env(safe-area-inset-bottom, 0px)));
    --compact-canvas: clamp(180px, calc(min(100svh, var(--visible-height, 100svh)) - 438px), 440px);
  }
  .site-header {
    height: auto;
    min-height: 64px;
    padding: max(6px, env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px)) 0 max(16px, env(safe-area-inset-left, 0px));
    gap: 0 12px;
    flex-wrap: wrap;
  }
  .brand { min-height: 52px; gap: 9px; min-width: 0; }
  .brand-seal { font-size: 15px; padding: 4px 5px; }
  .brand b { font-size: 20px; letter-spacing: .08em; }
  .brand small { display: none; }
  .brand .saved-state { display: block; font-size: 12px; line-height: 1.4; color: var(--muted); }
  .site-header .gallery-open { margin-left: auto; min-height: 48px; font-size: 14px; padding: 0; }
  .site-header .gallery-open svg { width: 18px; height: 18px; }
  #progress { order: 3; flex-basis: 100%; gap: 0; justify-content: space-between; }
  #progress button { flex: 1; justify-content: center; min-height: 44px; font-size: 14px; gap: 5px; color: var(--muted); }
  #progress button span { display: none; }
  #progress button:disabled { opacity: .45; }
  #progress button[aria-current="step"] { color: var(--accent); }

  .cover { min-height: calc(100svh - 64px); display: flex; flex-direction: column; padding: 24px 24px 0; gap: 12px; }
  .hero-copy { width: 100%; padding: 0; }
  .hero-copy .eyebrow { font-size: 14px; margin-bottom: 12px; }
  .hero-copy h1 { font-size: clamp(30px, 8.5vw, 42px); line-height: 1.4; letter-spacing: .06em; }
  .hero-intro { font-size: 15px; line-height: 1.85; margin: 14px 0 20px; color: var(--muted); }
  .hero-actions { flex-wrap: wrap; gap: 8px 16px; }
  .hero-actions .primary { height: 52px; min-width: 180px; font-size: 16px; }
  .hero-actions .text-button { font-size: 14px; }
  .craft-note { margin-top: 16px; font-size: 14px; gap: 10px; }
  .craft-note small { display: none; }
  .hero-art { align-self: center; width: 100%; height: clamp(270px, 65vw, 430px); min-height: 0; margin: -8px 0 0; }
  .hero-art canvas { transform: rotate(-12deg); }
  .hero-caption { left: 0; right: auto; bottom: 0; gap: 8px; align-items: baseline; writing-mode: horizontal-tb; }
  .hero-caption > span { font-size: 20px; }
  .hero-caption small { font-size: 14px; letter-spacing: 0; color: var(--muted); }
  .vertical-note { display: none; }
  .cover-bottom { width: 100%; padding: 16px 0; margin: 0; font-size: 12px; gap: 10px; letter-spacing: 0; }
  .cover-bottom span:last-child { display: none; }

  .workspace {
    display: flex;
    flex-direction: column;
    gap: 8px;
    height: auto;
    min-height: 0;
    padding: 10px max(12px, env(safe-area-inset-right, 0px)) calc(var(--mobile-footer) + 16px) max(12px, env(safe-area-inset-left, 0px));
  }
  .studio { flex: none; }
  .stage-heading { align-items: center; min-height: 38px; padding: 0 4px 8px; gap: 8px; }
  .stage-heading h1 { font-size: clamp(20px, 5.5vw, 24px); line-height: 1.4; margin: 0; letter-spacing: .02em; }
  #stage-title:focus { outline: none; }
  .stage-heading .eyebrow, .stage-heading > span { display: none; }
  .art-stage {
    flex: none;
    min-height: 0;
    max-height: none;
    height: clamp(220px, calc(min(100svh, var(--visible-height, 100svh)) - 370px), 480px);
    border: 0;
    border-radius: 4px;
  }
  .art-stage[data-view="work"] { height: var(--compact-canvas); }
  .art-stage[data-view="pick"] { height: clamp(150px, 39vw, 260px); }
  .art-stage[data-view="dip"] { height: clamp(180px, calc(min(100svh, var(--visible-height, 100svh)) - 490px), 350px); }
  .art-stage[data-view="res"] { height: clamp(240px, 70vw, 430px); }
  .stage-footer, .stage-badge { display: none; }
  .live-preview { width: 68px; right: 8px; bottom: 8px; padding: 4px; background: #F9F7EFEE; }
  .live-preview canvas { width: 52px; height: 64px; }
  .live-preview span { font-size: 12px; letter-spacing: 0; }
  #expand { right: 8px; top: 8px; box-shadow: none; }

  .control-panel {
    align-self: stretch;
    position: static;
    min-width: 0;
    min-height: 0;
    max-height: none;
    padding: 0 4px;
    margin: 0;
    border-top: 1px solid var(--line);
    overflow: visible;
  }
  .panel-handle { display: flex; align-items: center; gap: 8px; min-height: 44px; }
  .panel-toggle { flex: 1; min-width: 0; display: flex; align-items: center; justify-content: space-between; min-height: 44px; gap: 6px; padding: 0; font-size: 14px; text-align: left; }
  #panel-summary { color: var(--ink); }
  #panel-toggle-state { white-space: nowrap; color: var(--muted); }
  .mobile-history { display: block; }
  .mobile-history:empty { display: none; }
  .mobile-history .history-row { gap: 0; }
  .mobile-history .history-row button { flex: none; width: 44px; height: 44px; min-height: 44px; padding: 0; font-size: 0; border: 0; box-shadow: none; background: none; }
  .mobile-history .history-row svg { width: 20px; height: 20px; }
  .panel-body { gap: 12px; min-width: 0; padding-bottom: 8px; }
  .panel-title { display: none; }
  .panel-description, .tool-hint, .small-details, .small-details p,
  .section-label > span, .fan-options small, .recipe-card small, .field,
  .size-row, .sizes button, .range-field, .phrase-options button, .dip-status,
  .export-options small, .panel-body > .text-button { font-size: 14px; }
  .panel-description, .tool-hint { color: var(--muted); line-height: 1.65; }
  .section-label h3 { font-size: 14px; }
  .section-label > span { color: var(--muted); }
  .field, .range-field, .fan-options small, .recipe-card small,
  .phrase-options button, .craft-note, .gallery-empty p { color: var(--muted); }
  #panel-work > .section-label { display: none; }
  .panel-collapsed[data-panel="work"] #work-options,
  .panel-collapsed:not([data-panel="work"]) > .panel-body { display: none; }
  .work-options { gap: 12px; }
  .tool-hint { margin: 0; }
  .palette { display: flex; overflow-x: auto; overscroll-behavior-x: contain; scrollbar-width: thin; padding: 4px 0; gap: 2px; margin: 0; }
  .swatch { flex: 1 0 44px; width: auto; min-width: 44px; min-height: 54px; padding: 0; gap: 2px; line-height: 1.3; font-size: 14px; color: var(--muted); }
  .swatch i { width: 30px; height: 30px; border-width: 3px; }
  .swatch[aria-pressed="true"] i { outline-offset: 2px; }
  .tools { gap: 4px; padding: 4px; }
  .tools button { min-height: 44px; font-size: 15px; color: var(--muted); }
  .sizes button { min-width: 48px; min-height: 44px; }
  .history-row > .secondary, .history-row .quiet { font-size: 14px; min-height: 44px; }
  .small-details { padding: 8px 0; }
  .small-details summary { min-height: 44px; display: flex; align-items: center; justify-content: space-between; }
  .recipe-switch { gap: 8px; }
  .fan-options button { padding: 12px; min-height: 78px; }
  .fan-options b { font-size: 22px; margin-bottom: 2px; }
  .recipe-grid { margin-top: 0; gap: 10px; }
  .recipe-card img { height: 64px; }
  .recipe-card > div { padding: 8px 10px; }
  .recipe-card b { font-size: 19px; }
  .blank-option { min-height: 48px; font-size: 15px; }
  .blank-option span { font-size: 14px; }
  .methods { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
  .method { display: flex; justify-content: center; align-items: center; padding: 10px 2px; min-height: 48px; text-align: center; }
  .method b { font: 15px var(--sans); }
  .method svg, .method small { display: none; }
  .method:last-child { grid-column: auto; }
  #panel-dip > .section-label, .control-panel[data-panel="dip"] > .panel-handle { display: none; }
  .dip-status { margin: 0; padding: 8px 0; min-height: 38px; color: var(--muted); }
  .split-actions { display: flex; }
  .split-actions #reset-dip { min-height: 44px; flex: 1; padding: 8px 12px; font-size: 14px; }
  .field { gap: 6px; }
  .field input { min-height: 48px; font-size: 16px; }
  .phrase-options { gap: 6px; }
  .phrase-options button { min-height: 44px; }
  .range-field input { min-height: 44px; }
  .export-options .secondary { font-size: 15px; min-height: 56px; }
  .panel-divider { margin: 2px 0; }
  .mobile-actions {
    position: fixed;
    z-index: 10;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px max(16px, env(safe-area-inset-right, 0px)) max(12px, env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px));
    background: var(--paper);
    border-top: 1px solid var(--line);
  }
  .mobile-actions > .primary { flex: 1; min-width: 0; min-height: 48px; padding: 12px; font-size: 15px; border-radius: 4px; }
  .mobile-actions > #back { flex: none; min-height: 48px; padding: 0 4px; font-size: 14px; }
  .keyboard-open .mobile-actions { position: static; margin: 0 -4px; padding-left: 0; padding-right: 0; }
  #toast { width: max-content; max-width: calc(100vw - 32px); bottom: calc(var(--mobile-footer) + 12px); font-size: 14px; }
  .busy { padding: max(16px, env(safe-area-inset-top, 0px)) 16px; }
  .busy > div { max-width: 100%; }
  dialog { width: calc(100% - max(12px, env(safe-area-inset-left, 0px)) - max(12px, env(safe-area-inset-right, 0px))); max-height: calc(100dvh - 32px); padding: 16px; border-radius: 8px; }
  .dialog-top { gap: 12px; margin-bottom: 16px; }
  .dialog-top h2 { font-size: 24px; }
  .dialog-top .eyebrow, .dialog-foot { font-size: 14px; }
  .gallery-list { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
  .gallery-card { min-width: 0; }
  .gallery-card img { height: 180px; }
  .gallery-card .card-open div { padding: 10px; }
  .gallery-card h3 { font-size: 16px; overflow-wrap: anywhere; }
  .gallery-card small, .gallery-card .card-actions button { font-size: 14px; }
  .gallery-card .card-actions { flex-wrap: wrap; padding: 0 8px 8px; }
  .gallery-card .card-actions button { min-height: 44px; }
  .zoom-viewport { height: min(62dvh, 580px); }
  html { scroll-padding-block: 16px calc(var(--mobile-footer) + 16px); }
}

@media (min-width: 900px) and (max-width: 1099px) and (min-height: 561px) {
  .workspace { height: calc(100svh - 85px); min-height: 0; }
  .studio { min-height: 0; }
  .art-stage { min-height: 300px; }
  .control-panel { max-height: calc(100svh - 135px); overflow-y: auto; scrollbar-width: thin; padding-right: 5px; }
}

@media (max-width: 560px) and (max-height: 700px) {
  .site-header { padding-top: 4px; }
  .brand, .site-header .gallery-open { min-height: 44px; }
  .workspace { padding-top: 8px; }
  .stage-heading { min-height: 34px; padding-bottom: 6px; }
  .panel-body { gap: 8px; }
  #panel-dip > .panel-description { display: none; }
}

@media (max-width: 374px) and (min-height: 561px) {
  .site-header { padding-left: 12px; padding-right: 12px; }
  .brand { gap: 6px; }
  .brand b { font-size: 18px; }
  .brand .saved-state { font-size: 12px; }
  .site-header .gallery-open { gap: 5px; }
  .workspace { padding-left: 10px; padding-right: 10px; gap: 4px; }
  .live-preview { display: none; }
  .panel-handle { gap: 4px; }
  .mobile-actions { gap: 8px; padding-left: 12px; padding-right: 12px; }
  .blank-option span { display: block; }
}

/* A short/wide viewport uses the same controls beside the canvas. */
@media (min-width: 561px) and (max-width: 1199px) and (max-height: 560px) {
  :root { --dock-width: clamp(224px, 34vw, 292px); }
  .site-header { min-height: 56px; height: 56px; flex-wrap: nowrap; padding-top: 0; gap: 12px; }
  .brand { flex: none; }
  .brand-seal, .brand .saved-state { display: none; }
  .brand b { font-size: 18px; }
  #progress { flex-basis: auto; order: 0; flex: 1; max-width: 360px; }
  .site-header .gallery-open { flex: none; margin: 0; }
  .workspace { display: grid; grid-template-columns: minmax(0, 1fr) var(--dock-width); align-items: start; gap: 16px; padding: 10px max(12px, env(safe-area-inset-right, 0px)) 10px max(12px, env(safe-area-inset-left, 0px)); }
  .studio { position: sticky; top: 8px; }
  .stage-heading { min-height: 34px; padding-bottom: 6px; }
  .stage-heading h1 { font-size: 20px; }
  .art-stage, .art-stage[data-view] { height: calc(min(100svh, var(--visible-height, 100svh)) - 116px); min-height: 144px; }
  .control-panel { max-height: calc(min(100svh, var(--visible-height, 100svh)) - 144px); overflow: auto; overscroll-behavior: contain; padding: 0; border: 0; scrollbar-width: thin; }
  .panel-handle { position: sticky; top: 0; z-index: 2; background: var(--paper); gap: 2px; }
  .mobile-history .history-row button { width: 44px; }
  .panel-toggle { flex-direction: column; align-items: start; justify-content: center; gap: 0; }
  .panel-toggle span { font-size: 14px; }
  .swatch { flex-basis: 44px; }
  .methods { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .mobile-actions { left: auto; width: calc(var(--dock-width) + 12px + max(12px, env(safe-area-inset-right, 0px))); gap: 6px; padding-left: 12px; padding-right: max(12px, env(safe-area-inset-right, 0px)); }
  .mobile-actions > #back { padding: 0; font-size: 14px; }
  .mobile-actions > .primary { padding-left: 6px; padding-right: 6px; gap: 6px; }
  .live-preview { display: none; }
  .cover { display: grid; grid-template-columns: 1fr 1fr; padding: 20px 5% 0; gap: 10px 24px; min-height: calc(100svh - 56px); }
  .hero-copy h1 { font-size: 30px; }
  .hero-copy .eyebrow { margin-bottom: 6px; }
  .hero-intro { margin: 8px 0 12px; }
  .hero-art { grid-column: 2; grid-row: 1; height: calc(100svh - 110px); max-height: 340px; min-height: 210px; }
  .cover-bottom { grid-column: 1 / -1; }
  .craft-note, .vertical-note { display: none; }
  .zoom-viewport { height: 46dvh; }
}

@media (prefers-reduced-motion: reduce) {
  .mobile-actions, .art-stage { transition: none; }
}
