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

:root {
    --bg: #0f0f13;
    --surface: #17171d;
    --border: #26262f;
    --text: #e8e8ec;
    --text-dim: #9494a0;
    --accent: #6d5ef2;
}

body {
    margin: 0;
    font-family: system-ui, -apple-system, sans-serif;
    line-height: 1.5;
    background: var(--bg);
    color: var(--text);
}

.page {
    min-height: 100vh;
}

.btn {
    display: inline-block;
    padding: 0.6rem 1.2rem;
    border-radius: 0.5rem;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 0.95rem;
    cursor: pointer;
    text-decoration: none;
}

.btn--google {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    font-weight: 600;
}

.btn--ghost {
    background: transparent;
}

.btn--primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    font-weight: 600;
}

/* Login */
.login-screen {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.login-card {
    width: 100%;
    max-width: 360px;
    text-align: center;
    padding: 2.5rem 2rem;
    border: 1px solid var(--border);
    border-radius: 1rem;
    background: var(--surface);
}

.login-card__brand {
    margin: 0 0 0.5rem;
    font-size: 1.75rem;
}

.login-card__tagline {
    margin: 0 0 1.5rem;
    color: var(--text-dim);
    font-size: 0.9rem;
}

/* Chat shell */
[x-cloak] {
    display: none !important;
}

.shell {
    display: flex;
    height: 100vh;
}

.sidebar {
    width: 260px;
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 1rem;
    gap: 0.75rem;
    overflow-y: auto;
}

.sidebar__brand {
    font-weight: 700;
    font-size: 1.1rem;
}

.tabs {
    display: flex;
    gap: 0.25rem;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    padding: 0.25rem;
}

.tab {
    flex: 1;
    text-align: center;
    padding: 0.4rem 0.5rem;
    border-radius: 0.35rem;
    color: var(--text-dim);
    text-decoration: none;
    font-size: 0.85rem;
}

.tab--active {
    background: var(--surface);
    color: var(--text);
    font-weight: 600;
}

.btn--new-chat {
    display: block;
    text-align: center;
    background: var(--accent);
    color: #fff;
    font-weight: 600;
    padding: 0.55rem 1rem;
    border-radius: 0.5rem;
    text-decoration: none;
}

.chat-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}

.chat-list__empty {
    color: var(--text-dim);
    font-size: 0.85rem;
    padding: 0.5rem;
}

.chat-list-item {
    display: block;
    color: var(--text);
    text-decoration: none;
    font-size: 0.88rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding: 0.5rem 0.6rem;
    border-radius: 0.4rem;
}

.chat-list-item:hover,
.chat-list-item--active {
    background: var(--bg);
}

/* Conversation header (top of main-pane): chat title + rename/delete menu,
   replacing the old per-row sidebar actions. */
.chat-menu {
    flex-shrink: 0;
}

.chat-menu__row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
}

