/* ==========================================================================
   CCPL RAG — Design system
   ========================================================================== */

:root {
    --bg: #f4f2ec;
    --bg-soft: #ebe9e0;
    --ink: #1a2420;
    --ink-soft: #34433c;
    --muted: #66756c;
    --muted-soft: #8b978f;
    --brand: #0b6b57;
    --brand-deep: #063f34;
    --brand-bright: #128a6f;
    --brand-soft: #e3efe8;
    --accent: #bd6a34;
    --accent-deep: #96501f;
    --accent-soft: #f5e6d8;
    --surface: #ffffff;
    --surface-alt: #fbfaf7;
    --line: rgba(26, 36, 32, 0.11);
    --line-soft: rgba(26, 36, 32, 0.06);
    --danger: #ae3b34;
    --danger-deep: #862c27;
    --danger-soft: #f7e3e1;
    --success: #1f7a4d;
    --success-soft: #e2f1e7;
    --warning: #a15c00;
    --warning-soft: #f7ecd9;
    --shadow-sm: 0 2px 10px rgba(20, 30, 26, 0.06);
    --shadow: 0 18px 44px rgba(20, 30, 26, 0.10);
    --shadow-lg: 0 34px 84px rgba(20, 30, 26, 0.18);
    --radius-sm: 10px;
    --radius: 16px;
    --radius-lg: 24px;
    --font: "DM Sans", sans-serif;
    --display: "Fraunces", Georgia, serif;
}

* { box-sizing: border-box; }

html { color-scheme: light; }

body {
    margin: 0;
    min-height: 100vh;
    font-family: var(--font);
    color: var(--ink);
    -webkit-font-smoothing: antialiased;
    background:
        radial-gradient(1100px 560px at 10% -10%, #ffffff 0%, transparent 55%),
        radial-gradient(900px 520px at 105% -6%, color-mix(in srgb, var(--brand) 10%, transparent) 0%, transparent 50%),
        radial-gradient(700px 480px at 50% 115%, color-mix(in srgb, var(--accent) 7%, transparent) 0%, transparent 55%),
        linear-gradient(165deg, var(--bg), var(--bg-soft));
}

::selection { background: color-mix(in srgb, var(--brand) 25%, transparent); }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
    background: color-mix(in srgb, var(--ink) 16%, transparent);
    border-radius: 999px;
    border: 2px solid transparent;
}
::-webkit-scrollbar-thumb:hover { background: color-mix(in srgb, var(--ink) 26%, transparent); }

.shell {
    max-width: 1120px;
    margin: 0 auto;
    padding: 2.25rem 1.5rem 4rem;
}

/* ——— Top bar ——— */

.topbar {
    position: relative;
    background: color-mix(in srgb, #fff 94%, var(--bg));
    border-bottom: 1px solid var(--line);
}

.topbar::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    bottom: -1px;
    height: 3px;
    background: linear-gradient(90deg, var(--brand) 0%, var(--brand-bright) 45%, var(--accent) 100%);
    opacity: 0.85;
}

.topbar-inner {
    max-width: 1120px;
    margin: 0 auto;
    padding: 0.95rem 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
}

.brand {
    text-decoration: none;
    color: inherit;
    display: flex;
    align-items: center;
    gap: 0.85rem;
    border-radius: 12px;
    transition: opacity 140ms ease;
}

.brand:hover { opacity: 0.82; }

.brand-logo {
    display: block;
    height: 50px;
    width: auto;
    border-radius: 10px;
}

.brand-text {
    display: grid;
    gap: 0.05rem;
}

.brand-mark {
    font-family: var(--display);
    font-size: 1.28rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    color: var(--brand-deep);
    line-height: 1.15;
}

.brand-sub {
    color: var(--muted);
    font-size: 0.82rem;
    font-weight: 500;
}

.topbar-actions {
    display: flex;
    gap: 0.6rem;
    align-items: center;
}

/* ——— Generic layout / typography ——— */

.main { min-height: 60vh; }

.eyebrow {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    font-size: 0.74rem;
    color: var(--brand);
    margin: 0 0 0.6rem;
    font-weight: 700;
}

.eyebrow::before {
    content: "";
    width: 0.4rem;
    height: 0.4rem;
    border-radius: 50%;
    background: var(--accent);
}

h1 {
    font-family: var(--display);
    font-weight: 700;
    font-size: clamp(1.9rem, 3.4vw, 2.6rem);
    margin: 0 0 0.5rem;
    line-height: 1.12;
    color: var(--ink);
    letter-spacing: -0.01em;
}

.lede {
    color: var(--muted);
    margin: 0;
    max-width: 42rem;
    font-size: 1.02rem;
    line-height: 1.5;
}

.section-head { margin-bottom: 2rem; }

