/* Smartphone-Layout: Das Desktop-Layout oberhalb von 640 px bleibt unveraendert. */
@media (width <= 640px) {
  html,
  body {
    max-width: 100%;
    overflow-x: hidden;
  }

  body {
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }

  .topbar {
    gap: 10px;
    padding: max(10px, env(safe-area-inset-top)) 12px 10px;
  }

  .brand {
    min-width: 0;
  }

  .brand-text h1 {
    font-size: 17px;
  }

  .topbar-right {
    display: grid;
    gap: 8px;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    width: 100%;
  }

  .topbar-right .search-input,
  .topbar-right .seg-switch {
    grid-column: 1 / -1;
    min-width: 0;
    width: 100%;
  }

  .topbar-right .seg-switch .seg-btn {
    flex: 1;
    justify-content: center;
  }

  .topbar-right .theme-select-wrap,
  .topbar-right .sync-pill,
  .topbar-right > .btn {
    justify-content: center;
    min-width: 0;
    width: 100%;
  }

  main {
    padding: 16px 12px 30px;
  }

  .info-banner,
  .mode-banner,
  .printer-banner {
    align-items: flex-start;
    border-radius: 10px;
    padding: 11px 12px;
  }

  .info-banner .mw-link,
  .printer-banner-cta {
    margin-left: 0;
    white-space: normal;
    width: 100%;
  }

  .stat-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .stat-card {
    min-width: 0;
    padding: 12px;
  }

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

  .overlay {
    align-items: flex-end;
    overscroll-behavior: contain;
    padding: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom));
  }

  .overlay .modal,
  .overlay .modal.wide {
    border-radius: 18px 18px 12px 12px;
    max-height: calc(100dvh - max(16px, env(safe-area-inset-top)) - max(16px, env(safe-area-inset-bottom)));
    max-width: none;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 18px 16px max(16px, env(safe-area-inset-bottom));
    scroll-padding-block: 72px;
    scrollbar-gutter: stable;
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
    width: 100%;
  }

  .overlay .modal h2 {
    backdrop-filter: blur(14px);
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    border-bottom: 1px solid var(--border-soft);
    margin: -18px -16px 16px;
    padding: 16px;
    position: sticky;
    top: -18px;
    z-index: 12;
  }

  .overlay .modal h2 .x,
  .broadcast-popup-x {
    align-items: center;
    display: inline-flex;
    flex: 0 0 44px;
    height: 44px;
    justify-content: center;
    margin: -10px;
    width: 44px;
  }

  .modal-actions {
    background: linear-gradient(180deg, transparent, var(--surface) 18%);
    bottom: calc(-1 * max(16px, env(safe-area-inset-bottom)));
    flex-direction: column-reverse;
    margin: 16px -4px calc(-1 * max(16px, env(safe-area-inset-bottom)));
    padding: 20px 4px max(16px, env(safe-area-inset-bottom));
    position: sticky;
    z-index: 10;
  }

  .modal-actions .btn,
  .conversation-actions .btn {
    justify-content: center;
    min-height: 46px;
    width: 100%;
  }

  button,
  .btn,
  .chip,
  summary,
  select,
  input,
  textarea {
    touch-action: manipulation;
  }

  button:not(.broadcast-popup-x),
  .btn,
  select,
  input[type="text"],
  input[type="url"],
  input[type="number"],
  input[type="datetime-local"] {
    min-height: 44px;
  }

  .field input[type="text"],
  .field input[type="url"],
  .field input[type="number"],
  .field input[type="datetime-local"],
  .field select,
  .field textarea,
  .add-row-form input,
  .add-row-form select {
    font-size: 16px;
  }

  .two-col,
  .detail-grid,
  .spec-groups {
    grid-template-columns: 1fr;
  }

  .img-input-row,
  .printer-hero,
  .detail-hero,
  .admin-message-bar {
    align-items: stretch;
    flex-direction: column;
  }

  .img-input-row .file-btn,
  .admin-message-bar > .btn,
  .admin-customer-picker {
    justify-content: center;
    width: 100%;
  }

  .detail-hero img,
  .detail-hero .swatch-fallback-lg {
    aspect-ratio: 16 / 9;
    height: auto;
    width: 100%;
  }

  .conversation-modal {
    height: calc(100dvh - max(16px, env(safe-area-inset-top)) - max(16px, env(safe-area-inset-bottom)));
    max-height: none;
  }

  .conversation-thread {
    min-height: 180px;
  }

  .conversation-message {
    max-width: 92%;
  }

  .filament-table-wrap,
  .text-modal-body,
  .conversation-thread {
    -webkit-overflow-scrolling: touch;
  }

  .footer-event-list {
    bottom: calc(62px + env(safe-area-inset-bottom));
    left: 8px;
    max-height: min(58dvh, 430px);
    position: fixed;
    right: 8px;
    width: auto;
  }

  .footer-event-list .footer-event-item {
    min-height: 56px;
  }

  footer {
    align-items: stretch;
    gap: 10px;
    padding: 18px 12px max(28px, calc(16px + env(safe-area-inset-bottom)));
  }

  footer > button,
  footer > a,
  footer .language-select-wrap,
  footer .footer-event-dropdown {
    justify-content: center;
    min-height: 44px;
    width: 100%;
  }

  .toast {
    bottom: max(12px, env(safe-area-inset-bottom));
    border-radius: 12px;
    width: calc(100% - 24px);
  }
}

@media (width <= 380px) {
  .stat-grid,
  .topbar-right {
    grid-template-columns: 1fr;
  }

  .topbar-right > * {
    grid-column: 1;
  }

  .brand-text h1 {
    font-size: 16px;
  }
}

@media (width <= 900px) and (height <= 520px) and (orientation: landscape) {
  .overlay {
    align-items: center;
    padding: 6px max(8px, env(safe-area-inset-right)) 6px max(8px, env(safe-area-inset-left));
  }

  .overlay .modal,
  .overlay .modal.wide {
    max-height: calc(100dvh - 12px);
  }
}