.chat-menu__title {
    font-weight: 600;
    font-size: 1rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-menu__dropdown-wrap {
    position: relative;
    flex-shrink: 0;
}

.chat-menu__trigger {
    background: none;
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    color: var(--text-dim);
    font-size: 1rem;
    line-height: 1;
    padding: 0.3rem 0.6rem;
    cursor: pointer;
}

.chat-menu__trigger:hover {
    color: var(--text);
}

.chat-menu__dropdown {
    position: absolute;
    right: 0;
    top: calc(100% + 0.5rem);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    padding: 0.4rem;
    min-width: 140px;
    z-index: 10;
}

.chat-menu__item {
    display: block;
    width: 100%;
    text-align: left;
    background: none;
    border: none;
    border-radius: 0.35rem;
    color: var(--text);
    font-size: 0.85rem;
    cursor: pointer;
    padding: 0.4rem 0.5rem;
}

.chat-menu__item:hover {
    background: var(--bg);
}

.chat-menu__item--danger {
    color: var(--danger, #e5484d);
}

.chat-menu__rename-input {
    width: 100%;
    background: var(--surface);
    border: 1px solid var(--accent);
    border-radius: 0.3rem;
    color: var(--text);
    font-size: 1rem;
    padding: 0.3rem 0.5rem;
}

.shell__main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.topbar__mobile-brand {
    display: none;
    align-items: center;
    gap: 0.6rem;
    font-weight: 700;
    font-size: 1.05rem;
}

.sidebar-toggle {
    display: none;
    background: none;
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    color: var(--text);
    font-size: 1.1rem;
    line-height: 1;
    padding: 0.4rem 0.6rem;
    cursor: pointer;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.75rem 1.25rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.user-menu {
    position: relative;
}

.user-menu__trigger {
    background: none;
    border: none;
    padding: 0;
    cursor: pointer;
}

.user-menu__avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    object-fit: cover;
}

.user-menu__avatar--fallback {
    background: var(--accent);
    color: #fff;
    font-weight: 600;
}

.user-menu__dropdown {
    position: absolute;
    right: 0;
    top: calc(100% + 0.5rem);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    padding: 0.75rem;
    min-width: 200px;
    z-index: 10;
}

.user-menu__name {
    font-weight: 600;
    font-size: 0.9rem;
}

.user-menu__email {
    color: var(--text-dim);
    font-size: 0.8rem;
    margin-bottom: 0.5rem;
}

.user-menu__logout {
    border-top: 1px solid var(--border);
    padding-top: 0.5rem;
}

.user-menu__item {
    width: 100%;
    text-align: left;
    background: none;
    border: none;
    color: var(--text);
    font-size: 0.85rem;
    cursor: pointer;
    padding: 0.3rem 0;
}

.main-pane {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    min-width: 0;
    padding: 1.25rem;
    gap: 1rem;
}

.topbar__model {
    flex: 1;
    min-width: 0;
}

/* Model picker */
.model-picker {
    position: relative;
}

.model-picker__trigger {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    color: var(--text);
    padding: 0.4rem 0.75rem;
    font-size: 0.85rem;
    cursor: pointer;
    transition: border-color 0.15s ease;
}

.model-picker__trigger:hover {
    border-color: var(--text-dim);
}

.model-picker__caret {
    color: var(--text-dim);
    transition: transform 0.15s ease;
}

.model-picker__caret--open {
    transform: rotate(180deg);
}

.model-picker__dropdown {
    position: absolute;
    left: 0;
    top: calc(100% + 0.5rem);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.65rem;
    padding: 0.35rem;
    width: 300px;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.4);
    z-index: 10;
}

.model-picker__option-form {
    margin: 0;
}

.model-picker__option {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 0.6rem;
    background: none;
    border: 1px solid transparent;
    color: var(--text);
    padding: 0.45rem 0.6rem;
    border-radius: 0.4rem;
    cursor: pointer;
    font-size: 0.85rem;
    text-align: left;
}

.model-picker__option:hover {
    background: var(--bg);
}

.model-picker__option:focus-visible,
.model-picker__trigger:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.model-picker__option--selected {
    border-color: var(--border);
    background: var(--bg);
}

.model-picker__option-text {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0.05rem;
    min-width: 0;
}

.model-picker__option-name {
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.model-picker__option--selected .model-picker__option-name {
    color: var(--accent);
    font-weight: 600;
}

.model-picker__option-provider {
    color: var(--text-dim);
    font-size: 0.75rem;
}

.model-picker__stats {
    display: flex;
    width: 100%;
    gap: 0.5rem;
    align-items: center;
    margin-top: 0.3rem;
}

.model-picker__stat {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 0.25rem;
    min-width: 0;
}

.model-picker__stat-label {
    color: var(--text-dim);
    font-size: 0.6rem;
    font-weight: 700;
    line-height: 1;
}

.model-picker__stat-bar {
    flex: 1;
    min-width: 0;
    height: 3px;
    border-radius: 2px;
    background: var(--border);
    overflow: hidden;
}

.model-picker__stat-fill {
    display: block;
    height: 100%;
    background: var(--text-dim);
}

.model-picker__option--selected .model-picker__stat-fill {
    background: var(--accent);
}

@media (prefers-reduced-motion: reduce) {
    .model-picker__trigger,
    .model-picker__caret {
        transition: none;
    }
}

@media (max-width: 768px) {
    .topbar {
        flex-wrap: wrap;
    }

    .topbar__mobile-brand {
        display: flex;
        order: 1;
    }

    .user-menu {
        order: 2;
    }

    .topbar__model {
        order: 3;
        flex-basis: 100%;
    }

    .sidebar-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        z-index: 60;
        width: 82vw;
        max-width: 300px;
        transform: translateX(-100%);
        transition: transform 0.2s ease;
        box-shadow: 8px 0 24px rgba(0, 0, 0, 0.4);
    }

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

    .sidebar-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 50;
    }
}

