/* ── Portfolio assistant widget ─────────────────────────────────────
 * Its own accent — deliberately not the site teal, so the assistant
 * reads as a separate surface rather than another page element.
 * Everything else inherits the site's design tokens from style.css.
 * ------------------------------------------------------------------ */

:root {
    --chat-accent:       #A6540C;
    --chat-accent-hover: #8A4409;
    --chat-soft:         rgba(166, 84, 12, 0.08);
    --chat-on-accent:    #ffffff;
}

html.dark-mode {
    --chat-accent:       #E9A13B;
    --chat-accent-hover: #F2B45C;
    --chat-soft:         rgba(233, 161, 59, 0.12);
    --chat-on-accent:    #1d1206;
}

/* ── Launcher ────────────────────────────────────────────────────── */

#chat-fab {
    position: fixed;
    right: 22px;
    bottom: 22px;
    width: 52px;
    height: 52px;
    border: none;
    border-radius: 50%;
    background: var(--chat-accent);
    color: var(--chat-on-accent);
    cursor: pointer;
    display: grid;
    place-items: center;
    box-shadow: 0 6px 20px -6px rgba(0, 0, 0, 0.45);
    transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1), background 0.2s ease;
    z-index: 950;
}

#chat-fab:hover { background: var(--chat-accent-hover); transform: translateY(-2px) scale(1.04); }
#chat-fab:active { transform: scale(0.97); }

#chat-fab .close-icon { display: none; }
#chat-fab.open .open-icon { display: none; }
#chat-fab.open .close-icon { display: block; }

/* ── Panel ───────────────────────────────────────────────────────── */

#chat-panel {
    position: fixed;
    right: 22px;
    bottom: 86px;
    width: 400px;
    max-width: calc(100vw - 28px);
    height: min(600px, calc(100vh - 130px));
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: 0 24px 60px -20px rgba(0, 0, 0, 0.35);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    z-index: 950;
    opacity: 0;
    transform: translateY(10px) scale(0.985);
    pointer-events: none;
    transition: opacity 0.2s ease, transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

#chat-panel.open { opacity: 1; transform: none; pointer-events: auto; }

/* ── Header ──────────────────────────────────────────────────────── */

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
}

.chat-id { display: flex; align-items: center; gap: 9px; min-width: 0; }

.chat-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--chat-accent);
    box-shadow: 0 0 0 3px var(--chat-soft);
    flex-shrink: 0;
}

.chat-header h4 {
    font-size: 0.88rem;
    font-weight: 600;
    color: var(--ink);
    letter-spacing: -0.01em;
    line-height: 1.25;
}

.chat-sub {
    display: block;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.62rem;
    letter-spacing: 0.09em;
    text-transform: uppercase;
    color: var(--ink-3);
}

.chat-actions { display: flex; gap: 2px; flex-shrink: 0; }

.chat-icon-btn {
    background: none;
    border: none;
    color: var(--ink-3);
    cursor: pointer;
    padding: 6px;
    line-height: 0;
    border-radius: 6px;
    transition: color 0.15s, background 0.15s;
}

.chat-icon-btn:hover:not(:disabled) { color: var(--chat-accent); background: var(--chat-soft); }
.chat-icon-btn:disabled { opacity: 0.35; cursor: default; }

/* Second press deletes — see onDelete() in chat.js. */
.chat-icon-btn.confirm,
.chat-icon-btn.confirm:hover { color: #fff; background: #c0392b; }

/* ── Topics menu ─────────────────────────────────────────────────── */

#chat-topics {
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
    max-height: 46%;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    scrollbar-color: var(--border-strong) transparent;
}

.chat-topics-title {
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.62rem;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: var(--ink-3);
    margin-bottom: 8px;
}

.chat-topics-body .chat-topics-title { margin-top: 12px; }
.chat-topics-body .chat-topics-title:first-child { margin-top: 0; }

