/* ========== THEME SYSTEM ========== */
:root, [data-theme="maths"] {
    --accent: #555;
    --accent-light: #f0f0f0;
    --accent-rgb: 85,85,85;
    --bg: #ffffff;
    --bg-secondary: #f7f7f9;
    --surface: #ffffff;
    --surface-hover: #f0f0f2;
    --text: #1a1a2e;
    --text-secondary: #6b7280;
    --border: #e5e7eb;
    --input-bg: #f3f4f6;
    --msg-user: #1a1a2e;
    --msg-user-text: #ffffff;
    --msg-delta: #f3f4f6;
    --msg-delta-text: #1a1a2e;
    --nav-bg: #ffffff;
    --shadow: 0 -1px 10px rgba(0,0,0,0.06);
}

[data-theme="physique"] {
    --accent: #EE9547;
    --accent-light: #FFF3E8;
    --accent-rgb: 238,149,71;
    --bg: #FFFAF5;
    --bg-secondary: #FFF3E8;
    --surface: #ffffff;
    --surface-hover: #FFF0E0;
    --text: #2D1B00;
    --text-secondary: #9A7050;
    --border: #F5D5B0;
    --input-bg: #FFF5EC;
    --msg-user: #EE9547;
    --msg-user-text: #ffffff;
    --msg-delta: #FFF3E8;
    --msg-delta-text: #2D1B00;
    --nav-bg: #ffffff;
    --shadow: 0 -1px 10px rgba(238,149,71,0.1);
}

[data-theme="chimie"] {
    --accent: #57B3FE;
    --accent-light: #EBF5FF;
    --accent-rgb: 87,179,254;
    --bg: #F5FAFF;
    --bg-secondary: #EBF5FF;
    --surface: #ffffff;
    --surface-hover: #E0F0FF;
    --text: #0A1929;
    --text-secondary: #5078A0;
    --border: #B0D5F5;
    --input-bg: #EEF6FF;
    --msg-user: #57B3FE;
    --msg-user-text: #ffffff;
    --msg-delta: #EBF5FF;
    --msg-delta-text: #0A1929;
    --nav-bg: #ffffff;
    --shadow: 0 -1px 10px rgba(87,179,254,0.1);
}

[data-theme="biologie"] {
    --accent: #6DC82A;
    --accent-light: #EDFAE0;
    --accent-rgb: 109,200,42;
    --bg: #F5FFF0;
    --bg-secondary: #EDFAE0;
    --surface: #ffffff;
    --surface-hover: #E0F5D5;
    --text: #0A2900;
    --text-secondary: #508040;
    --border: #B0E5A0;
    --input-bg: #F0FFEA;
    --msg-user: #6DC82A;
    --msg-user-text: #ffffff;
    --msg-delta: #EDFAE0;
    --msg-delta-text: #0A2900;
    --nav-bg: #ffffff;
    --shadow: 0 -1px 10px rgba(109,200,42,0.1);
}

/* ========== RESET & BASE ========== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    overflow: hidden;
    height: 100dvh;
    transition: background 0.3s, color 0.3s;
}

input, select, textarea, button {
    font-family: inherit;
    font-size: inherit;
}

a { color: var(--accent); text-decoration: none; }

/* ========== AUTH SCREEN ========== */
#auth-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100dvh;
    padding: 20px;
    background: linear-gradient(135deg, #FFF8F0 0%, #F0F8FF 50%, #F0FFF0 100%);
}

.auth-container {
    width: 100%;
    max-width: 380px;
    text-align: center;
}

.auth-logo {
    width: 100px;
    height: 100px;
    margin: 0 auto 12px;
}

/* Rotation par paliers de 120° toutes les 3s (le triangle est 3-symétrique,
   seules les couleurs du dégradé tournent). Cycle complet sur 9s (3 x 120°). */
.rotating-triangle,
.triangle-trail {
    transform-origin: 96px 99px;
    animation: logo-spin-120 9s infinite;
}

