/* ==========================================================================
   CSS Variables & Design Tokens
   ========================================================================== */
:root {
    /* Base / Fallback Layout and Typographic Tokens */
    --sidebar-width: 320px;
    --header-height: 64px;
    --font-heading: 'Outfit', -apple-system, sans-serif;
    --font-body: 'Inter', -apple-system, sans-serif;
    --font-mono: 'Menlo', 'Monaco', 'Courier New', monospace;
    --transition-fast: 0.15s cubic-bezier(0.4, 0, 0.2, 1);
    --transition-normal: 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    /* Spring-like micro-interaction */
    --error-color: #ef4444;
    --success-color: #10b981;
}

/* ==========================================================================
   Modes (Light / Dark)
   ========================================================================== */
body.mode-dark {
    --bg-base: #030712;
    /* True deep black/blue */
    --bg-surface: #0b0f19;
    --bg-surface-elevated: rgba(17, 24, 39, 0.5);
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --border-color: rgba(255, 255, 255, 0.05);
    --border-highlight: rgba(255, 255, 255, 0.1);
    --glass-bg: rgba(11, 15, 25, 0.4);
    --glass-border: rgba(255, 255, 255, 0.08);
    --shadow-subtle: 0 8px 32px rgba(0, 0, 0, 0.4);
    --code-bg: #0f172a;
    --code-border: rgba(255, 255, 255, 0.1);
}

body.mode-light {
    --bg-base: #f8fafc;
    --bg-surface: #ffffff;
    --bg-surface-elevated: rgba(255, 255, 255, 0.6);
    --text-primary: #0f172a;
    --text-secondary: #475569;
    --text-muted: #94a3b8;
    --border-color: rgba(0, 0, 0, 0.06);
    --border-highlight: rgba(0, 0, 0, 0.12);
    --glass-bg: rgba(255, 255, 255, 0.7);
    --glass-border: rgba(255, 255, 255, 0.4);
    --shadow-subtle: 0 8px 32px rgba(0, 0, 0, 0.05);
    --code-bg: #f1f5f9;
    --code-border: #cbd5e1;
}

/* ==========================================================================
   Themes (Glassmorphism, Minimalist, Cyberpunk)
   ========================================================================== */
/* 1. Theme: Glassmorphism (Premium Aurora) */
body.theme-glass {
    --accent-primary: #8b5cf6;
    --accent-secondary: #3b82f6;
    --accent-glow: rgba(139, 92, 246, 0.5);
    --radius-sm: 8px;
    --radius-md: 16px;
    --radius-lg: 24px;
    --radius-full: 9999px;
    --msg-radius: 1.5rem;
    --glass-blur: blur(24px);
    /* Premium heavy blur */
    --shadow-glow: 0 0 30px var(--accent-glow);
    --msg-user-bg: rgba(255, 255, 255, 0.05);
    --msg-user-color: var(--text-primary);
    --msg-user-border: 1px solid rgba(255, 255, 255, 0.1);
    /* Premium Aurora Mesh Gradient */
    --app-bg-image:
        radial-gradient(circle at 100% 0%, rgba(139, 92, 246, 0.15) 0%, transparent 50%),
        radial-gradient(circle at 0% 100%, rgba(59, 130, 246, 0.15) 0%, transparent 50%),
        radial-gradient(circle at 50% 50%, rgba(236, 72, 153, 0.05) 0%, transparent 50%);
}

body.mode-light.theme-glass {
    --msg-user-bg: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    --msg-user-color: #ffffff;
    --msg-user-border: none;
    --app-bg-image:
        radial-gradient(circle at 100% 0%, rgba(139, 92, 246, 0.1) 0%, transparent 50%),
        radial-gradient(circle at 0% 100%, rgba(59, 130, 246, 0.1) 0%, transparent 50%);
}

/* 2. Theme: Minimalist (Ultra Clean) */
body.theme-minimal {
    --accent-primary: #171717;
    --accent-secondary: #737373;
    --accent-glow: rgba(0, 0, 0, 0.1);
    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-full: 9999px;
    --msg-radius: 0.75rem;
    --glass-blur: blur(0px);
    --shadow-glow: none;
    --msg-user-bg: var(--bg-surface-elevated);
    --msg-user-color: var(--text-primary);
    --msg-user-border: 1px solid var(--border-color);
    --app-bg-image: none;
}

body.mode-dark.theme-minimal {
    --accent-primary: #ededed;
    --accent-secondary: #a3a3a3;
    --accent-glow: rgba(255, 255, 255, 0.1);
}

