/*
 * Simulador de Paralelismo de Dados RISC-V
 * Tema claro e tema escuro definidos por variáveis; o tema é escolhido em <html data-theme="light|dark">.
 * Cores de etiquetas (estações, ROB, instruções) chegam pela variável --tag e são misturadas com o fundo.
 */

:root,
[data-theme='light'] {
    --bg: #f8fafc;
    --panel: #ffffff;
    --panel-2: #f2f4f8;
    --line: #b4bfcf;
    --line-strong: #8796ad;
    --ink: #111827;
    --muted: #475266;
    --faint: #667085;
    --accent: #1650ff;
    --accent-ink: #ffffff;
    --accent-soft: #e3ebff;
    --focus: #d99700;
    --focus-soft: #fff3cf;
    --warn: #b23a12;
    --ok: #1d6e42;
    --ok-soft: #d6f0e0;
    --bad-soft: #f8d9d0;
    --wire: #8f9bb0;
    --c-issue: #e3e7fb;
    --c-id: #e7ebf2;
    --c-exec: #d7f0e3;
    --c-mem: #d6eaf6;
    --c-write: #efe0f6;
    --c-commit: #f8ebcc;
    --c-squash: #f6d6d2;
    --c-stall: #fbe5cc;
    --tag-bg: 18%;
    --tag-fg: 75%;
    --shadow: 0 4px 16px rgba(17, 24, 39, 0.10);
    color-scheme: light;
}

[data-theme='dark'] {
    --bg: #000000;
    --panel: #0d0f14;
    --panel-2: #1a1e27;
    --line: #6b7486;
    --line-strong: #9aa3b5;
    --ink: #ffffff;
    --muted: #dde2ea;
    --faint: #b4bccb;
    --accent: #5294ff;
    --accent-ink: #000000;
    --accent-soft: #0f2a5e;
    --focus: #ffd23f;
    --focus-soft: #3d3200;
    --warn: #ff9e7a;
    --ok: #7ef0ad;
    --ok-soft: #0f3a24;
    --bad-soft: #4a1d1d;
    --wire: #7c869a;
    --c-issue: #22306e;
    --c-id: #2a3140;
    --c-exec: #11452c;
    --c-mem: #0f3c57;
    --c-write: #44255a;
    --c-commit: #574300;
    --c-squash: #5c1f22;
    --c-stall: #5a3410;
    --tag-bg: 40%;
    --tag-fg: 75%;
    --shadow: 0 0 0 1px #6b7486;
    color-scheme: dark;
}

:root {
    --sans: 'IBM Plex Sans', system-ui, 'Segoe UI', Roboto, sans-serif;
    --mono: 'IBM Plex Mono', ui-monospace, 'SFMono-Regular', Consolas, monospace;
    --radius: 6px;
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    width: 100%;
    height: 100%;
    background: var(--bg);
    color: var(--ink);
    font-family: var(--sans);
    font-size: 16px;
    line-height: 1.4;
}

a {
    color: var(--accent);
}

code {
    font-family: var(--mono);
}

.hidden {
    display: none !important;
}

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

.screen {
    position: fixed;
    inset: 0;
}

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

/* Ícones: SVG usados como máscara, para seguirem a cor do texto */

.icon {
    display: inline-block;
    width: 18px;
    height: 18px;
    background: currentColor;
    -webkit-mask: var(--icon) center / contain no-repeat;
    mask: var(--icon) center / contain no-repeat;
    flex: none;
}

.i-cpu { --icon: url('../svg/cpu.svg'); }
.i-new { --icon: url('../svg/new.svg'); }
.i-close { --icon: url('../svg/close.svg'); }
.i-moon { --icon: url('../svg/moon.svg'); }
.i-sun { --icon: url('../svg/sun.svg'); }
.i-skip-back { --icon: url('../svg/skipBack.svg'); }
.i-skip-fwd { --icon: url('../svg/skipFwd.svg'); }
.i-step-back { --icon: url('../svg/stepBack.svg'); }
.i-step-fwd { --icon: url('../svg/stepFwd.svg'); }

/* Botões */

.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font: inherit;
    font-size: 0.875rem;
    color: var(--ink);
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--radius);
    padding: 5px 10px;
    cursor: pointer;
    white-space: nowrap;
}

.btn:hover {
    background: var(--panel-2);
    border-color: var(--line);
}

.btn.primary {
    background: var(--accent);
    color: var(--accent-ink);
    font-weight: 600;
}

.btn.primary:hover {
    background: color-mix(in srgb, var(--accent) 88%, var(--ink));
    border-color: transparent;
}

.btn.small {
    font-size: 0.8rem;
    padding: 2px 8px;
    border-color: var(--line);
}

.icon-btn {
    padding: 6px;
}

/* Estrutura ----------------------------------------------------------------------------------------------- */

#workspace {
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

#header {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 8px 14px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    z-index: 30;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding-right: 10px;
    border-right: 1px solid var(--line);
}

.brand-mark {
    width: 26px;
    height: 26px;
    color: var(--accent);
}

.brand-name {
    font-weight: 500;
    font-size: 0.95rem;
    letter-spacing: 0.01em;
    white-space: nowrap;
}

.brand-name b {
    font-weight: 600;
    color: var(--accent);
}

.actions {
    display: flex;
    align-items: center;
    gap: 4px;
    flex: 1;
    min-width: 0;
    overflow-x: auto;
}

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

.lang select {
    font: inherit;
    font-size: 0.85rem;
    color: var(--ink);
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 4px 6px;
}

.menu {
    position: relative;
}

.menu .menu-items {
    display: none;
    position: fixed;
    z-index: 60;
    margin-top: 4px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 4px;
    min-width: 260px;
}

.menu.open .menu-items {
    display: flex;
    flex-direction: column;
}

