/* ===== CSS Variables & Reset ===== */
:root {
    --bg-primary: #0a0a0f;
    --bg-secondary: #12121a;
    --bg-tertiary: #1a1a28;
    --bg-card: #16162266;
    --bg-hover: #ffffff0a;
    
    --surface-1: #1e1e30;
    --surface-2: #252540;
    --surface-3: #2f2f4a;
    
    --text-primary: #f0f0f5;
    --text-secondary: #a0a0b8;
    --text-muted: #6a6a82;
    
    --accent: #ff6b35;
    --accent-light: #ff8a5c;
    --accent-dark: #d44a15;
    --accent-glow: #ff6b3530;
    
    --metro-red: #d62828;
    --metro-orange: #ff6b35;
    --metro-gold: #f7b731;
    
    --gradient-main: linear-gradient(135deg, #ff6b35, #d62828);
    --gradient-subtle: linear-gradient(135deg, #ff6b3520, #d6282820);
    
    --border: #ffffff12;
    --border-hover: #ffffff25;
    
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 20px;
    --radius-full: 9999px;
    
    --shadow-sm: 0 2px 8px rgba(0,0,0,0.3);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.4);
    --shadow-lg: 0 8px 32px rgba(0,0,0,0.5);
    --shadow-glow: 0 0 30px rgba(255,107,53,0.15);
    
    --sidebar-width: 280px;
    --header-height: 64px;
    
    --font: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    
    --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    font-size: 15px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

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

button {
    font-family: inherit;
    cursor: pointer;
    border: none;
    background: none;
    color: inherit;
}

textarea {
    font-family: inherit;
    color: inherit;
    resize: none;
}

/* ===== Layout ===== */
.app-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

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

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 18px;
    border-bottom: 1px solid var(--border);
}

.logo {
    display: flex;
    align-items: center;
    gap: 10px;
}

.logo-icon {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    background: var(--gradient-main);
    box-shadow: var(--shadow-glow);
}

.logo-text {
    font-weight: 700;
    font-size: 1.05rem;
    background: var(--gradient-main);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.sidebar-close {
    display: none;
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm);
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    font-size: 16px;
}
.sidebar-close:hover {
    background: var(--bg-hover);
}

.new-chat-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin: 14px 14px 10px;
    padding: 10px 16px;
    border-radius: var(--radius-md);
    border: 1px dashed var(--border-hover);
    color: var(--text-secondary);
    font-size: 0.88rem;
    font-weight: 500;
    transition: all var(--transition);
}
.new-chat-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: var(--accent-glow);
}

.sidebar-nav {
    flex: 1;
    overflow-y: auto;
    padding: 6px 10px;
}

.nav-section-title {
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
    padding: 10px 10px 6px;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 9px 12px;
    border-radius: var(--radius-sm);
    font-size: 0.88rem;
    color: var(--text-secondary);
    text-align: left;
    transition: all var(--transition);
    margin-bottom: 2px;
}
.nav-item:hover {
    background: var(--surface-1);
    color: var(--text-primary);
}
.nav-item .nav-icon {
    font-size: 1rem;
    width: 22px;
    text-align: center;
}

.sidebar-footer {
    padding: 14px 18px;
    border-top: 1px solid var(--border);
}
.footer-info {
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.ai-badge {
    font-size: 0.75rem;
    font-weight: 600;
    padding: 4px 10px;
    border-radius: var(--radius-full);
    background: var(--gradient-subtle);
    border: 1px solid var(--accent-glow);
    color: var(--accent-light);
}
.version {
    font-size: 0.72rem;
    color: var(--text-muted);
}

/* ===== Sidebar Overlay (mobile) ===== */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    backdrop-filter: blur(4px);
    z-index: 90;
}

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

/* ===== Chat Header ===== */
.chat-header {
    height: var(--header-height);
    min-height: var(--header-height);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-secondary);
    backdrop-filter: blur(10px);
}

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

.menu-btn {
    display: none;
    width: 38px;
    height: 38px;
    border-radius: var(--radius-sm);
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
}
.menu-btn:hover {
    background: var(--surface-1);
}

