/* --- css/chat.css --- */

/*
  --- Design System Variables ---
  This makes the theme easy to customize.
  For site-wide consistency, you can move this :root block to your css/global.css file.
*/
:root {
    /* Color Palette - New Dark Theme */
    --color-primary: #00BFFF; /* Deep Sky Blue - A vibrant accent */
    --color-primary-dark: #009ACD;
    --bg-dark-primary: #161C29;   /* Dark Navy */
    --bg-dark-secondary: #21283A; /* Slightly Lighter Navy */
    --text-light-primary: #EAEAEA;
    --text-light-secondary: #a0a0a0;
    --border-color: #343A4B;

    /* Sizing & Spacing */
    --border-radius: 12px;
    --spacing-sm: 8px;
    --spacing-md: 16px;
    --spacing-lg: 24px;
}

/* --- Custom Scrollbar for a polished look --- */
::-webkit-scrollbar {
    width: 8px;
}
::-webkit-scrollbar-track {
    background: var(--bg-dark-secondary, #21283A);
}
::-webkit-scrollbar-thumb {
    background: var(--border-color, #343A4B);
    border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* --- Main Layout --- */
.chat-container {
    max-width: 800px;
    margin: 2rem auto;
    display: flex;
    flex-direction: column;
    height: calc(100vh - 100px);
    max-height: 850px;
    background-color: var(--bg-dark-primary, #161C29);
    border-radius: var(--border-radius, 12px);
    box-shadow: 0 10px 30px rgba(0,0,0,0.3);
    overflow: hidden;
    border: 1px solid var(--border-color, #343A4B);
}

.chat-header {
    background-color: transparent;
    color: var(--text-light-primary, #EAEAEA);
    padding: var(--spacing-lg, 24px);
    border-bottom: 1px solid var(--border-color, #343A4B);
    flex-shrink: 0;
}

/* NEW: Added flexbox to align title and clear button */
.chat-header__title-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--spacing-md);
}

.chat-header__back-link {
    color: var(--text-light-secondary, #a0a0a0);
    text-decoration: none;
    margin-bottom: var(--spacing-md, 16px);
    display: inline-block;
    transition: color 0.2s ease;
}
.chat-header__back-link:hover {
    color: var(--color-primary, #00BFFF);
}

.chat-header__title h1 {
    margin: 0;
    font-family: var(--font-heading, 'Bebas Neue', sans-serif);
    font-size: 3rem;
    line-height: 1;
    color: #fff;
}

.chat-header__title h2 {
    margin: 0;
    font-family: var(--font-body, 'Inter', sans-serif);
    font-weight: 400;
    font-size: 1.1rem;
    color: var(--text-light-secondary, #a0a0a0);
}

/* CORRECTED: The "overflow: hidden;" line has been removed from this block */
.chat-content {
    padding: var(--spacing-lg, 24px);
    display: flex;
    flex-direction: column;
    flex-grow: 1;
    background-color: transparent;
    min-height: 0; /* Fix for flexbox overflow */
}

/* --- Controls & Input --- */
.chat-controls {
    margin-bottom: var(--spacing-md, 16px);
    display: flex;
    align-items: center;
    gap: var(--spacing-sm, 8px);
    color: var(--text-light-primary, #EAEAEA);
}

#character-select {
    flex-grow: 1;
    padding: 10px;
    border-radius: var(--spacing-sm, 8px);
    border: 1px solid var(--border-color, #343A4B);
    font-family: var(--font-body, 'Inter', sans-serif);
    background-color: var(--bg-dark-secondary, #21283A);
    color: var(--text-light-primary, #EAEAEA);
    font-size: 1rem;
}

.clear-chat-button {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
    background: var(--bg-dark-secondary);
    border: 1px solid var(--border-color);
    color: var(--text-light-secondary);
    padding: 8px 12px;
    border-radius: var(--spacing-sm);
    cursor: pointer;
    transition: all 0.2s ease;
}
.clear-chat-button:not(:disabled):hover {
    color: #ff5555;
    border-color: #581d1d;
}
.clear-chat-button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}
.clear-chat-button svg {
    width: 16px;
    height: 16px;
    fill: currentColor;
}


.chat-form {
    display: flex;
    gap: var(--spacing-md, 16px);
    padding: var(--spacing-md, 16px);
    margin: var(--spacing-md, 16px) -24px -24px -24px; /* Extend to edges */
    background-color: var(--bg-dark-secondary, #21283A);
    border-top: 1px solid var(--border-color, #343A4B);
    align-items: center;
}

#message-input {
    flex: 1;
    padding: 12px 18px;
    border-radius: 22px;
    border: 1px solid var(--border-color, #343A4B);
    background-color: var(--bg-dark-primary, #161C29);
    font-family: var(--font-body, 'Inter', sans-serif);
    font-size: 1rem;
    color: var(--text-light-primary, #EAEAEA);
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
#message-input:focus {
    outline: none;
    border-color: var(--color-primary, #00BFFF);
    box-shadow: 0 0 0 3px rgba(0, 191, 255, 0.2);
}
#message-input:disabled {
    background-color: var(--bg-dark-primary, #161C29);
}

#send-button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: none;
    background-color: var(--color-primary, #00BFFF);
    color: #fff;
    cursor: pointer;
    transition: all 0.2s ease;
    flex-shrink: 0;
}
#send-button:hover {
    background-color: var(--color-primary-dark, #009ACD);
}
#send-button:active {
    transform: scale(0.95);
}
#send-button:disabled {
    background-color: var(--border-color, #343A4B);
    cursor: not-allowed;
    opacity: 0.5;
}
#send-button svg {
    width: 24px;
    height: 24px;
    margin-left: 2px; /* Optical alignment */
}


/* --- Chat Window & Messages --- */
.chat-window {
    flex-grow: 1;
    overflow-y: auto;
    padding: var(--spacing-sm, 8px) var(--spacing-md, 16px);
    border: 1px solid var(--border-color, #343A4B);
    border-radius: var(--border-radius);
    position: relative; /* For empty state positioning */
    background-color: var(--bg-dark-primary, #161C29);
    background-image:
        linear-gradient(rgba(45, 52, 71, 0.5) 1px, transparent 1px),
        linear-gradient(to right, rgba(45, 52, 71, 0.5) 1px, transparent 1px);
    background-size: 30px 30px;
}

.chat-empty-state {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    color: var(--text-light-secondary);
    pointer-events: none; /* Allows clicks to go through */
    opacity: 0;
    transition: opacity 0.3s ease;
}
.chat-empty-state.visible {
    opacity: 1;
}
.chat-empty-state__icon {
    width: 60px;
    height: 60px;
    margin-bottom: var(--spacing-md);
    opacity: 0.5;
}
.chat-empty-state__title {
    font-family: var(--font-heading);
    font-size: 1.5rem;
    color: var(--text-light-primary);
    margin-bottom: var(--spacing-sm);
}
.chat-empty-state__text {
    font-size: 1rem;
    margin: 0;
}

.chat-messages {
    list-style-type: none;
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md, 16px);
}

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

.message {
    display: flex;
    flex-direction: column; /* Changed for footer layout */
    gap: 4px; /* Small gap between core and footer */
    max-width: 80%;
    animation: fadeIn 0.3s ease-out;
}

.message__core {
    display: flex;
    gap: 10px;
    align-items: flex-end;
}

.message__avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: contain;
    flex-shrink: 0;
    border: 2px solid var(--border-color);
}

.message__content {
    padding: 10px 16px;
    border-radius: 18px;
    font-size: 0.95rem;
    word-wrap: break-word;
    color: var(--text-light-primary, #EAEAEA);
    line-height: 1.5;
}
.message__content p:last-child {
    margin-bottom: 0;
}

/* Message Variants */
.message--user {
    align-self: flex-end;
    align-items: flex-end;
}
.message--user .message__core {
    flex-direction: row-reverse;
}
.message--user .message__content {
    background-color: var(--color-primary, #00BFFF);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.message--bot {
    align-self: flex-start;
    align-items: flex-start;
}
.message--bot .message__content {
    background-color: var(--bg-dark-secondary, #21283A);
    border-bottom-left-radius: 4px;
}

.message--error .message__content {
    background-color: #581d1d;
    color: #ffacac;
    border: 1px solid #a13d3d;
}

/* Typing Indicator */
.bot-message.typing-indicator .message__content {
    padding: 10px 15px;
}
.typing-indicator span {
    height: 8px;
    width: 8px;
    background-color: var(--text-light-secondary, #a0a0a0);
    border-radius: 50%;
    display: inline-block;
    margin: 0 2px;
    animation: bounce 1.4s infinite ease-in-out both;
}
.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }

@keyframes bounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1.0); }
}

/* --- Responsive --- */
@media (max-width: 768px) {
    .chat-container {
        margin: 0; height: 100vh; max-height: none; border-radius: 0; border: none;
    }
    .chat-content { padding: var(--spacing-md, 16px); }
    .chat-form { margin: var(--spacing-md, 16px) -16px -16px -16px; }
}


/* --- NEW: Message Footer and Play Button --- */
.message__footer {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    padding-left: 50px; /* Aligns with the start of the message bubble */
    gap: var(--spacing-sm);
}
.message--user .message__footer {
    justify-content: flex-end;
    padding-left: 0;
    padding-right: 50px;
}

.message__timestamp {
    font-size: 0.75rem;
    color: var(--text-light-secondary);
    opacity: 0.7;
}

.play-audio-button {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-light-secondary);
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    opacity: 0;
    pointer-events: none;
    flex-shrink: 0;
}

.play-audio-button:not(:disabled) {
    opacity: 0.7;
    pointer-events: auto;
}
.play-audio-button:not(:disabled):hover {
    opacity: 1;
    border-color: var(--color-primary);
    color: var(--color-primary);
}

.play-audio-button svg {
    width: 14px;
    height: 14px;
    fill: currentColor;
    margin-left: 2px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.play-audio-button.loading svg {
    display: none;
}
.play-audio-button.loading::after {
    content: '';
    display: block;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: 2px solid var(--border-color);
    border-top-color: var(--color-primary);
    animation: spin 1s linear infinite;
}

.play-audio-button.playing {
    border-color: var(--color-primary);
    color: var(--color-primary);
    opacity: 1;
}

.play-audio-button.error {
    opacity: 0.7;
    color: #ff5555;
    border-color: #581d1d;
    cursor: not-allowed;
}
.play-audio-button.error svg {
    display: none;
}