.app {
    display: grid;
    grid-template-columns: 400px minmax(0, 1fr);
    height: 100dvh;
    overflow: hidden;
}

/* ---------- Sidebar ---------- */

.sidebar {
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
}

.sidebar__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 16px 12px;
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    font-weight: 600;
    letter-spacing: -0.01em;
}

.brand__mark {
    display: grid;
    place-items: center;
    width: 28px;
    height: 28px;
    border-radius: 8px;
    background: var(--accent);
    color: var(--accent-contrast);
    font-size: 15px;
    font-weight: 700;
}

.sidebar__actions {
    display: inline-flex;
    gap: 4px;
}

.sidebar__body {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 4px 16px 16px;
    display: flex;
    flex-direction: column;
    gap: 18px;
}

.sidebar__footer {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 12px 16px;
    border-top: 1px solid var(--border);
}

.sidebar__user {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.sidebar__user-name {
    font-size: 13px;
    font-weight: 500;
    color: var(--text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.btn--small {
    padding: 6px 10px;
    font-size: 12px;
}

/* ---------- Controls ---------- */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    background: var(--surface-2);
    font-size: 14px;
    font-weight: 500;
    transition: background 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
}

.btn:hover {
    background: var(--accent-soft);
}

.btn--block {
    width: 100%;
}

.btn--ghost {
    border-color: var(--border);
    background: transparent;
}

.btn--ghost:hover {
    background: var(--surface-2);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.icon-btn {
    display: grid;
    place-items: center;
    width: 34px;
    height: 34px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-muted);
    transition: background 0.15s ease, color 0.15s ease;
}

.icon-btn:hover {
    background: var(--surface-2);
    color: var(--text);
}

.icon-btn--close,
.icon-btn--menu {
    display: none;
}

[data-theme="light"] .icon--sun,
[data-theme="dark"] .icon--moon {
    display: none;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.field__label {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 12px;
    font-weight: 500;
    color: var(--text-muted);
    letter-spacing: 0.01em;
}

.field__label .hint {
    flex: none;
}

.field--full {
    grid-column: 1 / -1;
}

.field--grow {
    flex: 1 1 auto;
}

.field--grow .input--area {
    flex: 1 1 auto;
    min-height: 200px;
    max-height: none;
}

.input {
    width: 100%;
    padding: 9px 11px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    transition: border-color 0.15s ease, background 0.15s ease;
}

.input:hover {
    border-color: var(--border-strong);
}

.input:focus {
    outline: none;
    border-color: var(--text);
    background: var(--surface);
}

.input--area {
    resize: none;
    min-height: 36px;
    max-height: 200px;
    overflow-y: auto;
    line-height: 1.5;
}

select.input {
    appearance: none;
    background-image: linear-gradient(45deg, transparent 50%, var(--text-muted) 50%),
        linear-gradient(135deg, var(--text-muted) 50%, transparent 50%);
    background-position: calc(100% - 16px) center, calc(100% - 11px) center;
    background-size: 5px 5px, 5px 5px;
    background-repeat: no-repeat;
    padding-right: 30px;
}

.switch {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    user-select: none;
    font-size: 14px;
}

.switch input {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
}

.switch__track {
    position: relative;
    width: 36px;
    height: 20px;
    border-radius: 999px;
    background: var(--border-strong);
    transition: background 0.18s ease;
    flex: none;
}

.switch__thumb {
    position: absolute;
    top: 2px;
    left: 2px;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--surface);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
    transition: transform 0.18s ease;
}

.switch input:checked + .switch__track {
    background: var(--accent);
}

.switch input:checked + .switch__track .switch__thumb {
    transform: translateX(16px);
    background: var(--accent-contrast);
}

.switch input:focus-visible + .switch__track {
    outline: 2px solid var(--text);
    outline-offset: 2px;
}

.switch__text {
    color: var(--text);
}

.hint {
    display: inline-grid;
    place-items: center;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--surface-2);
    color: var(--text-faint);
    font-size: 10px;
    font-style: italic;
    font-weight: 600;
    cursor: help;
}

.params {
    flex-shrink: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

.params__summary {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-muted);
    cursor: pointer;
    list-style: none;
}

.params__summary::-webkit-details-marker {
    display: none;
}

.params__summary:hover {
    background: var(--surface-2);
    color: var(--text);
}

.icon--chevron {
    width: 15px;
    height: 15px;
    transition: transform 0.18s ease;
}

.params[open] .icon--chevron {
    transform: rotate(180deg);
}

.params__grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    padding: 12px;
    border-top: 1px solid var(--border);
    max-height: max(160px, calc(100dvh - 470px));
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    scrollbar-color: var(--border-strong) transparent;
}

