/* ============================================
   CHAT PAGE LAYOUT - Complete Redesign
   ============================================ */

/* == CHAT CSS VARIABLES (override in tenant CSS) == */
:root {
    --chat-page-bg:             transparent;
    --chat-card-bg:             var(--color-surface);
    --chat-card-border:         none;
    --chat-card-shadow:         0 2px 8px rgba(0, 0, 0, 0.1);
    --chat-card-radius:         12px;
    --chat-header-bg:           inherit;
    --chat-header-border:       inherit;
    --chat-header-color:        inherit;
    --chat-input-area-bg:       inherit;
    --chat-input-area-border:   1px solid var(--color-border);
    --chat-input-bg:            inherit;
    --chat-input-border:        inherit;
    --chat-input-color:         inherit;
    --chat-input-placeholder:   inherit;
    --chat-typing-bg:           var(--color-surface);
    --chat-typing-color:        var(--color-text);
    --chat-typing-border:       1px solid var(--color-border);
    --chat-progress-color:      #fbbf24;
    --chat-bubble-bg:           var(--color-surface);
    --chat-bubble-color:        var(--color-text);
    --chat-bubble-border:       2px solid #fbbf24;
}

/* == FULL PAGE LAYOUT == */

/* Full viewport layout */
html, body.chat-page {
    height: 100vh !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
}

body.chat-page {
    display: flex !important;
    flex-direction: column !important;
    background: var(--chat-page-bg) !important;
}

/* Hide footer on chat page */
body.chat-page footer {
    display: none !important;
}

/* Navbar stays visible but doesn't grow */
body.chat-page nav.navbar {
    flex: 0 0 auto !important;
}

/* Main element - part of flex chain between body and content-container */
body.chat-page > main {
    flex: 1 1 auto !important;
    display: flex !important;
    flex-direction: column !important;
    min-height: 0 !important;
}

/* Main content area fills remaining space */
body.chat-page .content-container {
    flex: 1 1 auto !important;
    display: flex !important;
    flex-direction: column !important;
    min-height: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
    max-width: 100% !important;
}

/* Container fluid - override external CSS conflicts */
body.chat-page .container-fluid {
    flex: 1 1 auto !important;
    display: flex !important;
    flex-direction: column !important;
    min-height: 0 !important;
    height: auto !important; /* Override height: 100vh from new_style.css */
    max-height: 100% !important; /* Prevent growing beyond parent */
    padding: 0.5rem !important;
    overflow: visible !important; /* Override overflow: hidden from new_style.css */
}

/* Row fills height */
body.chat-page .container-fluid > .row {
    flex: 1 1 auto !important;
    display: flex !important;
    min-height: 0 !important;
    max-height: 100% !important; /* Prevent growing beyond parent */
    margin: 0 !important;
}

/* Column fills height and centers content */
body.chat-page .container-fluid > .row > .col-12 {
    flex: 1 1 auto !important;
    display: flex !important;
    flex-direction: column !important;
    min-height: 0 !important;
    max-height: 100% !important; /* Prevent growing beyond parent */
    padding-left: 12px !important;
    padding-right: 12px !important;
}

/* Desktop only: Center and constrain width to match sessions manager (1200px) */
@media (min-width: 768px) {
    body.chat-page .container-fluid > .row > .col-12 {
        max-width: 1200px !important;
        margin: 0 auto !important;
    }
}

/* == MAIN CARD == */

body.chat-page .main-card {
    flex: 1 1 auto !important;
    display: flex !important;
    flex-direction: column !important;
    min-height: 0 !important;
    background: var(--chat-card-bg) !important;
    border: var(--chat-card-border) !important;
    border-radius: var(--chat-card-radius) !important;
    overflow: hidden !important;
    box-shadow: var(--chat-card-shadow) !important;
    max-height: 100% !important;
    margin: 0 !important;
    margin-bottom: 0 !important;
    padding: 20px 20px 0 20px !important; /* Override 2rem padding from new_style.css: top 20px, left/right 20px, bottom 0 */
}

/* Card header - fixed height */
body.chat-page .card-header {
    flex: 0 0 auto !important;
    background: var(--chat-header-bg) !important;
    border-bottom: var(--chat-header-border) !important;
    color: var(--chat-header-color) !important;
}

/* == CHAT BODY == */

