/*
 * static/css/artifacts.css — display cards, stage pane / mobile sheet, viewer chrome, deck controls, lightbox. Artifact-only tokens live here; global tokens are requested from the tokens.css owner.
 *
 * Owner: WP B-15. Spec: FR §3, §4.1, §4.8, §6, §10 (tokens from tokens.css §10.1: --card-*, --canvas-*, --checker-*,
 * --badge-*, --stage-*, --z-stage, --z-lightbox).
 * Every selector is defined once, in one file (I7; T-CSS checks the critical ones).
 * No inline styles are written by the JS (CSP): sizes go through CSSOM custom properties / properties.
 */

/* Artifact-only values use var() fallbacks instead of a second :root block (tokens.css owns :root, I7).
 * --aiw-stage-w is set on <html> by stage.js while the desktop pane is open. */

/* ---- the card (FR §3.1) ------------------------------------------------------------------ */
.aiw-card {
    container-type: inline-size;
    container-name: aiw-card;
    position: relative;
    margin: var(--space-3) 0;
    max-width: 100%;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-md);
    box-shadow: var(--elev-1);
    overflow: hidden;
}

.aiw-card__bar {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-height: 40px;
    padding: var(--space-1) var(--space-2) var(--space-1) var(--space-3);
    background: var(--card-bar-bg);
    border-bottom: 1px solid var(--card-border);
    /* the fixed "made by AI" strip: chrome always outside the iframe (FR §3.1) */
    box-shadow: inset 3px 0 0 var(--gold-accent);
    font-size: 13px;
}

.aiw-card__icon { flex: 0 0 auto; width: 18px; text-align: center; color: var(--card-fg-muted); }

.aiw-card__title {
    flex: 0 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 600;
    color: var(--text-color);
}

/* The meta is secondary: a 0 basis gives it only the room the title leaves, so it shrinks away
   before the title is cut (V-6 Y34). */
.aiw-card__meta {
    flex: 1 1 0;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--card-fg-muted);
}

.aiw-badge {
    flex: 0 0 auto;
    padding: 1px 8px;
    border-radius: var(--radius-pill);
    background: var(--badge-bg);
    color: var(--badge-fg);
    font-size: 11px;
    font-weight: 600;
}

.aiw-badge[data-badge="deleted"] { background: var(--danger-rich-soft); color: var(--danger-text); }

.aiw-card__errors {
    flex: 0 0 auto;
    padding: 2px 8px;
    border: 1px solid var(--warning-border);
    border-radius: var(--radius-pill);
    background: var(--warning-bg);
    color: var(--warning-text);
    font: inherit;
    font-size: 11px;
    cursor: pointer;
}

.aiw-card__errors-pop {
    position: absolute;
    top: 42px;
    right: var(--space-2);
    z-index: 3;
    max-width: min(420px, calc(100% - 16px));
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-sm);
    background: var(--surface-1);
    color: var(--text-dim);
    font-size: 12px;
    box-shadow: var(--elev-1);
}

.aiw-card__errors-item { overflow-wrap: anywhere; padding: 2px 0; }

.aiw-card__actions { display: inline-flex; flex: 0 0 auto; align-items: center; gap: 2px; margin-left: auto; }

.aiw-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 30px;
    height: 30px;
    padding: 0 var(--space-2);
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-dim);
    font: inherit;
    font-size: 12px;
    line-height: 1;
    text-decoration: none;
    white-space: nowrap;
    cursor: pointer;
}

.aiw-btn:hover { background: var(--copy-btn-bg-hover); color: var(--text-color); }
.aiw-btn:focus-visible { outline: none; box-shadow: var(--ring); border-color: var(--focus-border); }
.aiw-btn[disabled] { opacity: 0.5; cursor: default; }
.aiw-btn[data-copy="ok"]::after { content: " ✓"; color: var(--gold-accent); }
.aiw-btn[data-copy="fail"]::after { content: " ✕"; color: var(--danger-text); }

.aiw-card__more { display: none; }

/* below 480 px of card width the actions collapse into the ⋯ menu (FR §10.3) */
@container aiw-card (max-width: 480px) {
    .aiw-card__actions > .aiw-btn { display: none; }
    .aiw-card__actions > .aiw-card__more { display: inline-flex; }
    .aiw-card__meta { display: none; }
}

