/* XLA fusion playground — Hyperstack tokens, theme-aware, no CDNs. */
:root {
  --bg:#fff; --fg:#0f172a; --muted:#475569; --surface:#f1f5f9; --panel:#fff; --border:#e2e8f0; --inset:#f8fafc;
  --primary:#2563eb; --primary-contrast:#fff; --good:#0f766e; --warn:#b45309; --danger:#b91c1c; --hbm:#7c3aed; --vmem:#0f766e; --compile:#0369a1;
}
@media (prefers-color-scheme: dark) {
  :root { --bg:#0f172a; --fg:#f8fafc; --muted:#a5b1c2; --surface:#1e293b; --panel:#1e293b; --border:#334155; --inset:#0f172a;
    --primary:#60a5fa; --primary-contrast:#0b1220; --good:#2dd4bf; --warn:#fbbf24; --danger:#f87171; --hbm:#a78bfa; --vmem:#2dd4bf; --compile:#38bdf8; }
}
[data-theme="light"] { --bg:#fff; --fg:#0f172a; --muted:#475569; --surface:#f1f5f9; --panel:#fff; --border:#e2e8f0; --inset:#f8fafc; --primary:#2563eb; --primary-contrast:#fff; --good:#0f766e; --warn:#b45309; --danger:#b91c1c; --hbm:#7c3aed; --vmem:#0f766e; --compile:#0369a1; }
[data-theme="dark"] { --bg:#0f172a; --fg:#f8fafc; --muted:#a5b1c2; --surface:#1e293b; --panel:#1e293b; --border:#334155; --inset:#0f172a; --primary:#60a5fa; --primary-contrast:#0b1220; --good:#2dd4bf; --warn:#fbbf24; --danger:#f87171; --hbm:#a78bfa; --vmem:#2dd4bf; --compile:#38bdf8; }

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body { background: var(--bg); color: var(--fg); font-family: ui-sans-serif, system-ui, sans-serif; }
#app { padding: 20px; max-width: 960px; margin: 0 auto; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

.header-bar { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 20px; flex-wrap: wrap; gap: 16px; }
.hint { font-size: 14px; color: var(--muted); line-height: 1.55; max-width: 600px; margin: 0; }
.hint b { color: var(--fg); }
.controls-panel { display: flex; flex-direction: column; gap: 10px; }
.btn-group { display: flex; gap: 8px; }

.btn-run { font: inherit; font-size: 14px; font-weight: 800; padding: 12px 18px; border: none; border-radius: 8px; cursor: pointer; transition: filter .2s, transform .2s, background .2s; display: inline-flex; align-items: center; gap: 8px; }
.btn-run.compile { background: var(--compile); color: var(--primary-contrast); box-shadow: 0 4px 12px color-mix(in srgb, var(--compile) 40%, transparent); }
.btn-run.execute { background: var(--primary); color: var(--primary-contrast); box-shadow: 0 4px 12px color-mix(in srgb, var(--primary) 40%, transparent); }
.btn-run.secondary { background: transparent; color: var(--fg); border: 2px solid var(--border); box-shadow: none; padding: 10px 16px; }
.btn-run.secondary:hover:not(:disabled):not([aria-disabled="true"]) { background: color-mix(in srgb, var(--fg) 5%, transparent); border-color: var(--muted); }
.btn-run.compile.secondary { color: var(--compile); border-color: var(--compile); }
.btn-run.compile.secondary:hover:not(:disabled):not([aria-disabled="true"]) { background: color-mix(in srgb, var(--compile) 10%, transparent); }
.btn-run:hover:not(:disabled):not([aria-disabled="true"]) { filter: brightness(.92); transform: translateY(-1px); }
.btn-run:disabled, .btn-run[aria-disabled="true"] { background: var(--surface); color: var(--muted); box-shadow: none; transform: none; cursor: not-allowed; border: 1px solid var(--border); }

/* Compile pipeline */
.compile-stage { display: flex; justify-content: space-between; align-items: center; background: var(--inset); padding: 16px 20px; border-radius: 14px; border: 1px solid var(--border); margin-bottom: 16px; gap: 8px; }
.c-node { flex: 1; min-width: 140px; background: var(--panel); border: 2px solid var(--border); border-radius: 10px; padding: 14px 10px; text-align: center; transition: border-color .4s, box-shadow .4s, transform .4s; border-style: dashed; }
.c-node.active { border-style: solid; border-color: var(--compile); box-shadow: 0 4px 16px color-mix(in srgb, var(--compile) 30%, transparent); transform: translateY(-2px); }
.c-node.active.xla { border-color: var(--primary); box-shadow: 0 4px 20px color-mix(in srgb, var(--primary) 40%, transparent); }
.c-title { font-size: 11px; font-weight: 800; text-transform: uppercase; color: var(--compile); margin-bottom: 6px; letter-spacing: .05em; }
.c-node.active.xla .c-title { color: var(--primary); }
.c-desc { font-size: 14px; font-weight: 700; color: var(--fg); line-height: 1.4; }
.c-desc small { color: var(--muted); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11.5px; font-weight: 500; display: block; margin-top: 4px; }
.c-arrow { color: var(--border); font-size: 24px; font-weight: bold; opacity: .4; transition: color .4s, opacity .4s; }
.c-arrow.active { color: var(--compile); opacity: 1; }
/* In a row the four stages need about 680 px. Narrower, they stack and the arrows point down, where the row used to
   scroll sideways out of a keyboard's reach (LMS-content#130). After the rules it overrides, so that it wins. */
@media (max-width: 760px) {
  .compile-stage { flex-direction: column; align-items: stretch; }
  .c-node { min-width: 0; }
  .c-arrow { align-self: center; transform: rotate(90deg); line-height: 1; }
}

/* Compiler inspector */
.inspector-panel { display: none; background: var(--panel); border: 1px solid var(--compile); border-radius: 12px; margin-bottom: 24px; overflow: hidden; box-shadow: 0 8px 24px color-mix(in srgb, var(--compile) 15%, transparent); }
.inspector-panel.visible { display: block; animation: slideDown .4s ease; }
.inspector-header { background: color-mix(in srgb, var(--compile) 10%, var(--surface)); padding: 10px 16px; font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: .05em; color: var(--compile); border-bottom: 1px solid color-mix(in srgb, var(--compile) 20%, transparent); display: flex; align-items: center; gap: 8px; }
.inspector-header svg { width: 14px; height: 14px; }
.inspector-content { display: grid; grid-template-columns: 1fr 1fr; gap: 0; }
@media (max-width: 768px) { .inspector-content { grid-template-columns: 1fr; } }
.inspector-code { padding: 16px; background: var(--inset); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; color: var(--fg); white-space: pre-wrap; line-height: 1.6; border-right: 1px solid var(--border); overflow-x: auto; }
@media (max-width: 768px) { .inspector-code { border-right: none; border-bottom: 1px solid var(--border); } }
.inspector-explain { padding: 16px; font-size: 14px; line-height: 1.6; color: var(--fg); display: flex; flex-direction: column; justify-content: center; }
.inspector-explain h4 { margin: 0 0 8px 0; color: var(--compile); font-size: 15px; }
.inspector-explain.xla-h4 h4 { color: var(--primary); }
.inspector-explain p { margin: 0; color: var(--muted); }
.inspector-explain code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; color: var(--fg); background: color-mix(in srgb, var(--muted) 14%, transparent); padding: 1px 5px; border-radius: 4px; }
@keyframes slideDown { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: translateY(0); } }

/* Syntax highlighting — theme-aware tokens */
.syn-kw { color: var(--primary); font-weight: 600; }
.syn-fn { color: var(--vmem); }
.syn-ty { color: var(--warn); }
.syn-op { color: var(--hbm); }
.syn-cm { color: var(--muted); font-style: italic; }

/* Race stage */
.race-stage { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 24px; }
@media (max-width: 880px) { .race-stage { grid-template-columns: 1fr; gap: 32px; } } /* two columns need about 860 px (LMS-content#130) */
.race-col { background: var(--inset); border: 1px solid var(--border); border-radius: 16px; padding: 16px; display: flex; flex-direction: column; transition: opacity .5s, filter .5s; }
.race-col.locked { filter: grayscale(1); border-style: dashed; pointer-events: none; } /* greyed, never faded: its text keeps its contrast (LMS-content#99) */
.highlight-pulse { animation: pulseCol 1.2s ease-out; }
@keyframes pulseCol {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--primary) 60%, transparent); border-color: var(--primary); }
  50% { box-shadow: 0 0 30px 10px color-mix(in srgb, var(--primary) 0%, transparent); border-color: var(--primary); }
  100% { box-shadow: 0 0 0 0 transparent; border-color: var(--border); }
}
.col-header { font-size: 14px; font-weight: 800; text-transform: uppercase; letter-spacing: .05em; text-align: center; margin-bottom: 16px; padding-bottom: 12px; border-bottom: 2px solid var(--border); }
.unfused-header { color: var(--muted); }
.fused-header { color: var(--primary); }