@media (prefers-reduced-motion: reduce) {
    .sidebar {
        transition: none;
    }
}

/* Empty state */
.empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    text-align: center;
}

.empty-state__title {
    font-size: 1.4rem;
    margin: 0;
}

.empty-state__hint {
    color: var(--text-dim);
    margin: 0;
}

.empty-state__chips {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    justify-content: center;
    max-width: 640px;
}

.chip {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 999px;
    color: var(--text-dim);
    padding: 0.5rem 1rem;
    font-size: 0.82rem;
    cursor: pointer;
}

.chip:hover {
    color: var(--text);
    border-color: var(--accent);
}

/* Message list */
.message-list {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.message {
    display: flex;
    gap: 0.75rem;
    max-width: min(720px, 100%);
    min-width: 0;
}

.message--user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.message__avatar {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--surface);
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    color: var(--text-dim);
}

.message__body {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
    min-width: 0;
}

.message--user .message__body {
    align-items: flex-end;
}

.message__text {
    margin: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.75rem;
    padding: 0.6rem 0.9rem;
    font-size: 0.92rem;
    overflow-wrap: anywhere;
}

.message--user .message__text {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

.message__image {
    width: auto;
    max-width: min(360px, 100%);
    max-height: 360px;
    border-radius: 0.75rem;
    border: 1px solid var(--border);
    display: block;
    cursor: zoom-in;
}

.message__image:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.image-lightbox {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    z-index: 200;
}

.image-lightbox__img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    border-radius: 0.5rem;
    cursor: zoom-out;
}

.image-lightbox__close {
    position: fixed;
    top: 1rem;
    right: 1.25rem;
    background: none;
    border: none;
    color: #fff;
    font-size: 1.8rem;
    line-height: 1;
    cursor: pointer;
    padding: 0.25rem;
}

.image-lightbox__close:hover {
    color: var(--text-dim);
}

.message__image--broken {
    display: none;
}

.message__image-wrap {
    display: block;
    width: fit-content;
    max-width: 100%;
}

.message__image-grid {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 0.5rem;
}

.message__image-grid .message__image {
    max-width: min(180px, 100%);
    max-height: 180px;
}

.message__image-grid .message__image-placeholder {
    width: 120px;
    height: 90px;
}

.message__image-placeholder {
    display: none;
    align-items: center;
    justify-content: center;
    width: 240px;
    height: 160px;
    max-width: 360px;
    border-radius: 0.75rem;
    border: 1px dashed var(--border);
    background: var(--surface);
    color: var(--text-dim);
    font-size: 0.85rem;
}

.message__image-wrap--broken .message__image {
    display: none;
}

.message__image-wrap--broken .message__image-placeholder {
    display: flex;
}

.message__caption {
    margin: 0;
    color: var(--text-dim);
    font-size: 0.8rem;
}

.message__cost {
    color: var(--text-dim);
    opacity: 0.7;
    font-variant-numeric: tabular-nums;
}

.message__pending {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    color: var(--text-dim);
    font-size: 0.85rem;
}