@media (pointer: coarse) {
    .aiw-btn { min-width: 40px; height: 40px; }
}

/* ---- body, viewer box, canvas backgrounds ------------------------------------------------ */
.aiw-card__body {
    position: relative;
    width: 100%;
    overflow: hidden;
    background: var(--card-bg);
}

.aiw-card__body[data-bg="light"] { background: var(--canvas-light); }
.aiw-card__body[data-bg="dark"] { background: var(--canvas-dark); }
.aiw-card__body[data-bg="checker"] {
    background-color: var(--checker-b);
    background-image:
        linear-gradient(45deg, var(--checker-a) 25%, transparent 25%),
        linear-gradient(-45deg, var(--checker-a) 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, var(--checker-a) 75%),
        linear-gradient(-45deg, transparent 75%, var(--checker-a) 75%);
    background-size: 16px 16px;
    background-position: 0 0, 0 8px, 8px -8px, -8px 0;
}

.aiw-card__viewer { position: relative; width: 100%; }
.aiw-card[data-view="deck"] .aiw-card__viewer { position: absolute; inset: 0; }

.aiw-card__caption {
    padding: var(--space-2) var(--space-3);
    border-top: 1px solid var(--card-border);
    color: var(--text-dim);
    font-size: 13px;
    line-height: 1.5;
}

/* loading skeleton (FR §3.2); a static block under prefers-reduced-motion */
.aiw-card__skeleton {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: flex-end;
    justify-content: flex-end;
    padding: var(--space-2) var(--space-3);
    background: linear-gradient(100deg, transparent 30%, var(--gold-soft) 50%, transparent 70%);
    background-size: 200% 100%;
    animation: aiw-shimmer 1.4s linear infinite;
}

.aiw-card__loading-text { color: var(--card-fg-muted); font-size: 12px; }

@keyframes aiw-shimmer {
    from { background-position: 100% 0; }
    to { background-position: -100% 0; }
}

.aiw-card__error {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2);
    min-height: 72px;
    padding: var(--space-3);
    color: var(--text-dim);
    font-size: 13px;
}

.aiw-card[data-error="deleted"] .aiw-card__error-text { color: var(--card-fg-muted); font-style: italic; }
.aiw-card[data-error="misbehaved"] .aiw-card__error-text { color: var(--danger-text); }

.aiw-card__resume,
.aiw-card__staged {
    position: absolute;
    inset: 0;
    width: 100%;
    border: 0;
    background: transparent;
    color: var(--card-fg-muted);
    font: inherit;
    font-size: 13px;
    cursor: pointer;
}

.aiw-card__resume:hover,
.aiw-card__staged:hover { color: var(--text-color); background: var(--gold-soft); }
.aiw-card__resume:focus-visible,
.aiw-card__staged:focus-visible { outline: none; box-shadow: inset var(--ring); }

/* mobile "Tap to interact" shield over scrolling html frames (FR §4.8) */
.aiw-card__shield {
    position: absolute;
    inset: 0;
    z-index: 2;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding-bottom: var(--space-3);
    border: 0;
    background: transparent;
    font: inherit;
    font-size: 12px;
}

/* fixed colours: the html canvas is always white, whatever the theme (L10) */
.aiw-card__shield-label {
    padding: 4px 10px;
    border-radius: 999px;
    background: rgba(0, 0, 0, 0.62);
    color: #fff;
    line-height: 1.4;
    pointer-events: none;
}

.aiw-card__shield::after {
    content: "";
    position: absolute;
    inset: 0;
}

/* ---- the sandboxed iframe (FR §4.1) -------------------------------------------------------- */
.aiw-frame {
    display: block;
    width: 100%;
    border: 0;
    color-scheme: normal;
}

/* html and interactive svg: white canvas like a browser tab, never transparent (FR §4.1, §10.2) */
.aiw-frame--canvas { background: var(--canvas-light); }
/* decks paint their own background and fill their box */
.aiw-frame--deck { height: 100%; background: var(--canvas-dark); }

/* ---- gallery (FR §3.5) ---------------------------------------------------------------------- */
.aiw-gallery {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--space-2);
    margin: var(--space-3) 0;
}

.aiw-gallery > .aiw-gallery__tile { margin: 0; }
.aiw-gallery__tile .aiw-card__body { max-height: 320px; }