body.chat-page .chat-body {
    flex: 1 1 auto !important;
    display: flex !important;
    flex-direction: column !important;
    min-height: 0 !important;
    background: transparent !important;
}

/* Messages container - scrollable area */
body.chat-page .chat-container {
    flex: 1 1 auto !important;
    display: flex !important;
    flex-direction: column !important;
    justify-content: flex-start !important; /* Align content to top, not stretched */
    min-height: 0 !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    padding-top: 20px !important;
    padding-bottom: 0 !important;
    padding-left: 0.5rem !important;
    padding-right: 0.5rem !important;
}

/* Messages list */
body.chat-page #chat-messages {
    flex: 0 0 auto !important; /* Don't grow to fill space */
    display: flex !important;
    flex-direction: column !important;
    gap: 0.5rem !important;
}

/* Empty messages container */
body.chat-page #chat-messages:empty {
    min-height: 100px !important;
}

/* Typing indicator - always stays at bottom with margin-top: auto */
body.chat-page #typing-indicator {
    margin-top: auto !important;
    margin-bottom: 1rem !important;
    flex-shrink: 0 !important;
}

/* == INPUT CONTAINERS == */

/* Input container - fixed at bottom */
body.chat-page #text-input-container,
body.chat-page #voice-recording-container {
    flex: 0 0 auto !important;
    background: var(--chat-input-area-bg) !important;
    border-top: var(--chat-input-area-border) !important;
}

/* Text input field */
body.chat-page #user-message {
    background: var(--chat-input-bg) !important;
    border: var(--chat-input-border) !important;
    color: var(--chat-input-color) !important;
}

body.chat-page #user-message::placeholder {
    color: var(--chat-input-placeholder) !important;
}

/* Continue button container - replaces text input for progressive paragraph display */
#continue-button-container {
    display: none;
    justify-content: center;
    align-items: center;
    padding: 0.75rem;
    width: 100%;
    flex: 0 0 auto;
    border-top: 1px solid var(--color-border);
}

/* Show button when it has the inline style display: flex */
#continue-button-container[style*="display: flex"] {
    display: flex !important;
}

/* When button is visible, hide the text input container that comes after it */
#continue-button-container[style*="display: flex"] + #text-input-container {
    display: none !important;
}

/* Continue button base styling */
#continue-paragraph-btn,
#continue-button-container .action-btn {
    padding: 0.75rem 2rem;
    border-radius: 8px;
    font-weight: 600;
    max-width: 300px;
    width: 100%;
    transition: all 0.3s ease;
    cursor: pointer;
}

/* Header action toolbar - uniform button height so every button (including
   Finish) lines up on one row. */
#header-action-buttons .btn,
#header-action-buttons .action-btn {
    height: 38px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
}

/* Icon-only toolbar buttons (reset, voice, language) are perfect squares. */
#header-action-buttons .chat-icon-btn {
    width: 38px;
    padding: 0;
}

#header-action-buttons .chat-icon-btn > i,
#header-action-buttons .chat-icon-btn > span {
    font-size: 1.05rem;
    line-height: 1;
}

/* No Bootstrap dropdown caret on the square icon buttons */
#header-action-buttons .chat-icon-btn::after {
    display: none !important;
}

/* == MESSAGE STYLING == */

/* Message bubbles */
.message {
    display: flex;
    margin-bottom: 1.5rem;
    max-width: 85%;
    min-width: 50%;
}

.message.user-message {
    margin-left: auto !important;
}

/* Message wrapper (groups content and timestamp) */
.message-wrapper {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    flex: 1;
    min-width: 0;
}

/* Message content - avatar in the left column; the audio player and text
   stack in the right column. Two-column grid so the avatar is never overlapped
   and the player sits ABOVE the text, both aligned to the avatar's top. */
.message-content {
    padding: 0.75rem 1.25rem;
    border-radius: var(--radius-lg, 12px);
    word-wrap: break-word;
    line-height: 1.5;
    display: grid;
    grid-template-columns: auto 1fr;
    column-gap: 0.75rem;
    align-items: start;
}

/* Message avatar (fixed left column, top-aligned) */
.message-avatar {
    grid-column: 1;
    grid-row: 1;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    overflow: hidden;
    background: transparent;
    border: 3px solid rgba(255, 255, 255, 0.3);
    box-sizing: border-box;
}

