/**
 * Dedicated responsive cleanup for creator surfaces.
 * Loaded last. Does not hide controls — stacks, wraps, and contains them.
 *
 * Widths: 320, 360, 375, 390, 412, 480, 768, 1024, 1280, 1440+
 */

:root {
  --nav-safe: calc(var(--mobile-nav-h, 64px) + env(safe-area-inset-bottom, 0px));
  --page-gutter: clamp(10px, 3vw, 24px);
}

html {
  overflow-x: clip;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

img,
video,
canvas,
svg {
  max-width: 100%;
  height: auto;
}

iframe,
audio {
  max-width: 100%;
}

button,
.btn,
input,
select,
textarea {
  max-width: 100%;
}

.app-shell,
.main,
.content,
.card,
.form-row,
.form-grid,
.grid,
.page-head,
.sg-page,
.sg-studio,
.ac {
  min-width: 0;
}

/* —— Shared chrome —— */
.page-head {
  min-width: 0;
  flex-wrap: wrap;
  align-items: flex-start;
}

.page-head > * { min-width: 0; }

.page-head h1,
.page-head p {
  overflow-wrap: anywhere;
}

.page-head .btn,
.page-head-actions {
  flex: 0 1 auto;
}

.modal,
.auth-modal-card,
.ac-modal__panel,
.bottom-sheet {
  max-width: min(100%, 100vw);
  max-height: min(90dvh, 100svh);
  overflow: auto;
  overscroll-behavior: contain;
}

.modal-backdrop.open,
.auth-modal.open,
.ac-modal:not([hidden]) {
  padding: max(12px, env(safe-area-inset-top, 0px))
    max(12px, env(safe-area-inset-right, 0px))
    max(12px, env(safe-area-inset-bottom, 0px))
    max(12px, env(safe-area-inset-left, 0px));
  align-items: safe center;
}

.toast-wrap {
  bottom: calc(var(--nav-safe) + 12px);
  right: max(12px, env(safe-area-inset-right, 0px));
}

.studio-layout {
  grid-template-columns: minmax(0, 22rem) minmax(0, 1fr);
}

.form-row.two-col {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.compare-model-grid {
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 200px), 1fr));
}

/* ============================================================
   1440+
   ============================================================ */
@media (min-width: 1440px) {
  .page-studio .content,
  .page-character .content,
  .page-world .content,
  .page-director .content,
  .page-feature .content,
  .page-create .content,
  .page-audio .content {
    width: min(1440px, 100%);
  }

  .page-home .content { max-width: 1240px; }

  .sg-studio[data-layout="split"] .sg-studio__body,
  .sg-studio[data-layout="audio"] .sg-studio__body {
    grid-template-columns: minmax(0, 28rem) minmax(0, 1fr);
  }
}

/* ============================================================
   1280
   ============================================================ */
@media (min-width: 1025px) and (max-width: 1280px) {
  .content { padding-inline: var(--page-gutter); }

  .sg-studio__body,
  .sg-studio[data-layout="split"] .sg-studio__body {
    grid-template-columns: minmax(0, 22rem) minmax(0, 1fr);
  }

  .sg-cast,
  .sg-world,
  .sg-director {
    grid-template-columns: minmax(0, 20rem) minmax(0, 1fr);
  }
}

/* ============================================================
   1024 — tablet landscape / small laptop
   ============================================================ */
@media (max-width: 1024px) {
  .studio-layout,
  .sg-studio__body,
  .sg-studio[data-layout="split"] .sg-studio__body,
  .sg-studio[data-layout="audio"] .sg-studio__body,
  .sg-studio[data-layout="compare"] .sg-studio__body,
  .sg-cast,
  .sg-world,
  .sg-director,
  .sg-compare {
    grid-template-columns: minmax(0, 1fr);
  }

  .dash-hero,
  .dash-grid { grid-template-columns: minmax(0, 1fr); }

  .promo-grid,
  .card-grid,
  .models-grid,
  .media-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .tool-cover-grid {
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 200px), 1fr));
  }

  .oa-create-arc {
    --arc-w: min(340px, 92vw);
  }

  .ac { --ac-rail: 220px; }
}

@media (min-width: 481px) and (max-width: 768px) {
  .tool-cover-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }
}

/* ============================================================
   768 — tablet / large phone
   ============================================================ */
