/* ============================================================
   GDL Chatbot — answer rendering & table styles
   Loaded only on the chatbot page (ai_chatbot.html).
   Accent: NISR blue #2D51A1.
   ============================================================ */

:root {
    --gdl-chat-accent: #2D51A1;
    --gdl-chat-accent-soft: #eef2fa;
    --gdl-chat-rule: #dbe1ec;
    --gdl-chat-zebra: #f7f9fc;
    --gdl-chat-ink: #1f2937;
    --gdl-chat-ink-soft: #4b5563;
}

/* ---------- Answer typography ---------- */

.ai-bubble {
    text-align: left; /* override justify: ragged-right reads better for stats prose */
}

.ai-bubble h1, .ai-bubble h2, .ai-bubble h3, .ai-bubble h4 {
    text-align: left;
    color: var(--gdl-chat-ink);
}
.ai-bubble h1 { font-size: 1.25rem; }
.ai-bubble h2 { font-size: 1.15rem; }
.ai-bubble h3 { font-size: 1.05rem; }
.ai-bubble h4 { font-size: 1rem; }
.ai-bubble h2, .ai-bubble h3 {
    padding-bottom: 0.15em;
    border-bottom: 1px solid var(--gdl-chat-rule);
}
.ai-bubble ul, .ai-bubble ol { text-align: left; }
.ai-bubble p { text-align: left; }
.ai-bubble hr {
    border: none;
    border-top: 1px solid var(--gdl-chat-rule);
    margin: 0.9em 0;
}
.ai-bubble blockquote {
    margin: 0.5em 0;
    padding: 0.25em 0.9em;
    border-left: 3px solid var(--gdl-chat-accent);
    background: var(--gdl-chat-accent-soft);
    border-radius: 0 6px 6px 0;
    color: var(--gdl-chat-ink-soft);
}

/* ---------- Table block: toolbar + scrollable table ---------- */

.gdl-table-block {
    margin: 0.75em 0 1em;
    max-width: 100%;
}

.gdl-table-toolbar {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 6px;
    padding: 0 2px 4px;
}

.gdl-table-scroll {
    overflow-x: auto;
    overflow-y: auto;
    max-height: 480px;
    -webkit-overflow-scrolling: touch;
    border: 1px solid var(--gdl-chat-rule);
    border-radius: 8px;
    background: #ffffff;
}

/* Table itself (overrides the generic .ai-bubble table rules) */
.ai-bubble .gdl-table-scroll table {
    border-collapse: separate;
    border-spacing: 0;
    width: max-content;      /* natural width; container scrolls horizontally */
    min-width: 100%;
    margin: 0;
    font-size: 0.9rem;
    font-variant-numeric: tabular-nums lining-nums;
}

.ai-bubble .gdl-table-scroll th,
.ai-bubble .gdl-table-scroll td {
    border: none;
    border-bottom: 1px solid var(--gdl-chat-rule);
    padding: 7px 12px;
    text-align: left;
    vertical-align: top;
    line-height: 1.4;
    white-space: nowrap;
    max-width: 22em;         /* long label cells wrap instead of stretching */
    overflow-wrap: break-word;
}
/* Frozen first column (row/observation labels) — pinned left while
   scrolling horizontally, on desktop and mobile. */
.ai-bubble .gdl-table-scroll td:first-child,
.ai-bubble .gdl-table-scroll th:first-child {
    position: sticky;
    left: 0;
    z-index: 1;                       /* above scrolling body cells, below sticky header (2) */
    white-space: normal;              /* long labels wrap rather than truncate */
    min-width: 10em;
    background: #ffffff;              /* opaque so body cells never show through */
    box-shadow: 1px 0 0 var(--gdl-chat-rule),           /* crisp divider where the column ends */
                4px 0 6px -4px rgba(15, 23, 42, 0.18);  /* subtle depth shadow */
}

/* Sticky header — header text wraps so columns stay compact */
.ai-bubble .gdl-table-scroll thead th {
    position: sticky;
    top: 0;
    z-index: 2;
    background: var(--gdl-chat-accent-soft);
    color: var(--gdl-chat-ink);
    font-weight: 600;
    font-size: 0.82rem;
    border-bottom: 2px solid var(--gdl-chat-accent);
    white-space: normal;
    vertical-align: bottom;
    min-width: 5.5em;
    max-width: 11em;
}

/* Zebra rows */
.ai-bubble .gdl-table-scroll tbody tr:nth-child(even) td {
    background: var(--gdl-chat-zebra);
}
/* Keep the frozen column's zebra in sync with its row (it must stay opaque,
   so re-assert the striped fill over the column's default white). */
.ai-bubble .gdl-table-scroll tbody tr:nth-child(even) td:first-child {
    background: var(--gdl-chat-zebra);
}
.ai-bubble .gdl-table-scroll tbody tr:last-child td {
    border-bottom: none;
}

/* Top-left cell: header row ∩ frozen column. It inherits sticky top:0 from
   `thead th` and sticky left:0 from `th:first-child`, so it is pinned in
   BOTH directions. Lift it above both sticky layers and keep it opaque. */
.ai-bubble .gdl-table-scroll thead th:first-child {
    z-index: 3;                        /* above sticky header (2) and frozen column (1) */
    background: var(--gdl-chat-accent-soft);
}

/* Numeric columns: right-aligned, tabular figures (set by JS) */
.ai-bubble .gdl-table-scroll td.gdl-num,
.ai-bubble .gdl-table-scroll th.gdl-num {
    text-align: right;
}

/* ---------- Buttons (per-table + answer toolbar) ---------- */

.gdl-answer-toolbar {
    display: flex;
    gap: 6px;
    padding: 2px 16px 0;
    flex-wrap: wrap;
}

.gdl-dl-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font: inherit;
    font-size: 0.78rem;
    color: var(--gdl-chat-ink-soft);
    background: #ffffff;
    border: 1px solid #c7cedd;
    border-radius: 6px;
    padding: 3px 8px;
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease;
}
.gdl-dl-btn:hover {
    background: var(--gdl-chat-accent-soft);
    border-color: var(--gdl-chat-accent);
    color: var(--gdl-chat-accent);
}
.gdl-dl-btn i { font-size: 0.85em; }

/* Visible focus for keyboard users (buttons + scrollable region) */
.gdl-dl-btn:focus-visible,
.gdl-table-scroll:focus-visible {
    outline: 2px solid var(--gdl-chat-accent);
    outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
    .gdl-dl-btn { transition: none; }
    .gdl-table-scroll { scroll-behavior: auto; }
}

/* ---------- Mobile: cap the frozen column so it doesn't eat the viewport ---------- */

@media (max-width: 640px) {
    .ai-bubble .gdl-table-scroll td:first-child,
    .ai-bubble .gdl-table-scroll th:first-child {
        /* border-box so the vw cap includes cell padding and actually binds
           (the table is width:max-content, which otherwise keeps the column
           at its natural, un-capped width). Long labels wrap, never truncate. */
        box-sizing: border-box;
        min-width: 6em;
        max-width: 42vw;   /* leave room for the data columns */
    }
}
