/* ============================================
   Smart AI Chat - Production CSS
   Design inspired by chatbot-ui / shadcn
   ============================================ */

/* ========== CSS Variables (HSL-based like shadcn/ui) ========== */
:root {
    --sidebar-width: 280px;
    --header-height: 56px;
    --max-content-width: 768px;
    --radius: 8px;
    --radius-lg: 12px;
    --radius-xl: 16px;
    --transition: 0.2s ease;
    --transition-slow: 0.3s ease;
}

/* Dark theme (default) */
[data-theme="dark"] {
    --bg-primary: 240 6% 10%;
    --bg-secondary: 240 5% 15%;
    --bg-tertiary: 240 5% 18%;
    --bg-hover: 240 5% 20%;
    --bg-active: 240 5% 23%;
    --bg-input: 240 5% 13%;
    --bg-sidebar: 240 6% 8%;
    --bg-code: 240 6% 7%;

    --text-primary: 0 0% 98%;
    --text-secondary: 240 5% 65%;
    --text-tertiary: 240 5% 45%;
    --text-muted: 240 5% 40%;

    --border-primary: 240 4% 20%;
    --border-secondary: 240 4% 16%;

    --accent: 217 91% 60%;
    --accent-hover: 217 91% 55%;
    --accent-foreground: 0 0% 100%;

    --success: 142 76% 36%;
    --warning: 38 92% 50%;
    --error: 0 84% 60%;
    --info: 199 89% 48%;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);

    --bot-avatar-bg: 217 91% 60%;
    --user-avatar-bg: 240 5% 25%;
    --message-bot-bg: 240 5% 13%;
    --message-user-bg: transparent;
}

/* Light theme */
[data-theme="light"] {
    --bg-primary: 0 0% 100%;
    --bg-secondary: 240 5% 96%;
    --bg-tertiary: 240 5% 93%;
    --bg-hover: 240 5% 90%;
    --bg-active: 240 5% 87%;
    --bg-input: 0 0% 100%;
    --bg-sidebar: 240 5% 97%;
    --bg-code: 240 5% 95%;

    --text-primary: 240 10% 10%;
    --text-secondary: 240 5% 40%;
    --text-tertiary: 240 5% 55%;
    --text-muted: 240 5% 65%;

    --border-primary: 240 6% 90%;
    --border-secondary: 240 6% 94%;

    --accent: 217 91% 60%;
    --accent-hover: 217 91% 50%;
    --accent-foreground: 0 0% 100%;

    --success: 142 76% 36%;
    --warning: 38 92% 50%;
    --error: 0 84% 60%;
    --info: 199 89% 48%;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);

    --bot-avatar-bg: 217 91% 60%;
    --user-avatar-bg: 240 5% 88%;
    --message-bot-bg: 240 5% 96%;
    --message-user-bg: transparent;
}

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

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "PingFang SC", "Microsoft YaHei", sans-serif;
    background: hsl(var(--bg-primary));
    color: hsl(var(--text-primary));
    font-size: 14px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ========== App Container ========== */
.app-container {
    display: flex;
    height: 100vh;
    width: 100%;
    overflow: hidden;
}

/* ========== Custom Scrollbar ========== */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: hsl(var(--text-muted) / 0.3);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: hsl(var(--text-muted) / 0.5);
}

/* ========== Sidebar ========== */
.sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    height: 100vh;
    background: hsl(var(--bg-sidebar));
    border-right: 1px solid hsl(var(--border-secondary));
    display: flex;
    flex-direction: column;
    transition: transform var(--transition-slow), width var(--transition-slow), min-width var(--transition-slow);
    z-index: 100;
}

.sidebar.collapsed {
    width: 0;
    min-width: 0;
    transform: translateX(calc(var(--sidebar-width) * -1));
    border-right: none;
    overflow: hidden;
}

.sidebar-header {
    padding: 12px;
    flex-shrink: 0;
}

.new-chat-btn {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 16px;
    background: transparent;
    border: 1px solid hsl(var(--border-primary));
    border-radius: var(--radius);
    color: hsl(var(--text-primary));
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background var(--transition), border-color var(--transition);
}

.new-chat-btn:hover {
    background: hsl(var(--bg-hover));
    border-color: hsl(var(--text-muted));
}

.new-chat-btn svg {
    flex-shrink: 0;
}

/* Sidebar Chat List */
.sidebar-chats {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.chat-group {
    margin-bottom: 8px;
}

.chat-group:empty,
.chat-group-label:empty + .chat-group-items:empty {
    display: none;
}

.chat-group-label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: hsl(var(--text-muted));
    padding: 8px 12px 4px;
}

