/* ───────────────────────────────────────────────────
   Moodle Support AI – Chat Styles
   ─────────────────────────────────────────────────── */

:root {
    /* Dynamics: JS probes these from the theme and injects them into the app container */
    --mai-primary:   #4338ca; /* Fallback */
    --mai-btn-text:  #ffffff; /* Fallback */
    --mai-primary-l: #6366f1; /* Fallback */
    --mai-bg:        #ffffff;
    --mai-surface:   #f8fafc;
    --mai-surface-2: rgba(0, 0, 0, 0.05);
    --mai-border:    #cbd5e1;
    --mai-text:      #0f172a; /* Fallback */
    --mai-text-dim:  #64748b;
    --mai-accent:    #0ea5e9; /* Fallback (JS probes this) */
    --mai-success:   #10b981;
    --mai-radius:    1rem;
}

/* ── Wrapper ────────────────────────────────────── */
.moodle-ai-chat-app,
.moodle-ai-chat-app *,
.moodle-ai-chat-app input,
.moodle-ai-chat-app button,
.moodle-ai-chat-app select,
.moodle-ai-chat-app textarea {
    font-family: var(--mai-theme-font, inherit) !important;
}

.moodle-ai-chat-app {
    font-family: var(--mai-theme-font, inherit);
    color: var(--mai-text);
    width: 100%;
    margin: 0 auto;
    border-radius: var(--mai-radius);
    overflow: hidden;
    box-shadow: 0 10px 40px -10px rgba(0, 0, 0, 0.15);
    background: var(--mai-bg);
    color: var(--mai-text);
}

/* ── Role Picker ────────────────────────────────── */
/* ── Role Picker (Redesigned Welcome Screen) ── */
.moodle-ai-role-picker {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 520px;
    padding: 0;
    background: #fdfdff;
    overflow: hidden;
}

.moodle-ai-role-picker__container {
    display: grid;
    grid-template-columns: 1fr 1fr; /* Symmetrical grid to avoid "too narrow" image */
    width: 100%;
    max-width: 1000px;
    height: 100%;
    gap: 0;
}

