* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --bg: #fff;
    --sidebar: #f7f7f8;
    --surface: #fff;
    --surface-2: #f1f1f2;
    --text: #171717;
    --muted: #737373;
    --border: #e5e5e5;
    --user: #171717;
    --user-text: #fff;
    --assistant: #f1f1f1;
    --assistant-text: #171717;
    --accent: #111;
    color-scheme: light;
}

:root[data-theme="dark"] {
    --bg: #0b0b0c;
    --sidebar: #111113;
    --surface: #0b0b0c;
    --surface-2: #1a1a1d;
    --text: #f5f5f5;
    --muted: #999;
    --border: #29292d;
    --user: #f5f5f5;
    --user-text: #111;
    --assistant: #1a1a1d;
    --assistant-text: #f5f5f5;
    --accent: #fff;
    color-scheme: dark;
}

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, textarea, input {
    font: inherit;
}

button {
    cursor: pointer;
}

.app {
    width: 100%;
    height: 100dvh;
    display: flex;
    overflow: hidden;
    background: var(--bg);
}

.sidebar {
    width: 280px;
    height: 100dvh;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    background: var(--sidebar);
    border-right: 1px solid var(--border);
    padding: 14px;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 50px;
    margin-bottom: 8px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
}

.brand-icon, .welcome-icon {
    width: 36px;
    height: 36px;
    display: grid;
    place-items: center;
    border-radius: 10px;
    background: var(--accent);
    color: var(--bg);
    font-size: 12px;
    font-weight: 800;
}

.new-chat-btn {
    width: 100%;
    min-height: 46px;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0 14px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    color: var(--text);
    margin-bottom: 16px;
    font-weight: 500;
}

.new-chat-btn:hover, .history-item:hover, .sidebar-btn:hover, .theme-option:hover, .icon-btn:hover {
    background: var(--surface-2);
}

.history-label {
    font-size: 12px;
    font-weight: 600;
    color: var(--muted);
    margin: 0 8px 8px;
    letter-spacing: 0.5px;
    text-transform: uppercase;
}

.pinned-section {
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border);
}

.history-list, .pinned-list {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
}

.history-item {
    width: 100%;
    min-height: 42px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 8px 10px;
    border-radius: 8px;
    color: var(--text);
    cursor: pointer;
    margin-bottom: 2px;
    transition: background 0.15s;
}

.history-item.active {
    background: var(--surface-2);
    font-weight: 600;
}

.history-title {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-size: 14px;
    flex: 1;
    min-width: 0;
}

.history-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
    opacity: 0;
    transition: opacity 0.15s;
}

.history-item:hover .history-actions {
    opacity: 1;
}

.history-action-btn {
    border: 0;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    font-size: 14px;
    line-height: 1;
    display: grid;
    place-items: center;
}

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

.history-action-btn.is-pinned {
    opacity: 1;
    color: var(--text);
}

.rename-input {
    width: 100%;
    flex: 1;
    min-width: 0;
    height: 30px;
    padding: 2px 8px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface-2);
    color: var(--text);
    font-size: 13px;
    outline: none;
}

.empty-history {
    padding: 15px 8px;
    color: var(--muted);
    font-size: 13px;
}

.sidebar-bottom {
    border-top: 1px solid var(--border);
    padding-top: 10px;
    margin-top: 10px;
}

.sidebar-btn {
    width: 100%;
    border: 0;
    background: transparent;
    color: var(--text);
    min-height: 42px;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0 10px;
    border-radius: 8px;
    text-align: left;
}

.main {
    min-width: 0;
    flex: 1;
    height: 100dvh;
    display: flex;
    flex-direction: column;
}

.topbar {
    height: 58px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 18px;
    border-bottom: 1px solid var(--border);
}

.topbar-title {
    flex: 1;
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.icon-btn {
    width: 38px;
    height: 38px;
    border: 0;
    border-radius: 9px;
    display: grid;
    place-items: center;
    background: transparent;
    color: var(--text);
    font-size: 20px;
}

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

.messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 20px;
    scroll-behavior: smooth;
    display: flex;
    flex-direction: column;
}

.messages > :first-child {
    margin-top: auto;
}

.welcome {
    height: 100%;
    min-height: 300px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    text-align: center;
    gap: 12px;
    color: var(--muted);
    margin: auto;
}

.welcome h1 {
    color: var(--text);
    font-size: clamp(24px, 4vw, 38px);
}

.welcome p {
    font-size: 15px;
}

.message {
    width: 100%;
    max-width: 850px;
    margin: 0 auto 22px;
    display: flex;
}

.message.user {
    justify-content: flex-end;
}

.bubble {
    max-width: min(75%, 720px);
    padding: 11px 15px;
    border-radius: 16px;
    line-height: 1.55;
    font-size: 15px;
    overflow-wrap: anywhere;
}

.message.user .bubble {
    background: var(--user);
    color: var(--user-text);
    border-bottom-right-radius: 5px;
}

.message.assistant {
    justify-content: flex-start;
}

.message.assistant .bubble {
    background: var(--assistant);
    color: var(--assistant-text);
    border-bottom-left-radius: 5px;
}

.bubble pre {
    overflow-x: auto;
    padding: 12px;
    margin: 8px 0;
    border-radius: 8px;
    background: #000;
    color: #fff;
    font-size: 13px;
}