.dots {
    display: inline-flex;
    gap: 0.2rem;
}

.dots span {
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: var(--text-dim);
    animation: dots-bounce 1.2s infinite ease-in-out;
}

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

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

@keyframes dots-bounce {
    0%, 80%, 100% { opacity: 0.3; }
    40% { opacity: 1; }
}

.message__error {
    color: #f2857a;
    background: rgba(242, 133, 122, 0.1);
    border: 1px solid rgba(242, 133, 122, 0.3);
    border-radius: 0.6rem;
    padding: 0.6rem 0.9rem;
    font-size: 0.85rem;
}

.message__actions {
    display: flex;
    gap: 0.5rem;
}

/* Composer */
.composer {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.75rem;
    padding: 0.75rem;
}

.composer__attach {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
}

.composer__preview-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
}

.composer__attach-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    background: var(--bg);
    border: 1px dashed var(--border);
    border-radius: 0.5rem;
    padding: 0.4rem 0.75rem;
    font-size: 0.8rem;
    color: var(--text-dim);
    cursor: pointer;
}

.composer__file-input {
    display: none;
}

.composer__preview-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    padding: 0.3rem 0.5rem;
}

.composer__preview-thumb {
    width: 28px;
    height: 28px;
    object-fit: cover;
    border-radius: 0.3rem;
}

.composer__preview-name {
    font-size: 0.78rem;
    color: var(--text-dim);
    max-width: 160px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.composer__preview-remove {
    background: none;
    border: none;
    color: var(--text-dim);
    cursor: pointer;
    font-size: 1rem;
    line-height: 1;
}

.composer__input {
    resize: none;
    background: transparent;
    border: none;
    color: var(--text);
    font-family: inherit;
    font-size: 0.95rem;
    min-height: 2.4rem;
}

.composer__input:focus {
    outline: none;
}

.composer__submit {
    align-self: flex-end;
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    font-weight: 600;
}

/* Settings modal */
.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    z-index: 100;
}

.modal {
    position: relative;
    width: 100%;
    max-width: 420px;
    max-height: 90vh;
    overflow-y: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.75rem;
    padding: 1.5rem;
}

.modal__close {
    position: absolute;
    top: 0.75rem;
    right: 0.75rem;
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 1.3rem;
    line-height: 1;
    cursor: pointer;
    padding: 0.25rem;
}

.modal__close:hover {
    color: var(--text);
}

.settings-modal__title {
    margin: 0 0 0.5rem;
    font-size: 1.15rem;
}

.settings-modal__status {
    margin: 0 0 1.25rem;
    color: var(--text-dim);
    font-size: 0.85rem;
}

.settings-modal__form {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.settings-modal__label {
    font-size: 0.8rem;
    color: var(--text-dim);
}

.settings-modal__input-row {
    display: flex;
    gap: 0.4rem;
}

.settings-modal__input {
    flex: 1;
    min-width: 0;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    color: var(--text);
    font-size: 0.9rem;
    padding: 0.5rem 0.65rem;
}

.settings-modal__input:focus {
    outline: none;
    border-color: var(--accent);
}

.settings-modal__reveal-toggle {
    flex-shrink: 0;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    color: var(--text-dim);
    font-size: 0.78rem;
    padding: 0 0.75rem;
    cursor: pointer;
}

.settings-modal__hint {
    margin: 0.3rem 0 0;
    color: var(--text-dim);
    font-size: 0.75rem;
}

.settings-modal__actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    margin-top: 0.75rem;
}

.settings-modal__clear-form {
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border);
}

.settings-modal__clear-btn {
    width: 100%;
    color: var(--text-dim);
}

/* Toast */
.toast {
    position: fixed;
    bottom: 1.5rem;
    left: 50%;
    transform: translateX(-50%);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    padding: 0.65rem 1.1rem;
    font-size: 0.85rem;
    color: var(--text);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
    z-index: 200;
}