.menu .menu-items button {
    font: inherit;
    font-size: 0.875rem;
    text-align: left;
    color: var(--ink);
    background: none;
    border: 0;
    border-radius: 4px;
    padding: 7px 10px;
    cursor: pointer;
}

.menu .menu-items button:hover {
    background: var(--panel-2);
}

/* Abas */

#tab-names {
    flex: 0 0 auto;
    display: flex;
    gap: 2px;
    padding: 6px 12px 0;
    background: var(--bg);
    border-bottom: 1px solid var(--line);
    overflow-x: auto;
    user-select: none;
}

#tab-names .tab {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 5px 6px 5px 12px;
    font-size: 0.85rem;
    color: var(--muted);
    border: 1px solid transparent;
    border-bottom: 0;
    border-radius: var(--radius) var(--radius) 0 0;
    white-space: nowrap;
    cursor: pointer;
}

#tab-names .tab.active {
    color: var(--ink);
    background: var(--panel);
    border-color: var(--line);
    margin-bottom: -1px;
    box-shadow: inset 0 2px 0 var(--accent);
}

#tab-names .close-tab {
    display: inline-flex;
    padding: 2px;
    color: var(--faint);
    background: none;
    border: 0;
    border-radius: 4px;
    cursor: pointer;
}

#tab-names .close-tab .icon {
    width: 14px;
    height: 14px;
}

#tab-names .close-tab:hover {
    color: var(--ink);
    background: var(--panel-2);
}

#tab-filler {
    flex: 1;
}

#tab-visual {
    flex: 1 1 400px;
    position: relative;
    overflow: hidden;
    min-height: 200px;
}

/* Ajuda */

#readme {
    position: absolute;
    inset: 0;
    overflow: auto;
    z-index: 15;
    background: var(--bg);
    scroll-behavior: smooth;
}

#readme.overlay {
    display: block !important;
    z-index: 40;
}

.help {
    display: grid;
    grid-template-columns: 250px minmax(0, 1fr);
    gap: 40px;
    max-width: 1180px;
    padding: 24px 32px 80px;
}

.help-toc {
    position: sticky;
    top: 0;
    align-self: start;
    max-height: calc(100vh - 140px);
    overflow: auto;
    padding-top: 4px;
}

.help-search {
    width: 100%;
    font: inherit;
    font-size: 0.875rem;
    color: var(--ink);
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 6px 10px;
}

.help-toc-title {
    margin: 18px 0 6px;
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--faint);
}

.help-toc ol {
    list-style: none;
    margin: 0;
    padding: 0;
    counter-reset: toc;
}

.help-toc li {
    counter-increment: toc;
}

.help-toc a {
    display: block;
    padding: 5px 10px;
    border-left: 2px solid var(--line);
    color: var(--muted);
    text-decoration: none;
    font-size: 0.9rem;
}

.help-toc a::before {
    content: counter(toc) '.';
    display: inline-block;
    width: 2em;
    color: var(--faint);
    font-family: var(--mono);
    font-size: 0.85em;
}

.help-toc a:hover {
    color: var(--ink);
    background: var(--panel-2);
}

.help-toc a.current {
    color: var(--accent);
    border-left-color: var(--accent);
    font-weight: 600;
}

.help-body {
    max-width: 78ch;
    font-size: 0.95rem;
    line-height: 1.6;
}

.help-head {
    display: flex;
    gap: 16px;
    align-items: flex-start;
    justify-content: space-between;
    padding-bottom: 18px;
    border-bottom: 1px solid var(--line);
}

.help-head h1 {
    font-size: 1.7rem;
    font-weight: 600;
    margin: 0 0 6px;
}

.help-lead {
    margin: 0;
    color: var(--muted);
}

.help-close {
    display: none;
    border-color: var(--line);
}

#readme.overlay .help-close {
    display: inline-flex;
    width: 100%;
    justify-content: center;
    margin-bottom: 10px;
}

body.help-open #tab-timeline {
    display: none;
}

.help-section {
    padding-top: 10px;
}

.help-section h2 {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 30px 0 8px;
    color: var(--ink);
}

.help-section h3 {
    font-size: 1rem;
    font-weight: 600;
    margin: 22px 0 6px;
    color: var(--accent);
}

.help-section ul,
.help-section ol {
    padding-left: 22px;
}

.help-section li {
    margin: 4px 0;
}

.help-section dt {
    font-weight: 600;
    margin-top: 12px;
}

.help-section dd {
    margin: 2px 0 0 0;
    color: var(--ink);
}

.help-section table {
    border-collapse: collapse;
    margin: 10px 0 14px;
    font-size: 0.9rem;
    width: 100%;
}

.help-section th,
.help-section td {
    border-bottom: 1px solid var(--line);
    padding: 6px 10px;
    text-align: left;
    vertical-align: top;
}

.help-section th {
    background: var(--panel-2);
    font-weight: 600;
    color: var(--muted);
    font-size: 0.85rem;
}

.help-section pre {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 12px 14px;
    overflow: auto;
    font-size: 0.85rem;
    line-height: 1.5;
}

.help-section pre code {
    background: none;
    border: 0;
    padding: 0;
}

.help-section p.tip {
    background: var(--accent-soft);
    border-left: 3px solid var(--accent);
    border-radius: 4px;
    padding: 8px 12px;
}

.help-empty {
    margin-top: 30px;
    color: var(--muted);
}

kbd {
    font-family: var(--mono);
    font-size: 0.82em;
    background: var(--panel);
    border: 1px solid var(--line-strong);
    border-bottom-width: 2px;
    border-radius: 4px;
    padding: 0 5px;
    white-space: nowrap;
}

#readme code,
.dialog code,
.errors code {
    font-family: var(--mono);
    font-size: 0.9em;
    background: var(--panel-2);
    border: 1px solid var(--line);
    border-radius: 4px;
    padding: 0 4px;
}