.message-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

/* Audio player + text share the right column and stack vertically; the space
   below the avatar stays empty instead of the text wrapping under it. */
.message-content > .inline-audio-player,
.message-content > .message-text {
    grid-column: 2;
    min-width: 0;
}

/* Player sits at the very top of the column, flush with the avatar's top */
.message-content > .inline-audio-player {
    margin-top: 0;
}

/* User messages put the avatar on the right column instead */
.message.user-message .message-content {
    grid-template-columns: 1fr auto;
}

.message.user-message .message-avatar {
    grid-column: 2;
}

.message.user-message .message-content > .inline-audio-player,
.message.user-message .message-content > .message-text {
    grid-column: 1;
}

.message-text {
    word-wrap: break-word;
}

/* Keep the first/last block flush so the text top-aligns with the avatar and
   no stray leading/trailing gap appears on split messages */
.message-text > :first-child {
    margin-top: 0;
}

.message-text > :last-child {
    margin-bottom: 0;
}

.message.assistant-message .message-content,
.message.coach-message .message-content {
    background: var(--chat-coach-gradient, linear-gradient(135deg, var(--chat-coach-color, var(--blue, #0d6efd)), var(--chat-coach-color-dark, var(--blue-dark, #0a58ca))));
    color: var(--white, #ffffff);
    border: none;
}

.message.user-message .message-content {
    background: var(--chat-user-gradient, linear-gradient(135deg, var(--chat-user-color, var(--brand-1, #6366f1)), var(--chat-user-color-dark, var(--brand-2, #8b5cf6))));
    color: var(--white, #ffffff);
}

/* Message timestamp - hidden */
.message-time,
.message-timestamp {
    display: none !important;
}

/* == MARKDOWN FORMATTING == */

.message-content h1,
.message-content h2,
.message-content h3,
.message-content h4,
.message-content h5,
.message-content h6 {
    margin: 0.5rem 0;
    font-weight: 600;
}

.message-content h1 { font-size: 1.25rem; }
.message-content h2 { font-size: 1.15rem; }
.message-content h3 { font-size: 1.1rem; }
.message-content h4 { font-size: 1.05rem; }
.message-content h5 { font-size: 1rem; }
.message-content h6 { font-size: 0.95rem; }

.message-content p {
    margin: 0.5rem 0;
}

.message-content p:last-child {
    margin-bottom: 0;
}

.message-content ul,
.message-content ol {
    margin: 0.5rem 0;
    padding-left: 1.5rem;
}

.message-content li {
    margin: 0.15rem 0;
}

.message-content blockquote {
    border-left: 3px solid rgba(255, 255, 255, 0.3);
    padding-left: 1rem;
    margin: 0.5rem 0;
    font-style: italic;
}

.message-content code {
    background: rgba(0, 0, 0, 0.2);
    padding: 0.2rem 0.4rem;
    border-radius: 3px;
    font-family: 'Courier New', monospace;
    font-size: 0.9em;
}

.message-content pre {
    background: rgba(0, 0, 0, 0.2);
    padding: 1rem;
    border-radius: 5px;
    overflow-x: auto;
    margin: 0.5rem 0;
}

.message-content pre code {
    background: none;
    padding: 0;
}

.message-content strong,
.message-content b {
    font-weight: 600;
}

.message-content em,
.message-content i {
    font-style: italic;
}

.message-content a {
    color: rgba(255, 255, 255, 0.9);
    text-decoration: underline;
}

.message-content a:hover {
    color: white;
}

/* == TYPING INDICATOR == */

.theme-dark #typing-indicator,
body.chat-page #typing-indicator {
    background: var(--chat-typing-bg);
    color: var(--chat-typing-color);
    border: var(--chat-typing-border);
    border-radius: 18px;
    padding: 10px 15px;
    margin: 10px 0;
    align-self: flex-start;
    max-width: 70%;
    border-bottom-left-radius: 5px;
    box-shadow: var(--shadow-sm);
}

/* == MOBILE RESPONSIVENESS == */

@media (max-width: 768px) {
    /* Full viewport for mobile - no navbar */
    html, body.chat-page {
        height: 100vh !important;
        height: 100dvh !important; /* Dynamic viewport height for mobile browsers */
    }

    /* Hide navigation and footer on mobile */
    body.chat-page nav,
    body.chat-page .navbar,
    body.chat-page footer {
        display: none !important;
    }

    /* Content container takes full viewport on mobile */
    body.chat-page > .content-container {
        flex: 1 1 100% !important;
        height: 100% !important;
        max-height: 100vh !important;
        max-height: 100dvh !important;
    }

    /* Full height chat on mobile */
    body.chat-page .container-fluid {
        padding: 0 !important;
        max-width: 100% !important;
        height: 100% !important;
    }

    body.chat-page .container {
        padding: 0;
        max-width: 100%;
    }

    body.chat-page .row {
        margin: 0;
        height: 100% !important;
    }

    body.chat-page .col-12 {
        padding: 0 !important;
        max-width: 100% !important;
        height: 100% !important;
    }

    body.chat-page .main-card {
        border-radius: 0 !important;
        border: none !important;
        margin: 0 !important;
        padding: 0 !important;
        height: 100% !important;
        max-height: 100vh !important;
        max-height: 100dvh !important;
    }

    /* Ensure card header is visible and interactive on mobile */
    body.chat-page .card-header {
        padding: 0.75rem 1rem !important;
        pointer-events: auto !important;
    }

    /* Messages take less space on mobile */
    .message {
        max-width: 95%;
    }

    /* Adjust chat container padding */
    body.chat-page .chat-container {
        padding: 1rem !important;
    }

    /* Hide title on mobile */
    .card-header h4,
    .card-header p {
        display: none;
    }

    /* Show back button on mobile */
    .card-header .mobile-back-btn {
        display: inline-flex !important;
        visibility: visible !important;
        pointer-events: auto !important;
        z-index: 1000 !important;
    }

    /* Hide admin dropdown menu on mobile */
    #adminDropdownMenu {
        display: none !important;
    }

    /* Mobile Enter key behavior - disable send on Enter */
    #user-message {
        font-size: 16px !important; /* Prevent zoom on iOS */
    }
}

/* == BACK BUTTON == */

.mobile-back-btn {
    display: none !important; /* Hidden by default */
    background: none;
    border: none;
    color: white;
    font-size: 1.2rem;
    padding: 0.5rem;
    margin-right: 0.5rem;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    text-decoration: none;
}

.mobile-back-btn:hover {
    background: rgba(255,255,255,0.1) !important;
    color: white !important;
    text-decoration: none;
}

.mobile-back-btn:focus {
    outline: none;
    background: rgba(255,255,255,0.2) !important;
}

.mobile-back-btn:active {
    background: rgba(255,255,255,0.3) !important;
    color: white !important;
}

/* Center all buttons vertically in header */
.card-header .d-flex {
    align-items: center !important;
}

.card-header .action-btn {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
}

/* == CHAT SETTINGS == */

.action-btn.disabled-setting {
    opacity: 0.5;
}

/* == SEND BUTTON == */

#send-message {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
}

#send-message i {
    margin: 0 !important;
    line-height: 1 !important;
}

/* == HEADER DROPDOWNS (dark context — header is bg-gradient-primary) == */

.card-header .dropdown-menu {
    background: var(--surface-dark, #141414);
    border: 1px solid rgba(255, 255, 255, 0.15);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    min-width: 200px;
}

.card-header .dropdown-item {
    color: rgba(255, 255, 255, 0.9);
    padding: 0.5rem 1rem;
    transition: background-color 0.2s;
}

.card-header .dropdown-item:hover,
.card-header .dropdown-item:focus {
    background-color: rgba(255, 255, 255, 0.1);
    color: #fff;
}

.card-header .dropdown-item i {
    width: 20px;
    text-align: center;
}

.card-header .dropdown-divider {
    border-color: rgba(255, 255, 255, 0.15);
    margin: 0.25rem 0;
}

/* == MOVE ON BUTTON == */

#move-on-button {
    display: none; /* Hidden by default, shown after first message */
    white-space: nowrap !important; /* Prevent text wrapping */
}

#move-on-button.visible {
    display: inline-flex;
}

#move-on-button.blink-slow {
    animation: blinkSlow 3s ease-in-out infinite;
}

#move-on-button.blink-fast {
    animation: blinkFast 1s ease-in-out infinite;
}

#move-on-button.critical {
    background: linear-gradient(135deg, #ef4444, #dc2626) !important;
    animation: blinkCritical 0.5s ease-in-out infinite;
}

@keyframes blinkSlow {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.6; }
}

@keyframes blinkFast {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

@keyframes blinkCritical {
    0%, 100% {
        opacity: 1;
        box-shadow: 0 0 20px rgba(239, 68, 68, 0.6);
    }
    50% {
        opacity: 0.7;
        box-shadow: 0 0 30px rgba(239, 68, 68, 0.9);
    }
}

/* == ADMIN RIBBON MODAL == */

#adminRibbonModal .modal-dialog {
    position: fixed;
    left: 10px;
    top: 80px;
    margin: 0;
    max-width: 520px;
}

#adminRibbonModal .modal-dialog::before {
    content: '';
    position: absolute;
    top: -1px;
    left: -1px;
    right: -1px;
    bottom: -1px;
    border: 1px solid rgba(0, 0, 0, 0.5);
    border-radius: inherit;
    pointer-events: none;
    z-index: 1;
}

