﻿:root {
    --primary: #1f4fd8;
    --bg: #f4f6f8;
    --surface: #ffffff;
    --bot: #eef1f6;
    --text: #1f2937;
    /* ✅ Added */
    --muted: #6b7280;
    --border: rgba(31, 41, 55, 0.12);
    --shadow: 0 10px 30px rgba(17, 24, 39, 0.10);
    --shadow-soft: 0 8px 22px rgba(17, 24, 39, 0.08);
    --radius: 16px;
    --focus: rgba(31, 79, 216, 0.25);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    overflow-x: hidden;
}

body {
    background: var(--bg);
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial;
    overscroll-behavior: none;
}

.chat-app {
    position: relative;
    width: 100%;
    height: 100%;
    background: var(--surface);
}

/* ✅ Added header (does not interfere with your existing structure) */
.chat-header {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px;
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border);
}

.chat-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.brand-logo {
    width: 38px;
    height: 38px;
    border-radius: 12px;
    object-fit: cover;
    box-shadow: var(--shadow-soft);
    border: 1px solid var(--border);
}

.brand-meta {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.brand-title {
    font-weight: 800;
    font-size: 14px;
    line-height: 1.15;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.brand-subtitle {
    font-size: 12px;
    color: var(--muted);
    margin-top: 2px;
}

.clear-btn {
    border: 1px solid var(--border);
    background: transparent;
    border-radius: 12px;
    padding: 8px 10px;
    font-weight: 600;
    cursor: pointer;
    color: var(--text);
}

    .clear-btn:hover {
        background: rgba(31, 79, 216, 0.06);
    }

    .clear-btn:focus {
        outline: none;
        box-shadow: 0 0 0 4px var(--focus);
    }

/* 🔥 BOTTOM → TOP CHAT (your original approach, kept) */
.chat-messages {
    position: absolute;
    top: 64px; /* ✅ adjusted to make room for header */
    left: 0;
    right: 0;
    bottom: calc(76px + env(safe-area-inset-bottom)); /* ✅ safer for your input height */
    display: flex;
    flex-direction: column-reverse;
    padding: 16px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y;
    background: var(--surface); /* changed to white chat background */
}

.message {
    display: flex;
    margin-bottom: 12px;
}

.user {
    justify-content: flex-end;
}

.bot,
.error {
    justify-content: flex-start;
}

.bubble {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 15px;
    line-height: 1.4;
    overflow-wrap: anywhere;
    word-break: break-word;
    /* ✅ Added */
    box-shadow: var(--shadow-soft);
    border: 1px solid rgba(0,0,0,0.03);
}

.user .bubble {
    background: var(--primary);
    color: #fff;
    border-bottom-right-radius: 6px; /* slightly softer */
}

.bot .bubble {
    background: var(--bot);
    color: var(--text);
    border-bottom-left-radius: 6px;
    border: 1px solid var(--border);
}

.error .bubble {
    background: #fee;
    color: #b91c1c;
}

/* ✅ Make logo appear on the left without changing your HTML template */
.bot-row {
    display: flex;
    gap: 10px;
    align-items: flex-start;
    flex-direction: row-reverse; /* ← keeps your existing DOM but flips order */
}

.bot-text {
    flex: 1;
    min-width: 0;
    white-space: normal;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.bot-logo {
    width: 26px;
    height: 26px;
    flex: 0 0 26px;
    object-fit: contain;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: rgba(255,255,255,0.6);
}

.bot-text p {
    margin: 3px 0;
}

.bot-text ul,
.bot-text ol {
    margin: 8px 0 8px 18px;
}

.bot-text li {
    margin-bottom: 6px;
}

.bot-text a {
    color: var(--primary);
    word-break: break-all;
}

.bot-text pre,
.bot-text code {
    white-space: pre-wrap;
    word-break: break-word;
    overflow-x: auto;
    max-width: 100%;
}

.dots i {
    display: inline-block;
    width: 6px;
    height: 6px;
    margin: 0 2px;
    background: #999;
    border-radius: 50%;
    animation: blink 1.4s infinite both;
}

    .dots i:nth-child(2) {
        animation-delay: .2s;
    }

    .dots i:nth-child(3) {
        animation-delay: .4s;
    }

@keyframes blink {
    0%, 80%, 100% {
        opacity: .3;
    }

    40% {
        opacity: 1;
    }
}

.chat-input {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    gap: 12px; /* small gap between input and button */
    padding: 12px 12px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    background: rgba(255, 255, 255, 0.92);
    backdrop-filter: blur(10px);
    border-top: 1px solid var(--border);
}

    /* keep your existing selectors */
    .chat-input input {
        display: block;
        /* let input flex to fill remaining space */
        flex: 1 1 auto;
        min-width: 0; /* allow shrinking in flex container */
        padding: 12px 14px;
        border-radius: 12px;
        border: 1px solid var(--border);
        font-size: 16px;
        background: #fff;
        box-shadow: var(--shadow-soft);
        max-width: none;
        margin: 0; /* flush to container padding */
    }

        .chat-input input:focus {
            outline: none;
            box-shadow: 0 0 0 4px var(--focus), var(--shadow-soft);
            border-color: transparent;
        }

    .chat-input button {
        position: relative;
        background: var(--primary);
        color: white;
        border: none;
        border-radius: 12px;
        padding: 10px 16px;
        font-size: 16px;
        font-weight: 700;
        box-shadow: var(--shadow);
        cursor: pointer;
        height: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        z-index: 10;
        flex: 0 0 auto;
    }

        .chat-input button:hover {
            filter: brightness(1.05);
        }

        .chat-input button:disabled {
            background: #a5b4fc;
            box-shadow: none;
            cursor: not-allowed;
        }

/* ✅ Added hint line */
.hint {
    flex-basis: 100%;
    width: 100%;
    margin-top: 8px;
    font-size: 12px;
    color: var(--muted);
}

.bot-text strong,
.bot-text b {
    font-weight: 700;
}

/* =========================================================
   FIX: Markdown formatting for desktop (tables, headings, lists)
   Keep existing logic; only add missing styles.
========================================================= */

/* Headings were not styled, so they look like normal text */
.bot-text h1, .bot-text h2, .bot-text h3,
.bot-text h4, .bot-text h5, .bot-text h6 {
    font-weight: 800;
    margin: 10px 0 6px;
    line-height: 1.25;
}

/* Bullets may not appear if list-style was reset somewhere */
.bot-text ul {
    list-style-type: disc;
    list-style-position: outside;
}

.bot-text ol {
    list-style-type: decimal;
    list-style-position: outside;
}

/* ✅ TABLE BORDERS for desktop */
.bot-text table {
    border-collapse: collapse;
    width: 100%;
    margin: 8px 0;
    font-size: 14px;
    border: 1px solid #d0d7de; /* outer border */
}

/* grid lines */
.bot-text th, .bot-text td {
    border: 1px solid #d0d7de;
    padding: 8px;
    text-align: left;
    vertical-align: top;
}

.bot-text thead th {
    font-weight: 700;
}

.bot-text tr:nth-child(even) td {
    background: #f6f8fa;
}

/* Allow horizontal scroll for wide tables without breaking layout */
.bot-text table {
    display: block;
    overflow-x: auto;
    max-width: 100%;
}

@media (max-width: 600px) {
    .chat-messages {
        padding: 12px;
        top: 60px;
        bottom: calc(100px + env(safe-area-inset-bottom));
    }

    .bubble {
        max-width: 92%;
        font-size: 14px;
    }

    .bot-logo {
        width: 24px;
        height: 24px;
        flex: 0 0 24px;
    }

    .brand-subtitle {
        display: none;
    }

    /* ensure input and button are touch-friendly */
    .chat-input input {
        padding: 14px 12px;
        font-size: 16px;
    }

    .chat-input button {
        height: 48px;
        padding: 12px 16px;
    }

    /* (kept from your file) mobile table styles */
    .bot-text table {
        border-collapse: collapse;
        width: 100%;
        margin: 8px 0;
        font-size: 14px;
    }

    .bot-text th, .bot-text td {
        border: 1px solid #d0d7de;
        padding: 8px;
        text-align: left;
        vertical-align: top;
    }

    .bot-text thead th {
        font-weight: 700;
    }

    .bot-text tr:nth-child(even) td {
        background: #f6f8fa;
    }

    .bot-text {
        overflow-x: auto;
    }
}