.empty {
    color: var(--muted);
    padding: 2rem 0;
}

/* ——— Home / topic grid ——— */

.topic-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 1.1rem;
}

.topic-card {
    position: relative;
    display: grid;
    gap: 0.5rem;
    padding: 1.5rem 1.5rem 1.4rem;
    border-radius: var(--radius-lg);
    background: var(--surface);
    border: 1px solid var(--line);
    box-shadow: var(--shadow-sm);
    text-decoration: none;
    color: inherit;
    overflow: hidden;
    transition: transform 200ms cubic-bezier(0.22, 1, 0.36, 1), box-shadow 200ms ease, border-color 200ms ease;
}

.topic-card::before {
    content: "";
    position: absolute;
    inset: 0;
    background: linear-gradient(140deg, color-mix(in srgb, var(--brand) 5%, transparent), transparent 55%);
    opacity: 0;
    transition: opacity 200ms ease;
}

.topic-card:hover {
    transform: translateY(-3px);
    border-color: color-mix(in srgb, var(--brand) 35%, var(--line));
    box-shadow: var(--shadow);
}

.topic-card:hover::before { opacity: 1; }

.topic-avatar {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.6rem;
    height: 2.6rem;
    border-radius: 12px;
    background: linear-gradient(135deg, var(--brand), var(--brand-bright));
    color: #fff;
    font-family: var(--display);
    font-weight: 700;
    font-size: 1.05rem;
    margin-bottom: 0.35rem;
    box-shadow: 0 6px 16px color-mix(in srgb, var(--brand) 35%, transparent);
}

.topic-slug {
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.09em;
    color: var(--brand);
    text-transform: uppercase;
}

.topic-card h2 {
    font-family: var(--display);
    font-size: 1.32rem;
    margin: 0;
    line-height: 1.2;
}

.topic-card p {
    margin: 0;
    color: var(--muted);
    font-size: 0.93rem;
    line-height: 1.45;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.topic-cta {
    position: relative;
    margin-top: 0.65rem;
    font-weight: 700;
    font-size: 0.88rem;
    color: var(--brand-deep);
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
}

.topic-cta::after {
    content: "→";
    display: inline-block;
    transition: transform 200ms ease;
}

.topic-card:hover .topic-cta::after {
    transform: translateX(3px);
}

/* ——— Login ——— */

.login-panel {
    min-height: 72vh;
    display: grid;
    place-items: center;
}

.login-card {
    position: relative;
    width: min(100%, 440px);
    padding: 2.35rem 2.15rem 2.15rem;
    border-radius: var(--radius-lg);
    background: var(--surface);
    border: 1px solid var(--line);
    box-shadow: var(--shadow-lg);
    text-align: center;
    overflow: hidden;
}

.login-card::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: linear-gradient(90deg, var(--brand), var(--brand-bright), var(--accent));
}

.login-logo {
    display: block;
    width: min(100%, 240px);
    height: auto;
    margin: 0 auto 1.5rem;
}

.login-card .eyebrow { justify-content: center; }
.login-card h1 { text-align: center; }
.login-card .lede { max-width: none; text-align: center; margin: 0 auto 0.5rem; }

.stack { display: grid; gap: 1.05rem; margin-top: 1.75rem; text-align: left; }

label {
    display: grid;
    gap: 0.4rem;
    font-size: 0.86rem;
    font-weight: 600;
    color: var(--ink-soft);
}

input, textarea {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 0.75rem 0.95rem;
    font: inherit;
    background: var(--surface-alt);
    color: var(--ink);
    transition: border-color 140ms ease, background 140ms ease, box-shadow 140ms ease;
}

input:focus, textarea:focus {
    outline: none;
    border-color: color-mix(in srgb, var(--brand) 45%, var(--line));
    background: #fff;
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 14%, transparent);
}

textarea { resize: vertical; }

.login-card .btn-primary {
    width: 100%;
    margin-top: 0.3rem;
    padding: 0.85rem 1.15rem;
}

/* ——— Buttons ——— */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    border-radius: 999px;
    padding: 0.72rem 1.2rem;
    font: inherit;
    font-weight: 600;
    font-size: 0.94rem;
    text-decoration: none;
    border: 1px solid transparent;
    cursor: pointer;
    transition: background 150ms ease, border-color 150ms ease, color 150ms ease, transform 150ms ease;
}

.btn:active { transform: translateY(1px); }

.btn-primary {
    background: linear-gradient(135deg, var(--brand), var(--brand-bright));
    color: #fff;
    box-shadow: 0 8px 20px color-mix(in srgb, var(--brand) 32%, transparent);
}

.btn-primary:hover {
    background: linear-gradient(135deg, var(--brand-deep), var(--brand));
    box-shadow: 0 10px 24px color-mix(in srgb, var(--brand) 40%, transparent);
}