.moodle-ai-role-picker__left {
    padding: 3rem 4rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
    animation: mai-fadeRight 0.6s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.moodle-ai-role-picker__right {
    position: relative;
    background: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    padding: 3rem; /* Increased padding */
}

.moodle-ai-welcome__label {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: rgba(67, 56, 202, 0.1);
    color: var(--mai-primary);
    padding: 4px 12px;
    border-radius: 999px;
    font-size: 0.7rem;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    margin-bottom: 1.5rem;
    width: fit-content;
}

.moodle-ai-welcome__sparkle {
    color: var(--mai-primary);
    animation: mai-sparkle 2s infinite ease-in-out;
}

.moodle-ai-welcome__title {
    margin: 0 0 1.5rem; /* Layout only */
    line-height: 1.2;
    font-family: inherit;
    color: inherit; /* Take color from theme/chosen tag */
    /* Removed font-size and font-weight to let H1/H2/H3 styles from theme dominate */
}

.moodle-ai-welcome__description {
    margin: 0 0 2.5rem;
    max-width: 580px; /* Widened slightly for better readability with theme fonts */
    color: inherit;
    font-family: inherit;
    /* Removed font-size and line-height to let theme paragraph styles dominate */
}

.moodle-ai-role-picker.has-no-image .moodle-ai-role-picker__container {
    grid-template-columns: 1fr;
    max-width: 800px;
}

.moodle-ai-role-picker.has-no-image .moodle-ai-role-picker__left {
    padding: 4rem 2rem;
    align-items: center;
    text-align: center;
}

.moodle-ai-role-picker.has-no-image .moodle-ai-welcome__label {
    margin-left: auto;
    margin-right: auto;
}

.moodle-ai-role-picker.has-no-image .moodle-ai-role-selector {
    justify-content: center;
    flex-wrap: wrap; /* Ensure cards wrap nicely if needed */
}

.moodle-ai-role-picker.has-no-image .moodle-ai-role-card {
    min-width: 160px;
    max-width: 220px;
}

.moodle-ai-role-picker.has-no-image .moodle-ai-welcome__input-group {
    width: 100%;
    max-width: 720px;
}

.moodle-ai-role-picker.has-no-image .moodle-ai-role-picker__right {
    display: none;
}

/* ── Role Selector Cards ── */
.moodle-ai-role-selector {
    display: flex;
    gap: 1rem;
    margin-bottom: 2rem;
}

.moodle-ai-role-card {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.75rem;
    padding: 1.25rem;
    background: #fff;
    border: 2px solid #eef0f7;
    border-radius: 1rem;
    cursor: pointer;
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    text-align: center;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
}

.moodle-ai-role-card:hover {
    border-color: var(--mai-primary-l);
    background: #fff;
    transform: translateY(-4px);
    box-shadow: 0 12px 24px -10px rgba(67, 56, 202, 0.25);
}

.moodle-ai-role-card.mai-is-active {
    border-color: var(--mai-primary);
    background: #f8faff !important; /* Force light background to avoid theme black */
    box-shadow: 0 12px 20px -8px rgba(67, 56, 202, 0.2);
}

.moodle-ai-role-card.mai-is-active .moodle-ai-role-card__name {
    color: var(--mai-primary); /* Ensure name is readable */
}

.moodle-ai-role-card__icon {
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f1f4ff;
    border-radius: 0.75rem;
    font-size: 1.5rem;
    color: var(--mai-primary);
}

.moodle-ai-role-card__icon img {
    width: 32px;
    height: 32px;
    object-fit: contain;
}

.moodle-ai-role-card__name {
    font-size: 0.875rem;
    font-weight: 700;
    color: #1e1b4b;
}

/* ── Welcome Input Group ── */
.moodle-ai-welcome__input-group {
    position: relative;
    display: flex;
    align-items: center;
    background: #fff;
    border: 2px solid var(--mai-primary); /* Use theme color probed by JS */
    border-radius: 1.25rem;
    padding: 6px 6px 6px 1.5rem;
    transition: all 0.2s ease;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
    width: 100%;
    max-width: 720px; /* Much wider as requested */
}

.moodle-ai-welcome__input-group:focus-within {
    border-color: var(--mai-primary-l);
    box-shadow: 0 10px 15px -3px rgba(67, 56, 202, 0.1);
}

.moodle-ai-welcome__input-group input {
    flex: 1;
    border: none !important;
    background-color: transparent !important;
    background: transparent !important;
    padding: 12px 0;
    font-size: 1rem;
    color: #1e1b4b;
    outline: none !important;
    box-shadow: none !important;
    appearance: none;
}

.moodle-ai-welcome__start-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--mai-primary);
    color: var(--mai-btn-text);
    border: none;
    padding: 12px 24px;
    border-radius: 1rem;
    font-weight: 700;
    font-size: 0.95rem;
    cursor: pointer;
    transition: all 0.2s ease;
}

.moodle-ai-welcome__start-btn:hover {
    background: #3730a3;
    transform: scale(1.02);
}

.moodle-ai-welcome__image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 15%;
    animation: mai-fadeBlur 1s ease forwards;
}

.moodle-ai-welcome__image-placeholder {
    width: 80%;
    height: 80%;
    background: linear-gradient(135deg, #eef2ff 0%, #e0e7ff 100%);
    border-radius: 2rem;
}

/* ── Accessibility ──────────────────────────────── */
.screen-reader-text {
    border: 0;
    clip: rect(1px, 1px, 1px, 1px);
    clip-path: inset(50%);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
    word-wrap: normal !important;
}

/* Styles removed as they were replaced by newer components */

/* ── Chat Container ─────────────────────────────── */
.moodle-ai-chat {
    display: flex;
    flex-direction: column;
    height: 520px;
    background: var(--mai-bg);
    color: var(--mai-text);
    border: 1px solid var(--mai-border);
    border-radius: var(--mai-radius);
    overflow: hidden;
    animation: mai-fadeUp .4s ease forwards;
}

/* ── Modal Overlay & Modal State ────────────────── */
.moodle-ai-chat-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
    z-index: 99998;
    animation: mai-fadeIn .3s ease forwards;
}