@media (max-width: 768px) {
  .grid-2,
  .grid-3,
  .grid-4,
  .form-row.two-col,
  .compare-grid.cols-2,
  .compare-grid.cols-3,
  .compare-grid.cols-4,
  .arena-grid,
  .role-check-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .page-head {
    flex-direction: column;
    align-items: stretch;
  }

  .page-head .btn,
  .page-head-actions .btn {
    width: auto;
    justify-content: center;
  }

  .sg-page__head {
    flex-direction: column;
    align-items: stretch;
  }

  .sg-page__head .btn { width: fit-content; }

  .sg-generate {
    position: sticky;
    bottom: calc(var(--nav-safe) + 12px);
    z-index: 6;
    background: color-mix(in srgb, var(--panel) 94%, transparent);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    box-shadow: 0 -8px 20px color-mix(in srgb, var(--bg) 55%, transparent);
  }

  .sg-generate .btn {
    flex: 1 1 auto;
    justify-content: center;
  }

  .sg-studio__editor,
  .sg-cast__form,
  .sg-world__form,
  .sg-director__form {
    --sg-generate-space: 5.5rem;
  }

  .sg-studio__scroll {
    padding-bottom: var(--sg-generate-space);
  }

  .sg-history__meta,
  .sg-stage__tabs,
  .sg-director-scene header,
  .sg-director .director-scene header {
    flex-wrap: wrap;
  }

  .sg-stage__tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .sg-output video,
  .sg-compare-slider video,
  .sg-compare-slider img,
  .sg-preview-slot img,
  .sg-preview-slot video {
    max-height: min(52vh, 360px);
  }

  .ac {
    height: calc(100dvh - var(--mobile-topbar-h, 56px) - env(safe-area-inset-top, 0px) - var(--nav-safe));
    min-height: 0;
  }

  .page-chat .content {
    overflow: visible;
    padding-bottom: 0 !important;
  }

  .page-home .content {
    padding-bottom: calc(var(--nav-safe) + 24px);
  }

  .ac__composer {
    flex-wrap: wrap;
    border-radius: 18px;
    align-items: center;
  }

  .ac__composer textarea {
    flex: 1 1 100%;
    min-width: 0;
    order: -1;
  }

  .ac__multi-btn {
    max-width: none;
    flex: 1 1 auto;
  }

  .ac__msg-actions { opacity: 1; }

  .ac__model-menu {
    min-width: 0;
    width: min(280px, calc(100vw - 24px));
    max-width: calc(100vw - 24px);
    left: 0;
    right: auto;
  }

  .ac-modal__head,
  .ac-modal__foot {
    flex-wrap: wrap;
  }

  .sg-composer__bar {
    flex-wrap: wrap;
  }

  .sg-composer__hint {
    flex: 1 1 100%;
    white-space: normal;
  }

  .sg-plus__menu {
    min-width: 0;
    width: min(240px, calc(100vw - 32px));
  }

  .sg-section__head {
    flex-wrap: wrap;
  }

  .chat-layout { grid-template-columns: minmax(0, 1fr); }

  .admin-shell .card.form-grid .grid-2,
  .admin-shell .grid.grid-2 {
    grid-template-columns: minmax(0, 1fr);
  }

  .admin-shell .card .btn,
  .admin-shell .form-actions .btn {
    max-width: 100%;
  }
}

/* ============================================================
   480
   ============================================================ */