/* 3. Theme: Cyberpunk (Neon grid) */
body.theme-cyberpunk {
    --accent-primary: #39ff14;
    --accent-secondary: #f0f;
    --accent-glow: rgba(57, 255, 20, 0.5);
    --radius-sm: 0px;
    --radius-md: 0px;
    --radius-lg: 0px;
    --radius-full: 0px;
    --msg-radius: 0px;
    --glass-blur: blur(2px);
    --shadow-glow: 0 0 20px rgba(57, 255, 20, 0.4);
    --msg-user-bg: rgba(57, 255, 20, 0.05);
    --msg-user-color: var(--accent-primary);
    --msg-user-border: 1px solid var(--accent-primary);
    --app-bg-image:
        linear-gradient(rgba(57, 255, 20, 0.03) 1px, transparent 1px),
        linear-gradient(90deg, rgba(57, 255, 20, 0.03) 1px, transparent 1px);
    background-size: 30px 30px;
}

body.mode-light.theme-cyberpunk {
    --accent-primary: #ff003c;
    --accent-secondary: #00e0ff;
    --accent-glow: rgba(255, 0, 60, 0.4);
    --msg-user-bg: rgba(255, 0, 60, 0.05);
    --msg-user-color: var(--accent-primary);
    --msg-user-border: 1px solid var(--accent-primary);
    --app-bg-image:
        linear-gradient(rgba(255, 0, 60, 0.03) 1px, transparent 1px),
        linear-gradient(90deg, rgba(255, 0, 60, 0.03) 1px, transparent 1px);
    background-size: 30px 30px;
}

/* ==========================================================================
   Global Reset & Base Styles
   ========================================================================== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: var(--font-body);
    background-color: var(--bg-base);
    background-image: var(--app-bg-image);
    background-attachment: fixed;
    color: var(--text-primary);
    line-height: 1.6;
    height: 100vh;
    /* Fallback */
    height: 100dvh;
    width: 100vw;
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
    position: fixed;
    /* Khóa viewport iOS/Android */
    inset: 0;
}

h1,
h2,
h3,
h4,
h5,
h6 {
    font-family: var(--font-heading);
    font-weight: 500;
}

a {
    color: var(--accent-secondary);
    text-decoration: none;
    transition: color var(--transition-fast);
}

a:hover {
    color: var(--accent-primary);
    text-decoration: underline;
}

/* Custom Scrollbar */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.1);
    border-radius: var(--radius-full);
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* ==========================================================================
   Layout & Grid
   ========================================================================== */
.app-container {
    display: flex;
    height: 100vh;
    /* Fallback */
    height: 100dvh;
    width: 100%;
    /* Avoid 100vw overflow */
    position: relative;
    overflow: hidden;
    padding: 1rem;
    gap: 1rem;
    min-height: 0;
    min-width: 0;
}

.mobile-only {
    display: none !important;
}

/* ==========================================================================
   Sidebar
   ========================================================================== */
.sidebar {
    width: var(--sidebar-width);
    background-color: var(--glass-bg);
    backdrop-filter: var(--glass-blur);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-lg);
    display: flex;
    flex-direction: column;
    z-index: 100;
    transition: transform var(--transition-normal);
    box-shadow: var(--shadow-subtle);
    overflow: hidden;
}

