/* AgentDisplay — the display itself.
 *
 * Styling here is distribution, not polish: the thing being optimised is how a
 * display looks in a screenshot pasted into Discord or X at roughly 800px wide.
 * Which means: status legible at a glance, high contrast, nothing that depends
 * on hover or motion to be understood.
 *
 * Shared by the public display and (via js/render.js) the MissionBoard tile.
 * Emitted class names live in js/render.js — this file has no markup opinions
 * beyond them.
 *
 * NOTE: the five status hexes below are mirrored in STATUS_META in
 * js/render.js, which needs them where inline colour is unavoidable. Keep the
 * two in step — same convention as CONFIG.DEFAULT_ACCENT / --accent on the
 * MissionBoard side.
 */

:root {
    --ad-bg: #06060c;
    --ad-surface: #0c0c16;
    --ad-surface-2: #12121e;
    --ad-border: rgba(255, 255, 255, 0.09);
    --ad-border-soft: rgba(255, 255, 255, 0.05);
    --ad-text: #e6e9ef;
    --ad-text-2: #b9bfcb;
    --ad-dim: #7c828f;

    /* The frozen five, plus the grey everything else falls back to. */
    --ad-running: #4fc3f7;
    --ad-waiting: #f59e0b;
    --ad-complete: #4ade80;
    --ad-error: #ef4444;
    --ad-blocked: #c084fc;
    --ad-unknown: #5a5e6a;

    --ad-font: 'Outfit', system-ui, -apple-system, 'Segoe UI', sans-serif;
    --ad-mono: 'JetBrains Mono', ui-monospace, 'Cascadia Mono', Consolas, monospace;
    --ad-head: 'Space Grotesk', var(--ad-font);

    --ad-radius: 14px;
}

/* Each status resolves --ad-c once, and everything downstream — pill, rail,
 * glow, focus ring — reads that. Adding a sixth status would be one block
 * here and one entry in the enum, which is the point of freezing five. */
.ad-status--running,  [data-status="running"]  { --ad-c: var(--ad-running); }
.ad-status--waiting,  [data-status="waiting"]  { --ad-c: var(--ad-waiting); }
.ad-status--complete, [data-status="complete"] { --ad-c: var(--ad-complete); }
.ad-status--error,    [data-status="error"]    { --ad-c: var(--ad-error); }
.ad-status--blocked,  [data-status="blocked"]  { --ad-c: var(--ad-blocked); }
.ad-status--unknown,  [data-status="unknown"]  { --ad-c: var(--ad-unknown); }

/* ── Status pill ─────────────────────────────────────────────────────────────
 * The most-screenshotted element on the site. Icon + word + colour, all three:
 * colour alone fails for colour-blind readers and fails again the moment
 * someone posts a greyscale crop. */
.ad-status {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    flex-shrink: 0;
    padding: 6px 14px 6px 11px;
    border-radius: 999px;
    background: color-mix(in srgb, var(--ad-c, var(--ad-unknown)) 16%, transparent);
    border: 1px solid color-mix(in srgb, var(--ad-c, var(--ad-unknown)) 42%, transparent);
    color: var(--ad-c, var(--ad-unknown));
    font-family: var(--ad-head);
    font-size: 13px;
    font-weight: 600;
    letter-spacing: 0.02em;
    line-height: 1;
    white-space: nowrap;
}

.ad-status .ad-icon { display: block; flex-shrink: 0; }

.ad-status--compact {
    padding: 3px 9px 3px 7px;
    font-size: 11px;
    gap: 5px;
}

/* `running` is the only status that earns motion — it is the only one making a
 * claim about right now. The other four are settled states and sit still. */
.ad-status--running .ad-icon {
    animation: ad-pulse 2s ease-in-out infinite;
}

@keyframes ad-pulse {
    0%, 100% { opacity: 1; }
    50%      { opacity: 0.45; }
}

/* ── Display shell ───────────────────────────────────────────────────────── */
.ad-display {
    max-width: 860px;
    margin: 0 auto;
}

