/**
 * PayU Ask AI — Chat Modal UI
 * Scoped chat modal styles (header button lives in styles.css)
 */

/* ── Design tokens ── */
:root {
    --askai-purple: #7c3aed;
    --askai-purple-dark: #6d28d9;
    --askai-purple-light: #e9d5ff;
    --askai-purple-muted: #a78bfa;
    --askai-gradient-start: #9e25c9;
    --askai-gradient-mid: #d22777;
    --askai-gradient-end: #d28e27;
    --askai-teal: #27a5be;
    --askai-teal-light: #e9f6f9;
    --askai-text: #1f2937;
    --askai-text-muted: #6b7280;
    --askai-text-light: #9ca3af;
    --askai-border: #e5e7eb;
    --askai-border-input: #b4b4b4;
    --askai-bg: #ffffff;
    --askai-bg-subtle: #f9fafb;
    --askai-bg-user: #f3f4f6;
    --askai-overlay: rgba(0, 0, 0, 0.55);
    --askai-radius: 16px;
    --askai-radius-sm: 10px;
    --askai-radius-xs: 8px;
    --askai-shadow: 0 20px 60px rgba(0, 0, 0, 0.28), 0 4px 16px rgba(0, 0, 0, 0.08);
    --askai-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --askai-z-overlay: 10001;
    --askai-z-modal: 10002;
    --askai-transition: 0.22s ease;
}

/* ── Overlay & modal visibility ── */
.chat-overlay,
.chat-container {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition:
        opacity var(--askai-transition),
        visibility var(--askai-transition);
}

.chat-overlay.is-visible,
.chat-container.is-visible {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

/* ── Overlay ── */
.chat-overlay {
    position: fixed;
    inset: 0;
    z-index: var(--askai-z-overlay);
    background: var(--askai-overlay);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
}

/* ── Modal container ── */
.chat-container {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: var(--askai-z-modal);
    display: none;
    flex-direction: column;
    width: min(600px, calc(100vw - 2rem));
    max-height: 85vh;
    background: var(--askai-bg);
    border-radius: var(--askai-radius);
    box-shadow: var(--askai-shadow);
    overflow: hidden;
    font-family: var(--askai-font);
    font-size: 15px;
    line-height: 1.5;
    color: var(--askai-text);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.chat-container.is-visible {
    display: flex;
    animation: askaiModalIn 0.28s ease;
}

@keyframes askaiModalIn {
    from {
        opacity: 0;
        transform: translate(-50%, calc(-50% + 14px)) scale(0.98);
    }
    to {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1);
    }
}

/* ── Header ── */
.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    flex-shrink: 0;
    padding: 0.9rem 1.1rem;
    border-bottom: 1px solid var(--askai-border);
    background: var(--askai-bg);
}

.chat-container .header-title {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    min-width: 0;
}

.chat-container .header-logo {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
}

.chat-container .header-logo .logo-text {
    font-size: 1.15rem;
    font-weight: 700;
    line-height: 1.1;
    color: #9E25C9;
    letter-spacing: -0.01em;
}

.beta-pill-chat {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 2px 8px;
    border-radius: 999px;
    border: 1px solid #EAD8F3;
    background: linear-gradient(271.41deg, #FEF6FA 1%, #FBF0FE 98.65%);
    color: #9E25C9;
    font-size: 12px;
    font-weight: 500;
    line-height: 1.2;
    letter-spacing: 0.01em;
    white-space: nowrap;
}

.chat-header-actions {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    flex-shrink: 0;
}

.chat-container .restart-btn,
.chat-container .close-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.2rem;
    height: 2.2rem;
    padding: 0;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: var(--askai-text-muted);
    cursor: pointer;
    transition:
        background var(--askai-transition),
        color var(--askai-transition),
        transform 0.12s ease;
}

.chat-container .restart-btn {
    display: none;
}

.chat-container .restart-btn.is-visible {
    display: inline-flex;
}

.chat-container .restart-btn:hover,
.chat-container .close-btn:hover {
    background: rgba(15, 160, 128, 0.08);
    color: #0fa080;
}

.chat-container .restart-btn:active,
.chat-container .close-btn:active {
    transform: scale(0.94);
}

.chat-container .restart-btn svg,
.chat-container .close-btn svg {
    display: block;
    width: 1.35rem;
    height: 1.35rem;
}

.chat-container .restart-btn:hover svg path,
.chat-container .close-btn:hover svg path {
    stroke: #0fa080;
}

/* ── Messages area ── */
.chat-messages {
    flex: 1 1 auto;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    padding: 1.25rem 1.1rem;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
}

.chat-messages.initial-view {
    display: flex;
    flex-direction: column;
    justify-content: center;
}

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

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

