:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --panel: #f7f7f5;
  --raised: #ffffff;
  --border: #e3e3df;
  --text: #16161a;
  --muted: #76767f;
  --accent: #16161a;
  --accent-text: #ffffff;
  --danger: #b4291f;
  --ok: #2f7d4f;
  --warn: #9a6a12;
  --radius: 12px;
  --radius-sm: 8px;
  --rail: 360px;
  /* Hauteur commune aux en-têtes des deux colonnes : sans ça leurs bordures
     basses ne tombent pas au même pixel, les titres n'ayant pas la même taille. */
  --head-h: 55px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131316;
    --panel: #1a1a1e;
    --raised: #212127;
    --border: #2f2f38;
    --text: #ececf1;
    --muted: #9a9aa6;
    --accent: #ececf1;
    --accent-text: #131316;
    --danger: #f0776c;
    --ok: #62c78d;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 ui-sans-serif, -apple-system, "Segoe UI", system-ui, sans-serif;
  overflow: hidden;
}

h1 { font-size: 16px; margin: 0; font-weight: 600; }
h2 { margin: 0; font-size: 15px; font-weight: 600; }

.spacer { flex: 1; }
.muted { color: var(--muted); font-weight: 400; font-size: 12px; }

.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--rail);
  height: 100dvh;
  overflow: hidden;
}
.app[data-rails="hidden"] { grid-template-columns: minmax(0, 1fr); }
.app[data-rails="hidden"] .rail { display: none; }

/* ───────────────────────── mosaic gallery ───────────────────────── */

.rail {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-width: 0; min-height: 0;
  background: var(--panel);
  border-left: 1px solid var(--border);
  overflow: hidden;
}

.rail__head {
  display: flex; align-items: center; justify-content: space-between;
  height: var(--head-h); padding: 0 16px;
  border-bottom: 1px solid var(--border);
}

.rail__scroll {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 10px;
}

.empty--rail { margin: 40px 0; font-size: 13px; }

/* A true mosaic: CSS columns let each tile keep its own aspect ratio and the
   next tile packs straight underneath, so mixed square/16:9 output stays tight. */
.mosaic { column-count: 2; column-gap: 8px; }

.tile {
  display: block;
  width: 100%;
  padding: 0;
  margin: 0 0 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: #fff;
  cursor: zoom-in;
  break-inside: avoid;
  position: relative;
  line-height: 0;
}
.tile img { width: 100%; height: auto; display: block; }
.tile:hover { border-color: var(--muted); }
.tile[aria-current="true"] { outline: 2px solid var(--accent); outline-offset: 1px; }

.tile__cap {
  position: absolute; inset: auto 0 0 0;
  padding: 14px 8px 5px;
  font-size: 11px; line-height: 1.3; text-align: left;
  color: #fff;
  background: linear-gradient(transparent, rgba(0,0,0,.72));
  opacity: 0; transition: opacity .15s;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.tile:hover .tile__cap, .tile:focus-visible .tile__cap { opacity: 1; }

.sentinel { height: 1px; }

.rail__end { text-align: center; font-size: 11px; color: var(--muted); padding: 6px 0 14px; }

/* ───────────────────────── conversation ───────────────────────── */

.chat { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; min-width: 0; min-height: 0; border-right: 0; }

.chat__head {
  display: flex; align-items: center; gap: 10px;
  height: var(--head-h); padding: 0 20px;
  border-bottom: 1px solid var(--border);
}

.thread {
  overflow-y: auto;
  padding: 24px 20px 8px;
  display: flex; flex-direction: column; gap: 22px;
}

.empty { color: var(--muted); text-align: center; margin: auto; font-size: 14px; }
.empty__eg { font-size: 13px; opacity: .7; font-style: italic; }

.turn { max-width: 720px; width: 100%; margin: 0 auto; display: flex; flex-direction: column; gap: 10px; }

.bubble {
  align-self: flex-end; max-width: 82%;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); border-bottom-right-radius: 4px;
  padding: 10px 14px;
}
.bubble__title { font-weight: 600; font-size: 13px; margin-bottom: 2px; }
.bubble__subject { font-size: 14px; }

.reply { display: flex; flex-direction: column; gap: 8px; }
.reply__status { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.reply__status[data-s="error"] { color: var(--danger); }
.reply__status[data-s="done"] { color: var(--ok); }

.dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; flex: none; }
.dot--pulse { animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: .25; } 50% { opacity: 1; } }

.bar { height: 3px; border-radius: 2px; background: var(--border); overflow: hidden; width: 150px; }
.bar > i { display: block; height: 100%; background: var(--ok); transition: width .3s; }

