* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --color-bg: #FAFAFA;
    --color-header-bg: #1a237e;
    --color-header-text: #ffffff;
    --color-card-bg: #ffffff;
    --color-card-shadow: rgba(0, 0, 0, 0.1);
    --color-border: #e0e0e0;
    --color-text: #333333;
    --color-text-light: #666666;
    --color-side-a: #2196F3;
    --color-side-a-dark: #1976D2;
    --color-side-b: #F44336;
    --color-side-b-dark: #D32F2F;
    --color-dss: #FF9800;
    --color-ses: #9C27B0;
    --color-btn-primary: #1a237e;
    --color-btn-secondary: #e0e0e0;
    --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
}

body {
    font-family: var(--font-family);
    background: var(--color-bg);
    color: var(--color-text);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

header {
    background: var(--color-header-bg);
    color: var(--color-header-text);
    padding: 24px 32px;
    text-align: center;
}

header h1 {
    font-size: 1.6rem;
    font-weight: 600;
    margin-bottom: 6px;
}

header .subtitle {
    font-size: 0.95rem;
    opacity: 0.85;
}

.app-container {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.control-panel {
    width: 300px;
    min-width: 300px;
    background: var(--color-card-bg);
    border-right: 1px solid var(--color-border);
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.panel-section h3 {
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-text-light);
    margin-bottom: 8px;
    border-bottom: 1px solid var(--color-border);
    padding-bottom: 4px;
}

.control-panel select {
    width: 100%;
    padding: 8px;
    border: 1px solid var(--color-border);
    border-radius: 4px;
    font-size: 0.9rem;
    background: var(--color-card-bg);
}

.preset-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
}

.preset-btn {
    padding: 6px 8px;
    border: 1px solid var(--color-border);
    border-radius: 4px;
    background: var(--color-card-bg);
    font-size: 0.8rem;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

.preset-btn:hover {
    background: var(--color-header-bg);
    color: white;
    border-color: var(--color-header-bg);
}

.preset-btn.active {
    background: var(--color-header-bg);
    color: white;
    border-color: var(--color-header-bg);
}

.slider-group {
    margin-bottom: 10px;
}

.slider-group label {
    display: block;
    font-size: 0.85rem;
    font-weight: 500;
    margin-bottom: 2px;
}

.param-desc {
    display: block;
    font-size: 0.75rem;
    color: var(--color-text-light);
    font-weight: 400;
    margin-bottom: 2px;
}

.slider-group input[type="range"] {
    width: calc(100% - 40px);
    vertical-align: middle;
}

.slider-value {
    display: inline-block;
    width: 32px;
    text-align: right;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--color-header-bg);
}

.side-input {
    margin-bottom: 8px;
}

.side-input label {
    display: block;
    font-size: 0.85rem;
    margin-bottom: 2px;
}

.side-input input[type="text"] {
    width: 100%;
    padding: 6px 8px;
    border: 1px solid var(--color-border);
    border-radius: 4px;
    font-size: 0.9rem;
}

.action-buttons {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.btn-primary, .btn-secondary {
    padding: 10px 16px;
    border: none;
    border-radius: 4px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.15s;
}

.btn-primary {
    background: var(--color-btn-primary);
    color: white;
}

.btn-primary:hover {
    opacity: 0.9;
}

.btn-secondary {
    background: var(--color-btn-secondary);
    color: var(--color-text);
}

.btn-secondary:hover:not(:disabled) {
    background: #d0d0d0;
}

.btn-secondary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.speed-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.speed-option {
    font-size: 0.78rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 3px;
    padding: 2px 4px;
    border-radius: 3px;
    transition: background 0.15s;
}

.speed-option:hover {
    background: var(--color-bg);
}

.speed-option input[type="radio"] {
    margin: 0;
    width: 12px;
    height: 12px;
}

.visualization-area {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.chart-card {
    background: var(--color-card-bg);
    border-radius: 8px;
    box-shadow: 0 2px 8px var(--color-card-shadow);
    padding: 20px;
}

.chart-card h2 {
    font-size: 1.1rem;
    margin-bottom: 12px;
    color: var(--color-header-bg);
}

.chart-container {
    width: 100%;
    position: relative;
}

.chart-container canvas {
    width: 100%;
    height: 280px;
    display: block;
}

.chart-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid var(--color-border);
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.8rem;
}

.legend-swatch {
    width: 14px;
    height: 14px;
    border-radius: 2px;
}

.outcome-card {
    text-align: center;
    padding: 32px 20px;
}

.outcome-text {
    font-size: 1.8rem;
    font-weight: 700;
    color: var(--color-header-bg);
    margin-bottom: 16px;
}

.outcome-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 12px;
    text-align: left;
}

.stat-item {
    padding: 8px 12px;
    background: var(--color-bg);
    border-radius: 4px;
}

.stat-label {
    font-size: 0.75rem;
    color: var(--color-text-light);
    text-transform: uppercase;
}

.stat-value {
    font-size: 1rem;
    font-weight: 600;
}

footer {
    background: var(--color-header-bg);
    color: var(--color-header-text);
    text-align: center;
    padding: 12px;
    font-size: 0.8rem;
    opacity: 0.85;
}

footer a {
    color: var(--color-header-text);
}

/* History Mode styles */
.history-toggle-wrap {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 0;
}

.history-toggle-wrap label {
    font-size: 0.85rem;
    font-weight: 500;
}

.toggle-switch {
    position: relative;
    display: inline-block;
    width: 42px;
    height: 22px;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    cursor: pointer;
    top: 0; left: 0; right: 0; bottom: 0;
    background: #ccc;
    border-radius: 22px;
    transition: background 0.3s;
}

.toggle-slider::before {
    content: '';
    position: absolute;
    height: 16px;
    width: 16px;
    left: 3px;
    bottom: 3px;
    background: white;
    border-radius: 50%;
    transition: transform 0.3s;
}

.toggle-switch input:checked + .toggle-slider {
    background: var(--color-header-bg);
}

.toggle-switch input:checked + .toggle-slider::before {
    transform: translateX(20px);
}

.app-container.history-active .visualization-area {
    display: grid;
    grid-template-columns: 1fr 280px;
    grid-template-rows: auto auto auto;
    gap: 16px;
}

.app-container.history-active .chart-card:nth-child(1) {
    grid-column: 1;
}

.app-container.history-active .chart-card:nth-child(2) {
    grid-column: 1;
}

.app-container.history-active .chart-card.outcome-card {
    grid-column: 1;
}

.app-container.history-active .confidence-panel {
    grid-column: 1;
}

.history-sidebar {
    grid-column: 2;
    grid-row: 1 / -1;
    background: var(--color-card-bg);
    border-radius: 8px;
    box-shadow: 0 2px 8px var(--color-card-shadow);
    padding: 16px;
    overflow-y: auto;
    max-height: 700px;
    display: none;
}

.app-container.history-active .history-sidebar {
    display: block;
}

.history-sidebar h3 {
    font-size: 0.9rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-text-light);
    margin-bottom: 10px;
    border-bottom: 1px solid var(--color-border);
    padding-bottom: 4px;
}