.sidebar-header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.5rem;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-header h2 {
    font-size: 1.25rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: linear-gradient(135deg, var(--text-primary), var(--text-secondary));
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.sidebar-header .fa-sparkles {
    color: var(--accent-primary);
    -webkit-text-fill-color: var(--accent-primary);
}

.settings-panel {
    padding: 1.5rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

.setting-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.setting-group label {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-secondary);
}

.helper-text {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* Inputs & Selects */
.input-with-icon {
    position: relative;
    display: flex;
    align-items: center;
}

.input-with-icon i {
    position: absolute;
    left: 1rem;
    color: var(--text-muted);
    font-size: 0.875rem;
}

.input-with-icon input {
    width: 100%;
    background: var(--bg-surface-elevated);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 0.625rem 1rem 0.625rem 2.5rem;
    color: var(--text-primary);
    font-family: var(--font-mono);
    font-size: 0.875rem;
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.input-with-icon input:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 1px var(--accent-primary);
}

.custom-dropdown-wrapper {
    position: relative;
    width: 100%;
}

.custom-dropdown-trigger {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--bg-surface-elevated);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 0.625rem 1rem;
    color: var(--text-primary);
    font-family: var(--font-body);
    font-size: 0.875rem;
    cursor: pointer;
    transition: all var(--transition-normal);
    text-align: left;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.custom-dropdown-trigger:hover,
.custom-dropdown-wrapper.open .custom-dropdown-trigger {
    border-color: var(--accent-primary);
    background: rgba(139, 92, 246, 0.05);
}

.custom-dropdown-trigger i {
    color: var(--text-muted);
    transition: transform var(--transition-normal);
    font-size: 0.75rem;
    margin-left: 0.5rem;
}

.custom-dropdown-wrapper.open .custom-dropdown-trigger i {
    transform: rotate(180deg);
}

.custom-dropdown-menu {
    position: absolute;
    bottom: calc(100% + 0.5rem);
    /* Dropup direction */
    left: 0;
    width: 100%;
    max-height: 400px;
    /* Much taller to see more models */
    overflow-y: auto;
    background: var(--bg-surface);
    /* Solid background, less transparent */
    border: 1px solid var(--border-color);
    /* Harder border */
    border-radius: var(--radius-md);
    padding: 0.5rem;
    box-shadow: var(--shadow-subtle), 0 -10px 40px rgba(0, 0, 0, 0.4);
    /* Shadow points up */
    z-index: 1000;

    /* Animation */
    opacity: 0;
    visibility: hidden;
    transform: translateY(10px) scale(0.98);
    /* Animate from below */
    transition: all var(--transition-normal);
    transform-origin: bottom center;
    /* Grow from the bottom up */
}

.custom-dropdown-wrapper.open .custom-dropdown-menu {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1);
}

.dropdown-option {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    padding: 0.625rem 0.75rem;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 0.875rem;
    cursor: pointer;
    transition: all var(--transition-fast);
    text-align: left;
}

.dropdown-option:hover {
    background: var(--bg-surface-elevated);
    color: var(--text-primary);
}

.dropdown-option.active {
    background: rgba(139, 92, 246, 0.15);
    color: var(--accent-primary);
    font-weight: 500;
}

.dropdown-option .model-id-hint {
    font-size: 0.7rem;
    color: var(--text-muted);
    font-family: var(--font-mono);
    margin-top: 0.1rem;
}

.truncate-text {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 90%;
}

.select-icon {
    position: absolute;
    right: 1rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    pointer-events: none;
    font-size: 0.75rem;
}

/* History */
.history-panel {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.history-actions {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.history-header h3 {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.chat-history-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.history-item {
    padding: 0.5rem 0.75rem;
    border-radius: var(--radius-md);
    cursor: pointer;
    font-size: 0.875rem;
    color: var(--text-secondary);
    transition: background-color var(--transition-fast), color var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    position: relative;
}

.history-item-content {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    overflow: hidden;
    flex: 1;
}

.history-item-title {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.delete-chat-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 0.875rem;
    padding: 4px;
    border-radius: 4px;
    opacity: 0;
    transition: all var(--transition-fast);
}

.delete-chat-btn:hover {
    color: var(--error-color);
    background: rgba(239, 68, 68, 0.1);
}

.history-item:hover .delete-chat-btn {
    opacity: 1;
}

.history-item:hover,
.history-item.active {
    background-color: var(--bg-surface-elevated);
    color: var(--text-primary);
}

.history-item.empty-state {
    cursor: default;
    color: var(--text-muted);
    font-style: italic;
    justify-content: center;
    padding: 2rem 0;
}

.history-item.empty-state:hover {
    background-color: transparent;
}

.sidebar-footer {
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--border-color);
    text-align: center;
}

.text-link {
    font-size: 0.875rem;
    color: var(--text-muted);
}

/* ==========================================================================
   Main Chat Area
   ========================================================================== */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    background-color: var(--glass-bg);
    backdrop-filter: var(--glass-blur);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-lg);
    position: relative;
    max-width: 100%;
    box-shadow: var(--shadow-subtle);
    overflow: hidden;
    min-height: 0;
    min-width: 0;
    -webkit-overflow-scrolling: touch;
}

.topbar {
    height: var(--header-height);
    background: transparent;
    border-bottom: 1px solid var(--glass-border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.5rem;
    z-index: 10;
}

.topbar-left {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.app-brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-family: var(--font-heading);
}

.brand-logo {
    width: 32px;
    height: 32px;
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 1rem;
    box-shadow: var(--shadow-glow);
}

.brand-name {
    font-weight: 600;
    font-size: 1.25rem;
    letter-spacing: -0.02em;
}

.topbar-right {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.theme-dropdown {
    position: relative;
}

.theme-toggle-btn {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--bg-surface-elevated);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-full);
    padding: 0.5rem 1rem;
    color: var(--text-primary);
    font-family: var(--font-body);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.theme-toggle-btn:hover {
    border-color: var(--accent-primary);
    background: rgba(139, 92, 246, 0.1);
}

.theme-toggle-btn i {
    color: var(--text-muted);
}

.theme-menu {
    position: absolute;
    top: calc(100% + 0.5rem);
    right: 0;
    width: 220px;
    background: var(--glass-bg);
    backdrop-filter: var(--glass-blur);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 0.5rem;
    box-shadow: var(--shadow-subtle), 0 10px 30px rgba(0, 0, 0, 0.2);
    display: flex;
    flex-direction: column;
    gap: 0.25rem;

    /* Animation initial state */
    opacity: 0;
    visibility: hidden;
    transform: translateY(-10px);
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
    z-index: 100;
}

.theme-dropdown.open .theme-menu {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.theme-option {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    width: 100%;
    padding: 0.625rem 0.75rem;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 0.875rem;
    font-weight: 500;
    text-align: left;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.theme-option i {
    width: 16px;
    text-align: center;
    color: var(--text-muted);
}

.theme-option:hover {
    background: var(--bg-surface-elevated);
    color: var(--text-primary);
}

.theme-option.active {
    background: rgba(139, 92, 246, 0.15);
    color: var(--accent-primary);
}

.theme-option.active i {
    color: var(--accent-primary);
}

.current-model-display {
    font-family: var(--font-heading);
    font-weight: 500;
}

.chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 0;
    scroll-behavior: smooth;
    position: relative;
}

.chat-messages {
    max-width: 800px;
    margin: 0 auto;
    padding: 2rem 1rem 6rem 1rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    min-height: 100%;
}

/* ==========================================================================
   Welcome Screen
   ========================================================================== */
.welcome-screen {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    margin: auto 0;
    padding: 4rem 1rem;
    animation: fadeIn 0.8s ease-out;
}

.welcome-logo {
    position: relative;
    width: 80px;
    height: 80px;
    border-radius: var(--radius-full);
    background: var(--bg-surface-elevated);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 1.5rem;
    border: 1px solid var(--border-highlight);
    box-shadow: var(--shadow-subtle);
}

.welcome-logo i {
    font-size: 2rem;
    color: var(--text-primary);
    z-index: 2;
}

.glow-orb {
    position: absolute;
    width: 150%;
    height: 150%;
    background: radial-gradient(circle, var(--accent-glow) 0%, transparent 60%);
    border-radius: 50%;
    z-index: 1;
    animation: pulse 4s infinite alternate;
}

.welcome-screen h1 {
    font-size: 2.5rem;
    margin-bottom: 0.5rem;
    background: linear-gradient(135deg, #fff, #a78bfa);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.welcome-screen p {
    color: var(--text-secondary);
    margin-bottom: 2.5rem;
    max-width: 400px;
}

.suggestion-chips {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.75rem;
}

.chip {
    background: var(--bg-surface-elevated);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 0.75rem 1.25rem;
    border-radius: var(--radius-full);
    font-size: 0.875rem;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.chip:hover {
    border-color: var(--accent-primary);
    box-shadow: 0 0 15px rgba(139, 92, 246, 0.2);
    transform: translateY(-2px);
}

/* ==========================================================================
   Message Bubbles
   ========================================================================== */
.message {
    display: flex;
    gap: 1rem;
    padding: 0.5rem 1rem;
    margin-bottom: 1rem;
    width: 100%;
    max-width: 100%;
    animation: slideUp 0.3s ease-out forwards;
    opacity: 0;
    transform: translateY(10px);
    position: relative;
}

.message.user {
    justify-content: flex-end;
}

.message.assistant {
    justify-content: flex-start;
}

.avatar {
    width: 36px;
    height: 36px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 1rem;
}

.user .avatar {
    display: none;
    /* Hide user avatar for modern look */
}

.assistant .avatar {
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    color: white;
    box-shadow: 0 0 10px rgba(139, 92, 246, 0.3);
}

.message-content {
    overflow-x: auto;
}

.user .message-content {
    background: var(--msg-user-bg);
    backdrop-filter: var(--glass-blur);
    color: var(--msg-user-color);
    border: var(--msg-user-border);
    padding: 1rem 1.5rem;
    border-radius: var(--msg-radius);
    border-bottom-right-radius: 0.25rem;
    max-width: 85%;
    box-shadow: var(--shadow-subtle);
}

.assistant .message-content {
    color: var(--text-primary);
    line-height: 1.7;
    flex: 1;
    max-width: 90%;
    padding-top: 0.25rem;
    padding-right: 4rem;
    /* Space for copy/speak btns */
}



/* Copy & Speak Buttons on Messages */
.message-actions {
    position: absolute;
    top: 8px;
    right: 8px;
    opacity: 0;
    transition: opacity var(--transition-fast);
    display: flex;
    gap: 4px;
}

.message:hover .message-actions {
    opacity: 1;
}

.copy-btn,
.speak-btn {
    background: var(--glass-bg);
    backdrop-filter: var(--glass-blur);
    border: 1px solid var(--glass-border);
    color: var(--text-muted);
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 0.85rem;
    transition: all var(--transition-normal);
}

.copy-btn:hover,
.speak-btn:hover {
    color: var(--text-primary);
    border-color: var(--accent-primary);
    background: rgba(139, 92, 246, 0.15);
    transform: scale(1.1);
}

.copy-btn:active,
.speak-btn:active {
    transform: scale(0.9);
}

/* Audio Visualizer Canvas */
.audio-visualizer {
    margin-left: auto;
    border-radius: var(--radius-sm);
    opacity: 1;
    transition: opacity var(--transition-normal);
}

.audio-visualizer.hidden {
    opacity: 0;
    pointer-events: none;
}

/* Markdown Styling within message */
.message-content p {
    margin-bottom: 1rem;
}

.message-content p:last-child {
    margin-bottom: 0;
}

.message-content code:not(pre code) {
    background: rgba(255, 255, 255, 0.1);
    padding: 0.1rem 0.3rem;
    border-radius: 4px;
    font-family: var(--font-mono);
    font-size: 0.85em;
    color: #e2e8f0;
}

.message-content pre {
    background: var(--code-bg);
    padding: 1rem;
    border-radius: var(--radius-md);
    overflow-x: auto;
    margin: 1rem 0;
    border: 1px solid var(--code-border);
}

.message-content pre code {
    font-family: var(--font-mono);
    font-size: 0.85rem;
    background: transparent;
    padding: 0;
}

.message-content ul,
.message-content ol {
    margin-left: 1.5rem;
    margin-bottom: 1rem;
}

/* Typing Indicator */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 0.5rem;
}

.typing-dot {
    width: 6px;
    height: 6px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: typingBounce 1.4s infinite ease-in-out both;
}

.typing-dot:nth-child(1) {
    animation-delay: -0.32s;
}

.typing-dot:nth-child(2) {
    animation-delay: -0.16s;
}

/* ==========================================================================
   Input Area Redesign (V8 Premium)
   ========================================================================== */
.input-area {
    position: relative;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(to top, var(--bg-base) 60%, transparent);
    padding: 0 1.5rem 2rem 1.5rem;
    z-index: 20;
}

.input-wrapper {
    max-width: 840px;
    /* Wider for premium feel */
    margin: 0 auto;
    background: var(--glass-bg);
    backdrop-filter: var(--glass-blur);
    border: 1px solid var(--glass-border);
    border-radius: 2rem;
    padding: 0.5rem 0.5rem 0.5rem 1.25rem;
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
    box-shadow: var(--shadow-subtle);
    transition: all var(--transition-normal);
}

.input-wrapper:focus-within {
    border-color: rgba(139, 92, 246, 0.5);
    box-shadow: var(--shadow-subtle), var(--shadow-glow);
    transform: translateY(-2px);
}

.input-core {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: transparent;
    /* No inner background */
    border-radius: 2rem;
    overflow: hidden;
    border: none;
}

.textarea-container {
    display: flex;
    align-items: flex-end;
    padding: 0.25rem;
    gap: 0.5rem;
}

.sidebar-textarea {
    background: var(--bg-surface-elevated);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
    display: flex;
    /* Added for correct layout */
}

.sidebar-textarea:focus-within {
    border-color: var(--accent-primary);
}

#system-prompt-input {
    width: 100%;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-family: var(--font-body);
    font-size: 0.875rem;
    padding: 0.625rem;
    resize: vertical;
    min-height: 60px;
    max-height: 200px;
}

#system-prompt-input:focus {
    outline: none;
}

.attach-btn {
    width: 36px;
    height: 36px;
    margin-bottom: 2px;
}

.hidden-file-input {
    display: none;
}

#system-prompt-input {
    width: 100%;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-family: var(--font-body);
    font-size: 0.875rem;
    padding: 0.625rem;
    resize: vertical;
    min-height: 60px;
    max-height: 200px;
}

#system-prompt-input:focus {
    outline: none;
}

