:root {
    --composer-offset: 260px;
    --topbar-height: 48px;
    --bottom-nav-height: 0px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
}

html { -webkit-text-size-adjust: 100%; }

body {
    padding-left: var(--safe-left);
    padding-right: var(--safe-right);
}

button, .btn, .mode, .nav__btn, .icon-btn, .send-btn, .bottom-nav__btn {
    -webkit-tap-highlight-color: transparent;
}

@media (hover: none), (pointer: coarse) {
    .recent-card__actions,
    .history-card__actions { opacity: 1; }
}

/* ── Mobile (≤768px) ── */
@media (max-width: 768px) {
    :root {
        --composer-offset: 0px;
        --bottom-nav-height: calc(3.5rem + var(--safe-bottom));
    }

    .app {
        height: 100dvh;
        max-height: 100dvh;
        overflow: hidden;
    }

    .topbar {
        flex-shrink: 0;
        padding: calc(.5rem + var(--safe-top)) .85rem .5rem;
    }

    .topbar__left {
        flex: 1;
        min-width: 0;
        gap: .45rem;
    }

    .topbar__left .nav { display: none; }

    .brand__name { font-size: .88rem; }
    .brand__tag { display: none; }

    .topbar__right {
        flex-shrink: 0;
        gap: .3rem;
    }

    .topbar__right #api-status,
    .topbar__right .pill--user { display: none; }

    .btn--ghost.btn--sm {
        padding: .35rem .5rem;
        font-size: .74rem;
    }

    .main {
        flex: 1;
        min-height: 0;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        padding: 0;
    }

    .main:not(.main--chat) {
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding: .65rem .65rem .35rem;
    }

    .main--chat {
        padding: 0;
        overflow: hidden;
    }

    .view--studio,
    .view--gallery,
    .view--admin { min-height: 0; }

    .studio-stage {
        min-height: auto;
        padding: .75rem 0 .5rem;
    }

    .empty-state {
        padding: 1.5rem .75rem 1rem;
    }

    .empty-state__title { font-size: 1.65rem; }

    .studio-hero__actions {
        opacity: 1;
        transform: none;
    }

    .studio-hero__media img {
        max-height: min(52vh, 480px);
    }

    .studio-thread__item {
        flex: 0 0 48px;
        width: 48px;
        height: 48px;
    }

    .masonry-grid--gallery,
    .masonry-grid--recent {
        column-gap: .55rem;
    }

    .masonry-grid > .history-card,
    .masonry-grid > .recent-card {
        margin-bottom: .55rem;
    }

    .gallery-grid {
        padding-bottom: .5rem;
    }

    .gallery-toolbar {
        margin-bottom: .85rem;
    }

    .history-card__body {
        opacity: 1;
        max-height: none;
    }

    /* Composer: in-flow on mobile */
    .composer {
        position: static;
        transform: none;
        width: 100%;
        flex-shrink: 0;
        z-index: auto;
        max-height: none;
        padding: .4rem .65rem .45rem;
        pointer-events: auto;
    }

    .composer > * { pointer-events: auto; }

    .composer__drawer-btn { display: inline-grid; }

    .composer__drawer {
        display: none;
        max-height: min(42dvh, 300px);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        margin-bottom: .4rem;
        padding: .65rem .75rem;
        border-radius: var(--radius-lg);
    }

    .composer.composer--drawer-open .composer__drawer { display: block; }

    .composer__prompt-shell { flex-shrink: 0; }

    .prompt-row {
        gap: .3rem;
        padding: .4rem .45rem .4rem .55rem;
        border-radius: var(--radius-lg);
        max-width: none;
        align-items: center;
    }

    #prompt {
        font-size: 16px;
        max-height: 80px;
        padding: .4rem 0;
    }

    .spicy-toggle { padding: .32rem .45rem; }
    .spicy-toggle__label { display: none; }

    .icon-btn, .send-btn {
        width: 40px;
        height: 40px;
    }

    .modes, .style-bar {
        justify-content: flex-start;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        flex-wrap: nowrap;
        margin: 0 0 .4rem;
    }

    .modes::-webkit-scrollbar,
    .style-bar::-webkit-scrollbar { display: none; }

    .mode, .style-chip {
        flex-shrink: 0;
        font-size: .76rem;
    }

    .attachment-bar,
    .edit-options-bar,
    .settings-panel { margin-bottom: .4rem; }

    .ref-slot { width: 68px; height: 68px; }

    .settings-grid {
        grid-template-columns: 1fr 1fr;
        gap: .5rem;
    }

    .aspect-option {
        flex: 1 1 calc(33.333% - .3rem);
        min-width: 52px;
    }

    /* Bottom nav */
    .bottom-nav {
        display: flex;
        flex-shrink: 0;
        align-items: stretch;
        justify-content: space-around;
        gap: .1rem;
        padding: .4rem .45rem calc(.4rem + var(--safe-bottom));
        background: rgba(0, 0, 0, 0.94);
        border-top: 1px solid var(--border);
        backdrop-filter: blur(16px);
        z-index: 35;
    }

    .bottom-nav__btn {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: .12rem;
        border: none;
        background: transparent;
        color: var(--text-subtle);
        padding: .3rem .2rem;
        border-radius: var(--radius-sm);
        cursor: pointer;
        min-height: 46px;
        transition: color .2s var(--ease), background .2s var(--ease);
    }

    .bottom-nav__btn--active {
        color: var(--text);
        background: var(--surface-2);
    }

    .bottom-nav__icon {
        width: 20px;
        height: 20px;
    }

    .bottom-nav__icon svg {
        width: 100%;
        height: 100%;
    }

    .bottom-nav__label {
        font-size: .6rem;
        font-weight: 600;
        letter-spacing: .02em;
    }

    /* Chat mobile */
    .chat-layout {
        grid-template-columns: 1fr;
        grid-template-rows: auto minmax(0, 1fr);
        flex: 1;
        min-height: 0;
    }

    .chat-sidebar {
        flex-direction: row;
        align-items: center;
        gap: .35rem;
        padding: .45rem .55rem;
        border-right: none;
        border-bottom: 1px solid var(--border);
        flex-shrink: 0;
        max-height: none;
    }

    .chat-sidebar__title { display: none; }

    #chat-new {
        width: auto;
        flex-shrink: 0;
        padding: .35rem .55rem;
        font-size: .7rem;
    }

    #chat-sessions {
        flex: 1;
        flex-direction: row;
        overflow-x: auto;
        overflow-y: hidden;
        min-height: 0;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        gap: .3rem;
    }

    #chat-sessions::-webkit-scrollbar { display: none; }

    .chat-session-wrap {
        min-width: 108px;
        max-width: 140px;
        flex-shrink: 0;
    }

    .chat-session {
        padding: .4rem .5rem;
        font-size: .72rem;
    }

    .chat-main { min-height: 0; overflow: hidden; }

    .chat-messages {
        flex: 1;
        min-height: 0;
        padding: .65rem .55rem;
        gap: .65rem;
    }

    .chat-msg { max-width: 100%; }

    .chat-msg__media img {
        max-height: min(42dvh, 260px);
    }

    .chat-composer {
        padding: .5rem .55rem calc(.5rem + var(--safe-bottom));
    }

    .chat-mode-hint { display: none; }

    .chat-modes {
        flex-wrap: nowrap;
        overflow-x: auto;
        scrollbar-width: none;
        margin-bottom: .35rem;
    }

    .chat-modes::-webkit-scrollbar { display: none; }

    .chat-mode {
        flex-shrink: 0;
        font-size: .72rem;
    }

    #chat-input { font-size: 16px; max-height: 80px; }

    .chat-input-row {
        padding: .38rem .4rem;
        gap: .3rem;
        border-radius: var(--radius-lg);
    }

    .chat-ref-slot { width: 60px; height: 60px; }
    .chat-ref-strip__hint { display: none; }

    /* Viewer */
    .viewer { padding: 0; align-items: stretch; }

    .viewer__panel {
        width: 100%;
        max-height: none;
        height: 100%;
        border-radius: 0;
        border: none;
        padding: calc(.85rem + var(--safe-top)) .85rem calc(.85rem + var(--safe-bottom));
    }

    .viewer__image,
    .viewer__video { max-height: 42dvh; }

    .viewer__actions { flex-direction: column; }
    .viewer__actions .btn { width: 100%; }

    /* Admin */
    .admin-header { flex-direction: column; align-items: stretch; }
    .admin-tabs { flex-wrap: wrap; gap: .3rem; }

    .admin-tab {
        flex: 1 1 calc(50% - .3rem);
        min-width: 0;
        text-align: center;
        padding: .42rem .45rem;
        font-size: .74rem;
    }

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

    .recent-card__action,
    .history-card__action {
        width: 34px;
        height: 34px;
    }
}

@media (max-width: 480px) {
    .settings-grid,
    .admin-stats { grid-template-columns: 1fr; }

    .login-card { padding: 2rem 1.25rem; }

    .admin-toolbar {
        flex-direction: column;
        align-items: stretch;
    }

    .admin-toolbar .field {
        width: 100%;
        min-width: 0;
    }

    .admin-modal {
        align-items: end;
        padding: 0;
    }

    .admin-modal__card {
        align-self: end;
        max-height: 90dvh;
        overflow-y: auto;
        border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    }
}