:root {
    --paper: #f7f1e7;
    --paper-strong: #fffaf2;
    --ink: #1d1913;
    --muted: #5f584d;
    --line: #d8ccb8;
    --panel: rgba(255, 250, 242, 0.88);
    --shadow: 0 22px 48px rgba(29, 25, 19, 0.08);
    --sucker: #d18b47;
    --cheater: #c44b3b;
    --grudger: #2d6f62;
    --cooperation: #61789f;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    font-family: 'IBM Plex Sans', sans-serif;
    color: var(--ink);
    background:
        radial-gradient(circle at top left, rgba(196, 75, 59, 0.14), transparent 28%),
        radial-gradient(circle at top right, rgba(45, 111, 98, 0.16), transparent 26%),
        linear-gradient(180deg, #faf5ed 0%, #f3eadc 48%, #efe3cf 100%);
}

.sim-page {
    max-width: 1320px;
    margin: 0 auto;
    padding: 16px;
}

.panel {
    background: var(--panel);
    border: 1px solid rgba(216, 204, 184, 0.9);
    border-radius: 24px;
    box-shadow: var(--shadow);
    -webkit-backdrop-filter: blur(12px);
    backdrop-filter: blur(12px);
}

.topbar {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    align-items: flex-start;
    padding: 16px 18px;
    margin-bottom: 12px;
}

.eyebrow,
.section-kicker {
    margin: 0 0 6px;
    font-size: 0.76rem;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    color: var(--muted);
}

h1,
h2,
h3 {
    margin: 0;
    font-family: 'Fraunces', serif;
    font-weight: 600;
    line-height: 1.1;
}

h1 {
    font-size: clamp(1.85rem, 2.3vw, 2.6rem);
    margin-bottom: 4px;
}

h2 {
    font-size: 1.35rem;
}

h3 {
    font-size: 1rem;
}

.lede,
.subtle,
.notes-grid p,
.event-log,
label,
button,
input,
select,
table {
    font-size: 0.95rem;
}

.lede,
.subtle,
.notes-grid p {
    color: var(--muted);
    line-height: 1.55;
}

.topbar-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: flex-end;
}

.text-link,
.primary-button,
.ghost-button,
.preset-button {
    border-radius: 999px;
    padding: 8px 12px;
    font: inherit;
    text-decoration: none;
    transition: transform 0.18s ease, background-color 0.18s ease, color 0.18s ease, border-color 0.18s ease;
}

.text-link,
.primary-button,
.ghost-button,
.preset-button {
    font-size: 0.9rem;
}

.text-link,
.ghost-button,
.preset-button {
    border: 1px solid var(--line);
    color: var(--ink);
    background: rgba(255, 250, 242, 0.6);
}

.primary-button {
    border: 1px solid transparent;
    color: #fffdf9;
    background: var(--ink);
}

.text-link:hover,
.ghost-button:hover,
.preset-button:hover,
.primary-button:hover {
    transform: translateY(-1px);
}

.preset-button.active {
    background: var(--ink);
    color: #fffdf9;
    border-color: var(--ink);
}

.top-state-grid,
.analysis-grid {
    display: grid;
    gap: 12px;
}

.top-state-grid {
    grid-template-columns: minmax(380px, 0.92fr) minmax(0, 1.08fr);
    align-items: start;
    margin-bottom: 12px;
}

.analysis-grid {
    grid-template-columns: 340px;
    justify-content: end;
    margin-bottom: 12px;
}

.controls-panel,
.state-panel,
.chart-panel,
.matrix-panel,
.log-panel,
.notes-panel {
    padding: 16px;
}

.section-head {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: flex-start;
    margin-bottom: 10px;
}

.section-head.compact {
    margin-bottom: 10px;
}

.button-row,
.preset-row,
.legend-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.viz-head-actions {
    display: grid;
    justify-items: end;
    gap: 8px;
}

.viz-button-row {
    justify-content: flex-end;
}

.compact-legend-row {
    gap: 6px;
}

.controls-form {
    display: grid;
    gap: 10px;
}