.chat-topics-list { display: flex; flex-wrap: wrap; gap: 6px; }

.chat-topics-list button {
    font-family: inherit;
    font-size: 0.74rem;
    font-weight: 500;
    padding: 5px 10px;
    border-radius: 6px;
    border: 1px solid var(--border-strong);
    background: var(--bg);
    color: var(--ink-2);
    cursor: pointer;
    transition: border-color 0.15s, color 0.15s, background 0.15s;
}

.chat-topics-list button:hover {
    border-color: var(--chat-accent);
    color: var(--chat-accent);
    background: var(--chat-soft);
}

/* ── Log ─────────────────────────────────────────────────────────── */

#chat-log {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 16px 14px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scrollbar-width: thin;
    scrollbar-color: var(--border-strong) transparent;
}

#chat-log::-webkit-scrollbar { width: 8px; }
#chat-log::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 4px; }

.chat-msg {
    max-width: 92%;
    padding: 11px 13px;
    border-radius: 12px;
    font-size: 0.855rem;
    line-height: 1.6;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    animation: chat-in 0.24s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes chat-in {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: none; }
}

.chat-msg.bot {
    align-self: flex-start;
    background: var(--surface);
    color: var(--ink-2);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.chat-msg.user {
    align-self: flex-end;
    max-width: 85%;
    background: var(--chat-accent);
    color: var(--chat-on-accent);
    border-bottom-right-radius: 4px;
    white-space: pre-wrap;
}

.chat-msg.error {
    align-self: flex-start;
    background: transparent;
    border: 1px dashed var(--border-strong);
    color: var(--ink-3);
    font-size: 0.76rem;
    padding: 7px 11px;
}

/* ── Structured answer rendering ─────────────────────────────────── */

.chat-msg.bot p { margin: 0 0 0.62em; }
.chat-msg.bot p:last-child { margin-bottom: 0; }

.chat-msg.bot .ans-h {
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--chat-accent);
    margin: 0.9em 0 0.4em;
}

.chat-msg.bot > *:first-child { margin-top: 0; }

.chat-msg.bot .ans-list {
    list-style: none;
    margin: 0 0 0.62em;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 0.3em;
}

.chat-msg.bot .ans-list:last-child { margin-bottom: 0; }

.chat-msg.bot .ans-list li {
    position: relative;
    padding-left: 1.05em;
    line-height: 1.55;
}

.chat-msg.bot .ans-list li::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0.72em;
    width: 5px;
    height: 1.5px;
    background: var(--chat-accent);
    opacity: 0.7;
}

.chat-msg.bot .ans-list li.numbered { padding-left: 1.5em; }
.chat-msg.bot .ans-list li.numbered::before { content: none; }

.ans-num {
    position: absolute;
    left: 0;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--chat-accent);
}

/* ── Suggestion chips ────────────────────────────────────────────── */

.chat-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 0 14px 10px;
    flex-shrink: 0;
}

.chat-suggestions:empty { display: none; }

.chat-suggestions button {
    font-family: inherit;
    font-size: 0.73rem;
    padding: 5px 10px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--ink-3);
    cursor: pointer;
    transition: border-color 0.15s, color 0.15s, background 0.15s;
}

.chat-suggestions button:hover {
    border-color: var(--chat-accent);
    color: var(--chat-accent);
    background: var(--chat-soft);
}

/* ── Composer ────────────────────────────────────────────────────── */

.chat-input-row {
    display: flex;
    gap: 8px;
    padding: 10px 12px;
    border-top: 1px solid var(--border);
    background: var(--bg);
    flex-shrink: 0;
}

#chat-input {
    flex: 1;
    resize: none;
    font-family: inherit;
    font-size: 0.85rem;
    line-height: 1.45;
    padding: 9px 11px;
    border: 1px solid var(--border);
    border-radius: 9px;
    background: var(--surface);
    color: var(--ink);
    max-height: 110px;
}

#chat-input::placeholder { color: var(--ink-3); }

