@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600&family=Noto+Sans+Thai:wght@300;400;500;600&display=swap');

:root {
    --accent:        #818cf8;
    --accent-dark:   #4f46e5;
    --accent-hover:  #a5b4fc;
    --text-main:     #f1f5f9;
    --text-muted:    #94a3b8;
    --border:        rgba(255,255,255,0.08);
    --bg-msg-user:   rgba(129,140,248,0.12);
    --bg-msg-ai:     rgba(255,255,255,0.05);
    --bg-input:      rgba(0,0,0,0.35);
    --bg-surface:    rgba(15,15,22,0.85);
}

* { margin:0; padding:0; box-sizing:border-box; font-family:'Inter','Noto Sans Thai',sans-serif; }

body {
    background: #06060f;
    background-image:
        radial-gradient(ellipse 80% 60% at 10% 10%,  rgba(59,130,246,0.18) 0%, transparent 60%),
        radial-gradient(ellipse 70% 50% at 90% 90%,  rgba(139,92,246,0.18) 0%, transparent 60%);
    color: var(--text-main);
    height: 100vh;
    overflow: hidden;
}

.background-blobs { display: none; }

.app-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    padding: 16px 0;
}

/* ── Main Card ── */
.chat-main {
    display: flex;
    flex-direction: column;
    width: 95%;
    max-width: 860px;
    height: 96vh;
    /* Solid bg instead of backdrop-filter: blur() — blur resamples every pixel on keypress */
    background: #0e0e1a;
    border: 1px solid var(--border);
    border-radius: 20px;
    overflow: hidden;
    box-shadow: 0 8px 40px rgba(0,0,0,0.5);
    /* Tell browser this subtree is isolated — no need to recalc outside layout */
    contain: layout style paint;
}

/* ── Header ── */
.chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 20px;
    background: rgba(255,255,255,0.03);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.glass-effect { background: rgba(255,255,255,0.03); }

.header-content { display:flex; align-items:center; gap:12px; }

.logo-icon {
    width: 36px; height: 36px;
    background: linear-gradient(135deg, var(--accent), var(--accent-dark));
    border-radius: 10px;
    display: flex; align-items:center; justify-content:center;
    color: #fff; font-size: 1rem; flex-shrink: 0;
    box-shadow: 0 0 12px rgba(129,140,248,0.35);
}

.title-container h2 { font-size: 1rem; font-weight: 600; }

.status-indicator {
    font-size: 0.72rem; color: var(--text-muted);
    display: flex; align-items:center; gap:5px; margin-top:2px;
}

.pulse {
    width: 7px; height: 7px;
    background: #4ade80; border-radius: 50%;
    display: inline-block;
    box-shadow: 0 0 6px #4ade80;
}

.header-settings {
    display: flex;
    gap: 6px;
    align-items: center;
    background: rgba(0,0,0,0.25);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 5px 8px;
}

/* ── Custom Dropdown ── */
.custom-select {
    position: relative;
    display: inline-block;
    font-size: 0.82rem;
    user-select: none;
}
.custom-select-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(255,255,255,0.06);
    color: var(--text-main);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 8px;
    padding: 5px 10px;
    cursor: pointer;
    white-space: nowrap;
    font-size: 0.82rem;
    font-family: inherit;
}
.custom-select-btn:hover { background: rgba(129,140,248,0.12); border-color: var(--accent); }
.custom-select-btn .arrow {
    width: 0; height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 5px solid var(--text-muted);
    margin-left: 2px;
}
.custom-select-menu {
    display: none;
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    min-width: 100%;
    background: #1a1a2e;
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 10px;
    overflow: hidden;
    z-index: 200;
    box-shadow: 0 8px 24px rgba(0,0,0,0.5);
}
.custom-select-menu.open { display: block; }
.custom-select-option {
    padding: 8px 14px;
    cursor: pointer;
    color: var(--text-main);
    font-size: 0.82rem;
    white-space: nowrap;
}
.custom-select-option:hover     { background: rgba(129,140,248,0.15); }
.custom-select-option.selected  { color: var(--accent); font-weight: 500; }

/* Divider */
.header-divider { width:1px; height:20px; background:var(--border); margin:0 2px; }

.icon-btn {
    background: transparent;
    color: var(--text-muted);
    border: none;
    border-radius: 7px;
    width: 30px; height: 30px;
    display: flex; align-items:center; justify-content:center;
    cursor: pointer; font-size: 0.85rem;
}
.icon-btn:hover { background: rgba(129,140,248,0.15); color: var(--accent-hover); }

