:root {
    color-scheme: dark;
    --bg: #0d1117;
    --panel: rgba(18, 24, 32, 0.92);
    --panel-strong: rgba(24, 31, 41, 0.98);
    --line: rgba(218, 232, 224, 0.18);
    --text: #eef5f7;
    --muted: #9eacb4;
    --accent: #42c6ff;
    --gold: #f2c464;
    --green: #3ee68a;
    --red: #ff7373;
    --black-stone: #1a2028;
    --white-stone: #eef2ef;
}

* { box-sizing: border-box; }

[hidden] {
    display: none !important;
}

body {
    margin: 0;
    min-height: 100vh;
    overflow-x: hidden;
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background:
        linear-gradient(rgba(13, 17, 23, 0.9), rgba(13, 17, 23, 0.95)),
        radial-gradient(circle at 30% 20%, rgba(66, 198, 255, 0.15), transparent 28%),
        radial-gradient(circle at 76% 75%, rgba(242, 196, 100, 0.11), transparent 32%),
        var(--bg);
}

body.modal-open {
    overflow: hidden;
}

button, select, input, textarea {
    font: inherit;
}

.app-shell {
    width: min(1440px, 100%);
    max-width: 100vw;
    margin: 0 auto;
    padding: clamp(14px, 2.4vw, 30px);
    display: grid;
    gap: 16px;
    overflow-x: clip;
}

.top-bar {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: center;
    gap: 18px;
}

.home-link, button, select, input {
    border: 1px solid rgba(66, 198, 255, 0.38);
    border-radius: 7px;
    background: rgba(10, 15, 22, 0.76);
    color: var(--text);
}

.home-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 34px;
    padding: 8px 12px;
    border: 1px solid rgba(97, 174, 255, 0.42);
    border-radius: 999px;
    background: rgba(5, 12, 24, 0.92);
    color: #eef5ff;
    text-decoration: none;
    font-weight: 900;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.28);
}

.eyebrow {
    margin: 0 0 5px;
    color: var(--gold);
    font-size: 12px;
    font-weight: 900;
    text-transform: uppercase;
}

h1, h2 {
    margin: 0;
    letter-spacing: 0;
}

h1 {
    font-size: clamp(28px, 4vw, 54px);
    line-height: 0.98;
}

h2 {
    font-size: 14px;
}

.simple-research-mode-toggle {
    display: inline-grid;
    grid-template-columns: repeat(2, minmax(78px, 1fr));
    gap: 4px;
    padding: 4px;
    border: 1px solid rgba(125, 211, 252, 0.32);
    border-radius: 8px;
    background: rgba(5, 12, 24, 0.76);
}

.simple-research-mode-toggle button {
    min-height: 34px;
    padding: 6px 10px;
    border-color: transparent;
    background: transparent;
    color: var(--muted);
    font-size: 13px;
    font-weight: 950;
    cursor: pointer;
}

.simple-research-mode-toggle button[aria-pressed="true"] {
    border-color: rgba(66, 198, 255, 0.54);
    background: rgba(66, 198, 255, 0.14);
    color: #f8fafc;
}

.labs-home,
.lab-model-header,
.lab-left-panels,
.lab-info-panel {
    min-width: 0;
}

.labs-home,
.lab-model-header {
    display: grid;
    gap: 12px;
    padding: 14px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(15, 20, 27, 0.9);
    box-shadow: 0 18px 54px rgba(0, 0, 0, 0.2);
}

.labs-home-header {
    display: grid;
    grid-template-columns: minmax(0, 0.8fr) minmax(0, 1.2fr);
    gap: 16px;
    align-items: end;
}

.labs-home-header h2,
.lab-model-header h2 {
    margin: 0;
    color: #f8fafc;
    font-size: clamp(22px, 2.8vw, 34px);
    line-height: 1.05;
}

.labs-home-header p,
.lab-model-heading p:last-child {
    margin: 0;
    color: var(--muted);
    line-height: 1.5;
}

.labs-model-catalog {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
    gap: 10px;
}

.labs-model-card {
    display: grid;
    gap: 8px;
    min-width: 0;
    min-height: 154px;
    padding: 12px;
    border: 1px solid rgba(148, 163, 184, 0.18);
    border-radius: 8px;
    background: rgba(2, 6, 23, 0.28);
    color: inherit;
    text-align: left;
    cursor: pointer;
}

.labs-model-card:hover,
.labs-model-card:focus-visible,
.labs-model-card.is-active {
    border-color: rgba(125, 211, 252, 0.65);
    background: rgba(14, 31, 43, 0.72);
    outline: none;
}

.labs-model-card span {
    color: #7dd3fc;
    font-size: 11px;
    font-weight: 950;
    text-transform: uppercase;
}

.labs-model-card strong {
    color: #fff;
    font-size: 16px;
    line-height: 1.2;
}

.labs-model-card small {
    color: var(--muted);
    line-height: 1.35;
}

.labs-card-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.labs-card-meta em,
.lab-header-badges span,
.validation-level-badge {
    display: inline-flex;
    align-items: center;
    min-height: 24px;
    padding: 3px 7px;
    border: 1px solid rgba(148, 163, 184, 0.2);
    border-radius: 999px;
    background: rgba(15, 23, 42, 0.66);
    color: #dbeafe;
    font-size: 11px;
    font-style: normal;
    font-weight: 900;
}

