* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
    --bg: #080810;
    --surface: #10101c;
    --card: #181828;
    --border: #252535;
    --text: #eeeef8;
    --muted: #8888a8;
    --accent: #7c3aed;
    --accent2: #a855f7;
    --grad: linear-gradient(135deg, #7c3aed, #a855f7);
    --user-bg: #1e1e32;
    --ai-bg: #12121e;
    --green: #10b981;
    --red: #ef4444;
    --sidebar: 280px;
    --header: 62px;
    --font-head: 'Syne', sans-serif;
    --font-body: 'DM Sans', sans-serif;
}

[data-theme="light"] {
    --bg: #f0f0f8;
    --surface: #ffffff;
    --card: #e8e8f4;
    --border: #d0d0e0;
    --text: #111124;
    --muted: #606080;
    --user-bg: #e0e0f0;
    --ai-bg: #ffffff;
}

html, body { height: 100%; overflow: hidden; }

body {
    font-family: var(--font-body);
    background: var(--bg);
    color: var(--text);
    transition: background 0.3s, color 0.3s;
}

/* ─── Layout ─── */
.app {
    display: flex;
    height: 100vh;
    height: calc(var(--vh, 1vh) * 100);
    height: 100dvh; /* mobile browser chrome fix */
    position: relative;
}

/* ─── Sidebar ─── */
.sidebar {
    width: var(--sidebar);
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    z-index: 200;
    transition: transform 0.3s cubic-bezier(0.4,0,0.2,1);
    overflow: hidden;
}

.sidebar-inner {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0;
    overflow: hidden;
}

.sidebar-top {
    padding: 20px 16px 16px;
    flex-shrink: 0;
}

.logo {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
}

.logo-icon {
    width: 42px;
    height: 42px;
    background: var(--grad);
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    color: white;
    flex-shrink: 0;
    box-shadow: 0 4px 16px rgba(124,58,237,0.35);
}

.logo-text h1 {
    font-family: var(--font-head);
    font-size: 1.25rem;
    font-weight: 800;
    letter-spacing: -0.03em;
    line-height: 1.1;
}

.logo-text h1 span { color: var(--accent2); }

.logo-text p {
    font-size: 0.68rem;
    color: var(--muted);
    margin-top: 2px;
}

.new-chat-btn {
    width: 100%;
    background: var(--grad);
    border: none;
    padding: 11px 16px;
    border-radius: 40px;
    color: white;
    font-family: var(--font-body);
    font-weight: 600;
    font-size: 0.88rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: transform 0.2s, box-shadow 0.2s;
    box-shadow: 0 4px 14px rgba(124,58,237,0.3);
}

.new-chat-btn:hover { transform: translateY(-2px); box-shadow: 0 8px 20px rgba(124,58,237,0.45); }
.new-chat-btn:active { transform: translateY(0); }

/* ─── History ─── */
.history-label {
    font-family: var(--font-head);
    font-size: 0.65rem;
    font-weight: 700;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--muted);
    padding: 16px 16px 8px;
    flex-shrink: 0;
}

.history-list {
    flex: 1;
    overflow-y: auto;
    padding: 0 8px 8px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.history-list::-webkit-scrollbar { width: 3px; }
.history-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.history-item {
    padding: 9px 12px;
    border-radius: 12px;
    cursor: pointer;
    font-size: 0.82rem;
    color: var(--muted);
    display: flex;
    align-items: center;
    gap: 8px;
    transition: all 0.2s;
    border: 1px solid transparent;
    min-width: 0;
}

.history-item i { flex-shrink: 0; font-size: 0.75rem; }
.history-item span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.history-item:hover { background: var(--card); color: var(--text); }
.history-item.active { background: rgba(124,58,237,0.15); color: var(--accent2); border-color: rgba(124,58,237,0.3); }

.history-item .del-btn {
    margin-left: auto;
    background: none;
    border: none;
    color: var(--muted);
    font-size: 0.7rem;
    cursor: pointer;
    padding: 2px 4px;
    border-radius: 4px;
    opacity: 0;
    transition: opacity 0.2s, color 0.2s;
    flex-shrink: 0;
}

.history-item:hover .del-btn { opacity: 1; }
.history-item .del-btn:hover { color: var(--red); }

.history-empty {
    text-align: center;
    padding: 20px 12px;
    font-size: 0.78rem;
    color: var(--muted);
}

/* ─── Sidebar Bottom ─── */
.sidebar-bottom {
    padding: 12px 16px 16px;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 8px;
    flex-shrink: 0;
}

.sidebar-pill {
    padding: 8px 12px;
    border-radius: 30px;
    font-size: 0.78rem;
    background: var(--card);
    border: 1px solid var(--border);
    color: var(--muted);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    cursor: pointer;
    transition: all 0.2s;
}

.sidebar-pill:hover { background: var(--accent); color: white; border-color: var(--accent); }

.footer-note {
    text-align: center;
    font-size: 0.66rem;
    color: var(--muted);
}

/* ─── Main ─── */
.main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    overflow: hidden;
    background: var(--bg);
}