.btn-ghost {
    background: var(--surface);
    border-color: var(--line);
    color: var(--ink-soft);
}

.btn-ghost:hover {
    border-color: color-mix(in srgb, var(--brand) 35%, var(--line));
    background: color-mix(in srgb, var(--brand) 5%, #fff);
    color: var(--brand-deep);
}

.btn-sm {
    padding: 0.48rem 0.95rem;
    font-size: 0.86rem;
}

.btn-danger-ghost {
    background: var(--surface);
    border-color: color-mix(in srgb, var(--danger) 30%, var(--line));
    color: var(--danger);
}

.btn-danger-ghost:hover {
    background: var(--danger-soft);
    border-color: var(--danger);
}

.btn-danger {
    background: var(--danger);
    color: #fff;
}

.btn-danger:hover { background: var(--danger-deep); }

.linkish {
    appearance: none;
    border: 0;
    background: none;
    padding: 0;
    color: var(--brand-deep);
    font: inherit;
    font-weight: 600;
    text-decoration: underline;
    text-underline-offset: 2px;
    cursor: pointer;
}

/* ——— Alerts / flashes ——— */

.alert {
    background: var(--danger-soft);
    color: var(--danger-deep);
    border: 1px solid color-mix(in srgb, var(--danger) 25%, transparent);
    border-radius: 12px;
    padding: 0.8rem 1rem;
    font-size: 0.9rem;
    text-align: left;
}

.flash {
    display: flex;
    align-items: flex-start;
    gap: 0.6rem;
    margin: 0 0 0.85rem;
    padding: 0.75rem 1rem;
    border-radius: 12px;
    font-size: 0.9rem;
    line-height: 1.4;
    animation: msgIn 220ms ease both;
}

.flash::before {
    content: "";
    flex-shrink: 0;
    margin-top: 0.35rem;
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 50%;
}

.flash-success {
    background: var(--success-soft);
    color: #145c39;
    border: 1px solid color-mix(in srgb, var(--success) 25%, transparent);
}
.flash-success::before { background: var(--success); }

.flash-error {
    background: var(--danger-soft);
    color: var(--danger-deep);
    border: 1px solid color-mix(in srgb, var(--danger) 25%, transparent);
}
.flash-error::before { background: var(--danger); }

.muted { color: var(--muted); }

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ==========================================================================
   Chat layout
   ========================================================================== */

body.page-chat {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    overflow: hidden;
    background: var(--bg);
}

body.page-chat .shell-chat {
    max-width: none;
    margin: 0;
    padding: 0;
    flex: 1;
    min-height: 0;
}

body.page-chat .shell-chat .main {
    min-height: 0;
    height: 100%;
}

.chat {
    --composer-h: 48px;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    height: 100%;
    max-width: 880px;
    margin: 0 auto;
    padding: 0 1.25rem;
}

.chat-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    margin: 0 -1.25rem;
    padding: 0.8rem 1.25rem;
    border-bottom: 1px solid var(--line);
    background: color-mix(in srgb, #fff 88%, var(--bg));
    position: sticky;
    top: 0;
    z-index: 8;
    backdrop-filter: blur(10px);
}

.chat-toolbar-main {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-width: 0;
}

.chat-topic-avatar {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.15rem;
    height: 2.15rem;
    flex-shrink: 0;
    border-radius: 10px;
    background: linear-gradient(135deg, var(--brand), var(--brand-bright));
    color: #fff;
    font-family: var(--display);
    font-weight: 700;
    font-size: 0.92rem;
    box-shadow: 0 4px 12px color-mix(in srgb, var(--brand) 35%, transparent);
}

.chat-toolbar-heading {
    display: grid;
    gap: 0.1rem;
    min-width: 0;
}

.chat-crumb {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.75rem;
    color: var(--muted);
    line-height: 1.2;
}

.chat-crumb a {
    color: var(--muted);
    text-decoration: none;
    font-weight: 500;
}

.chat-crumb a:hover { color: var(--brand-deep); }

.chat-crumb-sep { opacity: 0.5; }

.chat-crumb-current {
    color: var(--brand);
    font-weight: 700;
    letter-spacing: 0.03em;
}

.chat-toolbar h1 {
    font-family: var(--display);
    font-size: clamp(1.02rem, 2.2vw, 1.22rem);
    font-weight: 700;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.25;
}

.chat-toolbar-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-shrink: 0;
}

.chat-tool-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.45rem 0.85rem;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--surface);
    color: var(--ink-soft);
    text-decoration: none;
    font-size: 0.85rem;
    font-weight: 600;
    transition: border-color 140ms ease, background 140ms ease, color 140ms ease;
}

.chat-tool-btn svg { flex-shrink: 0; opacity: 0.75; }