#adminRibbonModal .modal-content {
    opacity: 0;
    transition: opacity 0.3s ease;
    background-color: var(--color-surface);
    color: var(--color-text);
    position: relative;
}

#adminRibbonModal .modal-content:hover {
    opacity: 1;
}

#adminRibbonModal .modal-header {
    background-color: var(--color-surface);
    color: var(--color-text);
    border-bottom-color: var(--color-border);
}

#adminRibbonModal .modal-body {
    background-color: var(--color-surface);
    color: var(--color-text);
}

#adminRibbonModal .btn-close {
    filter: var(--bs-btn-close-filter, none);
    position: relative;
    z-index: 2;
}

/* == CHECKPOINT PROGRESS BAR == */

#checkpoint-progress-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 9999;
    pointer-events: none;
}

.checkpoint-progress-bar {
    position: absolute;
    background: var(--chat-progress-color);
    transition: all 0.5s ease-in-out;
}

/* Left side - top to bottom (last 25% of progress) */
#checkpoint-progress-left {
    left: 0;
    top: 0;
    width: 4px;
    height: 0%;
}

/* Top side - right to left (50-75% of progress) */
#checkpoint-progress-top {
    right: 0;
    top: 0;
    width: 0%;
    height: 4px;
}

/* Right side - bottom to top (25-50% of progress) */
#checkpoint-progress-right {
    right: 0;
    bottom: 0;
    width: 4px;
    height: 0%;
}

