:root {
    --bg: #0e1016;
    --panel: #161922;
    --card: #1c202b;
    --border: #2b3040;
    --text: #e3e7f0;
    --muted: #8d95a9;
    --faint: #5c6478;
    --accent: #4fd1a5;
    --blue: #5e9cff;
    --amber: #ffb547;
    --red: #ff6b6b;
    --mono: ui-monospace, 'JetBrains Mono', 'SF Mono', Menlo, Consolas, monospace;
    --radius: 10px;
}

* {
    box-sizing: border-box;
}

html,
body {
    height: 100%;
}

body {
    margin: 0;
    font-family:
        system-ui,
        -apple-system,
        'Segoe UI',
        Inter,
        Roboto,
        sans-serif;
    font-size: 13px;
    background: var(--bg);
    color: var(--text);
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* ---- Sidebar ------------------------------------------------------------- */
#sidebar {
    width: 340px;
    min-width: 340px;
    background: linear-gradient(180deg, #181c26 0%, var(--panel) 100%);
    padding: 14px 14px 28px;
    overflow-y: auto;
    border-right: 1px solid var(--border);
    scrollbar-width: thin;
    scrollbar-color: #33394a transparent;
}
#sidebar::-webkit-scrollbar {
    width: 8px;
}
#sidebar::-webkit-scrollbar-thumb {
    background: #33394a;
    border-radius: 4px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 2px 2px 14px;
}
.brand .logo {
    width: 38px;
    height: 38px;
    border-radius: 11px;
    display: grid;
    place-items: center;
    font-size: 19px;
    background: linear-gradient(135deg, #2c3e66, #1f6b58);
    box-shadow: 0 4px 16px rgba(79, 209, 165, 0.18);
}
h1 {
    font-size: 16px;
    margin: 0;
    font-weight: 650;
    letter-spacing: 0.01em;
}
.subtitle {
    font-size: 11px;
    color: var(--muted);
}

.mode-tabs {
    display: flex;
    gap: 4px;
    padding: 4px;
    background: #111420;
    border: 1px solid var(--border);
    border-radius: 12px;
    margin-bottom: 10px;
}
.mode-tabs button {
    flex: 1;
    padding: 8px 6px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    font-size: 13px;
    font-weight: 600;
    transition:
        background 0.15s,
        color 0.15s;
}
.mode-tabs button:hover {
    color: var(--text);
    background: #1a1e2a;
}
.mode-tabs button.active {
    background: linear-gradient(135deg, #2f7a66, #3aa084);
    color: #fff;
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.06);
}

.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px;
    margin: 10px 0;
}
.card > h2:first-child {
    margin-top: 0;
}
h2 {
    font-size: 11px;
    margin: 14px 0 8px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.09em;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 8px;
}
.badge {
    font-size: 10px;
    background: #2a3140;
    color: var(--accent);
    padding: 1px 7px;
    border-radius: 999px;
    font-weight: 600;
    letter-spacing: 0;
}
.badge:empty {
    display: none;
}

.hint {
    font-size: 11px;
    color: var(--muted);
    margin: 6px 0;
    line-height: 1.45;
}
kbd {
    font-family: var(--mono);
    font-size: 10px;
    background: #2a2f3d;
    border: 1px solid #3a4052;
    border-bottom-width: 2px;
    border-radius: 4px;
    padding: 0 4px;
    color: #cfd5e3;
}

/* ---- Controls ------------------------------------------------------------ */
.row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 8px 0;
}
.row label {
    flex: 1;
    font-size: 12px;
    color: #c3c9d8;
}
.row .val {
    width: 52px;
    text-align: right;
    font-family: var(--mono);
    font-size: 11.5px;
    color: var(--blue);
    font-variant-numeric: tabular-nums;
}
input[type='range'] {
    flex: 2;
    -webkit-appearance: none;
    appearance: none;
    height: 4px;
    background: #2c3242;
    border-radius: 2px;
    outline: none;
}
input[type='range']::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--blue);
    border: 2px solid #0f1117;
    box-shadow: 0 0 0 2px #2c3242;
    cursor: pointer;
    transition: transform 0.1s;
}
input[type='range']::-webkit-slider-thumb:hover {
    transform: scale(1.15);
}
input[type='range']::-moz-range-thumb {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--blue);
    border: 2px solid #0f1117;
    cursor: pointer;
}
input[type='range']::-moz-range-track {
    height: 4px;
    background: #2c3242;
    border-radius: 2px;
}
input[type='number'],
select {
    background: #101320;
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 5px 7px;
    font-size: 12px;
    font-family: var(--mono);
}
input[type='number'] {
    width: 70px;
}
select {
    flex: 2;
    font-family: inherit;
}
input[type='number']:focus,
select:focus,
textarea:focus {
    outline: none;
    border-color: var(--blue);
    box-shadow: 0 0 0 2px rgba(94, 156, 255, 0.2);
}
.check {
    display: flex;
    align-items: center;
    gap: 7px;
    font-size: 12px;
    color: #c3c9d8;
    margin: 8px 0;
    cursor: pointer;
}
input[type='checkbox'] {
    accent-color: var(--accent);
    width: 14px;
    height: 14px;
    margin: 0;
}