.moodle-ai-chat.is-modal {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 99999;
    width: 92%;
    max-width: 680px;
    height: 85vh;
    max-height: 800px;
    border-radius: var(--mai-radius);
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
    border: 1px solid var(--mai-border);
    animation: mai-modalIn .35s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.moodle-ai-chat__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: .85rem 1.25rem;
    background: var(--mai-surface);
    border-bottom: 1px solid var(--mai-border);
}

.moodle-ai-chat__header-info {
    display: flex;
    align-items: center;
    gap: .5rem;
    font-size: .9rem;
}

.moodle-ai-chat__header-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--mai-success);
    animation: mai-pulse 2s infinite;
}

/* ── Role badge: avatar (links) + tekst-pill ─────── */
.moodle-ai-chat__role-badge {
    display: flex;
    align-items: center;
    gap: 0;
}

/* Avatar-cirkel – staat links, buiten de pill */
.moodle-ai-role-badge__avatar {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: #fff;
    flex-shrink: 0;
    overflow: hidden;
    z-index: 2; /* Sit on top of the pill slightly */
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.moodle-ai-role-badge__avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
}

.moodle-ai-role-badge__avatar--emoji {
    font-size: 16px;
    line-height: 1;
}

/* Tekst-pill – overlapt lichtjes de avatar */
.moodle-ai-role-badge__text {
    display: inline-flex;
    align-items: center;
    height: 24px;
    padding: 0 .75rem 0 1.25rem;
    margin-left: -14px;          /* Tuck behind the avatar */
    border-radius: 999px;
    background: var(--mai-primary);
    color: #fff;
    font-size: .65rem;
    font-weight: 700;
    letter-spacing: .05em;
    text-transform: uppercase;
    line-height: 1;
    z-index: 1;
}

/* Zonder avatar: volledige pill */
.moodle-ai-chat__role-badge:not(:has(.moodle-ai-role-badge__avatar)) .moodle-ai-role-badge__text {
    padding: .25rem .75rem;
    margin-left: 0;
    border-radius: 999px;
}

/* ── Switch-role wrapper & dropdown ─────────────── */
.moodle-ai-switch-role-wrapper {
    position: relative;
}

/* ── Role dropdown menu ──────────────────────────── */
.moodle-ai-role-dropdown {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    min-width: 160px;
    background: var(--mai-bg);
    border: 1.5px solid var(--mai-border);
    border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0,0,0,.12);
    overflow: hidden;
    z-index: 9999;
    /* hidden by default */
    opacity: 0;
    transform: translateY(-6px) scale(.97);
    pointer-events: none;
    transition: opacity .18s ease, transform .18s ease;
}

.moodle-ai-role-dropdown.is-open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

.moodle-ai-dropdown-item {
    display: flex !important;
    align-items: center !important;
    gap: 10px !important;
    width: 100% !important;
    padding: .65rem 1rem !important;
    background: none !important;
    border: none !important;
    border-radius: 0 !important;
    color: var(--mai-text) !important;
    font-size: .875rem !important;
    font-weight: 500 !important;
    text-align: left !important;
    cursor: pointer !important;
    transition: background .15s !important;
}

.moodle-ai-dropdown-item:not(:last-child) {
    border-bottom: 1px solid var(--mai-border) !important;
}

.moodle-ai-dropdown-item:hover {
    background: var(--mai-surface-2) !important;
    color: var(--mai-primary) !important;
}