@media (max-width: 860px) {
    .help {
        grid-template-columns: 1fr;
        gap: 16px;
        padding: 16px;
    }

    .help-toc {
        position: static;
        max-height: none;
    }
}

/* Diagrama ------------------------------------------------------------------------------------------------ */

#viewport {
    position: absolute;
    inset: 0;
    overflow: hidden;
    cursor: grab;
    display: none;
}

#viewport.visible {
    display: block;
}

#viewport.dragging {
    cursor: grabbing;
}

#diagram {
    position: relative;
    display: inline-flex;
    align-items: flex-start;
    gap: 40px;
    padding: 8px 8px 110px;
    transform-origin: 0 0;
    font-size: 14px;
    user-select: none;
}

#diagram .col {
    display: flex;
    flex-direction: column;
    gap: 20px;
    position: relative;
    z-index: 1;
}

#diagram svg.buses {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 0;
    pointer-events: none;
    overflow: visible;
}

#diagram svg.buses polyline {
    fill: none;
    stroke-width: 2.5;
    stroke-linejoin: round;
}

#diagram svg.buses .op {
    stroke: var(--wire);
}

#diagram svg.buses .op.active {
    stroke: var(--ok);
    stroke-width: 3.5;
}

#diagram svg.buses .cdb {
    stroke: var(--wire);
    stroke-dasharray: 6 4;
}

#diagram svg.buses .cdb.active {
    stroke: var(--accent);
    stroke-width: 4;
    stroke-dasharray: none;
}

/* Painéis e tabelas */

.panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 8px 10px 10px;
}

.panel.focus {
    border-color: var(--focus);
    box-shadow: 0 0 0 2px var(--focus);
}

.panel h3 {
    margin: 0 0 8px;
    font-size: 0.95rem;
    font-weight: 600;
    white-space: nowrap;
}

.panel .sub {
    color: var(--muted);
    font-weight: 400;
    font-size: 0.85em;
    margin-left: 6px;
}

.panel.stations .sub {
    display: inline-block;
    max-width: 380px;
    overflow: hidden;
    text-overflow: ellipsis;
    vertical-align: bottom;
}

.panel .note {
    margin: 6px 0 0;
    font-size: 0.85rem;
    color: var(--muted);
}

.note.warn,
.warn {
    color: var(--warn);
    font-weight: 600;
}

.panel table {
    border-collapse: collapse;
}

.panel th,
.panel td {
    border-bottom: 1px solid var(--line);
    padding: 3px 8px;
    height: 26px;
    text-align: left;
    white-space: nowrap;
}

.panel tr:first-child th {
    font-weight: 500;
    font-size: 0.78rem;
    color: var(--muted);
    background: var(--panel-2);
    border-bottom-color: var(--line-strong);
}

.panel tr:last-child td,
.panel tr:last-child th {
    border-bottom: 0;
}

.panel td.num {
    text-align: right;
    font-family: var(--mono);
    font-size: 0.95em;
}

.dim,
.panel td.dim {
    color: var(--faint);
}

.panel td.tag,
.cdb-item {
    font-family: var(--mono);
    font-weight: 600;
    text-align: center;
    background: color-mix(in srgb, var(--tag) var(--tag-bg), var(--panel));
    color: color-mix(in srgb, var(--tag) var(--tag-fg), var(--ink));
}

.panel th.tagname {
    color: color-mix(in srgb, var(--tag) var(--tag-fg), var(--ink));
    font-weight: 600;
}

.panel code {
    font-family: var(--mono);
    font-size: 0.95em;
}

.panel tr.busy td {
    background: color-mix(in srgb, var(--tag) 8%, var(--panel));
}

.panel tr.busy td.tag {
    background: color-mix(in srgb, var(--tag) var(--tag-bg), var(--panel));
}

.panel tr.focus th,
.panel tr.focus td {
    background: var(--focus-soft) !important;
}

.panel tr.mispredict td {
    color: var(--warn);
}

.panel .imm {
    font-style: italic;
    color: var(--muted);
}

.panel .abi {
    color: var(--faint);
    font-size: 0.8em;
    margin-left: 5px;
    font-weight: 400;
}

.panel table.regs {
    min-width: 220px;
}

.panel td.stage {
    min-width: 150px;
    font-size: 0.9em;
    color: var(--muted);
}

.bar {
    display: inline-block;
    width: 44px;
    height: 6px;
    margin-left: 6px;
    border-radius: 3px;
    background: var(--panel-2);
    border: 1px solid var(--line);
    vertical-align: middle;
    overflow: hidden;
}

.bar span {
    display: block;
    height: 100%;
    background: var(--accent);
}

ol.queue {
    margin: 0;
    padding: 0;
    list-style: none;
    min-width: 260px;
}

ol.queue li {
    padding: 3px 6px;
    border-bottom: 1px solid var(--line);
}

ol.queue li:last-child {
    border-bottom: 0;
}

ol.queue li.next {
    background: var(--accent-soft);
    border-radius: 4px;
    font-weight: 600;
}

.pc {
    font-family: var(--mono);
    color: var(--faint);
    font-size: 0.85em;
    margin-right: 8px;
    font-weight: 400;
}

.panel.cdb {
    min-height: 34px;
}

.panel.cdb.active {
    border-color: var(--accent);
}

.cdb-item {
    display: inline-block;
    padding: 1px 8px;
    border-radius: 10px;
    margin: 2px 4px 2px 0;
}

.ptr {
    display: inline-block;
    font-size: 0.72rem;
    padding: 0 6px;
    border-radius: 8px;
    background: var(--accent);
    color: var(--accent-ink);
    margin-right: 3px;
}

.ptr.tail {
    background: var(--muted);
    color: var(--panel);
}