/* Flou de mouvement réaliste : 3 copies du triangle (markup dupliqué, pas
   cloné via <use>, pour un rendu fiable du foreignObject/masque) qui suivent
   la rotation avec un léger retard (animation-delay) et une opacité
   décroissante, créant une traînée. La rotation démarre vite (ease-out) :
   l'écart angulaire entre chaque copie est donc maximal juste après le
   départ (traînée bien visible) et se referme en douceur à l'arrêt. Pendant
   les pauses, les copies se superposent exactement au triangle principal et
   disparaissent naturellement. */
.triangle-trail-1 { opacity: 0.40; animation-delay: 0.08s; }
.triangle-trail-2 { opacity: 0.22; animation-delay: 0.16s; }
.triangle-trail-3 { opacity: 0.10; animation-delay: 0.24s; }

@keyframes logo-spin-120 {
    0%      { transform: rotate(0deg);   animation-timing-function: ease-out; }
    6.667%  { transform: rotate(120deg); }
    33.333% { transform: rotate(120deg); animation-timing-function: ease-out; }
    40%     { transform: rotate(240deg); }
    66.667% { transform: rotate(240deg); animation-timing-function: ease-out; }
    73.333% { transform: rotate(360deg); }
    100%    { transform: rotate(360deg); }
}

@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-8px); }
}

.auth-title {
    font-size: 2rem;
    font-weight: 800;
    letter-spacing: -0.5px;
    color: #1a1a2e;
}

.auth-subtitle {
    color: #6b7280;
    margin-bottom: 28px;
    font-size: 0.95rem;
}

.auth-tabs {
    display: flex;
    background: #f3f4f6;
    border-radius: 12px;
    padding: 4px;
    margin-bottom: 20px;
}

.auth-tab {
    flex: 1;
    padding: 10px;
    border: none;
    background: none;
    border-radius: 10px;
    font-weight: 600;
    font-size: 0.9rem;
    cursor: pointer;
    color: #6b7280;
    transition: all 0.2s;
}

.auth-tab.active {
    background: #ffffff;
    color: #1a1a2e;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.auth-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.input-group input,
.input-group select {
    width: 100%;
    padding: 14px 16px;
    border: 2px solid #e5e7eb;
    border-radius: 12px;
    font-size: 0.95rem;
    background: #ffffff;
    color: #1a1a2e;
    transition: border-color 0.2s;
    outline: none;
}

.input-group input:focus,
.input-group select:focus {
    border-color: #EE9547;
}

.btn-primary {
    padding: 14px;
    border: none;
    border-radius: 12px;
    background: var(--accent, #EE9547);
    color: #ffffff;
    font-weight: 700;
    font-size: 1rem;
    cursor: pointer;
    transition: transform 0.1s, opacity 0.2s;
}

.btn-primary:active { transform: scale(0.98); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

#auth-screen .btn-primary {
    background: linear-gradient(135deg, #EE9547, #e07830);
}

.auth-link {
    font-size: 0.85rem;
    color: #6b7280;
}

.auth-divider {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 16px 0;
    color: #9ca3af;
    font-size: 0.85rem;
}

.auth-divider::before,
.auth-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: #e5e7eb;
}

.btn-google {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    width: 100%;
    padding: 12px;
    border: 2px solid #e5e7eb;
    border-radius: 12px;
    background: #ffffff;
    font-weight: 600;
    font-size: 0.9rem;
    cursor: pointer;
    color: #1a1a2e;
    transition: border-color 0.2s, background 0.2s;
}

.btn-google:hover { background: #f9fafb; border-color: #d1d5db; }

.auth-error {
    margin-top: 12px;
    padding: 10px 14px;
    background: #FEF2F2;
    color: #DC2626;
    border-radius: 10px;
    font-size: 0.85rem;
    text-align: left;
}

/* ========== APP LAYOUT ========== */
#app {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    overflow: hidden;
}

/* ========== TOP BAR ========== */
#topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    z-index: 10;
    min-height: 52px;
}

.topbar-left {
    display: flex;
    align-items: center;
    gap: 8px;
}

.topbar-title {
    font-weight: 800;
    font-size: 1.2rem;
    letter-spacing: -0.3px;
}

.topbar-logo-btn {
    border: none;
    background: none;
    padding: 0;
    display: flex;
    cursor: pointer;
    border-radius: 8px;
    transition: transform 0.15s;
}

.topbar-logo-btn:active { transform: scale(0.88) rotate(15deg); }

.topbar-btn {
    width: 36px;
    height: 36px;
    border: none;
    background: var(--accent-light);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--accent);
    transition: transform 0.1s;
}