/* ─── Header ─── */
.header {
    height: var(--header);
    padding: 0 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    background: var(--surface);
    gap: 12px;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.menu-btn {
    display: none;
    background: none;
    border: none;
    color: var(--text);
    font-size: 1.1rem;
    cursor: pointer;
    padding: 8px;
    border-radius: 10px;
    transition: background 0.2s;
    flex-shrink: 0;
}

.menu-btn:hover { background: var(--card); }

.header-status {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.82rem;
    color: var(--muted);
    min-width: 0;
}

.status-dot {
    width: 8px;
    height: 8px;
    background: var(--green);
    border-radius: 50%;
    flex-shrink: 0;
    animation: pulse 2s infinite;
}

.status-dot.recording { background: var(--red); animation: pulse-red 0.8s infinite; }

@keyframes pulse { 0%,100% { opacity:1; transform:scale(1); } 50% { opacity:0.5; transform:scale(1.3); } }
@keyframes pulse-red { 0%,100% { opacity:1; } 50% { opacity:0.4; } }

.header-actions {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
}

.icon-btn {
    background: none;
    border: none;
    color: var(--muted);
    font-size: 1rem;
    cursor: pointer;
    padding: 8px 10px;
    border-radius: 10px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    position: relative;
}

.icon-btn:hover { background: var(--card); color: var(--accent2); }
.icon-btn.active { color: var(--accent2); background: rgba(124,58,237,0.15); }

.icon-btn .tooltip {
    position: absolute;
    bottom: calc(100% + 8px);
    left: 50%;
    transform: translateX(-50%);
    background: var(--card);
    border: 1px solid var(--border);
    color: var(--text);
    font-size: 0.72rem;
    padding: 4px 8px;
    border-radius: 6px;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    z-index: 10;
}

.icon-btn:hover .tooltip { opacity: 1; }

/* ─── Messages ─── */
.messages {
    flex: 1;
    overflow-y: auto;
    padding: 24px 20px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    scroll-behavior: smooth;
}

.messages::-webkit-scrollbar { width: 4px; }
.messages::-webkit-scrollbar-track { background: transparent; }
.messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; }

/* ─── Welcome ─── */
.welcome {
    text-align: center;
    max-width: 560px;
    margin: auto;
    padding: 40px 20px;
    width: 100%;
}

.welcome-icon {
    font-size: clamp(44px, 10vw, 60px);
    background: var(--grad);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 16px;
    display: block;
}

.welcome h2 {
    font-family: var(--font-head);
    font-size: clamp(1.3rem, 5vw, 2rem);
    font-weight: 800;
    letter-spacing: -0.03em;
    margin-bottom: 10px;
}

.welcome p {
    color: var(--muted);
    font-size: clamp(0.85rem, 3.5vw, 0.95rem);
    line-height: 1.6;
    margin-bottom: 28px;
}

.chips {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
}

.chip {
    background: var(--card);
    border: 1px solid var(--border);
    padding: 9px 16px;
    border-radius: 40px;
    font-size: 0.82rem;
    cursor: pointer;
    transition: all 0.2s;
    color: var(--text);
    font-family: var(--font-body);
}

.chip:hover { background: var(--accent); border-color: var(--accent); color: white; transform: translateY(-2px); }

/* ─── Message Bubbles ─── */
.msg {
    display: flex;
    gap: 12px;
    max-width: 82%;
    animation: fadeUp 0.25s ease;
}