.chat-tool-btn:hover {
    border-color: color-mix(in srgb, var(--brand) 40%, var(--line));
    background: color-mix(in srgb, var(--brand) 6%, #fff);
    color: var(--brand-deep);
}

.chat-tool-btn:hover svg { opacity: 1; }

.chat-tool-count {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 1.3rem;
    height: 1.3rem;
    padding: 0 0.35rem;
    border-radius: 999px;
    background: color-mix(in srgb, var(--brand) 13%, #fff);
    color: var(--brand-deep);
    font-size: 0.7rem;
    font-weight: 700;
}

.chat-tool-primary { white-space: nowrap; }

/* ——— Thread ——— */

.chat-thread {
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    scroll-behavior: smooth;
    padding: 0.25rem 0 1rem;
    mask-image: linear-gradient(to bottom, transparent, #000 12px, #000 calc(100% - 8px), transparent);
}

.chat-thread-inner {
    display: flex;
    flex-direction: column;
    gap: 1.35rem;
    padding: 0.9rem 0.15rem 1rem;
}

.chat-empty {
    margin: 6vh auto 0;
    max-width: 38rem;
    text-align: center;
    color: var(--muted);
    animation: msgIn 320ms ease both;
}

.chat-empty-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 3.4rem;
    height: 3.4rem;
    margin: 0 auto 1.1rem;
    border-radius: 16px;
    background: linear-gradient(135deg, var(--brand-soft), color-mix(in srgb, var(--accent-soft) 60%, var(--brand-soft)));
    color: var(--brand-deep);
    box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--brand) 12%, transparent);
}

.chat-empty-title {
    font-family: var(--display);
    font-weight: 700;
    font-size: 1.5rem;
    color: var(--ink);
    margin: 0 0 0.45rem;
}

.chat-empty-lede {
    margin: 0;
    font-size: 0.96rem;
}

.chat-suggestions-label {
    margin: 1.75rem 0 0.7rem;
    font-size: 0.74rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--muted-soft);
    text-align: left;
    max-width: 36rem;
    margin-inline: auto;
}

.chat-suggestions {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    margin: 0 auto;
    max-width: 36rem;
    text-align: left;
}

.chat-suggestion {
    appearance: none;
    width: 100%;
    margin: 0;
    padding: 0.9rem 1.05rem;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: var(--surface);
    color: var(--ink);
    font: inherit;
    font-size: 0.93rem;
    line-height: 1.35;
    text-align: left;
    cursor: pointer;
    box-shadow: var(--shadow-sm);
    transition: border-color 160ms ease, background 160ms ease, transform 160ms ease, box-shadow 160ms ease;
}

.chat-suggestion:hover {
    border-color: color-mix(in srgb, var(--brand) 45%, var(--line));
    background: color-mix(in srgb, var(--brand) 4%, #fff);
    transform: translateY(-1px);
    box-shadow: var(--shadow);
}

.chat-suggestion:focus-visible {
    outline: 2px solid color-mix(in srgb, var(--brand) 55%, transparent);
    outline-offset: 2px;
}

/* ——— Messages ——— */

.msg {
    max-width: min(92%, 44rem);
    animation: msgIn 280ms cubic-bezier(0.22, 1, 0.36, 1) both;
}

.msg-user { align-self: flex-end; }

.msg-assistant {
    align-self: flex-start;
    width: min(92%, 44rem);
}

.msg-meta {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--muted);
    margin: 0 0 0.4rem 0.15rem;
}

.msg-meta::before {
    content: "";
    display: inline-flex;
    width: 1.3rem;
    height: 1.3rem;
    border-radius: 50%;
    flex-shrink: 0;
}

.msg-user .msg-meta {
    justify-content: flex-end;
    margin-right: 0.15rem;
}