.chat-messages::-webkit-scrollbar-thumb {
    background: #d1d5db;
    border-radius: 999px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: #9ca3af;
}

/* ── Initial / intro view ── */
.initial-content-wrapper {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 1.75rem;
    width: 100%;
    max-width: 100%;
}

.ai-intro {
    display: block;
    width: 100%;
}

.thinking-indicator {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    gap: 1rem;
}

.ai-icon-final {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 72px;
    height: 72px;
    flex-shrink: 0;
}

.ai-icon-final svg {
    width: 72px;
    height: 72px;
    display: block;
}

.ai-icon-final .icon-final-frame,
.ai-icon-final .icon-final-main-star,
.ai-icon-final .icon-final-sparkle {
    fill: url(#final-gradient);
}

/* Fallback if gradient id missing */
@supports not (fill: url(#final-gradient)) {
    .ai-icon-final .icon-final-frame,
    .ai-icon-final .icon-final-main-star,
    .ai-icon-final .icon-final-sparkle {
        fill: var(--askai-purple);
    }
}

.intro-text {
    margin: 0;
    max-width: 26rem;
    font-size: 1.05rem;
    line-height: 1.55;
    color: #374151;
    font-weight: 400;
}

/* ── Example questions ── */
.example-questions {
    width: 100%;
}

.example-title {
    margin: 0 0 0.75rem;
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--askai-text-light);
}

.questions-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
}

.questions-list li {
    display: none;
}

.example-question {
    display: block;
    width: 100%;
    padding: 0.85rem 1rem;
    border: 1.5px solid #e0f2f1;
    border-radius: var(--askai-radius-sm);
    background: var(--askai-bg);
    color: var(--askai-text);
    font-size: 0.92rem;
    line-height: 1.45;
    cursor: pointer;
    transition:
        border-color var(--askai-transition),
        background var(--askai-transition),
        box-shadow var(--askai-transition),
        transform 0.12s ease;
}

.example-question:hover {
    border-color: #0fa080;
    background: #f0fffe;
    box-shadow: 0 4px 12px rgba(15, 160, 128, 0.15);
}

.example-question:active {
    transform: scale(0.98);
}

.example-question .question-content {
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
}

.example-question .question-content span {
    flex: 1;
}

/* ── User messages ── */
.user-message {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 1rem;
    animation: askaiFadeUp 0.25s ease;
}