.header-title h1 {
    font-size: 1.05rem;
    font-weight: 700;
}
.header-status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.75rem;
    color: var(--text-muted);
}
.status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #22c55e;
    animation: pulse-dot 2s ease-in-out infinite;
}
@keyframes pulse-dot {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

.header-btn {
    width: 38px;
    height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    transition: all var(--transition);
}
.header-btn:hover {
    background: var(--surface-1);
    color: var(--text-primary);
}

/* ===== Chat Messages ===== */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 0;
    scroll-behavior: smooth;
}
.chat-messages::-webkit-scrollbar {
    width: 6px;
}
.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}
.chat-messages::-webkit-scrollbar-thumb {
    background: var(--surface-3);
    border-radius: 3px;
}

/* ===== Welcome Screen ===== */
.welcome-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 24px;
    min-height: 100%;
    animation: fadeUp 0.6s ease;
}
@keyframes fadeUp {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

.welcome-hero {
    text-align: center;
    margin-bottom: 36px;
    max-width: 480px;
}
.welcome-icon-wrap {
    position: relative;
    display: inline-flex;
    margin-bottom: 20px;
}
.welcome-icon {
    font-size: 3.5rem;
    position: relative;
    z-index: 1;
}
.welcome-pulse {
    position: absolute;
    inset: -12px;
    border-radius: 50%;
    background: var(--accent-glow);
    animation: welcome-pulse 3s ease-in-out infinite;
}
@keyframes welcome-pulse {
    0%, 100% { transform: scale(1); opacity: 0.6; }
    50% { transform: scale(1.3); opacity: 0.1; }
}

.welcome-hero h2 {
    font-size: 1.8rem;
    font-weight: 800;
    margin-bottom: 10px;
}
.welcome-hero p {
    color: var(--text-secondary);
    line-height: 1.6;
    font-size: 0.95rem;
}

.suggestion-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 10px;
    width: 100%;
    max-width: 640px;
}

.suggestion-card {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    background: var(--bg-card);
    backdrop-filter: blur(8px);
    cursor: pointer;
    transition: all var(--transition);
    text-align: left;
    font-size: 0.88rem;
    color: var(--text-secondary);
}
.suggestion-card:hover {
    border-color: var(--accent);
    background: var(--accent-glow);
    color: var(--text-primary);
    transform: translateY(-2px);
    box-shadow: var(--shadow-glow);
}
.suggestion-card .s-icon {
    font-size: 1.3rem;
    flex-shrink: 0;
}

/* ===== Message Bubbles ===== */
.message-row {
    padding: 4px 24px;
    animation: msgIn 0.3s ease;
}
@keyframes msgIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.message-row.user-row {
    display: flex;
    justify-content: flex-end;
}
.message-row.bot-row {
    display: flex;
    justify-content: flex-start;
}

.message-bubble {
    max-width: 720px;
    padding: 12px 18px;
    border-radius: var(--radius-lg);
    line-height: 1.7;
    font-size: 0.92rem;
    word-break: break-word;
}

.user-bubble {
    background: var(--gradient-main);
    color: white;
    border-bottom-right-radius: 4px;
    box-shadow: 0 2px 12px rgba(255,107,53,0.3);
}

.bot-bubble {
    background: var(--surface-1);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
    color: var(--text-primary);
}

.bot-bubble-wrapper {
    display: flex;
    gap: 12px;
    align-items: flex-start;
    max-width: 780px;
}

.bot-avatar {
    width: 32px;
    height: 32px;
    min-width: 32px;
    border-radius: var(--radius-sm);
    background: var(--gradient-main);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9rem;
    margin-top: 2px;
    box-shadow: var(--shadow-glow);
}

/* Message content formatting */
.bot-bubble h1, .bot-bubble h2, .bot-bubble h3 {
    margin: 12px 0 6px;
    font-weight: 700;
}
.bot-bubble h2 { font-size: 1rem; }
.bot-bubble h3 { font-size: 0.95rem; }

.bot-bubble p {
    margin: 6px 0;
}

.bot-bubble ul, .bot-bubble ol {
    margin: 6px 0;
    padding-left: 20px;
}
.bot-bubble li {
    margin: 3px 0;
}

.bot-bubble strong {
    color: var(--accent-light);
    font-weight: 600;
}

.bot-bubble code {
    background: var(--bg-primary);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.85em;
}

.bot-bubble pre {
    background: var(--bg-primary);
    padding: 12px;
    border-radius: var(--radius-sm);
    overflow-x: auto;
    margin: 8px 0;
}

.bot-bubble a {
    color: var(--accent-light);
    text-decoration: underline;
}