#message-input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-family: var(--font-body);
    font-size: 1rem;
    min-height: 24px;
    max-height: 200px;
    padding: 0.5rem 0;
    resize: none;
    line-height: 1.5;
}

#message-input:focus {
    outline: none;
}

#message-input::placeholder {
    color: var(--text-muted);
}

/* Attachment Previews */
.attachment-preview-zone {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    padding: 0.75rem 0.75rem 0 0.75rem;
    border-bottom: 1px solid var(--border-color);
}

.attachment-preview-zone.hidden {
    display: none;
}

.attachment-chip {
    position: relative;
    background: var(--bg-surface-elevated);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 0.25rem 0.5rem 0.25rem 0.5rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.75rem;
    color: var(--text-primary);
    max-width: 150px;
}

.attachment-chip.image-attachment {
    padding: 0;
    overflow: hidden;
    height: 48px;
    width: 48px;
    justify-content: center;
}

.attachment-chip img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.attachment-chip .file-name {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.remove-attachment-btn {
    position: absolute;
    top: -6px;
    right: -6px;
    background: var(--error-color);
    color: white;
    border: none;
    border-radius: 50%;
    width: 16px;
    height: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.6rem;
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
}

/* Buttons */
.icon-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 1.1rem;
    transition: all var(--transition-normal);
    flex-shrink: 0;
}