#chat-input:focus {
    outline: none;
    border-color: var(--chat-accent);
    box-shadow: 0 0 0 3px var(--chat-soft);
}

#chat-send {
    flex-shrink: 0;
    border: none;
    border-radius: 9px;
    width: 38px;
    background: var(--chat-accent);
    color: var(--chat-on-accent);
    cursor: pointer;
    display: grid;
    place-items: center;
    transition: background 0.15s, opacity 0.15s;
}

#chat-send:hover:not(:disabled) { background: var(--chat-accent-hover); }
#chat-send:disabled { opacity: 0.4; cursor: default; }

#chat-send .stop-icon { display: none; }
#chat-send.stopping .send-icon { display: none; }
#chat-send.stopping .stop-icon { display: block; }

.chat-foot {
    padding: 0 14px 10px;
    font-size: 0.65rem;
    color: var(--ink-3);
    text-align: center;
    flex-shrink: 0;
}

/* ── Typing indicator ────────────────────────────────────────────── */

.chat-typing { display: flex; gap: 4px; align-items: center; padding: 13px; }

.chat-typing span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--ink-3);
    animation: chat-blink 1.2s infinite ease-in-out;
}

.chat-typing span:nth-child(2) { animation-delay: 0.18s; }
.chat-typing span:nth-child(3) { animation-delay: 0.36s; }

@keyframes chat-blink {
    0%, 60%, 100% { opacity: 0.25; }
    30%           { opacity: 1; }
}

/* ── Conversation bar ────────────────────────────────────────────── */
/* Past conversations, a new one, and delete. The select is the history:
 * every conversation is titled by its first question. */

.chat-bar {
    display: flex;
    align-items: center;
    gap: 3px;
    padding: 8px 11px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
}

#chat-convo {
    flex: 1;
    min-width: 0;
    appearance: none;
    -webkit-appearance: none;
    font-family: inherit;
    font-size: 0.75rem;
    color: var(--ink-2);
    background-color: var(--bg);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%239aa5ac' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 7px center;
    background-size: 13px 13px;
    border: 1px solid var(--border-strong);
    border-radius: 8px;
    padding: 6px 26px 6px 9px;
    cursor: pointer;
    text-overflow: ellipsis;
}

#chat-convo:focus {
    outline: none;
    border-color: var(--chat-accent);
    box-shadow: 0 0 0 3px var(--chat-soft);
}

#chat-convo:disabled { opacity: 0.5; cursor: default; }
#chat-convo option { color: var(--ink); background: var(--bg); }

/* ── Typewriter caret ────────────────────────────────────────────── */

.chat-caret {
    display: inline-block;
    width: 6px;
    height: 0.95em;
    margin-left: 2px;
    vertical-align: -0.14em;
    background: var(--chat-accent);
    opacity: 0.85;
    animation: chat-caret-blink 1s steps(1) infinite;
}

@keyframes chat-caret-blink {
    0%, 49%   { opacity: 0.85; }
    50%, 100% { opacity: 0; }
}

/* Replayed history should not animate in — restoring a long conversation
 * would cascade twenty entry animations at once. */
.chat-msg.no-anim { animation: none; }

/* ── Screen-reader only ──────────────────────────────────────────── */
/* The log itself is not a live region: an answer arriving word by word
 * would be read out word by word. The finished answer lands here instead. */

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

/* ── Responsive ──────────────────────────────────────────────────── */

@media (max-width: 520px) {
    #chat-panel {
        right: 10px;
        left: 10px;
        width: auto;
        bottom: 80px;
        height: min(74vh, calc(100vh - 120px));
    }
    #chat-fab { right: 16px; bottom: 16px; }
    .chat-msg { max-width: 96%; }
}

@media (prefers-reduced-motion: reduce) {
    .chat-msg { animation: none; }
    .chat-typing span { animation: none; opacity: 0.5; }
    .chat-caret { animation: none; }
}