.msg-user .msg-meta::before {
    order: 2;
    background: linear-gradient(135deg, #4b5b53, #2c3934);
}

.msg-assistant .msg-meta::before {
    background: linear-gradient(135deg, var(--brand), var(--brand-bright));
}

.msg-body {
    line-height: 1.58;
    font-size: 0.98rem;
}

.msg-user .msg-body {
    background: linear-gradient(155deg, #eef1ee, #e6ebe6);
    border: 1px solid var(--line);
    color: var(--ink);
    padding: 0.72rem 1rem;
    border-radius: 18px 18px 5px 18px;
    box-shadow: var(--shadow-sm);
}

.msg-assistant .msg-body {
    background: var(--surface);
    border: 1px solid var(--line);
    padding: 0.95rem 1.15rem;
    border-radius: 18px 18px 18px 5px;
    box-shadow: var(--shadow-sm);
}

.msg-body.md > :first-child { margin-top: 0; }
.msg-body.md > :last-child { margin-bottom: 0; }
.msg-body.md p { margin: 0.6em 0; }
.msg-body.md strong { font-weight: 700; color: var(--ink); }
.msg-body.md em { font-style: italic; }
.msg-body.md ul,
.msg-body.md ol {
    margin: 0.6em 0;
    padding-left: 1.3rem;
}
.msg-body.md li + li { margin-top: 0.28em; }
.msg-body.md h1,
.msg-body.md h2,
.msg-body.md h3,
.msg-body.md h4 {
    font-family: var(--display);
    font-weight: 700;
    font-size: 1.08em;
    margin: 0.9em 0 0.4em;
    line-height: 1.25;
    color: var(--brand-deep);
}
.msg-body.md code {
    font-size: 0.9em;
    background: color-mix(in srgb, var(--ink) 6%, transparent);
    padding: 0.12em 0.4em;
    border-radius: 5px;
}
.msg-body.md pre {
    overflow-x: auto;
    background: color-mix(in srgb, var(--ink) 6%, transparent);
    padding: 0.8rem 1rem;
    border-radius: 12px;
}
.msg-body.md pre code { background: none; padding: 0; }
.msg-body.md blockquote {
    margin: 0.6em 0;
    padding-left: 0.9rem;
    border-left: 3px solid color-mix(in srgb, var(--brand) 40%, transparent);
    color: var(--muted);
}
.msg-body.md a {
    color: var(--brand-deep);
    text-decoration-color: color-mix(in srgb, var(--brand) 40%, transparent);
}

.msg-body.md .msg-media {
    margin: 1rem 0;
    padding: 0;
    border: 0;
}

.msg-body.md .msg-media img,
.msg-body.md > p > img,
.msg-body.md img {
    display: block;
    width: 100%;
    max-width: 100%;
    height: auto;
    border-radius: 12px;
    border: 1px solid var(--line);
    background: #fff;
    box-shadow: var(--shadow-sm);
}

.msg-body.md .msg-media figcaption {
    margin-top: 0.4rem;
    font-size: 0.78rem;
    color: var(--muted);
    text-align: center;
}

.msg-typing .msg-body {
    min-height: 1.4em;
    display: inline-flex;
    align-items: center;
    width: fit-content;
    padding: 0.78rem 1.05rem;
}

.typing-status {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    color: var(--muted);
    font-size: 0.92rem;
}

.typing-dots {
    display: inline-flex;
    gap: 0.22rem;
    font-weight: 700;
    color: var(--brand);
}

.typing-dots span {
    width: 0.36rem;
    height: 0.36rem;
    border-radius: 50%;
    background: currentColor;
    font-size: 0;
    animation: typingPulse 1.15s infinite ease-in-out;
}

.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }

.msg-stream { white-space: normal; }

.stream-caret {
    display: inline-block;
    width: 0.55ch;
    height: 1.05em;
    margin-left: 1px;
    vertical-align: text-bottom;
    background: var(--brand);
    animation: caretBlink 0.9s steps(1) infinite;
}

@keyframes caretBlink {
    0%, 45% { opacity: 1; }
    50%, 100% { opacity: 0; }
}

@keyframes typingPulse {
    0%, 80%, 100% { opacity: 0.25; transform: translateY(0); }
    40% { opacity: 1; transform: translateY(-3px); }
}

@keyframes msgIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.sources {
    margin-top: 0.75rem;
    font-size: 0.85rem;
    background: var(--surface-alt);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 0.55rem 0.8rem;
}

.sources summary {
    cursor: pointer;
    font-weight: 700;
    color: var(--brand-deep);
    list-style: none;
    display: flex;
    align-items: center;
    gap: 0.35rem;
}

.sources summary::before {
    content: "▸";
    font-size: 0.8em;
    transition: transform 160ms ease;
    display: inline-block;
}

.sources[open] summary::before { transform: rotate(90deg); }

.sources ul {
    margin: 0.6rem 0 0;
    padding-left: 1.05rem;
}

.sources li + li {
    margin-top: 0.6rem;
    padding-top: 0.6rem;
    border-top: 1px dashed var(--line);
}

.sources p {
    margin: 0.25rem 0 0;
    color: var(--muted);
}

.source-file {
    color: var(--brand-deep);
    font-weight: 700;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.source-file:hover { color: var(--brand); }

.msg-stats {
    margin: 0.6rem 0 0;
    padding: 0;
    font-size: 0.72rem;
    letter-spacing: 0.01em;
    color: var(--muted-soft);
}

/* ——— Composer ——— */

.chat-dock {
    position: sticky;
    bottom: 0;
    padding: 0.75rem 0 1.1rem;
    background: transparent;
}

.chat-dock::before {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    bottom: 100%;
    height: 32px;
    pointer-events: none;
    background: linear-gradient(to top, var(--bg), transparent);
}

.chat-composer {
    position: relative;
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0.55rem;
    align-items: end;
    padding: 0.5rem;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 20px;
    box-shadow:
        0 1px 0 rgba(255, 255, 255, 0.8) inset,
        var(--shadow);
    transition: border-color 160ms ease, box-shadow 160ms ease;
}

.chat-composer:has(textarea:focus) {
    border-color: color-mix(in srgb, var(--brand) 40%, var(--line));
    box-shadow:
        0 1px 0 rgba(255, 255, 255, 0.8) inset,
        0 0 0 3px color-mix(in srgb, var(--brand) 12%, transparent),
        var(--shadow);
}

.chat-composer textarea {
    min-height: var(--composer-h);
    max-height: 160px;
    height: var(--composer-h);
    resize: none !important;
    overflow-y: auto;
    border: 0 !important;
    border-radius: 12px;
    padding: 0.85rem 0.9rem;
    line-height: 1.3;
    box-shadow: none !important;
    background: transparent;
}

.chat-composer textarea:focus { outline: none; }

.chat-send {
    height: var(--composer-h);
    min-width: 6.7rem;
    padding: 0 1.2rem;
    border-radius: 15px;
    align-self: end;
}

.chat-send-icon { display: none; }

.chat-hint {
    margin: 0.5rem 0 0;
    text-align: center;
    color: var(--muted-soft);
    font-size: 0.74rem;
    background: none !important;
    box-shadow: none;
    border: 0;
    padding: 0;
}

.chat-tool-primary-short,
.chat-tool-logout,
.chat-tool-home {
    display: none;
}

.chat-tool-primary-short {
    align-items: center;
    justify-content: center;
}

@media (max-width: 720px) {
    .topbar-inner {
        padding: 0.5rem 0.75rem;
        gap: 0.5rem;
    }

    .brand-logo {
        height: 32px;
    }

    .brand-text { display: none; }

    .topbar-home { display: none; }

    .topbar-actions .btn-sm {
        padding: 0.32rem 0.65rem;
        font-size: 0.76rem;
    }

    /* Chat mobile : topbar site masquée, une seule barre compacte */
    body.page-chat .topbar {
        display: none;
    }

    body.page-chat {
        height: 100dvh;
        height: 100svh;
    }

    .chat {
        --composer-h: 42px;
        padding: 0 0.65rem;
        padding-bottom: env(safe-area-inset-bottom, 0);
    }

    .chat-toolbar {
        flex-wrap: nowrap;
        align-items: center;
        gap: 0.4rem;
        margin: 0 -0.65rem;
        padding: 0.45rem 0.65rem;
        padding-top: max(0.45rem, env(safe-area-inset-top, 0px));
    }

    .chat-topic-avatar,
    .chat-crumb {
        display: none;
    }

    .chat-toolbar-main {
        min-width: 0;
        flex: 1;
    }

    .chat-toolbar h1 {
        font-size: 0.92rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .chat-toolbar-actions {
        flex-shrink: 0;
        gap: 0.3rem;
    }

    .chat-tool-btn {
        width: 2.15rem;
        height: 2.15rem;
        padding: 0;
        justify-content: center;
        gap: 0;
        position: relative;
    }

    .chat-tool-label { display: none; }

    .chat-tool-count {
        position: absolute;
        top: -0.25rem;
        right: -0.25rem;
        min-width: 1.05rem;
        height: 1.05rem;
        font-size: 0.62rem;
        padding: 0 0.2rem;
    }

    .chat-tool-primary {
        width: 2.15rem;
        height: 2.15rem;
        min-width: 0;
        padding: 0;
        border-radius: 999px;
    }

    .chat-tool-primary-full { display: none; }
    .chat-tool-primary-short { display: inline-flex; }
    .chat-tool-logout,
    .chat-tool-home { display: inline-flex; }

    .chat-thread {
        padding: 0.1rem 0 0.5rem;
    }

    .chat-thread-inner {
        gap: 0.95rem;
        padding: 0.45rem 0 0.5rem;
    }

    .chat-empty {
        margin: 2.5vh auto 0;
        padding: 0 0.15rem;
    }

    .chat-empty-icon {
        width: 2.7rem;
        height: 2.7rem;
        margin-bottom: 0.75rem;
    }

    .chat-empty-title { font-size: 1.2rem; }
    .chat-empty-lede { font-size: 0.88rem; }

    .chat-suggestions-label {
        margin-top: 1.1rem;
        margin-bottom: 0.5rem;
    }

    .chat-suggestion {
        padding: 0.7rem 0.85rem;
        font-size: 0.86rem;
        border-radius: 12px;
    }

    .msg { max-width: 100%; }
    .msg-assistant { width: 100%; }

    .msg-meta {
        font-size: 0.68rem;
        margin-bottom: 0.3rem;
    }

    .msg-user .msg-body,
    .msg-assistant .msg-body {
        padding: 0.65rem 0.85rem;
        font-size: 0.93rem;
    }

    .msg-user .msg-body { border-radius: 14px 14px 4px 14px; }
    .msg-assistant .msg-body { border-radius: 14px 14px 14px 4px; }

    .msg-stats {
        font-size: 0.65rem;
        margin-top: 0.4rem;
    }

    .sources {
        font-size: 0.8rem;
        padding: 0.45rem 0.65rem;
    }

    .chat-dock {
        padding: 0.45rem 0 calc(0.55rem + env(safe-area-inset-bottom, 0px));
    }

    .chat-dock::before { height: 20px; }

    .chat-composer {
        gap: 0.35rem;
        padding: 0.3rem;
        border-radius: 16px;
        box-shadow:
            0 1px 0 rgba(255, 255, 255, 0.8) inset,
            0 8px 24px rgba(20, 30, 26, 0.1);
    }

    .chat-composer textarea {
        padding: 0.65rem 0.7rem;
        font-size: 16px; /* évite le zoom iOS */
    }

    .chat-send {
        width: var(--composer-h);
        min-width: var(--composer-h);
        height: var(--composer-h);
        padding: 0;
        border-radius: 12px;
    }

    .chat-send-label { display: none; }
    .chat-send-icon { display: block; }

    .chat-hint { display: none; }
}

/* ==========================================================================
   Documents page
   ========================================================================== */

.docs-page-head .docs-page-nav {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-top: 1.1rem;
    flex-wrap: wrap;
}

.docs-page-nav-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.docs-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}

.docs-search {
    position: relative;
    flex: 1;
    min-width: 15rem;
}

.docs-search::before {
    content: "";
    position: absolute;
    left: 0.9rem;
    top: 50%;
    width: 0.95rem;
    height: 0.95rem;
    transform: translateY(-50%);
    border: 1.6px solid var(--muted-soft);
    border-radius: 50%;
    pointer-events: none;
}

.docs-search::after {
    content: "";
    position: absolute;
    left: 1.62rem;
    top: 63%;
    width: 0.5rem;
    height: 1.6px;
    background: var(--muted-soft);
    transform: rotate(45deg);
    pointer-events: none;
}

.docs-search input {
    width: 100%;
    margin: 0;
    padding: 0.7rem 0.9rem 0.7rem 2.35rem;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--surface);
    font: inherit;
    color: var(--ink);
}

.docs-count {
    margin: 0;
    font-size: 0.86rem;
    white-space: nowrap;
}

.docs-table {
    list-style: none;
    margin: 0;
    padding: 0;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
}

.docs-row {
    display: grid;
    grid-template-columns: auto 1fr auto;
    gap: 0.9rem;
    align-items: center;
    padding: 0.9rem 1.1rem;
    border-top: 1px solid var(--line-soft);
    transition: background 140ms ease;
}

.docs-row:hover { background: var(--surface-alt); }
.docs-row:first-child { border-top: 0; }

.docs-row-main { min-width: 0; }

.docs-row-name {
    display: block;
    color: var(--ink);
    font-weight: 600;
    text-decoration: none;
    overflow-wrap: anywhere;
}

.docs-row-name:hover {
    color: var(--brand-deep);
    text-decoration: underline;
    text-underline-offset: 2px;
}

.docs-row-meta {
    margin: 0.3rem 0 0;
    font-size: 0.8rem;
    display: flex;
    align-items: center;
    gap: 0.4rem;
    flex-wrap: wrap;
}

.docs-row-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    align-items: center;
    justify-content: flex-end;
}