.icon-btn:hover {
    color: var(--text-primary);
    background: rgba(255, 255, 255, 0.1);
    transform: scale(1.1);
}

.icon-btn:active {
    transform: scale(0.95);
}

.mic-btn.listening {
    color: #ef4444;
    /* Web red */
    animation: micPulse 1.5s infinite ease-in-out;
}

@keyframes micPulse {
    0% {
        transform: scale(1);
        opacity: 1;
    }

    50% {
        transform: scale(1.15);
        opacity: 0.7;
    }

    100% {
        transform: scale(1);
        opacity: 1;
    }
}

.primary-btn {
    background: var(--text-primary);
    color: var(--bg-base);
    border: none;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all var(--transition-normal);
    flex-shrink: 0;
    box-shadow: 0 4px 15px rgba(255, 255, 255, 0.1);
}

.primary-btn:hover {
    background: var(--accent-primary);
    color: white;
    transform: scale(1.1) translateY(-2px);
    box-shadow: 0 8px 25px var(--accent-glow);
}

.primary-btn:active {
    transform: scale(0.95);
}

.primary-btn:disabled {
    background: rgba(255, 255, 255, 0.1);
    color: rgba(255, 255, 255, 0.3);
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.disclaimer {
    text-align: center;
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 0.5rem;
    max-width: 800px;
    margin-left: auto;
    margin-right: auto;
}

/* Tooltips */
.tooltip-btn {
    position: relative;
}

.tooltip-btn::before {
    content: attr(data-tooltip);
    position: absolute;
    top: -30px;
    left: 50%;
    transform: translateX(-50%) translateY(5px);
    background: var(--bg-surface-elevated);
    color: var(--text-primary);
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 0.75rem;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: all var(--transition-fast);
    border: 1px solid var(--border-color);
}

.tooltip-btn:hover::before {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ==========================================================================
   Animations & Keyframes
   ========================================================================== */
@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes slideUp {
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes pulse {
    from {
        transform: scale(1);
        opacity: 0.3;
    }

    to {
        transform: scale(1.1);
        opacity: 0.5;
    }
}

@keyframes typingBounce {

    0%,
    80%,
    100% {
        transform: scale(0);
        opacity: 0.3;
    }

    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* ==========================================================================
   Canvas Panel (V10 Artifacts)
   ========================================================================== */
.canvas-panel {
    display: flex;
    flex-direction: column;
    width: 0;
    max-width: 0;
    opacity: 0;
    visibility: hidden;
    background: var(--bg-surface-elevated);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-subtle);
    transition: all var(--transition-normal);
    overflow: hidden;
    z-index: 50;
}

body.canvas-open .canvas-panel {
    width: 45vw;
    max-width: 800px;
    opacity: 1;
    visibility: visible;
}

body.canvas-fullscreen .canvas-panel {
    position: fixed;
    top: 1rem;
    left: 1rem;
    right: 1rem;
    bottom: 1rem;
    width: auto;
    max-width: none;
    z-index: 2000;
}

.canvas-header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1rem;
    border-bottom: 1px solid var(--border-color);
    background: var(--glass-bg);
}

.canvas-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 500;
    color: var(--text-primary);
}

.canvas-controls {
    display: flex;
    gap: 0.25rem;
}

.canvas-body {
    flex: 1;
    position: relative;
    background: #ffffff;
    /* HTML/CSS inside iframes assume white bg by default */
}

.canvas-body iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border: none;
    background: transparent;
}