.bot-bubble table {
    width: 100%;
    border-collapse: collapse;
    margin: 10px 0;
    font-size: 0.85rem;
}
.bot-bubble th, .bot-bubble td {
    padding: 6px 10px;
    border: 1px solid var(--border);
    text-align: left;
}
.bot-bubble th {
    background: var(--surface-2);
    font-weight: 600;
}

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

/* ===== Chat Input ===== */
.chat-input-area {
    padding: 12px 20px 16px;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
}

.input-container {
    max-width: 780px;
    margin: 0 auto;
}

.input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--surface-1);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 6px 8px 6px 18px;
    transition: border-color var(--transition), box-shadow var(--transition);
}
.input-wrapper:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

.input-wrapper textarea {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    font-size: 0.93rem;
    line-height: 1.5;
    padding: 8px 0;
    max-height: 150px;
    overflow-y: auto;
}
.input-wrapper textarea::placeholder {
    color: var(--text-muted);
}

.send-btn {
    width: 38px;
    height: 38px;
    min-width: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-md);
    background: var(--gradient-main);
    color: white;
    transition: all var(--transition);
    opacity: 0.4;
}
.send-btn:not(:disabled) {
    opacity: 1;
}
.send-btn:not(:disabled):hover {
    transform: scale(1.05);
    box-shadow: var(--shadow-glow);
}
.send-btn:disabled {
    cursor: not-allowed;
}

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

/* ===== Settings Button ===== */
.settings-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 9px 12px;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 10px;
    transition: all var(--transition);
}
.settings-btn:hover {
    background: var(--surface-1);
    color: var(--text-primary);
}

/* ===== Modal ===== */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.7);
    backdrop-filter: blur(6px);
    z-index: 200;
    align-items: center;
    justify-content: center;
}
.modal-overlay.show {
    display: flex;
}
.modal-content {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    width: 90%;
    max-width: 480px;
    box-shadow: var(--shadow-lg);
    animation: fadeUp 0.3s ease;
}
.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 24px 16px;
    border-bottom: 1px solid var(--border);
}
.modal-header h2 {
    font-size: 1.1rem;
    font-weight: 700;
}
.modal-close {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 16px;
}
.modal-close:hover {
    background: var(--surface-1);
}
.modal-body {
    padding: 20px 24px 24px;
}
.setting-group {
    margin-bottom: 20px;
}
.setting-group:last-child {
    margin-bottom: 0;
}
.setting-label {
    display: block;
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 10px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

/* Provider Cards */
.provider-cards {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.provider-card {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    background: var(--bg-card);
    transition: all var(--transition);
    text-align: left;
    width: 100%;
}
.provider-card:hover {
    border-color: var(--border-hover);
    background: var(--surface-1);
}
.provider-card.active {
    border-color: var(--accent);
    background: var(--accent-glow);
}
.provider-icon {
    font-size: 1.5rem;
    width: 36px;
    text-align: center;
}
.provider-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.provider-name {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-primary);
}
.provider-desc {
    font-size: 0.75rem;
    color: var(--text-muted);
}
.provider-check {
    display: none;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--gradient-main);
    color: white;
    font-size: 0.75rem;
    font-weight: 700;
    align-items: center;
    justify-content: center;
}
.provider-card.active .provider-check {
    display: flex;
}

/* Gemini Key Input */
.key-input-wrap {
    display: flex;
    gap: 8px;
}
.setting-input {
    flex: 1;
    background: var(--surface-1);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 9px 14px;
    color: var(--text-primary);
    font-size: 0.88rem;
    font-family: var(--font);
    outline: none;
    transition: border-color var(--transition);
}
.setting-input:focus {
    border-color: var(--accent);
}
.setting-input::placeholder {
    color: var(--text-muted);
}
.key-save-btn {
    padding: 9px 18px;
    border-radius: var(--radius-sm);
    background: var(--gradient-main);
    color: white;
    font-weight: 600;
    font-size: 0.85rem;
    transition: all var(--transition);
    white-space: nowrap;
}
.key-save-btn:hover {
    transform: scale(1.03);
    box-shadow: var(--shadow-glow);
}
.key-status {
    margin-top: 8px;
    font-size: 0.78rem;
    color: var(--text-muted);
}
.key-status.success {
    color: #22c55e;
}
.key-status.error {
    color: #ef4444;
}