/* Controle ------------------------------------------------------------------------------------------------ */

#control {
    position: absolute;
    left: 12px;
    right: 12px;
    bottom: 10px;
    display: flex;
    align-items: stretch;
    gap: 8px;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s;
    z-index: 20;
}

#control.visible {
    pointer-events: auto;
    opacity: 1;
}

.ctl-buttons,
#control-msg {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 10px;
    box-shadow: var(--shadow);
}

.ctl-buttons {
    display: flex;
    align-items: center;
    padding: 4px;
    flex: none;
}

.control-button {
    display: inline-flex;
    padding: 8px;
    color: var(--faint);
    background: none;
    border: 0;
    border-radius: 8px;
    cursor: default;
}

.control-button .icon {
    width: 20px;
    height: 20px;
}

.control-button.active {
    color: var(--ink);
    cursor: pointer;
}

.control-button.active:hover {
    background: var(--panel-2);
}

#control-counter {
    min-width: 84px;
    text-align: center;
    font-family: var(--mono);
    font-size: 0.95rem;
}

#control-msg {
    flex: 1;
    padding: 8px 14px;
    font-size: 0.95rem;
    line-height: 1.45;
    max-height: 120px;
    overflow: auto;
    opacity: 0;
    transition: opacity 0.15s;
}

#control-msg.active {
    opacity: 1;
}

#control-msg .bold {
    font-weight: 600;
}

#control-msg .italic {
    font-style: italic;
    font-family: var(--mono);
    font-size: 0.92em;
}

#control-msg .mono {
    font-family: var(--mono);
    font-weight: 500;
    background: var(--panel-2);
    border-radius: 4px;
    padding: 0 3px;
}

/* Linha do tempo ------------------------------------------------------------------------------------------ */

#tab-timeline {
    flex: 0 2 230px;
    overflow: auto;
    background: var(--panel);
    border-top: 1px solid var(--line);
}

table.timeline {
    border-collapse: separate;
    border-spacing: 0;
    user-select: none;
    font-size: 0.8rem;
}

table.timeline th,
table.timeline td {
    min-width: 50px;
    padding: 3px 5px;
    text-align: center;
    border-bottom: 1px solid var(--line);
    border-right: 1px solid var(--line);
}

table.timeline tr:first-child th {
    position: sticky;
    top: 0;
    background: var(--panel-2);
    color: var(--muted);
    font-weight: 500;
    z-index: 2;
}

table.timeline th.cur,
table.timeline td.cur {
    box-shadow: inset 2px 0 0 var(--accent), inset -2px 0 0 var(--accent);
}

table.timeline tr:first-child th.cur {
    color: var(--accent);
    font-weight: 600;
}

table.timeline .inst {
    position: sticky;
    left: 0;
    background: var(--panel);
    font-family: var(--mono);
    font-weight: 500;
    text-align: left;
    white-space: nowrap;
    z-index: 1;
}

table.timeline tr:first-child th.inst {
    z-index: 3;
    background: var(--panel-2);
    font-family: var(--sans);
}

table.timeline td.issue { background: var(--c-issue); }
table.timeline td.id { background: var(--c-id); }
table.timeline td.exec { background: var(--c-exec); }
table.timeline td.mem { background: var(--c-mem); }
table.timeline td.write { background: var(--c-write); }
table.timeline td.commit { background: var(--c-commit); }
table.timeline td.squash { background: var(--c-squash); }
table.timeline td.stall { background: var(--c-stall); font-style: italic; }
table.timeline td.lat { background: var(--c-mem); }

table.timeline tr.squashed td.inst {
    text-decoration: line-through;
    color: var(--faint);
}

table.timeline tr.mispredicted td.inst {
    color: var(--warn);
}

/* Janela de simulação ------------------------------------------------------------------------------------- */

.modal {
    display: flex;
    justify-content: center;
    align-items: center;
    background: color-mix(in srgb, var(--bg) 40%, rgba(10, 14, 22, 0.55));
    z-index: 50;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s;
}

.modal.visible {
    pointer-events: auto;
    opacity: 1;
}

.modal .dialog {
    position: relative;
    background: var(--bg);
    border: 1px solid var(--line);
    border-radius: 10px;
    box-shadow: var(--shadow);
    max-width: 96vw;
    max-height: 94vh;
    overflow: auto;
    padding: 18px 22px;
}

.modal .dialog.wide {
    width: 1200px;
}

.modal .dialog h1 {
    margin: 0 0 14px;
    font-size: 1.3rem;
    font-weight: 600;
}

.modal .close {
    position: absolute;
    top: 14px;
    right: 14px;
}

.editor-layout {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    align-items: flex-start;
}

.editor-code {
    flex: 1 1 420px;
    min-width: 0;
}

.editor-config {
    flex: 1 1 480px;
    min-width: 0;
    font-size: 0.85rem;
}

.field {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin: 5px 0;
}

.field select,
.field input[type='number'],
table.groups input,
table.exercise input {
    font: inherit;
    color: var(--ink);
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 4px;
    padding: 3px 5px;
}

.field input[type='number'] {
    width: 76px;
}

.field.check {
    justify-content: flex-start;
}

.editor-code > .field {
    justify-content: flex-start;
    margin-bottom: 8px;
}

.editor-code > .field select {
    flex: 1;
}

.code-box {
    display: flex;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    height: 54vh;
    min-height: 260px;
    overflow: hidden;
}

#novo-gutter {
    flex: 0 0 auto;
    margin: 0;
    padding: 8px 8px;
    min-width: 34px;
    font-family: var(--mono);
    font-size: 14px;
    line-height: 20px;
    text-align: right;
    color: var(--faint);
    background: var(--panel-2);
    border-right: 1px solid var(--line);
    overflow: hidden;
    user-select: none;
}