.docs-row-actions form { margin: 0; }

.chat-docs-ext {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 2.7rem;
    height: 1.7rem;
    padding: 0 0.4rem;
    border-radius: 8px;
    background: var(--brand-soft);
    color: var(--brand-deep);
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    flex-shrink: 0;
}

.docs-status {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.1rem 0.55rem 0.1rem 0.4rem;
    border-radius: 999px;
    font-size: 0.71rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.docs-status::before {
    content: "";
    width: 0.4rem;
    height: 0.4rem;
    border-radius: 50%;
    background: currentColor;
}

.docs-status-indexed { background: var(--success-soft); color: var(--success); }
.docs-status-error { background: var(--danger-soft); color: var(--danger); }
.docs-status-pending { background: var(--warning-soft); color: var(--warning); }

.docs-empty-filter {
    margin: 1.25rem 0 0;
    text-align: center;
}

@media (max-width: 720px) {
    .docs-row { grid-template-columns: auto 1fr; }
    .docs-row-actions { grid-column: 2; justify-content: flex-start; }
}

/* ——— Upload panel ——— */

.docs-panel {
    width: min(36rem, calc(100vw - 2rem));
    max-height: min(90vh, 42rem);
    padding: 0;
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    background: var(--surface);
    box-shadow: var(--shadow-lg);
}

.docs-panel::backdrop {
    background: rgba(20, 30, 26, 0.45);
    backdrop-filter: blur(2px);
}

.docs-panel-inner {
    padding: 1.35rem 1.4rem 1.5rem;
}

.docs-panel-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 0.4rem;
}

