/* ==========================================================================
   Chat Layout - Mobile-first two-state design
   State 1: Empty (centered composer)
   State 2: Conversation (scrollable messages + docked composer)
   ========================================================================== */

/* Shell: fills available viewport below the app bar */
.chat-shell {
    position: fixed;
    top: var(--mud-appbar-height, 64px);
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding: 0;
    margin: 0;
    z-index: 1; /* above MudMainContent but below dialogs/drawers */
}

/* === EMPTY STATE === */
.chat-shell--empty {
    justify-content: center;
    align-items: center;
}

.chat-empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1.5rem;
    padding: 1rem;
    width: 100%;
    max-width: 600px;
    animation: fadeIn 0.3s ease-out;
}

.chat-greeting {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    text-align: center;
}

.chat-greeting__avatar {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    object-fit: cover;
}

.chat-greeting__title {
    font-weight: 600 !important;
    margin-top: 0.5rem !important;
}

.chat-greeting__subtitle {
    opacity: 0.7;
}

/* Suggestion chips */
.chat-suggestions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.5rem;
    padding: 0 1rem;
    max-width: 100%;
}

.chat-suggestion-chip {
    background: var(--academy-purple-20, #d7d2e0);
    color: var(--academy-purple, #351f65);
    border: 1px solid var(--academy-purple-40, #aea5c1);
    border-radius: 20px;
    padding: 0.6rem 1rem;
    font-size: 0.875rem;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.2s ease;
    line-height: 1.3;
    text-align: left;
}

.chat-suggestion-chip:hover {
    background: var(--academy-purple-40, #aea5c1);
    color: white;
    transform: translateY(-1px);
}

.chat-suggestion-chip:active {
    transform: scale(0.97);
}

/* === COMPOSER (shared styles) === */
.chat-composer {
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
    width: 100%;
    max-width: 600px;
    padding: 0 1rem;
}

.chat-composer__input-wrap {
    flex: 1;
    min-width: 0;
}

/* === Voice Input Container: stacked layout (textarea on top, controls below) === */
.voice-input-container {
    display: flex;
    flex-direction: column;
    border: 1.5px solid var(--academy-purple-20, #d7d2e0);
    border-radius: 24px;
    background: var(--mud-palette-surface, #fff);
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
    padding: 0.75rem 1rem;
    transition:
        border-color 0.2s,
        box-shadow 0.2s;
    width: 100%;
}

.voice-input-container:focus-within {
    border-color: var(--academy-purple, #351f65);
    box-shadow: 0 2px 16px rgba(53, 31, 101, 0.15);
}

/* The text field inside: no border, flush with container */
.voice-input-field {
    flex: 1;
    min-width: 0;
}

.voice-input-field .mud-input-root {
    padding: 0 !important;
    margin: 0 !important;
}

/* Text area padding — match visual distance to container edge */
.voice-input-field .mud-input-slot,
.voice-input-field textarea {
    padding: 0.5rem 0.25rem !important;
}

/* Remove underline from Variant.Text */
.voice-input-field .mud-input::before,
.voice-input-field .mud-input::after {
    display: none !important;
}

/* Controls row: counter left, mic + send right */
.voice-input-controls {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: 0.25rem;
}

.voice-input-counter {
    font-size: 0.75rem;
    color: var(--academy-gray-60, #989a9c);
    white-space: nowrap;
    padding-left: 0.25rem;
}

/* Mic and send buttons — 44px minimum touch target for mobile */
.voice-input-mic,
.voice-input-send {
    flex-shrink: 0;
    width: 44px !important;
    height: 44px !important;
}

.voice-input-mic .mud-icon-root,
.voice-input-send .mud-icon-root {
    font-size: 1.5rem !important;
}

/* Centered state */
.chat-composer--centered {
    margin: 0 auto;
}

/* Docked state */
.chat-composer--docked {
    position: sticky;
    bottom: 0;
    margin: 0 auto;
    padding: 0.75rem 1rem;
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0px));
    background: var(--mud-palette-background, #fff);
    border-top: 1px solid var(--academy-purple-20, #d7d2e0);
    max-width: 100%;
    z-index: 10;
}

.chat-clear-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 1px solid var(--academy-purple-20, #d7d2e0);
    background: transparent;
    color: var(--academy-gray, #53565a);
    cursor: pointer;
    flex-shrink: 0;
    margin-bottom: 0.5rem;
    transition: all 0.2s;
}

.chat-clear-btn:hover {
    background: var(--academy-purple-20, #d7d2e0);
    color: var(--academy-purple, #351f65);
}

.chat-clear-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* === CONVERSATION STATE: Messages area === */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 1rem 1rem 0.5rem;
    padding-top: max(1rem, env(safe-area-inset-top));
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    scroll-behavior: smooth;
}

/* Spacer after last bubble so it's never hidden behind the docked composer */
.chat-messages::after {
    content: "";
    display: block;
    min-height: 0.5rem;
    flex-shrink: 0;
}

/* === CHAT BUBBLES === */
.chat-bubble {
    max-width: 85%;
    padding: 0.875rem 1.125rem;
    border-radius: 18px;
    line-height: 1.5;
    font-size: 0.9375rem;
    animation: bubbleIn 0.25s ease-out;
    word-break: break-word;
}

.chat-bubble--user {
    align-self: flex-end;
    background: var(--academy-purple, #351f65);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.chat-bubble--assistant {
    align-self: stretch;
    max-width: 100%;
    background: var(--mud-palette-surface, #f5f5f5);
    color: var(--mud-palette-text-primary, #333);
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}

.chat-bubble--error {
    border-left: 3px solid var(--academy-red, #d05a57);
}

/* Remove MudPaper elevation inside assistant bubbles */
.chat-bubble--assistant .mud-paper {
    box-shadow: none !important;
    background: transparent !important;
    padding: 0 !important;
}

/* === RENDERED MARKDOWN (answer body) === */
/* Markdig emits block <p>/<ul>/headings; give them real spacing (they lose it inside chat bubbles). */
.chat-markdown {
    line-height: 1.5;
}

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

.chat-markdown > *:last-child {
    margin-bottom: 0;
}

.chat-markdown p {
    margin: 0 0 0.75em;
}

.chat-markdown ul,
.chat-markdown ol {
    margin: 0 0 0.75em;
    padding-left: 1.5em;
}

.chat-markdown li {
    margin-bottom: 0.25em;
}

.chat-markdown li:last-child {
    margin-bottom: 0;
}

.chat-markdown h1,
.chat-markdown h2,
.chat-markdown h3,
.chat-markdown h4 {
    margin: 0.75em 0 0.4em;
    font-weight: 600;
    line-height: 1.3;
}

.chat-markdown a {
    color: var(--mud-palette-primary);
    text-decoration: underline;
}

/* Inline citation marker ([N] -> superscript chip linking to the source). */
.chat-markdown a.citation-link {
    text-decoration: none;
    margin-left: 1px;
}

/* Space between the dev-only tab header (Answer/Thought/Supporting) and its content. */
.answer-tabs .mud-tabs-panels {
    padding-top: 16px;
}

.chat-markdown code {
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    background: var(--mud-palette-action-default-hover, rgba(0, 0, 0, 0.06));
    padding: 0.1em 0.3em;
    border-radius: 4px;
    font-size: 0.9em;
}

.chat-markdown pre {
    margin: 0 0 0.75em;
    padding: 0.75em;
    overflow-x: auto;
    background: var(--mud-palette-action-default-hover, rgba(0, 0, 0, 0.06));
    border-radius: 6px;
}

/* === PIPELINE STATUS LINE (transient progress, above the response) === */
.chat-status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.75rem;
    line-height: 1.3;
    color: var(--mud-palette-text-secondary);
    opacity: 0.85;
    margin-bottom: 6px;
    font-style: italic;
    animation: fadeIn 0.2s ease-in;
}

.chat-status__spinner {
    height: 14px !important;
    width: 14px !important;
    flex: 0 0 auto;
}

/* === THINKING INDICATOR (replaces spinner) === */
.chat-thinking {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 0.25rem 0;
}

.chat-thinking__dot {
    width: 8px;
    height: 8px;
    background: var(--academy-purple, #351f65);
    border-radius: 50%;
    opacity: 0.4;
    animation: thinkingPulse 1.4s ease-in-out infinite;
}

.chat-thinking__dot:nth-child(2) {
    animation-delay: 0.2s;
}

.chat-thinking__dot:nth-child(3) {
    animation-delay: 0.4s;
}

/* === ANIMATIONS === */
@keyframes thinkingPulse {
    0%,
    60%,
    100% {
        opacity: 0.3;
        transform: scale(0.8);
    }
    30% {
        opacity: 1;
        transform: scale(1);
    }
}

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

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

/* Streaming cursor - blinking caret */
.chat-streaming-cursor {
    display: inline-block;
    width: 2px;
    height: 1em;
    background: var(--mud-palette-text-primary, #53565a);
    margin-left: 2px;
    vertical-align: text-bottom;
    animation: cursorBlink 0.8s steps(2) infinite;
}

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

/* === RESPONSIVE: Tablet+ === */
@media (min-width: 600px) {
    .chat-shell {
        left: 50%;
        right: auto;
        transform: translateX(-50%);
        max-width: 720px;
        width: 100%;
    }

    .chat-greeting__avatar {
        width: 100px;
        height: 100px;
    }

    .chat-bubble {
        max-width: 70%;
        font-size: 1rem;
    }

    /* Answer fills the content column, matching the composer width */
    .chat-bubble--assistant {
        max-width: 100%;
    }

    .chat-composer--docked {
        max-width: 720px;
        padding-left: 1.5rem;
        padding-right: 1.5rem;
    }
}

/* === DARK THEME ADJUSTMENTS === */
.mud-theme-dark .chat-bubble--assistant {
    background: var(--mud-palette-surface, #2d2d2d);
}

.mud-theme-dark .chat-suggestion-chip {
    background: rgba(255, 255, 255, 0.08);
    color: var(--academy-purple-20, #d7d2e0);
    border-color: rgba(255, 255, 255, 0.15);
}

.mud-theme-dark .chat-suggestion-chip:hover {
    background: rgba(255, 255, 255, 0.15);
    color: white;
}

.mud-theme-dark .voice-input-container {
    background: var(--mud-palette-surface, #2d2d2d);
    border-color: rgba(255, 255, 255, 0.15);
}

.mud-theme-dark .chat-composer--docked {
    background: var(--mud-palette-background, #1e1e1e);
    border-top-color: rgba(255, 255, 255, 0.1);
}

.mud-theme-dark .chat-clear-btn {
    border-color: rgba(255, 255, 255, 0.15);
    color: rgba(255, 255, 255, 0.7);
}

.mud-theme-dark .chat-clear-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}