.params__grid::-webkit-scrollbar {
    width: 8px;
}

.params__grid::-webkit-scrollbar-track {
    background: transparent;
}

.params__grid::-webkit-scrollbar-thumb {
    background: var(--border-strong);
    border: 2px solid transparent;
    background-clip: padding-box;
    border-radius: 999px;
}

.params__grid::-webkit-scrollbar-thumb:hover {
    background: var(--text-faint);
    background-clip: padding-box;
}

.field--compact .field__label {
    font-size: 11px;
}

.field--compact .input {
    padding: 7px 9px;
    font-size: 13px;
}

/* ---------- Status ---------- */

.status {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
    color: var(--text-muted);
}

.status__dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--text-faint);
    flex: none;
}

.status--online .status__dot {
    background: var(--success);
}

.status--offline .status__dot {
    background: var(--danger);
}

/* ---------- Chat ---------- */

.chat {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    background: var(--bg);
}

.chat__header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 20px;
    border-bottom: 1px solid var(--border);
    background: color-mix(in srgb, var(--bg) 85%, transparent);
    backdrop-filter: blur(8px);
}

.chat__title {
    flex: 1;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat__messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 28px 20px;
    scroll-behavior: smooth;
}

/* ---------- Messages ---------- */

.msg {
    display: flex;
    gap: 12px;
    width: 100%;
    max-width: 780px;
    margin: 0 auto 26px;
}

.msg--user {
    flex-direction: row-reverse;
}

.msg__avatar {
    display: grid;
    place-items: center;
    width: 28px;
    height: 28px;
    border-radius: 8px;
    font-size: 12px;
    font-weight: 700;
    flex: none;
    margin-top: 2px;
}

.msg--user .msg__avatar {
    background: var(--accent);
    color: var(--accent-contrast);
}

.msg--assistant .msg__avatar {
    background: var(--surface-2);
    color: var(--text-muted);
    border: 1px solid var(--border);
}

.msg__content {
    min-width: 0;
    max-width: 100%;
}

.msg--user .msg__content {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

.msg__bubble {
    padding: 11px 15px;
    border-radius: var(--radius);
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.msg--user .msg__bubble {
    background: var(--accent);
    color: var(--accent-contrast);
    border-bottom-right-radius: 4px;
}

.msg--assistant .msg__bubble {
    background: var(--surface);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.msg__bubble:empty {
    display: none;
}

.msg__bubble code {
    font-family: var(--mono);
    font-size: 0.88em;
    background: var(--surface-2);
    padding: 1px 5px;
    border-radius: 5px;
}

.msg--user .msg__bubble code {
    background: rgba(255, 255, 255, 0.16);
}

.msg__bubble pre {
    margin: 10px 0 2px;
    padding: 12px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow-x: auto;
}

.msg__bubble pre code {
    background: none;
    padding: 0;
    font-size: 13px;
}

.msg__meta {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 8px;
    font-size: 11px;
    color: var(--text-faint);
}

.reasoning {
    margin-bottom: 8px;
    border: 1px dashed var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--surface-2);
}

.reasoning > summary {
    padding: 7px 11px;
    font-size: 12px;
    color: var(--text-muted);
    cursor: pointer;
    list-style: none;
}

.reasoning > summary::-webkit-details-marker {
    display: none;
}

.reasoning__body {
    padding: 0 11px 11px;
    font-size: 13px;
    color: var(--text-muted);
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    max-height: 320px;
    overflow-y: auto;
}

.typing {
    display: inline-flex;
    gap: 4px;
    padding: 4px 0;
}

.typing span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--text-faint);
    animation: typing 1.2s infinite ease-in-out;
}

