/* ==========================================================================
   AI Assistant Modal
   Description: Styles for the chat modal UI (layout, messages, input, loader).
   Notes for buyers:
   - Keep class names intact to preserve JS behavior.
   - Customize colors/spacing freely; animations are timing-safe.
   - Avoid renaming structural classes: .modal-ai, .chat-messages, .chat-input, .chat-message.
   ========================================================================== */

#modal-ai-assistant.modal.modal-ai {
    position: fixed;
    top: 50%;
    left: 50%;
    right: auto;
    bottom: auto;
    width: min(420px, calc(100vw - 48px));
    height: min(560px, 74vh);
    max-height: 74vh;
    transform: translate(-50%, -50%) scale(0.96);
    background: #f6f7f9;
    border: 1px solid rgba(255, 255, 255, 0.88);
    border-radius: 20px;
    z-index: 1000;
    transition: transform var(--modal-open-duration, 380ms) var(--modal-ease-enter, cubic-bezier(0.32, 0.72, 0, 1)),
                opacity var(--modal-open-duration, 380ms) var(--modal-ease-enter, cubic-bezier(0.32, 0.72, 0, 1));
    opacity: 0;
    box-shadow:
        0 1px 0 rgba(255, 255, 255, 0.9) inset,
        0 10px 32px rgba(28, 36, 48, 0.08),
        0 2px 8px rgba(28, 36, 48, 0.04);
    display: none;
    flex-direction: column;
    overflow: hidden;
    contain: none;
    will-change: transform;
    backface-visibility: hidden;
}

#modal-ai-assistant.modal.modal-ai.show {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
    display: flex;
}

#modal-ai-assistant.modal.modal-ai.hide,
#modal-ai-assistant.modal.modal-ai.closing {
    transform: translate(-50%, -50%) scale(0.94);
    opacity: 0;
}

#modal-ai-assistant.modal-ai .close-button {
    top: 8px;
    left: 16px;
}

#modal-ai-assistant.modal-ai .close-esc-line {
    width: 16px;
}

.chat-container {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    background: transparent;
    height: 100%;
}

.chat-header {
    padding: 16px 20px 12px;
    background: transparent;
    text-align: center;
    border-bottom: none;
    flex-shrink: 0;
    position: relative;
}

.chat-header::after {
    content: '';
    position: absolute;
    left: 20px;
    right: 20px;
    bottom: 0;
    height: 1px;
    background: rgba(60, 68, 82, 0.08);
}

.chat-header h2 {
    margin: 0;
    font-family: 'Raleway', sans-serif;
    font-size: 16px;
    font-weight: 400;
    letter-spacing: 0.06em;
    color: #3c4452;
    line-height: 1.15;
}