.bubble-image {
    max-width: 100%;
    max-height: 300px;
    border-radius: 10px;
    margin-top: 8px;
    cursor: pointer;
    display: block;
    transition: opacity 0.15s;
}

.bubble-image:hover {
    opacity: 0.85;
}

.error {
    color: #ef4444;
}

.typing {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 2px;
}

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

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

@keyframes typingBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: .4; }
    30% { transform: translateY(-5px); opacity: 1; }
}

.composer-wrapper {
    flex-shrink: 0;
    width: 100%;
    padding: 8px 20px max(10px, env(safe-area-inset-bottom));
}

.composer-wrapper.drag-over {
    outline: 2px dashed var(--accent);
    outline-offset: -4px;
    border-radius: 16px;
}

.file-preview {
    display: none;
    flex-wrap: wrap;
    gap: 8px;
    max-width: 850px;
    margin: 0 auto 8px;
    padding: 6px 8px;
}

.preview-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 13px;
    max-width: 220px;
    position: relative;
}

.preview-item img {
    width: 36px;
    height: 36px;
    object-fit: cover;
    border-radius: 6px;
}

.preview-file-icon {
    font-size: 20px;
    width: 36px;
    height: 36px;
    display: grid;
    place-items: center;
}

.preview-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 110px;
    color: var(--text);
}

.preview-remove {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: 0;
    background: var(--accent);
    color: var(--bg);
    font-size: 13px;
    cursor: pointer;
    display: grid;
    place-items: center;
    line-height: 1;
}

.composer {
    width: 100%;
    max-width: 850px;
    margin: 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 8px;
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 16px;
    box-shadow: 0 4px 20px rgba(0,0,0,.05);
}

.attach-btn {
    width: 38px;
    height: 38px;
    flex-shrink: 0;
    border: 0;
    border-radius: 10px;
    background: transparent;
    color: var(--muted);
    font-size: 20px;
    display: none;
    place-items: center;
    cursor: pointer;
    transition: color 0.15s;
}

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

.composer textarea {
    flex: 1;
    min-width: 0;
    max-height: 180px;
    resize: none;
    border: 0;
    outline: 0;
    background: transparent;
    color: var(--text);
    padding: 9px 8px;
    line-height: 1.45;
    font-size: 16px;
}

.composer textarea::placeholder {
    color: var(--muted);
}

.send-btn {
    width: 38px;
    height: 38px;
    flex-shrink: 0;
    border: 0;
    border-radius: 10px;
    background: var(--accent);
    color: var(--bg);
    font-size: 20px;
    display: grid;
    place-items: center;
}

.send-btn:hover {
    opacity: .85;
}

.composer-note {
    max-width: 850px;
    margin: 6px auto 0;
    text-align: center;
    font-size: 11px;
    color: var(--muted);
}

.modal {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.modal.show {
    display: flex;
}

.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,.55);
}

.settings-panel {
    position: relative;
    width: min(430px, 100%);
    max-height: min(85vh, 700px);
    background: var(--surface);
    padding: 20px;
    overflow-y: auto;
    border-radius: 16px;
    box-shadow: 0 10px 60px rgba(0,0,0,.35);
}

.settings-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 30px;
}

.setting {
    margin-bottom: 28px;
}

.setting label {
    display: block;
    font-weight: 600;
    margin-bottom: 7px;
}

.setting p {
    color: var(--muted);
    font-size: 13px;
    line-height: 1.5;
    margin-bottom: 12px;
}

.setting textarea {
    width: 100%;
    min-height: 160px;
    resize: vertical;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 12px;
    outline: 0;
    background: var(--surface-2);
    color: var(--text);
    font-size: 15px;
}

.theme-options {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

.theme-option {
    min-height: 42px;
    border: 1px solid var(--border);
    border-radius: 9px;
    background: var(--surface);
    color: var(--text);
}

.save-settings {
    width: 100%;
    min-height: 46px;
    border: 0;
    border-radius: 10px;
    background: var(--accent);
    color: var(--bg);
    font-weight: 600;
}

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

* {
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
}

::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 10px;
}

@media(max-width: 760px) {
    .sidebar {
        position: fixed;
        z-index: 50;
        left: 0;
        top: 0;
        transform: translateX(-100%);
        transition: transform .2s ease;
        box-shadow: 10px 0 40px rgba(0,0,0,.2);
    }
    .sidebar.open {
        transform: translateX(0);
    }
    .sidebar-overlay.show {
        display: block;
    }
    .menu-btn, .mobile-close {
        display: grid;
    }
    .messages {
        padding: 20px 12px;
    }
    .composer-wrapper {
        padding: 7px 10px max(7px, env(safe-area-inset-bottom));
    }
    .bubble {
        max-width: 88%;
        font-size: 15px;
    }
    .topbar {
        padding: 0 8px;
    }
    .composer-note {
        display: none;
    }
    .welcome h1 {
        font-size: 28px;
    }
    * {
        scrollbar-width: none;
    }
    *::-webkit-scrollbar {
        display: none;
    }
}

@media(max-width: 400px) {
    .bubble {
        max-width: 94%;
    }
    .theme-options {
        grid-template-columns: 1fr;
    }
}