.topbar-btn:active { transform: scale(0.92); }

/* ========== MAIN CONTENT ========== */
#main {
    flex: 1;
    overflow: hidden;
    position: relative;
}

.view {
    display: none;
    height: 100%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.view.active { display: flex; flex-direction: column; }

/* ========== CHAT VIEW ========== */
#chat-view {
    position: relative;
}

#chat-main-col {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    height: 100%;
}

#chat-subject-bar {
    display: flex;
    gap: 6px;
    padding: 8px 12px;
    overflow-x: auto;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.chat-subject-btn {
    flex-shrink: 0;
    padding: 6px 12px;
    border: 1.5px solid var(--border);
    border-radius: 16px;
    background: var(--surface);
    color: var(--text);
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.chat-subject-btn[data-subject="maths"].active     { border-color: #555;    background: #55555522; color: #333; }
.chat-subject-btn[data-subject="physique"].active  { border-color: #EE9547; background: #EE954722; color: #EE9547; }
.chat-subject-btn[data-subject="chimie"].active    { border-color: #57B3FE; background: #57B3FE22; color: #2E8AD6; }
.chat-subject-btn[data-subject="biologie"].active  { border-color: #6DC82A; background: #6DC82A22; color: #4B9A18; }

#welcome-screen {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 24px 20px;
    text-align: center;
    gap: 12px;
}

.welcome-logo {
    animation: float 3s ease-in-out infinite;
}

.welcome-title {
    font-size: 1.4rem;
    font-weight: 700;
}

.welcome-text {
    color: var(--text-secondary);
    font-size: 0.9rem;
    max-width: 300px;
}

.quick-prompts {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
    margin-top: 8px;
    max-width: 360px;
}

.quick-prompt {
    padding: 8px 14px;
    border: 1.5px solid var(--border);
    border-radius: 20px;
    background: var(--surface);
    font-size: 0.82rem;
    color: var(--text);
    cursor: pointer;
    transition: all 0.2s;
}

.quick-prompt:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: var(--accent-light);
}

/* Messages */
#messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
}

.message {
    display: flex;
    gap: 8px;
    max-width: 92%;
    animation: msgIn 0.2s ease-out;
}

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

.message.user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.msg-avatar {
    width: 30px;
    height: 30px;
    border-radius: 10px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 2px;
}

.msg-avatar svg {
    width: 22px;
    height: 22px;
}

.msg-avatar.delta-avatar {
    background: var(--accent-light);
}

/* Logo animé pendant que Delta réfléchit — même animation que l'écran de
   connexion (triangle + traînée), en couleur unie via currentColor. */
.thinking-logo {
    color: var(--accent);
    overflow: visible;
}

.msg-bubble {
    padding: 10px 14px;
    border-radius: 16px;
    font-size: 0.92rem;
    line-height: 1.6;
    word-break: break-word;
}

.message.user .msg-bubble {
    background: var(--msg-user);
    color: var(--msg-user-text);
    border-bottom-right-radius: 4px;
}

.message.delta .msg-bubble {
    background: var(--msg-delta);
    color: var(--msg-delta-text);
    border-bottom-left-radius: 4px;
}

.msg-bubble p { margin-bottom: 8px; }
.msg-bubble p:last-child { margin-bottom: 0; }

.msg-bubble strong { font-weight: 700; }
.msg-bubble em { font-style: italic; }

.msg-bubble code {
    background: rgba(var(--accent-rgb), 0.1);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
    font-size: 0.85em;
}

.msg-bubble pre {
    background: #1a1a2e;
    color: #e5e7eb;
    padding: 12px;
    border-radius: 10px;
    overflow-x: auto;
    margin: 8px 0;
    font-size: 0.82rem;
}

.msg-bubble pre code {
    background: none;
    padding: 0;
    color: inherit;
}

.msg-bubble ol, .msg-bubble ul {
    padding-left: 20px;
    margin: 6px 0;
}

.msg-bubble li { margin: 4px 0; }

.msg-bubble h1, .msg-bubble h2, .msg-bubble h3 {
    margin: 12px 0 6px;
    font-weight: 700;
}

.msg-bubble h3 { font-size: 1rem; }

.msg-bubble .katex-display {
    overflow-x: auto;
    padding: 8px 0;
}

.msg-image {
    max-width: 100%;
    border-radius: 10px;
    margin: 8px 0;
}

.msg-bubble .plotly-chart {
    width: 100%;
    min-height: 250px;
    border-radius: 10px;
    overflow: hidden;
    margin: 8px 0;
}

.msg-bubble .three-canvas {
    width: 100%;
    height: 250px;
    border-radius: 10px;
    overflow: hidden;
    margin: 8px 0;
}

.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 8px 14px;
}

.typing-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--accent);
    animation: typing 1.2s infinite;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-6px); opacity: 1; }
}