@keyframes fadeUp { from { opacity:0; transform:translateY(8px); } to { opacity:1; transform:translateY(0); } }

.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.msg.ai { align-self: flex-start; }

.avatar {
    width: 34px;
    height: 34px;
    border-radius: 11px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 15px;
    flex-shrink: 0;
}

.msg.user .avatar { background: var(--grad); color: white; }
.msg.ai .avatar { background: var(--card); color: var(--accent2); border: 1px solid var(--border); }

.msg-body { display: flex; flex-direction: column; gap: 4px; min-width: 0; }

.msg-time {
    font-size: 0.65rem;
    color: var(--muted);
    padding: 0 4px;
}

.msg.user .msg-time { text-align: right; }

.msg-content {
    background: var(--user-bg);
    padding: 11px 16px;
    border-radius: 18px;
    line-height: 1.55;
    font-size: 0.9rem;
    border: 1px solid var(--border);
    word-break: break-word;
}

.msg.ai .msg-content { background: var(--ai-bg); border-radius: 4px 18px 18px 18px; }
.msg.user .msg-content { border-radius: 18px 4px 18px 18px; }

.msg-content strong { color: var(--accent2); }
.msg-content em { color: var(--muted); }

.msg-content code {
    background: rgba(124,58,237,0.12);
    border: 1px solid rgba(124,58,237,0.2);
    padding: 1px 6px;
    border-radius: 5px;
    font-size: 0.85em;
    font-family: 'Fira Code', monospace;
}

.msg-content pre {
    background: #0a0a14;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 14px;
    overflow-x: auto;
    margin: 8px 0;
    font-size: 0.82rem;
}

.msg-content pre code {
    background: none;
    border: none;
    padding: 0;
    font-size: inherit;
}

/* ─── Message Actions ─── */
.msg-actions {
    display: flex;
    gap: 4px;
    opacity: 0;
    transition: opacity 0.2s;
}

.msg:hover .msg-actions { opacity: 1; }

.msg-action-btn {
    background: var(--card);
    border: 1px solid var(--border);
    color: var(--muted);
    font-size: 0.7rem;
    padding: 3px 8px;
    border-radius: 20px;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 4px;
}

.msg-action-btn:hover { background: var(--accent); color: white; border-color: var(--accent); }

/* ─── Typing Indicator ─── */
.typing {
    display: flex;
    gap: 5px;
    align-items: center;
    padding: 14px 18px;
}

.typing span {
    width: 7px;
    height: 7px;
    background: var(--accent2);
    border-radius: 50%;
    animation: bounce 1.2s infinite;
}

.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce { 0%,60%,100% { transform:translateY(0); } 30% { transform:translateY(-8px); } }

/* ─── Input Area ─── */
.input-area {
    padding: 12px 20px 16px;
    border-top: 1px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
}

.input-wrap {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--card);
    border: 1.5px solid var(--border);
    border-radius: 20px;
    padding: 6px 6px 6px 16px;
    transition: border-color 0.2s;
}

.input-wrap:focus-within { border-color: var(--accent); }

textarea {
    flex: 1;
    background: none;
    border: none;
    outline: none;
    color: var(--text);
    font-family: var(--font-body);
    font-size: 0.92rem;
    resize: none;
    padding: 8px 0;
    max-height: 120px;
    line-height: 1.5;
}

textarea::placeholder { color: var(--muted); }

.input-btns {
    display: flex;
    gap: 4px;
    align-items: flex-end;
    flex-shrink: 0;
}