/* Bottom side - left to right (0-25% of progress, starts from bottom-left corner) */
#checkpoint-progress-bottom {
    left: 0;
    bottom: 0;
    width: 0%;
    height: 4px;
}

#checkpoint-progress-bubble {
    position: fixed;
    background: var(--chat-bubble-bg);
    color: var(--chat-bubble-color);
    border: var(--chat-bubble-border);
    border-radius: 12px;
    padding: 4px 10px;
    font-size: 14px;
    font-weight: 600;
    white-space: nowrap;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
    pointer-events: none;
    transition: all 0.5s ease-in-out;
}

/* Pause state for consulting mode */
#checkpoint-progress-bubble.paused {
    pointer-events: auto;
    cursor: pointer;
    border-color: #f59e0b;
}

#checkpoint-progress-bubble.paused:hover {
    transform: scale(1.1);
    box-shadow: 0 4px 12px rgba(245, 158, 11, 0.3);
}

/* Pulsating pause icon */
#checkpoint-progress-bubble .pause-icon {
    display: inline-block;
    font-size: 16px;
    animation: pausePulsate 1.5s ease-in-out infinite;
}

@keyframes pausePulsate {
    0%, 100% {
        opacity: 0.7;
        transform: scale(1);
    }
    50% {
        opacity: 1;
        transform: scale(1.2);
    }
}

/* == FINISH BUTTON PULSATION == */

#finish-session-button.pulsate {
    animation: finishPulsate 0.8s ease-in-out infinite;
}

@keyframes finishPulsate {
    0%, 100% {
        transform: scale(1);
        box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.9);
        filter: brightness(1);
    }
    50% {
        transform: scale(1.15);
        box-shadow: 0 0 0 15px rgba(16, 185, 129, 0);
        filter: brightness(1.2);
    }
}