.chat-messages {
    flex: 1;
    min-height: 0;
    padding: 18px 18px 8px;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: var(--scrollbar-color, #d5d8de) transparent;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    gap: 4px;
}

.chat-messages::-webkit-scrollbar {
    width: 4px;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: var(--scrollbar-color, #d5d8de);
    border-radius: 99px;
}

.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.chat-message {
    margin-bottom: 8px;
    display: flex;
    align-items: flex-end;
    animation: chat-message-in 320ms cubic-bezier(0.22, 1, 0.36, 1) both;
}

.chat-message.user {
    justify-content: flex-end;
}

.chat-message.ai {
    justify-content: flex-start;
    gap: 8px;
}

.chat-message.ai.template {
    display: none;
}

.chat-message .message-content {
    max-width: 78%;
    padding: 11px 15px;
    font-family: 'DM Sans', 'Titillium Web', sans-serif;
    font-size: 14px;
    font-weight: 400;
    line-height: 1.5;
    position: relative;
}

.chat-message.user .message-content {
    background: #3c4452;
    color: #f4f6f8;
    border-radius: 14px 14px 4px 14px;
    box-shadow:
        0 1px 2px rgba(28, 36, 48, 0.08),
        0 6px 14px rgba(28, 36, 48, 0.12);
}

.chat-message.user .message-content::before {
    content: '';
    position: absolute;
    right: -7px;
    bottom: 0;
    width: 12px;
    height: 14px;
    background: #3c4452;
    clip-path: polygon(0 0, 100% 100%, 0 100%);
    filter: drop-shadow(1px 2px 2px rgba(28, 36, 48, 0.1));
}

.chat-message.user .message-content::after {
    content: none;
}

.chat-message.ai .message-content {
    background: #ffffff;
    color: var(--text-color, #2a2d33);
    border: 1px solid rgba(255, 255, 255, 0.9);
    border-radius: 14px 14px 14px 4px;
    box-shadow:
        0 1px 2px rgba(32, 40, 52, 0.05),
        0 8px 18px rgba(32, 40, 52, 0.08);
}

.chat-message.ai .message-content::before {
    content: '';
    position: absolute;
    left: -7px;
    bottom: 0;
    width: 12px;
    height: 14px;
    background: #ffffff;
    clip-path: polygon(100% 0, 100% 100%, 0 100%);
    filter: drop-shadow(-1px 2px 2px rgba(32, 40, 52, 0.06));
}

.chat-message.ai .message-content::after {
    content: none;
}

.chat-message.ai .message-icon-wrap {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    flex-shrink: 0;
    display: none;
    align-items: center;
    justify-content: center;
    background: #ffffff;
    border: 1px solid rgba(255, 255, 255, 0.95);
    box-shadow:
        0 1px 2px rgba(32, 40, 52, 0.05),
        0 6px 14px rgba(32, 40, 52, 0.08);
    color: #6b63c9;
    font-size: 13px;
}

.chat-message.ai:not(.template) .message-icon-wrap {
    display: flex;
}

.chat-message.ai .loading-dots {
    display: none;
    gap: 5px;
    align-items: center;
    padding: 12px 16px;
    min-height: 36px;
    position: relative;
    border-radius: 12px 12px 12px 3px;
    background: #ffffff;
    box-shadow:
        0 1px 2px rgba(32, 40, 52, 0.05),
        0 8px 18px rgba(32, 40, 52, 0.08);
}

.chat-message.ai .loading-dots::before {
    content: '';
    position: absolute;
    left: -7px;
    bottom: 0;
    width: 12px;
    height: 14px;
    background: #ffffff;
    clip-path: polygon(100% 0, 100% 100%, 0 100%);
}

.chat-message.ai.loading .loading-dots {
    display: flex;
}

.chat-message.ai .loading-dots span {
    width: 6px;
    height: 6px;
    background: #8a919c;
    border-radius: 50%;
    animation: bounce 0.6s infinite alternate;
}

.chat-message.ai .loading-dots span:nth-child(2) {
    animation-delay: 0.2s;
}

.chat-message.ai .loading-dots span:nth-child(3) {
    animation-delay: 0.4s;
}

.chat-input {
    padding: 12px 14px 16px;
    background: transparent;
    border-top: none;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    position: relative;
    z-index: 10;
}

.chat-input-shell {
    --tools-inset: 14px;
    --send-size: 36px;
    --mic-size: 32px;
    --tools-gap: 6px;
    --cta-ring: 1px;
    --cta-pad: 4px;
    --cta-rim: 0.5px;
    position: relative;
    width: 100%;
    display: block;
    overflow: visible;
    box-sizing: border-box;
    padding: var(--cta-pad);
    border-radius: 22px;
    background: #f7f8fa;
    box-shadow:
        inset 1px 1px 3px rgba(155, 164, 176, 0.12),
        inset -1px -1px 3px rgba(155, 164, 176, 0.12);
}

.chat-input-shell::before {
    content: '';
    position: absolute;
    inset: var(--cta-ring);
    padding: var(--cta-rim);
    border-radius: inherit;
    box-sizing: border-box;
    pointer-events: none;
    z-index: 2;
    background: var(--bg-color, #eef0f3);
    box-shadow:
        0 0 0.5px rgba(160, 168, 178, 0.22),
        0 0 0.5px rgba(255, 255, 255, 0.95),
        inset 0 0 0.5px rgba(255, 255, 255, 0.9),
        inset 0 0 0.5px rgba(150, 158, 170, 0.2);
    -webkit-mask:
        linear-gradient(#000 0 0) content-box,
        linear-gradient(#000 0 0);
    -webkit-mask-composite: xor;
    mask:
        linear-gradient(#000 0 0) content-box,
        linear-gradient(#000 0 0);
    mask-composite: exclude;
}

@property --pkg-angle {
    syntax: '<angle>';
    inherits: false;
    initial-value: 0deg;
}

@keyframes pkgSpinBorder {
    to { --pkg-angle: 360deg; }
}

.chat-input-shell::after {
    content: '';
    position: absolute;
    inset: var(--cta-ring);
    padding: 0.5px;
    border-radius: inherit;
    box-sizing: border-box;
    pointer-events: none;
    z-index: 3;
    opacity: 0;
    background: conic-gradient(
        from var(--pkg-angle, 0deg),
        transparent 0%,
        transparent 68%,
        #3d8ef0 82%,
        transparent 90%
    );
    -webkit-mask:
        linear-gradient(#000 0 0) content-box,
        linear-gradient(#000 0 0);
    -webkit-mask-composite: xor;
    mask:
        linear-gradient(#000 0 0) content-box,
        linear-gradient(#000 0 0);
    mask-composite: exclude;
    transition: opacity 0.4s ease;
}

.chat-input-shell:hover::after,
.chat-input-shell:focus-within::after {
    opacity: 0.95;
    animation: pkgSpinBorder 2.8s linear infinite;
}

.chat-input textarea {
    flex: 1;
    display: block;
    width: 100%;
    min-width: 0;
    position: relative;
    z-index: 1;
    padding: var(--tools-inset) calc(var(--tools-inset) + var(--send-size) + var(--tools-gap) + var(--mic-size) + 8px) var(--tools-inset) 18px;
    border: none;
    border-radius: 18px;
    background: #f8f9fb;
    font-size: 15px;
    min-height: calc(var(--send-size) + var(--tools-inset) * 2);
    height: auto;
    max-height: 140px;
    outline: none;
    box-shadow:
        inset 2px 2px 5px rgba(148, 158, 172, 0.16),
        inset -2px -2px 5px rgba(148, 158, 172, 0.16);
    transition: border-color 0.25s ease, background 0.25s ease;
    resize: none;
    box-sizing: border-box;
    font-family: 'DM Sans', 'Titillium Web', sans-serif;
    color: var(--text-color, #2a2d33);
    caret-color: #6b63c9;
    line-height: 1.45;
    overflow-y: auto;
    scrollbar-width: none;
}

.chat-input textarea::placeholder {
    color: #b0b8c2;
    font-weight: 300;
}

.chat-input textarea::-webkit-scrollbar {
    width: 0;
}

.chat-input textarea:focus {
    background: #f8f9fb;
    box-shadow:
        inset 2px 2px 5px rgba(148, 158, 172, 0.16),
        inset -2px -2px 5px rgba(148, 158, 172, 0.16);
}

.chat-input-tools {
    position: absolute;
    right: calc(var(--cta-pad) + var(--tools-inset));
    top: 50%;
    bottom: auto;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    gap: var(--tools-gap);
    z-index: 4;
}

.chat-input #chat-mic {
    width: var(--mic-size, 32px);
    height: var(--mic-size, 32px);
    margin: 0;
    padding: 0;
    flex-shrink: 0;
    border: none;
    border-radius: 50%;
    background: transparent;
    box-shadow: none;
    color: #8a919c;
    font-size: 15px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.2s ease, background 0.2s ease, transform 0.2s ease;
}

.chat-input #chat-mic i {
    color: inherit;
    text-shadow: none;
}

.chat-input #chat-mic:hover {
    color: #6b63c9;
    background: rgba(255, 255, 255, 0.45);
    transform: none;
    filter: none;
    box-shadow: none;
}

.chat-input #chat-mic:active {
    transform: scale(0.94);
    background: transparent;
    box-shadow: none;
}

.chat-input #chat-mic.is-listening {
    color: #6b63c9;
    background: rgba(107, 99, 201, 0.12);
    box-shadow: 0 0 0 3px rgba(107, 99, 201, 0.12);
    animation: chat-mic-pulse 1.4s ease-in-out infinite;
}

.chat-input #chat-mic.is-unsupported {
    opacity: 0.38;
    cursor: not-allowed;
}

.chat-send-well {
    --cta-ring: 1px;
    --cta-pad: 4px;
    --cta-rim: 1.25px;
    position: relative;
    right: auto;
    bottom: auto;
    width: var(--send-size, 36px);
    height: var(--send-size, 36px);
    flex-shrink: 0;
    border-radius: 50%;
    padding: var(--cta-pad);
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: visible;
    background: var(--bg-color, #eef0f3);
    box-shadow:
        inset 3px 4px 8px rgba(155, 164, 176, 0.26),
        inset -2px -3px 6px rgba(255, 255, 255, 0.92);
}

.chat-send-ring {
    position: absolute;
    inset: var(--cta-ring);
    padding: var(--cta-rim);
    border-radius: 50%;
    box-sizing: border-box;
    pointer-events: none;
    z-index: 2;
    background: var(--bg-color, #eef0f3);
    box-shadow:
        1px 1px 2px rgba(160, 168, 178, 0.2),
        -1px -1px 2px rgba(255, 255, 255, 0.95),
        inset 0.5px 0.5px 0.5px rgba(255, 255, 255, 0.9),
        inset -0.5px -0.5px 0.5px rgba(150, 158, 170, 0.22);
    -webkit-mask:
        linear-gradient(#000 0 0) content-box,
        linear-gradient(#000 0 0);
    -webkit-mask-composite: xor;
    mask:
        linear-gradient(#000 0 0) content-box,
        linear-gradient(#000 0 0);
    mask-composite: exclude;
}

.chat-send-well button {
    position: relative;
    z-index: 1;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0;
    line-height: 0;
    color: var(--ico-color, #4a5260);
    background: linear-gradient(145deg, var(--gradient-light, #f7f8fa), var(--gradient-dark, #e4e6eb));
    box-shadow:
        4px 4px 8px var(--shadow-dark, #d5d8de),
        -4px -4px 8px var(--shadow-light, #ffffff);
    transition: box-shadow 120ms ease, background 120ms ease, transform 120ms ease;
}

.chat-send-well button i {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0;
    padding: 0;
    line-height: 1;
    font-style: normal;
    font-size: 12px;
    color: var(--ico-color, #4a5260);
    transform: translate(0.5px, -1px);
    text-shadow: none;
}

.chat-send-well button i::before {
    display: block;
    line-height: 1;
}

@media (hover: hover) and (pointer: fine) {
    .chat-send-well button:hover {
        transform: none;
        filter: none;
        color: var(--ico-color, #4a5260);
        background: linear-gradient(145deg, var(--gradient-light, #f7f8fa), var(--gradient-dark, #e4e6eb));
        box-shadow: none;
    }
}

.chat-send-well button:active {
    transform: none;
    background: linear-gradient(145deg, var(--gradient-light, #f7f8fa), var(--gradient-dark, #e4e6eb));
    box-shadow: none;
}

.chat-send-well button:disabled {
    cursor: not-allowed;
    opacity: 0.45;
    transform: none;
    filter: none;
    color: var(--ico-color, #4a5260);
    background: linear-gradient(145deg, var(--gradient-light, #f7f8fa), var(--gradient-dark, #e4e6eb));
    box-shadow:
        3px 3px 6px var(--shadow-dark, #d5d8de),
        -3px -3px 6px var(--shadow-light, #ffffff);
}

.chat-send-well button:disabled i {
    color: var(--ico-color, #4a5260);
    text-shadow: none;
}

@keyframes bounce {
    from { transform: translateY(0); }
    to { transform: translateY(-6px); }
}

@keyframes chat-mic-pulse {
    0%, 100% { box-shadow: 0 0 0 3px rgba(107, 99, 201, 0.1); }
    50% { box-shadow: 0 0 0 6px rgba(107, 99, 201, 0.06); }
}

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

@media (prefers-reduced-motion: reduce) {
    .chat-message { animation: none; }
    .chat-input #chat-mic.is-listening { animation: none; }
    .chat-input-shell::after {
        animation: none !important;
    }
}

@media (max-width: 768px) and (-webkit-min-device-pixel-ratio: 1.5),
       (max-width: 768px) and (min-resolution: 144dpi) {
    .chat-header {
        padding: 16px 16px 12px;
    }
    .chat-messages {
        padding: 14px 14px 8px;
    }
    .chat-input {
        padding: 10px 12px 14px;
    }
    .chat-input-shell {
        --tools-inset: 12px;
        --send-size: 34px;
        --mic-size: 30px;
        --tools-gap: 4px;
    }
    .chat-input textarea {
        font-size: 15px;
        max-height: 120px;
        padding-left: 16px;
        border-radius: 16px;
    }
    .chat-input-shell {
        border-radius: 20px;
    }
    .chat-input-tools {
        right: calc(var(--cta-pad) + var(--tools-inset));
        top: 50%;
        bottom: auto;
        transform: translateY(-50%);
    }
    .chat-input #chat-mic {
        width: var(--mic-size);
        height: var(--mic-size);
        font-size: 14px;
    }
    .chat-send-well {
        width: var(--send-size);
        height: var(--send-size);
        --cta-pad: 4px;
        --cta-ring: 1px;
        --cta-rim: 1.25px;
    }
    .chat-send-well button i {
        font-size: 11px;
        transform: translate(0.5px, -1px);
    }
    .chat-message.ai .message-icon-wrap {
        width: 28px;
        height: 28px;
        font-size: 12px;
    }
}

@media (max-width: 768px), (orientation: portrait) {
    #modal-ai-assistant.modal.modal-ai {
        top: calc(62px + env(safe-area-inset-top, 0px));
        width: min(92vw, 420px);
        height: calc(100dvh - 62px - 102px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
        max-height: none;
        border-radius: 20px;
        transform: translate(-50%, 0) scale(0.96);
        background: #f6f7f9;
        border: 1px solid rgba(255, 255, 255, 0.9);
        box-shadow:
            0 1px 0 rgba(255, 255, 255, 0.92) inset,
            0 8px 28px rgba(28, 36, 48, 0.08),
            0 1px 6px rgba(28, 36, 48, 0.04);
    }
    #modal-ai-assistant.modal.modal-ai.show {
        transform: translate(-50%, 0) scale(1);
    }
    #modal-ai-assistant.modal.modal-ai.hide,
    #modal-ai-assistant.modal.modal-ai.closing {
        transform: translate(-50%, 0) scale(0.97);
    }
}

/* -------- Enhanced content styles (markdown-like) -------- */
.chat-message .message-content p {
    margin: 0 0 8px 0;
}
.chat-message .message-content p:last-child {
    margin-bottom: 0;
}
.chat-message .message-content a {
    color: #6b63c9;
    text-decoration: none;
    border-bottom: 1px dotted rgba(107, 99, 201, 0.4);
}
.chat-message .message-content a:hover {
    text-decoration: underline;
}
.chat-message .message-content strong {
    font-weight: 600;
}
