/* Light theme (default) color tokens. Dark theme overrides these below via [data-theme="dark"]. */
:root {
    color-scheme: light;
    --cw-bg: #f4f5f7;
    --cw-surface: #ffffff;
    --cw-surface-soft: #f8fafc;
    --cw-text: #2d3748;
    --cw-text-muted: #718096;
    --cw-border: #e2e8f0;
    --cw-toolbar-bg: #ffffff;
    --cw-toggle-bg: #edf2f7;
    --cw-toggle-active-bg: #e8491d;
    --cw-toggle-active-text: #ffffff;
    --cw-shadow: rgba(0, 0, 0, 0.08);
    --cw-accent: #e8491d;
    --cw-accent-strong: #d63f12;
}

/* Dark theme token overrides, applied when chat.js sets data-theme="dark" on <html>. */
:root[data-theme="dark"] {
    color-scheme: dark;
    --cw-bg: #1a202c;
    --cw-surface: #2d3748;
    --cw-surface-soft: #212832;
    --cw-text: #e2e8f0;
    --cw-text-muted: #a0aec0;
    --cw-border: #4a5568;
    --cw-toolbar-bg: #212832;
    --cw-toggle-bg: #2d3748;
    --cw-toggle-active-bg: #e8491d;
    --cw-toggle-active-text: #ffffff;
    --cw-shadow: rgba(0, 0, 0, 0.3);
    --cw-accent: #e8491d;
    --cw-accent-strong: #f97316;
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
    min-height: 100vh;
    font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    background: var(--cw-bg);
    color: var(--cw-text);
    transition: background 0.25s ease, color 0.25s ease;
}

/* Outer page background + centering wrapper around the chat card. */
.chat-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 20px;
    background: var(--cw-bg);
}

/* The fixed-size chat card itself (mirrors the original ChatKit widget dimensions). */
.chat-container {
    display: flex;
    flex-direction: column;
    width: 420px;
    height: 700px;
    border-radius: 12px;
    overflow: hidden;
    background: var(--cw-surface);
    box-shadow: 0 4px 24px var(--cw-shadow);
    border: 1px solid var(--cw-border);
    animation: fade-in 220ms ease-out;
}

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

/* Top toolbar: workflow switcher + title on the left, theme dropdown on the right. */
.chat-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 12px;
    background: var(--cw-toolbar-bg);
    border-bottom: 1px solid var(--cw-border);
    gap: 12px;
}

.chat-toolbar-right { display: flex; align-items: center; gap: 8px; }

.workflow-switch {
    border: 1px solid var(--cw-border);
    border-radius: 8px;
    padding: 6px 8px;
    background: var(--cw-surface);
    color: var(--cw-text);
    font-size: 12px;
    max-width: 155px;
}

.chat-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--cw-text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chatkit-box {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    background: var(--cw-surface);
}

/* Scrollable message list and individual chat bubbles. */
.messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: linear-gradient(180deg, rgba(255,255,255,0.02), rgba(255,255,255,0));
}

.message {
    max-width: 86%;
    padding: 12px 14px;
    border-radius: 16px;
    line-height: 1.55;
    font-size: 14px;
    white-space: pre-wrap;
    word-break: break-word;
    border: 1px solid transparent;
}

.message.user {
    align-self: flex-end;
    background: var(--cw-accent);
    color: white;
    border-bottom-right-radius: 6px;
}

.message.assistant {
    align-self: flex-start;
    background: var(--cw-surface-soft);
    color: var(--cw-text);
    border-color: var(--cw-border);
    border-bottom-left-radius: 6px;
}

/* Bottom composer: textarea + send button. */
.composer {
    padding: 12px;
    border-top: 1px solid var(--cw-border);
    background: var(--cw-toolbar-bg);
}

.composer-row { display: flex; gap: 10px; align-items: flex-end; }

textarea {
    flex: 1;
    min-height: 72px;
    max-height: 160px;
    resize: none;
    border-radius: 10px;
    border: 1px solid var(--cw-border);
    background: var(--cw-surface);
    color: var(--cw-text);
    padding: 10px 12px;
    font: inherit;
    line-height: 1.45;
    outline: none;
}

textarea:focus {
    border-color: var(--cw-accent);
    box-shadow: 0 0 0 3px rgba(232, 73, 29, 0.15);
}

.send {
    border: 0;
    border-radius: 10px;
    padding: 12px 16px;
    background: var(--cw-accent);
    color: white;
    font-weight: 700;
    cursor: pointer;
    min-width: 90px;
}

.send:hover { background: var(--cw-accent-strong); }
.send:disabled { opacity: 0.6; cursor: not-allowed; }

.hint { margin: 8px 2px 0; font-size: 12px; color: var(--cw-text-muted); }

/* Light/dark/system theme dropdown menu anchored to the toolbar toggle button. */
.theme-dropdown { position: relative; }

.theme-dropdown-trigger {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 2px;
    background: transparent;
    font-size: 16px;
    cursor: pointer;
    transition: background 0.15s ease;
    line-height: 1;
    color: var(--cw-text);
}

.theme-dropdown-trigger:hover { background: rgba(0, 0, 0, 0.06); }

:root[data-theme="dark"] .theme-dropdown-trigger:hover {
    background: rgba(255, 255, 255, 0.1);
}

.theme-dropdown-menu {
    position: absolute;
    top: 100%;
    right: 0;
    margin: 4px 0 0;
    padding: 4px;
    list-style: none;
    background: var(--cw-surface);
    border: 1px solid var(--cw-border);
    border-radius: 10px;
    box-shadow: 0 8px 24px var(--cw-shadow);
    min-width: 130px;
    z-index: 20;
}

.theme-dropdown-item {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    border: none;
    background: transparent;
    color: var(--cw-text);
    font-size: 13px;
    padding: 7px 10px;
    border-radius: 7px;
    cursor: pointer;
    transition: background 0.15s ease;
    white-space: nowrap;
}

.theme-dropdown-item:hover { background: var(--cw-toggle-bg); }

.theme-dropdown-item.active {
    background: var(--cw-toggle-active-bg);
    color: var(--cw-toggle-active-text);
}

.theme-dropdown-icon { font-size: 15px; line-height: 1; }

.status { display: flex; align-items: center; gap: 8px; color: var(--cw-text-muted); font-size: 12px; }

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #48bb78;
    box-shadow: 0 0 0 3px rgba(72, 187, 120, 0.15);
}

@media (max-width: 520px) {
    .chat-wrapper { padding: 0; }
    .chat-container { width: 100%; height: 100vh; border-radius: 0; }
}