.chat-group-items {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.chat-item {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    border-radius: var(--radius);
    cursor: pointer;
    transition: background var(--transition);
    position: relative;
    overflow: hidden;
}

.chat-item:hover {
    background: hsl(var(--bg-hover));
}

.chat-item.active {
    background: hsl(var(--bg-active));
}

.chat-item-text {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 13px;
    color: hsl(var(--text-secondary));
}

.chat-item.active .chat-item-text {
    color: hsl(var(--text-primary));
}

.chat-item-delete {
    opacity: 0;
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    background: hsl(var(--bg-tertiary));
    border: none;
    color: hsl(var(--text-muted));
    width: 28px;
    height: 28px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity var(--transition), color var(--transition), background var(--transition);
}

.chat-item:hover .chat-item-delete {
    opacity: 1;
}

.chat-item-delete:hover {
    color: hsl(var(--error));
    background: hsl(var(--error) / 0.1);
}

/* Sidebar Footer */
.sidebar-footer {
    padding: 12px;
    border-top: 1px solid hsl(var(--border-secondary));
    flex-shrink: 0;
}

.sidebar-footer-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: var(--radius);
    color: hsl(var(--text-secondary));
    text-decoration: none;
    font-size: 13px;
    transition: background var(--transition), color var(--transition);
}

.sidebar-footer-link:hover {
    background: hsl(var(--bg-hover));
    color: hsl(var(--text-primary));
}

/* ========== Main Content ========== */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
    min-width: 0;
}

/* Top Header */
.top-header {
    height: var(--header-height);
    padding: 0 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid hsl(var(--border-secondary));
    flex-shrink: 0;
    background: hsl(var(--bg-primary));
}

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

.header-title {
    font-size: 16px;
    font-weight: 600;
    color: hsl(var(--text-primary));
}

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

/* Icon Buttons */
.icon-btn {
    width: 36px;
    height: 36px;
    border-radius: var(--radius);
    border: none;
    background: transparent;
    color: hsl(var(--text-secondary));
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background var(--transition), color var(--transition);
}

.icon-btn:hover {
    background: hsl(var(--bg-hover));
    color: hsl(var(--text-primary));
}

/* Theme Toggle Icons */
[data-theme="dark"] .icon-sun { display: none; }
[data-theme="dark"] .icon-moon { display: block; }
[data-theme="light"] .icon-sun { display: block; }
[data-theme="light"] .icon-moon { display: none; }

/* ========== Messages Stream ========== */
.messages-stream {
    flex: 1;
    overflow-y: auto;
    padding: 0;
    display: flex;
    flex-direction: column;
}

/* Welcome Screen */
.welcome-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    padding: 40px 20px;
    max-width: var(--max-content-width);
    width: 100%;
    margin: 0 auto;
    animation: fadeInUp 0.5s ease;
}

.welcome-icon {
    width: 72px;
    height: 72px;
    border-radius: var(--radius-xl);
    background: hsl(var(--accent) / 0.1);
    color: hsl(var(--accent));
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 24px;
}

.welcome-screen h2 {
    font-size: 28px;
    font-weight: 700;
    margin-bottom: 8px;
    text-align: center;
    color: hsl(var(--text-primary));
}

.welcome-subtitle {
    color: hsl(var(--text-secondary));
    font-size: 15px;
    margin-bottom: 32px;
    text-align: center;
}

.suggestion-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    width: 100%;
    max-width: 600px;
}

.suggestion-card {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding: 16px;
    background: hsl(var(--bg-secondary));
    border: 1px solid hsl(var(--border-primary));
    border-radius: var(--radius-lg);
    cursor: pointer;
    text-align: left;
    transition: background var(--transition), border-color var(--transition), transform var(--transition);
}

.suggestion-card:hover {
    background: hsl(var(--bg-hover));
    border-color: hsl(var(--text-muted) / 0.3);
    transform: translateY(-1px);
}

