/* === Reset & Base === */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-surface: #0f3460;
    --bg-input: #1a1a3e;
    --text-primary: #e0e0e0;
    --text-secondary: #a0a0b8;
    --text-muted: #6a6a80;
    --accent: #00b4d8;
    --accent-hover: #0096c7;
    --accent-active: #0077b6;
    --border: #2a2a4a;
    --border-light: #3a3a5a;
    --danger: #e74c3c;
    --success: #27ae60;
    --shadow: rgba(0, 0, 0, 0.3);
    --radius: 6px;
    --radius-lg: 10px;
}

html {
    font-size: 14px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    line-height: 1.5;
}

/* === Header === */
header {
    text-align: center;
    padding: 1.5rem 1rem 1rem;
    border-bottom: 1px solid var(--border);
    background: var(--bg-secondary);
}

header h1 {
    font-size: 2rem;
    font-weight: 700;
    letter-spacing: 0.05em;
    color: var(--accent);
    margin-bottom: 0.15rem;
}

header .subtitle {
    color: var(--text-secondary);
    font-size: 0.95rem;
    font-weight: 400;
}

/* === Main Layout === */
main {
    display: flex;
    flex: 1;
    gap: 0;
    overflow: hidden;
}

/* === Controls Panel === */
.controls {
    width: 280px;
    min-width: 280px;
    padding: 1.25rem;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.control-group label {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.checkbox-group {
    flex-direction: row;
    align-items: center;
    gap: 0.6rem;
}

.checkbox-group label {
    order: 2;
    text-transform: none;
    font-weight: 400;
    font-size: 0.9rem;
    color: var(--text-primary);
}

.checkbox-group input[type="checkbox"] {
    order: 1;
    width: 18px;
    height: 18px;
    accent-color: var(--accent);
    cursor: pointer;
}

/* === Inputs & Selects === */
input[type="number"],
select {
    width: 100%;
    padding: 0.5rem 0.6rem;
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-family: inherit;
    outline: none;
    transition: border-color 0.15s;
}

input[type="number"]:focus,
select:focus {
    border-color: var(--accent);
}

select {
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%23a0a0b8'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.6rem center;
    padding-right: 1.8rem;
}

input[type="range"] {
    width: 100%;
    accent-color: var(--accent);
    cursor: pointer;
}

/* === Size Inputs Row === */
.size-inputs {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.size-inputs input {
    flex: 1;
    min-width: 0;
}

.size-inputs span {
    color: var(--text-muted);
    font-size: 1rem;
}

/* === Seed Row === */
.seed-row {
    display: flex;
    gap: 0.4rem;
}

.seed-row input {
    flex: 1;
    min-width: 0;
}

.seed-row button {
    padding: 0.5rem 0.65rem;
    background: var(--bg-surface);
    color: var(--text-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.75rem;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s, color 0.15s;
}

.seed-row button:hover {
    background: var(--border-light);
    color: var(--text-primary);
}

/* === Speed Row === */
.speed-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.speed-row input[type="range"] {
    flex: 1;
}

.speed-row span {
    font-size: 0.75rem;
    color: var(--text-muted);
    white-space: nowrap;
    min-width: 6em;
    text-align: right;
}

/* === Buttons === */
.buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.4rem;
    margin-top: 0.3rem;
}

button {
    padding: 0.55rem 0.8rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg-surface);
    color: var(--text-primary);
    font-size: 0.85rem;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, transform 0.08s;
}

button:hover:not(:disabled) {
    background: var(--border-light);
    border-color: var(--border-light);
}

button:active:not(:disabled) {
    transform: scale(0.97);
}

button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

button.primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    font-weight: 600;
}

button.primary:hover:not(:disabled) {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
}

button.primary:active:not(:disabled) {
    background: var(--accent-active);
    border-color: var(--accent-active);
}

button.active {
    background: var(--danger);
    border-color: var(--danger);
    color: #fff;
}

button.active:hover:not(:disabled) {
    background: #c0392b;
    border-color: #c0392b;
}

/* === Message === */
.message {
    padding: 0.6rem 0.75rem;
    border-radius: var(--radius);
    font-size: 0.85rem;
    font-weight: 500;
    text-align: center;
    transition: opacity 0.3s;
}

.message.info {
    background: rgba(0, 180, 216, 0.15);
    border: 1px solid rgba(0, 180, 216, 0.3);
    color: var(--accent);
}

.message.success {
    background: rgba(39, 174, 96, 0.15);
    border: 1px solid rgba(39, 174, 96, 0.3);
    color: var(--success);
}

.message.error {
    background: rgba(231, 76, 60, 0.15);
    border: 1px solid rgba(231, 76, 60, 0.3);
    color: var(--danger);
}

.hidden {
    display: none !important;
}

/* === Stats === */
.stats {
    font-size: 0.8rem;
    color: var(--text-secondary);
    line-height: 1.6;
}

.stats .stat-row {
    display: flex;
    justify-content: space-between;
    padding: 0.15rem 0;
    border-bottom: 1px solid var(--border);
}

.stats .stat-row:last-child {
    border-bottom: none;
}

.stats .stat-label {
    color: var(--text-muted);
}

.stats .stat-value {
    font-weight: 600;
    color: var(--text-primary);
}

/* === Legend === */
.legend h3 {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin-bottom: 0.4rem;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.2rem 0;
    font-size: 0.85rem;
}

.swatch {
    display: inline-block;
    width: 14px;
    height: 14px;
    border-radius: 3px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    flex-shrink: 0;
}

/* === Canvas Container === */
.canvas-container {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    overflow: auto;
    background: var(--bg-primary);
}

#canvas {
    border: 2px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: 0 4px 24px var(--shadow), 0 0 80px rgba(0, 180, 216, 0.04);
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

/* === Footer === */
footer {
    text-align: center;
    padding: 1rem;
    border-top: 1px solid var(--border);
    background: var(--bg-secondary);
    font-size: 0.8rem;
    color: var(--text-muted);
}

footer a {
    color: var(--accent);
    text-decoration: none;
}

footer a:hover {
    text-decoration: underline;
}

/* === Responsive === */
@media (max-width: 768px) {
    main {
        flex-direction: column;
        overflow: visible;
    }

    .controls {
        width: 100%;
        min-width: 0;
        border-right: none;
        border-bottom: 1px solid var(--border);
        max-height: none;
    }

    .canvas-container {
        padding: 1rem;
    }

    .buttons {
        grid-template-columns: 1fr 1fr;
    }
}

@media (max-width: 480px) {
    header h1 {
        font-size: 1.5rem;
    }

    .controls {
        padding: 1rem;
    }

    .canvas-container {
        padding: 0.5rem;
    }
}

/* === 3D Container === */
#three-container {
    width: 100%;
    height: 100%;
    min-height: 500px;
    position: relative;
}