button.action {
    padding: 7px 12px;
    margin: 0;
    border: 1px solid transparent;
    border-radius: 8px;
    background: #2a3142;
    color: #e6eaf3;
    cursor: pointer;
    font-size: 12.5px;
    font-weight: 600;
    white-space: nowrap;
    transition:
        background 0.15s,
        transform 0.05s,
        box-shadow 0.15s,
        filter 0.15s;
}
button.action:hover {
    background: #333b50;
}
button.action:active {
    transform: translateY(1px);
}
button.action.primary {
    background: linear-gradient(135deg, #3d78d6, #5e9cff);
    color: #fff;
    box-shadow: 0 2px 10px rgba(94, 156, 255, 0.25);
}
button.action.primary:hover {
    filter: brightness(1.08);
}
button.action.secondary {
    background: transparent;
    border-color: var(--border);
    color: var(--muted);
}
button.action.secondary:hover {
    color: var(--text);
    background: #1f2432;
}
button.action.danger {
    background: #3a2430;
    color: #ffb3b3;
    border-color: #4d2c38;
}
button.action.danger:hover {
    background: #4a2a39;
}
button.action:disabled,
button.action:disabled:hover {
    background: #20242f;
    color: #5c6478;
    cursor: default;
    box-shadow: none;
    filter: none;
    transform: none;
}
.toolbar {
    display: flex;
    gap: 6px;
    margin: 8px 0;
    flex-wrap: wrap;
}
.toolbar button.action {
    flex: 1;
}

/* ---- Readouts ------------------------------------------------------------ */
.stats {
    display: grid;
    grid-template-columns: 1fr;
    gap: 4px 8px;
    font-family: var(--mono);
    font-size: 12px;
    margin: 8px 0;
}
.stats.grid2 {
    grid-template-columns: 1fr 1fr;
}
.stats > div {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    background: #171b25;
    border: 1px solid #232838;
    border-radius: 6px;
    padding: 5px 8px;
    min-width: 0;
}
.stats .lbl {
    color: var(--muted);
}
.stats .v {
    color: var(--blue);
    font-variant-numeric: tabular-nums;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.readout {
    font-family: var(--mono);
    font-size: 11.5px;
    line-height: 1.6;
    color: var(--muted);
    min-height: 1.6em;
}
.readout span {
    color: var(--blue);
}

textarea {
    width: 100%;
    height: 150px;
    background: #0d1018;
    color: #9ed8a5;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-family: var(--mono);
    font-size: 11px;
    padding: 8px;
    resize: vertical;
}
.status {
    font-size: 11.5px;
    color: #8fd1a8;
    min-height: 16px;
    margin: 6px 0;
    line-height: 1.4;
}
.status.err {
    color: var(--red);
}

canvas.small {
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #0d1018;
    display: block;
    margin: 8px 0;
    width: 100%;
    cursor: crosshair;
    touch-action: none;
}
#heatmap-wrap,
#transitions-wrap {
    display: none;
}
#heatmap-wrap.show,
#transitions-wrap.show {
    display: block;
}

