* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #2a1a1a 0%, #4a2020 100%);
    color: #333;
    padding: 8px;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    background: #f5f5f5;
    border-radius: 12px;
    padding: 8px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.3);
    position: relative;
    display: flex;
    flex-direction: column;
    height: 90vh;
}

.game-header {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 12px;
}

h1 {
    color: #8b0000;
    margin: 0 0 8px 0;
    font-size: 1.4em;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
    text-align: center;
}

.game-info {
    display: flex;
    flex-direction: column;
    gap: 2px;
    margin: 0 0 8px 0;
    padding: 6px 8px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    width: fit-content;
    margin-left: auto;
    margin-right: auto;
}

.rules-popup {
    position: fixed;
    top: 80px;
    right: 12px;
    width: 280px;
    max-height: 500px;
    background: #fff;
    border: 2px solid #8b0000;
    border-radius: 8px;
    padding: 12px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
    z-index: 101;
    overflow-y: auto;
}

.rules-popup h3 {
    color: #8b0000;
    margin: 0 0 10px 0;
    font-size: 0.95em;
}

.rules-content {
    max-height: 400px;
    overflow-y: auto;
    padding-right: 8px;
}

.rules-content p {
    font-size: 0.8em;
    line-height: 1.4;
    margin: 6px 0;
    color: #333;
}

.current-player, .game-status {
    font-size: 0.7em;
    font-weight: bold;
    color: #8b0000;
    margin: 0;
}

/* --- Main Layout: 5-column grid --- */
.main-layout {
    display: grid;
    grid-template-columns: 200px 100px auto 100px 180px;
    gap: 8px;
    margin-bottom: 8px;
    min-width: 0;
}

/* --- Trapdoor Status Panel (left sidebar) --- */
.trapdoor-status {
    background: #fff;
    border: 2px solid #cc4444;
    border-radius: 8px;
    padding: 10px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    overflow-y: auto;
    max-height: 420px;
}

.trapdoor-status h3 {
    color: #8b0000;
    margin: 0 0 8px 0;
    font-size: 0.85em;
    text-align: center;
    border-bottom: 2px solid #cc4444;
    padding-bottom: 6px;
}

.timer-section h4 {
    color: #555;
    font-size: 0.7em;
    margin: 8px 0 4px 0;
}

.timer-list {
    min-height: 20px;
}

.timer-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 3px 6px;
    margin: 2px 0;
    border-radius: 4px;
    font-size: 0.75em;
    font-weight: bold;
}

.timer-item.safe {
    background: #e8f5e9;
    border-left: 3px solid #4caf50;
    color: #2e7d32;
}

.timer-item.warning {
    background: #fff8e1;
    border-left: 3px solid #ff9800;
    color: #e65100;
}

.timer-item.danger {
    background: #ffebee;
    border-left: 3px solid #f44336;
    color: #c62828;
    animation: pulse-danger 1s infinite;
}