.moodle-ai-dropdown-item__img {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.moodle-ai-dropdown-item__emoji {
    font-size: 18px;
    line-height: 1;
    flex-shrink: 0;
}

.moodle-ai-switch-role {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    width: 38px !important;
    height: 38px !important;
    padding: 0 !important;
    border: 1.5px solid var(--mai-border) !important;
    border-radius: 50% !important;
    background: var(--mai-bg) !important;
    color: var(--mai-text) !important;
    cursor: pointer !important;
    transition: all .2s !important;
    flex-shrink: 0 !important;
}

.moodle-ai-switch-role svg, 
.moodle-ai-switch-role svg path {
    width: 20px !important;
    height: 20px !important;
    stroke: var(--mai-text) !important;
    stroke-width: 2px !important;
    fill: none !important;
    display: block !important;
    visibility: visible !important;
    opacity: 1 !important;
}

.moodle-ai-switch-role:hover {
    background: var(--mai-surface-2) !important;
    border-color: var(--mai-text-dim) !important;
    color: var(--mai-primary) !important;
}

.moodle-ai-switch-role:hover svg,
.moodle-ai-switch-role:hover svg path {
    stroke: var(--mai-primary) !important;
}

/* ── Messages Area ──────────────────────────────── */
.moodle-ai-chat__messages {
    flex: 1;
    overflow-y: auto;
    padding: 1.25rem;
    display: flex;
    flex-direction: column;
    gap: .75rem;
    scrollbar-width: thin;
    scrollbar-color: var(--mai-surface-2) transparent;
}

.moodle-ai-msg {
    max-width: 82%;
    padding: .8rem 1rem;
    border-radius: var(--mai-radius);
    font-size: .92rem;
    line-height: 1.55;
    animation: mai-fadeUp .25s ease;
    word-break: break-word;
}

.moodle-ai-msg--user {
    align-self: flex-end;
    background: var(--mai-primary);
    color: #fff;
    border-bottom-right-radius: .25rem;
}

.moodle-ai-msg--bot {
    align-self: flex-start;
    background: var(--mai-surface);
    border: 1px solid var(--mai-border);
    border-bottom-left-radius: .25rem;
}

/* Typing indicator */
.moodle-ai-msg--typing .mai-dots {
    display: inline-flex;
    gap: 4px;
}

.moodle-ai-msg--typing .mai-dots span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--mai-text-dim);
    animation: mai-bounce .6s infinite alternate;
}

.moodle-ai-msg--typing .mai-dots span:nth-child(2) { animation-delay: .15s; }
.moodle-ai-msg--typing .mai-dots span:nth-child(3) { animation-delay: .3s; }

/* ── Partner Card ───────────────────────────────── */
.moodle-ai-partner-card {
    background: linear-gradient(135deg, var(--mai-surface), var(--mai-surface-2));
    border: 1px solid var(--mai-primary); /* Changed from primary-l to primary for consistency */
    border-radius: var(--mai-radius);
    padding: 1rem 1.15rem;
    margin-top: .35rem;
    max-width: 82%;
    align-self: flex-start;
    animation: mai-fadeUp .3s ease;
}

.moodle-ai-partner-card__label {
    font-size: .7rem;
    text-transform: uppercase;
    letter-spacing: .07em;
    color: var(--mai-accent); /* Use probed theme accent */
    font-weight: 700;
    margin-bottom: .45rem;
}

.moodle-ai-partner-card__name {
    font-size: 1rem;
    font-weight: 700;
    margin-bottom: .25rem;
}

.moodle-ai-partner-card__meta {
    font-size: .8rem;
    color: var(--mai-text-dim);
    margin-bottom: .5rem;
    line-height: 1.5;
}

.moodle-ai-partner-card__link {
    display: inline-block;
    font-size: .82rem;
    color: var(--mai-accent);
    text-decoration: none;
    font-weight: 600;
    transition: color .2s;
}

.moodle-ai-partner-card__link:hover {
    color: var(--mai-primary);
}

/* ── Input Area ─────────────────────────────────── */
.moodle-ai-chat__form {
    display: flex;
    padding: .75rem 1rem;
    gap: .5rem;
    background: var(--mai-surface);
    border-top: 1px solid var(--mai-border);
}

.moodle-ai-chat__input {
    flex: 1;
    padding: .7rem 1rem;
    border: 1px solid var(--mai-border);
    border-radius: var(--mai-radius);
    background: var(--mai-bg);
    color: var(--mai-text);
    font-size: .92rem;
    outline: none;
    transition: border-color .2s;
}