@media (max-width: 480px) {
    .aiw-gallery { grid-template-columns: minmax(0, 1fr); }
}

/* ---- control chip (FR §3.6, §6.3) ------------------------------------------------------------ */
.aiw-control-chip {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    max-width: 100%;
    margin: var(--space-1) 0;
    padding: 4px 12px;
    border: 1px solid var(--card-border);
    border-radius: var(--radius-pill);
    background: var(--card-bar-bg);
    color: var(--text-dim);
    font: inherit;
    font-size: 12px;
    cursor: pointer;
}

.aiw-control-chip:hover { color: var(--text-color); border-color: var(--border-strong); }
.aiw-control-chip:focus-visible { outline: none; box-shadow: var(--ring); }
.aiw-control-chip[disabled] { cursor: default; opacity: 0.7; }
.aiw-control-chip[data-flash="true"] { border-color: var(--gold-accent); color: var(--gold-accent); box-shadow: 0 0 0 3px var(--gold-glow); }
.aiw-control-chip__icon { color: var(--card-fg-muted); }
.aiw-control-chip__text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---- action / outline menu (portaled) --------------------------------------------------------- */
.aiw-menu {
    position: fixed;
    z-index: var(--z-lightbox);
    display: flex;
    flex-direction: column;
    min-width: 180px;
    max-width: min(320px, calc(100vw - 16px));
    max-height: min(60vh, 420px);
    overflow-y: auto;
    padding: var(--space-1);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-sm);
    background: var(--surface-1);
    box-shadow: var(--elev-1), 0 12px 32px var(--shadow-soft);
}

.aiw-menu__item {
    display: block;
    width: 100%;
    min-height: 34px;
    padding: 6px 10px;
    border: 0;
    border-radius: 6px;
    background: transparent;
    color: var(--text-color);
    font: inherit;
    font-size: 13px;
    text-align: left;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: pointer;
}

.aiw-menu__item:hover,
.aiw-menu__item:focus-visible { outline: none; background: var(--gold-soft); }

@media (pointer: coarse) {
    .aiw-menu__item { min-height: 40px; }
}

/* ---- the stage (FR §6.1) ---------------------------------------------------------------------- */
.aiw-stage {
    position: fixed;
    top: 0;
    bottom: 0;
    right: 0;
    z-index: var(--z-stage);
    display: flex;
    flex-direction: column;
    background: var(--stage-bg);
    color: var(--text-color);
}

/* min(): the JS clamp is re-applied on resize (L08); until it runs the pane never takes the chat's 480 px */
.aiw-stage--pane {
    width: min(var(--aiw-stage-w, 45vw), max(360px, 100vw - 480px));
    border-left: 1px solid var(--stage-divider);
}

/* the chat column reflows next to the desktop pane and keeps its max width */
body.aiw-stage-open #main-area { margin-right: min(var(--aiw-stage-w, 45vw), max(360px, 100vw - 480px)); }
body.aiw-stage-resizing { cursor: col-resize; user-select: none; }
body.aiw-stage-resizing .aiw-frame { pointer-events: none; }

.aiw-stage--sheet {
    left: 0;
    width: 100%;
    height: 100dvh;
}

body.aiw-stage-sheet-open { overflow: hidden; }

.aiw-stage__divider {
    position: absolute;
    top: 0;
    bottom: 0;
    left: -4px;
    z-index: 1;
    width: 8px;
    cursor: col-resize;
    background: transparent;
}

.aiw-stage__divider:hover,
.aiw-stage__divider:focus-visible { outline: none; background: var(--gold-glow); }

.aiw-stage__bar {
    container-type: inline-size;
    container-name: aiw-stage-bar;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2);
    min-height: 48px;
    padding: var(--space-2) var(--space-3);
    border-bottom: 1px solid var(--stage-divider);
    background: var(--card-bar-bg);
    font-size: 13px;
}

.aiw-stage__title {
    flex: 1 1 120px;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 600;
}

.aiw-stage__deck { display: inline-flex; align-items: center; gap: var(--space-1); }
.aiw-stage__counter { min-width: 52px; text-align: center; color: var(--card-fg-muted); font-variant-numeric: tabular-nums; }