@keyframes pulse-danger {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

/* --- Fallen Pieces Panel --- */
.fallen-pieces {
    background: #fff;
    border-radius: 8px;
    padding: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.fallen-pieces h3 {
    color: #8b0000;
    margin-bottom: 6px;
    font-size: 0.75em;
    text-align: center;
}

.fallen-list {
    margin: 4px 0;
}

.fallen-label {
    font-size: 0.65em;
    color: #777;
    font-weight: bold;
}

.fallen-row {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    min-height: 22px;
    font-size: 1.1em;
}

.fallen-piece {
    opacity: 0.5;
    filter: grayscale(50%);
}

/* --- Controls Panel --- */
.controls-panel {
    background: #fff;
    border-radius: 8px;
    padding: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.controls-panel h3 {
    color: #8b0000;
    margin-bottom: 6px;
    font-size: 0.75em;
    text-align: center;
}

/* --- Board --- */
.board-container {
    display: flex;
    flex-direction: column;
    gap: 5px;
    width: fit-content;
}

.board-wrapper {
    display: grid;
    grid-template-columns: 30px repeat(8, 40px);
    grid-template-rows: auto auto;
    gap: 1px;
    align-items: start;
    width: fit-content;
    margin: 0 auto;
}

.rank-labels {
    display: flex;
    flex-direction: column;
    gap: 0;
    justify-content: space-between;
    min-height: 100%;
    grid-row: 1 / 2;
    grid-column: 1;
}

.rank-label {
    width: 30px;
    text-align: right;
    font-weight: bold;
    font-size: 0.75em;
    color: #8b0000;
    padding: 2px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.file-labels {
    display: grid;
    grid-template-columns: repeat(8, 40px);
    gap: 4px 4px;
    margin-top: 0;
    margin-left: 0;
    grid-row: 2;
    grid-column: 2 / 10;
}

.file-label {
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 0.75em;
    color: #8b0000;
    height: 24px;
}

.chessboard {
    display: grid;
    grid-template-columns: repeat(8, 40px);
    gap: 0;
    background: #5a3a3a;
    padding: 2px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2),
                inset 0 0 20px rgba(0,0,0,0.2);
    width: 320px;
    height: 320px;
    grid-row: 1;
    grid-column: 2 / 10;
}

.square {
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.8em;
    cursor: pointer;
    border: 2px solid transparent;
    transition: all 0.2s ease;
    position: relative;
    overflow: hidden;
}

.square.light {
    background: #f0d9b5;
}

.square.dark {
    background: #b58863;
}

.square.selected {
    border-color: #ffff00;
    box-shadow: inset 0 0 10px rgba(255,255,0,0.5);
}

.square.valid-move {
    box-shadow: inset 0 0 8px rgba(0,255,0,0.6);
}

.square.valid-move::before {
    content: '';
    position: absolute;
    width: 20px;
    height: 20px;
    background: rgba(0,255,0,0.6);
    border-radius: 50%;
    pointer-events: none;
}

.square.check {
    box-shadow: inset 0 0 15px rgba(255,0,0,0.8);
}

/* Trapdoor timer badge on square */
.trapdoor-badge {
    position: absolute;
    top: 1px;
    right: 1px;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    font-size: 0.35em;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    z-index: 2;
}

.trapdoor-badge.timer-5 {
    background: #4caf50;
    color: #fff;
}

.trapdoor-badge.timer-4 {
    background: #8bc34a;
    color: #fff;
}

.trapdoor-badge.timer-3 {
    background: #ff9800;
    color: #fff;
}

.trapdoor-badge.timer-2 {
    background: #ff5722;
    color: #fff;
}

.trapdoor-badge.timer-1 {
    background: #f44336;
    color: #fff;
    animation: pulse-danger 0.6s infinite;
}

/* Trapdoor falling animation */
@keyframes trapdoor-fall {
    0% { transform: scale(1) rotate(0deg); opacity: 1; }
    30% { transform: scale(0.9) rotate(5deg); opacity: 0.8; }
    60% { transform: scale(0.5) rotate(-10deg); opacity: 0.5; }
    100% { transform: scale(0) rotate(30deg); opacity: 0; }
}

.square.trapdoor-falling {
    animation: trapdoor-fall 0.8s ease-in forwards;
}

/* Trapdoor open flash */
@keyframes trapdoor-open {
    0% { background: inherit; }
    25% { background: #2a0000; }
    50% { background: #440000; }
    100% { background: inherit; }
}

.square.trapdoor-opened {
    animation: trapdoor-open 0.8s ease-out;
}

.piece {
    pointer-events: auto;
    user-select: none;
}

.piece.white {
    filter: drop-shadow(2px 2px 3px rgba(0,0,0,0.3));
}

.piece.black {
    filter: drop-shadow(-2px -2px 3px rgba(255,255,255,0.3));
}

/* --- Move History --- */
.move-history {
    background: #fff;
    border-radius: 8px;
    padding: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    display: flex;
    flex-direction: column;
    max-height: 400px;
    min-width: 0;
    overflow: hidden;
}

.move-history h4 {
    color: #8b0000;
    margin-bottom: 6px;
    font-size: 0.75em;
    margin: 0 0 6px 0;
}

.history-list {
    background: #f9f9f9;
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 8px;
    max-height: 360px;
    overflow-y: auto;
    font-size: 0.75em;
    flex: 1;
}

.history-item {
    padding: 2px;
    margin: 1px 0;
    background: #fff;
    border-radius: 2px;
    border-left: 3px solid #cc4444;
    padding-left: 6px;
}

.history-item.trapdoor-event {
    border-left-color: #f44336;
    background: #fff5f5;
    font-style: italic;
}

/* --- Buttons --- */
.action-btn {
    width: 100%;
    padding: 4px;
    margin: 2px 0;
    background: #cc4444;
    color: #fff;
    border: none;
    border-radius: 4px;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 0.7em;
}

.action-btn:hover {
    background: #aa3333;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(204, 68, 68, 0.3);
}

.action-btn:active {
    transform: translateY(0);
}

.action-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.save-slots {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.save-slot-row {
    display: grid;
    grid-template-columns: 1fr auto auto;
    gap: 4px;
    align-items: center;
}

.slot-label {
    font-size: 0.65em;
    color: #999;
    padding: 2px 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.slot-label.slot-used {
    color: #333;
    font-weight: bold;
}

.slot-btn {
    width: auto !important;
    padding: 2px 6px !important;
    font-size: 0.6em !important;
    min-width: 32px;
}

@media (max-width: 1200px) {
    .main-layout {
        grid-template-columns: 180px 100px auto 100px 150px;
        gap: 10px;
    }
    
    .square {
        font-size: 1.8em;
    }
}
