:root {
    --bg: #0b0f14;
    --sidebar: #0d1117;
    --panel: #111827;
    --panel-2: #0d1117;
    --border: #1f2937;
    --border-2: #30363d;
    --text: #e6edf3;
    --muted: #8b949e;
    --subtle: #6e7681;
    --accent: #58a6ff;
    --accent-soft: rgba(88,166,255,.12);
    --green: #238636;
    --green-hover: #2ea043;
    --bubble-user: #1f6feb;
    --bubble-user-border: #388bfd;
    --bubble-ai: #161b22;
    --bubble-ai-border: #30363d;
    --red: #f85149;
    --amber: #d29922;
    --radius: 16px;
    --shadow: 0 12px 32px rgba(0,0,0,.35);
    --shadow-lg: 0 24px 64px rgba(0,0,0,.5);
    --menu-bg: #1c2128;
}
[data-theme="light"] {
    --bg: #f6f8fa;
    --sidebar: #ffffff;
    --panel: #ffffff;
    --panel-2: #f6f8fa;
    --border: #e1e4e8;
    --border-2: #d0d7de;
    --text: #1f2328;
    --muted: #57606a;
    --subtle: #6e7781;
    --accent: #0969da;
    --accent-soft: rgba(9,105,218,.1);
    --green: #1a7f37;
    --green-hover: #1f883d;
    --bubble-user: #0969da;
    --bubble-user-border: #0860ca;
    --bubble-ai: #f6f8fa;
    --bubble-ai-border: #d0d7de;
    --red: #cf222e;
    --amber: #9a6700;
    --shadow: 0 10px 28px rgba(31,35,40,.1);
    --shadow-lg: 0 24px 64px rgba(31,35,40,.18);
    --menu-bg: #ffffff;
}

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

/* ===================== ROOT / BODY ===================== */
html, body {
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
    -webkit-tap-highlight-color: transparent;
}
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
}
a { color: var(--accent); text-decoration: none; }
button { font-family: inherit; cursor: pointer; }
[hidden] { display: none !important; }

/* ===================== APP SHELL ===================== */
.app {
    display: flex;
    height: 100vh;
    height: 100dvh;
    width: 100%;
    overflow: hidden;
}

/* ===================== SIDEBAR ===================== */
.sidebar {
    width: 288px; flex-shrink: 0;
    background: var(--sidebar);
    border-right: 1px solid var(--border);
    display: flex; flex-direction: column;
    padding: 14px 10px;
    padding-top: max(14px, env(safe-area-inset-top));
    padding-bottom: max(14px, env(safe-area-inset-bottom));
    height: 100vh; height: 100dvh;
    overflow: hidden;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 6px 14px; flex-shrink: 0; }