.aiw-stage__outline {
    max-width: 200px;
    height: 30px;
    padding: 0 26px 0 var(--space-2);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-sm);
    background: var(--input-bg) var(--select-chevron) no-repeat right 9px center / 10px 10px;
    appearance: none;
    -webkit-appearance: none;
    cursor: pointer;
    color: var(--text-color);
    font: inherit;
    font-size: 12px;
}

.aiw-stage__actions { display: inline-flex; align-items: center; gap: 2px; }
.aiw-stage__back { font-weight: 600; }

/* The desktop pane's close button never wraps onto a row of its own (X46): the title stops asking
   for its 120 px basis (it takes what is left and ellipsizes). Below 600 px of bar content width the
   deck controls take a second row, so the title keeps readable room next to the actions and ✕ (the
   default 45% pane at 1440 px stays one row). The sheet (phones, landscape too) has no ✕ (Back sits
   first) and keeps its wrap. */
.aiw-stage--pane .aiw-stage__title { flex-basis: 0; }

@container aiw-stage-bar (max-width: 599.98px) {
    .aiw-stage--pane .aiw-stage__deck { order: 1; flex: 1 0 100%; }
}

.aiw-stage__body {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    overflow: auto;
}

.aiw-stage__body .aiw-frame { height: 100%; }
.aiw-stage__body[data-view="deck"] { display: flex; align-items: center; justify-content: center; background: var(--canvas-dark); }
.aiw-stage__body[data-view="deck"] .aiw-frame { width: 100%; height: 100%; }
.aiw-stage__error { padding: var(--space-4); color: var(--text-dim); }

/* ---- lightbox (FR §5.1), portaled to body, z 45 (below system notices, I1) ------------------------ */
.aiw-lightbox {
    position: fixed;
    inset: 0;
    z-index: var(--z-lightbox);
    display: flex;
    flex-direction: column;
    background: var(--lightbox-scrim);   /* dark in both themes: a media viewer needs dark */
    color: #fff;
}

.aiw-lightbox:focus { outline: none; }
body.aiw-lightbox-open { overflow: hidden; }

.aiw-lightbox__bar {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
}

.aiw-lightbox__spacer { flex: 1 1 auto; }
.aiw-lightbox__counter { font-size: 13px; font-variant-numeric: tabular-nums; opacity: 0.85; }

/* Shared pill look of the bar buttons and the side arrows; each sets its own font-size below,
   so no property is defined twice for one selector (I7). */
.aiw-lightbox__btn,
.aiw-lightbox__nav {
    min-width: 40px;
    height: 40px;
    padding: 0 var(--space-3);
    border: 1px solid rgba(255, 255, 255, 0.25);
    border-radius: var(--radius-pill);
    background: rgba(0, 0, 0, 0.45);
    color: #fff;
    font: inherit;
    cursor: pointer;
}
.aiw-lightbox__btn { font-size: 14px; }

.aiw-lightbox__btn:focus-visible,
.aiw-lightbox__nav:focus-visible { outline: none; box-shadow: var(--ring); }

/* The side arrows, on the stage's middle edge. */
.aiw-lightbox__nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    font-size: 22px;
}

.aiw-lightbox__stage {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    touch-action: none;
}

.aiw-lightbox__img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    transform-origin: center center;
    transition: transform 0.12s ease-out;
    user-select: none;
    -webkit-user-drag: none;
    cursor: zoom-in;
}

.aiw-lightbox[data-zoomed="true"] .aiw-lightbox__img { cursor: grab; transition: none; }

.aiw-lightbox__nav--prev { left: var(--space-3); }
.aiw-lightbox__nav--next { right: var(--space-3); }

.aiw-lightbox__caption {
    padding: var(--space-2) var(--space-4) var(--space-4);
    text-align: center;
    font-size: 13px;
    opacity: 0.9;
}

/* ---- utilities ----------------------------------------------------------------------------- */
.aiw-offscreen {
    position: fixed;
    left: -9999px;
    top: 0;
    width: 1px;
    height: 1px;
    opacity: 0;
}

/* ---- reduced motion (FR §10.4): no shimmer, no lightbox zoom animation ------------------------ */
@media (prefers-reduced-motion: reduce) {
    .aiw-card__skeleton { animation: none; background: var(--gold-soft); }
    .aiw-lightbox__img { transition: none; }
}