@media (max-width: 480px) {
  .promo-grid,
  .card-grid,
  .models-grid,
  .media-grid,
  .tool-catalog,
  .tools-grid,
  .tools-2col,
  .tool-cover-grid {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .tool-cover {
    min-height: 148px;
  }

  .tools-2col,
  .tool-catalog.tools-grid {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .sg-hero__switch {
    width: 100%;
  }

  .sg-hero__switch button {
    min-width: 0;
    font-size: 13px;
    padding-inline: 8px;
  }

  .sg-carousel {
    grid-auto-columns: minmax(72vw, 1fr);
  }

  .create-radial__stage {
    height: calc(var(--radius, 120px) + var(--origin, 24px) + var(--icon-size, 44px) + 28px);
  }

  .create-radial__label,
  .radial-label {
    font-size: 11px;
  }

  .sg-generate {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
  }

  .sg-generate__cost { margin-right: auto; }

  .sg-generate .btn {
    width: auto;
    flex: 1 1 8rem;
  }

  .mobile-nav a span {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

/* ============================================================
   412 / 390 / 375 / 360 / 320
   ============================================================ */
@media (max-width: 412px) {
  .content {
    padding-left: max(10px, env(safe-area-inset-left, 0px));
    padding-right: max(10px, env(safe-area-inset-right, 0px));
  }

  .sg-tools__tab,
  .sg-chip,
  .ac__chip,
  .creator-chip {
    padding-inline: 10px;
    font-size: 12px;
  }

  .sg-field--prompt textarea { min-height: 100px; }

  .ac__msg.is-user .ac__msg-body { max-width: 100%; }

  .sg-hero__title { font-size: var(--hero-title-size-mobile, 1.38rem); }

  .admin-shell .card { padding: 14px; }

  .admin-shell .page-head .btn,
  .admin-shell .page-head a.btn {
    width: 100%;
  }
}

@media (max-width: 390px) {
  .sg-page__titles h1,
  .page-head h1 {
    font-size: clamp(1.25rem, 7vw, 1.6rem);
  }

  .create-orb {
    width: 42px;
    height: 42px;
  }

  .create-radial__orb,
  .create-radial__icon {
    width: var(--icon-size, 44px);
    height: var(--icon-size, 44px);
  }

  .sg-hero__modes { gap: 8px; }
}

@media (max-width: 360px) {
  .mobile-nav {
    padding-inline: 2px;
  }

  .mobile-nav a {
    font-size: 9px;
    gap: 2px;
  }

  .sg-drop { padding: 10px; }

  .sg-studio__scroll,
  .sg-stage,
  .sg-cast__form,
  .sg-world__form,
  .sg-director__form,
  .sg-cast__board,
  .sg-world__board,
  .sg-director__board {
    padding: 12px;
  }

  .ac__topbar {
    padding: 8px 10px;
    gap: 6px;
  }

  .ac__model-btn {
    max-width: 46vw;
  }

  .ac__model-btn span,
  .ac__model-btn {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

@media (max-width: 320px) {
  .btn {
    padding-inline: 12px;
  }

  .sg-tools__tab { padding-inline: 8px; }

  .credit-pill.compact { padding-inline: 7px; }

  .sg-hero__switch button { font-size: 12px; gap: 4px; }

  .create-radial__label,
  .radial-label { font-size: 10px; }
}

/* ============================================================
   Container queries — studio body when the column is narrow
   ============================================================ */
.sg-studio,
.sg-cast,
.sg-world,
.sg-director,
.ac__main {
  container-type: inline-size;
}

@container (max-width: 640px) {
  .sg-studio__body,
  .sg-studio[data-layout="split"] .sg-studio__body,
  .sg-studio[data-layout="audio"] .sg-studio__body,
  .sg-studio[data-layout="compare"] .sg-studio__body {
    grid-template-columns: minmax(0, 1fr);
  }

  .sg-compare { grid-template-columns: minmax(0, 1fr); }
}

/* ============================================================
   Studio settings sheet — above bottom nav, no overflow
   ============================================================ */
.sg-sheet:not([hidden]) {
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.sg-sheet__panel {
  width: min(560px, 100%);
  max-height: min(72dvh, calc(100dvh - var(--nav-safe) - 16px));
  overflow: auto;
  margin: 0 12px calc(var(--nav-safe) + 8px);
  padding: 14px;
  border-radius: 18px 18px 12px 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  min-width: 0;
}

.sg-sheet__panel header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

@media (max-width: 900px) {
  .sg-more.is-sheet {
    bottom: calc(var(--nav-safe) + 8px);
    max-height: min(70dvh, calc(100dvh - var(--nav-safe) - 24px));
  }

  .bottom-sheet {
    padding-bottom: var(--nav-safe);
    max-height: min(80dvh, calc(100dvh - var(--nav-safe)));
  }

  .sheet-row {
    flex-wrap: wrap;
    align-items: flex-start;
  }

  .sheet-row > * { min-width: 0; }
}

/* ============================================================
   Admin tools / features forms
   ============================================================ */
.admin-shell .card,
.admin-shell .form-grid,
.admin-shell .form-row {
  min-width: 0;
}

.admin-shell .card {
  overflow-wrap: anywhere;
}

.admin-shell .form-grid .grid,
.admin-shell .form-grid .grid-2 {
  min-width: 0;
}

.admin-shell .form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.admin-shell .form-actions .btn {
  flex: 0 1 auto;
}

.admin-shell .table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 768px) {
  .admin-shell .card.form-grid .grid,
  .admin-shell .card .grid-2 {
    grid-template-columns: minmax(0, 1fr);
  }

  .admin-shell input,
  .admin-shell select,
  .admin-shell textarea,
  .admin-shell .form-row input,
  .admin-shell .form-row select,
  .admin-shell .form-row textarea {
    width: 100%;
    min-width: 0;
    max-width: 100%;
  }

  .admin-filters input,
  .admin-filters select {
    min-width: 0;
    flex: 1 1 140px;
  }
}

/* ============================================================
   Touch — keep every control reachable
   ============================================================ */
@media (pointer: coarse) {
  .sg-chip,
  .sg-tools__tab,
  .sg-expand__btn,
  .creator-chip,
  .ac__chip,
  .sg-inspire-tabs button,
  .sg-hero__switch button {
    min-height: 40px;
  }

  .sg-generate .btn,
  .studio-actions .btn {
    min-height: 44px;
  }

  .ac__msg-actions { opacity: 1; }

  .ac__msg-action {
    min-height: 32px;
    padding: 4px 2px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .sg-generate,
  .create-radial__item { transition: none; }
}
