:root {
    color-scheme: light;
    --blue: #2563eb;
    --blue-dark: #1d4ed8;
    --surface: #ffffff;
    --page: #eef2f7;
    --muted: #64748b;
    --line: #dbe3ef;
    --bot: #eef2f7;
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html,
body {
    width: 100%;
    min-height: 100%;
    margin: 0;
    overflow-x: hidden;
}

body {
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: var(--page);
    color: #0f172a;
    font-family: Arial, Helvetica, sans-serif;
}

button,
input {
    font: inherit;
}

button {
    -webkit-tap-highlight-color: transparent;
}

/* PAGE D'ACCUEIL */

.welcome-screen {
    width: min(100%, 790px);
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 36px 54px;
    background: #fff;
}

.welcome-logo {
    width: 112px;
    height: 112px;
    margin-bottom: 22px;
    border-radius: 50%;
    object-fit: cover;
    filter: drop-shadow(0 7px 12px rgba(30, 64, 175, .16));
}

.welcome-screen h1 {
    margin: 0;
    color: #173c91;
    text-align: center;
    font-size: clamp(30px, 5vw, 42px);
    line-height: 1.15;
}

.welcome-screen > p {
    margin: 17px 0 43px;
    color: var(--muted);
    text-align: center;
    font-size: clamp(19px, 3vw, 25px);
}

/* CARTES DES QUATRE MATIÈRES */

.subject-grid {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 24px 22px;
}

.subject-card {
    min-height: 156px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 13px;
    padding: 20px 12px;
    border: 2px solid #d6e7ff;
    border-radius: 24px;
    color: #153b90;
    background: #f7faff;
    box-shadow: 0 7px 18px rgba(37, 99, 235, .07);
    cursor: pointer;
    transition: transform .18s ease,
                border-color .18s ease,
                box-shadow .18s ease;
}

.subject-card:hover,
.subject-card:focus-visible {
    transform: translateY(-3px);
    border-color: #93c5fd;
    box-shadow: 0 12px 25px rgba(37, 99, 235, .14);
    outline: 0;
}

.subject-card:active {
    transform: translateY(0);
}

.subject-icon {
    font-size: 52px;
    line-height: 1;
}

.subject-card strong {
    text-align: center;
    font-size: 22px;
}

/* PAGE DE DISCUSSION */

.chat-container {
    width: min(100%, 920px);
    height: min(860px, 100dvh);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--surface);
    border-radius: 20px;
    box-shadow: 0 14px 40px rgba(15, 23, 42, .16);
}

header {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 12px;
    min-height: 120px;
    padding: 20px 72px;
    color: #fff;
    text-align: center;
    background: linear-gradient(135deg, var(--blue), #1746b4);
}

.back-btn {
    position: absolute;
    left: 15px;
    top: 50%;
    width: 50px;
    height: 50px;
    padding: 0;
    transform: translateY(-50%);
    border: 1px solid rgba(255, 255, 255, .55);
    border-radius: 50%;
    color: #fff;
    background: rgba(255, 255, 255, .20);
    cursor: pointer;
    font-size: 27px;
    line-height: 1;
}

.back-btn:hover {
    background: rgba(255, 255, 255, .24);
}

.brand-logo {
    width: 72px;
    height: 72px;
    display: block;
    flex: 0 0 auto;
    border: 2px solid rgba(255, 255, 255, .72);
    border-radius: 50%;
    background: #fff;
    object-fit: cover;
}

.chat-heading {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
}

.chat-title-row {
    display: flex;
    align-items: center;
    gap: 10px;
}

.chat-subject-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border: 2px solid rgba(255, 255, 255, .9);
    border-radius: 5px;
    background: rgba(255, 255, 255, .16);
    font-size: 27px;
    line-height: 1;
}

header h1 {
    margin: 0;
    font-size: 30px;
}

header p {
    margin: 3px 0 0;
    opacity: .95;
    font-size: 18px;
}

/* MESSAGES ET CORRECTIONS */

.chat-box {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 14px;
    padding: 18px;
    overflow-y: auto;
    overflow-x: hidden;
    scroll-behavior: smooth;
}

.message {
    flex: 0 0 auto;
    width: fit-content;
    max-width: 86%;
    padding: 12px 15px;
    border-radius: 16px;
    line-height: 1.58;
    font-size: 18px;
    overflow-wrap: anywhere;
}

.message.bot {
    align-self: flex-start;
    background: var(--bot);
    border-bottom-left-radius: 5px;
}

.message.user {
    align-self: flex-end;
    color: #fff;
    background: var(--blue);
    border-bottom-right-radius: 5px;
}

.message.waiting {
    color: var(--muted);
    animation: pulse 1.4s infinite;
}

/* Correction sur toute la largeur disponible */

.message.bot.correction-message {
    width: 100%;
    max-width: 100%;
    padding: 16px 18px;
}

@keyframes pulse {
    50% {
        opacity: .55;
    }
}

.response-content {
    width: 100%;
    max-width: 100%;
    overflow: hidden;
}

.response-content p {
    max-width: 100%;
    margin: 0 0 10px;
    overflow-wrap: anywhere;
}

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

.response-content ul,
.response-content ol {
    margin: 8px 0;
    padding-left: 24px;
}

/* FORMULES MATHÉMATIQUES */

.response-content mjx-container[display="true"] {
    display: block !important;
    width: 100% !important;
    max-width: 100% !important;
    overflow-x: auto !important;
    overflow-y: hidden;
    padding: 8px 2px;
    text-align: center;
    -webkit-overflow-scrolling: touch;
}

.response-content mjx-container:not([display="true"]) {
    display: inline-block !important;
    max-width: 100% !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    vertical-align: middle;
    -webkit-overflow-scrolling: touch;
}

