/* ===== Reset & Base ===== */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
    --sidebar-w: 280px;
    --bg-primary: #0f172a;
    --bg-secondary: #1e293b;
    --bg-tertiary: #111827;
    --bg-card: #1a2332;
    --border: #334155;
    --border-light: #475569;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --accent: #6366f1;
    --accent-hover: #818cf8;
    --accent-bg: rgba(99, 102, 241, 0.12);
    --green: #22c55e;
    --green-bg: rgba(34, 197, 94, 0.12);
    --amber: #f59e0b;
    --red: #ef4444;
    --shadow: 0 1px 3px rgba(0,0,0,0.3);
    --radius: 10px;
    --radius-sm: 6px;
    --radius-lg: 14px;
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans SC", sans-serif;
    --font-mono: 'JetBrains Mono', 'Consolas', monospace;
}

html, body { height: 100%; }
body {
    font-family: var(--font);
    background: var(--bg-primary);
    color: var(--text-primary);
    overflow: hidden;
}

/* ===== App Layout ===== */
#app {
    display: flex;
    height: 100vh;
    width: 100vw;
}

/* ===== Sidebar ===== */
#sidebar {
    width: var(--sidebar-w);
    min-width: var(--sidebar-w);
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease;
    z-index: 100;
}
.sidebar-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 18px 16px;
    border-bottom: 1px solid var(--border);
}
.sidebar-header .logo {
    width: 36px; height: 36px;
    background: linear-gradient(135deg, var(--accent), var(--accent-hover));
    border-radius: 10px;
    display: flex; align-items: center; justify-content: center;
    font-size: 18px;
    flex-shrink: 0;
}
.sidebar-header h2 { font-size: 15px; font-weight: 600; color: var(--text-primary); }

/* Session List */
#session-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
}
.session-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 16px;
    cursor: pointer;
    color: var(--text-secondary);
    font-size: 13px;
    transition: all 0.15s;
    border-left: 3px solid transparent;
}
.session-item:hover {
    background: var(--accent-bg);
    color: var(--text-primary);
}
.session-item.active {
    background: var(--accent-bg);
    color: var(--text-primary);
    border-left-color: var(--accent);
}
.session-item .s-icon { font-size: 14px; opacity: 0.6; flex-shrink: 0; }
.session-item .s-title {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-footer {
    padding: 12px 16px;
    border-top: 1px solid var(--border);
}
.btn-new-session {
    width: 100%;
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    border: 1px dashed var(--border-light);
    background: transparent;
    color: var(--text-secondary);
    font-size: 13px;
    cursor: pointer;
    transition: all 0.15s;
    display: flex; align-items: center; gap: 6px;
    justify-content: center;
}
.btn-new-session:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: var(--accent-bg);
}

/* ===== Main Area ===== */
#main-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--bg-primary);
}

/* ===== Header ===== */
.chat-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 20px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-tertiary);
}
.menu-btn {
    display: none;
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 20px;
    cursor: pointer;
    padding: 4px;
}
.chat-header h1 { font-size: 16px; font-weight: 600; color: var(--text-primary); }
.header-badges {
    margin-left: auto;
    display: flex;
    gap: 8px;
    align-items: center;
}
.badge {
    font-size: 10px; padding: 3px 10px; border-radius: 12px;
    background: var(--accent-bg); color: var(--accent-hover);
}
#session-id-badge {
    font-size: 11px; color: var(--text-muted);
}

/* ===== Toolbar ===== */
.toolbar {
    display: flex;
    gap: 6px;
    padding: 10px 20px;
    flex-wrap: wrap;
    border-bottom: 1px solid var(--border);
    background: var(--bg-primary);
}
.toolbar button {
    padding: 5px 12px;
    border-radius: 20px;
    border: 1px solid var(--border);
    background: var(--bg-secondary);
    color: var(--text-secondary);
    font-size: 12px;
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
}
.toolbar button:hover {
    background: var(--bg-card);
    color: var(--text-primary);
    border-color: var(--accent);
}

/* ===== Chat Box ===== */
#chat-box {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    scroll-behavior: smooth;
}

/* ===== Messages ===== */
.msg {
    display: flex;
    gap: 12px;
    margin-bottom: 20px;
    animation: fadeIn 0.3s ease;
}
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.msg .avatar {
    width: 34px; height: 34px; border-radius: 10px;
    flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    font-size: 16px;
}
.msg.user { flex-direction: row-reverse; }
.msg.user .avatar { background: var(--accent); }
.msg.assistant .avatar { background: var(--bg-card); border: 1px solid var(--border); }