.brand-mark {
    width: 36px; height: 36px; border-radius: 10px;
    background: linear-gradient(135deg, var(--accent), var(--green));
    color: #fff; font-weight: 700;
    display: flex; align-items: center; justify-content: center;
    font-size: 18px;
}
.brand-title { font-size: 14px; font-weight: 600; }
.brand-sub { font-size: 11px; color: var(--muted); }
.new-chat-form { margin-bottom: 10px; }
.new-chat {
    display: flex; align-items: center; justify-content: center; gap: 8px;
    width: 100%; padding: 10px 12px;
    background: var(--panel-2); color: var(--text);
    border: 1px solid var(--border-2); border-radius: 10px;
    font-size: 13px; transition: all .15s;
}
.new-chat:hover { background: var(--border); border-color: var(--accent); }
.search-wrap { position: relative; margin-bottom: 10px; }
.search-wrap svg {
    position: absolute; left: 10px; top: 50%;
    transform: translateY(-50%);
    width: 14px; height: 14px; stroke: var(--muted);
}
.search-input {
    width: 100%; padding: 9px 10px 9px 32px;
    background: var(--panel-2); color: var(--text);
    border: 1px solid var(--border); border-radius: 9px;
    font-size: 12.5px; outline: none;
}
.search-input:focus { border-color: var(--accent); }
.chat-list {
    flex: 1; overflow-y: auto;
    display: flex; flex-direction: column; gap: 2px;
    min-height: 0;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    scrollbar-color: var(--border-2) transparent;
}
.chat-list::-webkit-scrollbar { width: 4px; }
.chat-list::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 4px; }
.chat-item {
    display: flex; align-items: center; gap: 8px;
    padding: 9px 8px; border-radius: 9px;
    color: var(--muted); font-size: 13px;
    cursor: pointer; border: 1px solid transparent;
    width: 100%; background: transparent; text-align: left;
    transition: background .12s, color .12s;
}
.chat-item:hover { background: var(--panel-2); color: var(--text); }
.chat-item.active { background: var(--accent-soft); color: var(--accent); }
.chat-item .chat-icon { width: 14px; height: 14px; stroke: currentColor; fill: none; stroke-width: 1.8; flex-shrink: 0; }
.chat-item .chat-title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .chat-menu-btn {
    background: transparent; border: none; color: var(--muted);
    padding: 2px 6px; border-radius: 5px;
    font-size: 16px; line-height: 1;
    opacity: 0; transition: opacity .12s, background .12s;
}
.chat-item:hover .chat-menu-btn,
.chat-item.active .chat-menu-btn { opacity: 1; }
.chat-item .chat-menu-btn:hover { background: var(--border); color: var(--text); }
.side-foot { border-top: 1px solid var(--border); padding-top: 10px; flex-shrink: 0; }
.user-chip { display: flex; align-items: center; gap: 10px; padding: 6px; border-radius: 10px; }
.avatar {
    width: 34px; height: 34px; border-radius: 50%;
    background: linear-gradient(135deg, #1f6feb, #238636);
    color: #fff; display: flex; align-items: center; justify-content: center;
    font-weight: 600; font-size: 15px;
}
.user-meta { min-width: 0; flex: 1; }
.user-name { font-size: 13px; font-weight: 600; }
.user-handle { font-size: 11px; color: var(--muted); }
.token-info { font-size: 11px; color: var(--muted); padding: 4px 8px; line-height: 1.4; }

/* ===================== MAIN COLUMN ===================== */
.main {
    flex: 1;
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

/* ---------- Topbar ---------- */
.topbar {
    display: flex; align-items: center; justify-content: space-between;
    padding: 12px 22px;
    padding-top: max(12px, env(safe-area-inset-top));
    padding-left: max(22px, env(safe-area-inset-left));
    padding-right: max(22px, env(safe-area-inset-right));
    border-bottom: 1px solid var(--border);
    background: var(--panel);
    z-index: 30;
    gap: 12px;
    flex-shrink: 0;
    position: relative;
}
.topbar-title { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
.topbar h1 {
    font-size: 14.5px; font-weight: 600;
    display: flex; align-items: center; gap: 8px; min-width: 0;
}
.status-dot {
    width: 8px; height: 8px; border-radius: 50%;
    background: #2ea043; display: inline-block; flex-shrink: 0;
    box-shadow: 0 0 0 3px rgba(46,160,67,.18);
}
.conv-title-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 45vw; }
.model-badge {
    font-size: 10.5px; font-weight: 600; padding: 2px 8px;
    border-radius: 99px; background: var(--accent-soft);
    color: var(--accent); border: 1px solid var(--accent);
    flex-shrink: 0;
}
.topbar-actions { display: flex; gap: 8px; align-items: center; flex-shrink: 0; }
.btn-icon {
    width: 34px; height: 34px;
    padding: 0 !important;
    display: inline-flex; align-items: center; justify-content: center;
    font-size: 15px;
}

/* ---------- Chat scroll area (KEY FIX) ---------- */
.chat-scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    position: relative;
    display: flex;
    flex-direction: column;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    scrollbar-color: var(--border-2) transparent;
}
.chat-scroll::-webkit-scrollbar { width: 6px; }
.chat-scroll::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 6px; }
.chat-inner {
    max-width: 840px; width: 100%; margin: 0 auto;
    padding: 24px 22px;
    display: flex; flex-direction: column; gap: 16px;
    min-height: 0;
}
.day-divider {
    display: flex; align-items: center; gap: 12px;
    font-size: 11px; color: var(--subtle);
    text-transform: uppercase; margin: 6px 0;
}
.day-divider::before, .day-divider::after { content: ''; flex: 1; height: 1px; background: var(--border); }
.msg { display: flex; gap: 10px; align-items: flex-start; animation: fadeIn .25s ease-out; min-width: 0; }
.msg.user { flex-direction: row-reverse; }
.msg-avatar {
    width: 30px; height: 30px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 12px; font-weight: 600; color: #fff; flex-shrink: 0;
}
.msg.user .msg-avatar { background: linear-gradient(135deg, #1f6feb, #0969da); }
.msg.assistant .msg-avatar { background: linear-gradient(135deg, #238636, #2ea043); }
.msg-body { display: flex; flex-direction: column; max-width: 76%; min-width: 0; }
.msg.user .msg-body { align-items: flex-end; }
.bubble {
    padding: 11px 15px; border-radius: 16px;
    font-size: 14px; line-height: 1.55;
    word-wrap: break-word; overflow-wrap: anywhere;
    border: 1px solid;
    transition: box-shadow .15s;
    max-width: 100%;
}
.msg.user .bubble {
    background: var(--bubble-user);
    border-color: var(--bubble-user-border);
    color: #fff;
    border-bottom-right-radius: 6px;
}
.msg.assistant .bubble {
    background: var(--bubble-ai);
    border-color: var(--bubble-ai-border);
    color: var(--text);
    border-bottom-left-radius: 6px;
}
.bubble:hover { box-shadow: var(--shadow); }

/* Media */
.media-container {
    margin-top: 8px;
    max-width: 100%;
    min-width: 0;
    position: relative;
    border-radius: 12px;
    overflow: hidden;
    border: 1px solid var(--border);
    background: var(--panel-2);
}
.media-container img, .media-container video {
    max-width: 100%;
    max-height: 70vh;
    max-height: 70dvh;
    height: auto;
    object-fit: contain;
    border-radius: 0;
    display: block;
}
.media-blur { filter: blur(24px); opacity: .55; pointer-events: none; transform: scale(1.05); }
.media-label {
    position: absolute; bottom: 12px; left: 50%;
    transform: translateX(-50%);
    background: rgba(0,0,0,.75); color: #fff;
    padding: 6px 14px; border-radius: 99px;
    font-size: 12px; font-weight: 500; z-index: 2; pointer-events: none;
}

.msg-meta {
    display: flex; align-items: center; gap: 8px;
    margin-top: 4px; font-size: 11px; color: var(--subtle);
    opacity: 0; transition: opacity .15s;
}
.msg:hover .msg-meta { opacity: 1; }
.meta-btn {
    background: transparent; border: none; color: var(--subtle);
    font-size: 11px; display: inline-flex; align-items: center; gap: 4px;
    padding: 2px 6px; border-radius: 5px;
}
.meta-btn:hover { background: var(--panel-2); color: var(--text); }
.meta-btn.danger:hover { color: var(--red); background: rgba(248,81,73,.1); }

/* ---------- Empty ---------- */
.empty {
    display: flex; flex-direction: column; align-items: center;
    justify-content: center; text-align: center;
    min-height: 55vh; padding: 20px 0; gap: 6px;
}
.empty-logo {
    width: 64px; height: 64px; border-radius: 20px;
    background: linear-gradient(135deg, var(--accent), var(--green));
    color: #fff; display: flex; align-items: center; justify-content: center;
    font-size: 30px; margin-bottom: 16px;
    box-shadow: 0 16px 40px rgba(35,134,54,.3);
}
.empty-title { font-size: 24px; font-weight: 700; }
.empty-sub { font-size: 14px; color: var(--muted); max-width: 440px; }

/* ---------- Suggestions ---------- */
.suggestions {
    display: grid; grid-template-columns: repeat(2, 1fr);
    gap: 10px; max-width: 620px; width: 100%; margin-top: 16px;
}
.suggest {
    display: flex; flex-direction: column; gap: 4px;
    padding: 14px 16px;
    border-radius: 12px;
    background: var(--panel-2);
    border: 1px solid var(--border);
    text-align: left; font-size: 13px; color: var(--text);
    transition: border-color .15s, transform .15s, background .15s;
    cursor: pointer;
}
.suggest:hover { border-color: var(--accent); transform: translateY(-2px); background: var(--panel); }
.suggest-icon { font-size: 20px; margin-bottom: 2px; }
.suggest-title { font-weight: 600; font-size: 13px; }
.suggest-sub { font-size: 12px; color: var(--muted); }

/* ---------- Composer ---------- */
.composer-wrap {
    flex-shrink: 0;
    padding: 10px 22px;
    padding-bottom: max(14px, env(safe-area-inset-bottom));
    padding-left: max(22px, env(safe-area-inset-left));
    padding-right: max(22px, env(safe-area-inset-right));
}
.composer {
    max-width: 840px; margin: 0 auto;
    display: flex; align-items: flex-end; gap: 6px;
    background: var(--panel);
    border: 1px solid var(--border-2);
    border-radius: 18px;
    padding: 6px;
    flex-wrap: wrap;
    box-shadow: var(--shadow);
    transition: border-color .15s, box-shadow .15s;
}
.composer:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}
.attach-btn {
    width: 34px; height: 34px; border-radius: 10px;
    background: transparent; border: none; color: var(--muted);
    display: flex; align-items: center; justify-content: center;
    transition: background .15s, color .15s;
}
.attach-btn:hover { background: var(--panel-2); color: var(--text); }
.attach-btn svg { width: 16px; height: 16px; }
.composer textarea {
    flex: 1; resize: none; border: none; outline: none;
    background: transparent; color: var(--text);
    font-family: inherit; font-size: 14px; line-height: 1.5;
    padding: 8px 4px;
    max-height: 200px; min-height: 24px;
}
.video-select {
    width: 70px;
    background: var(--panel-2); color: var(--text);
    border: 1px solid var(--border-2); border-radius: 8px;
    padding: 5px; font-size: 12px;
}
.send-btn {
    width: 36px; height: 36px; border-radius: 10px;
    background: var(--green); border: none; color: #fff;
    display: flex; align-items: center; justify-content: center;
    transition: background .15s, transform .1s;
    flex-shrink: 0;
}
.send-btn:hover { background: var(--green-hover); }
.send-btn:disabled { opacity: .5; cursor: not-allowed; }
.send-btn svg { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 2.2; }
.attach-preview {
    display: flex; align-items: center; gap: 8px;
    width: 100%; margin-bottom: 6px;
    padding: 9px 12px;
    background: var(--accent-soft);
    border: 1px solid rgba(88,166,255,.35);
    border-radius: 10px; font-size: 12px;
}
.attach-preview .ap-remove {
    background: transparent; border: none;
    color: var(--muted); font-size: 18px;
    margin-left: auto; cursor: pointer; line-height: 1;
}
.composer-meta {
    max-width: 840px; margin: 6px auto 0;
    display: flex; justify-content: space-between;
    font-size: 11px; color: var(--subtle);
}
.count.warn { color: var(--amber); }
.count.over { color: var(--red); }

/* ---------- Type tabs ---------- */
.type-tabs {
    display: flex; gap: 8px;
    margin: 12px auto;
    max-width: 840px;
    padding: 0 22px;
    padding-left: max(22px, env(safe-area-inset-left));
    padding-right: max(22px, env(safe-area-inset-right));
    flex-shrink: 0;
}
.type-tab {
    flex: 1; padding: 9px 0;
    border-radius: 10px;
    border: 1px solid var(--border-2);
    background: var(--panel-2);
    color: var(--muted);
    font-size: 13px; text-align: center;
    transition: all .15s;
}
.type-tab:hover { border-color: var(--accent); }
.type-tab.active {
    background: var(--accent-soft);
    color: var(--accent);
    border-color: var(--accent);
    font-weight: 500;
}

/* ---------- Thinking ---------- */
.thinking-dots { display: inline-flex; gap: 4px; align-items: center; }
.thinking-dots span {
    width: 6px; height: 6px; border-radius: 50%;
    background: var(--muted);
    animation: thinking 1.2s infinite;
}
.thinking-dots span:nth-child(2) { animation-delay: .2s; }
.thinking-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes thinking {
    0%, 80%, 100% { transform: scale(.6); opacity: .4; }
    40% { transform: scale(1); opacity: 1; }
}

/* ---------- Chat menu popover ---------- */
.chat-menu-popover {
    background: var(--menu-bg);
    border: 1px solid var(--border-2);
    border-radius: 10px;
    padding: 4px;
    min-width: 170px;
    box-shadow: 0 12px 32px rgba(0,0,0,.4);
    display: flex; flex-direction: column; gap: 2px;
    animation: menuIn .12s ease-out;
    z-index: 300;
}
@keyframes menuIn {
    from { opacity: 0; transform: translateY(-4px); }
    to   { opacity: 1; transform: translateY(0); }
}
.cm-item {
    display: flex; align-items: center; gap: 8px;
    padding: 8px 10px;
    border: none; background: transparent;
    color: var(--text); font-size: 13px;
    border-radius: 7px; text-align: left;
    cursor: pointer; width: 100%;
}
.cm-item:hover { background: var(--panel-2); }
.cm-item.danger { color: var(--red); }
.cm-item.danger:hover { background: rgba(248,81,73,.12); }
.cm-item svg { flex-shrink: 0; }

/* ---------- Modal ---------- */
.modal-overlay {
    position: fixed; inset: 0;
    background: rgba(0,0,0,.6);
    display: flex; align-items: center; justify-content: center;
    z-index: 400;
    backdrop-filter: blur(4px);
    opacity: 0; transition: opacity .2s;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.modal-overlay.show { opacity: 1; }
.modal-overlay .modal {
    background: var(--panel);
    border: 1px solid var(--border-2);
    border-radius: 18px;
    padding: 24px;
    max-width: 420px; width: 90%;
    box-shadow: var(--shadow-lg);
    transform: scale(.95); transition: transform .2s;
}
.modal-overlay.show .modal { transform: scale(1); }
.modal h2 { font-size: 18px; margin-bottom: 10px; }
.modal-text { color: var(--muted); margin-bottom: 20px; line-height: 1.5; font-size: 14px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }
.modal .form-group { margin-bottom: 16px; }
.modal .input {
    width: 100%; padding: 11px 14px;
    border-radius: 10px; background: var(--panel-2);
    color: var(--text); border: 1px solid var(--border-2);
    font-size: 14px; outline: none;
}
.modal .input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* ---------- Toast ---------- */
.toast-host {
    position: fixed;
    bottom: max(24px, env(safe-area-inset-bottom));
    right: max(24px, env(safe-area-inset-right));
    z-index: 500;
    display: flex; flex-direction: column; gap: 10px;
    pointer-events: none;
}
.toast {
    background: var(--panel);
    border: 1px solid var(--border-2);
    color: var(--text);
    padding: 12px 18px;
    border-radius: 10px;
    font-size: 13px;
    box-shadow: var(--shadow-lg);
    transform: translateY(20px); opacity: 0;
    transition: transform .25s, opacity .25s;
    max-width: 320px;
}
.toast.show { transform: translateY(0); opacity: 1; }
.toast-error   { border-color: var(--red); }
.toast-success { border-color: var(--green); }
.toast-info    { border-color: var(--accent); }

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ===================== RESPONSIVE ===================== */
@media (max-width: 900px) {
    .sidebar {
        position: fixed;
        left: -320px;
        top: 0;
        z-index: 60;
        transition: left .22s ease;
        box-shadow: none;
        width: 288px;
        max-width: 85vw;
        height: 100vh;
        height: 100dvh;
    }
    .sidebar.open { left: 0; box-shadow: 4px 0 24px rgba(0,0,0,.45); }

    .topbar {
        padding: 10px 12px;
        padding-top: max(10px, env(safe-area-inset-top));
        padding-left: max(12px, env(safe-area-inset-left));
        padding-right: max(12px, env(safe-area-inset-right));
        gap: 8px;
    }
    .topbar-title { gap: 8px; min-width: 0; flex: 1; }

    /* Menu button must NEVER shrink or be pushed out */
    .topbar-title .menu-btn {
        flex: 0 0 auto !important;
        flex-shrink: 0 !important;
        min-width: 34px;
        width: 34px;
        height: 34px;
        padding: 0 !important;
        display: inline-flex !important;
        align-items: center;
        justify-content: center;
    }

    .topbar h1 { min-width: 0; flex: 1; gap: 6px; }
    .status-dot { flex-shrink: 0; }
    .model-badge { flex-shrink: 0; }
    .conv-title-text {
        max-width: none;
        min-width: 0;
        flex: 1 1 auto;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .topbar-actions { flex-shrink: 0; gap: 6px; }
    .topbar-actions .btn-icon { width: 32px; height: 32px; }

    .chat-inner { padding: 14px; padding-top: 18px; }
    .msg-body { max-width: 85%; }
    .suggestions { grid-template-columns: 1fr; }
    .type-tabs {
        margin: 8px 12px;
        padding: 0 12px;
        padding-left: max(12px, env(safe-area-inset-left));
        padding-right: max(12px, env(safe-area-inset-right));
    }
    .composer-wrap {
        padding: 8px 12px;
        padding-bottom: max(12px, env(safe-area-inset-bottom));
        padding-left: max(12px, env(safe-area-inset-left));
        padding-right: max(12px, env(safe-area-inset-right));
    }
    .brand-title { font-size: 16px; }
}

/* Phones: hide duplicate Account button (exists in sidebar) */
@media (max-width: 640px) {
    .topbar-actions .btn.btn-sm:not(.btn-icon):not(.btn-primary) {
        display: none;
    }
}

/* Small phones: hide model badge + compact upgrade */
@media (max-width: 420px) {
    .model-badge { display: none; }
    .topbar h1 { font-size: 13.5px; gap: 5px; }
    .topbar-actions .btn-primary {
        padding: 5px 9px;
        font-size: 12px;
    }
    .topbar-actions .btn-icon { width: 30px; height: 30px; }
    .topbar-title .menu-btn { width: 32px; height: 32px; min-width: 32px; }
}