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

:root {
    --bg: #f7f7f8;
    --sidebar-bg: #1a1a2e;
    --sidebar-text: #e0e0e0;
    --sidebar-hover: #16213e;
    --chat-bg: #ffffff;
    --msg-user-bg: #e8f0fe;
    --msg-assistant-bg: #f0f0f0;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --text: #1a1a1a;
    --text-muted: #6b7280;
    --border: #e5e7eb;
    --danger: #dc2626;
    --radius: 8px;
    --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

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

/* ── Login page ──────────────────────────────────────────────────────────── */
.login-container {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 1rem;
}

.login-card {
    background: var(--chat-bg);
    border-radius: 12px;
    padding: 2.5rem;
    max-width: 400px;
    width: 100%;
    text-align: center;
    box-shadow: 0 4px 24px rgba(0,0,0,0.08);
}

.login-card h1 {
    font-size: 1.5rem;
    margin-bottom: 0.5rem;
}

.login-subtitle {
    color: var(--text-muted);
    margin-bottom: 1.5rem;
    font-size: 0.9rem;
}

.flash {
    padding: 0.75rem 1rem;
    border-radius: var(--radius);
    margin-bottom: 1rem;
    font-size: 0.85rem;
}
.flash-error { background: #fee2e2; color: var(--danger); }
.flash-info { background: #dbeafe; color: var(--primary); }

.google-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1.5rem;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.95rem;
    color: var(--text);
    text-decoration: none;
    cursor: pointer;
    transition: box-shadow 0.15s;
}
.google-btn:hover { box-shadow: 0 2px 8px rgba(0,0,0,0.12); }

/* ── App layout ──────────────────────────────────────────────────────────── */
.app-layout {
    display: flex;
    height: 100vh;
}

/* ── Sidebar ─────────────────────────────────────────────────────────────── */
.sidebar {
    width: 280px;
    min-width: 280px;
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.sidebar-header {
    padding: 1rem;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}

.sidebar-header h2 {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 0.75rem;
}

.new-chat-btn {
    width: 100%;
    padding: 0.6rem;
    background: rgba(255,255,255,0.1);
    border: 1px solid rgba(255,255,255,0.2);
    border-radius: var(--radius);
    color: var(--sidebar-text);
    cursor: pointer;
    font-size: 0.85rem;
    transition: background 0.15s;
}
.new-chat-btn:hover { background: rgba(255,255,255,0.2); }

.conversation-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
}

.conv-item {
    padding: 0.6rem 0.75rem;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 0.85rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 2px;
}
.conv-item:hover { background: var(--sidebar-hover); }
.conv-item.active { background: var(--sidebar-hover); }

.conv-item .conv-title { flex: 1; overflow: hidden; text-overflow: ellipsis; }

.conv-delete {
    display: none;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 0.2rem;
    font-size: 0.75rem;
}
.conv-item:hover .conv-delete { display: block; }

.sidebar-footer {
    padding: 1rem;
    border-top: 1px solid rgba(255,255,255,0.1);
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.8rem;
}

.sidebar-footer-links { display: flex; gap: 0.75rem; }
.logout-link {
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.8rem;
}
.logout-link:hover { color: #fff; }

.sidebar-toggle {
    display: none;
    position: fixed;
    top: 0.75rem;
    left: 0.75rem;
    z-index: 100;
    background: var(--sidebar-bg);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    padding: 0.5rem 0.75rem;
    font-size: 1.2rem;
    cursor: pointer;
}

/* ── Main chat ───────────────────────────────────────────────────────────── */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--chat-bg);
}

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem 1rem 0;
}

/* Welcome screen */
.welcome-message {
    max-width: 640px;
    margin: 4rem auto;
    text-align: center;
}
.welcome-message h2 {
    font-size: 1.4rem;
    margin-bottom: 0.5rem;
}
.welcome-message p {
    color: var(--text-muted);
    margin-bottom: 1.5rem;
}
.example-questions {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    align-items: center;
}
.example-q {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.6rem 1rem;
    font-size: 0.85rem;
    cursor: pointer;
    color: var(--text);
    max-width: 500px;
    width: 100%;
    text-align: left;
    transition: background 0.15s;
}
.example-q:hover { background: var(--msg-user-bg); }

/* Message bubbles */
.message {
    max-width: 800px;
    margin: 0 auto 1rem;
    padding: 1rem 1.25rem;
    border-radius: var(--radius);
    line-height: 1.6;
    font-size: 0.92rem;
}
.message-user {
    background: var(--msg-user-bg);
}
.message-assistant {
    background: var(--msg-assistant-bg);
}
.message-label {
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--text-muted);
    margin-bottom: 0.4rem;
}
.message-content { word-wrap: break-word; }
.message-content p { margin-bottom: 0.5rem; }
.message-content p:last-child { margin-bottom: 0; }
.message-content pre {
    background: #1e1e1e;
    color: #d4d4d4;
    padding: 0.75rem 1rem;
    border-radius: var(--radius);
    overflow-x: auto;
    font-size: 0.82rem;
    font-family: var(--mono);
    margin: 0.5rem 0;
}
.message-content code {
    font-family: var(--mono);
    font-size: 0.85em;
    background: rgba(0,0,0,0.06);
    padding: 0.15em 0.35em;
    border-radius: 3px;
}
.message-content pre code {
    background: none;
    padding: 0;
}
.message-content table {
    border-collapse: collapse;
    width: 100%;
    margin: 0.5rem 0;
    font-size: 0.85rem;
}
.message-content th, .message-content td {
    border: 1px solid var(--border);
    padding: 0.4rem 0.6rem;
    text-align: left;
}
.message-content th { background: var(--bg); font-weight: 600; }
.message-content img {
    max-width: 100%;
    border-radius: var(--radius);
    margin: 0.5rem 0;
}
.message-content ul, .message-content ol {
    margin: 0.5rem 0 0.5rem 1.5rem;
}