/* Input Area */
#input-area {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 12px;
    border-top: 1px solid var(--border);
    background: var(--surface);
}

.input-wrapper {
    flex: 1;
    position: relative;
}

#user-input {
    width: 100%;
    padding: 10px 14px;
    border: 2px solid var(--border);
    border-radius: 20px;
    background: var(--input-bg);
    color: var(--text);
    font-size: 0.92rem;
    resize: none;
    outline: none;
    max-height: 120px;
    line-height: 1.4;
    transition: border-color 0.2s;
}

#user-input:focus { border-color: var(--accent); }

.input-btn {
    width: 40px;
    height: 40px;
    border: none;
    background: none;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--text-secondary);
    flex-shrink: 0;
    transition: all 0.2s;
}

.input-btn:hover { color: var(--accent); }

#send-btn:disabled { opacity: 0.3; }
#send-btn:not(:disabled):hover { transform: scale(1.1); }

/* Photo preview */
#photo-preview {
    position: relative;
    padding: 8px 12px;
    border-top: 1px solid var(--border);
    background: var(--surface);
}

#photo-preview-img {
    height: 80px;
    border-radius: 10px;
    object-fit: cover;
}

#photo-remove {
    position: absolute;
    top: 4px;
    right: 8px;
    width: 24px;
    height: 24px;
    border: none;
    background: rgba(0,0,0,0.6);
    color: white;
    border-radius: 50%;
    font-size: 1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ========== EXERCISES VIEW ========== */
#exercises-view {
    padding: 20px 16px;
    gap: 20px;
}

.exercises-header h2 {
    font-size: 1.3rem;
    font-weight: 700;
}

.exercises-header p {
    color: var(--text-secondary);
    font-size: 0.88rem;
    margin-top: 4px;
}

.exercises-config {
    display: flex;
    flex-direction: column;
    gap: 16px;
    background: var(--surface);
    padding: 16px;
    border-radius: 16px;
    border: 1px solid var(--border);
}

.config-row {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.config-row label {
    font-weight: 600;
    font-size: 0.88rem;
    color: var(--text-secondary);
}

.subject-chips {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.subject-chip {
    padding: 10px;
    border: 2px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
    font-weight: 600;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--text);
}

.subject-chip.active {
    border-color: var(--accent);
    background: var(--accent-light);
    color: var(--accent);
}

.chip-icon { font-size: 1.1rem; }

.difficulty-chips {
    display: flex;
    gap: 8px;
}

.diff-chip {
    flex: 1;
    padding: 8px;
    border: 2px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    font-weight: 600;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s;
    color: var(--text);
}

.diff-chip.active {
    border-color: var(--accent);
    background: var(--accent-light);
    color: var(--accent);
}

#ex-topic {
    width: 100%;
    padding: 10px 14px;
    border: 2px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    color: var(--text);
    font-size: 0.9rem;
    outline: none;
}