.compact-controls-panel {
    padding: 16px 18px;
    margin-bottom: 12px;
}

.controls-topbar,
.control-actions,
.weight-head {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: center;
}

.controls-topbar {
    margin-bottom: 10px;
}

.control-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
}

.compact-controls-form {
    gap: 12px;
}

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

.compact-weight-grid {
    gap: 8px;
}

.weight-card,
.slider-card,
.metric-card {
    border: 1px solid var(--line);
    border-radius: 18px;
    background: rgba(255, 250, 242, 0.72);
}

.weight-card {
    padding: 10px 10px 8px;
}

.weight-card.sucker {
    box-shadow: inset 0 0 0 1px rgba(209, 139, 71, 0.12);
}

.weight-card.cheater {
    box-shadow: inset 0 0 0 1px rgba(196, 75, 59, 0.14);
}

.weight-card.grudger {
    box-shadow: inset 0 0 0 1px rgba(45, 111, 98, 0.14);
}

.weight-meta,
.slider-top,
.bar-head,
.ribbon-head {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    align-items: baseline;
}

.weight-meta {
    margin-top: 4px;
}

.value,
.share,
.badge,
.metric-value,
.bar-value {
    font-variant-numeric: tabular-nums;
}

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

.slider-card {
    padding: 10px;
    min-height: 0;
}

.slider-card label,
.weight-card label {
    font-size: 0.84rem;
    line-height: 1.2;
}

.controls-footer {
    display: grid;
    gap: 10px;
    margin-top: 2px;
}

.compact-controls-footer {
    margin-top: 10px;
}

.compact-controls-footer .subtle {
    font-size: 0.88rem;
}

.wide-button {
    width: 100%;
    justify-content: center;
}

input[type="range"] {
    width: 100%;
    margin-top: 6px;
    accent-color: var(--ink);
}

.metric-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
    margin-bottom: 10px;
}

.metric-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 7px 10px;
    min-height: 0;
    border-radius: 999px;
}

.metric-value {
    display: inline-block;
    margin-top: 0;
    font-size: 0.98rem;
    font-weight: 600;
    color: var(--ink);
    white-space: nowrap;
}

.metric-card .subtle {
    font-size: 0.82rem;
    line-height: 1.2;
    white-space: nowrap;
}

.state-panel h2,
.top-chart-panel h2 {
    font-size: 1.1rem;
}

.state-panel .section-kicker,
.top-chart-panel .section-kicker {
    font-size: 0.72rem;
    margin-bottom: 4px;
}

.badge {
    display: inline-flex;
    align-items: center;
    border-radius: 999px;
    border: 1px solid var(--line);
    padding: 7px 11px;
    background: rgba(255, 250, 242, 0.7);
    color: var(--muted);
    white-space: nowrap;
    font-size: 0.86rem;
}

.ribbon-shell,
.bar-group {
    display: grid;
    gap: 6px;
}

.state-panel .ribbon-head h3,
.state-panel .bar-head h3 {
    font-size: 0.88rem;
}

.state-panel .subtle {
    font-size: 0.8rem;
}

.state-panel .ribbon-head,
.state-panel .bar-head {
    margin-bottom: 2px;
}

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

.population-cell {
    aspect-ratio: 1 / 1;
    border-radius: 5px;
    background: var(--line);
}

.bar-stack {
    display: grid;
    gap: 6px;
}

.top-chart-panel {
    min-width: 0;
    align-self: start;
}

.top-chart-panel #history-canvas {
    height: 250px;
}

@media (max-width: 1440px) {
    .state-panel,
    .top-chart-panel {
        padding: 14px;
    }

    .state-panel h2,
    .top-chart-panel h2 {
        font-size: 0.96rem;
    }

    .metric-grid {
        gap: 6px;
        margin-bottom: 8px;
    }

    .metric-card {
        gap: 8px;
        padding: 6px 8px;
    }

    .metric-card .subtle {
        font-size: 0.74rem;
    }

    .metric-value {
        font-size: 0.88rem;
    }

    .badge {
        padding: 6px 10px;
        font-size: 0.78rem;
    }

    .state-panel .section-head,
    .top-chart-panel .section-head {
        margin-bottom: 8px;
    }

    .state-panel .ribbon-head h3,
    .state-panel .bar-head h3 {
        font-size: 0.84rem;
    }

    .state-panel .subtle {
        font-size: 0.76rem;
    }
}