.chip-boundary { padding: 24px 8px 12px; border-radius: 12px; border: 2px dashed var(--border); background: color-mix(in srgb, var(--muted) 3%, transparent); position: relative; transition: border-color .3s, background .3s; }
.chip-boundary.fused { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 5%, transparent); border-style: solid; }
.kernel-label { position: absolute; top: -10px; left: 16px; background: var(--inset); padding: 0 8px; font-size: 10px; font-weight: 800; color: var(--primary); letter-spacing: .05em; text-transform: uppercase; }

.ops-row { display: flex; align-items: flex-start; justify-content: space-between; }
.op-wrapper { display: flex; flex-direction: column; align-items: center; gap: 8px; flex: 1; }
.connector { display: flex; flex-direction: column; align-items: center; width: 16px; padding-top: 16px; }
.op { width: 100%; max-width: 76px; padding: 12px 4px; border-radius: 8px; border: 2px solid var(--border); background: var(--panel); text-align: center; font-size: 13px; font-weight: 800; color: var(--fg); transition: border-color .2s, background .2s, transform .2s, box-shadow .2s; position: relative; z-index: 5; }
.op.mm { border-color: color-mix(in srgb, var(--primary) 40%, transparent); }
.op.active { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 10%, var(--panel)); box-shadow: 0 0 16px color-mix(in srgb, var(--primary) 40%, transparent); transform: scale(1.08); }
.mem { font-size: 9px; font-weight: 800; text-transform: uppercase; padding: 3px 6px; border-radius: 999px; white-space: nowrap; }
.mem.hbm { background: color-mix(in srgb, var(--hbm) 15%, transparent); color: color-mix(in srgb, var(--hbm) 75%, var(--fg)); border: 1px solid color-mix(in srgb, var(--hbm) 30%, transparent); }
.mem.vmem { background: color-mix(in srgb, var(--vmem) 15%, transparent); color: color-mix(in srgb, var(--vmem) 75%, var(--fg)); border: 1px solid color-mix(in srgb, var(--vmem) 30%, transparent); }
.arrow { font-size: 16px; font-weight: bold; color: var(--muted); }