.shots { display: flex; flex-wrap: wrap; gap: 8px; }
.shot {
  width: 132px; height: 132px; padding: 0;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  overflow: hidden; background: #fff; cursor: zoom-in;
}
.shot img { width: 100%; height: 100%; object-fit: contain; display: block; }

.skeleton {
  width: 132px; height: 132px; border-radius: var(--radius-sm);
  background: linear-gradient(100deg, var(--panel) 30%, var(--border) 50%, var(--panel) 70%);
  background-size: 300% 100%;
  animation: shimmer 1.4s linear infinite;
}
@keyframes shimmer { from { background-position: 150% 0; } to { background-position: -150% 0; } }

.turn__err { color: var(--danger); font-size: 12px; word-break: break-word; }
.turn__actions { display: flex; gap: 12px; }

/* ───────────────────────── composer ───────────────────────── */

.composer {
  border-top: 1px solid var(--border);
  padding: 12px 20px 16px;
  display: flex; flex-direction: column; gap: 8px;
}
.composer > * { max-width: 720px; width: 100%; margin: 0 auto; }

.composer__controls { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }

.pill {
  font: inherit; font-size: 12px; color: var(--text);
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 999px; padding: 5px 10px; cursor: pointer;
}
.pill--input { cursor: text; width: 160px; }
.pill--check { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }
.pill--check input { margin: 0; cursor: pointer; }
.pill--check:has(input:checked) { color: var(--text); border-color: var(--muted); }

.composer__row {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--raised); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 8px 8px 8px 14px;
}
.composer__row:focus-within { border-color: var(--muted); }
.composer__row textarea {
  flex: 1; border: 0; background: none; color: inherit; font: inherit;
  resize: none; padding: 5px 0; max-height: 180px; outline: none;
}

.send {
  flex: none; width: 32px; height: 32px;
  display: grid; place-items: center;
  border: 0; border-radius: 50%;
  background: var(--accent); color: var(--accent-text); cursor: pointer;
}
.send:disabled { opacity: .35; cursor: default; }

.composer__preview { font-size: 12px; color: var(--muted); }
.composer__preview summary { cursor: pointer; }
.composer__preview p { margin: 6px 0 0; line-height: 1.5; }

.error { color: var(--danger); font-size: 12px; margin: 0; }

/* ───────────────────────── shared controls ───────────────────────── */

.button {
  flex: 1; font: inherit; font-size: 13px; font-weight: 500;
  text-align: center; text-decoration: none;
  padding: 9px 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--raised); color: var(--text);
  cursor: pointer;
}
.button:hover { border-color: var(--muted); }
.button--danger:hover { color: var(--danger); border-color: var(--danger); }

.ghost {
  font: inherit; font-size: 12px; background: none;
  border: 1px solid var(--border); border-radius: 999px;
  padding: 4px 10px; color: var(--muted); cursor: pointer;
  text-decoration: none;
}
.ghost:hover { color: var(--text); }

.link { background: none; border: 0; padding: 0; font: inherit; font-size: 12px; color: var(--muted); cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }
.link:hover { color: var(--text); }
.link--danger:hover { color: var(--danger); }

/* Point d'état, fixe en bas à droite. Au repos il n'y a que la pastille ;
   le libellé n'apparaît qu'au survol, pour ne rien prendre à l'interface. */