/* ── Chat Area ── */
.chat-box {
    flex-grow: 1;
    overflow-y: auto;
    padding: 24px 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    scroll-behavior: smooth;
}

.chat-box::-webkit-scrollbar { width: 5px; }
.chat-box::-webkit-scrollbar-track { background: transparent; }
.chat-box::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 8px; }

/* ── Messages ── */
.message {
    display: flex; gap: 10px;
    max-width: 85%; width: fit-content;
    line-height: 1.65;
}
.user-message { flex-direction:row-reverse; align-self:flex-end; }
.ai-message   { align-self:flex-start; }

.avatar {
    width: 30px; height: 30px;
    border-radius: 8px;
    display: flex; align-items:center; justify-content:center;
    font-size: 0.85rem; flex-shrink: 0; margin-top: 4px;
}
.ai-avatar   { background: rgba(255,255,255,0.05); color:var(--accent); border:1px solid rgba(129,140,248,0.25); }
.user-avatar { background: linear-gradient(135deg,var(--accent),var(--accent-dark)); color:#fff; }

.message-content {
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 0.92rem;
    color: var(--text-main);
    line-height: 1.65;
    word-break: break-word;
}
.user-message .message-content {
    background: var(--bg-msg-user);
    border: 1px solid rgba(129,140,248,0.25);
    border-bottom-right-radius: 3px;
}
.ai-message .message-content {
    background: var(--bg-msg-ai);
    border: 1px solid var(--border);
    border-bottom-left-radius: 3px;
}

/* ── Typing ── */
.typing-indicator { display:flex; gap:4px; padding:4px 6px; align-items:center; }
.typing-dot {
    width:6px; height:6px;
    background: var(--accent); border-radius:50%;
    animation: bounce 1.2s infinite ease-in-out both;
}
.typing-dot:nth-child(1) { animation-delay:-0.32s; }
.typing-dot:nth-child(2) { animation-delay:-0.16s; }
@keyframes bounce {
    0%,80%,100% { transform:scale(0.6); opacity:0.4; }
    40%         { transform:scale(1);   opacity:1; }
}

/* ── Input ── */
.input-container {
    padding: 14px 20px;
    background: rgba(255,255,255,0.02);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.input-box {
    display: flex;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 6px 6px 6px 14px;
    align-items: flex-end;
    /* No transition here — transitions trigger style recalc on every keypress */
}
.input-box:focus-within {
    border-color: var(--accent);
    /* Removed box-shadow to reduce paint cost on keypress */
}

textarea {
    flex-grow: 1; border:none; outline:none; resize:none;
    padding: 8px 0; font-size: 0.93rem;
    field-sizing: content; min-height:24px; max-height:130px;
    background: transparent; color: var(--text-main);
    line-height: 1.5; font-family: inherit;
    /* Own compositor layer so typing repaints only this element */
    will-change: transform;
}
textarea::placeholder { color: var(--text-muted); }

#send-btn, #stop-btn {
    background: linear-gradient(135deg,var(--accent),var(--accent-dark));
    color: #fff; border:none; border-radius:10px;
    width:38px; height:38px;
    margin-bottom:3px; margin-left:6px;
    cursor:pointer; font-size:1rem; flex-shrink:0;
    display:flex; align-items:center; justify-content:center;
    transition: opacity 0.2s, transform 0.15s;
    box-shadow: 0 2px 8px rgba(79,70,229,0.4);
}
#stop-btn {
    background: linear-gradient(135deg, #ff7675, #d63031);
    box-shadow: 0 2px 8px rgba(214, 48, 49, 0.4);
}
#send-btn:hover, #stop-btn:hover { opacity:0.9; transform:translateY(-1px); }
#send-btn:disabled { background:#333; box-shadow:none; color:#555; cursor:not-allowed; transform:none; }

.disclaimer { text-align:center; font-size:0.72rem; color:var(--text-muted); margin-top:10px; }

/* ── Markdown ── */
.message-content strong { color:var(--accent-hover); font-weight:600; }
.message-content ul, .message-content ol { margin-left:18px; margin-top:4px; margin-bottom:4px; }
.message-content li   { margin-bottom:3px; }
.message-content p    { margin-bottom:6px; }
.message-content p:last-child { margin-bottom:0; }
.message-content a    { color:var(--accent-hover); }