/* Phone layouts share the desktop views; only navigation and density change. */
.mobile-workbench-links {
  display: none;
}

@media screen and (max-width: 600px) {
  :root {
    --mobile-nav-height: 72px;
  }

  /* Keep all five destinations visible, including their names. */
  .sidebar {
    inset: auto 0 0;
    width: 100%;
    height: calc(var(--mobile-nav-height) + env(safe-area-inset-bottom));
    padding: 7px max(8px, env(safe-area-inset-right))
      calc(7px + env(safe-area-inset-bottom))
      max(8px, env(safe-area-inset-left));
    background: #fbfcf8;
    border-right: 0;
    border-top: 1px solid #dce2d4;
    box-shadow: 0 -5px 22px #2c393208;
    z-index: 30;
  }
  .sidebar .brand,
  .sidebar .workspace-label,
  .sidebar .side-note,
  .sidebar .sidebar-bottom {
    display: none;
  }
  .sidebar nav {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 3px;
    width: 100%;
    height: 100%;
    margin: 0;
  }
  .nav-item {
    min-width: 0;
    min-height: 54px;
    padding: 5px 0;
    gap: 5px;
    flex-direction: column;
    justify-content: center;
    font-size: 10px;
    line-height: 1.3;
    white-space: nowrap;
    border-radius: 10px;
  }
  .nav-item .icon {
    width: 22px;
    height: 22px;
  }
  .nav-item.active {
    background: #e5eddf;
    color: var(--green);
    font-weight: 600;
  }
  .workspace {
    margin-left: 0;
    min-width: 0;
    padding-bottom: calc(
      var(--mobile-nav-height) + env(safe-area-inset-bottom)
    );
  }
  main {
    padding: 24px max(16px, env(safe-area-inset-right)) 32px
      max(16px, env(safe-area-inset-left));
    min-height: calc(100dvh - 160px);
  }
  .topbar {
    gap: 12px;
    height: auto;
    min-height: 64px;
    padding: 8px max(16px, env(safe-area-inset-right)) 8px
      max(16px, env(safe-area-inset-left));
  }
  .breadcrumb {
    gap: 7px;
    min-width: 0;
    flex-wrap: wrap;
    line-height: 1.6;
  }
  .breadcrumb strong {
    overflow-wrap: anywhere;
  }
  .top-actions {
    gap: 6px;
    flex-shrink: 0;
  }
  .top-actions .button {
    min-width: 44px;
    padding: 10px;
  }
  footer {
    flex-wrap: wrap;
    gap: 6px;
    padding: 0 16px 20px;
    font-size: 10px;
  }
  #toast {
    bottom: calc(var(--mobile-nav-height) + env(safe-area-inset-bottom) + 12px);
    left: 50%;
    width: max-content;
    max-width: calc(100vw - 32px);
    padding: 12px 16px;
  }

  /* Comfortable tap targets and fields that do not zoom the iOS viewport. */
  .button,
  .icon-button,
  .text-link,
  .quick-color,
  .paint-palette button,
  .view-switch button,
  .editor-tool-tabs button,
  .preview-controls button,
  .stitch-chips button,
  .picker-library button,
  .foundation-details summary,
  .advanced-fields summary,
  .fitting-scope summary {
    min-height: 44px;
  }
  .icon-button,
  .project-copy .card-row .icon-button,
  .picker-library button {
    width: 44px;
    height: 44px;
    flex-shrink: 0;
  }
  .field input,
  .field select,
  .field textarea,
  .search input,
  .scheme-actions select {
    font-size: 16px;
    min-height: 44px;
    max-width: 100%;
  }
  .field,
  .field-pair > *,
  .parameter-fields > *,
  .page-heading > div,
  .section-heading > div {
    min-width: 0;
  }
  .check-label {
    min-height: 44px;
    align-items: center;
  }
  .check-label input {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
  }
  .foundation-details summary {
    align-content: center;
  }

  /* Search and category strips own their overflow instead of widening pages. */
  .page-heading {
    gap: 18px;
  }
  .page-heading p {
    max-width: none;
    font-size: 14px;
  }
  .heading-actions:empty {
    display: none;
  }
  .heading-actions .button {
    font-size: 13px;
    padding: 11px 8px;
  }
  .collection-toolbar {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }
  .collection-toolbar .search {
    order: -1;
  }
  .tabs {
    min-width: 0;
    width: 100%;
    overflow-x: auto;
    gap: 18px;
    padding: 0 1px 1px;
    scrollbar-width: thin;
  }
  .tabs button {
    min-height: 44px;
    flex-shrink: 0;
    font-size: 13px;
  }
  .tabs button span {
    margin-left: 4px;
    font-size: 11px;
  }
  .search {
    min-width: 0;
    margin: 0 0 6px;
    padding: 0 10px;
  }
  .search input {
    min-width: 0;
    width: 100%;
  }
  .section-heading {
    flex-wrap: wrap;
    gap: 10px;
    align-items: center;
  }
  .section-heading .search {
    flex-basis: 100%;
  }
  .section-heading .muted {
    font-size: 12px;
  }
  .project-grid {
    grid-template-columns: minmax(0, 1fr);
    gap: 16px;
  }
  .project-art {
    height: 195px;
  }
  .project-copy {
    padding: 14px 16px 18px;
  }
  .project-name {
    min-height: 44px;
    font-size: 18px;
    overflow-wrap: anywhere;
  }
  .template-copy p {
    font-size: 12px;
    line-height: 1.6;
  }
  .template-copy h3 {
    font-size: 18px;
  }
  .tags span,
  .color-name strong,
  .scheme-identity small {
    overflow-wrap: anywhere;
  }
  .progress-overview {
    gap: 8px;
    padding: 18px 10px;
  }
  .progress-overview > div {
    padding-left: 8px;
  }
  .palette-summary {
    padding: 24px 20px;
  }
  .library-card small,
  .merchant-color small {
    font-size: 11px;
    line-height: 1.5;
  }
  .merchant-color strong {
    font-size: 12px;
  }
  .stitch-grid {
    grid-template-columns: minmax(0, 1fr);
  }
  .stitch-card {
    padding: 22px;
  }
  .empty-inline {
    padding: 36px 12px;
  }

  /* Stack the linked views while keeping row progress within easy reach. */
  .editor-heading {
    display: grid;
    grid-template-columns: 44px minmax(0, 1fr) 44px;
    gap: 8px;
    margin-bottom: 12px;
  }
  .editor-heading h1 {
    font-size: 24px;
    overflow-wrap: anywhere;
  }
  .editor-heading-actions {
    grid-column: 1 / -1;
    justify-content: space-between;
    width: 100%;
    margin: 4px 0 0;
  }
  .editor-tool-tabs {
    gap: 4px;
  }
  .editor-tool-tabs button {
    font-size: 13px;
    padding: 8px;
  }
  .editor-progress {
    position: sticky;
    top: 8px;
    z-index: 6;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 8px;
    padding: 10px 12px;
    box-shadow: 0 4px 16px #344c2c0d;
  }
  .editor-progress > div:first-child {
    grid-column: 1 / -1;
    justify-content: space-between;
  }
  .editor-progress strong {
    font-size: 22px;
  }
  .editor-progress .mini-label {
    font-size: 13px;
  }
  .mobile-workbench-links {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    margin: -8px 0 12px;
  }
  .mobile-workbench-links a {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
    padding: 0 8px;
    font-size: 13px;
    text-underline-offset: 4px;
  }
  .appearance-panel,
  .linked-chart-panel {
    scroll-margin-top: 128px;
  }
  .workbench-grid {
    gap: 18px;
  }
  .workbench-toolbar {
    height: auto;
    min-height: 76px;
    padding: 12px;
    flex-wrap: wrap;
  }
  .workbench-toolbar h2 {
    font-size: 20px;
  }
  .view-switch button {
    font-size: 12px;
    padding: 8px;
  }
  .yarn-stage {
    height: clamp(260px, 80vw, 380px);
  }
  .yarn-stage.structure-stage {
    touch-action: pan-y;
  }
  .preview-controls {
    height: auto;
    padding: 8px;
    gap: 6px;
    flex-wrap: wrap;
  }
  .preview-controls > div {
    flex: 1;
  }
  .preview-controls button {
    font-size: 12px;
    padding: 8px;
  }
  .preview-caption,
  .chart-footer {
    height: auto;
    min-height: 50px;
    font-size: 11px;
    line-height: 1.6;
    align-items: flex-start;
    flex-direction: column;
    gap: 4px;
  }
  .unit-navigation {
    min-height: 66px;
    height: auto;
    padding: 6px 8px;
  }
  .unit-navigation div {
    flex: 1;
    min-width: 0;
    flex-direction: column;
    align-items: center;
    gap: 4px;
  }
  .unit-navigation strong {
    font-size: 14px;
  }
  .unit-navigation span {
    max-width: none;
    text-align: center;
    font-size: 10px;
  }
  .grid-mode-toolbar {
    height: auto;
    min-height: 58px;
    gap: 6px;
    padding: 6px 8px;
    flex-wrap: wrap;
  }
  .grid-mode-toolbar .view-switch button {
    font-size: 12px;
  }
  .grid-mode-toolbar > span {
    flex: 1;
    min-width: 40px;
    max-width: none;
  }
  .linked-grid-surface,
  .linked-instructions {
    height: clamp(300px, 56dvh, 520px);
    max-width: 100%;
    -webkit-overflow-scrolling: touch;
  }
  .linked-grid-surface {
    text-align: left;
  }
  .linked-instructions {
    padding: 0 12px;
  }
  .linked-instructions .pattern-row {
    gap: 7px;
    padding: 12px 7px;
  }
  .row-check {
    width: 44px;
    height: 44px;
    font-size: 13px;
  }
  .linked-instructions .row-dot:not(:disabled) {
    width: 44px;
    height: 44px;
    font-size: 13px;
  }
  .linked-instructions .row-dot:disabled {
    width: 20px;
    height: 20px;
  }
  .linked-instructions .row-description {
    min-height: 44px;
    padding: 4px 0;
    font-size: 13px;
    overflow-wrap: anywhere;
  }
  .linked-instructions .row-description small {
    font-size: 11px;
  }
  .linked-instructions .row-total {
    max-width: 38px;
    font-size: 11px;
  }
  .color-name {
    flex-wrap: wrap;
  }
  .color-name i {
    flex-shrink: 0;
  }
  .color-name .icon {
    margin-left: auto;
  }
  .notes-layout .reference-image {
    max-width: 100%;
  }

  /* Worksheets and dialogs remain usable with a narrow screen or keyboard. */
  .fitting-form .field-pair,
  dialog .field-pair {
    grid-template-columns: minmax(0, 1fr);
    gap: 0;
  }
  .fitting-steps button {
    min-height: 60px;
    font-size: 13px;
    line-height: 1.6;
  }
  .fitting-steps button span {
    display: block;
    margin: 0;
    font-size: 17px;
  }
  .fitting-actions {
    flex-wrap: wrap;
  }
  .fitting-actions .button {
    flex: 1;
  }
  .fitting-source-summary {
    padding: 14px;
    gap: 8px;
  }
  dialog {
    width: calc(100vw - 24px);
    max-width: calc(100vw - 24px);
    max-height: calc(100dvh - 24px);
    padding: 24px 20px 20px;
    overscroll-behavior: contain;
  }
  dialog h2 {
    margin-right: 36px;
    font-size: 24px;
    overflow-wrap: anywhere;
  }
  .modal-close {
    top: 10px;
    right: 10px;
  }
  dialog.sheet {
    width: 100%;
    max-width: 100%;
    height: auto;
    max-height: 92dvh;
    margin: auto 0 0;
    border-radius: 18px 18px 0 0;
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
  }
  .modal-actions {
    flex-wrap: wrap;
  }
  .modal-actions .button {
    flex: 1;
    min-width: 100px;
  }
  .modal-templates {
    gap: 10px;
  }
  .modal-templates .template-copy {
    padding: 12px;
  }
  .merchant-picker {
    flex-wrap: wrap;
  }
  .merchant-picker .button {
    width: 100%;
  }
  .picker-library {
    padding: 3px;
    max-height: 200px;
  }
}

@media screen and (max-width: 360px) {
  .parameter-fields,
  .template-grid {
    grid-template-columns: minmax(0, 1fr);
  }
  .heading-actions {
    flex-wrap: wrap;
  }
  .heading-actions .button {
    flex-basis: 100%;
  }
  .nav-item {
    font-size: 9px;
  }
}