.health {
  position: fixed; right: 12px; bottom: 10px; z-index: 40;
  display: inline-flex; align-items: center; justify-content: center;
  width: 22px; height: 22px; padding: 0; gap: 0;
  font: inherit; font-size: 11px; line-height: 1;
  background: var(--panel); border: 1px solid var(--border); border-radius: 999px;
  color: var(--muted); cursor: pointer;
  transition: width .15s, padding .15s, gap .15s;
}
.health__label {
  max-width: 0; overflow: hidden; white-space: nowrap;
  opacity: 0; transition: max-width .15s, opacity .15s;
}
.health:hover, .health:focus-visible { width: auto; gap: 6px; padding: 0 11px 0 8px; }
.health:hover .health__label, .health:focus-visible .health__label { max-width: 140px; opacity: 1; }
.health__dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.health[data-state="online"]  { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, var(--border)); }
.health[data-state="online"] .health__dot { background: var(--ok); }
.health[data-state="offline"] { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.health[data-state="offline"] .health__dot { background: var(--danger); }
.health[data-state="unknown"] .health__dot { animation: pulse 1.1s ease-in-out infinite; }

.offline--full { color: var(--danger); }
.offline {
  margin: 0; font-size: 12px; color: var(--warn, #9a6a12);
  background: color-mix(in srgb, currentColor 10%, transparent);
  border: 1px solid color-mix(in srgb, currentColor 30%, transparent);
  border-radius: var(--radius-sm); padding: 7px 11px;
}

.badge--warn { color: var(--warn); }
.badge--full { color: var(--danger); }
.badge { font-size: 11px; padding: 2px 9px; border-radius: 999px; background: var(--border); color: var(--muted); }

/* ───────────────────────── viewer ───────────────────────── */

.viewer {
  border: 0; padding: 0; background: none;
  max-width: 100vw; max-height: 100vh;
  width: 100vw; height: 100dvh;
  display: none;
  align-items: center; justify-content: center;
  gap: 8px;
}
.viewer[open] { display: flex; }
.viewer::backdrop { background: rgba(0,0,0,.72); backdrop-filter: blur(3px); }

.viewer__panel {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 330px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  max-width: min(1200px, calc(100vw - 110px));
  max-height: calc(100dvh - 40px);
}

.viewer__stage {
  display: grid; place-items: center;
  background: #fff; min-width: 0; min-height: 0;
  padding: 10px;
}
.viewer__stage img { max-width: 100%; max-height: calc(100dvh - 60px); object-fit: contain; display: block; }

.viewer__info {
  border-left: 1px solid var(--border);
  padding: 16px;
  display: flex; flex-direction: column; gap: 10px;
  overflow-y: auto; min-width: 0;
}
.viewer__head { display: flex; align-items: flex-start; gap: 10px; }
.viewer__head h2 { flex: 1; word-break: break-word; }
.viewer__subject { margin: 0; font-size: 13px; color: var(--muted); }

.viewer__meta { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 16px; margin: 2px 0; }
.viewer__meta div { display: flex; flex-direction: column; min-width: 0; }
.viewer__meta dt { font-size: 10px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.viewer__meta dd { margin: 0; font-size: 12px; font-variant-numeric: tabular-nums; overflow: hidden; text-overflow: ellipsis; }

/* Takes whatever height is left over, so the prompt scrolls inside the column
   instead of being clipped above a gap. */
.viewer__prompt { display: flex; flex-direction: column; gap: 4px; flex: 1; min-height: 0; }
.viewer__prompt-head { display: flex; justify-content: space-between; align-items: center; font-size: 10px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.viewer__prompt p { margin: 0; font-size: 12px; line-height: 1.5; color: var(--muted); flex: 1; min-height: 0; overflow-y: auto; }

.viewer__actions { display: flex; gap: 8px; padding-top: 4px; }

.nav {
  flex: none; width: 42px; height: 72px;
  border: 1px solid rgba(255,255,255,.22); border-radius: var(--radius-sm);
  background: rgba(0,0,0,.35); color: #fff;
  font-size: 26px; line-height: 1; cursor: pointer;
}
.nav:hover:not(:disabled) { background: rgba(0,0,0,.6); }
.nav:disabled { opacity: .25; cursor: default; }

/* ───────────────────────── narrow ───────────────────────── */

@media (max-width: 1240px) { :root { --rail: 290px; } }

/* Téléphone : on ne garde que ce qui sert à générer. Tout le reste
   (modèle, titre, doc, galerie, usage, aperçu du prompt) disparaît. */
@media (max-width: 700px) {
  .desktop-only { display: none !important; }

  .chat__head { padding: 0 14px; gap: 8px; }
  h1 { font-size: 15px; }

  .composer { padding: 10px 12px 12px; gap: 6px; }
  .composer__controls { gap: 5px; }
  .pill { font-size: 12px; padding: 5px 9px; }
  .composer__row { padding: 6px 6px 6px 12px; }

  .thread { padding: 16px 14px 4px; }
  .bubble { max-width: 90%; }
  .shot, .skeleton { width: 104px; height: 104px; }

  /* Le composer occupe tout le bas de l'écran sur téléphone : un point fixe en
     bas à droite recouvre le bouton d'envoi et le rend à moitié intouchable.
     Il remonte donc dans l'en-tête, qui lui est vide à cette largeur. */
  .health { top: 16px; bottom: auto; right: 14px; }
}

@media (max-width: 1000px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .rail { display: none; }
  .app[data-rails="shown"] .chat { display: none; }
  .app[data-rails="shown"] .rail { display: grid; border-left: 0; }
  .app[data-rails="shown"] .mosaic { column-count: 3; }
  .viewer__panel { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; }
  .viewer__info { border-left: 0; border-top: 1px solid var(--border); max-height: 42dvh; }
  .nav { width: 32px; }
}