.validation-level-badge[data-level="toy"] { color: #fde68a; }
.validation-level-badge[data-level="estimator"] { color: #bfdbfe; }
.validation-level-badge[data-level="benchmarked"] { color: #bbf7d0; }
.validation-level-badge[data-level="research_grade"] { color: #f0fdfa; }

.lab-model-header {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
}

.lab-model-heading {
    min-width: 0;
}

.lab-header-badges {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 6px;
}

.lab-workspace {
    display: grid;
    grid-template-columns: minmax(0, 0.72fr) minmax(0, 1.2fr) minmax(0, 0.78fr);
    gap: 16px;
    align-items: start;
    min-width: 0;
}

.lab-left-panels {
    display: grid;
    gap: 10px;
}

.lab-info-panel,
.research-notes-panel {
    border: 1px solid rgba(218, 232, 224, 0.13);
    border-radius: 8px;
    background: rgba(15, 20, 27, 0.9);
}

.lab-info-panel summary {
    padding: 12px;
    color: var(--gold);
    font-size: 11px;
    font-weight: 1000;
    text-transform: uppercase;
    cursor: pointer;
}

.lab-info-panel dl,
.research-notes-panel dl {
    display: grid;
    gap: 9px;
    margin: 0;
    padding: 0 12px 12px;
}

.lab-info-panel dl div,
.research-notes-panel dl div {
    display: grid;
    gap: 3px;
}

.lab-info-panel dt,
.research-notes-panel dt {
    color: var(--muted);
    font-size: 11px;
    font-weight: 900;
    text-transform: uppercase;
}

.lab-info-panel dd,
.research-notes-panel dd {
    margin: 0;
    color: #d8e3ec;
    font-size: 13px;
    line-height: 1.45;
    overflow-wrap: anywhere;
}

.visualization-panel-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
    color: var(--gold);
    font-size: 11px;
    font-weight: 1000;
    text-transform: uppercase;
}

.visualization-panel-title button {
    width: auto;
    min-height: 30px;
    padding: 5px 10px;
    font-size: 12px;
    font-weight: 900;
    cursor: pointer;
}

.turn-card, .stat-grid div {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    padding: 10px 12px;
}

.turn-card span, .stat-grid span {
    display: block;
    color: var(--muted);
    font-size: 11px;
    font-weight: 900;
    text-transform: uppercase;
}

.turn-card strong, .stat-grid strong {
    display: block;
    margin-top: 3px;
    font-size: 20px;
}

.control-panel, .layer-panel {
    display: grid;
    grid-template-columns: repeat(12, minmax(0, 1fr));
    gap: 10px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
}

.control-section {
    display: grid;
    align-content: start;
    gap: 9px;
    min-width: 0;
    padding-right: 10px;
    border-right: 1px solid rgba(218, 232, 224, 0.1);
}

.online-status {
    margin: 10px 0 0;
    color: var(--muted);
    font-size: 13px;
}

.connection-status {
    grid-column: 1 / -1;
    padding: 9px;
    border-radius: 8px;
    font-weight: 900;
    text-align: center;
}

.connection-status.disconnected { background: rgba(239, 68, 68, 0.16); color: #fecaca; }
.connection-status.connecting { background: rgba(245, 158, 11, 0.16); color: #fde68a; }
.connection-status.connected { background: rgba(34, 197, 94, 0.16); color: #bbf7d0; }

.online-button-grid {
    padding: 12px;
    border: 1px solid rgba(34, 197, 94, 0.55);
    border-radius: 8px;
    background: rgba(34, 197, 94, 0.09);
}

.online-button-grid #onlineFindButton { border-color: rgba(245, 158, 11, 0.82); }
.online-button-grid #onlineCreateButton { border-color: rgba(34, 197, 94, 0.7); }
.online-button-grid #onlineJoinButton { border-color: rgba(56, 189, 248, 0.7); }

.divider-text {
    grid-column: 1 / -1;
    color: var(--muted);
    font-size: 11px;
    font-weight: 900;
    text-align: center;
    text-transform: uppercase;
}

.room-info {
    grid-column: 1 / -1;
    display: grid;
    gap: 8px;
    padding: 9px;
    border: 1px solid rgba(224, 232, 220, 0.14);
    border-radius: 7px;
}

.room-info[hidden] { display: none; }

.room-id-display {
    font-size: 22px;
    font-weight: 950;
    letter-spacing: 0.12em;
    color: var(--accent);
}

.share-link-box {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 80px;
    gap: 6px;
}

.online-button-grid,
.room-info,
.share-link-box {
    min-width: 0;
    max-width: 100%;
    box-sizing: border-box;
    overflow: hidden;
}

.room-id-display {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    letter-spacing: 0.08em;
}

.share-link-input {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
}

.online-chat {
    display: grid;
    gap: 8px;
    margin-top: 4px;
}

.online-chat h3 {
    margin: 0;
    color: var(--text);
    font-size: 13px;
}

.online-chat-messages {
    min-height: 88px;
    max-height: 180px;
    overflow-y: auto;
    padding: 8px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: rgba(5, 10, 16, 0.46);
}

.online-chat-empty {
    color: var(--muted);
    font-size: 12px;
}

.online-chat-message {
    margin-bottom: 7px;
}

.online-chat-message span {
    color: var(--accent);
    font-size: 10px;
    font-weight: 900;
    text-transform: uppercase;
}

.online-chat-message p {
    margin: 2px 0 0;
    overflow-wrap: anywhere;
}

.online-chat-message.mine {
    text-align: right;
}

.online-chat-compose {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
}

.online-button-grid[hidden] {
    display: none;
}

.control-section h2 {
    color: var(--gold);
    font-size: 11px;
    font-weight: 1000;
    text-transform: uppercase;
}

.section-game {
    grid-column: span 3;
    order: 1;
}

.section-board {
    grid-column: span 3;
    order: 2;
}

.section-dynamics {
    grid-column: span 3;
    order: 3;
}

.section-algebra {
    grid-column: 1 / -1;
    order: 6;
    padding: 12px 0 0;
    border-top: 1px solid rgba(218, 232, 224, 0.1);
    border-right: 0;
}

.section-actions {
    grid-column: span 3;
    order: 4;
    padding-right: 0;
    border-right: 0;
}

.section-online {
    grid-column: 1 / -1;
    order: 5;
    padding: 12px 0 0;
    border-top: 1px solid rgba(218, 232, 224, 0.1);
    border-right: 0;
}

.section-online .control-fields {
    grid-template-columns: minmax(140px, 220px) minmax(220px, 1fr);
}

.section-online .online-button-grid {
    width: min(100%, 820px);
    grid-template-columns: repeat(3, minmax(150px, 1fr));
    align-items: center;
    gap: 8px;
    padding: 10px;
}

.section-online .connection-status,
.section-online .room-info {
    grid-column: 1 / -1;
}

.section-online .divider-text {
    display: none;
}

.section-online #onlineFindButton,
.section-online #onlineCreateButton,
.section-online #onlineJoinButton,
.section-online #onlineReconnectButton,
.section-online #onlineLeaveButton {
    min-height: 42px;
}

.control-fields {
    display: grid;
    gap: 8px;
}

.control-fields.two-col {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.control-fields.three-col {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.mini-start-button {
    align-self: end;
    justify-self: start;
    min-height: 34px;
    padding: 7px 13px;
    border-color: rgba(62, 230, 138, 0.65);
    color: #d8ffe8;
    font-size: 12px;
    font-weight: 1000;
}

.section-algebra > .algebra-mode-fields {
    grid-template-columns: repeat(4, minmax(160px, 1fr));
}

.algebra-mode-fields > .control-details {
    grid-column: 1 / -1;
}

.button-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
}

.control-details {
    display: grid;
    gap: 8px;
}

.control-details[hidden] {
    display: none;
}

.control-details summary {
    cursor: pointer;
    color: var(--muted);
    font-size: 11px;
    font-weight: 900;
    text-transform: uppercase;
}

.control-details[open] summary {
    color: var(--text);
}

.control-details > .control-fields {
    margin-top: 7px;
}

.performance-controls {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
}

.performance-toggle {
    display: flex;
    min-height: 42px;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    border: 1px solid var(--line);
    border-radius: 6px;
}

.performance-toggle input {
    width: 18px;
    height: 18px;
    margin: 0;
}

@media (max-width: 620px) {
    .performance-controls {
        grid-template-columns: 1fr;
    }
}

.rules-intro-panel {
    position: fixed;
    inset: 0;
    z-index: 160;
    display: none;
    place-items: center;
    padding: clamp(12px, 3vw, 32px);
    background: rgba(2, 6, 12, 0.78);
    backdrop-filter: blur(8px);
}

.rules-intro-panel[aria-hidden="true"] {
    display: none;
}

.rules-intro-panel.is-open {
    display: grid;
}

.rules-intro-dialog {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    width: min(1120px, 94vw);
    height: min(860px, 88vh);
    border: 1px solid rgba(125, 211, 252, 0.46);
    border-radius: 8px;
    background:
        linear-gradient(180deg, rgba(20, 31, 44, 0.98), rgba(7, 11, 17, 0.98));
    box-shadow: 0 24px 80px rgba(0, 0, 0, 0.58);
    overflow: hidden;
}

.rules-intro-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 16px clamp(16px, 3vw, 28px);
    border-bottom: 1px solid rgba(125, 211, 252, 0.24);
    background: rgba(15, 23, 34, 0.92);
}

.rules-intro-header > div {
    display: grid;
    gap: 4px;
}

.rules-intro-header span {
    color: var(--muted);
    font-size: 12px;
    font-weight: 900;
    text-transform: uppercase;
}

.rules-intro-header strong {
    color: var(--text);
    font-size: clamp(20px, 3vw, 34px);
    line-height: 1.08;
}

.rules-intro-header button {
    width: auto;
    min-width: 96px;
    padding: 0 16px;
    border-color: rgba(248, 113, 113, 0.55);
    color: #fecaca;
}

.rules-intro-content {
    min-height: 0;
    overflow: auto;
    padding: clamp(18px, 3vw, 34px);
}

.rules-intro-content > [hidden] {
    display: none;
}

.rules-intro-content > :not([hidden]) {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px 20px;
    align-content: start;
}

.rules-intro-content h2 {
    grid-column: 1 / -1;
    margin: 0 0 4px;
    color: #f8fafc;
    font-size: clamp(24px, 4vw, 42px);
    line-height: 1.05;
}

.rules-intro-content p {
    margin: 0;
    padding: 14px;
    border: 1px solid rgba(148, 163, 184, 0.18);
    border-radius: 8px;
    background: rgba(2, 6, 23, 0.34);
    color: #d8e3ec;
    font-size: clamp(14px, 1.55vw, 17px);
    line-height: 1.58;
}

.rules-intro-content strong {
    color: #ffffff;
}

.lab-guide-book {
    grid-column: 1 / -1;
    display: grid;
    gap: 14px;
    padding: clamp(14px, 2vw, 20px);
    border: 1px solid rgba(125, 211, 252, 0.34);
    border-radius: 8px;
    background:
        linear-gradient(180deg, rgba(14, 31, 43, 0.82), rgba(8, 13, 22, 0.86));
}

.lab-guide-book-header {
    display: grid;
    gap: 6px;
    max-width: 900px;
}

.lab-guide-book-header span {
    color: #7dd3fc;
    font-size: 12px;
    font-weight: 900;
    letter-spacing: 0;
    text-transform: uppercase;
}

.lab-guide-book-header h3 {
    margin: 0;
    color: #f8fafc;
    font-size: clamp(20px, 3vw, 30px);
    line-height: 1.12;
}

.lab-guide-book-subtitle {
    color: #cbd5e1;
    font-size: clamp(14px, 1.4vw, 16px);
    line-height: 1.5;
}

.lab-guide-book-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
}

.lab-guide-card {
    min-width: 0;
    padding: 13px 14px;
    border: 1px solid rgba(148, 163, 184, 0.18);
    border-radius: 8px;
    background: rgba(2, 6, 23, 0.32);
}

.lab-guide-card h4 {
    margin: 0 0 8px;
    color: #ffffff;
    font-size: 15px;
    line-height: 1.25;
}

.lab-guide-card ul {
    margin: 0;
    padding-left: 1.1rem;
    color: #d8e3ec;
    font-size: clamp(13px, 1.35vw, 15px);
    line-height: 1.48;
}

.lab-guide-card li {
    margin: 0.35rem 0;
}

.lab-guide-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
    padding-top: 4px;
}

.lab-guide-actions a {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 36px;
    padding: 8px 12px;
    border: 1px solid rgba(125, 211, 252, 0.42);
    border-radius: 7px;
    background: rgba(5, 12, 24, 0.82);
    color: #eef5ff;
    font-weight: 950;
    text-decoration: none;
}

.lab-guide-actions a:hover,
.lab-guide-actions a:focus-visible {
    border-color: rgba(125, 211, 252, 0.78);
    background: rgba(20, 33, 45, 0.94);
    outline: none;
}

.lab-guide-actions p {
    flex: 1 1 260px;
    margin: 0;
    color: #cbd5e1;
    font-size: 12px;
    line-height: 1.4;
}

.site-action-palette {
    position: fixed;
    z-index: 80;
    display: grid;
    gap: 6px;
    width: min(240px, calc(100vw - 20px));
    padding: 8px;
    border: 1px solid rgba(66, 198, 255, 0.46);
    border-radius: 8px;
    background: rgba(8, 12, 18, 0.96);
    box-shadow: 0 18px 48px rgba(0, 0, 0, 0.42);
}

.site-action-title {
    padding: 2px 4px 5px;
    color: var(--muted);
    font-size: 11px;
    font-weight: 1000;
    text-transform: uppercase;
}

.site-action-choice {
    display: grid;
    gap: 2px;
    min-height: 42px;
    padding: 7px 9px;
    text-align: left;
    border-color: rgba(66, 198, 255, 0.28);
}

.site-action-choice span {
    color: var(--text);
    font-size: 13px;
    font-weight: 1000;
}

.site-action-choice small {
    color: var(--muted);
    font-size: 11px;
    line-height: 1.25;
}

.site-action-choice:disabled {
    cursor: not-allowed;
    opacity: 0.42;
}

.control-panel label, .layer-panel label {
    display: grid;
    gap: 5px;
    color: var(--muted);
    font-size: 11px;
    font-weight: 900;
    text-transform: uppercase;
}

.control-panel select, .control-panel input, .layer-panel input {
    width: 100%;
    min-height: 38px;
    padding: 6px 8px;
}

.control-panel button {
    align-self: end;
    min-height: 38px;
    padding: 7px 10px;
    font-weight: 900;
    cursor: pointer;
}

.control-panel button:hover, .home-link:hover {
    border-color: rgba(66, 198, 255, 0.78);
    background: rgba(20, 33, 45, 0.94);
}

.layer-panel {
    grid-template-columns: minmax(120px, 240px) minmax(120px, 240px) auto;
    align-items: end;
}

.game-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 16px;
}

.board-wrap, .side-panel {
    min-width: 0;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(15, 20, 27, 0.9);
    box-shadow: 0 24px 80px rgba(0, 0, 0, 0.3);
}

.lab-history-panels {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: 14px;
}

.lab-history-panels section,
.lab-export-panel {
    min-width: 0;
    display: grid;
    gap: 8px;
    padding: 12px;
    border: 1px solid rgba(218, 232, 224, 0.13);
    border-radius: 8px;
    background: rgba(15, 20, 27, 0.9);
    box-shadow: 0 18px 54px rgba(0, 0, 0, 0.22);
}

.lab-history-panels h2,
.lab-export-panel h2 {
    margin: 0;
    font-size: 16px;
}

.board-wrap {
    min-height: 620px;
    padding: 14px;
}

.board-header {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
}

.board-title {
    display: grid;
    gap: 4px;
    min-width: 0;
}

.board-title strong {
    font-size: 18px;
}

.board-title span {
    color: var(--muted);
    font-size: 12px;
    text-align: left;
}

.board-turn-card {
    min-width: 96px;
    padding: 8px 10px;
}

.board {
    width: 100%;
    max-width: min(86vh, 980px);
    margin: 0 auto;
    display: grid;
    gap: 3px;
    aspect-ratio: 1;
}

.interactive-board-shell {
    position: relative;
    width: 100%;
    max-width: 100%;
    min-height: 520px;
    overflow: clip;
}

.algebraic-3d-board {
    display: block;
    width: 100%;
    height: min(72vh, 820px);
    min-height: 560px;
    background: #091016;
    border: 1px solid rgba(79, 137, 164, 0.42);
    border-radius: 6px;
    touch-action: none;
}

.reset-3d-camera {
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 4;
    width: auto;
    min-width: 116px;
    padding: 8px 12px;
    background: rgba(11, 17, 23, 0.9);
    backdrop-filter: blur(8px);
}

.cell {
    position: relative;
    min-width: 0;
    min-height: 0;
    border: 1px solid rgba(137, 166, 179, 0.24);
    border-radius: 5px;
    background: rgba(27, 38, 49, 0.88);
    color: var(--text);
    cursor: pointer;
    overflow: hidden;
}

.cell:nth-child(odd) {
    background: rgba(32, 46, 58, 0.88);
}

.board.lattice-triangular .cell {
    background-image: linear-gradient(
        135deg,
        transparent 47%,
        rgba(137, 166, 179, 0.32) 48%,
        rgba(137, 166, 179, 0.32) 52%,
        transparent 53%
    );
}

.board.go-node-board {
    gap: 0;
    padding: clamp(8px, 1.8vw, 18px);
    border: 1px solid rgba(137, 166, 179, 0.28);
    border-radius: 8px;
    background: radial-gradient(circle at center, rgba(34, 48, 64, 0.74), rgba(11, 17, 23, 0.94));
}

.board.go-node-board .cell,
.board.go-node-board .cell:nth-child(odd) {
    border: 0;
    border-radius: 0;
    background: transparent;
    overflow: visible;
}

.board.go-node-board .cell::before,
.board.go-node-board .cell::after {
    content: "";
    position: absolute;
    pointer-events: none;
    z-index: 0;
    background: rgba(137, 166, 179, 0.38);
}

.board.go-node-board .cell::before {
    left: 50%;
    top: 0;
    bottom: 0;
    width: 1px;
    transform: translateX(-0.5px);
}

.board.go-node-board .cell::after {
    top: 50%;
    left: 0;
    right: 0;
    height: 1px;
    transform: translateY(-0.5px);
}

.board.go-node-board .cell.legal {
    box-shadow: inset 0 0 0 999px rgba(66, 198, 255, 0.08);
}

.board.go-node-board .cell.preview-flip {
    box-shadow: inset 0 0 0 999px rgba(242, 196, 100, 0.12);
}

.board.go-node-board .stone.go-stone,
.board.go-node-board .stone.cluster-site {
    inset: 19%;
    z-index: 2;
}


.board.lattice-honeycomb-nodes,
.board.lattice-hex-cells {
    position: relative;
    display: block;
    overflow: hidden;
}

.lattice-edge-layer {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.lattice-edge-layer line {
    stroke: rgba(190, 218, 226, 0.82);
    stroke-width: 0.42;
}

.lattice-edge-layer .honeycomb-face {
    fill: rgba(53, 92, 105, 0.26);
    stroke: none;
}

.lattice-edge-layer .honeycomb-face.alternate {
    fill: rgba(88, 122, 105, 0.22);
}

.board.lattice-honeycomb-nodes .cell,
.board.lattice-hex-cells .cell {
    position: absolute;
}

.board.lattice-honeycomb-nodes .cell {
    border: 0;
    border-radius: 50%;
    background: radial-gradient(
        circle,
        rgba(224, 247, 255, 0.88) 0 7%,
        rgba(27, 38, 49, 0.18) 8% 38%,
        transparent 39%
    );
    overflow: visible;
}

.board.lattice-honeycomb-nodes .cell:nth-child(odd) {
    background: radial-gradient(
        circle,
        rgba(224, 247, 255, 0.88) 0 7%,
        rgba(27, 38, 49, 0.18) 8% 38%,
        transparent 39%
    );
}

.board.lattice-honeycomb-nodes .cell:hover {
    box-shadow: 0 0 0 2px rgba(66, 198, 255, 0.65);
}

.board.lattice-honeycomb-nodes .cell.legal {
    outline: 0;
    background: radial-gradient(
        circle,
        rgba(62, 230, 138, 0.96) 0 10%,
        rgba(62, 230, 138, 0.2) 11% 23%,
        transparent 24%
    );
}

.board.lattice-hex-cells .cell {
    clip-path: polygon(25% 6.7%, 75% 6.7%, 100% 50%, 75% 93.3%, 25% 93.3%, 0 50%);
    border: 0;
    border-radius: 0;
    background: #255d49;
    box-shadow: inset 0 0 0 1px rgba(3, 9, 12, 0.82);
    overflow: visible;
}

.board.lattice-hex-cells .cell:nth-child(odd) {
    background: #367c62;
}

.board.lattice-honeycomb-nodes .coord,
.board.lattice-hex-cells .coord {
    display: none;
}

.cell:hover {
    border-color: rgba(66, 198, 255, 0.72);
}

.cell.legal {
    outline: 2px solid rgba(62, 230, 138, 0.62);
    outline-offset: -3px;
}

.cell.preview-flip {
    box-shadow: inset 0 0 0 3px rgba(242, 196, 100, 0.82);
}

.cell.jump-path {
    background: rgba(42, 96, 112, 0.74);
}

.cell.braid-trail::before {
    content: "";
    position: absolute;
    left: 8%;
    right: 8%;
    top: 47%;
    height: 2px;
    border-radius: 99px;
    background: rgba(242, 196, 100, 0.34);
    transform: rotate(-22deg);
    pointer-events: none;
}

.cell.exchange-active {
    outline: 2px solid rgba(242, 196, 100, 0.92);
    outline-offset: -4px;
    box-shadow: inset 0 0 0 2px rgba(66, 198, 255, 0.26), 0 0 22px rgba(242, 196, 100, 0.24);
}

.cell.exchange-active::before {
    content: "";
    position: absolute;
    left: 15%;
    right: 15%;
    top: 50%;
    height: 48%;
    border: 2px solid rgba(242, 196, 100, 0.72);
    border-left-color: transparent;
    border-bottom-color: transparent;
    border-radius: 50%;
    pointer-events: none;
    z-index: 2;
}

.cell.exchange-counterclockwise::before {
    transform: scaleX(-1);
}

.cell.exchange-clockwise .anyon,
.cell.exchange-clockwise .stone,
.cell.exchange-clockwise .majorana-mode-token {
    animation: braidExchangeClockwise 560ms ease-in-out;
}

.cell.exchange-counterclockwise .anyon,
.cell.exchange-counterclockwise .stone,
.cell.exchange-counterclockwise .majorana-mode-token {
    animation: braidExchangeCounterclockwise 560ms ease-in-out;
}

.exchange-badge {
    position: absolute;
    right: 3px;
    top: 4px;
    z-index: 4;
    min-width: 22px;
    padding: 1px 5px;
    border: 1px solid rgba(242, 196, 100, 0.92);
    border-radius: 999px;
    background: rgba(44, 31, 9, 0.92);
    color: #fff0b6;
    font-size: clamp(8px, 0.9vw, 11px);
    font-weight: 1000;
    line-height: 1.2;
    pointer-events: none;
}

.cell.fusion-site::after {
    content: "";
    position: absolute;
    width: 34%;
    height: 34%;
    left: 33%;
    top: 33%;
    border: 1px solid rgba(242, 196, 100, 0.8);
    transform: rotate(45deg);
    pointer-events: none;
}

.cell.noisy {
    box-shadow: inset 0 0 0 2px rgba(255, 115, 115, 0.5);
}

.cell.measured {
    border-color: rgba(62, 230, 138, 0.7);
}

.cell.fielded {
    box-shadow: inset 0 0 0 2px rgba(242, 196, 100, 0.45), 0 0 14px rgba(242, 196, 100, 0.16);
}

.cell.stressed {
    background:
        radial-gradient(circle at 50% 50%, rgba(242, 196, 100, 0.24), transparent 72%),
        rgba(27, 38, 49, 0.88);
}

.cell.stress-preview {
    outline: 2px solid rgba(242, 196, 100, 0.78);
    outline-offset: -4px;
}

.cell.unstable-group {
    border-color: rgba(255, 115, 115, 0.9);
    box-shadow: inset 0 0 0 2px rgba(255, 115, 115, 0.42), 0 0 18px rgba(255, 115, 115, 0.18);
}

.stress-value {
    position: absolute;
    right: 4px;
    top: 3px;
    z-index: 2;
    min-width: 20px;
    padding: 1px 4px;
    border-radius: 999px;
    background: rgba(8, 11, 16, 0.76);
    color: #f8df9c;
    font-size: clamp(8px, 0.95vw, 11px);
    font-weight: 1000;
    line-height: 1.25;
    pointer-events: none;
}

.cell.cancel-latest {
    border-color: rgba(62, 230, 138, 0.9);
    box-shadow: inset 0 0 0 2px rgba(62, 230, 138, 0.58), 0 0 18px rgba(62, 230, 138, 0.24);
}

.cell.legal-unbraid {
    outline: 2px solid rgba(62, 230, 138, 0.78);
    outline-offset: -5px;
}

.unbraid-badge {
    position: absolute;
    left: 50%;
    top: 4px;
    transform: translateX(-50%);
    z-index: 3;
    padding: 2px 5px;
    border: 1px solid rgba(62, 230, 138, 0.92);
    border-radius: 999px;
    background: rgba(6, 32, 21, 0.92);
    color: #d7ffe8;
    font-size: clamp(7px, 0.8vw, 10px);
    font-weight: 1000;
    letter-spacing: 0;
    pointer-events: none;
}

.unbraid-badge.wrong {
    border-color: rgba(255, 115, 115, 0.88);
    background: rgba(48, 12, 16, 0.92);
    color: #ffd7d7;
}

.cell.wrong-unbraid {
    border-color: rgba(255, 115, 115, 0.84);
    box-shadow: inset 0 0 0 2px rgba(255, 115, 115, 0.36);
}

.cell.cancellation-flash {
    animation: cancelFlash 720ms ease-out;
}

.cell.wrong-unbraid-flash {
    animation: wrongUnbraidFlash 840ms ease-out;
}

.coord {
    position: absolute;
    left: 4px;
    bottom: 3px;
    color: rgba(238, 245, 247, 0.42);
    font-size: clamp(7px, 1vw, 10px);
    font-weight: 800;
    pointer-events: none;
}

.stone, .anyon {
    position: absolute;
    inset: 13%;
    display: grid;
    place-items: center;
    border-radius: 50%;
    font-size: clamp(12px, 2.1vw, 22px);
    font-weight: 1000;
    letter-spacing: 0;
    z-index: 1;
}

.stone.go-stone {
    inset: 14%;
    overflow: visible;
}

.anyon {
    inset: 18%;
    font-size: clamp(10px, 1.65vw, 18px);
}

.majorana-mode-token {
    position: absolute;
    inset: 11%;
    display: grid;
    place-items: center;
    border-radius: 50%;
    z-index: 2;
    background: radial-gradient(circle at 32% 26%, rgba(255, 255, 255, 0.9), rgba(64, 224, 255, 0.78) 32%, rgba(5, 15, 24, 0.96) 72%);
    border: 2px solid rgba(139, 225, 255, 0.88);
    box-shadow: 0 0 18px rgba(66, 198, 255, 0.32), inset 0 0 0 1px rgba(255, 255, 255, 0.22);
    color: #ecfeff;
    font-size: clamp(10px, 1.55vw, 18px);
    font-weight: 1000;
    line-height: 1;
    pointer-events: none;
}

.majorana-mode-token.sector-b {
    background: radial-gradient(circle at 32% 26%, rgba(255, 255, 255, 0.96), rgba(242, 196, 100, 0.86) 34%, rgba(31, 22, 8, 0.96) 74%);
    border-color: rgba(242, 196, 100, 0.9);
    box-shadow: 0 0 18px rgba(242, 196, 100, 0.28), inset 0 0 0 1px rgba(255, 255, 255, 0.18);
}

.majorana-mode-token.selected,
.cell.majorana-selected .majorana-mode-token {
    outline: 3px solid rgba(66, 230, 138, 0.95);
    outline-offset: 2px;
}

.majorana-mode-token.braided {
    box-shadow: 0 0 0 3px rgba(242, 196, 100, 0.32), 0 0 20px rgba(242, 196, 100, 0.3);
}

.majorana-gamma {
    transform: translateY(-0.12em);
}

.majorana-flavor,
.majorana-channel {
    position: absolute;
    min-width: 1.6em;
    padding: 1px 4px;
    border-radius: 999px;
    background: rgba(4, 8, 12, 0.82);
    color: #fff2b8;
    font-size: 0.48em;
    border: 1px solid rgba(242, 196, 100, 0.55);
}

.majorana-flavor {
    left: -0.15em;
    bottom: -0.2em;
}

.majorana-channel {
    right: -0.15em;
    bottom: -0.2em;
}

.stone.jump-particle-site {
    inset: 18%;
    font-size: clamp(10px, 1.45vw, 17px);
}

.stone.ising-spin {
    inset: 15%;
    font-size: clamp(11px, 1.5vw, 18px);
    font-weight: 1000;
}

.stone.ising-spin.black {
    box-shadow: 0 0 0 2px rgba(66, 198, 255, 0.32), 0 10px 20px rgba(0, 0, 0, 0.32);
}

.stone.ising-spin.white {
    box-shadow: 0 0 0 2px rgba(242, 196, 100, 0.38), 0 10px 20px rgba(255, 255, 255, 0.14);
}

.stone.two-phase-site {
    font-size: clamp(11px, 1.6vw, 18px);
    letter-spacing: 0;
}

.stone.two-phase-site.black {
    box-shadow: inset 0 0 0 2px rgba(34, 211, 238, 0.56), 0 0 16px rgba(20, 184, 166, 0.28);
}

.stone.two-phase-site.white {
    box-shadow: inset 0 0 0 2px rgba(250, 204, 21, 0.5), 0 0 16px rgba(250, 204, 21, 0.24);
}

.stone.cluster-site {
    inset: 13%;
    font-size: clamp(12px, 1.7vw, 19px);
    letter-spacing: 0;
}

.stone.cluster-site .cft-badge {
    bottom: -0.3rem;
    font-size: 0.55em;
}

.stone.cluster-site.black {
    box-shadow: inset 0 0 0 2px rgba(66, 198, 255, 0.48), 0 0 18px rgba(34, 211, 238, 0.24);
}

.stone.cluster-site.white {
    box-shadow: inset 0 0 0 2px rgba(248, 250, 252, 0.6), 0 0 18px rgba(242, 196, 100, 0.22);
}

.stone.spin-ice-vertex {
    inset: 12%;
    font-size: clamp(10px, 1.35vw, 16px);
    letter-spacing: 0;
}

.stone.spin-ice-vertex:not(.violation) {
    opacity: 0.72;
    background: rgba(17, 28, 38, 0.86);
    color: #b8e7ff;
}

.stone.spin-ice-vertex.violation.black {
    box-shadow: inset 0 0 0 2px rgba(248, 113, 113, 0.7), 0 0 18px rgba(248, 113, 113, 0.28);
}

.stone.spin-ice-vertex.violation.white {
    box-shadow: inset 0 0 0 2px rgba(250, 204, 21, 0.65), 0 0 18px rgba(250, 204, 21, 0.28);
}

.stone.z2-gauge-vertex {
    inset: 26% 15%;
    border-radius: 7px;
    font-size: clamp(9px, 1.2vw, 15px);
    letter-spacing: 0;
    z-index: 2;
    pointer-events: none;
}

.stone.z2-gauge-vertex:not(.violation) {
    opacity: 0.76;
    background: rgba(11, 29, 43, 0.88);
    color: #9be7ff;
}

.stone.z2-gauge-vertex.violation {
    box-shadow: inset 0 0 0 2px rgba(248, 113, 113, 0.68), 0 0 18px rgba(248, 113, 113, 0.3);
}

.z2-gauge-edge {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 47%;
    height: max(3px, 7%);
    border-radius: 999px;
    transform: translateY(-50%) rotate(var(--edge-angle, 0deg));
    transform-origin: left center;
    z-index: 3;
    pointer-events: auto;
}

.z2-gauge-edge.black {
    background: #02070c;
    box-shadow: 0 0 0 1px rgba(72, 201, 255, 0.42), 0 0 8px rgba(72, 201, 255, 0.24);
}

.z2-gauge-edge.white {
    background: #f8fbff;
    box-shadow: 0 0 0 1px rgba(6, 10, 16, 0.82), 0 0 10px rgba(248, 251, 255, 0.42);
}

.stone.go-stone.unstable {
    outline: 2px solid rgba(255, 115, 115, 0.92);
    outline-offset: 2px;
    box-shadow: 0 0 18px rgba(255, 115, 115, 0.34);
}

.cft-badge, .cft-pressure {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    z-index: 2;
    border-radius: 999px;
    border: 1px solid rgba(242, 196, 100, 0.72);
    background: rgba(8, 11, 16, 0.84);
    color: #f8df9c;
    font-size: clamp(7px, 0.75vw, 10px);
    font-weight: 1000;
    line-height: 1.1;
    padding: 2px 4px;
    pointer-events: none;
    white-space: nowrap;
}

.cft-badge {
    top: -5px;
}

.cft-pressure {
    bottom: -6px;
    border-color: rgba(255, 115, 115, 0.78);
    color: #ffd9d9;
}

.stone.cft-primary-stone {
    inset: 10%;
    overflow: visible;
    font-family: Georgia, "Times New Roman", serif;
    font-size: clamp(11px, 1.65vw, 19px);
}

.stone.cft-primary-stone.hidden-channel {
    outline: 2px dashed rgba(242, 196, 100, 0.8);
    outline-offset: 2px;
}

.cell.cft-selected-region {
    box-shadow: inset 0 0 0 3px rgba(66, 198, 255, 0.85);
}

.cft-observable-panel {
    display: grid;
    gap: 9px;
}

.cft-block-row {
    display: grid;
    grid-template-columns: 68px minmax(0, 1fr) 48px;
    align-items: center;
    gap: 8px;
    color: var(--muted);
    font-size: 12px;
}

.cft-block-row meter {
    width: 100%;
    accent-color: var(--gold);
}

.cft-observable-panel p {
    margin: 0;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.45;
}

.qec-observable-panel {
    display: grid;
    gap: 10px;
}

.qec-observable-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 7px;
}

.qec-observable-grid div {
    min-width: 0;
    padding: 8px;
    border: 1px solid rgba(218, 232, 224, 0.14);
    border-radius: 6px;
    background: rgba(8, 12, 18, 0.54);
}

.qec-observable-grid span {
    display: block;
    color: var(--muted);
    font-size: 9px;
    font-weight: 900;
    text-transform: uppercase;
}

.qec-observable-grid strong {
    display: block;
    margin-top: 3px;
    overflow-wrap: anywhere;
    font-size: 16px;
}

.qec-observable-panel p {
    margin: 0;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.45;
}

.stone.black, .anyon.black {
    color: #f8fbfd;
    background: radial-gradient(circle at 35% 28%, #3c4653, var(--black-stone) 62%);
    border: 1px solid rgba(238, 245, 247, 0.18);
}

.stone.white, .anyon.white {
    color: #17202a;
    background: radial-gradient(circle at 35% 28%, #fff, var(--white-stone) 64%);
    border: 1px solid rgba(18, 24, 32, 0.22);
}

.stone.ancilla {
    outline: 2px solid rgba(62, 230, 138, 0.92);
    outline-offset: 2px;
}

.stone.ancilla.non-stabilizer {
    outline-color: rgba(255, 140, 66, 0.95);
    box-shadow: 0 0 18px rgba(255, 140, 66, 0.34);
}

.anyon.selected {
    box-shadow: 0 0 0 2px rgba(66, 198, 255, 0.82), 0 0 18px rgba(66, 198, 255, 0.32);
}

.anyon.braided {
    outline: 1px solid rgba(242, 196, 100, 0.86);
    outline-offset: 1px;
}

.anyon.status-trivial {
    box-shadow: inset 0 0 0 1px rgba(238, 245, 247, 0.18);
}

.anyon.status-braided {
    outline: 1px solid rgba(242, 196, 100, 0.9);
    outline-offset: 1px;
}

.anyon.status-partially_unbraided {
    outline: 1px dashed rgba(62, 230, 138, 0.9);
    outline-offset: 1px;
}

.anyon.phase-active {
    overflow: visible;
}

.anyon.phase-active::before {
    content: "";
    position: absolute;
    left: 10%;
    right: 10%;
    bottom: -5px;
    height: 8px;
    border-radius: 999px;
    filter: blur(0.5px);
    pointer-events: none;
}

.anyon.phase-positive::before {
    background: rgba(62, 230, 138, 0.55);
    box-shadow: 0 0 16px rgba(62, 230, 138, 0.45);
}

.anyon.phase-negative::before {
    background: rgba(255, 115, 115, 0.52);
    box-shadow: 0 0 16px rgba(255, 115, 115, 0.42);
}

.anyon-label {
    position: relative;
    z-index: 2;
}

.phase-pancake {
    position: absolute;
    left: 50%;
    bottom: -7px;
    transform: translateX(-50%);
    z-index: 3;
    min-width: 30px;
    padding: 1px 5px;
    border-radius: 999px;
    border: 1px solid rgba(238, 245, 247, 0.36);
    background: rgba(8, 11, 16, 0.84);
    color: #f8df9c;
    font-size: clamp(7px, 0.8vw, 10px);
    font-weight: 1000;
    line-height: 1.15;
    pointer-events: none;
    white-space: nowrap;
}

.stone.hidden, .anyon.hidden {
    color: #f2c464;
    border-style: dashed;
}

@keyframes cancelFlash {
    0% {
        box-shadow: inset 0 0 0 3px rgba(62, 230, 138, 0.95), 0 0 0 rgba(62, 230, 138, 0);
    }
    55% {
        box-shadow: inset 0 0 0 3px rgba(62, 230, 138, 0.9), 0 0 26px rgba(62, 230, 138, 0.58);
    }
    100% {
        box-shadow: inset 0 0 0 0 rgba(62, 230, 138, 0), 0 0 0 rgba(62, 230, 138, 0);
    }
}

@keyframes wrongUnbraidFlash {
    0%, 100% {
        box-shadow: inset 0 0 0 0 rgba(255, 115, 115, 0), 0 0 0 rgba(255, 115, 115, 0);
    }
    45% {
        box-shadow: inset 0 0 0 3px rgba(255, 115, 115, 0.82), 0 0 24px rgba(255, 115, 115, 0.42);
    }
}

@keyframes braidExchangeClockwise {
    0%, 100% {
        transform: translate(0, 0) scale(1);
    }
    45% {
        transform: translate(13%, -15%) scale(1.05);
    }
    70% {
        transform: translate(-7%, 7%) scale(0.98);
    }
}

@keyframes braidExchangeCounterclockwise {
    0%, 100% {
        transform: translate(0, 0) scale(1);
    }
    45% {
        transform: translate(-13%, -15%) scale(1.05);
    }
    70% {
        transform: translate(7%, 7%) scale(0.98);
    }
}

.side-panel {
    padding: 14px;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    align-content: start;
    align-items: start;
    gap: 14px;
}

.side-panel section {
    min-width: 0;
    display: grid;
    gap: 8px;
    padding: 12px;
    border: 1px solid rgba(218, 232, 224, 0.13);
    border-radius: 8px;
    background: rgba(8, 12, 18, 0.32);
}

.side-panel section:last-child {
    border-bottom: 1px solid rgba(218, 232, 224, 0.13);
    padding-bottom: 12px;
}

.cft-observable-panel,
.qec-observable-panel,
.final-answer-panel {
    grid-column: 1 / -1;
}

.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 8px;
}

#statusText {
    min-height: 44px;
    margin: 0;
    color: var(--muted);
    line-height: 1.45;
}

.time-status {
    margin: 0;
    color: var(--muted);
    line-height: 1.45;
}

.phase-timeline {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.phase-timeline span {
    --phase-fill: rgba(66, 198, 255, 0.16);
    width: 32px;
    height: 32px;
    display: grid;
    place-items: center;
    border: 1px solid rgba(148, 163, 184, 0.22);
    border-radius: 999px;
    background:
        radial-gradient(circle, rgba(7, 10, 14, 0.92) 0 54%, transparent 56%),
        conic-gradient(var(--phase-fill) 0 72deg, rgba(148, 163, 184, 0.1) 72deg 360deg);
    color: var(--muted);
    font-size: 12px;
    font-weight: 900;
}

.phase-timeline span i {
    font-style: normal;
    line-height: 1;
}

.phase-timeline span.active {
    border-color: rgba(66, 198, 255, 0.85);
    color: var(--text);
    --phase-fill: rgba(245, 182, 71, 0.86);
    box-shadow: 0 0 18px rgba(66, 198, 255, 0.26);
}

.rules-mini-diagram {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(128px, 1fr));
    gap: 8px;
    margin: 2px 0 4px;
}

.rules-mini-diagram span {
    position: relative;
    min-height: 42px;
    display: grid;
    place-items: center;
    padding: 8px;
    border: 1px solid rgba(125, 211, 252, 0.24);
    border-radius: 8px;
    background: rgba(8, 13, 22, 0.72);
    color: rgba(237, 244, 246, 0.9);
    font-size: 12px;
    font-weight: 900;
    text-align: center;
}

.rules-mini-diagram span:not(:last-child)::after {
    content: ">";
    position: absolute;
    right: -8px;
    top: 50%;
    transform: translateY(-50%);
    color: rgba(245, 182, 71, 0.92);
    font-size: 13px;
}

.lab-export-panel {
    display: grid;
    gap: 8px;
}

.lab-export-actions {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(130px, max-content));
    justify-content: start;
    gap: 8px;
}

.lab-export-actions button {
    min-height: 38px;
    padding: 7px 12px;
    border-color: rgba(66, 198, 255, 0.38);
    font-weight: 900;
    cursor: pointer;
}

.legend {
    display: flex;
    flex-wrap: wrap;
    gap: 7px;
}

.legend span {
    padding: 5px 7px;
    border: 1px solid var(--line);
    border-radius: 999px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 800;
}

.history-list {
    min-height: 178px;
    max-height: 260px;
    margin: 0;
    padding-left: 22px;
    overflow: auto;
    color: var(--muted);
    font-size: 13px;
    line-height: 1.45;
}

textarea {
    width: 100%;
    min-height: 170px;
    resize: vertical;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(7, 10, 14, 0.78);
    color: #dce8ea;
    padding: 9px;
    font-family: "Cascadia Mono", Consolas, monospace;
    font-size: 11px;
}

@media (max-width: 1080px) {
    .control-panel {
        grid-template-columns: repeat(6, minmax(0, 1fr));
    }

    .control-section,
    .section-game,
    .section-board,
    .section-dynamics {
        grid-column: span 3;
        border-right: 0;
    }

    .section-algebra {
        grid-column: 1 / -1;
    }

    .section-actions {
        grid-column: span 3;
    }

    .section-algebra > .algebra-mode-fields {
        grid-template-columns: repeat(3, minmax(150px, 1fr));
    }

    .game-layout {
        grid-template-columns: 1fr;
    }

    .board-wrap {
        min-height: auto;
    }
}

@media (max-width: 620px) {
    .top-bar {
        grid-template-columns: 1fr;
    }

    .control-panel, .layer-panel {
        grid-template-columns: 1fr 1fr;
    }

    .control-section,
    .section-game,
    .section-board,
    .section-algebra,
    .section-dynamics,
    .section-actions,
    .section-online {
        grid-column: 1 / -1;
        border-right: 0;
    }

    .control-fields.two-col,
    .control-fields.three-col,
    .section-algebra > .algebra-mode-fields,
    .button-grid {
        grid-template-columns: 1fr 1fr;
    }

    .section-online .control-fields,
    .section-online .online-button-grid {
        grid-template-columns: 1fr;
    }

    .section-online .divider-text {
        min-height: 22px;
    }

    .rules-intro-panel {
        padding: 8px;
    }

    .rules-intro-dialog {
        width: calc(100vw - 16px);
        height: calc(100vh - 16px);
    }

    .rules-intro-header {
        align-items: stretch;
        flex-direction: column;
    }

    .rules-intro-header button {
        width: 100%;
    }

    .rules-intro-content > :not([hidden]) {
        grid-template-columns: 1fr;
    }

    .lab-guide-book {
        padding: 13px;
    }

    .lab-guide-book-grid {
        grid-template-columns: 1fr;
    }

    .lab-guide-card,
    .lab-guide-book-subtitle {
        overflow-wrap: anywhere;
    }

    .board {
        gap: 2px;
    }
}

/* Temporal age indicator: a full circle means the piece is due for the current age/noise cycle. */
.cell .age-ring {
    position: absolute;
    inset: 11%;
    border-radius: 999px;
    pointer-events: none;
    background: conic-gradient(rgba(245, 182, 71, 0.96) 0 var(--age-angle), rgba(245, 182, 71, 0.08) var(--age-angle) 360deg);
    -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 4px), #000 calc(100% - 3px));
    mask: radial-gradient(farthest-side, transparent calc(100% - 4px), #000 calc(100% - 3px));
    opacity: 0.88;
    z-index: 7;
}
.cell .age-ring.age-ring-due { filter: drop-shadow(0 0 8px rgba(245, 182, 71, 0.86)); opacity: 1; }

/* More balanced Spin / Phase lab controls. */
body[data-mode="ising_domain_game"] .section-algebra > .algebra-mode-fields,
body[data-mode="two_phase_competition_game"] .section-algebra > .algebra-mode-fields,
body[data-mode="spin_ice_vertex_game"] .section-algebra > .algebra-mode-fields,
body[data-mode="z2_gauge_loop_game"] .section-algebra > .algebra-mode-fields {
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    align-items: end;
}
body[data-mode="ising_domain_game"] .section-dynamics .control-fields,
body[data-mode="two_phase_competition_game"] .section-dynamics .control-fields,
body[data-mode="spin_ice_vertex_game"] .section-dynamics .control-fields,
body[data-mode="z2_gauge_loop_game"] .section-dynamics .control-fields {
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
}

/* Balanced lab shell: keep the board visible first and put long lab settings in a usable side rail. */
@media (min-width: 1180px) {
    .app-shell {
        width: min(1680px, 100%);
        grid-template-columns: minmax(620px, 1fr) minmax(370px, 460px);
        align-items: start;
    }

    .top-bar {
        grid-column: 1 / -1;
    }

    .game-layout {
        grid-column: 1;
        grid-row: 2;
        align-self: start;
        min-width: 0;
    }

    .control-panel {
        grid-column: 2;
        grid-row: 2;
        grid-template-columns: 1fr;
        align-self: start;
        align-items: start;
        max-height: calc(100vh - 130px);
        overflow: auto;
        overscroll-behavior: contain;
    }

    .layer-panel {
        grid-column: 1 / -1;
    }

    .lab-history-panels,
    .lab-export-panel {
        grid-column: 1 / -1;
        grid-row: auto;
    }

    .section-game,
    .section-board,
    .section-actions,
    .section-dynamics,
    .section-algebra,
    .section-online {
        grid-column: 1;
        min-width: 0;
        border-right: 0;
        padding-right: 0;
        padding-bottom: 12px;
        border-bottom: 1px solid rgba(218, 232, 224, 0.1);
    }

    .section-online {
        border-bottom: 0;
        padding-bottom: 0;
    }

    .section-actions .button-grid {
        grid-template-columns: repeat(2, minmax(130px, 1fr));
        max-width: none;
    }

    .section-board .control-fields.two-col,
    .section-game .control-fields.two-col,
    .section-game .control-fields.three-col,
    .section-online .control-fields {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .section-algebra > .algebra-mode-fields,
    .section-dynamics .control-fields {
        grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    }

    .board-wrap {
        min-height: calc(100vh - 130px);
    }

    .board {
        max-width: min(78vh, 960px, 100%);
    }

    .algebraic-3d-board {
        height: min(74vh, 760px);
        min-height: 520px;
    }
}

@media (min-width: 1500px) {
    .app-shell {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 1179px) {
    .top-bar {
        order: 1;
    }

    .game-layout {
        display: contents;
    }

    .game-layout > .board-wrap {
        order: 2;
    }

    .control-panel {
        order: 3;
    }

    .layer-panel {
        order: 4;
    }

    .game-layout > .side-panel {
        order: 5;
    }

    .lab-history-panels {
        order: 6;
    }

    .lab-export-panel {
        order: 7;
    }
}

/* Research readability and scientific model hierarchy. */
.lab-system-introduction-button {
    width: fit-content;
    min-height: 40px;
    margin-top: 12px;
    padding: 8px 14px;
    border-color: rgba(242, 196, 100, 0.56);
    background: rgba(242, 196, 100, 0.1);
    color: #fff4cd;
    font-size: 14px;
    font-weight: 900;
    cursor: pointer;
}

.lab-system-introduction {
    display: grid;
    gap: 16px;
    min-width: 0;
    padding: 18px;
    border: 1px solid rgba(125, 211, 252, 0.3);
    border-radius: 8px;
    background: rgba(12, 19, 28, 0.96);
}

.lab-system-introduction-heading {
    display: flex;
    align-items: start;
    justify-content: space-between;
    gap: 16px;
}

.lab-system-introduction-heading h2 {
    color: #f8fafc;
    font-size: 24px;
    line-height: 1.2;
}

.lab-system-introduction-heading > span {
    flex: 0 0 auto;
    padding: 6px 9px;
    border: 1px solid rgba(125, 211, 252, 0.32);
    border-radius: 999px;
    color: #bae6fd;
    font-size: 13px;
    font-weight: 900;
}

.lab-system-definition-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
    margin: 0;
}

.lab-system-definition-grid > div {
    min-width: 0;
    padding: 13px;
    border-left: 3px solid rgba(125, 211, 252, 0.46);
    background: rgba(2, 6, 23, 0.34);
}

.lab-system-definition-grid dt,
.lab-science-references h3 {
    margin: 0 0 6px;
    color: #f6cf78;
    font-size: 14px;
    font-weight: 900;
    line-height: 1.35;
}

.lab-system-definition-grid dd {
    margin: 0;
    color: #dce7ee;
    font-size: 15px;
    line-height: 1.62;
    overflow-wrap: anywhere;
}

.scientific-equation {
    display: inline-block;
    margin: 0.08rem 0.12rem;
    padding: 0.05rem 0.32rem;
    border: 1px solid rgba(125, 211, 252, 0.3);
    border-radius: 0.35rem;
    background: rgba(125, 211, 252, 0.08);
    color: #f8fafc;
    font-family: "Cambria Math", "STIX Two Math", "Noto Sans Math", Georgia, serif;
    font-size: 0.98em;
    line-height: 1.45;
    vertical-align: baseline;
}

.scientific-equation sub,
.scientific-equation sup {
    line-height: 0;
}

.lab-science-references {
    padding-top: 4px;
    border-top: 1px solid rgba(218, 232, 224, 0.13);
}

.lab-observable-methods {
    display: grid;
    gap: 12px;
    padding-top: 12px;
    border-top: 1px solid rgba(218, 232, 224, 0.13);
}

.lab-observable-methods-heading {
    display: flex;
    align-items: end;
    justify-content: space-between;
    gap: 14px;
}

.lab-observable-methods-heading h3 {
    margin: 0;
    color: #f8fafc;
    font-size: 20px;
    line-height: 1.3;
}

.lab-observable-methods-heading label {
    display: grid;
    gap: 5px;
    min-width: min(100%, 290px);
    color: #f6cf78;
    font-size: 14px;
    font-weight: 850;
}

.lab-observable-methods-heading select {
    min-height: 40px;
    padding: 7px 9px;
    font-size: 15px;
}

.lab-observable-spec-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 9px;
}

.lab-observable-spec {
    display: grid;
    gap: 7px;
    min-width: 0;
    padding: 12px;
    border: 1px solid rgba(125, 211, 252, 0.2);
    background: rgba(2, 6, 23, 0.32);
    cursor: pointer;
}

.lab-observable-spec:hover,
.lab-observable-spec:focus-within {
    border-color: rgba(242, 196, 100, 0.62);
}

.lab-observable-spec > div {
    display: flex;
    align-items: start;
    justify-content: space-between;
    gap: 10px;
}

.lab-observable-spec strong {
    color: #dff4fd;
    font-size: 15px;
    line-height: 1.35;
}

.lab-observable-spec output {
    flex: 0 0 auto;
    color: #f7d783;
    font-size: 14px;
    font-weight: 850;
}

.lab-observable-spec p {
    margin: 0;
    color: #cbd9df;
    font-size: 14px;
    line-height: 1.55;
}

.lab-observable-figure {
    margin: 0;
    padding: 10px;
    border: 1px solid rgba(125, 211, 252, 0.22);
    background: #f8faf9;
}

.lab-observable-figure canvas {
    display: block;
    width: 100%;
    min-height: 320px;
    aspect-ratio: 16 / 7;
    background: #fff;
}

.lab-observable-figure figcaption {
    padding: 7px 8px 2px;
    color: #40564e;
    font-size: 13px;
    line-height: 1.5;
    text-align: center;
}

.lab-science-references > div {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.lab-science-references a {
    display: inline-flex;
    align-items: center;
    min-height: 38px;
    padding: 7px 10px;
    border: 1px solid rgba(125, 211, 252, 0.28);
    border-radius: 7px;
    color: #bae6fd;
    font-size: 14px;
    font-weight: 750;
    line-height: 1.4;
    text-decoration: none;
}

.lab-science-references a:hover,
.lab-science-references a:focus-visible {
    border-color: rgba(242, 196, 100, 0.72);
    color: #fff4cd;
    outline: none;
}

.lab-info-panel summary,
.visualization-panel-title,
.turn-card span,
.stat-grid span,
.lab-info-panel dt,
.research-notes-panel dt,
.eyebrow {
    font-size: 13px;
    line-height: 1.4;
}

.lab-info-panel dd,
.research-notes-panel dd,
.side-panel p,
.time-status,
.history-list,
.legend,
.board-title span,
.rules-intro-content p,
.rules-intro-content li,
.rules-intro-content summary {
    font-size: 15px;
    line-height: 1.58;
}

.side-panel h2,
.lab-history-panels h2,
.lab-export-panel h2 {
    font-size: 17px;
    line-height: 1.35;
}

.control-section h2 {
    font-size: 16px;
}

.control-section label,
.control-section select,
.control-section input,
.control-section button,
.button-grid button,
.control-details summary {
    font-size: 14px;
    line-height: 1.35;
}

.labs-model-card small {
    font-size: 14px;
    line-height: 1.5;
}

.labs-model-card strong {
    font-size: 18px;
}

.labs-model-card span,
.labs-card-meta em,
.lab-header-badges span,
.validation-level-badge {
    font-size: 13px;
}

.coord {
    font-size: 10px;
}

.qec-observable-grid span {
    font-size: 13px;
    line-height: 1.35;
}

@media (max-width: 760px) {
    .lab-system-introduction {
        padding: 14px;
    }

    .lab-system-introduction-heading {
        display: grid;
        grid-template-columns: 1fr;
    }

    .lab-system-introduction-heading > span {
        width: fit-content;
    }

    .lab-system-definition-grid {
        grid-template-columns: 1fr;
    }

    .lab-system-definition-grid dt,
    .lab-science-references h3 {
        font-size: 15px;
    }

    .lab-system-definition-grid dd,
    .lab-info-panel dd,
    .research-notes-panel dd,
    .side-panel p,
    .time-status,
    .history-list,
    .legend,
    .board-title span,
    .rules-intro-content p,
    .rules-intro-content li,
    .rules-intro-content summary {
        font-size: 16px;
        line-height: 1.62;
    }

    .control-section label,
    .control-section select,
    .control-section input,
    .control-section button,
    .button-grid button,
    .control-details summary {
        font-size: 16px;
    }

    .lab-science-references a {
        width: 100%;
        font-size: 15px;
    }

    .lab-observable-methods-heading {
        display: grid;
        grid-template-columns: 1fr;
        align-items: stretch;
    }

    .lab-observable-methods-heading label {
        min-width: 0;
    }

    .lab-observable-spec-grid {
        grid-template-columns: 1fr;
    }

    .lab-observable-spec strong,
    .lab-observable-spec output,
    .lab-observable-spec p {
        font-size: 15px;
    }

    .lab-observable-figure canvas {
        min-height: 300px;
        aspect-ratio: 4 / 3;
    }

    .lab-observable-figure figcaption {
        font-size: 15px;
        line-height: 1.6;
    }

    .coord {
        font-size: 9px;
    }

    .qec-observable-grid span {
        font-size: 14px;
    }
}

html[lang="zh-Hant"] .lab-system-definition-grid dd,
html[lang="zh-Hant"] .lab-info-panel dd,
html[lang="zh-Hant"] .research-notes-panel dd,
html[lang="zh-Hant"] .side-panel p,
html[lang="zh-Hant"] .history-list,
html[lang="zh-Hant"] .rules-intro-content {
    line-height: 1.72;
}

/* Unified Topoboard Labs research workspace. */
@media (min-width: 1180px) {
    .app-shell {
        width: min(1780px, 100%);
        grid-template-columns: 1fr;
    }

    .labs-home,
    .lab-model-header,
    .lab-workspace {
        grid-column: 1;
    }

    .lab-workspace > .control-panel {
        grid-column: 1;
        grid-row: 1;
        max-height: calc(100vh - 120px);
    }

    .lab-left-panels {
        grid-column: 1;
        grid-row: 2 / span 3;
    }

    .lab-workspace > .layer-panel {
        grid-column: 2;
        grid-row: 1;
    }

    .lab-workspace > .game-layout {
        grid-column: 2 / 4;
        grid-row: 2;
        display: grid;
        grid-template-columns: minmax(0, 1fr) clamp(280px, 28vw, 390px);
        align-items: start;
        min-width: 0;
    }

    .game-layout > .board-wrap {
        grid-column: 1;
    }

    .game-layout > .side-panel {
        grid-column: 2;
        display: grid;
        grid-template-columns: 1fr;
        max-height: calc(100vh - 120px);
        overflow: auto;
        overscroll-behavior: contain;
    }

    .lab-workspace > .lab-history-panels {
        grid-column: 3;
        grid-row: 3;
        grid-template-columns: 1fr;
    }

    .lab-workspace > .lab-export-panel {
        grid-column: 3;
        grid-row: 4;
    }

    .lab-export-actions {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 1179px) {
    .top-bar,
    .labs-home,
    .lab-model-header,
    .lab-workspace,
    .control-panel,
    .layer-panel,
    .board-wrap,
    .side-panel,
    .lab-left-panels,
    .lab-history-panels,
    .lab-export-panel {
        grid-column: 1;
    }

    .lab-workspace {
        display: grid;
        grid-template-columns: 1fr;
    }

    .lab-workspace > .game-layout {
        display: contents;
    }

    .lab-workspace > .game-layout > .board-wrap {
        order: 1;
    }

    .lab-workspace > .control-panel {
        order: 2;
    }

    .lab-workspace > .lab-left-panels {
        order: 3;
    }

    .lab-workspace > .layer-panel {
        order: 4;
    }

    .lab-workspace > .game-layout > .side-panel {
        order: 5;
    }

    .lab-workspace > .lab-history-panels {
        order: 6;
    }

    .lab-workspace > .lab-export-panel {
        order: 7;
    }

    .lab-info-panel:not([open]) dl {
        display: none;
    }
}

@media (max-width: 760px) {
    .top-bar,
    .labs-home-header,
    .lab-model-header {
        grid-template-columns: 1fr;
    }

    .simple-research-mode-toggle {
        width: 100%;
    }

    .labs-model-catalog {
        grid-template-columns: 1fr;
    }

    .lab-header-badges {
        justify-content: flex-start;
    }
}

body[data-lab-ui-mode="simple"] [data-research-only],
body[data-lab-ui-mode="simple"] .section-online,
body[data-lab-ui-mode="simple"] .lab-history-panels,
body[data-lab-ui-mode="simple"] .lab-export-panel textarea,
body[data-lab-ui-mode="simple"] .control-details {
    display: none !important;
}

body[data-lab-ui-mode="simple"] .labs-home {
    display: none;
}

body[data-lab-ui-mode="simple"] .lab-workspace {
    grid-template-columns: minmax(0, 0.7fr) minmax(0, 1.25fr) minmax(0, 0.72fr);
}

body[data-lab-ui-mode="simple"] .lab-left-panels {
    grid-row: 2;
}

body[data-lab-ui-mode="simple"] .lab-export-actions {
    grid-template-columns: 1fr;
}

/* Final responsive Lab shell pass: keep headings and mode buttons at the top, avoid
   narrow desktop controls, and give Simple mode a player-first layout. */
.top-bar {
    padding: 12px;
    border: 1px solid rgba(218, 232, 224, 0.13);
    border-radius: 8px;
    background: rgba(11, 16, 24, 0.88);
}

.top-bar h1 {
    max-width: 980px;
    font-size: clamp(26px, 3.6vw, 48px);
    line-height: 1.06;
}

.simple-research-mode-toggle {
    align-self: start;
    justify-self: end;
    min-width: min(100%, 220px);
}

.lab-export-actions {
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    justify-content: stretch;
}

.lab-export-actions button {
    width: 100%;
    min-width: 0;
    white-space: normal;
    overflow-wrap: anywhere;
}

body[data-lab-ui-mode="simple"] .lab-export-panel {
    display: none !important;
}

@media (min-width: 1180px) {
    .app-shell {
        width: min(1720px, 100%);
        grid-template-columns: 1fr;
    }

    .top-bar,
    .labs-home,
    .lab-model-header,
    .lab-workspace,
    .lab-history-panels,
    .lab-export-panel {
        grid-column: 1;
    }

    .lab-workspace {
        display: grid;
        grid-template-columns: minmax(0, 1fr) clamp(320px, 30vw, 480px);
        gap: 16px;
        align-items: start;
        width: 100%;
        max-width: 100%;
        overflow: clip;
    }

    .lab-workspace > .game-layout {
        grid-column: 1;
        grid-row: 1;
        display: grid;
        grid-template-columns: minmax(0, 1fr);
        gap: 16px;
    }

    .lab-workspace > .game-layout > .board-wrap {
        grid-column: 1 !important;
        grid-row: 1 !important;
        width: 100%;
        max-width: 100%;
    }

    .lab-workspace > .game-layout > .side-panel {
        grid-column: 1 !important;
        grid-row: 2 !important;
        width: 100%;
        max-width: 100%;
    }

    .lab-workspace > .control-panel {
        grid-column: 2;
        grid-row: 1;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        max-height: none;
        overflow: visible;
        align-self: start;
    }

    .lab-workspace > .layer-panel,
    .lab-workspace > .lab-left-panels,
    .lab-workspace > .lab-history-panels,
    .lab-workspace > .lab-export-panel {
        grid-column: 1 / -1;
        grid-row: auto;
    }

    .lab-workspace > .game-layout > .side-panel {
        max-height: none;
        overflow: visible;
    }

    .section-game,
    .section-board,
    .section-actions,
    .section-dynamics,
    .section-algebra,
    .section-online {
        grid-column: 1 / -1;
        border-right: 0;
        padding-right: 0;
        padding-bottom: 10px;
        border-bottom: 1px solid rgba(218, 232, 224, 0.1);
    }

    .section-actions .button-grid,
    .section-board .control-fields.two-col,
    .section-game .control-fields.two-col,
    .section-game .control-fields.three-col,
    .section-dynamics .control-fields,
    .section-online .control-fields {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .section-algebra > .algebra-mode-fields {
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    }

    body[data-lab-ui-mode="simple"] .lab-workspace {
        grid-template-columns: minmax(0, 1fr) clamp(300px, 30vw, 440px);
    }

    body[data-lab-ui-mode="simple"] .lab-workspace > .control-panel {
        grid-column: 2;
        grid-row: 1;
    }

    body[data-lab-ui-mode="simple"] .lab-workspace > .game-layout {
        grid-column: 1;
        grid-row: 1;
    }
}

@media (max-width: 1179px) {
    .top-bar {
        grid-template-columns: auto minmax(0, 1fr) auto;
        align-items: start;
    }

    .lab-workspace > .game-layout > .board-wrap {
        order: 1;
    }

    .lab-workspace > .control-panel {
        order: 2;
    }

    .lab-workspace,
    .lab-workspace > .game-layout,
    .board-wrap,
    .side-panel,
    .control-panel,
    .layer-panel,
    .lab-left-panels,
    .lab-history-panels,
    .lab-export-panel {
        width: 100%;
        max-width: 100%;
        min-width: 0;
    }
}

@media (max-width: 760px) {
    .app-shell {
        padding: 10px;
        gap: 12px;
    }

    .top-bar {
        grid-template-columns: 1fr;
        gap: 10px;
    }

    .home-link,
    .simple-research-mode-toggle {
        width: 100%;
        justify-self: stretch;
    }

    .top-bar h1 {
        font-size: 28px;
        line-height: 1.1;
    }

    .lab-model-header,
    .labs-home-header {
        grid-template-columns: 1fr;
    }

    .control-panel,
    .layer-panel {
        padding: 10px;
    }

    .control-fields.two-col,
    .control-fields.three-col,
    .section-algebra > .algebra-mode-fields,
    .button-grid,
    .section-actions .button-grid {
        grid-template-columns: 1fr;
    }

    .board-wrap {
        min-height: auto;
        padding: 10px;
    }

    .interactive-board-shell {
        min-height: min(92vw, 420px);
    }

    .algebraic-3d-board {
        min-height: 360px;
        height: min(72vh, 520px);
    }
}

/* Last-resort ordering and overflow guard for older rules above. */
.app-shell > .top-bar {
    order: -20 !important;
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) minmax(260px, auto) !important;
    align-items: start !important;
}

.top-bar > .lab-title-block {
    min-width: 0;
}

.top-bar > .lab-title-block h1,
.top-bar > .lab-title-block p {
    overflow-wrap: anywhere;
}

.top-bar > .lab-top-actions,
.top-bar > .game-title-actions.lab-top-actions {
    display: flex !important;
    flex-wrap: wrap !important;
    align-items: flex-start !important;
    justify-content: flex-end !important;
    gap: 8px !important;
    min-width: 0 !important;
    max-width: min(100%, 700px);
    margin-left: 0 !important;
}

.lab-top-actions .simple-research-mode-toggle {
    flex: 1 1 230px;
}

.lab-top-actions .home-link {
    flex: 0 0 auto;
}

.lab-research-builder-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 1 auto;
    min-height: 38px;
    padding: 7px 11px;
    border: 1px solid rgba(242, 196, 100, 0.54);
    border-radius: 7px;
    background: rgba(242, 196, 100, 0.1);
    color: #fff1bd;
    font-size: 14px;
    font-weight: 900;
    line-height: 1.25;
    text-align: center;
    text-decoration: none;
}

.lab-research-builder-link:hover,
.lab-research-builder-link:focus-visible {
    border-color: rgba(242, 196, 100, 0.88);
    background: rgba(242, 196, 100, 0.18);
    outline: none;
}

.lab-top-actions .shared-game-language {
    flex: 0 0 auto;
    min-height: 34px;
}

.simple-research-mode-toggle {
    min-width: 0 !important;
    max-width: 100%;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
}

@media (min-width: 981px) {
    .app-shell > .top-bar {
        grid-template-columns: minmax(360px, 1fr) minmax(570px, auto) !important;
    }
}

.lab-workspace,
.lab-workspace > .game-layout,
.board-wrap,
.side-panel,
.control-panel,
.layer-panel,
.lab-left-panels,
.lab-history-panels,
.lab-export-panel {
    min-width: 0;
    max-width: 100%;
}

@media (max-width: 980px) {
    .app-shell > .top-bar {
        grid-template-columns: 1fr !important;
    }

    .top-bar > .lab-top-actions,
    .top-bar > .game-title-actions.lab-top-actions {
        width: 100%;
        max-width: 100%;
        justify-content: flex-start !important;
    }
}

@media (max-width: 560px) {
    .top-bar > .lab-top-actions,
    .top-bar > .game-title-actions.lab-top-actions {
        display: grid !important;
        grid-template-columns: minmax(0, 1fr) auto auto;
        align-items: center !important;
    }

    .lab-top-actions .simple-research-mode-toggle {
        grid-column: 1 / -1;
        width: 100%;
        min-width: 0 !important;
    }

    .lab-top-actions .lab-research-builder-link {
        grid-column: 1;
        width: 100%;
        min-width: 0;
        font-size: 15px;
    }

    .lab-top-actions .home-link {
        grid-column: 2;
        width: auto;
        min-width: 0;
        padding-inline: 10px;
    }

    .lab-top-actions .shared-game-language {
        grid-column: 3;
    }
}

@media (min-width: 1180px) {
    body[data-lab-ui-mode="simple"] .lab-workspace {
        grid-template-columns: minmax(0, 1fr) minmax(360px, 460px) !important;
    }

    body[data-lab-ui-mode="simple"] .lab-workspace > .game-layout {
        grid-column: 1 !important;
        grid-row: 1 !important;
        display: grid !important;
        grid-template-columns: minmax(0, 1fr) !important;
    }

    body[data-lab-ui-mode="simple"] .lab-workspace > .control-panel {
        grid-column: 2 !important;
        grid-row: 1 !important;
    }

    body[data-lab-ui-mode="simple"] .lab-workspace > .game-layout > .board-wrap,
    body[data-lab-ui-mode="simple"] .lab-workspace > .game-layout > .side-panel {
        grid-column: 1 !important;
        width: 100%;
        max-width: 100%;
    }

    body[data-lab-ui-mode="simple"] .lab-workspace > .game-layout > .board-wrap {
        grid-row: 1 !important;
    }

    body[data-lab-ui-mode="simple"] .lab-workspace > .game-layout > .side-panel {
        grid-row: 2 !important;
    }
}

@media (max-width: 1179px) {
    body[data-lab-ui-mode="simple"] .lab-workspace {
        grid-template-columns: minmax(0, 1fr) !important;
        width: 100%;
        max-width: 100%;
        overflow-x: hidden;
    }

    body[data-lab-ui-mode="simple"] .lab-workspace > .game-layout {
        display: contents !important;
    }

    body[data-lab-ui-mode="simple"] .lab-workspace > .game-layout > .board-wrap,
    body[data-lab-ui-mode="simple"] .lab-workspace > .control-panel,
    body[data-lab-ui-mode="simple"] .lab-workspace > .game-layout > .side-panel,
    body[data-lab-ui-mode="simple"] .lab-workspace > .lab-left-panels,
    body[data-lab-ui-mode="simple"] .lab-workspace > .layer-panel {
        grid-column: 1 !important;
        width: 100%;
        max-width: 100%;
    }
}
