/* ================================================================
   CRP Agent (DeepSeek) v1
   Consistent BEM class names used by both CSS and PHP/JS.
   All panel elements use .crpag-panel__ prefix.
   ================================================================ */

:root {
    --crpag-brand: #123a63;
    --crpag-brand-strong: #0d2c4a;
    --crpag-brand-soft: #edf4fb;
    --crpag-text: #15273a;
    --crpag-text-soft: #5d6f82;
    --crpag-border: #e6edf5;
    --crpag-surface: #ffffff;
    --crpag-surface-soft: #f7f9fc;
    --crpag-shadow: 0 18px 56px rgba(12, 31, 53, .18);
}

/* ── FAB ─────────────────────────────────────────────────── */
.crpag-fab {
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    z-index: 9990;
    display: flex;
    align-items: center;
    gap: .5rem;
    background: linear-gradient(135deg, var(--crpag-brand), #1d4f80);
    color: #fff;
    border: none;
    border-radius: 50px;
    padding: .72rem 1.25rem .72rem 1rem;
    font-size: .875rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    box-shadow: 0 10px 30px rgba(18,58,99,.34);
    transition: background .2s, transform .2s, box-shadow .2s;
    line-height: 1;
    white-space: nowrap;
}
.crpag-fab:hover {
    background: linear-gradient(135deg, var(--crpag-brand-strong), #18466f);
    transform: translateY(-2px);
    box-shadow: 0 12px 34px rgba(16,45,73,.42);
}
.crpag-fab__close { display: none; }
.crpag-fab.is-open .crpag-fab__open  { display: none; }
.crpag-fab.is-open .crpag-fab__close { display: flex; }
.crpag-fab.is-open .crpag-fab__label { display: none; }
.crpag-fab.is-open { border-radius: 50%; padding: .72rem; }

/* Hide FAB on full-page */
.is-crpag-agent-page .crpag-fab { display: none !important; }

/* ── Panel ───────────────────────────────────────────────── */
.crpag-panel {
    position: fixed;
    bottom: 5.5rem;
    right: 1.5rem;
    z-index: 9980;
    width: 390px;
    max-width: calc(100vw - 2rem);
    height: 580px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    background: var(--crpag-surface);
    border: 1px solid var(--crpag-border);
    border-radius: 20px;
    box-shadow: var(--crpag-shadow);
    overflow: hidden;
    /* closed state */
    transform: translateY(20px) scale(.95);
    opacity: 0;
    pointer-events: none;
    transition: transform .3s cubic-bezier(.34,1.56,.64,1), opacity .22s ease;
}
.crpag-panel.is-open {
    transform: translateY(0) scale(1);
    opacity: 1;
    pointer-events: all;
}

/* Full-page: override fixed positioning */
.is-crpag-agent-page .crpag-panel {
    position: static !important;
    bottom: auto !important;
    right: auto !important;
    transform: none !important;
    opacity: 1 !important;
    pointer-events: all !important;
    width: 100% !important;
    max-width: 100% !important;
    height: 100% !important;
    max-height: 100% !important;
    border-radius: 16px;
    box-shadow: 0 4px 32px rgba(0,0,0,.1);
    flex: 1;
}

/* ── Header ──────────────────────────────────────────────── */
.crpag-panel__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: .85rem 1rem .85rem 1.1rem;
    background: linear-gradient(135deg, var(--crpag-brand), #174977);
    flex-shrink: 0;
    gap: .5rem;
}
.crpag-panel__brand {
    display: flex;
    align-items: center;
    gap: .65rem;
    min-width: 0;
}
.crpag-panel__avatar {
    width: 36px;
    height: 36px;
    background: rgba(255,255,255,.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    flex-shrink: 0;
}
.crpag-panel__name {
    font-size: .94rem;
    font-weight: 700;
    color: #fff;
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.crpag-panel__status {
    display: flex;
    align-items: center;
    gap: .3rem;
    font-size: .7rem;
    color: rgba(255,255,255,.78);
    margin-top: .1rem;
}
.crpag-panel__dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #facc15;
    flex-shrink: 0;
    transition: background .3s;
}
.crpag-panel__actions {
    display: flex;
    align-items: center;
    gap: .15rem;
    flex-shrink: 0;
}
.crpag-panel__btn {
    background: none;
    border: none;
    cursor: pointer;
    color: rgba(255,255,255,.6);
    padding: .38rem;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    transition: color .15s, background .15s;
    line-height: 0;
}
.crpag-panel__btn:hover {
    color: #fff;
    background: rgba(255,255,255,.12);
}

/* ── Messages ────────────────────────────────────────────── */
.crpag-panel__messages {
    flex: 1;                        /* ← this makes it fill available space */
    overflow-y: auto;               /* ← this makes it scroll */
    padding: 1.15rem 1rem;
    display: flex;
    flex-direction: column;
    gap: .8rem;
    scroll-behavior: smooth;
    min-height: 0;                  /* ← required for flex children to scroll */
}
.crpag-panel__messages::-webkit-scrollbar { width: 4px; }
.crpag-panel__messages::-webkit-scrollbar-track { background: transparent; }
.crpag-panel__messages::-webkit-scrollbar-thumb { background: #e0e0e0; border-radius: 4px; }
.crpag-panel__messages::-webkit-scrollbar-thumb:hover { background: #ccc; }

/* ── Message bubbles ─────────────────────────────────────── */
.crpag-msg {
    display: flex;
    flex-direction: column;
    max-width: 86%;
    animation: crpag-slide-in .18s ease;
}
@keyframes crpag-slide-in {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}
.crpag-msg--user  { align-self: flex-end;  align-items: flex-end; }
.crpag-msg--agent { align-self: flex-start; align-items: flex-start; }

.crpag-msg__bubble {
    padding: .72rem .98rem;
    border-radius: 16px;
    font-size: .89rem;
    line-height: 1.62;
    word-break: break-word;
    overflow-wrap: break-word;
}
.crpag-msg--user .crpag-msg__bubble {
    background: linear-gradient(140deg, var(--crpag-brand), #1a4d7e);
    color: #fff;
    border-bottom-right-radius: 4px;
}
.crpag-msg--agent .crpag-msg__bubble {
    background: var(--crpag-surface-soft);
    color: var(--crpag-text);
    border: 1px solid #eaf0f6;
    border-bottom-left-radius: 4px;
}
.crpag-msg--error .crpag-msg__bubble {
    background: #fee2e2;
    color: #991b1b;
    border-bottom-left-radius: 4px;
}
.crpag-msg__bubble a      { color: #1D6FA4; text-underline-offset: 2px; }
.crpag-msg__bubble a:hover{ color: var(--crpag-brand-strong); }
.crpag-msg__bubble code   { background: rgba(0,0,0,.08); padding: 1px 5px; border-radius: 3px; font-size: .8em; }
.crpag-msg__bubble strong { font-weight: 700; }
.crpag-msg__bubble em     { font-style: italic; }

/* Message footer (time + badges) */
.crpag-msg__foot {
    display: flex;
    align-items: center;
    gap: .4rem;
    margin-top: .25rem;
    padding: 0 .2rem;
    flex-wrap: wrap;
}
/* Per-stage server timing (e.g. cache → prepare_payload → llm → postprocess) */
.crpag-msg__pipeline {
    width: 100%;
    flex-basis: 100%;
    font-size: .65rem;
    font-family: ui-monospace, Consolas, "Courier New", monospace;
    color: var(--crpag-text-soft);
    line-height: 1.35;
    margin-top: .2rem;
}
.crpag-msg--error .crpag-msg__pipeline {
    color: #b45353;
}
.crpag-msg__gateway-diag {
    width: 100%;
    flex-basis: 100%;
    font-size: .62rem;
    font-family: ui-monospace, Consolas, "Courier New", monospace;
    color: #0f766e;
    line-height: 1.4;
    margin-top: .15rem;
    white-space: pre-wrap;
    word-break: break-word;
}
.crpag-msg--error .crpag-msg__gateway-diag {
    color: #9a3412;
}
.crpag-msg__time {
    font-size: .67rem;
    color: #bbb;
}
.crpag-msg__badge {
    font-size: .6rem;
    font-weight: 700;
    padding: 1px 5px;
    border-radius: 4px;
    letter-spacing: .02em;
    text-transform: uppercase;
}
.crpag-msg__refine {
    font-size: .68rem;
    font-weight: 700;
    border: 1px solid #93c5fd;
    color: #1e3a8a;
    background: #eff6ff;
    border-radius: 6px;
    padding: 2px 8px;
    cursor: pointer;
    transition: background .15s, border-color .15s;
}
.crpag-msg__refine:hover:not(:disabled) {
    background: #dbeafe;
    border-color: #60a5fa;
}
.crpag-msg__refine:disabled {
    opacity: .6;
    cursor: not-allowed;
}
.crpag-badge--cache   { background: #DBEAFE; color: #1e40af; }
.crpag-badge--instant { background: #D1FAE5; color: #065f46; }
.crpag-badge--elapsed { background: transparent; color: #bbb; font-weight: 400; font-style: italic; text-transform: none; letter-spacing: 0; }

/* ── Typing indicator ────────────────────────────────────── */
.crpag-msg--typing .crpag-msg__bubble {
    display: flex;
    align-items: center;
    gap: .3rem;
    flex-wrap: wrap;
    padding: .75rem 1rem;
}
.crpag-typing-dot {
    width: 7px;
    height: 7px;
    background: #bbb;
    border-radius: 50%;
    animation: crpag-bounce 1.2s infinite;
    flex-shrink: 0;
}
.crpag-typing-dot:nth-child(2) { animation-delay: .2s; }
.crpag-typing-dot:nth-child(3) { animation-delay: .4s; }
@keyframes crpag-bounce {
    0%,60%,100% { transform: translateY(0); }
    30%          { transform: translateY(-6px); }
}
.crpag-timer-note {
    width: 100%;
    font-size: .78rem;
    color: #1f3b57;
    font-style: normal;
    font-weight: 600;
    margin-top: .2rem;
    min-height: 1em;
}

/* ── Suggested prompts ───────────────────────────────────── */
.crpag-panel__suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: .4rem;
    padding: .65rem 1rem;
    border-top: 1px solid var(--crpag-border);
    flex-shrink: 0;
}
.crpag-panel__suggestions.is-hidden { display: none; }
.crpag-chip {
    background: var(--crpag-brand-soft);
    color: var(--crpag-brand);
    border: 1px solid #c7dff6;
    border-radius: 20px;
    padding: .3rem .85rem;
    font-size: .75rem;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    white-space: nowrap;
    transition: background .15s, color .15s, border-color .15s;
    line-height: 1.4;
}
.crpag-chip:hover { background: var(--crpag-brand); color: #fff; border-color: var(--crpag-brand); }

/* ── Input area ──────────────────────────────────────────── */
.crpag-panel__input-area {
    display: flex;
    align-items: flex-end;
    gap: .5rem;
    padding: .75rem 1rem;
    border-top: 1px solid var(--crpag-border);
    background: #fbfcfe;
    flex-shrink: 0;
}
.crpag-panel__input {
    flex: 1;
    border: 1.5px solid #dbe5f0;
    border-radius: 12px;
    padding: .6rem .9rem;
    font-size: .875rem;
    font-family: inherit;
    resize: none;
    min-height: 42px;
    max-height: 120px;
    overflow-y: auto;
    line-height: 1.5;
    background: #fff;
    color: var(--crpag-text);
    transition: border-color .15s, box-shadow .15s;
}
.crpag-panel__input:focus {
    outline: none;
    border-color: var(--crpag-brand);
    box-shadow: 0 0 0 3px rgba(18,58,99,.11);
}
.crpag-panel__input::placeholder { color: #adb5bd; }
.crpag-panel__send {
    width: 40px;
    height: 40px;
    background: linear-gradient(135deg, var(--crpag-brand), #1a4b7a);
    color: #fff;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background .15s, transform .1s;
}
.crpag-panel__send:hover:not(:disabled) { background: linear-gradient(135deg, var(--crpag-brand-strong), #194469); }
.crpag-panel__send:active:not(:disabled) { transform: scale(.92); }
.crpag-panel__send:disabled { background: #d0d0d0; cursor: not-allowed; }

/* ── Footer note ─────────────────────────────────────────── */
.crpag-panel__footnote {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.35rem;
    text-align: center;
    font-size: 0.75rem;
    line-height: 1.4;
    color: #7a8a99;
    padding: 0.5rem 1rem 0.65rem;
    background: #fbfcfe;
    flex-shrink: 0;
}

.crpag-panel__footnote a.crpag-panel__support-link {
    display: inline-block;
    font-size: 0.875rem;
    font-weight: 600;
    line-height: 1.3;
    color: #1a4b7a;
    text-decoration: none;
    padding: 0.35rem 0.75rem;
    border-radius: 6px;
    background: rgba(40, 125, 250, 0.12);
    border: 1px solid rgba(40, 125, 250, 0.22);
}

.crpag-panel__footnote a.crpag-panel__support-link:hover {
    color: #fff;
    background: var(--crpag-brand, #287dfa);
    border-color: var(--crpag-brand, #287dfa);
}

.crpag-msg--agent.crpag-msg--outage .crpag-msg__bubble {
    background: #fff8f0;
    border-color: #f0d9b8;
    color: #3d3428;
}

/* ── Mobile ──────────────────────────────────────────────── */
@media (max-width: 480px) {
    .crpag-panel {
        bottom: 0;
        right: 0;
        left: 0;
        width: 100%;
        max-width: 100%;
        height: auto;
        max-height: 88svh;
        border-radius: 20px 20px 0 0;
    }
    .crpag-fab { bottom: 1rem; right: 1rem; }
}

/* ── Full-page chat container ────────────────────────────── */
.crpag-fullpage-wrap {
    display: flex;
    height: calc(100vh - 140px);   /* viewport minus nav */
    min-height: 500px;
    max-height: 900px;
    max-width: 980px;
    margin: 0 auto;
    padding: 0 1.5rem 2rem;
    flex-direction: column;
}
.crpag-fullpage-header {
    padding: 1.9rem 0 1.35rem;
    text-align: center;
    flex-shrink: 0;
}
.crpag-fullpage-header h1 {
    font-size: clamp(1.4rem, 3vw, 2rem);
    margin: 0 0 .4rem;
    color: var(--crpag-text);
}
.crpag-fullpage-header p {
    font-size: .9rem;
    color: var(--crpag-text-soft);
    margin: 0;
    line-height: 1.6;
}
.crpag-fullpage-panel {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}