.response-content mjx-container {
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.response-content mjx-container::-webkit-scrollbar {
    width: 0;
    height: 0;
    display: none;
}

/* FIGURES TIKZ */

.tikz-figure {
    margin: 18px 0;
    padding: 12px;
    background: white;
    border-radius: 12px;
}

.tikz-figure figcaption {
    font-weight: bold;
    margin-bottom: 8px;
}

.tikz-figure iframe {
    width: 100%;
    height: 360px;
    border: 0;
    background: white;
}

.tikz-figure pre {
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    font-size: 13px;
}

.tikz-figure summary {
    cursor: pointer;
    color: var(--blue-dark);
}

/* PIÈCES JOINTES */

.message-attachment {
    max-width: 100%;
    margin-bottom: 7px;
    font-weight: 700;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.composer {
    flex: 0 0 auto;
    padding: 10px 14px max(10px, env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
    background: #fff;
}

.attachment-preview {
    position: relative;
    width: min(100%, 430px);
    min-height: 60px;
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 0 0 9px;
    padding: 8px 40px 8px 9px;
    border: 1px solid #bfdbfe;
    border-radius: 12px;
    background: #eff6ff;
}

.attachment-preview img {
    width: 52px;
    height: 52px;
    object-fit: cover;
    border-radius: 8px;
}

.file-icon {
    font-size: 30px;
}

.file-meta {
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.file-meta strong {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-meta span {
    color: var(--muted);
    font-size: 12px;
}

.remove-file {
    position: absolute;
    top: 7px;
    right: 8px;
    width: 28px;
    height: 28px;
    border: 0;
    border-radius: 50%;
    color: #475569;
    background: #dbeafe;
    cursor: pointer;
    font-size: 21px;
    line-height: 1;
}

/* ZONE DE SAISIE */

.input-area {
    display: flex;
    gap: 8px;
    align-items: center;
}

#user-input {
    min-width: 0;
    flex: 1;
    padding: 12px 14px;
    border: 1px solid #cbd5e1;
    border-radius: 12px;
    outline: 0;
}

#user-input:focus {
    border-color: var(--blue);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}

.icon-btn,
#send-btn,
.latex-button,
.copy-latex-button {
    border: 0;
    border-radius: 10px;
    cursor: pointer;
    font-weight: 700;
}

.icon-btn {
    width: 45px;
    height: 45px;
    flex: 0 0 auto;
    background: #e2e8f0;
    font-size: 21px;
}

#send-btn {
    padding: 12px 18px;
    color: #fff;
    background: var(--blue);
}

#send-btn:hover {
    background: var(--blue-dark);
}

button:disabled {
    cursor: wait;
    opacity: .62;
}

.upload-help {
    margin: 7px 0 0 53px;
    color: var(--muted);
    font-size: 11px;
}

/* AFFICHAGE ET COPIE DU CODE LATEX */

.latex-container {
    margin-top: 12px;
}

.latex-button,
.copy-latex-button {
    padding: 8px 11px;
    color: #fff;
    background: #334155;
    font-size: 12px;
}

.copy-latex-button {
    margin-top: 7px;
    background: var(--blue);
}

.latex-code-container pre {
    max-width: 100%;
    margin: 8px 0 0;
    padding: 12px;
    overflow-x: auto;
    white-space: pre-wrap;
    border-radius: 8px;
    color: #e2e8f0;
    background: #0f172a;
    font: 12px/1.5 Consolas, monospace;
}

/* ADAPTATION AUX TÉLÉPHONES */

@media (max-width: 600px) {
    body {
        align-items: stretch;
    }

    .welcome-screen {
        justify-content: flex-start;
        padding: 34px 16px 28px;
    }

    .welcome-logo {
        width: 88px;
        height: 88px;
        margin-bottom: 18px;
    }

    .welcome-screen h1 {
        font-size: 29px;
    }

    .welcome-screen > p {
        margin: 13px 0 30px;
        font-size: 19px;
    }

    .subject-grid {
        gap: 14px 12px;
    }

    .subject-card {
        min-height: 132px;
        padding: 15px 7px;
        border-radius: 19px;
    }

    .subject-icon {
        font-size: 42px;
    }

    .subject-card strong {
        font-size: 17px;
    }

    .chat-container {
        width: 100%;
        height: 100dvh;
        border-radius: 0;
        box-shadow: none;
    }

    header {
        min-height: 105px;
        padding: 13px 54px;
        gap: 8px;
    }

    .back-btn {
        left: 9px;
        width: 38px;
        height: 38px;
    }

    .brand-logo {
        width: 58px;
        height: 58px;
    }

    .chat-subject-icon {
        width: 32px;
        height: 32px;
        font-size: 21px;
    }

    .chat-title-row {
        gap: 7px;
    }

    header h1 {
        font-size: 23px;
    }

    header p {
        font-size: 14px;
    }

    .chat-box {
        padding: 12px;
        -webkit-overflow-scrolling: touch;
    }

    .message {
        max-width: 94%;
        font-size: 17px;
        overflow: hidden;
    }

    .message.user {
        max-width: 88%;
    }

    .composer {
        position: sticky;
        bottom: 0;
        padding-inline: 9px;
    }

    .input-area {
        gap: 6px;
    }

    .icon-btn {
        width: 42px;
        height: 42px;
    }

    #user-input {
        padding: 11px;
        font-size: 16px;
    }

    #send-btn {
        padding: 11px 12px;
        font-size: 14px;
    }

    .upload-help {
        margin-left: 48px;
    }

    .response-content mjx-container[display="true"] {
        font-size: 88% !important;
    }

    .response-content mjx-container:not([display="true"]) {
        font-size: 92% !important;
    }
}