.user-message-text {
    display: inline-block;
    max-width: 85%;
    padding: 0.65rem 0.9rem;
    border-radius: 14px 14px 4px 14px;
    background: var(--askai-bg-user);
    color: var(--askai-text);
    font-size: 0.95rem;
    line-height: 1.45;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

/* ── Bot reply ── */
.reply-message {
    display: flex;
    align-items: flex-start;
    gap: 0.65rem;
    margin-bottom: 1rem;
    animation: askaiFadeUp 0.3s ease;
}

.reply-message .reply-svg,
.reply-message > svg {
    flex-shrink: 0;
    width: 22px;
    height: 26px;
    margin-top: 0.15rem;
}

.reply-text {
    flex: 1;
    min-width: 0;
    color: var(--askai-text);
    font-size: 0.95rem;
    line-height: 1.6;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

/* Markdown inside replies */
.reply-text > *:first-child {
    margin-top: 0;
}

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

.reply-text p {
    margin: 0 0 0.75rem;
}

.reply-text h1,
.reply-text h2,
.reply-text h3,
.reply-text h4,
.reply-text h5,
.reply-text h6 {
    margin: 1rem 0 0.5rem;
    font-weight: 700;
    line-height: 1.3;
    color: var(--askai-text);
}

.reply-text h1 { font-size: 1.35rem; }
.reply-text h2 { font-size: 1.2rem; }
.reply-text h3 { font-size: 1.08rem; }
.reply-text h4 { font-size: 1rem; }
.reply-text h5 { font-size: 0.95rem; }
.reply-text h6 { font-size: 0.9rem; color: var(--askai-text-muted); }

.reply-text ul,
.reply-text ol {
    margin: 0 0 0.75rem;
    padding-left: 1.35rem;
}

.reply-text li {
    margin-bottom: 0.35rem;
}

.reply-text li:last-child {
    margin-bottom: 0;
}

.reply-text a {
    color: var(--askai-teal);
    text-decoration: underline;
    text-underline-offset: 2px;
    transition: color var(--askai-transition);
}

.reply-text a:hover {
    color: #1d8fa5;
}

.reply-text code {
    padding: 0.12rem 0.35rem;
    border-radius: 4px;
    background: #f3f4f6;
    color: #be185d;
    font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
    font-size: 0.88em;
}

.reply-text pre {
    margin: 0.75rem 0;
    padding: 0;
    overflow-x: auto;
    border-radius: var(--askai-radius-xs);
}

.reply-text pre code {
    display: block;
    padding: 0.85rem 1rem;
    background: #1e1e1e;
    color: #e5e7eb;
    font-size: 0.85rem;
    line-height: 1.5;
    border-radius: var(--askai-radius-xs);
}

.reply-text blockquote {
    margin: 0.75rem 0;
    padding: 0.5rem 0 0.5rem 0.85rem;
    border-left: 3px solid var(--askai-purple-muted);
    color: var(--askai-text-muted);
}

.reply-text table {
    width: 100%;
    margin: 0.75rem 0;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.reply-text th,
.reply-text td {
    padding: 0.5rem 0.65rem;
    border: 1px solid var(--askai-border);
    text-align: left;
}

.reply-text th {
    background: var(--askai-bg-subtle);
    font-weight: 600;
}

.reply-text hr {
    margin: 1rem 0;
    border: none;
    border-top: 1px solid var(--askai-border);
}

.reply-text strong {
    font-weight: 700;
}

/* ── Thinking indicator (in-thread) ── */
.thinking-indicators {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 1rem;
    color: var(--askai-text-muted);
    font-size: 0.92rem;
    animation: askaiFadeUp 0.25s ease;
}

.thinking-indicators .think-icon {
    flex-shrink: 0;
    width: 22px;
    height: 26px;
}

.thinking-indicators .thinking-text {
    font-style: italic;
    color: var(--askai-text-muted);
}

.thinking-indicators .dots {
    flex-shrink: 0;
    width: auto;
    height: 1em;
    animation: askaiDotsPulse 1.2s ease-in-out infinite;
}

@keyframes askaiDotsPulse {
    0%, 100% { opacity: 0.35; }
    50% { opacity: 1; }
}

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

/* ── Bot messages & code blocks ── */
.bot-message {
    margin: 0.75rem 0 1rem;
    border-radius: var(--askai-radius-xs);
    overflow: hidden;
    border: 1px solid #2d2d2d;
    background: #1e1e1e;
}

.bot-message.terminal-style {
    margin-left: 1.75rem;
}

.bot-message pre {
    margin: 0;
    padding: 0;
    overflow-x: auto;
}

.bot-message pre code {
    display: block;
    padding: 1rem;
    font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
    font-size: 0.82rem;
    line-height: 1.55;
    color: #e5e7eb;
    background: #1e1e1e;
    white-space: pre;
    tab-size: 2;
}

.terminal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.5rem 0.85rem;
    background: #2d2d2d;
    border-bottom: 1px solid #3d3d3d;
}

.terminal-language {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: #9ca3af;
}

.copy-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    padding: 0.35rem 0.65rem;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    background: #ffffff;
    color: #0fa080;
    font-size: 0.75rem;
    font-weight: 600;
    cursor: pointer;
    transition:
        background var(--askai-transition),
        color var(--askai-transition),
        border-color var(--askai-transition);
}

.copy-btn:hover {
    background: #f0fffe;
    color: #08846d;
    border-color: #0fa080;
}

.copy-btn svg {
    display: block;
    width: 14px;
    height: 16px;
}

/* ── Error state ── */
.bot-message-error {
    margin-bottom: 1rem;
    animation: askaiFadeUp 0.25s ease;
}

.error-container {
    display: flex;
    align-items: flex-start;
    gap: 0.6rem;
    padding: 0.85rem 1rem;
    border-radius: var(--askai-radius-sm);
    background: #fef2f2;
    border: 1px solid #fecaca;
    color: #991b1b;
    font-size: 0.92rem;
    line-height: 1.45;
}

.error-container .error-icon {
    flex-shrink: 0;
    width: 1.1rem;
    height: 1.1rem;
    margin-top: 0.1rem;
    border-radius: 50%;
    background: #ef4444;
}

/* ── Sources & feedback footer ── */
.response-footer {
    margin: 0.5rem 0 1rem 1.75rem;
    padding-top: 0.25rem;
    animation: askaiFadeUp 0.35s ease;
}

.sources-section {
    margin-bottom: 0.75rem;
}

.sources-section h4 {
    margin: 0 0 0.45rem;
    font-size: 0.78rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--askai-text-light);
}

.sources-list {
    margin: 0;
    padding-left: 1.25rem;
    font-size: 0.88rem;
    line-height: 1.5;
}

.sources-list li {
    margin-bottom: 0.3rem;
}

.sources-list a {
    color: var(--askai-teal);
    text-decoration: none;
    transition: color var(--askai-transition);
}