.msg .bubble {
    padding: 12px 18px;
    border-radius: var(--radius);
    line-height: 1.7;
    max-width: 80%;
    font-size: 14px;
    word-wrap: break-word;
}
.msg.user .bubble {
    background: var(--accent);
    color: white;
    border-bottom-right-radius: 4px;
}
.msg.assistant .bubble {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
    color: var(--text-primary);
}

/* Bubble rich content */
.msg.assistant .bubble strong { color: #fbbf24; }
.msg.assistant .bubble em { color: #d8b4fe; }
.msg.assistant .bubble code {
    background: var(--bg-primary);
    padding: 2px 6px; border-radius: 4px;
    font-family: var(--font-mono);
    font-size: 13px; color: var(--accent-hover);
}
.msg.assistant .bubble pre {
    background: var(--bg-primary);
    padding: 14px; border-radius: var(--radius-sm);
    overflow-x: auto; margin: 10px 0;
    border: 1px solid var(--border);
}
.msg.assistant .bubble pre code { background: none; padding: 0; color: var(--text-primary); }
.msg.assistant .bubble h3 { color: var(--accent-hover); margin: 14px 0 8px; font-size: 15px; }
.msg.assistant .bubble ul, .msg.assistant .bubble ol { margin: 8px 0 8px 24px; }
.msg.assistant .bubble li { margin: 4px 0; }

/* Typing indicator */
.typing-dots {
    display: inline-flex;
    gap: 4px;
    padding: 4px 0;
}
.typing-dots span {
    width: 7px; height: 7px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: typingBounce 1.4s infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typingBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.3; }
    30% { transform: translateY(-7px); opacity: 1; }
}

/* ===== Tool Call Cards ===== */
.tool-call {
    margin: 6px 0 6px 46px;
    background: var(--bg-card);
    border-left: 3px solid var(--accent);
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    font-size: 13px;
    animation: fadeIn 0.3s ease;
}
.tool-call.done { border-left-color: var(--green); }
.tool-header {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--text-primary);
    font-weight: 600;
    font-size: 12px;
}
.tool-args {
    font-weight: 400;
    color: var(--text-muted);
    font-size: 11px;
    font-family: var(--font-mono);
}
.tool-result {
    margin-top: 6px;
    padding: 6px 10px;
    background: var(--bg-primary);
    border-radius: var(--radius-sm);
    color: var(--green);
    font-size: 12px;
    line-height: 1.5;
}

/* ===== Input Area ===== */
#input-area {
    display: flex;
    gap: 10px;
    padding: 12px 20px;
    background: var(--bg-tertiary);
    border-top: 1px solid var(--border);
}
#input-area textarea {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    outline: none;
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-size: 14px;
    font-family: var(--font);
    resize: none;
    min-height: 44px;
    max-height: 120px;
    line-height: 1.5;
    transition: border-color 0.2s;
}
#input-area textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-bg);
}
#input-area textarea::placeholder { color: var(--text-muted); }

#input-area button {
    padding: 10px 24px;
    border-radius: var(--radius);
    border: none;
    background: linear-gradient(135deg, var(--accent), var(--accent-hover));
    color: white;
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
    align-self: flex-end;
}
#input-area button:hover { box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4); }
#input-area button:disabled { opacity: 0.4; cursor: not-allowed; box-shadow: none; }

/* ===== Status Bar ===== */
.status-bar {
    display: flex;
    justify-content: space-between;
    padding: 6px 20px;
    font-size: 11px;
    color: var(--text-muted);
    background: var(--bg-tertiary);
    border-top: 1px solid var(--border);
}
.status-bar .dot {
    display: inline-block;
    width: 7px; height: 7px;
    border-radius: 50%;
    margin-right: 6px;
}
.status-bar .dot.online { background: var(--green); }
.status-bar .dot.thinking { background: var(--amber); animation: pulse 0.8s infinite; }
.status-bar .dot.offline { background: var(--red); }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--border-light); }

/* ===== Responsive ===== */
@media (max-width: 768px) {
    #sidebar {
        position: fixed;
        left: 0; top: 0; bottom: 0;
        transform: translateX(-100%);
        box-shadow: 4px 0 20px rgba(0,0,0,0.5);
    }
    #sidebar.open { transform: translateX(0); }
    .menu-btn { display: block; }
    .toolbar button { font-size: 11px; padding: 4px 10px; }
    .msg .bubble { max-width: 90%; font-size: 13px; }
}

/* ===== Overlay for mobile sidebar ===== */
#sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 99;
}
#sidebar.open + #sidebar-overlay { display: block; }