.suggestion-icon {
    font-size: 24px;
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.suggestion-text {
    flex: 1;
    min-width: 0;
}

.suggestion-title {
    font-weight: 600;
    font-size: 14px;
    color: hsl(var(--text-primary));
    margin-bottom: 2px;
}

.suggestion-desc {
    font-size: 12px;
    color: hsl(var(--text-secondary));
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ========== Message Row ========== */
.message-row {
    width: 100%;
    padding: 16px 20px;
    animation: fadeInUp 0.3s ease;
    transition: background var(--transition);
}

.message-row:hover {
    background: hsl(var(--bg-secondary) / 0.5);
}

.message-row.bot {
    background: hsl(var(--message-bot-bg));
}

.message-row.bot:hover {
    background: hsl(var(--bg-tertiary) / 0.5);
}

.message-inner {
    max-width: var(--max-content-width);
    margin: 0 auto;
    display: flex;
    gap: 16px;
    align-items: flex-start;
}

.message-avatar {
    width: 32px;
    height: 32px;
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 14px;
    font-weight: 600;
}

.message-row.bot .message-avatar {
    background: hsl(var(--bot-avatar-bg));
    color: white;
}

.message-row.user .message-avatar {
    background: hsl(var(--user-avatar-bg));
    color: hsl(var(--text-primary));
}

.message-content {
    flex: 1;
    min-width: 0;
}

.message-sender {
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 4px;
    color: hsl(var(--text-primary));
}

.message-body {
    font-size: 14px;
    line-height: 1.7;
    color: hsl(var(--text-primary));
    word-break: break-word;
}

.message-body p {
    margin-bottom: 8px;
}

.message-body p:last-child {
    margin-bottom: 0;
}

.message-body ul, .message-body ol {
    padding-left: 24px;
    margin-bottom: 8px;
}

.message-body li {
    margin-bottom: 4px;
}

.message-body strong {
    font-weight: 600;
}

.message-body em {
    font-style: italic;
}

.message-body a {
    color: hsl(var(--accent));
    text-decoration: underline;
    text-underline-offset: 2px;
}

.message-body a:hover {
    color: hsl(var(--accent-hover));
}

/* Code blocks */
.message-body pre {
    background: hsl(var(--bg-code));
    border: 1px solid hsl(var(--border-primary));
    border-radius: var(--radius);
    padding: 16px;
    margin: 12px 0;
    overflow-x: auto;
    position: relative;
}

.message-body code {
    font-family: "JetBrains Mono", "Fira Code", "Cascadia Code", monospace;
    font-size: 13px;
}

.message-body :not(pre) > code {
    background: hsl(var(--bg-tertiary));
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 12px;
}

.code-copy-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    background: hsl(var(--bg-hover));
    border: 1px solid hsl(var(--border-primary));
    color: hsl(var(--text-secondary));
    padding: 4px 8px;
    border-radius: 6px;
    font-size: 11px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 4px;
    opacity: 0;
    transition: opacity var(--transition), background var(--transition);
}

.message-body pre:hover .code-copy-btn {
    opacity: 1;
}

.code-copy-btn:hover {
    background: hsl(var(--bg-active));
}

.code-copy-btn.copied {
    color: hsl(var(--success));
}

/* Tables */
.message-body table {
    width: 100%;
    border-collapse: collapse;
    margin: 12px 0;
    font-size: 13px;
}

.message-body th, .message-body td {
    padding: 8px 12px;
    border: 1px solid hsl(var(--border-primary));
    text-align: left;
}

.message-body th {
    background: hsl(var(--bg-tertiary));
    font-weight: 600;
}

.message-body tr:nth-child(even) {
    background: hsl(var(--bg-secondary) / 0.5);
}

/* Blockquotes */
.message-body blockquote {
    border-left: 3px solid hsl(var(--accent));
    padding-left: 16px;
    margin: 12px 0;
    color: hsl(var(--text-secondary));
    font-style: italic;
}

/* Confidence Tag */
.confidence-tag {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 12px;
    margin-top: 8px;
    font-weight: 500;
}

.confidence-tag.high {
    background: hsl(var(--success) / 0.1);
    color: hsl(var(--success));
}

.confidence-tag.medium {
    background: hsl(var(--warning) / 0.1);
    color: hsl(var(--warning));
}

.confidence-tag.low {
    background: hsl(var(--error) / 0.1);
    color: hsl(var(--error));
}

/* Message Actions */
.message-actions {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-top: 8px;
    opacity: 0;
    transition: opacity var(--transition);
}

.message-row:hover .message-actions {
    opacity: 1;
}

.action-btn {
    background: transparent;
    border: none;
    color: hsl(var(--text-muted));
    cursor: pointer;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background var(--transition), color var(--transition);
}

.action-btn:hover {
    background: hsl(var(--bg-hover));
    color: hsl(var(--text-secondary));
}

.action-btn.active-up {
    color: hsl(var(--success));
}

.action-btn.active-down {
    color: hsl(var(--error));
}

.action-btn.copied {
    color: hsl(var(--success));
}

/* ========== Typing Indicator ========== */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 8px 0;
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    background: hsl(var(--text-muted));
    border-radius: 50%;
    animation: typingPulse 1.4s infinite;
}

.typing-indicator span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typingPulse {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.4;
    }
    30% {
        transform: translateY(-6px);
        opacity: 1;
    }
}