.docs-panel-head h2 {
    font-family: var(--display);
    font-weight: 700;
    font-size: 1.3rem;
    margin: 0;
}

.docs-panel-close {
    appearance: none;
    border: 0;
    background: var(--surface-alt);
    color: var(--muted);
    width: 2rem;
    height: 2rem;
    border-radius: 50%;
    font-size: 1.25rem;
    line-height: 1;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 140ms ease, color 140ms ease;
}

.docs-panel-close:hover { background: var(--line); color: var(--ink); }

.docs-upload-help {
    margin: 0 0 1.15rem;
    font-size: 0.9rem;
    line-height: 1.45;
}

.docs-upload-form { display: grid; gap: 0.85rem; }

.docs-dropzone {
    position: relative;
    display: flex;
    align-items: center;
    gap: 0.7rem;
    min-height: 3.4rem;
    padding: 0.9rem 1.05rem;
    border: 1.5px dashed color-mix(in srgb, var(--brand) 38%, var(--line));
    border-radius: 14px;
    background: var(--brand-soft);
    cursor: pointer;
    overflow: hidden;
    transition: border-color 140ms ease, background 140ms ease;
}

.docs-dropzone:hover {
    border-color: var(--brand);
    background: color-mix(in srgb, var(--brand-soft) 70%, #fff);
}

.docs-dropzone-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.1rem;
    height: 2.1rem;
    flex-shrink: 0;
    border-radius: 10px;
    background: #fff;
    color: var(--brand-deep);
    box-shadow: 0 2px 8px color-mix(in srgb, var(--brand) 20%, transparent);
}

