:root {
    --bg: #000000;
    --bg-elevated: #0a0a0a;
    --bg-card: #0f1115;
    --bg-card-2: #16181c;
    --bg-hover: #1c1f24;
    --border: #2f3336;
    --border-hover: #3a3f45;
    --text: #e7e9ea;
    --text-muted: #71767b;
    --primary: #1d9bf0;
    --primary-hover: #1a8cd8;
    --primary-soft: rgba(29, 155, 240, 0.12);
    --danger: #f4212e;
    --success: #00ba7c;
    --free: #2f3336;
    --radius: 16px;
    --radius-sm: 12px;
    --radius-pill: 999px;
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
    --focus-ring: 0 0 0 3px rgba(29, 155, 240, 0.35);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

.page-wrapper {
    display: flex;
    justify-content: center;
    padding: 2rem 1rem;
}

.simulator {
    width: 100%;
    max-width: 1000px;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* Header */
.header {
    text-align: center;
    padding: 0.75rem 0 0.25rem;
}

.header h1 {
    font-size: 1.9rem;
    font-weight: 800;
    letter-spacing: -0.02em;
    color: var(--text);
}

.header h1::selection {
    background: var(--primary);
    color: #fff;
}

.subtitle {
    color: var(--text-muted);
    margin-top: 0.25rem;
    font-size: 0.95rem;
}

/* Cards */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    transition: border-color 0.2s ease;
}

.card:hover {
    border-color: var(--border-hover);
}

.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.card-header h2 {
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--text);
    letter-spacing: -0.01em;
}

/* Memory visualization */
.memory-container {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    min-height: 64px;
}

.block {
    flex: 1 1 0;
    min-width: 44px;
    height: 56px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.72rem;
    font-weight: 600;
    color: #fff;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    border: 1px solid rgba(255, 255, 255, 0.18);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
    cursor: default;
    transition: background-color 0.5s ease, transform 0.3s ease, box-shadow 0.3s ease;
    animation: fadeIn 0.5s ease;
}

.block.free {
    background: var(--free);
    color: #cbd5e1;
    border-color: rgba(255, 255, 255, 0.1);
}

.block.allocs {
    animation: popIn 0.45s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.block.freed {
    animation: fadeToGray 0.6s ease forwards;
}

.block:hover {
    transform: translateY(-4px) scale(1.04);
    box-shadow: 0 10px 22px rgba(0, 0, 0, 0.45);
    z-index: 2;
}

.block .block-index {
    position: absolute;
    font-size: 0.55rem;
    opacity: 0.6;
    top: 2px;
    right: 4px;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes popIn {
    0% { transform: scale(0.6); opacity: 0; }
    60% { transform: scale(1.12); }
    100% { transform: scale(1); opacity: 1; }
}

@keyframes fadeToGray {
    0% { background: #fff; }
    100% { background: var(--free); }
}

/* Legend */
.legend {
    display: flex;
    gap: 1rem;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.legend-item {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
}

.legend-swatch {
    width: 14px;
    height: 14px;
    border-radius: 4px;
    display: inline-block;
}

.legend-swatch.free { background: var(--free); }
.legend-swatch.allocated { background: var(--primary); }

/* Grid layout for controls + stats */
.grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.25rem;
}

@media (max-width: 720px) {
    .grid {
        grid-template-columns: 1fr;
    }
}

/* Controls form */
.controls-form {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.controls-form label {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
    display: block;
}

.controls-form input,
.controls-form select {
    width: 100%;
    padding: 0.65rem 0.8rem;
    border-radius: var(--radius-pill);
    border: 1px solid var(--border);
    background: var(--bg-elevated);
    color: var(--text);
    font-size: 0.95rem;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.controls-form input:focus,
.controls-form select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: var(--focus-ring);
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.65rem 1.2rem;
    border: none;
    border-radius: var(--radius-pill);
    font-size: 0.95rem;
    font-weight: 700;
    cursor: pointer;
    transition: transform 0.15s ease, background 0.2s ease, box-shadow 0.2s ease;
}

.btn:active {
    transform: scale(0.97);
}

.btn:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}

.btn-primary:hover {
    background: var(--primary-hover);
    box-shadow: 0 4px 14px rgba(29, 155, 240, 0.35);
}

.btn-danger {
    background: var(--danger);
    color: #fff;
}

.btn-danger:hover {
    background: #d61b27;
    box-shadow: 0 4px 14px rgba(244, 33, 46, 0.35);
}

.btn-sm {
    padding: 0.4rem 0.8rem;
    font-size: 0.8rem;
}

/* Stats */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.75rem;
}

.stat {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.9rem 0.6rem;
    text-align: center;
    transition: border-color 0.2s ease, background 0.2s ease;
}

.stat:hover {
    border-color: var(--border-hover);
    background: var(--bg-hover);
}

.stat-value {
    display: block;
    font-size: 1.5rem;
    font-weight: 800;
    color: var(--text);
    letter-spacing: -0.02em;
}

.stat-value.used { color: var(--primary); }
.stat-value.free { color: var(--success); }

.stat-label {
    display: block;
    font-size: 0.72rem;
    color: var(--text-muted);
    margin-top: 0.2rem;
}

/* Allocations table */
.table-wrap {
    overflow-x: auto;
}

.allocations-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.allocations-table th,
.allocations-table td {
    padding: 0.6rem 0.75rem;
    text-align: left;
    border-bottom: 1px solid var(--border);
}

.allocations-table th {
    color: var(--text-muted);
    font-weight: 600;
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.allocations-table tbody tr:hover {
    background: var(--bg-hover);
}

.alloc-name {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
}

.alloc-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    display: inline-block;
}

.empty-row td {
    text-align: center;
    color: var(--text-muted);
    padding: 1.2rem;
}

.table-actions {
    margin-top: 1rem;
    display: flex;
    justify-content: flex-end;
}

/* Event log */
.event-log {
    list-style: none;
    max-height: 220px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.event-log li {
    padding: 0.5rem 0.75rem;
    border-radius: var(--radius-sm);
    background: var(--bg-elevated);
    border-left: 3px solid var(--primary);
    font-size: 0.85rem;
    animation: slideIn 0.3s ease;
}

.event-log li.success { border-left-color: var(--success); }
.event-log li.failed { border-left-color: var(--danger); }
.event-log li.free { border-left-color: #f59e0b; }
.event-log li.reset { border-left-color: var(--text-muted); }

.event-log .time {
    color: var(--text-muted);
    font-size: 0.72rem;
    margin-right: 0.5rem;
}

@keyframes slideIn {
    from { opacity: 0; transform: translateX(-10px); }
    to { opacity: 1; transform: translateX(0); }
}

/* Toast */
.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: var(--bg-card-2);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 0.8rem 1.4rem;
    border-radius: var(--radius-pill);
    box-shadow: var(--shadow);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease, transform 0.3s ease;
    z-index: 100;
    max-width: 90%;
    text-align: center;
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.toast.error {
    border-color: var(--danger);
}

.toast.success {
    border-color: var(--success);
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg);
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 8px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--border-hover);
}