/* ---- Catalog ------------------------------------------------------------- */
#catalog {
    max-height: 260px;
    overflow-y: auto;
    margin: 6px -4px;
    padding: 0 4px;
    scrollbar-width: thin;
    scrollbar-color: #33394a transparent;
}
.catalog-item {
    position: relative;
    overflow: hidden;
    padding: 7px 9px;
    margin: 5px 0;
    border-radius: 8px;
    background: #171b25;
    border: 1px solid #242938;
    cursor: pointer;
    transition:
        border-color 0.12s,
        background 0.12s;
}
.catalog-item:hover {
    border-color: #3a4256;
    background: #1b2030;
}
.catalog-item.selected {
    border-color: var(--accent);
    box-shadow: 0 0 0 1px rgba(79, 209, 165, 0.35);
}
.catalog-item.saddle {
    border-style: dashed;
    opacity: 0.85;
}
.catalog-item .share {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    background: linear-gradient(90deg, rgba(94, 156, 255, 0.18), rgba(94, 156, 255, 0.04));
    pointer-events: none;
}
.catalog-item .head {
    position: relative;
    display: flex;
    gap: 8px;
    align-items: baseline;
    font-family: var(--mono);
    font-size: 12px;
}
.catalog-item .sid {
    color: var(--accent);
    font-weight: 700;
}
.catalog-item .u {
    color: var(--text);
}
.catalog-item .w {
    color: var(--muted);
    margin-left: auto;
    font-size: 11px;
}
.catalog-item .meta {
    position: relative;
    font-family: var(--mono);
    font-size: 10.5px;
    color: var(--muted);
    margin-top: 2px;
}
.chips {
    position: relative;
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin-top: 5px;
}
.chips:empty {
    display: none;
}
.chip {
    font-size: 10px;
    padding: 1px 6px;
    border-radius: 999px;
    background: #232a3b;
    color: #aeb6c9;
    border: 1px solid #2d3549;
    white-space: nowrap;
}
.chip.warn {
    color: var(--amber);
    border-color: #5a4520;
    background: #2c2418;
}
.chip.bad {
    color: var(--red);
    border-color: #5a2a2a;
    background: #2c1a1a;
}
.empty {
    color: var(--faint);
    font-size: 11.5px;
    padding: 8px 2px;
    font-style: italic;
}

/* ---- Transition detail --------------------------------------------------- */
#state-detail {
    margin-top: 6px;
}
.detail-head {
    font-family: var(--mono);
    font-size: 12px;
    margin: 8px 0 4px;
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}
.detail-head b {
    color: var(--accent);
}
.kv {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 3px 10px;
    font-size: 11.5px;
    margin: 6px 0;
}
.kv .k {
    color: var(--muted);
    white-space: nowrap;
}
.kv .v {
    font-family: var(--mono);
    color: #d5dae6;
    font-size: 11px;
    word-break: break-word;
}
.kv .v.warn {
    color: var(--amber);
}
.rays-wrap {
    max-height: 200px;
    overflow: auto;
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-top: 6px;
    scrollbar-width: thin;
    scrollbar-color: #33394a transparent;
}
table.rays {
    width: 100%;
    border-collapse: collapse;
    font-family: var(--mono);
    font-size: 10.5px;
}
table.rays th {
    position: sticky;
    top: 0;
    background: #1a1e29;
    text-align: left;
    color: var(--muted);
    font-weight: 600;
    padding: 4px 6px;
    border-bottom: 1px solid var(--border);
}
table.rays td {
    padding: 2px 6px;
    border-bottom: 1px solid #1f2431;
    color: #c9cfdd;
    white-space: nowrap;
}
table.rays tr:last-child td {
    border-bottom: none;
}
table.rays tr.stays td {
    color: var(--faint);
}
table.rays td.flag {
    color: var(--amber);
    text-align: center;
}

/* ---- Scene --------------------------------------------------------------- */
#main {
    flex: 1;
    position: relative;
    overflow: hidden;
    min-width: 0;
    background: radial-gradient(1200px 800px at 60% 40%, #131722 0%, #0c0e14 70%);
}
canvas#scene {
    /* Absolutely positioned so the backing-store size (width/height attributes) can
       never feed back into layout (see SceneRenderer.resize). */
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
    background: transparent;
    cursor: crosshair;
    touch-action: none;
}

/* ---- Settings dialog ----------------------------------------------------- */
dialog#config-dialog {
    background: var(--card);
    color: var(--text);
    border: 1px solid #3a4052;
    border-radius: 12px;
    padding: 18px 20px;
    width: 400px;
    max-width: 90vw;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
dialog#config-dialog::backdrop {
    background: rgba(5, 7, 12, 0.7);
    backdrop-filter: blur(2px);
}
dialog#config-dialog h2 {
    margin-top: 0;
}
dialog#config-dialog .row label {
    flex: 2;
}
dialog#config-dialog input[type='number'] {
    width: 90px;
}
dialog#config-dialog menu {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 0;
    margin: 16px 0 0;
}