.event-list {
    list-style: none;
    padding: 0;
    margin: 0 0 16px 0;
}

.event-item {
    padding: 8px 10px;
    border-left: 3px solid transparent;
    margin-bottom: 4px;
    border-radius: 0 4px 4px 0;
    cursor: pointer;
    transition: background 0.15s;
    font-size: 0.82rem;
    position: relative;
}

.event-item:hover {
    background: var(--color-bg);
}

.event-item.active {
    background: #e8eaf6;
}

.event-item[data-type="trigger"]   { border-left-color: #F44336; }
.event-item[data-type="military"]  { border-left-color: #2196F3; }
.event-item[data-type="shock"]     { border-left-color: #FF9800; }
.event-item[data-type="political"] { border-left-color: #9C27B0; }
.event-item[data-type="outcome"]   { border-left-color: #4CAF50; }

.event-month {
    font-weight: 600;
    font-size: 0.75rem;
    color: var(--color-text-light);
}

.event-label {
    display: block;
    margin-top: 2px;
}

.event-type-badge {
    display: inline-block;
    font-size: 0.65rem;
    padding: 1px 5px;
    border-radius: 3px;
    text-transform: uppercase;
    font-weight: 600;
    margin-left: 4px;
}

.event-type-badge.trigger   { background: #FFEBEE; color: #C62828; }
.event-type-badge.military  { background: #E3F2FD; color: #1565C0; }
.event-type-badge.shock     { background: #FFF3E0; color: #E65100; }
.event-type-badge.political { background: #F3E5F5; color: #6A1B9A; }
.event-type-badge.outcome   { background: #E8F5E9; color: #2E7D32; }

.event-detail {
    background: var(--color-bg);
    border-radius: 6px;
    padding: 12px;
    margin-top: 8px;
    font-size: 0.82rem;
    display: none;
}

.event-detail.visible {
    display: block;
}

.event-detail h4 {
    margin-bottom: 6px;
    font-size: 0.85rem;
}

.event-detail .model-state {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px;
    margin-top: 6px;
}

.event-detail .model-state span {
    font-size: 0.75rem;
}

.event-detail .model-state .label {
    color: var(--color-text-light);
}

.mechanism-breakdown {
    margin-top: 12px;
    padding: 12px;
    background: var(--color-bg);
    border-radius: 6px;
}

.mechanism-breakdown h4 {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-text-light);
    margin-bottom: 8px;
}

.mechanism-bar {
    height: 24px;
    border-radius: 4px;
    overflow: hidden;
    display: flex;
    margin-bottom: 6px;
}

.mechanism-bar .bar-segment {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    font-weight: 600;
    color: white;
    min-width: 30px;
    transition: width 0.5s;
}

.mechanism-bar .bar-exhaustion { background: var(--color-ses); }
.mechanism-bar .bar-shock      { background: var(--color-dss); }
.mechanism-bar .bar-mixed      { background: #9E9E9E; }

.mechanism-labels {
    font-size: 0.8rem;
    color: var(--color-text);
    line-height: 1.5;
}

.mechanism-labels .pct {
    font-weight: 700;
}

.mechanism-labels .lbl-exhaustion { color: var(--color-ses); }
.mechanism-labels .lbl-shock      { color: var(--color-dss); }

.confidence-panel {
    background: white;
    border-radius: 8px;
    padding: 16px;
    margin-top: 12px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.confidence-panel h3 {
    font-size: 1.1rem;
    margin-bottom: 12px;
    color: var(--color-header-bg);
}

.confidence-indicator {
    text-align: center;
}

.confidence-level {
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 8px;
}

.confidence-level.high { color: #4CAF50; }
.confidence-level.medium { color: #FF9800; }
.confidence-level.low { color: #F44336; }

.confidence-bar {
    width: 100%;
    height: 8px;
    background: #E0E0E0;
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 8px;
}

.confidence-fill {
    height: 100%;
    border-radius: 4px;
    transition: width 0.3s, background 0.3s;
}

.confidence-fill.high { background: #4CAF50; width: 90%; }
.confidence-fill.medium { background: #FF9800; width: 60%; }
.confidence-fill.low { background: #F44336; width: 30%; }

.confidence-text {
    font-size: 0.85rem;
    color: #666;
    line-height: 1.4;
}

.historical-comparison {
    padding: 10px 12px;
    border-radius: 6px;
    margin-bottom: 12px;
}

.historical-comparison.match { background: #E8F5E9; border-left: 4px solid #4CAF50; }
.historical-comparison.partial-match { background: #FFF8E1; border-left: 4px solid #FF9800; }
.historical-comparison.mismatch { background: #FFEBEE; border-left: 4px solid #F44336; }

.comparison-header {
    font-weight: 600;
    font-size: 0.9rem;
    margin-bottom: 6px;
}

.comparison-row {
    font-size: 0.82rem;
    margin-bottom: 2px;
}

.comp-label {
    color: var(--color-text-light);
}

.comparison-divider {
    border: none;
    border-top: 1px solid var(--color-border);
    margin: 12px 0;
}

.no-history-msg {
    text-align: center;
    color: var(--color-text-light);
    font-size: 0.85rem;
    padding: 30px 10px;
}

.app-status {
    font-size: 0.8rem;
    color: var(--color-text-light);
    text-align: center;
    padding: 4px 0;
}

.event-marker-line {
    pointer-events: none;
}

@media (max-width: 900px) {
    .app-container {
        flex-direction: column;
    }
    .control-panel {
        width: 100%;
        min-width: unset;
        border-right: none;
        border-bottom: 1px solid var(--color-border);
        max-height: 50vh;
    }
}