.moodle-ai-chat__input:focus {
    border-color: var(--mai-primary-l);
    outline: none;
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

.moodle-ai-chat__input::placeholder {
    color: var(--mai-text-dim);
}

.moodle-ai-chat__send {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: .5rem;
    height: 44px;
    padding: 0 1.25rem;
    border: none;
    border-radius: calc(var(--mai-radius) - 4px);
    background: var(--mai-primary);
    color: var(--mai-btn-text);
    cursor: pointer;
    font-size: .95rem;
    font-weight: 600;
    transition: background .2s, transform .15s;
    flex-shrink: 0;
}

.moodle-ai-chat__send:hover:not(:disabled) {
    background: var(--mai-primary-l);
    transform: scale(1.03);
}

.moodle-ai-chat__send:disabled {
    opacity: .6;
    background: var(--mai-text-dim);
    cursor: not-allowed;
    transform: none;
}

/* ── Animations ─────────────────────────────────── */
@keyframes mai-fadeRight {
    from { opacity: 0; transform: translateX(-30px); }
    to { opacity: 1; transform: translateX(0); }
}

@keyframes mai-fadeBlur {
    from { opacity: 0; filter: blur(10px); }
    to { opacity: 1; filter: blur(0); }
}

@keyframes mai-sparkle {
    0%, 100% { transform: scale(1) rotate(0deg); opacity: 1; }
    50% { transform: scale(1.2) rotate(15deg); opacity: 0.8; }
}

@keyframes mai-fadeUp {
    from { opacity: 0; transform: translateY(12px); }
    to   { opacity: 1; transform: translateY(0); }
}

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

@keyframes mai-modalIn {
    from { opacity: 0; transform: translate(-50%, -46%) scale(0.96); }
    to   { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}

@keyframes mai-pulse {
    0%, 100% { opacity: 1; }
    50%      { opacity: .4; }
}

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

/* ── Responsive ─────────────────────────────────── */
@media (max-width: 900px) {
    .moodle-ai-role-picker__container {
        grid-template-columns: 1fr;
    }
    .moodle-ai-role-picker__right {
        display: none;
    }
    .moodle-ai-role-picker__left {
        padding: 2.5rem 2rem;
    }
    .moodle-ai-welcome__title {
        font-size: 2rem;
    }
}

@media (max-width: 600px) {
    .moodle-ai-role-selector {
        flex-direction: column;
    }
    .moodle-ai-welcome__input-group {
        flex-direction: column;
        padding: 1.5rem;
        gap: 1rem;
        align-items: stretch;
    }
    .moodle-ai-welcome__input-group input {
        text-align: center;
    }
    .moodle-ai-welcome__start-btn {
        justify-content: center;
    }
}

@media (max-width: 560px) {
    .moodle-ai-chat-app {
        border-radius: 0;
    }

    .moodle-ai-chat {
        height: 100dvh;
    }

    .moodle-ai-chat.is-modal {
        width: 100%;
        height: 100dvh;
        max-height: 100dvh;
        border-radius: 0;
        border: none;
    }

    .moodle-ai-chat__send .moodle-ai-btn-text {
        display: none;
    }
    
    .moodle-ai-chat__send {
        padding: 0;
        width: 44px;
    }
}

/* ── Markdown Content Styling ───────────────────── */
.moodle-ai-msg p {
    margin: 0 0 .75rem 0;
}
.moodle-ai-msg p:last-child {
    margin-bottom: 0;
}
.moodle-ai-msg h2, .moodle-ai-msg h3 {
    margin: 1.25rem 0 .5rem 0;
    font-weight: 700;
    line-height: 1.3;
    color: inherit;
}
.moodle-ai-msg h2:first-child, .moodle-ai-msg h3:first-child {
    margin-top: 0;
}
.moodle-ai-msg h2 {
    font-size: 1.1rem;
}
.moodle-ai-msg h3 {
    font-size: 1rem;
}
.moodle-ai-msg ul, .moodle-ai-msg ol {
    margin: 0 0 .75rem 0;
    padding-left: 1.25rem;
}
.moodle-ai-msg li {
    margin-bottom: .25rem;
}
.moodle-ai-msg strong {
    font-weight: 700;
}
