/**
 * Console Claude — feuille unique.
 *
 * Parti pris : une transcription, pas une messagerie. Pas de bulles ni
 * d'avatars — un filet vertical, un rôle en petites capitales dans la gouttière,
 * le texte en pleine largeur de colonne. C'est la forme d'un procès-verbal, qui
 * est bien ce que l'écran contient : la trace exacte de ce qui a été envoyé et
 * reçu. Les bulles suggèrent une conversation qui passe ; ici elle reste.
 *
 * Base de départ à ajuster : les couleurs et le rythme sont posés en variables
 * pour être repris d'un seul endroit.
 */

:root {
    color-scheme: light;

    --ink:        #16202b;
    --ink-soft:   #55636f;
    --ink-faint:  #8c98a3;
    --paper:      #fbfaf7;
    --panel:      #f2efe9;
    --rule:       #ddd8cf;
    --accent:     #2f6f6a;
    --accent-ink: #1d4a46;
    --danger:     #9a3b32;

    /* Voiles : un survol, un fond de cellule. Posés en variables parce qu'un
       noir translucide devient invisible sur fond sombre — il faut du blanc. */
    --wash:        rgba(0, 0, 0, 0.04);
    --wash-strong: rgba(0, 0, 0, 0.05);
    --accent-wash: rgba(47, 111, 106, 0.07);
    --danger-wash: rgba(154, 59, 50, 0.06);
    --on-accent:   #fff;

    /* Les blocs de code restent sombres DANS LES DEUX THÈMES : c'est la
       convention d'un terminal, et ça les détache du texte courant. En thème
       sombre ils descendent sous la couleur de page pour rester lisibles. */
    --code-bg:  #16202b;
    --code-ink: #e6e4e0;

    --sans: ui-sans-serif, system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;

    --gutter: 5.5rem;
    --radius: 3px;
}

/* ===========================================================================
   Thème sombre
   ---------------------------------------------------------------------------
   Deux points d'entrée pour une seule palette :
   - [data-theme="dark"], choix explicite mémorisé ;
   - prefers-color-scheme, si aucun choix n'a été fait.
   Le second est encadré par :not([data-theme]) pour que le choix explicite
   l'emporte : sans quoi, choisir « clair » sur un système sombre serait
   silencieusement annulé.

   Fond très sombre plutôt que gris moyen : sur écran OLED, un pixel noir est
   un pixel éteint. C'est ce qui économise réellement la batterie.
   =========================================================================== */

[data-theme="dark"] {
    color-scheme: dark;

    --ink:        #e9e5de;
    --ink-soft:   #a49d93;
    --ink-faint:  #6d675f;
    --paper:      #100f0e;
    --panel:      #191714;
    --rule:       #2f2b26;
    --accent:     #5fb3aa;
    --accent-ink: #8ad0c8;
    --danger:     #d9796a;

    --wash:        rgba(255, 255, 255, 0.045);
    --wash-strong: rgba(255, 255, 255, 0.06);
    --accent-wash: rgba(95, 179, 170, 0.10);
    --danger-wash: rgba(217, 121, 106, 0.10);
    --on-accent:   #08100f;

    --code-bg:  #060605;
    --code-ink: #ddd9d2;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme]) {
        color-scheme: dark;

        --ink:        #e9e5de;
        --ink-soft:   #a49d93;
        --ink-faint:  #6d675f;
        --paper:      #100f0e;
        --panel:      #191714;
        --rule:       #2f2b26;
        --accent:     #5fb3aa;
        --accent-ink: #8ad0c8;
        --danger:     #d9796a;

        --wash:        rgba(255, 255, 255, 0.045);
        --wash-strong: rgba(255, 255, 255, 0.06);
        --accent-wash: rgba(95, 179, 170, 0.10);
        --danger-wash: rgba(217, 121, 106, 0.10);
        --on-accent:   #08100f;

        --code-bg:  #060605;
        --code-ink: #ddd9d2;
    }
}

* { box-sizing: border-box; }

body.app {
    margin: 0;
    background: var(--paper);
    color: var(--ink);
    font: 400 16px/1.6 var(--sans);
    -webkit-font-smoothing: antialiased;
}