.sources-list a:hover {
    color: #1d8fa5;
    text-decoration: underline;
}

.feedback-section {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    margin-top: 0.25rem;
}

.feedback-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 2rem;
    padding: 0;
    border: none;
    border-radius: 6px;
    background: transparent;
    cursor: pointer;
    transition: all var(--askai-transition);
    color: var(--askai-text-muted);
}

.feedback-btn:hover {
    background: rgba(15, 160, 128, 0.08);
    color: #0fa080;
}

.feedback-btn svg {
    display: block;
    width: 16px;
    height: 17px;
}

.feedback-btn[data-feedback="false"] svg {
    width: 20px;
    height: 20px;
}

/* ── Footer & input ── */
.chat-footer {
    flex-shrink: 0;
    padding: 0.85rem 1.1rem 1rem;
    border-top: 1px solid var(--askai-border);
    background: var(--askai-bg);
}

.chat-input {
    position: relative;
    display: flex;
    align-items: flex-end;
    width: 100%;
}

.chat-textarea {
    box-sizing: border-box;
    flex: 1;
    width: 100%;
    min-height: 44px;
    max-height: 150px;
    padding: 0.65rem 2.9rem 0.65rem 0.85rem;
    border: 1px solid var(--askai-border-input);
    border-radius: var(--askai-radius-xs);
    background: var(--askai-bg);
    color: var(--askai-text);
    font-family: var(--askai-font);
    font-size: 0.95rem;
    line-height: 1.45;
    resize: none;
    overflow-y: auto;
    outline: none;
    word-wrap: break-word;
    white-space: pre-wrap;
    transition:
        border-color var(--askai-transition),
        box-shadow var(--askai-transition);
}

.chat-textarea::placeholder {
    color: var(--askai-text-light);
}

.chat-textarea:focus,
.chat-textarea.focused {
    border-color: var(--askai-teal);
    box-shadow: 0 0 0 3px rgba(39, 165, 190, 0.18);
}

.chat-textarea:disabled {
    background: var(--askai-bg-subtle);
    color: var(--askai-text-muted);
    cursor: not-allowed;
}

.send-btn {
    position: absolute;
    right: 6px;
    bottom: 50%;
    transform: translateY(50%);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    padding: 0;
    margin: 0;
    border: none;
    border-radius: 50%;
    background: transparent;
    box-shadow: none;
    cursor: pointer;
    line-height: 0;
    overflow: hidden;
    transition: transform 0.15s ease, opacity 0.15s ease;
}

.send-btn:hover {
    background: transparent;
    box-shadow: none;
    transform: translateY(50%) scale(1.06);
}

.send-btn:active {
    background: transparent;
    box-shadow: none;
    transform: translateY(50%) scale(0.96);
}

.send-btn:disabled {
    cursor: not-allowed;
    opacity: 0.7;
}

.send-btn svg {
    display: block;
    width: 32px;
    height: 32px;
    flex-shrink: 0;
}

.ai-declaration-text {
    margin: 0.55rem 0 0;
    font-size: 0.72rem;
    line-height: 1.4;
    text-align: center;
    color: var(--askai-text-light);
}

/* ── Accessibility ── */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ── Mobile: full-screen modal ── */
@media (max-width: 640px) {
    .chat-container {
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        transform: none;
        width: 100%;
        max-width: 100%;
        max-height: 100%;
        height: 100%;
        border-radius: 0;
    }

    .chat-container.is-visible {
        animation: askaiModalInMobile 0.25s ease;
    }

    @keyframes askaiModalInMobile {
        from {
            opacity: 0;
            transform: translateY(100%);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    .chat-header {
        padding: 0.8rem 1rem;
    }

    .chat-messages {
        padding: 1rem 0.9rem;
    }

    .chat-footer {
        padding: 0.75rem 0.9rem calc(0.85rem + env(safe-area-inset-bottom, 0px));
    }

    .intro-text {
        font-size: 1rem;
    }

    .user-message-text {
        max-width: 92%;
    }

    .bot-message.terminal-style,
    .response-footer {
        margin-left: 0;
    }

    .reply-message .reply-svg,
    .reply-message > svg {
        width: 20px;
        height: 24px;
    }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
    .chat-overlay,
    .chat-container,
    .example-question,
    .restart-btn,
    .close-btn,
    .send-btn,
    .feedback-btn,
    .copy-btn {
        transition: none;
    }

    .chat-container.is-visible,
    .user-message,
    .reply-message,
    .thinking-indicators,
    .bot-message-error,
    .response-footer {
        animation: none;
    }

    .thinking-indicators .dots {
        animation: none;
        opacity: 0.7;
    }

    .chat-messages {
        scroll-behavior: auto;
    }
}