/* File attachments */
.file-attachment {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.82rem;
    color: var(--primary);
    text-decoration: none;
    margin-top: 0.5rem;
}
.file-attachment:hover { background: var(--bg); }

/* Loading indicator */
.typing-indicator {
    max-width: 800px;
    margin: 0 auto 1rem;
    padding: 1rem 1.25rem;
    background: var(--msg-assistant-bg);
    border-radius: var(--radius);
}
.status-row {
    display: flex;
    align-items: center;
    gap: 8px;
}
.status-text {
    font-size: 0.82rem;
    color: var(--text-muted);
}
.typing-dots {
    display: flex;
    gap: 4px;
}
.typing-dots span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-muted);
    animation: typing 1.4s infinite both;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing {
    0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
    40% { opacity: 1; transform: scale(1); }
}

/* ── Input area ──────────────────────────────────────────────────────────── */
.input-area {
    padding: 0.75rem 1rem 1rem;
    border-top: 1px solid var(--border);
}

.input-row {
    display: flex;
    gap: 0.5rem;
    max-width: 800px;
    margin: 0 auto;
    align-items: flex-end;
}

#messageInput {
    flex: 1;
    padding: 0.75rem 1rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.92rem;
    font-family: var(--font);
    resize: none;
    max-height: 200px;
    line-height: 1.5;
    outline: none;
    transition: border-color 0.15s;
}
#messageInput:focus { border-color: var(--primary); }

#sendBtn {
    padding: 0.7rem;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
    min-width: 40px;
    min-height: 40px;
}
#sendBtn:hover { background: var(--primary-hover); }
#sendBtn:disabled { background: #93c5fd; cursor: not-allowed; }

/* ── Admin pages ────────────────────────────────────────────────────────── */
.admin-container {
    max-width: 900px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
}
.admin-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 1.5rem;
}
.admin-header h1 { font-size: 1.4rem; margin-bottom: 0.25rem; }
.back-link { color: var(--primary); text-decoration: none; font-size: 0.85rem; }
.back-link:hover { text-decoration: underline; }

.admin-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.88rem;
    background: var(--chat-bg);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}
.admin-table th, .admin-table td {
    padding: 0.6rem 0.8rem;
    text-align: left;
    border-bottom: 1px solid var(--border);
}
.admin-table th { background: var(--bg); font-weight: 600; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--text-muted); }
.admin-table tr.inactive td { opacity: 0.5; }
.admin-table .actions { white-space: nowrap; display: flex; gap: 0.4rem; }

.badge {
    display: inline-block;
    padding: 0.15rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.02em;
}
.badge-admin { background: #dbeafe; color: #1e40af; }
.badge-viewer { background: #f3f4f6; color: #6b7280; }
.badge-active { background: #d1fae5; color: #065f46; }
.badge-inactive { background: #fee2e2; color: #991b1b; }

.btn {
    display: inline-block;
    padding: 0.5rem 1rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.85rem;
    cursor: pointer;
    text-decoration: none;
    color: var(--text);
    background: var(--chat-bg);
    transition: background 0.15s;
}
.btn:hover { background: var(--bg); }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { background: #fff; color: #dc2626; border-color: #fca5a5; }
.btn-danger:hover { background: #fee2e2; }
.btn-sm { padding: 0.3rem 0.6rem; font-size: 0.78rem; }

.form-card {
    background: var(--chat-bg);
    padding: 1.5rem;
    border-radius: var(--radius);
    box-shadow: 0 1px 4px rgba(0,0,0,0.06);
    max-width: 500px;
    margin-top: 1rem;
}
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; font-size: 0.85rem; font-weight: 500; margin-bottom: 0.3rem; }
.form-group input[type="email"],
.form-group input[type="text"],
.form-group select {
    width: 100%;
    padding: 0.6rem 0.8rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-family: var(--font);
}
.form-group input:focus, .form-group select:focus { outline: none; border-color: var(--primary); }
.form-group input[readonly] { background: var(--bg); color: var(--text-muted); }
.checkbox-group { display: flex; align-items: center; gap: 0.5rem; }
.checkbox-group label { margin-bottom: 0; }

/* ── Mobile ──────────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: -300px;
        top: 0;
        bottom: 0;
        z-index: 50;
        transition: left 0.25s ease;
    }
    .sidebar.open { left: 0; }
    .sidebar-toggle { display: block; }
    .chat-main { width: 100%; }
    .welcome-message { margin: 2rem 1rem; }
}