.sr-only {
    position: absolute; width: 1px; height: 1px;
    padding: 0; margin: -1px; overflow: hidden;
    clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.is-hidden { display: none !important; }

/* ---------- Ossature ---------- */

.shell { display: flex; min-height: 100vh; }

.sidebar {
    display: flex;
    flex-direction: column;
    width: 17rem;
    flex: 0 0 17rem;
    background: var(--panel);
    border-right: 1px solid var(--rule);
}

.main {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-width: 0;
    max-width: 54rem;
    padding: 2rem 2.5rem 1.5rem;
}

/* ---------- Barre latérale ---------- */

.sidebar-head { padding: 1.5rem 1.25rem 1rem; }

.brand {
    display: block;
    font: 600 0.75rem/1 var(--mono);
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--ink-soft);
    text-decoration: none;
    margin-bottom: 1rem;
}

.conv-list { flex: 1 1 auto; overflow-y: auto; padding: 0 0.75rem; }

.conv-empty {
    padding: 0 0.5rem;
    color: var(--ink-faint);
    font-size: 0.875rem;
}

.conv-item {
    display: block;
    padding: 0.55rem 0.65rem;
    margin-bottom: 0.15rem;
    border-radius: var(--radius);
    text-decoration: none;
    color: var(--ink);
}

.conv-item:hover { background: var(--wash); }

.conv-item.is-active {
    background: var(--paper);
    box-shadow: inset 2px 0 0 var(--accent);
}

.conv-title {
    display: block;
    font-size: 0.9rem;
    line-height: 1.35;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.conv-meta {
    display: block;
    margin-top: 0.15rem;
    font: 400 0.7rem/1 var(--mono);
    color: var(--ink-faint);
}

.sidebar-foot {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 0.5rem;
    padding: 1rem 1.25rem;
    border-top: 1px solid var(--rule);
    font-size: 0.8rem;
}

.user-name { color: var(--ink-soft); }
.logout { color: var(--ink-faint); text-decoration: none; }
.logout:hover { color: var(--danger); text-decoration: underline; }

/* ---------- En-tête de conversation ---------- */

.conv-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--rule);
}

.conv-heading { margin: 0; font-size: 1.25rem; font-weight: 600; }
.conv-actions { display: flex; gap: 0.4rem; flex: 0 0 auto; }

/* ---------- Réglages ---------- */

.settings {
    display: grid;
    grid-template-columns: auto 1fr;
    align-items: start;
    gap: 0.5rem 0.75rem;
    padding: 0.9rem 0;
    border-bottom: 1px solid var(--rule);
}

.settings label {
    font: 400 0.7rem/1.8 var(--mono);
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--ink-faint);
}

.settings select,
.settings textarea {
    width: 100%;
    font: inherit;
    font-size: 0.875rem;
    color: var(--ink);
    background: var(--paper);
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    padding: 0.35rem 0.5rem;
}

.settings textarea { resize: vertical; }

/* ---------- Transcription ---------- */

.transcript { flex: 1 1 auto; padding: 1.5rem 0; overflow-y: auto; }

.msg {
    position: relative;
    padding: 0 0 1.75rem var(--gutter);
}

/* Filet continu de gouttière : c'est lui qui tient la colonne ensemble. */
.msg::before {
    content: "";
    position: absolute;
    left: calc(var(--gutter) - 1.75rem);
    top: 0.45rem;
    bottom: 0;
    border-left: 1px solid var(--rule);
}

.msg:last-child::before { bottom: 1rem; }

.msg-role {
    position: absolute;
    left: 0;
    top: 0.15rem;
    width: calc(var(--gutter) - 2.5rem);
    text-align: right;
    font: 500 0.7rem/1.6 var(--mono);
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--ink-faint);
}

.msg-assistant .msg-role { color: var(--accent); }

.msg-body { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-user .msg-body { color: var(--ink-soft); }

/* Curseur de frappe pendant le flux. */
.msg-body.is-streaming::after {
    content: "";
    display: inline-block;
    width: 0.5em;
    height: 1em;
    margin-left: 0.1em;
    vertical-align: text-bottom;
    background: var(--accent);
    animation: blink 1s step-end infinite;
}

@keyframes blink { 50% { opacity: 0; } }

/* ---------- Zone de saisie ---------- */

.composer {
    flex: 0 0 auto;
    padding-top: 1rem;
    border-top: 1px solid var(--rule);
}

.composer textarea {
    width: 100%;
    font: inherit;
    color: var(--ink);
    background: var(--paper);
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    padding: 0.65rem 0.75rem;
    resize: vertical;
}

.composer textarea:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

.composer-foot {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: 0.6rem;
}

.composer-status {
    flex: 1 1 auto;
    font: 400 0.75rem/1.4 var(--mono);
    color: var(--ink-faint);
}

.composer-status.is-error { color: var(--danger); }

/* ---------- Boutons ---------- */

.btn {
    font: inherit;
    font-size: 0.875rem;
    padding: 0.4rem 0.85rem;
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    background: var(--paper);
    color: var(--ink);
    cursor: pointer;
    text-decoration: none;
    display: inline-block;
}

.btn:hover { border-color: var(--ink-faint); }

.btn-primary {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--on-accent);
}