.bar-row {
    display: grid;
    grid-template-columns: 68px minmax(0, 1fr) 50px;
    gap: 6px;
    align-items: center;
}

.bar-track {
    position: relative;
    overflow: hidden;
    height: 10px;
    border-radius: 999px;
    background: rgba(216, 204, 184, 0.55);
}

.bar-row span {
    font-size: 0.82rem;
}

.bar-fill {
    position: absolute;
    inset: 0 auto 0 0;
    border-radius: 999px;
}

.legend-chip {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 5px 8px;
    border-radius: 999px;
    border: 1px solid var(--line);
    background: rgba(255, 250, 242, 0.62);
    font-size: 0.8rem;
}

.legend-chip::before {
    content: '';
    width: 10px;
    height: 10px;
    border-radius: 999px;
    background: currentColor;
}

.legend-chip.sucker { color: var(--sucker); }
.legend-chip.cheater { color: var(--cheater); }
.legend-chip.grudger { color: var(--grudger); }
.legend-chip.cooperation { color: var(--cooperation); }

#history-canvas {
    display: block;
    width: 100%;
    height: auto;
    border-radius: 18px;
    border: 1px solid rgba(216, 204, 184, 0.8);
    background:
        linear-gradient(to right, rgba(29, 25, 19, 0.05) 1px, transparent 1px),
        linear-gradient(to bottom, rgba(29, 25, 19, 0.05) 1px, transparent 1px),
        rgba(255, 250, 242, 0.56);
    background-size: 44px 44px;
}

.stack-column {
    display: grid;
    gap: 12px;
}

.payoff-table {
    width: 100%;
    border-collapse: collapse;
}

.payoff-table th,
.payoff-table td {
    padding: 8px 8px;
    text-align: left;
    border-bottom: 1px solid rgba(216, 204, 184, 0.72);
    vertical-align: top;
}

.payoff-table th {
    color: var(--muted);
    font-weight: 500;
}

.event-log {
    display: grid;
    gap: 6px;
    max-height: 320px;
    overflow: auto;
    padding-right: 6px;
}

.log-entry {
    padding: 10px 10px;
    border-radius: 12px;
    border: 1px solid var(--line);
    background: rgba(255, 250, 242, 0.64);
}

.log-entry.signal {
    border-color: rgba(45, 111, 98, 0.28);
}

.log-entry.warning {
    border-color: rgba(196, 75, 59, 0.26);
}

.notes-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 12px;
}

.notes-grid article {
    padding: 14px;
    border-radius: 18px;
    border: 1px solid var(--line);
    background: rgba(255, 250, 242, 0.62);
}

.notes-grid p {
    margin: 8px 0 0;
}

.sucker-tone { color: var(--sucker); }
.cheater-tone { color: var(--cheater); }
.grudger-tone { color: var(--grudger); }

@media (max-width: 1120px) {
    .top-state-grid,
    .analysis-grid {
        grid-template-columns: 1fr;
    }

    .stack-column {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .notes-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 760px) {
    .sim-page {
        padding: 14px;
    }

    .topbar,
    .controls-topbar,
    .section-head,
    .section-head.compact {
        flex-direction: column;
        align-items: stretch;
    }

    .topbar-actions,
    .button-row,
    .control-actions {
        justify-content: stretch;
    }

    .viz-head-actions {
        justify-items: stretch;
    }

    .weight-grid,
    .compact-grid,
    .metric-grid,
    .notes-grid,
    .stack-column {
        grid-template-columns: 1fr;
    }

    .metric-card {
        border-radius: 16px;
    }

    .bar-row {
        grid-template-columns: 74px minmax(0, 1fr) 58px;
    }

    .population-ribbon {
        grid-template-columns: repeat(10, minmax(0, 1fr));
    }
}