/* A bare row, not a panel. The header used to be a full-width card carrying a
 * status pill and a wash of the status colour; the status now travels with the
 * newest message instead, which leaves nothing here worth drawing a box
 * around. */
.ad-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
    margin-bottom: 10px;
    min-height: 30px;
}

/* Stats hug the right edge whether or not a name sits opposite them. */
.ad-head .ad-stats {
    justify-content: flex-end;
    margin-left: auto;
    margin-bottom: 0;
    gap: 6px;
}

.ad-name {
    font-family: var(--ad-head);
    font-size: clamp(22px, 4.2vw, 30px);
    font-weight: 700;
    color: #fff;
    line-height: 1.15;
    margin: 0;
    min-width: 0;
    overflow-wrap: anywhere;
}

.ad-updated {
    position: relative;
    z-index: 1;
    font-family: var(--ad-mono);
    font-size: 11px;
    color: var(--ad-dim);
}

/* ── Stat cards ──────────────────────────────────────────────────────────────
 * Freeform by design: any param that isn't one of the three reserved keys
 * lands here, with no registration and no schema. The grid has to cope with
 * one card or twelve, and with keys nobody planned for. */
/* Small by design on the full display. A stat card is whatever the agent
 * decided to send — it may be the headline number of the run, or it may be a
 * build id nobody reads. The renderer cannot tell which, so it must not
 * gamble: a giant unexplained figure next to a word like TESTS dominates the
 * card and tells a stranger nothing. The message is the story here; stats are
 * supporting detail, sized accordingly.
 *
 * The board TILE overrides this and makes them large again (see .ad-tile-stats)
 * — different job. On a wall the number IS the point, and the board owner
 * chose which two appear. */
.ad-stats {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 16px;
}

/* Outlined, not filled. The border is enough to read them as discrete chips;
   a background made a row of them look like a toolbar of buttons competing
   with the messages underneath. */
.ad-stat {
    display: flex;
    align-items: baseline;
    gap: 8px;
    background: none;
    border: 1px solid rgba(255, 255, 255, 0.09);
    border-radius: 6px;
    padding: 4px 10px;
    min-width: 0;
}

.ad-stat-key {
    font-family: var(--ad-mono);
    font-size: 8.5px;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--ad-dim);
    flex: 0 0 auto;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ad-stat-value {
    font-family: var(--ad-mono);
    font-size: 12px;
    font-weight: 600;
    color: var(--ad-text-2);
    line-height: 1.25;
    overflow-wrap: anywhere;
}

/* Numbers get the tabular treatment so a column of them lines up; words keep
 * normal spacing and a smaller size, because "deploying to prod" is a label,
 * not a figure. */
.ad-stat--num .ad-stat-value {
    font-variant-numeric: tabular-nums;
}

.ad-stat:not(.ad-stat--num) .ad-stat-value {
    font-size: 11.5px;
}

/* Whatever sits on the right goes to the right, with or without a name beside
   it. Structural rather than a :has() selector, so it holds in any browser. */
.ad-head-top > .ad-head-right { margin-left: auto; }

/* The tile keeps the stacked, large treatment — see the note above .ad-stats. */
.ad-tile-stats .ad-stat {
    display: block;
    align-items: initial;
}

/* ── Correction notice ───────────────────────────────────────────────────────
 * Shown when an agent used an alias. Quiet — the display still works — but
 * present, because silent aliasing is how five status values become fifteen
 * and the format stops being a format. */
.ad-correction {
    display: flex;
    align-items: flex-start;
    gap: 9px;
    padding: 11px 15px;
    margin-bottom: 18px;
    background: rgba(245, 158, 11, 0.07);
    border: 1px dashed rgba(245, 158, 11, 0.28);
    border-radius: 10px;
    font-size: 13px;
    line-height: 1.55;
    color: #f7c667;
}

.ad-correction svg { flex-shrink: 0; margin-top: 3px; }
.ad-correction code {
    font-family: var(--ad-mono);
    font-size: 12px;
    background: rgba(245, 158, 11, 0.14);
    border-radius: 4px;
    padding: 1px 5px;
    color: #fbbf24;
}