.vmem-lane { width: 16px; height: 3px; background: color-mix(in srgb, var(--primary) 20%, transparent); position: relative; border-radius: 2px; overflow: hidden; opacity: 0; transition: opacity .3s; }
.chip-boundary.fused .vmem-lane { opacity: 1; }
.vmem-pulse { position: absolute; width: 8px; height: 8px; border-radius: 50%; top: -2.5px; background: var(--vmem); box-shadow: 0 0 8px var(--vmem); opacity: 0; }
.vmem-lane.active .vmem-pulse { animation: flowRight .2s linear forwards; }
@keyframes flowRight { 0%{left:0;opacity:0;transform:scale(.5)} 20%{opacity:1;transform:scale(1)} 80%{opacity:1} 100%{left:100%;opacity:0;transform:scale(.5)} }

.hbm-lanes { display: flex; justify-content: space-between; height: 40px; margin-top: -4px; padding: 0 8px; }
.hbm-lane-wrap { width: 76px; display: flex; justify-content: center; }
.hbm-gap { width: 16px; }
/* A race column holds four ops in a row with their memory lanes under them, about 440 px with the page's padding. On a
   narrow screen, and in a 286 px lesson frame, the ops, the gaps and the paddings shrink so that the column fits, and
   each lane stays under its op (LMS-content#130). */