.btn-primary:hover { background: var(--accent-ink); border-color: var(--accent-ink); }
.btn-primary:disabled { background: var(--ink-faint); border-color: var(--ink-faint); cursor: default; }

.btn-quiet { background: transparent; border-color: transparent; color: var(--ink-soft); }
.btn-quiet:hover { border-color: var(--rule); }
.btn-danger:hover { color: var(--danger); border-color: var(--danger); }

.btn-new { width: 100%; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Pages autonomes (login, 404, écran vide) ---------- */

body.app-plain { display: grid; place-items: center; min-height: 100vh; }

.plain-card, .empty-state {
    max-width: 24rem;
    padding: 2rem;
    text-align: left;
}

.empty-state { margin: auto; }

.eyebrow {
    margin: 0 0 0.35rem;
    font: 500 0.7rem/1 var(--mono);
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--ink-faint);
}

.plain-card h1, .empty-state h1 { margin: 0 0 0.75rem; font-size: 1.4rem; font-weight: 600; }
.plain-card p, .empty-state p { color: var(--ink-soft); font-size: 0.9rem; }

.form { display: grid; gap: 0.35rem; margin-top: 1.25rem; }

.form label {
    font: 400 0.7rem/1.8 var(--mono);
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--ink-faint);
}

.form input {
    font: inherit;
    padding: 0.5rem 0.6rem;
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    background: var(--paper);
    margin-bottom: 0.5rem;
}

.form .btn { margin-top: 0.5rem; }

/* ---------- Messages éphémères ---------- */

.flash {
    padding: 0.55rem 0.75rem;
    border-radius: var(--radius);
    font-size: 0.875rem;
    border: 1px solid var(--rule);
}

.flash-error { color: var(--danger); border-color: var(--danger); background: var(--danger-wash); }

/* ---------- Menu mobile ---------- */

.burger { display: none; }

@media (max-width: 48rem) {
    .burger {
        display: block;
        position: fixed;
        top: 0.75rem; left: 0.75rem;
        z-index: 20;
        width: 2.25rem; height: 2.25rem;
        padding: 0.5rem;
        background: var(--panel);
        border: 1px solid var(--rule);
        border-radius: var(--radius);
        cursor: pointer;
    }

    .burger span {
        display: block;
        height: 1px;
        margin: 0.25rem 0;
        background: var(--ink);
    }

    .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        z-index: 15;
        transform: translateX(-100%);
        transition: transform 0.2s ease;
    }

    .sidebar.is-open { transform: none; }

    .main { padding: 3.5rem 1.25rem 1.25rem; }

    :root { --gutter: 4rem; }
}

@media (prefers-reduced-motion: reduce) {
    * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* ===========================================================================
   Coûts et écran Tarifs
   ---------------------------------------------------------------------------
   Les montants sont en chiffres tabulaires et en gouttière : ils se lisent en
   colonne, se comparent d'un coup d'œil, et ne bougent pas quand la valeur
   change en cours de flux.
   =========================================================================== */

.conv-cost {
    flex: 0 0 auto;
    font: 400 0.8rem/1 var(--mono);
    font-variant-numeric: tabular-nums;
    color: var(--ink-faint);
}

/* --- Pied de la barre latérale ------------------------------------------- */

.sidebar-foot { display: block; }

.spend {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.4rem 0;
    margin-bottom: 0.5rem;
    text-decoration: none;
    border-bottom: 1px solid var(--rule);
}

.spend-label {
    font: 400 0.65rem/1.4 var(--mono);
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--ink-faint);
}

.spend-figure {
    font: 500 0.85rem/1 var(--mono);
    font-variant-numeric: tabular-nums;
    color: var(--accent);
}

.spend:hover .spend-figure { color: var(--accent-ink); }

.sidebar-user {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 0.5rem;
}

/* --- Panneaux ------------------------------------------------------------- */

.panel { padding: 1.25rem 0; border-bottom: 1px solid var(--rule); }
.panel:last-of-type { border-bottom: 0; }