.ad-correction-link {
    margin-left: auto;
    flex-shrink: 0;
    font-family: var(--ad-mono);
    font-size: 11px;
    color: rgba(245, 158, 11, 0.75);
    text-decoration: underline;
    text-underline-offset: 2px;
}

.ad-correction-link:hover { color: #fbbf24; }

/* ── Message feed ────────────────────────────────────────────────────────── */
.ad-feed {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.ad-msg {
    background: var(--ad-surface);
    border: 1px solid var(--ad-border-soft);
    border-radius: var(--ad-radius);
    padding: 18px 22px;
}

/* The newest message carries the status. It IS the current state, so the
 * colour belongs on it rather than on a separate pill above — and a thumbnail
 * still reads red or green before any text resolves, which was the one job
 * the old header wash was doing. */
.ad-msg--current {
    border-color: color-mix(in srgb, var(--ad-c, var(--ad-running)) 30%, transparent);
    border-left: 3px solid var(--ad-c, var(--ad-running));
    background:
        linear-gradient(100deg,
            color-mix(in srgb, var(--ad-c, var(--ad-running)) 10%, transparent),
            transparent 58%),
        var(--ad-surface);
}

/* The status word, beside the timestamp. Colour alone is not a signal — this
   is what a colour-blind reader and a greyscale screenshot rely on. */
.ad-msg-status {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    margin-left: auto;
    flex-shrink: 0;
    color: var(--ad-c, var(--ad-running));
    font-weight: 500;
    letter-spacing: 0.04em;
}

.ad-msg-status .ad-icon { display: block; }
.ad-msg--current[data-live] .ad-msg-status .ad-icon { animation: ad-pulse 2s ease-in-out infinite; }

/* Only the newest message is at full strength. Older ones recede rather than
 * disappear — the feed is the story of the run, but the top card is the answer
 * to "what is it doing". */
.ad-msg--history {
    opacity: 0.62;
    padding: 14px 22px;
    background: transparent;
    border-color: var(--ad-border-soft);
}

.ad-msg-meta {
    display: flex;
    align-items: baseline;
    gap: 10px;
    font-family: var(--ad-mono);
    font-size: 11px;
    color: var(--ad-dim);
    margin-bottom: 9px;
}

.ad-msg-ago { color: var(--ad-c, var(--ad-running)); opacity: 0.75; }

/* Quiet — it is an index, not a headline. Present so a reader can say "it has
   said eight things" without counting cards. */
.ad-msg-num {
    color: var(--ad-dim);
    opacity: 0.7;
    flex-shrink: 0;
}

.ad-msg-body {
    margin: 0;
    font-size: 15.5px;
    line-height: 1.75;
    color: var(--ad-text);
    overflow-wrap: anywhere;
}

.ad-msg--history .ad-msg-body { font-size: 14.5px; color: var(--ad-text-2); }

.ad-msg--new { animation: ad-slide-in 0.4s ease both; }

@keyframes ad-slide-in {
    from { opacity: 0; transform: translateY(-8px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* A press landed. The whole display takes a brief ring in the current status
 * colour, because the new message is not always the thing that changed — a
 * status flip or a stat card moving has to be just as obvious, and a reader
 * looking at the wrong part of the card would otherwise miss it entirely. */
.ad-display--flash { animation: ad-flash 0.9s ease both; }

@keyframes ad-flash {
    0%   { box-shadow: 0 0 0 0 color-mix(in srgb, var(--ad-c, var(--ad-running)) 55%, transparent); }
    100% { box-shadow: 0 0 0 14px transparent; }
}

.ad-display--flash .ad-head {
    animation: ad-flash-head 0.9s ease both;
}

@keyframes ad-flash-head {
    0%   { background: color-mix(in srgb, var(--ad-c, var(--ad-running)) 22%, var(--ad-surface)); }
    100% { background: var(--ad-surface); }
}

/* ── Empty state ─────────────────────────────────────────────────────────── */
.ad-empty {
    text-align: center;
    padding: 64px 24px;
    border: 1px dashed var(--ad-border);
    border-radius: var(--ad-radius);
}

.ad-empty p {
    margin: 0;
    font-family: var(--ad-mono);
    font-size: 13px;
    color: var(--ad-dim);
}

.ad-empty-dot {
    width: 11px;
    height: 11px;
    border-radius: 50%;
    background: var(--ad-running);
    margin: 0 auto 16px;
    animation: ad-pulse 2s ease-in-out infinite;
}

/* ── Tile mode (MissionBoard grid) ───────────────────────────────────────────
 * Different job to the full display: on a wall with twelve agents up nobody
 * reads paragraphs — they take in all of them at once. Name, status, at most two stats. Sizing
 * is container-relative (cqmin) to match every other MissionBoard widget, so
 * the same tile is legible small and on a wall TV. */
.ad-tile {
    height: 100%;
    display: flex;
    flex-direction: column;
    gap: 1.6cqh;
    padding: clamp(12px, 2.4cqmin, 22px);
    box-sizing: border-box;
    overflow: hidden;
    font-family: var(--ad-font);
}

.ad-tile-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    flex: 0 0 auto;
}

.ad-tile-name {
    font-family: var(--ad-head);
    font-size: clamp(0.72rem, calc(0.6rem + 1.7cqmin), 1.5rem);
    font-weight: 700;
    color: #fff;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}

.ad-tile .ad-status--compact {
    font-size: clamp(0.55rem, calc(0.48rem + 1.05cqmin), 0.95rem);
    padding: 0.5cqmin 1.4cqmin 0.5cqmin 1.1cqmin;
}

.ad-tile-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
    gap: 1.4cqw;
    flex: 0 0 auto;
}

.ad-tile-stats .ad-stat { padding: 1.4cqmin 1.8cqmin; background: rgba(255, 255, 255, 0.03); }
.ad-tile-stats .ad-stat-key { font-size: clamp(0.44rem, calc(0.4rem + 0.75cqmin), 0.72rem); }
.ad-tile-stats .ad-stat-value { font-size: clamp(0.8rem, calc(0.65rem + 2cqmin), 1.9rem); }
.ad-tile-stats .ad-stat:not(.ad-stat--num) .ad-stat-value {
    font-size: clamp(0.6rem, calc(0.52rem + 1.2cqmin), 1.05rem);
}

/* ── Cluster tile: one display, many agents ──────────────────────────────────
 * The face is the roster. Rows are deliberately tighter than stat cards —
 * six agents must fit a widget-sized tile and still read on a wall. */
.ad-cluster-list {
    display: flex;
    flex-direction: column;
    gap: 0.9cqmin;
    min-height: 0;
    overflow: hidden;
}

.ad-cluster-row {
    display: flex;
    align-items: center;
    gap: 1.6cqmin;
    font-size: clamp(0.62rem, calc(0.54rem + 1.3cqmin), 1.05rem);
    line-height: 1.5;
    color: var(--ad-text-2);
    min-width: 0;
}

.ad-cluster-dot {
    flex: 0 0 auto;
    width: 0.9em; height: 0.9em;
    border-radius: 50%;
}

.ad-cluster-lock { flex: 0 0 auto; font-size: 0.85em; }

.ad-cluster-name {
    font-weight: 600;
    color: var(--ad-text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}

.ad-cluster-status {
    flex: 0 0 auto;
    margin-left: auto;
    font-family: var(--ad-mono);
    font-size: 0.82em;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

.ad-cluster-row--locked { color: var(--ad-dim); }
.ad-cluster-row--locked .ad-cluster-name { color: var(--ad-dim); font-weight: 500; }

.ad-cluster-more {
    font-family: var(--ad-mono);
    font-size: clamp(0.5rem, calc(0.44rem + 0.9cqmin), 0.8rem);
    color: var(--ad-dim);
}

.ad-tile-msg {
    margin: 0;
    flex: 0 1 auto;
    min-height: 0;
    font-size: clamp(0.6rem, calc(0.53rem + 1.35cqmin), 1.1rem);
    line-height: 1.55;
    color: var(--ad-text-2);
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
}

.ad-tile-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-top: auto;
    flex: 0 0 auto;
    font-family: var(--ad-mono);
    font-size: clamp(0.48rem, calc(0.44rem + 0.85cqmin), 0.8rem);
    color: var(--ad-dim);
}

.ad-tile-more {
    color: var(--ad-c, var(--ad-unknown));
    opacity: 0.8;
}

/* Expanded tile — the message feed and the stats that didn't fit on the face.
 * Hidden by default because the face has one job (read at a glance) and a feed on
 * a twelve-tile wall is noise. */
.ad-tile-detail { display: none; }

.ad-tile--expanded .ad-tile-msg { display: none; }

.ad-tile--expanded .ad-tile-detail {
    display: flex;
    flex-direction: column;
    gap: 1.2cqh;
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    padding-right: 0.6cqw;
}

.ad-tile-detail .ad-stats {
    grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
    gap: 1.2cqw;
    margin-bottom: 0;
}

.ad-tile-detail .ad-msg {
    padding: 1.4cqmin 1.8cqmin;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.03);
}

.ad-tile-detail .ad-msg-meta {
    font-size: clamp(0.44rem, calc(0.4rem + 0.8cqmin), 0.75rem);
    margin-bottom: 0.6cqh;
}

.ad-tile-detail .ad-msg-body {
    font-size: clamp(0.58rem, calc(0.52rem + 1.15cqmin), 1rem);
    line-height: 1.5;
}

.ad-tile-toggle {
    background: none;
    border: none;
    padding: 0;
    cursor: pointer;
    font: inherit;
    color: var(--ad-dim);
    display: inline-flex;
    align-items: center;
    gap: 4px;
}
.ad-tile-toggle:hover { color: var(--ad-text-2); }
.ad-tile-toggle svg { transition: transform 0.2s; }
.ad-tile--expanded .ad-tile-toggle svg { transform: rotate(180deg); }

/* A tile whose display has gone quiet. Not a status — the agent never said
 * anything — so it reads as a note about the tile, not a claim about the run. */
.ad-tile-stale {
    color: var(--ad-waiting);
    opacity: 0.85;
}

/* ── Accessibility ───────────────────────────────────────────────────────────
 * Scoped to the display, not the document. This file is synced into the
 * MissionBoard tree (css/agentdisplay.css) and loaded by the board tile — a
 * bare `*` rule here would reach across a whole app that never asked for it. */
:where(.ad-display, .ad-tile) :where(a, button, [tabindex]):focus-visible {
    outline: 2px solid var(--ad-running);
    outline-offset: 2px;
    border-radius: 6px;
}

/* A status change is worth animating. Ambient movement everywhere is not, and
 * for a reader who has asked for less of it, none of it is. */
@media (prefers-reduced-motion: reduce) {
    .ad-display, .ad-tile,
    .ad-display *, .ad-tile *,
    .ad-display *::before, .ad-tile *::before,
    .ad-display *::after, .ad-tile *::after,
    .ad-status, .ad-status *, .ad-empty-dot {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
    }
}

/* Browsers without color-mix() fall back to a flat tint rather than an
 * invisible pill. Small enough to be worth carrying. */
@supports not (background: color-mix(in srgb, red 10%, transparent)) {
    .ad-status {
        background: rgba(255, 255, 255, 0.07);
        border-color: currentColor;
    }
    .ad-head::after { display: none; }
}

@media (max-width: 640px) {
    .ad-head { padding: 18px 18px 16px; }
    .ad-msg { padding: 15px 17px; }
    .ad-msg-body { font-size: 14.5px; }
    .ad-correction { flex-wrap: wrap; }
    .ad-correction-link { margin-left: 0; }

    /* The header stacks on a phone, so the stats go left-aligned under the
       name rather than clinging to a right edge that is no longer there. */
    .ad-head-right {
        align-items: flex-start;
        width: 100%;
    }
    .ad-head-right .ad-stats { justify-content: flex-start; }
}