#ex-topic:focus { border-color: var(--accent); }

input[type="range"] {
    width: 100%;
    accent-color: var(--accent);
}

#ex-count-label {
    font-weight: 700;
    color: var(--accent);
}

.btn-block { width: 100%; }

.btn-secondary-block {
    width: 100%;
    padding: 12px;
    margin-top: 8px;
    border: 2px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
    color: var(--text);
    font-weight: 600;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-secondary-block:hover { border-color: var(--accent); color: var(--accent); }

#ex-custom-request {
    width: 100%;
    padding: 10px 14px;
    border: 2px solid var(--border);
    border-radius: 12px;
    background: var(--input-bg);
    color: var(--text);
    font-size: 0.88rem;
    font-family: inherit;
    resize: vertical;
    outline: none;
    transition: border-color 0.2s;
}

#ex-custom-request:focus { border-color: var(--accent); }

.ex-file-upload-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    border: 2px dashed var(--border);
    border-radius: 12px;
    background: var(--surface);
    color: var(--text-secondary);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.ex-file-upload-btn:hover { border-color: var(--accent); color: var(--accent); }

.ex-file-upload-btn.has-file {
    border-style: solid;
    border-color: var(--accent);
    color: var(--accent);
    background: var(--accent-light);
}

.ex-file-remove-btn {
    width: 24px;
    height: 24px;
    border: none;
    background: var(--bg-secondary);
    color: var(--text-secondary);
    border-radius: 50%;
    font-size: 1rem;
    cursor: pointer;
    align-self: flex-start;
    margin-top: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.ex-file-remove-btn:hover { color: #DC2626; }

/* Exercise cards */
.exercise-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 16px;
    margin-bottom: 12px;
    animation: msgIn 0.3s ease-out;
}

.exercise-card .ex-number {
    font-weight: 700;
    font-size: 0.82rem;
    color: var(--accent);
    margin-bottom: 6px;
}

.exercise-card .ex-question {
    font-size: 0.92rem;
    margin-bottom: 12px;
    line-height: 1.6;
}

.exercise-card .ex-input {
    width: 100%;
    padding: 10px 14px;
    border: 2px solid var(--border);
    border-radius: 10px;
    background: var(--input-bg);
    color: var(--text);
    font-size: 0.9rem;
    outline: none;
    margin-bottom: 8px;
}

.exercise-card .ex-input:focus { border-color: var(--accent); }

.exercise-card .ex-check {
    padding: 8px 16px;
    border: none;
    border-radius: 8px;
    background: var(--accent);
    color: white;
    font-weight: 600;
    font-size: 0.85rem;
    cursor: pointer;
}

.exercise-card .ex-result {
    margin-top: 10px;
    padding: 10px;
    border-radius: 10px;
    font-size: 0.88rem;
    line-height: 1.5;
}

.exercise-card .ex-result.correct {
    background: #EDFAE0;
    color: #15803d;
    border: 1px solid #B0E5A0;
}

.exercise-card .ex-result.incorrect {
    background: #FEF2F2;
    color: #DC2626;
    border: 1px solid #FECACA;
}

.exercise-card .ex-solution {
    margin-top: 10px;
    padding: 10px;
    background: var(--bg-secondary);
    border-radius: 10px;
    font-size: 0.88rem;
    line-height: 1.6;
}

/* ========== RESOLUTION VIEW ========== */
#resolution-view {
    padding: 20px 16px;
    gap: 16px;
}

.resolution-header h2 {
    font-size: 1.3rem;
    font-weight: 700;
}