@media (max-width: 480px) {
  #app { padding: 12px; }
  .race-col { padding: 12px; }
  .chip-boundary { padding: 22px 3px 10px; }
  .connector, .hbm-gap, .vmem-lane { width: 10px; }
  .arrow { font-size: 11px; }
  .op { font-size: 11px; padding: 10px 1px; }
  .mem { font-size: 8px; padding: 2px 4px; }
  .hbm-lanes { padding: 0 5px; }
  .hbm-lane-wrap { width: auto; flex: 1; }
}
.hbm-lane { width: 3px; height: 100%; background: color-mix(in srgb, var(--hbm) 20%, transparent); position: relative; transition: opacity .3s; }
.hbm-lane.hidden { opacity: 0; }
.hbm-pulse-up, .hbm-pulse-down { position: absolute; left: -3.5px; width: 10px; height: 10px; border-radius: 50%; background: var(--hbm); box-shadow: 0 0 10px var(--hbm); opacity: 0; }
.hbm-lane.active-up .hbm-pulse-up { animation: flowUp .4s linear forwards; }
.hbm-lane.active-down .hbm-pulse-down { animation: flowDown .4s linear forwards; }
@keyframes flowUp { 0%{bottom:0;opacity:0} 10%{opacity:1} 90%{opacity:1} 100%{bottom:100%;opacity:0} }
@keyframes flowDown { 0%{top:0;opacity:0} 10%{opacity:1} 90%{opacity:1} 100%{top:100%;opacity:0} }

.hbm-block { padding: 12px 16px; border-radius: 10px; text-align: center; font-size: 12px; font-weight: 800; color: color-mix(in srgb, var(--hbm) 75%, var(--fg)); background: color-mix(in srgb, var(--hbm) 10%, transparent); border: 2px solid color-mix(in srgb, var(--hbm) 40%, transparent); text-transform: uppercase; letter-spacing: .05em; transition: background .2s, box-shadow .2s; }
.hbm-block.active { background: color-mix(in srgb, var(--hbm) 25%, transparent); box-shadow: 0 0 20px color-mix(in srgb, var(--hbm) 30%, transparent); }

.mini-dashboard { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 1px; background: var(--border); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; margin-top: 16px; }
.m-stat { background: var(--panel); padding: 12px 8px; text-align: center; transition: background .3s; }
.m-stat.highlight { background: color-mix(in srgb, var(--primary) 15%, var(--panel)); }
.m-k { font-size: 9px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 800; margin-bottom: 6px; }
.m-v { font-size: 20px; font-weight: 800; color: var(--fg); font-variant-numeric: tabular-nums; }
.m-v small { font-size: 10px; color: var(--muted); }
.m-bound { margin-top: 8px; padding: 10px; text-align: center; font-size: 12px; font-weight: 800; border-radius: 8px; background: var(--surface); color: var(--muted); border: 1px solid var(--border); text-transform: uppercase; letter-spacing: .03em; transition: background .3s, color .3s, border-color .3s; }
.m-bound.good { background: color-mix(in srgb, var(--good) 15%, transparent); color: color-mix(in srgb, var(--good) 75%, var(--fg)); border-color: var(--good); }
.m-bound.warn { background: color-mix(in srgb, var(--warn) 14%, transparent); color: color-mix(in srgb, var(--warn) 75%, var(--fg)); border-color: var(--warn); }

.note { font-size: 14px; line-height: 1.6; color: var(--muted); background: var(--surface); border-radius: 12px; padding: 16px 20px; border-left: 4px solid var(--primary); }
.note b { color: var(--fg); font-weight: 700; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; }
  .vmem-lane.active .vmem-pulse,
  .hbm-lane.active-up .hbm-pulse-up,
  .hbm-lane.active-down .hbm-pulse-down,
  .highlight-pulse, .inspector-panel.visible { animation: none !important; }
}