.typing span:nth-child(2) {
    animation-delay: 0.15s;
}

.typing span:nth-child(3) {
    animation-delay: 0.3s;
}

@keyframes typing {
    0%, 60%, 100% {
        opacity: 0.3;
        transform: translateY(0);
    }
    30% {
        opacity: 1;
        transform: translateY(-3px);
    }
}

.msg--error .msg__bubble {
    background: color-mix(in srgb, var(--danger) 10%, var(--surface));
    border-color: color-mix(in srgb, var(--danger) 40%, var(--border));
    color: var(--danger);
}

/* ---------- Empty state ---------- */

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    min-height: 60%;
    padding: 40px 20px;
    color: var(--text-muted);
}

.empty-state__mark {
    display: grid;
    place-items: center;
    width: 52px;
    height: 52px;
    margin-bottom: 18px;
    border-radius: 14px;
    background: var(--accent);
    color: var(--accent-contrast);
    font-size: 24px;
    font-weight: 700;
}

.empty-state__title {
    font-size: 20px;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 6px;
}

.empty-state__text {
    max-width: 420px;
    font-size: 14px;
}

.empty-state[hidden] {
    display: none;
}

/* ---------- Composer ---------- */

.composer {
    padding: 12px 20px 18px;
    border-top: 1px solid var(--border);
    background: var(--bg);
}

.composer__box {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    width: 100%;
    max-width: 780px;
    margin: 0 auto;
    padding: 8px 8px 8px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
    transition: border-color 0.15s ease;
}

.composer__box:focus-within {
    border-color: var(--text);
}

.composer__input {
    flex: 1;
    min-height: 24px;
    max-height: 200px;
    padding: 6px 0;
    border: none;
    background: transparent;
    resize: none;
    overflow-y: auto;
    line-height: 1.5;
}

.composer__input:focus {
    outline: none;
}

.composer__input::placeholder {
    color: var(--text-faint);
}

.composer__send {
    display: grid;
    place-items: center;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--accent);
    color: var(--accent-contrast);
    transition: opacity 0.15s ease, transform 0.1s ease;
    flex: none;
}

.composer__send:hover {
    opacity: 0.88;
}

.composer__send:active {
    transform: scale(0.96);
}

.composer__send:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.composer__hint {
    max-width: 780px;
    margin: 8px auto 0;
    font-size: 11px;
    color: var(--text-faint);
    text-align: center;
}

/* ---------- Toast ---------- */

.toast {
    position: fixed;
    left: 50%;
    bottom: 24px;
    transform: translate(-50%, 16px);
    max-width: min(90vw, 480px);
    padding: 11px 16px;
    border-radius: var(--radius-sm);
    background: var(--text);
    color: var(--bg);
    font-size: 13px;
    box-shadow: var(--shadow);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease, transform 0.2s ease;
    z-index: 60;
}

.toast--visible {
    opacity: 1;
    transform: translate(-50%, 0);
}

.toast--error {
    background: var(--danger);
    color: #fff;
}

/* ---------- Backdrop ---------- */

.backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 40;
}

.backdrop--visible {
    display: block;
}

/* ---------- Responsive ---------- */

@media (max-width: 860px) {
    .app {
        grid-template-columns: 1fr;
    }

    .sidebar {
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        width: 400px;
        max-width: 86vw;
        z-index: 50;
        transform: translateX(-100%);
        transition: transform 0.22s ease;
    }

    .sidebar--open {
        transform: translateX(0);
        box-shadow: var(--shadow);
    }

    .icon-btn--close,
    .icon-btn--menu {
        display: grid;
    }

    .chat__messages {
        padding: 20px 14px;
    }

    .composer {
        padding: 10px 14px 14px;
    }
}

@media (min-width: 861px) {
    .sidebar__actions .icon-btn--close {
        display: none;
    }
}

@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}