.resolution-header p {
    color: var(--text-secondary);
    font-size: 0.88rem;
    margin-top: 4px;
}

.resolution-upload-zone {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 36px 20px;
    border: 2px dashed var(--border);
    border-radius: 16px;
    background: var(--surface);
    color: var(--text-secondary);
    cursor: pointer;
    text-align: center;
    transition: all 0.2s;
}

.resolution-upload-zone:hover { border-color: var(--accent); color: var(--accent); }
.resolution-upload-zone p { font-size: 0.88rem; font-weight: 600; }

#resolution-preview {
    position: relative;
    display: flex;
    justify-content: center;
}

#resolution-preview-img {
    max-width: 100%;
    max-height: 320px;
    border-radius: 14px;
    object-fit: contain;
}

#resolution-remove {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 28px;
    height: 28px;
    border: none;
    background: rgba(0,0,0,0.6);
    color: white;
    border-radius: 50%;
    font-size: 1.1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

#resolution-result {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

#resolution-result .msg-bubble {
    background: var(--msg-delta);
    color: var(--msg-delta-text);
    border-radius: 16px;
    padding: 14px;
}

/* ========== CHAT SIDEBAR (historique par matière) ========== */
#chat-sidebar {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 280px;
    max-width: 82vw;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 16px 12px;
    z-index: 20;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    overflow-y: auto;
}

#chat-sidebar.open { transform: translateX(0); }

#sidebar-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 15;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
}

#sidebar-overlay.open { opacity: 1; pointer-events: auto; }

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 4px;
}

.sidebar-title { font-weight: 700; font-size: 1rem; }

.sidebar-close-btn {
    width: 28px;
    height: 28px;
    border: none;
    background: none;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
}

#chat-sidebar #history-list { flex: 1; display: flex; flex-direction: column; gap: 8px; }

.search-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    background: var(--input-bg);
    border-radius: 12px;
    border: 2px solid transparent;
    transition: border-color 0.2s;
}

.search-bar:focus-within { border-color: var(--accent); }

.search-bar input {
    flex: 1;
    border: none;
    background: none;
    outline: none;
    color: var(--text);
    font-size: 0.9rem;
}

.search-bar svg { color: var(--text-secondary); flex-shrink: 0; }

.history-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    cursor: pointer;
    transition: all 0.2s;
}

.history-item:hover { border-color: var(--accent); }