#novo-gutter .err {
    color: #fff;
    background: var(--warn);
    border-radius: 3px;
    padding: 0 3px;
}

.code-area {
    position: relative;
    flex: 1;
    overflow: hidden;
}

.code-area pre,
.code-area textarea {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 8px 10px;
    border: 0;
    font-family: var(--mono);
    font-size: 14px;
    line-height: 20px;
    white-space: pre;
    tab-size: 4;
}

.code-area pre {
    overflow: hidden;
    color: var(--ink);
    pointer-events: none;
}

.code-area textarea {
    color: transparent;
    background: transparent;
    caret-color: var(--ink);
    outline: none;
    resize: none;
    overflow: auto;
}

.code-area textarea::selection {
    background: color-mix(in srgb, var(--accent) 30%, transparent);
    color: transparent;
}

.hl-com { color: var(--faint); font-style: italic; }
.hl-op { color: var(--accent); font-weight: 600; }
.hl-reg { color: #8a4bc2; }
.hl-num { color: #b4532a; }
.hl-label { color: #a8650a; font-weight: 600; }
.hl-dir { color: #1d7fa6; font-weight: 600; }
.hl-str { color: var(--ok); }
.hl-bad { color: var(--warn); text-decoration: underline wavy var(--warn); }
.hl-err { background: color-mix(in srgb, var(--warn) 14%, transparent); text-decoration: underline wavy var(--warn); }

[data-theme='dark'] .hl-reg { color: #dcb6ff; }
[data-theme='dark'] .hl-num { color: #ffb68a; }
[data-theme='dark'] .hl-label { color: #ffd479; }
[data-theme='dark'] .hl-dir { color: #8fdcff; }

ul.errors {
    list-style: none;
    margin: 8px 0 0;
    padding: 0;
    max-height: 120px;
    overflow: auto;
    color: var(--warn);
    font-size: 0.85rem;
}

ul.errors li {
    padding: 2px 0;
    cursor: pointer;
}

fieldset {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--panel);
    margin: 0 0 12px;
    padding: 6px 14px 10px;
}

legend {
    font-weight: 600;
    padding: 0 4px;
}

table.groups {
    border-collapse: collapse;
    width: 100%;
    margin-bottom: 6px;
}

table.groups th {
    font-weight: 500;
    color: var(--muted);
    text-align: left;
    font-size: 0.9em;
}

table.groups td {
    padding: 4px 4px 4px 0;
    vertical-align: top;
    border-top: 1px solid var(--line);
}

table.groups input[type='number'] {
    width: 48px;
}

table.groups td.classes label,
table.groups td.units label {
    display: inline-flex;
    gap: 3px;
    align-items: center;
    margin-right: 8px;
    white-space: nowrap;
}

.latencies {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
    column-gap: 18px;
}

.dialog-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 14px;
}

/* Exercício e comparação ---------------------------------------------------------------------------------- */

#sheet {
    position: absolute;
    inset: 0;
    overflow: auto;
    z-index: 16;
    background: var(--bg);
}

.sheet-inner {
    padding: 14px 24px 48px;
    font-size: 0.9rem;
}

.sheet-inner h2 {
    font-size: 1.3rem;
    font-weight: 600;
    margin: 4px 0 8px;
}

.sheet-inner > p {
    max-width: 80ch;
    color: var(--muted);
}

.sheet-inner .panel {
    margin-bottom: 14px;
}

.ex-grid,
.compare-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(380px, 1fr));
    gap: 14px;
}

pre.code {
    font-family: var(--mono);
    font-size: 0.9em;
    margin: 0;
    max-height: 320px;
    overflow: auto;
}

ul.summary {
    margin: 0;
    padding-left: 18px;
}

ul.summary li {
    margin: 2px 0;
}

table.exercise input {
    width: 64px;
    text-align: center;
}

table.exercise input.right {
    background: var(--ok-soft);
    border-color: var(--ok);
}

table.exercise input.wrong {
    background: var(--bad-soft);
    border-color: var(--warn);
}

table.exercise input.revealed {
    background: var(--accent-soft);
    border-color: var(--accent);
}

.ex-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    align-items: center;
    margin-top: 12px;
}

.ex-actions .btn:not(.primary) {
    border-color: var(--line);
}

.score {
    font-weight: 600;
    color: var(--accent);
    margin-left: 6px;
}

p.big {
    font-size: 1.15rem;
    font-weight: 600;
    color: var(--accent);
}

.compare-timeline {
    overflow: auto;
    max-height: 460px;
    margin-top: 8px;
    border: 1px solid var(--line);
    border-radius: 4px;
}

/* Pipeline ------------------------------------------------------------------------------------------------ */

.pipe-wrap {
    display: flex;
    flex-direction: column;
    gap: 20px;
    position: relative;
    z-index: 1;
}

.pipe-head {
    display: flex;
    gap: 14px;
    align-items: baseline;
}

.pipe-head .sub {
    color: var(--muted);
}

.pcbox {
    font-family: var(--mono);
    font-weight: 600;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 2px 10px;
}

.pcbox.focus {
    border-color: var(--focus);
    box-shadow: 0 0 0 2px var(--focus);
}

.pipe-row {
    display: flex;
    align-items: stretch;
}

.panel.stage {
    width: 190px;
    min-height: 170px;
}

.panel.stage.busy {
    background: color-mix(in srgb, var(--tag) 9%, var(--panel));
    border-color: color-mix(in srgb, var(--tag) 55%, var(--line));
}

.panel.stage h3 {
    font-size: 1.1rem;
}

.latch {
    width: 24px;
    margin: 0 4px;
    background: repeating-linear-gradient(0deg, var(--line), var(--line) 5px, var(--panel-2) 5px, var(--panel-2) 10px);
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.latch span {
    writing-mode: vertical-rl;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--muted);
}

.pinst {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 4px;
    padding: 3px 6px;
    margin-bottom: 6px;
}

.pinst.stalled {
    outline: 2px dashed var(--warn);
}

.kv {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    font-size: 0.9em;
    color: var(--muted);
}

.kv b {
    font-family: var(--mono);
    font-weight: 500;
    color: var(--ink);
}

p.bubble {
    color: var(--faint);
    font-style: italic;
    text-align: center;
    margin-top: 40px;
}

.pipe-units {
    display: flex;
    gap: 20px;
    flex-wrap: wrap;
}

.pipe-units .panel {
    min-width: 320px;
}

ul.fwd {
    margin: 0;
    padding-left: 18px;
    font-family: var(--mono);
}

.pipe-bottom,
.single-wrap {
    display: flex;
    gap: 20px;
    align-items: flex-start;
    flex-wrap: wrap;
}

.single-side {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

td.on {
    color: var(--accent);
    font-weight: 600;
}

@media (max-width: 760px) {
    #header {
        flex-wrap: wrap;
        gap: 8px;
    }

    .brand {
        border-right: 0;
    }

    .brand-name {
        display: none;
    }

    .code-box {
        height: 40vh;
    }

    #control {
        flex-direction: column;
    }
}

@media (prefers-reduced-motion: reduce) {
    * {
        transition: none !important;
    }
}

.panel table.perf {
    width: 100%;
}

.panel table.perf th,
.panel table.perf td {
    white-space: normal;
}

.panel table.perf tr.total th,
.panel table.perf tr.total td {
    border-top: 1px solid var(--line-strong);
}

.panel table.perf td.num {
    white-space: nowrap;
}

/* Processador vetorial ------------------------------------------------------------------------------------ */

.vec-wrap {
    display: flex;
    flex-direction: column;
    gap: 20px;
    position: relative;
    z-index: 1;
}

.vec-row {
    display: flex;
    gap: 20px;
    align-items: flex-start;
}

.vec-side {
    display: flex;
    flex-direction: column;
    gap: 20px;
    width: 300px;
    flex: none;
}

.vec-units {
    display: flex;
    gap: 20px;
    align-items: flex-start;
}

.vec-row > *,
.vec-units > .panel {
    flex: none;
}

.panel.unit {
    min-width: 190px;
}

.panel.vregs .note {
    max-width: 560px;
}

.panel.unit.busy {
    border-color: var(--line-strong);
}

.panel p.dim,
p.dim {
    color: var(--faint);
    font-style: italic;
    margin: 4px 0;
}

.note.ok {
    color: var(--ok);
}

table.lanes {
    border-collapse: separate;
    border-spacing: 2px;
    margin: 4px 0 6px;
}

.panel table.lanes th {
    font-size: 0.75rem;
    color: var(--muted);
    font-weight: 500;
    text-align: center;
    padding: 0 4px;
    border: 0;
}

.panel table.lanes th.lane {
    border-left: 4px solid var(--lane);
    text-align: left;
}

.panel table.lanes td {
    width: 26px;
    min-width: 26px;
    height: 24px;
    text-align: center;
    font-family: var(--mono);
    font-size: 0.8rem;
    background: var(--panel-2);
    border: 1px solid var(--line);
    border-radius: 3px;
    padding: 0;
}

.panel table.lanes td.el {
    background: color-mix(in srgb, var(--tag) calc(var(--tag-bg) + 10%), var(--panel));
    border-color: color-mix(in srgb, var(--tag) 70%, var(--line));
    color: var(--ink);
    font-weight: 600;
}

ul.ops {
    list-style: none;
    margin: 4px 0 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

ul.ops li {
    font-size: 0.9em;
}

ul.ops .sub {
    display: block;
    margin-left: 0;
    color: var(--muted);
}

code.tagged {
    border-left: 4px solid var(--tag);
    padding-left: 6px;
}

.panel.issue .pinst {
    border-left: 4px solid var(--tag);
}

.panel.vregs {
    align-self: flex-start;
    max-width: 100%;
}

.panel table.vregs {
    border-collapse: separate;
    border-spacing: 2px;
}

.panel table.vregs th {
    text-align: left;
    white-space: nowrap;
    padding-right: 8px;
}

.panel table.vregs td {
    min-width: 46px;
    text-align: right;
    font-family: var(--mono);
    font-size: 0.82rem;
    padding: 2px 5px;
    background: var(--panel-2);
    border: 0;
    border-top: 3px solid var(--lane);
    border-radius: 2px;
}

.panel table.vregs td.tail {
    opacity: 0.45;
}

.panel table.vregs td.new {
    background: var(--focus-soft);
    outline: 2px solid var(--focus);
    font-weight: 600;
}

.panel table.vregs tr.focus th {
    color: var(--accent);
}

/* TPU ----------------------------------------------------------------------------------------------------- */

.tpu-core {
    display: flex;
    flex-direction: column;
    gap: 20px;
    flex: none;
}

.panel.unit.small {
    min-width: 0;
}

.panel.systolic .note,
.panel.fifo .note {
    max-width: 520px;
}

.panel.systolic.busy {
    border-color: var(--line-strong);
}

.panel table.systolic {
    border-collapse: separate;
    border-spacing: 4px;
}

.panel table.systolic th {
    font-size: 0.75rem;
    color: var(--muted);
    font-weight: 500;
    text-align: center;
    border: 0;
}

.panel table.systolic th.inq {
    text-align: right;
    min-width: 96px;
    white-space: nowrap;
}

.panel table.systolic td.pe {
    width: 82px;
    height: 62px;
    vertical-align: top;
    background: var(--panel-2);
    border: 1px solid var(--line);
    border-radius: 5px;
    padding: 3px 5px;
    font-family: var(--mono);
    font-size: 0.78rem;
    position: relative;
}

.panel table.systolic td.pe.on {
    background: color-mix(in srgb, var(--tag) calc(var(--tag-bg) + 6%), var(--panel));
    border-color: color-mix(in srgb, var(--tag) 70%, var(--line));
}

.panel table.systolic td.pe span {
    display: block;
    white-space: nowrap;
}

.panel table.systolic .w {
    color: var(--muted);
    font-size: 0.72rem;
}

.panel table.systolic .w::before {
    content: 'w ';
}

.panel table.systolic .x {
    font-weight: 600;
    color: var(--ink);
}

.panel table.systolic .ps {
    color: var(--accent);
}

.panel table.systolic td.out {
    text-align: center;
    font-family: var(--mono);
    font-weight: 600;
    font-size: 0.8rem;
    border: 0;
    color: color-mix(in srgb, var(--tag) 70%, var(--ink));
}

.panel table.systolic td.out .sub {
    display: block;
    margin: 0;
    font-weight: 400;
}

.panel table.systolic .q {
    display: inline-block;
    min-width: 22px;
    margin-left: 3px;
    padding: 1px 4px;
    border-radius: 3px;
    text-align: center;
    font-family: var(--mono);
    font-size: 0.75rem;
    color: var(--ink);
    background: color-mix(in srgb, var(--tag) calc(var(--tag-bg) + 6%), var(--panel));
}

.panel table.buf td {
    min-width: 44px;
    font-family: var(--mono);
    text-align: right;
}

.panel table.buf tr.new td {
    background: var(--focus-soft);
    font-weight: 600;
}

/* GPU ----------------------------------------------------------------------------------------------------- */

.panel.gunits .gunit {
    border-top: 1px solid var(--line);
    padding: 6px 0;
}

.panel.gunits .gunit.focus {
    outline: 2px solid var(--focus);
    border-radius: 4px;
}

.panel table.warps td,
.panel table.warps th {
    vertical-align: top;
    white-space: nowrap;
}

.panel table.warps tr td:first-of-type,
.panel table.warps th.wid {
    border-left: 4px solid var(--tag);
}

.panel table.warps tr.done td {
    opacity: 0.5;
}

.panel table.warps tr.focus td {
    background: var(--focus-soft);
}

.panel table.mask {
    border-collapse: separate;
    border-spacing: 2px;
}

.panel table.mask td {
    width: 16px;
    padding: 1px 0;
    text-align: center;
    font-family: var(--mono);
    font-size: 0.75rem;
    border: 0;
    border-radius: 2px;
}

.panel table.mask td.on {
    background: color-mix(in srgb, var(--tag) calc(var(--tag-bg) + 12%), var(--panel));
    font-weight: 600;
}

.panel table.mask td.off {
    background: var(--panel-2);
    color: var(--faint);
}

.panel td.wstate.ready,
.panel td.wstate.issued {
    color: var(--ok);
}

.panel td.wstate.dep,
.panel td.wstate.unit,
.panel td.wstate.branch,
.panel td.wstate.bar {
    color: var(--warn);
}

.panel td.wstack {
    font-family: var(--mono);
    font-size: 0.78rem;
}

.panel td.wstack div.stop {
    font-weight: 600;
}

.panel table.coal td {
    font-family: var(--mono);
    font-size: 0.75rem;
    text-align: center;
    padding: 2px 4px;
    background: color-mix(in srgb, var(--tag) calc(var(--tag-bg) + 6%), var(--panel));
}

.panel table.coal td.off {
    background: var(--panel-2);
    color: var(--faint);
}

.panel table.coal td .sub {
    display: block;
    margin: 0;
}

.panel.threads {
    align-self: flex-start;
    max-width: 100%;
}

.panel table.tregs th {
    white-space: nowrap;
}

.panel table.tregs tr:first-child th {
    border-bottom: 3px solid var(--tag);
    text-align: center;
    min-width: 34px;
}

.panel table.tregs td {
    font-family: var(--mono);
    font-size: 0.78rem;
    text-align: right;
    padding: 2px 4px;
}

.panel table.tregs .wstart {
    border-left: 2px solid var(--line-strong);
}

.panel table.tregs td.new {
    background: var(--focus-soft);
    font-weight: 600;
}

/* GPU: blocos da grade e memória compartilhada */
.gblock-row {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.gblock {
    padding: 2px 6px;
    border: 1px solid var(--line-strong);
    border-radius: 4px;
    font-family: var(--mono, monospace);
    font-size: 0.8rem;
    color: var(--muted);
}

.gblock.running {
    background: var(--accent-soft);
    border-color: var(--accent);
    color: var(--ink);
}

.gblock.done {
    background: var(--ok-soft);
    opacity: 0.7;
}

.smem-row {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    align-items: flex-start;
}

.panel.gblocks, .panel.smem {
    flex: none;
}

/* Comparação entre modelos */
.models-tabs {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin: 8px 0 12px;
}

table.models th .sub {
    display: block;
    font-weight: normal;
}

table.models tr.best th:first-child {
    color: var(--ok);
}

table.models td.bar-cell {
    width: 30%;
    min-width: 120px;
}

.mbar {
    display: block;
    height: 12px;
    border-radius: 3px;
    background: var(--accent);
}

.mbar-gpu {
    background: var(--ok);
}

.mbar-tpu {
    background: var(--focus);
}

table.sbanks td.lab {
    box-shadow: inset 2px 0 0 var(--accent);
}

table.models td.metric {
    font-size: 0.85rem;
    color: var(--muted);
}

/* Perguntas do exercício */
ol.questions {
    display: grid;
    gap: 8px;
    padding-left: 1.4em;
}

ol.questions li span {
    margin-right: 8px;
}

ol.questions input.qans {
    width: 9em;
    font: inherit;
    font-family: var(--mono, monospace);
    color: var(--ink);
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 4px;
    padding: 3px 5px;
}

ol.questions input.right {
    background: var(--ok-soft);
    border-color: var(--ok);
}

ol.questions input.wrong {
    background: var(--bad-soft);
    border-color: var(--warn);
}

ol.questions input.revealed {
    background: var(--accent-soft);
    border-color: var(--accent);
}

/* Diagrama de blocos do processador vetorial ---------------------------------------------------------------- */

.panel.vec-diagram {
    overflow-x: auto;
}

.vec-svg {
    display: block;
    font-family: var(--sans, system-ui, sans-serif);
    font-size: 12px;
}

.vec-svg text {
    fill: var(--ink);
}

.vec-svg text.title {
    font-size: 14px;
    font-weight: 600;
}

.vec-svg text.small {
    font-size: 11.5px;
}

.vec-svg text.tiny {
    font-size: 10px;
}

.vec-svg text.idx {
    font-size: 8.5px;
}

.vec-svg text.mono {
    font-family: var(--mono);
    font-size: 11px;
}

.vec-svg text.strong {
    font-weight: 600;
}

.vec-svg text.center {
    text-anchor: middle;
}

.vec-svg text.end {
    text-anchor: end;
}

.vec-svg text.dim {
    fill: var(--faint);
}

.vec-svg text.warn {
    fill: var(--warn);
    font-weight: 600;
}

.vec-svg text.ok {
    fill: var(--ok);
}

.vec-svg text.hl {
    fill: var(--focus);
}

.vec-svg rect {
    fill: var(--panel);
    stroke: var(--line-strong);
    stroke-width: 1;
}

.vec-svg rect.unitbox {
    fill: var(--panel);
    stroke: var(--line-strong);
}

.vec-svg .blk.focus rect.unitbox {
    stroke: var(--focus);
    stroke-width: 2.5;
    fill: var(--focus-soft);
}

.vec-svg rect.lane {
    fill: color-mix(in srgb, var(--bg) 60%, var(--panel));
    stroke: var(--line-strong);
}

.vec-svg rect.cell {
    fill: var(--panel);
    stroke: var(--line);
}

.vec-svg rect.cell.tail {
    fill: color-mix(in srgb, var(--bg) 70%, var(--panel));
}

.vec-svg rect.cell.new {
    fill: var(--focus-soft);
    stroke: var(--focus);
    stroke-width: 2;
}

.vec-svg g.focus rect.cell {
    stroke: var(--focus);
}

.vec-svg rect.pipe {
    fill: transparent;
    stroke: var(--line);
    stroke-dasharray: 3 2;
}

.vec-svg .unit.busy rect.pipe {
    stroke: var(--line-strong);
    stroke-dasharray: none;
}

.vec-svg rect.pipe.focus {
    stroke: var(--focus);
    stroke-width: 2;
    stroke-dasharray: none;
}

.vec-svg rect.stage {
    fill: var(--panel);
    stroke: var(--line);
}

.vec-svg rect.membox {
    fill: var(--c-mem);
    stroke: var(--line-strong);
}

.vec-svg rect.membox.on {
    stroke: var(--accent);
    stroke-width: 2;
}

.vec-svg .wire {
    fill: none;
    stroke: var(--wire);
    stroke-width: 1.5;
}

.vec-svg .wire.bus {
    stroke-width: 3;
}

.vec-svg .head {
    fill: var(--wire);
    stroke: none;
}

.vec-svg .wire.on {
    stroke: var(--accent);
}

.vec-svg .head.on {
    fill: var(--accent);
}

.vec-svg rect.inst {
    stroke-width: 1.5;
}

/* Diagrama de blocos da GPU */

.vec-svg rect.rowsel {
    stroke: none;
}

.vec-svg rect.rowsel.focus {
    stroke: var(--focus);
    stroke-width: 2;
}

.vec-svg rect.mbit {
    fill: var(--panel);
    stroke: var(--line-strong);
}

.vec-svg rect.regs {
    fill: color-mix(in srgb, var(--accent) 8%, var(--panel));
    stroke: var(--line-strong);
}

.vec-svg rect.alu {
    fill: var(--panel);
    stroke: var(--line);
    stroke-dasharray: 3 2;
}

.vec-svg rect.alu.on {
    stroke-dasharray: none;
    stroke-width: 1.5;
}

.vec-svg rect.alu.off {
    fill: color-mix(in srgb, var(--bg) 70%, var(--panel));
    stroke: var(--line-strong);
    stroke-dasharray: none;
}

.vec-svg rect.alu.focus {
    stroke: var(--focus);
    stroke-width: 2;
}

.vec-svg rect.lsubox,
.vec-svg rect.netbox {
    fill: color-mix(in srgb, var(--bg) 60%, var(--panel));
    stroke: var(--line-strong);
}

.vec-svg rect.lsubox.on {
    stroke: var(--accent);
    stroke-width: 2;
}

.vec-svg rect.lsubox.focus {
    stroke: var(--focus);
    stroke-width: 2.5;
}

.vec-svg rect.cell.lab {
    stroke: var(--accent);
}

.vec-svg rect.gblk.pending {
    fill: var(--panel);
}

.vec-svg rect.gblk.running {
    fill: var(--c-exec);
    stroke: var(--ok);
}

.vec-svg rect.gblk.done {
    fill: color-mix(in srgb, var(--bg) 70%, var(--panel));
}

/* Diagrama de blocos da TPU */

.vec-svg rect.unitbox.on {
    stroke: var(--accent);
    stroke-width: 2;
    fill: var(--accent-soft);
}

.vec-svg rect.lane.on {
    stroke: var(--accent);
    stroke-width: 1.5;
}

.vec-svg rect.pe {
    fill: var(--panel);
    stroke: var(--line-strong);
}

.vec-svg rect.pe.on {
    stroke-width: 1.5;
}