#three-container canvas {
    display: block;
    border-radius: var(--radius-lg);
}

.three-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.5);
    cursor: pointer;
    z-index: 10;
    border-radius: var(--radius-lg);
}

.three-overlay.hidden {
    display: none;
}

.three-overlay-text {
    color: var(--text-primary);
    font-size: 1.2rem;
    font-weight: 600;
    text-align: center;
    padding: 1.5rem 2rem;
    background: rgba(0, 0, 0, 0.6);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
}

.three-overlay-text small {
    font-size: 0.8rem;
    font-weight: 400;
    color: var(--text-secondary);
    display: block;
    margin-top: 0.3rem;
}

.three-hints {
    position: absolute;
    bottom: 12px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.7);
    color: var(--text-secondary);
    font-size: 0.75rem;
    padding: 0.4rem 0.8rem;
    border-radius: var(--radius);
    z-index: 10;
    white-space: nowrap;
    pointer-events: none;
    transition: opacity 0.5s;
}

.three-hints.three-hints-hidden {
    opacity: 0;
    pointer-events: none;
}

/* === How It Works === */
.how-it-works {
    margin-top: 0.5rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

.how-it-works summary {
    padding: 0.6rem 0.75rem;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    cursor: pointer;
    background: var(--bg-surface);
    user-select: none;
}

.how-it-works summary:hover {
    color: var(--text-primary);
}

.how-it-works[open] summary {
    border-bottom: 1px solid var(--border);
}

#how-it-works-content {
    padding: 0.75rem;
    font-size: 0.82rem;
    line-height: 1.6;
    color: var(--text-secondary);
}

#how-it-works-content p {
    margin-bottom: 0.6rem;
}

#how-it-works-content h4 {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0.6rem 0 0.3rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.adjacency-matrix {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.7rem;
    margin-top: 0.3rem;
}

.adjacency-matrix th,
.adjacency-matrix td {
    padding: 0.2rem 0.3rem;
    text-align: center;
    border: 1px solid var(--border);
    min-width: 1.8em;
}

.adjacency-matrix th {
    background: var(--bg-surface);
    color: var(--text-secondary);
    font-weight: 500;
    font-size: 0.65rem;
    white-space: nowrap;
}

.adjacency-matrix td.compat {
    background: rgba(39, 174, 96, 0.2);
    color: var(--success);
}

.adjacency-matrix td.incompat {
    background: transparent;
    color: var(--text-muted);
}

.matrix-swatch {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 2px;
    margin-right: 3px;
    vertical-align: middle;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.matrix-note {
    font-size: 0.72rem;
    color: var(--text-muted);
    font-style: italic;
    margin-top: 0.4rem;
}