.docs-dropzone input[type="file"] {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
}

.docs-dropzone-label {
    font-size: 0.92rem;
    font-weight: 600;
    color: var(--ink);
    pointer-events: none;
}

.docs-upload-busy {
    margin: 0;
    font-size: 0.86rem;
    display: flex;
    align-items: center;
    gap: 0.45rem;
}

.docs-panel-sep {
    height: 1px;
    margin: 1.3rem 0;
    background: var(--line);
}

.docs-reingest { display: grid; gap: 0.8rem; justify-items: start; }

.docs-reingest-title {
    margin: 0;
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--ink-soft);
}

.docs-force {
    display: flex !important;
    align-items: center;
    gap: 0.55rem;
    width: auto;
    max-width: 100%;
    font-size: 0.88rem;
    font-weight: 500;
    color: var(--muted);
    line-height: 1.35;
}

.docs-force input[type="checkbox"] {
    width: 1rem;
    height: 1rem;
    margin: 0;
    padding: 0;
    border: 1px solid var(--line);
    border-radius: 4px;
    flex: 0 0 auto;
    accent-color: var(--brand);
}

.docs-force span { flex: 0 1 auto; }

/* ——— Confirm modal ——— */

.confirm-modal {
    width: min(27rem, calc(100vw - 2rem));
    padding: 0;
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    background: var(--surface);
    box-shadow: var(--shadow-lg);
}

.confirm-modal::backdrop {
    background: rgba(20, 30, 26, 0.5);
    backdrop-filter: blur(3px);
}

.confirm-modal-inner {
    padding: 1.6rem 1.5rem 1.4rem;
    text-align: center;
}

.confirm-modal-icon {
    width: 3.2rem;
    height: 3.2rem;
    margin: 0 auto 1.1rem;
    display: grid;
    place-items: center;
    border-radius: 999px;
    color: var(--danger);
    background: var(--danger-soft);
    border: 1px solid color-mix(in srgb, var(--danger) 20%, transparent);
}

.confirm-modal-title {
    font-family: var(--display);
    font-weight: 700;
    font-size: 1.35rem;
    margin: 0 0 0.6rem;
    color: var(--ink);
    line-height: 1.25;
}

.confirm-modal-body {
    margin: 0;
    color: var(--muted);
    font-size: 0.95rem;
    line-height: 1.48;
}

.confirm-modal-file {
    margin: 0.9rem 0 0;
    padding: 0.65rem 0.85rem;
    border-radius: 10px;
    background: var(--surface-alt);
    border: 1px solid var(--line);
    color: var(--ink);
    font-size: 0.86rem;
    font-weight: 600;
    overflow-wrap: anywhere;
    text-align: left;
}

.confirm-modal-actions {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 0.6rem;
    margin-top: 1.4rem;
}