/* ==========================================================================
   Code Blocks & Artifacts Controls (V10)
   ========================================================================== */
.message-content pre {
    position: relative;
    background: #111111 !important;
    /* Force a cool dark bg for code */
    border-radius: var(--radius-md);
    margin: 1rem 0;
    padding: 3rem 1rem 1rem 1rem;
    /* Top padding for the controls bar */
    overflow-x: auto;
    border: 1px solid var(--border-color);
}

.message-content pre code {
    font-family: var(--font-mono);
    font-size: 0.85rem;
    color: #e5e5e5;
}

.artifact-controls {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 40px;
    background: var(--bg-surface-elevated);
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: 0 0.75rem;
    gap: 0.5rem;
    border-bottom: 1px solid var(--border-color);
    border-top-left-radius: var(--radius-md);
    border-top-right-radius: var(--radius-md);
}

.artifact-lang-label {
    margin-right: auto;
    font-size: 0.75rem;
    color: var(--text-muted);
    font-family: var(--font-mono);
    text-transform: uppercase;
    font-weight: bold;
}

.artifact-action-btn {
    background: transparent;
    color: var(--text-muted);
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    padding: 0.35rem 0.6rem;
    font-size: 0.75rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.35rem;
    transition: all var(--transition-fast);
}

.artifact-action-btn:hover {
    color: var(--text-primary);
    background: rgba(255, 255, 255, 0.1);
}