/* ========== Input Area ========== */
.input-area {
    padding: 16px 20px 24px;
    flex-shrink: 0;
    background: hsl(var(--bg-primary));
}

.input-container {
    max-width: var(--max-content-width);
    margin: 0 auto;
    width: 100%;
}

.input-box {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: hsl(var(--bg-input));
    border: 1px solid hsl(var(--border-primary));
    border-radius: var(--radius-lg);
    padding: 12px 16px;
    transition: border-color var(--transition), box-shadow var(--transition);
}

.input-box:focus-within {
    border-color: hsl(var(--accent));
    box-shadow: 0 0 0 2px hsl(var(--accent) / 0.1);
}

.input-box textarea {
    flex: 1;
    border: none;
    background: transparent;
    color: hsl(var(--text-primary));
    font-size: 14px;
    font-family: inherit;
    line-height: 1.5;
    resize: none;
    outline: none;
    max-height: 120px;
    overflow-y: auto;
    padding: 0;
}

.input-box textarea::placeholder {
    color: hsl(var(--text-muted));
}

.send-btn {
    width: 32px;
    height: 32px;
    border-radius: var(--radius);
    border: none;
    background: hsl(var(--accent));
    color: hsl(var(--accent-foreground));
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background var(--transition), opacity var(--transition), transform var(--transition);
}

.send-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.send-btn:not(:disabled):hover {
    background: hsl(var(--accent-hover));
    transform: scale(1.05);
}

.send-btn:not(:disabled):active {
    transform: scale(0.95);
}

.send-btn .icon-stop {
    display: none;
}

.send-btn.generating .icon-send {
    display: none;
}

.send-btn.generating .icon-stop {
    display: block;
}

.send-btn.generating {
    background: hsl(var(--error));
}

.input-footer {
    display: flex;
    justify-content: center;
    margin-top: 8px;
}

.input-hint {
    font-size: 11px;
    color: hsl(var(--text-muted));
}

/* ========== Toast Notifications ========== */
.toast-container {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 10000;
    display: flex;
    flex-direction: column;
    gap: 8px;
    pointer-events: none;
}

.toast {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 16px;
    background: hsl(var(--bg-secondary));
    border: 1px solid hsl(var(--border-primary));
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    font-size: 13px;
    color: hsl(var(--text-primary));
    pointer-events: auto;
    animation: toastIn 0.3s ease;
    max-width: 360px;
}

.toast.removing {
    animation: toastOut 0.3s ease forwards;
}

.toast-icon {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
}

.toast.success .toast-icon { color: hsl(var(--success)); }
.toast.error .toast-icon { color: hsl(var(--error)); }
.toast.info .toast-icon { color: hsl(var(--info)); }
.toast.warning .toast-icon { color: hsl(var(--warning)); }

@keyframes toastIn {
    from { opacity: 0; transform: translateX(20px); }
    to { opacity: 1; transform: translateX(0); }
}

@keyframes toastOut {
    from { opacity: 1; transform: translateX(0); }
    to { opacity: 0; transform: translateX(20px); }
}

/* ========== Animations ========== */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ========== Sidebar Overlay (Mobile) ========== */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 90;
    opacity: 0;
    transition: opacity var(--transition-slow);
}

.sidebar-overlay.visible {
    opacity: 1;
}

/* ========== Responsive ========== */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        height: 100vh;
        transform: translateX(-100%);
        box-shadow: var(--shadow-lg);
        z-index: 100;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .sidebar.collapsed {
        transform: translateX(-100%);
        width: var(--sidebar-width);
        min-width: var(--sidebar-width);
    }

    .sidebar-overlay {
        display: block;
    }

    .suggestion-grid {
        grid-template-columns: 1fr;
    }

    .welcome-screen h2 {
        font-size: 22px;
    }

    .message-row {
        padding: 12px 16px;
    }

    .message-inner {
        gap: 10px;
    }

    .input-area {
        padding: 12px 16px 20px;
    }
}

@media (max-width: 480px) {
    .header-title {
        font-size: 14px;
    }

    .suggestion-card {
        padding: 12px;
    }

    .suggestion-desc {
        display: none;
    }
}

/* ========== Selection ========== */
::selection {
    background: hsl(var(--accent) / 0.3);
    color: hsl(var(--text-primary));
}

/* ========== Focus Visible ========== */
:focus-visible {
    outline: 2px solid hsl(var(--accent));
    outline-offset: 2px;
}

/* ========== HLJS Overrides ========== */
.hljs {
    background: transparent !important;
    padding: 0 !important;
}
