:root {
  color-scheme: light;
  --bg: #f6f5f2;
  --surface: #fcfcfb;
  --surface-2: #efeee9;
  --border: #dddbd3;
  --text: #1b1b19;
  --text-2: #52514e;
  --muted: #7a7973;
  --accent: #2a5fc1;
  --accent-ink: #ffffff;
  --accent-soft: #e3ecfb;
  --review: #7a4fc4;
  --review-soft: #efe7fb;
  --warn-soft: #fff3d6;
  --warn-ink: #6b4b00;
  --danger: #b3261e;
  --danger-soft: #fde8e6;
  --ok: #1f7a3a;
  --series-1: #2a78d6;
  --bubble-user: #e3ecfb;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 5%);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #242422;
    --border: #34342f;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #94938b;
    --accent: #6d9cf0;
    --accent-ink: #0d1626;
    --accent-soft: #1d2a42;
    --review: #b08cf0;
    --review-soft: #2a2140;
    --warn-soft: #3a2f12;
    --warn-ink: #f3d68a;
    --danger: #f08a82;
    --danger-soft: #3d1c1a;
    --ok: #6fcf8a;
    --series-1: #3987e5;
    --bubble-user: #1d2a42;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.5; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.25rem; } h2 { font-size: 1.1rem; } h3 { font-size: 1rem; }
a { color: var(--accent); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }

button, .btn {
  font: inherit; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); border-radius: 8px; padding: .45rem .8rem; cursor: pointer;
  text-decoration: none; display: inline-flex; align-items: center; gap: .35rem;
}
button:hover, .btn:hover { background: var(--surface-2); }
button:disabled { opacity: .55; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.primary:hover { filter: brightness(1.08); }
button.review { border-color: var(--review); color: var(--review); }
button.ghost { border-color: transparent; background: transparent; }
button.danger { color: var(--danger); border-color: var(--danger); }
button.toggle[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: .45rem .6rem; width: 100%;
}
textarea { resize: vertical; }
label { display: block; font-size: .9rem; color: var(--text-2); margin: .6rem 0 .2rem; }
label.inline { display: inline-flex; align-items: center; gap: .4rem; margin: 0; color: var(--text); }
label.inline input { width: auto; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; box-shadow: var(--shadow); }
.banner { padding: .6rem .9rem; border-radius: 8px; background: var(--warn-soft); color: var(--warn-ink); }
.banner.error { background: var(--danger-soft); color: var(--danger); }
.badge { display: inline-block; font-size: .75rem; padding: .1rem .5rem; border-radius: 99px; background: var(--surface-2); color: var(--text-2); white-space: nowrap; }
.badge.review { background: var(--review-soft); color: var(--review); }
.badge.flag { background: var(--danger-soft); color: var(--danger); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }

/* --- Connexion --- */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-wrap .card { width: min(380px, 100%); }
.login-wrap h1 { font-size: 1.5rem; }

/* --- En-tête --- */
.topbar { display: flex; align-items: center; gap: .75rem; padding: .6rem 1rem; background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar .brand { font-weight: 700; }
.topbar .who { color: var(--text-2); }

/* --- Espace élève --- */
.app { display: grid; grid-template-rows: auto 1fr; grid-template-columns: minmax(0, 1fr); height: 100%; }
.layout { display: grid; grid-template-columns: 280px 1fr; min-height: 0; }
.sidebar { border-right: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
.sidebar .actions { padding: .75rem; display: grid; gap: .5rem; }
.conv-list { list-style: none; margin: 0; padding: 0 .5rem .75rem; overflow-y: auto; flex: 1; }
.conv-list li button { width: 100%; text-align: left; display: block; border: 0; background: transparent; padding: .55rem .6rem; border-radius: 8px; }
.conv-list li button:hover { background: var(--surface-2); }
.conv-list li button[aria-current="true"] { background: var(--accent-soft); }
.conv-list .t { display: block; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-list .m { font-size: .78rem; color: var(--muted); }

.main { display: grid; grid-template-rows: auto 1fr auto; min-height: 0; min-width: 0; }
.conv-head { padding: .6rem 1rem; border-bottom: 1px solid var(--border); display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; background: var(--surface); }
.conv-head .title { font-weight: 600; flex: 1; min-width: 8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-head select { width: auto; }
.docs { display: flex; gap: .4rem; flex-wrap: wrap; padding: 0 1rem; }
.docs:not(:empty) { padding: .5rem 1rem; }
.doc-chip { font-size: .8rem; background: var(--surface-2); border-radius: 99px; padding: .15rem .6rem; }

.messages { overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: .8rem; }
.msg { max-width: min(760px, 92%); padding: .7rem .9rem; border-radius: 12px; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--bubble-user); border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.assistant.review { border-left: 3px solid var(--review); }
.msg .meta { font-size: .75rem; color: var(--muted); margin-bottom: .25rem; display: flex; gap: .4rem; align-items: center; }
.msg p { margin: .4em 0; } .msg p:first-child { margin-top: 0; } .msg p:last-child { margin-bottom: 0; }
.msg ul, .msg ol { margin: .4em 0; padding-left: 1.3em; }
.msg pre { background: var(--surface-2); padding: .6rem; border-radius: 6px; overflow-x: auto; }
.msg code { background: var(--surface-2); padding: 0 .25em; border-radius: 4px; font-size: .92em; }
.msg pre code { background: none; padding: 0; }
.msg blockquote { margin: .4em 0; padding-left: .8em; border-left: 3px solid var(--border); color: var(--text-2); }
.msg .sources { margin-top: .5rem; font-size: .82rem; }
.msg .sources a { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status-line { font-size: .85rem; color: var(--muted); font-style: italic; }
.typing::after { content: "▍"; animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }

.welcome { margin: auto; max-width: 560px; text-align: center; color: var(--text-2); }
.welcome ul { text-align: left; }

/* minmax(0,1fr) : empêche le contenu d'élargir la grille (débordement mobile) */
.composer { grid-template-columns: minmax(0, 1fr); border-top: 1px solid var(--border); padding: .6rem 1rem 1rem; background: var(--surface); display: grid; gap: .5rem; }
.composer textarea { min-height: 3rem; max-height: 14rem; }
.composer .tools { display: flex; gap: .4rem; flex-wrap: wrap; align-items: center; }

dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 1.2rem; width: min(640px, calc(100% - 32px)); }
dialog::backdrop { background: rgb(0 0 0 / 40%); }
dialog .row { justify-content: flex-end; margin-top: 1rem; }

/* --- Espace parent --- */
.tabs { display: flex; gap: .25rem; padding: 0 1rem; background: var(--surface); border-bottom: 1px solid var(--border); overflow-x: auto; }
.tabs button { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: transparent; padding: .7rem .8rem; white-space: nowrap; }
.tabs button[aria-selected="true"] { border-bottom-color: var(--accent); color: var(--accent); font-weight: 600; }
.page { padding: 1rem; max-width: 1200px; margin: 0 auto; width: 100%; }
.admin-app { min-height: 100%; display: flex; flex-direction: column; }
.grid { display: grid; gap: 1rem; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(min(100%, 460px), 1fr)); }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: .6rem; margin: .6rem 0 1rem; }
.tile { background: var(--surface-2); border-radius: 8px; padding: .6rem .7rem; }
.tile .v { font-size: 1.5rem; font-weight: 650; font-variant-numeric: tabular-nums; }
.tile .l { font-size: .78rem; color: var(--text-2); }

.chart { position: relative; }
.chart .bars { display: flex; align-items: flex-end; gap: 2px; height: 90px; border-bottom: 1px solid var(--border); }
.chart .bar-hit { flex: 1; height: 100%; display: flex; align-items: flex-end; cursor: default; }
.chart .bar { width: 100%; background: var(--series-1); border-radius: 4px 4px 0 0; min-height: 0; }
.chart .bar-hit:hover .bar, .chart .bar-hit:focus .bar { filter: brightness(1.15); }
.chart .axis { display: flex; justify-content: space-between; font-size: .72rem; color: var(--muted); margin-top: .2rem; }
.chart .tip { position: absolute; pointer-events: none; background: var(--text); color: var(--bg); font-size: .78rem; padding: .25rem .5rem; border-radius: 6px; white-space: nowrap; transform: translate(-50%, -110%); }

.hbar { display: grid; grid-template-columns: minmax(6rem, 10rem) 1fr 2.5rem; gap: .5rem; align-items: center; font-size: .88rem; margin: .2rem 0; }
.hbar .track { height: 10px; }
.hbar .fill { height: 100%; background: var(--series-1); border-radius: 0 4px 4px 0; }
.hbar .n { text-align: right; font-variant-numeric: tabular-nums; color: var(--text-2); }

table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { text-align: left; padding: .45rem .5rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--text-2); font-weight: 600; font-size: .82rem; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr.clickable { cursor: pointer; } tr.clickable:hover { background: var(--surface-2); }
.table-wrap { overflow-x: auto; }

.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .5rem; align-items: end; margin-bottom: 1rem; }
.filters label { margin-top: 0; }

.viewer .msg { max-width: 100%; }
.viewer .messages { padding: 0; overflow: visible; }
.schedule-grid { display: grid; grid-template-columns: 6.5rem 1fr; gap: .3rem .6rem; align-items: center; }
.schedule-grid span { font-size: .9rem; }
pre.prompt { white-space: pre-wrap; background: var(--surface-2); padding: .8rem; border-radius: 8px; max-height: 60vh; overflow: auto; font-size: .85rem; }
textarea.code { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: .85rem; }
details summary { cursor: pointer; }

@media (max-width: 760px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .main { grid-template-columns: minmax(0, 1fr); }
  .topbar { gap: .4rem; padding: .5rem 16px; }
  .topbar .who { display: none; }
  .topbar .badge { font-size: .7rem; flex: 0 1 auto; }
  .topbar button { padding-left: .5rem; padding-right: .5rem; }
  .composer { padding: .5rem 16px .75rem; }
  .composer .tools button { padding: .4rem .55rem; font-size: .88rem; }
  .conv-head, .messages { padding-left: 16px; padding-right: 16px; }
  .sidebar { display: none; }
  .sidebar.open { display: flex; position: fixed; inset: 48px 0 0 0; z-index: 10; }
  .msg { max-width: 100%; }
  .page { padding: 16px; }
}
@media (min-width: 761px) { .only-mobile { display: none !important; } }
button.block { width: 100%; justify-content: center; }
dialog.viewer { width: min(920px, calc(100% - 32px)); max-height: 90vh; }
dialog.viewer .card { margin-bottom: .6rem; }
h4 { margin: .6rem 0 .2rem; font-size: .9rem; }

.card h3 { margin-top: 1rem; }
.card details + h3, .card .chart + details { margin-top: .4rem; }
.topbar button { white-space: nowrap; }
#new-student-box { margin-top: 1rem; }

.topbar .brand { display: inline-flex; align-items: center; gap: .4rem; white-space: nowrap; flex: none; }
/* Sur petit écran, le compteur de messages rétrécit (texte complet au survol) plutôt que de pousser la page */
.topbar .badge { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
img.logo { width: 28px; height: 28px; }
img.logo-big { width: 72px; height: 72px; display: block; margin: 0 auto .5rem; }
.msg img.avatar { width: 18px; height: 18px; }