.total-figure {
    margin: 0.2rem 0 0.4rem;
    font: 400 2rem/1 var(--mono);
    font-variant-numeric: tabular-nums;
    color: var(--accent);
}

.panel-note {
    margin: 0 0 1rem;
    font-size: 0.8rem;
    line-height: 1.5;
    color: var(--ink-soft);
    max-width: 42rem;
}

.panel-note a { color: var(--accent); }

/* --- Tableaux ------------------------------------------------------------- */

.grid { width: 100%; border-collapse: collapse; margin-bottom: 1rem; }

.grid th {
    text-align: left;
    padding: 0.4rem 0.5rem;
    border-bottom: 1px solid var(--rule);
    font: 400 0.65rem/1.4 var(--mono);
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--ink-faint);
}

.grid td {
    padding: 0.5rem;
    border-bottom: 1px solid var(--rule);
    font-size: 0.875rem;
    vertical-align: top;
}

.grid .num { text-align: right; font-variant-numeric: tabular-nums; }

.grid input {
    width: 6rem;
    font: inherit;
    font-family: var(--mono);
    font-size: 0.8rem;
    text-align: right;
    padding: 0.25rem 0.4rem;
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    background: var(--paper);
}

.grid tr.is-retired { opacity: 0.55; }

.model-label { display: block; }

.model-id {
    display: block;
    font: 400 0.7rem/1.6 var(--mono);
    color: var(--ink-faint);
}

.model-note {
    display: block;
    margin-top: 0.15rem;
    font-size: 0.75rem;
    color: var(--ink-soft);
    max-width: 26rem;
}

.tag {
    display: inline-block;
    margin-top: 0.2rem;
    padding: 0.05rem 0.35rem;
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    font: 400 0.65rem/1.6 var(--mono);
    color: var(--ink-faint);
}

.tag-warn { color: var(--danger); border-color: var(--danger); }

.flash-ok { color: var(--accent-ink); border-color: var(--accent); background: var(--accent-wash); }

@media (max-width: 48rem) {
    .grid { display: block; overflow-x: auto; }
}

/* --- Bascule de thème ----------------------------------------------------- */

.theme-toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0;
    border: 0;
    background: none;
    color: var(--ink-faint);
    font: 400 0.7rem/1.6 var(--mono);
    letter-spacing: 0.04em;
    cursor: pointer;
}

.theme-toggle:hover { color: var(--ink-soft); }

.theme-toggle svg {
    width: 0.85rem;
    height: 0.85rem;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.6;
    stroke-linecap: round;
}

/* Une seule icône est visible selon le thème actif : celle de l'état vers
   lequel le clic emmène, pas celle de l'état courant. */
.theme-toggle .icon-dark { display: none; }
[data-theme="dark"] .theme-toggle .icon-dark { display: block; }
[data-theme="dark"] .theme-toggle .icon-light { display: none; }

@media (prefers-color-scheme: dark) {
    :root:not([data-theme]) .theme-toggle .icon-dark  { display: block; }
    :root:not([data-theme]) .theme-toggle .icon-light { display: none; }
}

/* --- Périodes de mesure --------------------------------------------------- */

.period-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 1.5rem;
    margin-bottom: 0.75rem;
}

.inline-form { display: flex; gap: 0.4rem; align-items: center; }

.inline-form input[type="text"] {
    font: inherit;
    font-size: 0.875rem;
    padding: 0.4rem 0.5rem;
    min-width: 16rem;
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    background: var(--paper);
}

.panel-note-tight { margin-bottom: 0; }

.grid form { margin: 0; }

@media (max-width: 48rem) {
    .period-actions { flex-direction: column; gap: 0.75rem; }
    .inline-form { flex-wrap: wrap; }
    .inline-form input[type="text"] { min-width: 0; flex: 1 1 12rem; }
}

/* --- Conversion : champs courts en ligne --------------------------------- */

.inline-form label {
    font: 400 0.7rem/1.8 var(--mono);
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--ink-faint);
    white-space: nowrap;
}

.inline-form input.short {
    min-width: 0;
    width: 5.5rem;
    font-family: var(--mono);
    font-size: 0.8rem;
    text-align: right;
}

/* --- Relevé tarifaire : comparatif avant validation ----------------------- */

.panel-scan { background: var(--accent-wash); padding: 1.25rem; border-radius: var(--radius); }

.scan-old {
    color: var(--ink-faint);
    text-decoration: line-through;
    margin-right: 0.4rem;
}

