/*
 * CHAT responsive hardening.
 * Loaded after site.css, persona-features.css and voice.css so this file can
 * refine layout behavior without duplicating the base visual system.
 */

html {
    overflow-x: clip;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    min-width: 0;
    min-height: 100dvh;
    overflow-x: clip;
}

.app-main,
.chat-page,
.config-page,
.persona-toolbar,
.toolbar-primary,
.toolbar-actions,
.conversation-shell,
.conversation-topline,
.session-label,
.conversation-tools,
.composer,
.composer-footer,
.composer-left,
.editor-panel,
.editor-panel-head,
.persona-form,
.form-row,
.form-field,
.prompt-step,
.prompt-step-body,
.prompt-step-top {
    min-width: 0;
}

img,
video,
svg,
canvas {
    max-width: 100%;
}

button,
a,
select,
input,
textarea {
    touch-action: manipulation;
}

.nav-link {
    position: relative;
}

.app-header,
.persona-toolbar {
    -webkit-backdrop-filter: blur(18px) saturate(130%);
}

.persona-toolbar {
    -webkit-backdrop-filter: blur(14px);
}

.chat-feed {
    overscroll-behavior: contain;
    scrollbar-gutter: stable;
}

.message-bubble {
    max-width: 100%;
    word-break: break-word;
}

.message-bubble pre,
.message-bubble table {
    max-width: 100%;
}

.message-bubble table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.message-bubble img {
    height: auto;
    border-radius: 10px;
}

.session-label,
.session-label strong,
.voice-state,
.voice-state-text {
    min-width: 0;
}