/* ===== Chat History List ===== */
.history-list {
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.history-loading {
    font-size: 0.8rem;
    color: var(--text-muted);
    padding: 8px 12px;
}
.history-item {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    font-size: 0.82rem;
    color: var(--text-secondary);
    text-align: left;
    transition: all var(--transition);
    cursor: pointer;
}
.history-item:hover {
    background: var(--surface-1);
    color: var(--text-primary);
}
.history-item.active {
    background: var(--accent-glow);
    color: var(--accent-light);
}
.history-item-icon {
    font-size: 0.85rem;
    flex-shrink: 0;
}
.history-item-text {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.history-item-count {
    font-size: 0.7rem;
    color: var(--text-muted);
    background: var(--surface-2);
    padding: 2px 6px;
    border-radius: var(--radius-full);
    flex-shrink: 0;
}
.history-empty {
    font-size: 0.8rem;
    color: var(--text-muted);
    padding: 8px 12px;
    font-style: italic;
}

/* ===== Provider Toggle (header) ===== */
.provider-toggle {
    font-size: 1.1rem;
    position: relative;
}
.provider-toggle::after {
    content: '';
    position: absolute;
    bottom: 4px;
    right: 4px;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #22c55e;
    border: 1.5px solid var(--bg-secondary);
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        transform: translateX(-100%);
        box-shadow: var(--shadow-lg);
    }
    .sidebar.open {
        transform: translateX(0);
    }
    .sidebar-close {
        display: flex;
    }
    .sidebar-overlay.show {
        display: block;
    }
    .menu-btn {
        display: flex;
    }
    .message-row {
        padding: 4px 14px;
    }
    .chat-input-area {
        padding: 10px 14px 14px;
    }
    .suggestion-grid {
        grid-template-columns: 1fr;
    }
    .welcome-hero h2 {
        font-size: 1.4rem;
    }
}

@media (max-width: 480px) {
    .welcome-screen {
        padding: 24px 16px;
    }
    .message-bubble {
        max-width: 92%;
    }
}

/* ===== Registration Modal ===== */
.register-modal {
    max-width: 420px;
}
.register-header {
    text-align: center;
    padding: 28px 24px 20px;
}
.register-logo {
    margin-bottom: 16px;
}
.register-header h2 {
    font-size: 1.3rem;
    font-weight: 800;
    margin-bottom: 6px;
    background: var(--gradient-main);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}
.register-header p {
    color: var(--text-muted);
    font-size: 0.88rem;
}
.register-form {
    padding: 0 24px 28px;
}
.form-group {
    margin-bottom: 16px;
}
.form-group label {
    display: block;
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 6px;
}
.form-group .required {
    color: #ef4444;
}
.form-group .optional {
    color: var(--text-muted);
    font-weight: 400;
}
.register-btn {
    width: 100%;
    padding: 12px 20px;
    border-radius: var(--radius-md);
    background: var(--gradient-main);
    color: white;
    font-weight: 700;
    font-size: 0.95rem;
    border: none;
    cursor: pointer;
    transition: all var(--transition);
}
.register-btn:hover {
    transform: scale(1.02);
    box-shadow: var(--shadow-glow);
}

/* ===== Admin Message Bubble ===== */
.admin-avatar {
    background: linear-gradient(135deg, #3b82f6, #2563eb) !important;
    box-shadow: 0 0 20px rgba(59, 130, 246, 0.2) !important;
}
.admin-bubble {
    border-color: #3b82f640 !important;
    background: linear-gradient(135deg, #1e3a5f, #1a2a4a) !important;
}
.admin-tag {
    font-size: 0.72rem;
    font-weight: 700;
    color: #60a5fa;
    margin-bottom: 6px;
    display: flex;
    align-items: center;
    gap: 4px;
}

/* ===== System Messages ===== */
.system-row {
    display: flex;
    justify-content: center;
    padding: 8px 24px;
}
.system-message {
    font-size: 0.8rem;
    color: var(--metro-gold);
    background: rgba(247, 183, 49, 0.08);
    padding: 6px 16px;
    border-radius: var(--radius-full);
    border: 1px solid rgba(247, 183, 49, 0.2);
}

/* ===== User Badge ===== */
.user-badge {
    font-size: 0.75rem;
    font-weight: 600;
    padding: 4px 10px;
    border-radius: var(--radius-full);
    background: var(--surface-1);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 140px;
}
