/* ===========================================
   Component: Chat — Soelya
   Premium conversational UI
   =========================================== */

/* ---- Chat Section ---- */
.chat {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: calc(var(--header-height) + var(--space-8)) var(--space-6) var(--space-6);
    display: none;
}

.chat--active {
    display: block;
}

.chat__messages {
    max-width: 780px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: var(--space-5);
}

/* ---- Message Bubble ---- */
.chat__message {
    display: flex;
    gap: var(--space-3);
    max-width: 85%;
    opacity: 0;
    animation: msgAppear 0.5s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.chat__message--user {
    align-self: flex-end;
    flex-direction: row-reverse;
    animation-name: msgAppearRight;
}

.chat__message--bot {
    align-self: flex-start;
    animation-name: msgAppearLeft;
}

@keyframes msgAppearLeft {
    from { opacity: 0; transform: translateX(-16px) translateY(8px); }
    to   { opacity: 1; transform: translateX(0) translateY(0); }
}

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

/* ---- Avatar ---- */
.chat__avatar {
    width: 36px;
    height: 36px;
    border-radius: var(--radius-full);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--font-size-sm);
    margin-top: 2px;
    transition: transform 0.3s ease;
}

.chat__message:hover .chat__avatar {
    transform: scale(1.08);
}

.chat__avatar--user {
    background: linear-gradient(135deg, var(--color-accent), #5a3fd4);
    color: #fff;
    font-weight: 600;
    box-shadow: 0 2px 12px rgba(124, 92, 252, 0.3);
}

.chat__avatar--bot {
    background: var(--color-bg-secondary);
    border: 1px solid rgba(124, 92, 252, 0.2);
    overflow: hidden;
    position: relative;
    box-shadow: 0 2px 12px rgba(124, 92, 252, 0.15);
}

.chat__avatar--bot::before {
    content: '';
    position: absolute;
    inset: -1px;
    border-radius: var(--radius-full);
    background: conic-gradient(from 0deg, var(--color-accent), transparent, var(--color-accent));
    opacity: 0;
    transition: opacity 0.6s ease;
    z-index: -1;
    animation: avatarGlow 3s linear infinite;
}

.chat__message--bot:last-child .chat__avatar--bot::before {
    opacity: 0.6;
}

@keyframes avatarGlow {
    from { transform: rotate(0deg); }
    to   { transform: rotate(360deg); }
}

.chat__avatar--bot img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* ---- Bubble ---- */
.chat__bubble {
    padding: var(--space-3) var(--space-5);
    border-radius: 20px;
    font-size: var(--font-size-base);
    line-height: 1.75;
    word-wrap: break-word;
    position: relative;
}

/* User bubble — gradient accent */
.chat__message--user .chat__bubble {
    background: linear-gradient(135deg, var(--color-accent) 0%, #5a3fd4 100%);
    color: #fff;
    border-bottom-right-radius: 6px;
    box-shadow:
        0 2px 16px rgba(124, 92, 252, 0.25),
        inset 0 1px 0 rgba(255, 255, 255, 0.1);
}

/* Bot bubble — glassmorphism premium */
.chat__message--bot .chat__bubble {
    background: rgba(22, 22, 31, 0.7);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid rgba(124, 92, 252, 0.1);
    color: var(--color-text-primary);
    border-bottom-left-radius: 6px;
    box-shadow:
        0 4px 24px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.03);
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.chat__message--bot:hover .chat__bubble {
    border-color: rgba(124, 92, 252, 0.2);
    box-shadow:
        0 4px 24px rgba(0, 0, 0, 0.3),
        0 0 20px rgba(124, 92, 252, 0.06),
        inset 0 1px 0 rgba(255, 255, 255, 0.03);
}

.chat__bubble strong {
    font-weight: 600;
    color: var(--color-accent-hover);
}

/* ---- Timestamp (optional future) ---- */
.chat__time {
    font-size: 0.68rem;
    color: var(--color-text-tertiary);
    margin-top: var(--space-1);
    opacity: 0;
    transition: opacity 0.2s ease;
}

.chat__message:hover .chat__time {
    opacity: 1;
}

/* ---- Typing Indicator ---- */
.chat__typing {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: var(--space-3) var(--space-4);
}

.chat__typing-dot {
    width: 7px;
    height: 7px;
    border-radius: var(--radius-full);
    background: var(--color-accent);
    opacity: 0.4;
    animation: typingPulse 1.4s ease-in-out infinite;
}

.chat__typing-dot:nth-child(2) { animation-delay: 0.2s; }
.chat__typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingPulse {
    0%, 60%, 100% { transform: scale(1); opacity: 0.3; }
    30% { transform: scale(1.4); opacity: 1; }
}

/* ---- Thinking Block (ChatGPT-style) ---- */
.chat__thinking {
    border-radius: 16px;
    padding: 12px 16px;
    margin-bottom: 8px;
    background: linear-gradient(135deg,
        rgba(124, 92, 252, 0.08) 0%,
        rgba(168, 140, 255, 0.05) 100%);
    border: 1px solid rgba(124, 92, 252, 0.15);
    overflow: hidden;
    transition: border-color 0.4s cubic-bezier(0.4, 0, 0.2, 1),
                background 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Pulsing glow while thinking is active */
.chat__thinking:not(.chat__thinking--done) {
    animation: thinkingPulse 2s ease-in-out infinite;
}

@keyframes thinkingPulse {
    0%, 100% { border-color: rgba(124, 92, 252, 0.15);
               box-shadow: 0 0 0 0 rgba(124, 92, 252, 0); }
    50%      { border-color: rgba(124, 92, 252, 0.4);
               box-shadow: 0 0 12px 0 rgba(124, 92, 252, 0.08); }
}

.chat__thinking-header {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    user-select: none;
}

.chat__thinking-icon {
    font-size: 14px;
    color: var(--color-accent, #7c5cfc);
    animation: thinkingSpin 2s linear infinite;
}

.chat__thinking--done .chat__thinking-icon {
    animation: none;
}

.chat__thinking-label {
    font-size: 13px;
    font-weight: 500;
    color: rgba(255, 255, 255, 0.7);
    letter-spacing: 0.02em;
}

.chat__thinking--done .chat__thinking-label::after {
    content: ' terminée';
}

.chat__thinking-timer {
    font-size: 11px;
    color: rgba(255, 255, 255, 0.35);
    font-variant-numeric: tabular-nums;
    min-width: 24px;
}

.chat__thinking-spinner {
    width: 14px;
    height: 14px;
    border: 2px solid rgba(124, 92, 252, 0.2);
    border-top-color: var(--color-accent, #7c5cfc);
    border-radius: 50%;
    animation: thinkingSpin 0.8s linear infinite;
    margin-left: auto;
}

.chat__thinking--done .chat__thinking-spinner {
    display: none;
}

.chat__thinking--done .chat__thinking-header::after {
    content: '▾';
    margin-left: auto;
    color: rgba(255, 255, 255, 0.4);
    font-size: 12px;
    transition: transform 0.3s ease;
}

.chat__thinking--collapsed .chat__thinking-header::after {
    transform: rotate(-90deg);
}

.chat__thinking-steps {
    margin-top: 8px;
    display: flex;
    flex-direction: column;
    gap: 3px;
    will-change: max-height, opacity;
    transition: max-height 0.4s cubic-bezier(0.4, 0, 0.2, 1),
                opacity 0.3s ease,
                margin-top 0.3s ease;
}

.chat__thinking--collapsed .chat__thinking-steps {
    max-height: 0 !important;
    opacity: 0;
    margin-top: 0;
    overflow: hidden;
}

.chat__thinking-step {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.5);
    padding: 2px 0 2px 20px;
    position: relative;
    opacity: 0;
    transform: translateX(-8px);
    transition: opacity 0.3s ease, transform 0.3s ease;
}

.chat__thinking-step--visible {
    opacity: 1;
    transform: translateX(0);
}

.chat__thinking-step::before {
    content: '›';
    position: absolute;
    left: 6px;
    color: rgba(124, 92, 252, 0.5);
    font-weight: bold;
}

@keyframes thinkingSpin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

/* ---- Cursor blink for typing effect ---- */
.chat__cursor {
    display: inline-block;
    width: 2px;
    height: 1.15em;
    background: var(--color-accent);
    margin-left: 2px;
    vertical-align: text-bottom;
    border-radius: 1px;
    animation: cursorBlink 0.9s steps(2) infinite;
    box-shadow: 0 0 6px var(--color-accent-glow);
}

@keyframes cursorBlink {
    0% { opacity: 1; }
    50% { opacity: 0; }
}

/* ---- Input Area ---- */
/* En mode hero : dans le flux, sous les suggestions */
.chat__input-area {
    width: 100%;
    max-width: 780px;
    margin: 0 auto;
    padding: var(--space-6) var(--space-6) var(--space-8);
}

/* En mode chat : fixe en bas */
body.chat-active .chat__input-area {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-width: none;
    z-index: 50;
    background: rgba(10, 10, 15, 0.85);
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    border-top: 1px solid rgba(124, 92, 252, 0.08);
    padding: var(--space-4) var(--space-6);
}

body.chat-active .chat__input-area .search {
    max-width: 780px;
    margin: 0 auto;
}

/* ---- Hero adjustments when in chat mode ---- */
.hero--hidden {
    display: none;
}

/* ---- Markdown in bot bubbles ---- */
.chat__bubble--md p {
    margin: 0 0 0.5em;
}
.chat__bubble--md p:last-child {
    margin-bottom: 0;
}
.chat__bubble--md strong {
    font-weight: 600;
    color: var(--color-accent-hover);
}
.chat__bubble--md em {
    font-style: italic;
    opacity: 0.9;
}
.chat__bubble--md code {
    font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
    font-size: 0.88em;
    background: rgba(124, 92, 252, 0.12);
    color: #c4b5fd;
    padding: 0.15em 0.4em;
    border-radius: 4px;
}
.chat__bubble--md pre {
    margin: 0.6em 0;
    padding: var(--space-3);
    background: rgba(0, 0, 0, 0.35);
    border-radius: var(--radius-md);
    overflow-x: auto;
}
.chat__bubble--md pre code {
    background: none;
    padding: 0;
    color: #e2e8f0;
    font-size: 0.85em;
}

/* ---- Code Block (avec header + copier + aperçu) ---- */
.code-block {
    margin: 0.8em 0;
    border-radius: var(--radius-md);
    overflow: hidden;
    border: 1px solid rgba(124, 92, 252, 0.15);
    background: rgba(0, 0, 0, 0.4);
}
.code-block__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.4em 0.8em;
    background: rgba(124, 92, 252, 0.12);
    border-bottom: 1px solid rgba(124, 92, 252, 0.1);
    font-size: 0.78em;
}
.code-block__lang {
    color: var(--color-accent);
    font-weight: 600;
    font-size: 0.85em;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}
.code-block__actions {
    display: flex;
    gap: 0.4em;
}
.code-block__copy,
.code-block__preview {
    font-family: inherit;
    font-size: 0.85em;
    padding: 0.2em 0.6em;
    border-radius: 4px;
    border: 1px solid rgba(255, 255, 255, 0.15);
    background: rgba(255, 255, 255, 0.06);
    color: rgba(255, 255, 255, 0.65);
    cursor: pointer;
    transition: all 0.2s ease;
}
.code-block__copy:hover,
.code-block__preview:hover {
    background: rgba(124, 92, 252, 0.2);
    border-color: rgba(124, 92, 252, 0.4);
    color: #fff;
}
.code-block pre {
    margin: 0;
    padding: 0.8em 1em;
    background: none;
    border-radius: 0;
    max-height: 500px;
    overflow: auto;
}
.code-block pre code {
    background: none;
    padding: 0;
    color: #e2e8f0;
    font-size: 0.82em;
    line-height: 1.55;
    tab-size: 2;
    white-space: pre;
}
.code-block--streaming {
    border-color: rgba(124, 92, 252, 0.3);
}
.code-block--streaming .code-block__header {
    background: rgba(124, 92, 252, 0.18);
}
.code-block--streaming .code-block__lang::after {
    content: '';
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--color-accent);
    margin-left: 0.5em;
    animation: codePulse 1s ease-in-out infinite;
}
@keyframes codePulse {
    0%, 100% { opacity: 0.3; }
    50% { opacity: 1; }
}

/* ---- Aperçu HTML en direct ---- */
.code-preview {
    margin-top: 0.6em;
    border-radius: var(--radius-md);
    overflow: hidden;
    border: 1px solid rgba(124, 92, 252, 0.2);
    background: #fff;
}
.code-preview__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.35em 0.7em;
    background: rgba(124, 92, 252, 0.1);
    font-size: 0.78em;
    color: var(--color-accent);
    font-weight: 600;
}
.code-preview__header button {
    background: none;
    border: none;
    color: rgba(255, 255, 255, 0.5);
    cursor: pointer;
    font-size: 1.1em;
    padding: 0 0.3em;
    transition: color 0.2s;
}
.code-preview__header button:hover {
    color: #fff;
}
.code-preview__frame {
    width: 100%;
    height: 400px;
    border: none;
    display: block;
}
.chat__bubble--md ul {
    margin: 0.4em 0;
    padding-left: 1.2em;
    list-style: none;
}
.chat__bubble--md ul li {
    position: relative;
    padding-left: 0.2em;
    margin-bottom: 0.25em;
}
.chat__bubble--md ul li::before {
    content: '›';
    position: absolute;
    left: -1em;
    color: var(--color-accent);
    font-weight: 600;
}

/* ---- Content wrapper (bubble + chips) ---- */
.chat__content {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    min-width: 0;
}

/* ---- Suggestion Chips ---- */
.chat__suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    margin-top: var(--space-1);
    opacity: 0;
    transform: translateY(6px);
    transition: opacity 0.4s ease, transform 0.4s ease;
}
.chat__suggestions--visible {
    opacity: 1;
    transform: translateY(0);
}
.chat__suggestion-chip {
    display: inline-flex;
    align-items: center;
    padding: 0.4em 0.9em;
    font-size: var(--font-size-sm);
    font-family: inherit;
    color: var(--color-accent);
    background: rgba(124, 92, 252, 0.08);
    border: 1px solid rgba(124, 92, 252, 0.2);
    border-radius: 999px;
    cursor: pointer;
    transition: all 0.25s ease;
    white-space: nowrap;
}
.chat__suggestion-chip:hover {
    background: rgba(124, 92, 252, 0.18);
    border-color: rgba(124, 92, 252, 0.4);
    color: #fff;
    transform: translateY(-1px);
    box-shadow: 0 2px 10px rgba(124, 92, 252, 0.2);
}

/* ---- Speak Button (TTS) ---- */
.chat__speak-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    padding: 0;
    margin-top: 6px;
    background: rgba(124, 92, 252, 0.08);
    border: 1px solid rgba(124, 92, 252, 0.15);
    border-radius: 50%;
    color: rgba(255, 255, 255, 0.45);
    cursor: pointer;
    transition: all 0.25s ease;
    float: right;
}
.chat__speak-btn:hover {
    background: rgba(124, 92, 252, 0.2);
    border-color: rgba(124, 92, 252, 0.4);
    color: var(--color-accent);
    transform: scale(1.1);
}
.chat__speak-btn--active {
    background: rgba(124, 92, 252, 0.25);
    border-color: var(--color-accent);
    color: var(--color-accent);
    animation: speakPulse 1s ease-in-out infinite;
}
@keyframes speakPulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(124, 92, 252, 0.3); }
    50% { box-shadow: 0 0 0 6px rgba(124, 92, 252, 0); }
}
.chat__speak-btn svg {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
}