.history-item .hi-icon {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    background: var(--accent-light);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.history-item .hi-icon svg {
    width: 20px;
    height: 20px;
}

.history-item .hi-content { flex: 1; min-width: 0; }

.history-item .hi-title {
    font-weight: 600;
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-item .hi-meta {
    display: flex;
    gap: 8px;
    font-size: 0.78rem;
    color: var(--text-secondary);
    margin-top: 2px;
}

.history-item .hi-delete {
    width: 32px;
    height: 32px;
    border: none;
    background: none;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.2s;
}

.history-item .hi-delete:hover { color: #DC2626; }

.empty-state {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

/* ========== PROFILE VIEW ========== */
#profile-view {
    padding: 20px 16px;
    padding-bottom: 30px;
    gap: 16px;
    overflow-y: auto !important;
}

.profile-card {
    text-align: center;
    padding: 24px 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
}

.profile-avatar {
    width: 70px;
    height: 70px;
    margin: 0 auto 10px;
    background: var(--accent-light);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.profile-card h2 {
    font-size: 1.2rem;
    font-weight: 700;
}

.text-muted {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.settings-group {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 16px;
}

.settings-title {
    font-size: 0.85rem;
    font-weight: 700;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 10px;
}

.setting-select {
    width: 100%;
    padding: 10px 14px;
    border: 2px solid var(--border);
    border-radius: 10px;
    background: var(--input-bg);
    color: var(--text);
    font-size: 0.9rem;
    outline: none;
}

.setting-select:focus { border-color: var(--accent); }

.sub-upsell-text {
    font-size: 0.82rem;
    color: var(--text-secondary);
    margin-bottom: 10px;
    line-height: 1.5;
}

.sub-upsell-text strong { color: var(--text); }

.btn-bundle-gradient {
    background: linear-gradient(90deg, #EE9547, #6DC82A, #57B3FE);
}

/* Subscription */
.sub-card {
    padding: 14px;
    border-radius: 12px;
    margin-bottom: 10px;
}

.sub-free { background: var(--bg-secondary); }
.sub-subject { background: linear-gradient(135deg, #FFF3E8, #EBF5FF); }
.sub-bundle { background: linear-gradient(135deg, #FFF3E8, #EBF5FF, #EDFAE0); }

.sub-plan {
    font-weight: 700;
    font-size: 0.95rem;
}

.sub-detail {
    font-size: 0.82rem;
    color: var(--text-secondary);
    margin-top: 2px;
}

.usage-bar {
    height: 6px;
    background: var(--border);
    border-radius: 3px;
    margin-top: 10px;
    overflow: hidden;
}

.usage-fill {
    height: 100%;
    background: var(--accent);
    border-radius: 3px;
    transition: width 0.3s;
}

.sub-usage span {
    font-size: 0.78rem;
    color: var(--text-secondary);
    display: block;
    margin-top: 4px;
}

.subject-subs-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 10px;
}

.subject-sub-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 10px 12px;
    border: 1.5px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
}

.subject-sub-row .ssr-name {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    font-size: 0.88rem;
}

.subject-sub-row .ssr-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}

.subject-sub-row .ssr-btn {
    padding: 7px 12px;
    border: none;
    border-radius: 8px;
    background: var(--accent);
    color: white;
    font-weight: 600;
    font-size: 0.8rem;
    cursor: pointer;
    white-space: nowrap;
}

.subject-sub-row .ssr-badge {
    padding: 5px 10px;
    border-radius: 8px;
    background: #EDFAE0;
    color: #15803d;
    font-weight: 700;
    font-size: 0.78rem;
    white-space: nowrap;
}

/* Stats */
.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.stat-card {
    text-align: center;
    padding: 12px 8px;
    background: var(--bg-secondary);
    border-radius: 10px;
}

.stat-value {
    font-size: 1.4rem;
    font-weight: 800;
    color: var(--accent);
}

.stat-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-top: 2px;
}

.stat-value-sub {
    font-size: 1rem;
    margin-top: 8px;
    opacity: 0.85;
}

.btn-danger {
    width: 100%;
    padding: 14px;
    border: 2px solid #FCA5A5;
    border-radius: 12px;
    background: #FEF2F2;
    color: #DC2626;
    font-weight: 700;
    font-size: 0.95rem;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-danger:hover { background: #FEE2E2; }

/* ========== BOTTOM NAV ========== */
#bottom-nav {
    display: flex;
    border-top: 1px solid var(--border);
    background: var(--nav-bg);
    box-shadow: var(--shadow);
    padding: 4px 0;
    padding-bottom: env(safe-area-inset-bottom, 4px);
    z-index: 10;
}

.nav-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 6px 0;
    border: none;
    background: none;
    color: var(--text-secondary);
    font-size: 0.68rem;
    font-weight: 600;
    cursor: pointer;
    transition: color 0.2s;
    position: relative;
}

.nav-btn.active { color: var(--accent); }

.nav-btn.active::after {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 20px;
    height: 3px;
    background: var(--accent);
    border-radius: 0 0 3px 3px;
}

/* ========== LOADING OVERLAY ========== */
#loading-overlay {
    position: fixed;
    inset: 0;
    background: var(--bg);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

.loader-logo {
    animation: pulse 1.5s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.5; transform: scale(0.9); }
}

/* ========== TOAST ========== */
.toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    padding: 10px 20px;
    background: #1a1a2e;
    color: white;
    border-radius: 12px;
    font-size: 0.85rem;
    font-weight: 600;
    z-index: 200;
    transition: transform 0.3s;
    white-space: nowrap;
}

.toast.show { transform: translateX(-50%) translateY(0); }

/* ========== QUOTA MODAL ========== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50;
    padding: 20px;
    animation: fadeIn 0.2s;
}

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

.modal {
    background: var(--surface);
    border-radius: 20px;
    padding: 24px;
    max-width: 340px;
    width: 100%;
    text-align: center;
    animation: slideUp 0.3s;
}

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

.modal h3 {
    font-size: 1.1rem;
    margin-bottom: 8px;
}

.modal p {
    color: var(--text-secondary);
    font-size: 0.88rem;
    margin-bottom: 16px;
}

.modal-actions {
    display: flex;
    gap: 8px;
}

.modal-actions button {
    flex: 1;
    padding: 10px;
    border-radius: 10px;
    font-weight: 600;
    font-size: 0.88rem;
    cursor: pointer;
}

.modal-actions .btn-secondary {
    border: 2px solid var(--border);
    background: var(--surface);
    color: var(--text);
}

/* ========== SCROLLBAR ========== */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ========== RESPONSIVE (tablette & PC) ==========
   < 768px  : mobile — inchangé (nav du bas, pleine largeur).
   >= 768px : nav verticale à gauche (grille), contenu recentré dans une
              colonne de lecture confortable au lieu d'étirer sur toute
              la largeur.
   >= 1024px: en plus, la sidebar de conversations du chat reste ancrée
              en permanence à côté de la conversation (plus besoin de
              l'ouvrir/fermer), comme sur les apps de bureau. */
@media (min-width: 768px) {
    #auth-screen {
        background: linear-gradient(135deg, #FFF8F0 0%, #F0F8FF 50%, #F0FFF0 100%);
    }

    .auth-container {
        background: white;
        padding: 40px;
        border-radius: 24px;
        box-shadow: 0 20px 60px rgba(0,0,0,0.08);
    }

    #app {
        display: grid;
        grid-template-columns: 88px 1fr;
        grid-template-rows: 56px 1fr;
        grid-template-areas:
            "nav topbar"
            "nav main";
        height: 100dvh;
        max-width: none;
        margin: 0;
        border: none;
    }

    #topbar { grid-area: topbar; }

    #main {
        grid-area: main;
        overflow-y: auto;
    }

    /* La nav du bas devient un rail vertical à gauche, sans changer le HTML
       (mêmes boutons .nav-btn) grâce aux grid-template-areas ci-dessus. */
    #bottom-nav {
        grid-area: nav;
        flex-direction: column;
        justify-content: flex-start;
        gap: 6px;
        padding: 20px 8px;
        border-top: none;
        border-right: 1px solid var(--border);
    }

    .nav-btn {
        flex: none;
        width: 100%;
        padding: 12px 4px;
        border-radius: 12px;
        font-size: 0.7rem;
    }

    .nav-btn.active { background: var(--accent-light); }
    .nav-btn.active::after { display: none; }

    .message { max-width: 80%; }

    /* Colonnes de lecture recentrées au lieu de s'étirer edge-to-edge. */
    #chat-subject-bar, #welcome-screen, #messages, #input-area, #photo-preview {
        width: 100%;
        max-width: 760px;
        margin-left: auto;
        margin-right: auto;
    }

    #exercises-view, #resolution-view, #profile-view {
        max-width: 640px;
        margin: 0 auto;
        width: 100%;
    }
}

@media (min-width: 1024px) {
    /* La sidebar du chat reste ouverte en permanence à côté de la
       conversation : plus la peine de la déplier/replier sur grand écran. */
    #chat-view { flex-direction: row; }

    #chat-sidebar, #chat-sidebar.open {
        position: relative;
        transform: none;
        flex-shrink: 0;
    }

    #sidebar-overlay { display: none; }
    #sidebar-toggle-btn { display: none !important; }

    #chat-main-col { min-width: 0; }

    #bottom-nav { width: 96px; padding: 24px 10px; }
    .nav-btn { font-size: 0.74rem; }
}