.artifact-action-btn.run-btn {
    background: rgba(139, 92, 246, 0.1);
    color: var(--accent-primary);
    border: 1px solid rgba(139, 92, 246, 0.3);
}

.artifact-action-btn.run-btn:hover {
    background: var(--accent-primary);
    color: #fff;
    box-shadow: 0 0 10px rgba(139, 92, 246, 0.4);
}

/* ==========================================================================
   Mobile Optimizations — Cẩm nang L.I.S trên điện thoại
   ========================================================================== */
@media (max-width: 768px) {

    /* Cố định toàn màn hình, tránh scroll body trên iOS */
    body {
        position: fixed;
        width: 100%;
        height: 100%;
        height: 100dvh;
    }

    .app-container {
        height: 100vh;
        height: 100dvh;
        width: 100%;
        padding: 0;
        gap: 0;
    }

    /* Ẩn audio visualizer để chừa chỗ */
    .audio-visualizer {
        display: none !important;
    }

    /* ── Topbar ─────────────────────────────────────────── */
    .topbar {
        padding: 0 0.75rem;
        height: 52px;
        border-radius: 0;
        flex-shrink: 0;
    }

    .brand-name {
        display: none;
    }

    /* ── Chat Messages ──────────────────────────────────── */
    .chat-container {
        flex: 1;
        overflow: hidden;
    }

    .chat-messages {
        padding: 0.75rem 0.5rem 1rem 0.5rem;
    }

    /* Message bubbles — rộng hơn, dễ đọc hơn */
    .message {
        max-width: 100%;
        gap: 0.5rem;
    }

    .user .message-content,
    .assistant .message-content {
        max-width: 92%;
        padding: 0.75rem 0.875rem;
        font-size: 0.9375rem;
        /* 15px — dễ đọc trên phone */
        line-height: 1.65;
    }

    .message-content p {
        margin-bottom: 0.75rem;
    }

    .message-content ul,
    .message-content ol {
        margin-left: 1rem;
    }

    /* Avatar nhỏ hơn trên mobile */
    .avatar {
        width: 28px;
        height: 28px;
        font-size: 0.8rem;
        flex-shrink: 0;
    }

    /* ── Action Buttons (Copy/Speak) — LUÔN HIỂN THỊ trên mobile ── */
    /* Trên desktop ẩn đi cho đẹp, nhưng touch device không có hover */
    .message-actions {
        opacity: 1 !important;
        margin-top: 0.25rem;
    }

    .copy-btn,
    .speak-btn {
        width: 36px;
        height: 36px;
        font-size: 0.9rem;
    }

    /* ── Welcome Screen ──────────────────────────────────── */
    .welcome-screen {
        padding: 1.5rem 1rem;
        gap: 0.75rem;
    }

    .welcome-logo {
        width: 60px;
        height: 60px;
        font-size: 1.5rem;
    }

    .welcome-screen h1 {
        font-size: 1.5rem;
    }

    .welcome-screen p {
        font-size: 0.875rem;
        max-width: 90%;
        line-height: 1.6;
    }

    /* Suggestion chips — cuộn ngang, dễ bấm */
    .suggestion-chips {
        display: flex;
        flex-wrap: nowrap;
        /* không xuống dòng */
        overflow-x: auto;
        /* cuộn ngang */
        gap: 0.5rem;
        padding: 0.25rem 0.5rem 0.5rem;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        /* Ẩn scrollbar nhưng vẫn cuộn được */
        max-width: 100%;
        justify-content: flex-start;
    }

    .suggestion-chips::-webkit-scrollbar {
        display: none;
    }

    .chip {
        flex-shrink: 0;
        padding: 0.6rem 1rem;
        font-size: 0.8rem;
        border-radius: 2rem;
        min-height: 40px;
        /* Tap target tối thiểu 40px */
        white-space: nowrap;
    }

    /* ── Input Area ─────────────────────────────────────── */
    .input-area {
        padding: 0.5rem 0.5rem env(safe-area-inset-bottom, 0.75rem) 0.5rem;
        background: linear-gradient(to top, var(--bg-base) 70%, transparent);
        flex-shrink: 0;
    }

    .input-wrapper {
        border-radius: 1.5rem;
        padding: 0.375rem 0.375rem 0.375rem 1rem;
        /* Loại bỏ hiệu ứng nâng lên khi focus — tránh nhảy layout khi bàn phím mở */
        transform: none !important;
    }

    .input-wrapper:focus-within {
        transform: none !important;
    }

    #message-input {
        min-height: 36px;
        max-height: 120px;
        font-size: 16px;
        /* BẮT BUỘC 16px để iOS không tự zoom khi focus */
        padding: 0.35rem 0;
    }

    .icon-btn {
        width: 40px;
        height: 40px;
    }

    .primary-btn {
        width: 40px;
        height: 40px;
    }

    /* Ẩn disclaimer — tiết kiệm không gian */
    .disclaimer {
        display: none;
    }

    /* ── Sidebar (slide-in) ──────────────────────────────── */
    .mobile-only {
        display: flex !important;
    }

    .sidebar {
        position: absolute;
        top: 0;
        left: 0;
        height: 100%;
        z-index: 1000;
        transform: translateX(-100%);
        width: 85%;
        max-width: 320px;
        box-shadow: 20px 0 50px rgba(0, 0, 0, 0.5);
        border-radius: 0;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    /* ── Canvas Panel (slide-in từ phải) ────────────────── */
    .canvas-panel {
        position: absolute;
        top: 0;
        right: 0;
        height: 100%;
        z-index: 1000;
        transform: translateX(100%);
        width: 90%;
        max-width: 360px;
        box-shadow: -20px 0 50px rgba(0, 0, 0, 0.5);
        border-radius: 0;
    }

    body.canvas-open .canvas-panel {
        transform: translateX(0);
        width: 90%;
        max-width: 360px;
    }

    /* ── Tắt hover effects trên touch ───────────────────── */
    @media (hover: none) {

        .icon-btn:hover,
        .sidebar-menu-item:hover,
        .theme-option:hover,
        .chip:hover {
            background: transparent;
            transform: none;
        }

        .chip:active {
            transform: scale(0.97);
            opacity: 0.85;
        }

        .primary-btn:hover {
            transform: none;
        }

        .primary-btn:active {
            transform: scale(0.95);
        }

        /* Tắt tooltip vì không có hover trên mobile */
        .tooltip-btn::before,
        .tooltip-btn::after {
            display: none;
        }
    }
}

/* iPhone notch / Dynamic Island safe areas */
@supports (padding-top: env(safe-area-inset-top)) {
    @media (max-width: 768px) {
        .topbar {
            padding-top: env(safe-area-inset-top, 0);
        }
    }
}

/* ==========================================================================
   Admin Auth Zone
   ========================================================================== */
@keyframes shake {
    0% {
        transform: translateX(0);
    }

    20% {
        transform: translateX(-6px);
    }

    40% {
        transform: translateX(6px);
    }

    60% {
        transform: translateX(-4px);
    }

    80% {
        transform: translateX(4px);
    }

    100% {
        transform: translateX(0);
    }
}

.shake {
    animation: shake 0.4s ease;
}

#admin-tools-zone .secondary-btn {
    background: linear-gradient(135deg, rgba(var(--accent-primary-rgb, 139, 92, 246), 0.15), rgba(var(--accent-secondary-rgb, 79, 70, 229), 0.1));
    border: 1px solid rgba(var(--accent-primary-rgb, 139, 92, 246), 0.3);
}