.voice-btn {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    background: var(--card);
    border: 1px solid var(--border);
    color: var(--muted);
    font-size: 1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.voice-btn:hover { background: rgba(124,58,237,0.15); color: var(--accent2); border-color: var(--accent); }

.voice-btn.recording {
    background: var(--red);
    color: white;
    border-color: var(--red);
    animation: pulse-ring 1s infinite;
}

@keyframes pulse-ring {
    0% { box-shadow: 0 0 0 0 rgba(239,68,68,0.4); }
    100% { box-shadow: 0 0 0 10px rgba(239,68,68,0); }
}

.send-btn {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    background: var(--grad);
    border: none;
    color: white;
    font-size: 1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
    box-shadow: 0 2px 10px rgba(124,58,237,0.3);
}

.send-btn:hover { transform: scale(1.05); box-shadow: 0 4px 16px rgba(124,58,237,0.5); }
.send-btn:active { transform: scale(0.97); }
.send-btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.input-footer {
    display: flex;
    justify-content: space-between;
    padding: 6px 4px 0;
    font-size: 0.68rem;
    color: var(--muted);
}

/* ─── Voice & TTS Banners ─── */
.voice-banner {
    display: none;
    align-items: center;
    gap: 8px;
    background: rgba(239,68,68,0.1);
    border: 1px solid rgba(239,68,68,0.3);
    border-radius: 10px;
    padding: 8px 14px;
    margin-bottom: 8px;
    font-size: 0.8rem;
    color: var(--red);
    animation: fadeUp 0.2s ease;
}

.voice-banner.show { display: flex; }
.voice-banner i { animation: pulse-red 0.8s infinite; }

.tts-banner {
    display: none;
    align-items: center;
    gap: 8px;
    background: rgba(124,58,237,0.1);
    border: 1px solid rgba(124,58,237,0.3);
    border-radius: 10px;
    padding: 8px 14px;
    margin-bottom: 8px;
    font-size: 0.8rem;
    color: var(--accent2);
}

.tts-banner.show { display: flex; }

.tts-banner button {
    margin-left: auto;
    background: none;
    border: none;
    color: var(--accent2);
    cursor: pointer;
    font-size: 0.8rem;
}

/* ─── TTS Wave ─── */
.wave {
    display: inline-flex;
    gap: 2px;
    align-items: center;
}

.wave span {
    width: 3px;
    height: 12px;
    background: var(--accent2);
    border-radius: 2px;
    animation: wave 0.8s infinite ease-in-out;
}

.wave span:nth-child(2) { animation-delay: 0.15s; }
.wave span:nth-child(3) { animation-delay: 0.3s; }
.wave span:nth-child(4) { animation-delay: 0.45s; }

@keyframes wave { 0%,100% { height:4px; } 50% { height:14px; } }

/* ─── Overlay ─── */
.overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.55);
    z-index: 150;
    backdrop-filter: blur(3px);
}

/* ─── Responsive ─── */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100%;
        transform: translateX(-100%);
        box-shadow: 4px 0 24px rgba(0,0,0,0.4);
    }

    .sidebar.open { transform: translateX(0); }
    .overlay.show { display: block; }
    .menu-btn { display: flex; }

    .msg { max-width: 92%; }
    .messages { padding: 16px 14px; gap: 12px; }
    .header { padding: 0 14px; }
    .input-area { padding: 10px 14px 14px; }
}

@media (max-width: 480px) {
    :root { --sidebar: 85vw; }

    .msg { max-width: 96%; gap: 8px; }
    .avatar { width: 28px; height: 28px; font-size: 12px; border-radius: 9px; }
    .msg-content { padding: 9px 13px; font-size: 0.85rem; }
    .messages { padding: 12px 10px; gap: 10px; }
    .header { padding: 0 12px; }
    .header-status span:last-child { display: none; }
    .input-area { padding: 8px 10px 12px; }
    textarea { font-size: 16px; }
    .voice-btn, .send-btn { width: 36px; height: 36px; font-size: 0.9rem; }
    .chips { gap: 8px; }
    .chip { font-size: 0.78rem; padding: 7px 13px; }
    .welcome { padding: 24px 12px; }
}

@media (max-width: 360px) {
    .msg-content { font-size: 0.82rem; }
    .icon-btn .tooltip { display: none; }
}

@media (max-height: 600px) {
    .welcome { padding: 16px 12px; }
    .welcome-icon { font-size: 36px; margin-bottom: 10px; }
    .messages { padding: 10px 14px; gap: 8px; }
}

@media (hover: none) and (pointer: coarse) {
    textarea { font-size: 16px; }
    .chip:active, .new-chat-btn:active { transform: scale(0.97); }
    .icon-btn .tooltip { display: none; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}

@media print {
    .sidebar, .input-area, .menu-btn, .header-actions { display: none; }
    .main { width: 100%; background: white; }
    .msg { break-inside: avoid; }
    .msg-content { border: 1px solid #ccc; background: white; color: black; }
}