.session-label strong {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.composer textarea {
    min-height: 46px;
}

@supports (height: 100dvh) {
    .app-main {
        min-height: calc(100dvh - var(--header-h));
    }

    .conversation-shell {
        min-height: calc(100dvh - var(--header-h) - 122px);
    }

    .chat-feed {
        max-height: calc(100dvh - 330px);
    }
}

@media (max-width: 980px) {
    .app-main {
        padding-bottom: calc(82px + env(safe-area-inset-bottom, 0px));
    }

    .main-nav {
        bottom: calc(10px + env(safe-area-inset-bottom, 0px));
        width: max-content;
        max-width: calc(100vw - 20px);
        overflow-x: auto;
        overscroll-behavior-x: contain;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }

    .main-nav::-webkit-scrollbar {
        display: none;
    }

    .nav-link {
        flex: 0 0 auto;
        min-height: 42px;
    }

    .persona-toolbar {
        gap: 12px;
    }

    .toolbar-primary,
    .toolbar-actions {
        min-width: 0;
    }

    .toolbar-actions > * {
        min-width: 0;
    }

    .conversation-shell {
        overflow: hidden;
    }

    .editor-actions {
        flex-wrap: wrap;
    }

    .editor-actions > .btn,
    .editor-actions > form {
        flex: 1 1 150px;
        min-width: 0;
    }

    .editor-actions > form .btn {
        width: 100%;
    }
}

@media (max-width: 700px) {
    :root {
        --mobile-gutter: 12px;
    }

    .app-header {
        height: var(--header-h);
        padding-left: max(var(--mobile-gutter), env(safe-area-inset-left, 0px));
        padding-right: max(var(--mobile-gutter), env(safe-area-inset-right, 0px));
    }

    .brand {
        gap: 8px;
    }

    .brand-mark {
        width: 32px;
        height: 32px;
    }

    .main-nav {
        width: calc(100vw - 16px);
        max-width: 430px;
        justify-content: space-between;
        gap: 4px;
        padding: 5px;
        border-radius: 13px;
    }

    .nav-link {
        flex: 1 1 0;
        justify-content: center;
        min-width: 0;
        min-height: 44px;
        height: 44px;
        padding: 0 10px;
        font-size: 12px;
        white-space: nowrap;
    }

    .chat-page,
    .config-page {
        width: auto;
        margin-left: max(var(--mobile-gutter), env(safe-area-inset-left, 0px));
        margin-right: max(var(--mobile-gutter), env(safe-area-inset-right, 0px));
    }

    .chat-page {
        padding-top: 8px;
        padding-bottom: 12px;
    }

    /* A tall sticky toolbar consumes most of a phone viewport. Keep it in flow. */
    .persona-toolbar {
        position: static;
        top: auto;
        gap: 10px;
        box-shadow: 0 10px 28px rgba(0,0,0,.20);
    }

    .toolbar-actions {
        gap: 8px;
    }

    .toolbar-actions .btn,
    .switch-control,
    .select-control,
    .icon-button,
    .voice-button,
    .send-button {
        min-height: 44px;
    }

    .switch-control {
        padding: 7px 9px;
    }

    .switch-copy {
        min-width: 0;
    }

    .switch-copy strong,
    .switch-copy small {
        overflow-wrap: anywhere;
    }

    .popover {
        left: max(8px, env(safe-area-inset-left, 0px));
        right: max(8px, env(safe-area-inset-right, 0px));
        bottom: calc(72px + env(safe-area-inset-bottom, 0px));
        max-height: min(70dvh, 560px);
        border-radius: 14px;
    }

    .persona-check-list {
        max-height: min(58dvh, 440px);
        -webkit-overflow-scrolling: touch;
    }

    .privacy-status {
        margin-top: 8px;
    }

    .conversation-shell {
        min-height: 0;
        margin-top: 8px;
        border-radius: 16px;
        overflow: hidden;
    }

    .conversation-topline {
        min-height: 50px;
        gap: 10px;
    }

    .session-label {
        flex: 1 1 auto;
        overflow: hidden;
    }

    .session-label .eyebrow {
        flex: 0 0 auto;
    }

    .conversation-tools {
        flex: 0 0 auto;
    }

    .chat-feed {
        min-height: clamp(280px, 40dvh, 360px);
        max-height: 52dvh;
        padding: 20px 10px 18px;
        -webkit-overflow-scrolling: touch;
    }

    .empty-state {
        margin-top: 12px;
    }

    .empty-state h1 {
        font-size: clamp(25px, 8vw, 30px);
    }

    .empty-state > p {
        font-size: 13px;
    }

    .prompt-grid {
        margin-top: 20px;
        gap: 8px;
    }

    .prompt-card {
        min-height: 82px;
        padding: 13px;
    }

    .message-row {
        margin-bottom: 18px;
        gap: 9px;
    }

    .message-avatar {
        width: 32px;
        height: 32px;
        border-radius: 9px;
    }

    .message-wrap,
    .message-row.user .message-wrap {
        max-width: calc(100% - 40px);
    }

    .message-bubble {
        padding: 12px 13px;
        font-size: 14px;
        line-height: 1.58;
    }

    .message-meta {
        gap: 6px;
    }

    .message-meta span {
        max-width: 42vw;
    }

    .thinking-strip {
        min-height: 46px;
        margin: 0 10px 8px;
        padding: 7px 10px;
    }

    .thinking-strip > div:not(.thinking-dots) {
        min-width: 0;
    }

    .thinking-strip span {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .composer-wrap {
        padding: 0 8px 12px;
    }

    .selected-participants {
        max-height: 58px;
        overflow-y: auto;
        scrollbar-width: thin;
    }

    .composer {
        border-radius: 15px;
    }

    /* 16px prevents Safari iOS from zooming the page when a field receives focus. */
    .composer textarea,
    .select-control,
    .form-field input,
    .form-field textarea,
    .contacts-search {
        font-size: 16px;
    }

    .composer textarea {
        max-height: 34dvh;
        padding: 13px 13px 6px;
    }

    .composer-footer {
        min-height: 52px;
        padding: 5px 6px 6px 8px;
        gap: 8px;
        flex-wrap: wrap;
    }

    .composer-left {
        flex: 1 1 150px;
        gap: 7px;
    }

    .composer-actions {
        flex: 0 0 auto;
    }

    .voice-state {
        max-width: min(44vw, 210px);
    }

    .send-button {
        min-width: 88px;
        height: 44px;
    }

    .config-page {
        padding-top: 20px;
        padding-bottom: 18px;
    }

    .config-heading {
        gap: 16px;
        margin-bottom: 18px;
    }

    .config-heading h1 {
        font-size: clamp(28px, 9vw, 36px);
    }

    .config-heading p {
        font-size: 13px;
    }

    .persona-list-panel,
    .editor-panel {
        border-radius: 15px;
    }

    .persona-list {
        max-height: 260px;
        -webkit-overflow-scrolling: touch;
    }

    .panel-title-row {
        min-height: 58px;
    }

    .editor-panel-head {
        min-height: 0;
        padding: 14px;
        gap: 12px;
    }

    .editor-actions {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 7px;
    }

    .editor-actions > .btn,
    .editor-actions > form {
        min-width: 0;
        width: 100%;
    }

    .editor-actions > .btn:last-child,
    .editor-actions > form:last-child:nth-child(odd) {
        grid-column: 1 / -1;
    }

    .persona-form {
        padding: 14px;
    }

    .form-field {
        margin-bottom: 15px;
    }

    .form-field textarea {
        min-height: 220px;
    }

    .prompt-step .script-field textarea {
        min-height: 180px;
    }

    .prompt-pipeline-head {
        margin-top: 22px;
        padding-top: 18px;
        gap: 12px;
    }

    .prompt-pipeline-head .btn {
        width: 100%;
    }

    .form-footer {
        gap: 14px;
    }

    .contacts-list-head {
        flex-wrap: wrap;
        align-items: stretch;
    }

    .contacts-list-head > div {
        flex: 1 1 140px;
    }

    .contacts-search {
        width: 100% !important;
        min-height: 44px !important;
        flex: 1 1 180px;
    }
}

@media (max-width: 480px) {
    .toolbar-actions {
        grid-template-columns: 1fr;
    }

    .round-control {
        grid-column: auto;
        justify-content: space-between;
        min-height: 44px;
        padding: 0 8px;
    }

    .round-control .select-compact {
        width: 82px;
    }

    .nav-link {
        padding-inline: 6px;
        font-size: 11px;
    }

    .message-avatar {
        display: none;
    }

    .message-wrap,
    .message-row.user .message-wrap {
        max-width: 94%;
    }

    .message-meta span {
        max-width: 48vw;
    }

    .composer-footer {
        align-items: center;
    }

    .voice-state {
        max-width: 38vw;
    }

    .editor-actions {
        grid-template-columns: 1fr;
    }

    .editor-actions > .btn,
    .editor-actions > form,
    .editor-actions > .btn:last-child,
    .editor-actions > form:last-child:nth-child(odd) {
        grid-column: auto;
    }

    .privacy-note {
        padding: 11px 12px;
    }
}

@media (max-width: 360px) {
    .brand-word,
    .brand-dot {
        display: none;
    }

    .nav-link {
        font-size: 10.5px;
        letter-spacing: -.01em;
    }

    .chat-page,
    .config-page {
        margin-left: 8px;
        margin-right: 8px;
    }

    .persona-toolbar {
        padding: 8px;
    }

    .conversation-topline {
        padding-inline: 9px;
    }

    .session-label {
        gap: 6px;
    }

    .session-label .eyebrow {
        display: none;
    }

    .chat-feed {
        padding-inline: 8px;
    }

    .composer-actions {
        gap: 3px;
    }

    .send-button {
        min-width: 76px;
        padding-inline: 10px;
    }
}

@media (max-width: 980px) and (max-height: 620px) {
    .persona-toolbar {
        position: static;
        top: auto;
    }

    .chat-feed {
        min-height: 220px;
        max-height: 46dvh;
    }

    .popover {
        max-height: 72dvh;
    }
}

@media (hover: none) {
    .btn-primary:hover,
    .send-button:hover:not(:disabled),
    .prompt-card:hover,
    .voice-button:hover:not(:disabled) {
        transform: none;
    }
}

/* Fixed slim composer dock */
.chat-page {
    padding-bottom: 126px;
}

.chat-feed {
    padding-bottom: 136px;
    scroll-padding-bottom: 136px;
}

.composer-wrap {
    position: fixed;
    left: 50%;
    bottom: 0;
    z-index: 95;
    width: min(1240px, calc(100vw - 40px));
    padding: 5px clamp(12px, 3vw, 42px) max(7px, env(safe-area-inset-bottom, 0px));
    transform: translateX(-50%);
    border-top: 1px solid var(--border);
    background: rgba(9, 10, 12, .94);
    box-shadow: 0 -14px 38px rgba(0, 0, 0, .30);
    -webkit-backdrop-filter: blur(16px) saturate(125%);
    backdrop-filter: blur(16px) saturate(125%);
}

.selected-participants {
    min-height: 0;
    margin: 0 4px 4px;
}

.selected-participants:empty {
    display: none;
}

.participant-chip {
    min-height: 21px;
}

.composer {
    border-radius: 13px;
}

.composer textarea {
    min-height: 36px;
    max-height: 96px;
    padding: 9px 13px 2px;
    line-height: 1.4;
}

.composer-footer {
    min-height: 40px;
    padding: 2px 5px 5px 9px;
    gap: 8px;
}

.voice-button {
    width: 34px;
    height: 34px;
    flex-basis: 34px;
    border-radius: 9px;
}

.composer-actions .btn {
    min-height: 34px;
    padding-inline: 10px;
}

.send-button {
    min-width: 88px;
    height: 34px;
}

@media (max-width: 980px) {
    .app-main {
        padding-bottom: calc(174px + env(safe-area-inset-bottom, 0px));
    }

    .chat-page {
        padding-bottom: 154px;
    }

    .chat-feed {
        padding-bottom: 154px;
        scroll-padding-bottom: 154px;
    }

    .composer-wrap {
        bottom: calc(64px + env(safe-area-inset-bottom, 0px));
        z-index: 115;
        width: calc(100vw - 20px);
        padding: 5px 8px 7px;
        border: 1px solid var(--border);
        border-radius: 14px;
    }
}

@media (max-width: 700px) {
    .composer-wrap {
        width: calc(100vw - 16px);
    }

    .composer textarea {
        min-height: 36px;
        max-height: 92px;
        padding: 8px 11px 2px;
    }

    .composer-footer {
        min-height: 40px;
        padding: 2px 4px 4px 7px;
        flex-wrap: nowrap;
    }

    .voice-button,
    .send-button {
        min-height: 36px;
        height: 36px;
    }

    .voice-button {
        width: 36px;
        flex-basis: 36px;
    }

    .send-button {
        min-width: 82px;
    }
}