.scan-new { font-variant-numeric: tabular-nums; }
.scan-none { color: var(--ink-faint); }

.scan-identique { opacity: 0.6; }
.scan-différent .scan-new { color: var(--danger); font-weight: 600; }

.tag-différent { color: var(--danger); border-color: var(--danger); }
.tag-nouveau { color: var(--accent); border-color: var(--accent); }

.panel-scan label { cursor: pointer; }

/* ===========================================================================
   Rendu Markdown des réponses
   ---------------------------------------------------------------------------
   Les marges verticales sont posées en « lobotomized owl » (* + *) : seul
   l'espacement ENTRE éléments est défini, jamais avant le premier ni après le
   dernier. Un bloc rendu se cale ainsi sur son conteneur sans marge parasite.
   =========================================================================== */

.msg-assistant .msg-body > * + * { margin-top: 0.85rem; }
.msg-assistant .msg-body > :first-child { margin-top: 0; }
.msg-assistant .msg-body p { margin: 0; }

/* --- Titres --------------------------------------------------------------- */

.msg-body h1, .msg-body h2, .msg-body h3,
.msg-body h4, .msg-body h5, .msg-body h6 {
    margin: 1.4rem 0 0;
    line-height: 1.3;
    font-weight: 600;
}

.msg-body h1 { font-size: 1.3rem; }
.msg-body h2 { font-size: 1.15rem; }
.msg-body h3 { font-size: 1rem; }
.msg-body h4,
.msg-body h5,
.msg-body h6 { font-size: 0.9rem; color: var(--ink-soft); }

/* --- Listes --------------------------------------------------------------- */

.msg-body ul, .msg-body ol { margin: 0; padding-left: 1.3rem; }
.msg-body li + li { margin-top: 0.3rem; }
.msg-body li > ul, .msg-body li > ol { margin-top: 0.3rem; }

/* --- Code ----------------------------------------------------------------- */

.msg-body code {
    font-family: var(--mono);
    font-size: 0.85em;
    background: var(--wash-strong);
    padding: 0.1em 0.35em;
    border-radius: 3px;
}

.msg-body pre {
    position: relative;
    margin: 0;
    padding: 0.9rem 1rem;
    background: var(--code-bg);
    border-radius: var(--radius);
    overflow-x: auto;
}

.msg-body pre code {
    display: block;
    background: none;
    padding: 0;
    font-size: 0.8rem;
    line-height: 1.55;
    color: var(--code-ink);
    white-space: pre;
}

/* Le bouton reste posé sur le bloc pendant le défilement horizontal, d'où
   position: sticky plutôt qu'absolute. */
.copy-code {
    position: sticky;
    float: right;
    top: 0;
    margin: -0.4rem -0.4rem 0 0.5rem;
    padding: 0.15rem 0.5rem;
    font: 400 0.7rem/1.6 var(--mono);
    /* Couleurs claires assumées : ce bouton est posé sur --code-bg, sombre
       dans les deux thèmes. Il n'a donc pas à suivre la palette. */
    color: #b8b4ae;
    background: rgba(255, 255, 255, 0.08);
    border: 0;
    border-radius: 3px;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.15s;
}

.msg-body pre:hover .copy-code,
.copy-code:focus { opacity: 1; }
.copy-code:hover { color: #fff; background: rgba(255, 255, 255, 0.16); }

/* Sur écran tactile il n'y a pas de survol : le bouton reste visible. */
@media (hover: none) {
    .copy-code { opacity: 1; }
}

/* --- Citations, filets, liens --------------------------------------------- */

.msg-body blockquote {
    margin: 0;
    padding-left: 0.9rem;
    border-left: 2px solid var(--rule);
    color: var(--ink-soft);
}

.msg-body hr { border: 0; border-top: 1px solid var(--rule); margin: 1.2rem 0; }

.msg-body a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
.msg-body a:hover { color: var(--accent-ink); }

/* --- Tableaux ------------------------------------------------------------- */

.msg-body table {
    border-collapse: collapse;
    width: 100%;
    font-size: 0.85rem;
    display: block;
    overflow-x: auto;
}

.msg-body th, .msg-body td {
    padding: 0.4rem 0.6rem;
    border: 1px solid var(--rule);
    text-align: left;
}

.msg-body th { background: var(--wash); font-weight: 600; }

/* --- Message de l'utilisateur : littéral ---------------------------------- */

.msg-user .msg-body { white-space: pre-wrap; }
