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

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: #1a1a2e;
    color: #eee;
    min-height: 100vh;
}

.hidden { display: none !important; }

/* --- Index page --- */
.container {
    max-width: 600px;
    margin: 0 auto;
    padding: 40px 20px;
    text-align: center;
}

.container h1 {
    font-size: 2.5rem;
    margin-bottom: 30px;
    color: #e0c097;
}

.card {
    background: #16213e;
    border-radius: 12px;
    padding: 24px;
    margin-bottom: 20px;
}

.card h2 {
    margin-bottom: 16px;
    color: #e0c097;
    font-size: 1.3rem;
}

form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

input[type="text"],
input[type="password"] {
    padding: 12px 16px;
    border: 2px solid #0f3460;
    border-radius: 8px;
    background: #1a1a2e;
    color: #eee;
    font-size: 1rem;
    outline: none;
    transition: border-color 0.2s;
}

input:focus {
    border-color: #e0c097;
}

.color-select {
    display: flex;
    gap: 20px;
    justify-content: center;
    padding: 8px 0;
}

.color-select label {
    cursor: pointer;
    font-size: 1rem;
}

button {
    padding: 12px 24px;
    border: none;
    border-radius: 8px;
    background: #e0c097;
    color: #1a1a2e;
    font-size: 1rem;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.2s;
}

button:hover {
    background: #c9a46c;
}

.error {
    background: #e74c3c;
    color: #fff;
    padding: 12px;
    border-radius: 8px;
    margin-top: 16px;
}

/* --- Join/Rejoin flow --- */
.room-code-display {
    font-family: monospace;
    font-size: 1rem;
    color: #e0c097;
    background: #1a1a2e;
    padding: 8px 14px;
    border-radius: 8px;
    text-align: center;
    margin-bottom: 14px;
}

.section-label {
    margin-bottom: 10px;
    color: #aaa;
    font-size: 0.95rem;
}

.divider {
    display: flex;
    align-items: center;
    margin: 18px 0;
    color: #555;
    font-size: 0.85rem;
}

.divider::before,
.divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: #2a3a5e;
}

.divider span {
    padding: 0 12px;
    color: #777;
    text-transform: uppercase;
    font-weight: 600;
    letter-spacing: 1px;
}

.player-btn {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 14px 18px;
    margin-bottom: 8px;
    background: #1a1a2e;
    border: 2px solid #0f3460;
    border-radius: 10px;
    color: #eee;
    font-size: 1rem;
    cursor: pointer;
    transition: border-color 0.2s, background 0.2s;
}

.player-btn:hover {
    border-color: #e0c097;
    background: #0f3460;
}

.player-btn-nick {
    font-weight: 700;
}

.player-btn-color {
    font-size: 0.85rem;
    color: #aaa;
}

.btn-back {
    margin-top: 8px;
    background: #555;
    font-size: 0.85rem;
    padding: 8px 18px;
}

.btn-back:hover {
    background: #777;
}

/* --- Game page --- */
.game-container {
    max-width: 960px;
    margin: 0 auto;
    padding: 16px;
}

.game-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
    flex-wrap: wrap;
    gap: 10px;
}

.game-header h1 {
    font-size: 1.5rem;
    color: #e0c097;
}

.header-buttons {
    display: flex;
    gap: 8px;
}

#room-info {
    font-size: 1rem;
    color: #aaa;
    font-family: monospace;
    background: #16213e;
    padding: 6px 14px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
}

#room-info:hover {
    background: #1c2a4a;
    color: #e0c097;
}

#room-info::after {
    content: ' \1F4CB';
    font-size: 0.8rem;
}

/* Toast notification */
#toast {
    position: fixed;
    bottom: 50px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: #2e7d32;
    color: #fff;
    padding: 10px 24px;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 600;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s, transform 0.3s;
    z-index: 2000;
}

#toast.toast-show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

#settings-btn {
    font-size: 0.85rem;
    padding: 8px 16px;
    background: #0f3460;
    color: #ccc;
}

#settings-btn:hover {
    background: #1a4a8a;
    color: #fff;
}

#logout-btn {
    font-size: 0.85rem;
    padding: 8px 16px;
    background: #555;
}

#logout-btn:hover {
    background: #e74c3c;
}

.game-main {
    display: flex;
    gap: 24px;
    align-items: flex-start;
}

@media (max-width: 750px) {
    .game-main { flex-direction: column; align-items: center; }
}

.board-wrapper {
    flex-shrink: 0;
}

.player-info {
    padding: 10px 14px;
    font-size: 1rem;
    font-weight: 600;
    color: #ccc;
    background: #16213e;
    border-radius: 8px;
    margin: 6px 0;
    text-align: center;
}

/* ============================================
   CHESS BOARD - base + themes
   ============================================ */
.chess-board {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    width: min(520px, 92vw);
    height: min(520px, 92vw);
    border-radius: 6px;
    overflow: hidden;
    box-shadow: 0 8px 32px rgba(0,0,0,0.5), 0 0 0 4px rgba(255,255,255,0.06);
}

.square {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    user-select: none;
    transition: background 0.12s, box-shadow 0.12s;
    overflow: hidden;
    min-width: 0;
    min-height: 0;
}

/* --- Theme: Classic --- */
.theme-classic .square.light { background: #f0d9b5; }
.theme-classic .square.dark  { background: #b58863; }
.theme-classic .chess-board   { box-shadow: 0 8px 32px rgba(0,0,0,0.5), 0 0 0 5px #8b6914; }
.theme-classic .square.light .coord-rank,
.theme-classic .square.light .coord-file { color: #b58863; }
.theme-classic .square.dark .coord-rank,
.theme-classic .square.dark .coord-file  { color: #dfc49b; }

/* --- Theme: Blue --- */
.theme-blue .square.light { background: #dee3e6; }
.theme-blue .square.dark  { background: #8ca2ad; }
.theme-blue .chess-board   { box-shadow: 0 8px 32px rgba(0,0,0,0.5), 0 0 0 5px #5a7d8a; }
.theme-blue .square.light .coord-rank,
.theme-blue .square.light .coord-file { color: #8ca2ad; }
.theme-blue .square.dark .coord-rank,
.theme-blue .square.dark .coord-file  { color: #c5d0d5; }

/* --- Theme: Green --- */
.theme-green .square.light { background: #eeeed2; }
.theme-green .square.dark  { background: #769656; }
.theme-green .chess-board   { box-shadow: 0 8px 32px rgba(0,0,0,0.5), 0 0 0 5px #4e6b30; }
.theme-green .square.light .coord-rank,
.theme-green .square.light .coord-file { color: #769656; }
.theme-green .square.dark .coord-rank,
.theme-green .square.dark .coord-file  { color: #d5d5b2; }

/* --- Theme: 3D --- */
.theme-3d .chess-board {
    box-shadow: 0 12px 40px rgba(0,0,0,0.6), 0 0 0 5px #5c4a2a;
    transform: perspective(800px) rotateX(8deg);
}
.theme-3d .square.light {
    background: linear-gradient(135deg, #f5e6cc 0%, #e8d4a8 100%);
    border: 0.5px solid rgba(0,0,0,0.04);
}
.theme-3d .square.dark {
    background: linear-gradient(135deg, #c4935a 0%, #a07040 100%);
    border: 0.5px solid rgba(0,0,0,0.06);
}
.theme-3d .piece {
    filter: drop-shadow(2px 4px 3px rgba(0,0,0,0.45));
}
.theme-3d .piece-white {
    filter: drop-shadow(2px 4px 3px rgba(0,0,0,0.45));
}
.theme-3d .piece-black {
    filter: drop-shadow(2px 4px 3px rgba(0,0,0,0.35));
}
.theme-3d .square.light .coord-rank,
.theme-3d .square.light .coord-file { color: #a07040; }
.theme-3d .square.dark .coord-rank,
.theme-3d .square.dark .coord-file  { color: #e0cba8; }

/* --- Square states (shared) --- */
.square.selected {
    background: #f6f669 !important;
    box-shadow: inset 0 0 0 3px rgba(0,0,0,0.15);
}

.square.legal-target::after {
    content: '';
    position: absolute;
    width: 28%;
    height: 28%;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 50%;
    pointer-events: none;
}

.square.legal-target.has-piece::after {
    width: 90%;
    height: 90%;
    background: none;
    border: 4px solid rgba(0, 0, 0, 0.2);
    border-radius: 50%;
}

.square.last-move {
    background: rgba(255, 255, 50, 0.3) !important;
}

.square:hover {
    filter: brightness(1.08);
}

/* --- Piece (outline = default) --- */
.piece {
    font-size: min(3.5rem, 10vw);
    line-height: 1;
    pointer-events: none;
    position: absolute;
}

.piece-white {
    color: #fff;
    filter: drop-shadow(0 1px 2px rgba(0,0,0,0.4));
    -webkit-text-stroke: 0.5px #888;
}

.piece-black {
    color: #222;
    filter: drop-shadow(0 1px 2px rgba(0,0,0,0.3));
    -webkit-text-stroke: 0.5px #000;
}

/* --- Piece style: Filled (uses solid Unicode ♚♛♜♝♞♟) --- */
.pieces-filled .piece-white {
    color: #fff;
    -webkit-text-stroke: 0.5px #999;
    filter: drop-shadow(1px 2px 3px rgba(0,0,0,0.5));
}

.pieces-filled .piece-black {
    color: #111;
    -webkit-text-stroke: 0;
    filter: drop-shadow(1px 2px 3px rgba(0,0,0,0.35));
}

/* --- Coordinates --- */
.coord-rank {
    position: absolute;
    top: 2px;
    left: 4px;
    font-size: 0.65rem;
    font-weight: 700;
    opacity: 0.7;
    pointer-events: none;
}

.coord-file {
    position: absolute;
    bottom: 2px;
    right: 4px;
    font-size: 0.65rem;
    font-weight: 700;
    opacity: 0.7;
    pointer-events: none;
}

/* ============================================
   SETTINGS MODAL
   ============================================ */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal {
    background: #16213e;
    border-radius: 14px;
    padding: 28px;
    width: 420px;
    max-width: 92vw;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 16px 48px rgba(0,0,0,0.5);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.modal-header h2 {
    color: #e0c097;
    font-size: 1.4rem;
}

.modal-close {
    background: none;
    border: none;
    color: #888;
    font-size: 1.8rem;
    cursor: pointer;
    padding: 0 6px;
    line-height: 1;
}

.modal-close:hover {
    color: #e74c3c;
    background: none;
}

.modal-section {
    margin-bottom: 22px;
    padding-bottom: 18px;
    border-bottom: 1px solid rgba(255,255,255,0.06);
}

.modal-section:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
}

.modal-section h3 {
    color: #e0c097;
    font-size: 1rem;
    margin-bottom: 12px;
}

.modal-section form {
    gap: 10px;
}

.modal-section input[type="password"] {
    padding: 10px 14px;
    font-size: 0.9rem;
}

.modal-section button[type="submit"] {
    padding: 10px 20px;
    font-size: 0.9rem;
}

.settings-msg {
    margin-top: 8px;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 0.85rem;
}

.error-msg {
    background: #e74c3c;
    color: #fff;
}

.success-msg {
    background: #2e7d32;
    color: #c8e6c9;
}

/* Theme + piece style buttons in modal */
.settings-themes,
.settings-pieces {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.theme-btn {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 8px 14px;
    font-size: 0.85rem;
    background: #1a1a2e;
    color: #ccc;
    border: 2px solid transparent;
    border-radius: 8px;
    cursor: pointer;
    transition: border-color 0.2s, background 0.2s;
}

.theme-btn:hover {
    border-color: #555;
    background: #1c2a4a;
}

.theme-btn.active {
    border-color: #e0c097;
    color: #e0c097;
}

.theme-swatch {
    display: inline-block;
    width: 18px;
    height: 18px;
    border-radius: 4px;
    border: 1px solid rgba(255,255,255,0.15);
}

.classic-swatch {
    background: linear-gradient(135deg, #f0d9b5 50%, #b58863 50%);
}

.blue-swatch {
    background: linear-gradient(135deg, #dee3e6 50%, #8ca2ad 50%);
}

.green-swatch {
    background: linear-gradient(135deg, #eeeed2 50%, #769656 50%);
}

.threed-swatch {
    background: linear-gradient(135deg, #f5e6cc 50%, #a07040 50%);
    box-shadow: inset 0 -2px 3px rgba(0,0,0,0.25);
}

.piece-style-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    font-size: 0.85rem;
    background: #1a1a2e;
    color: #ccc;
    border: 2px solid transparent;
    border-radius: 8px;
    cursor: pointer;
    transition: border-color 0.2s, background 0.2s;
}

.piece-style-btn:hover {
    border-color: #555;
    background: #1c2a4a;
}

.piece-style-btn.active {
    border-color: #e0c097;
    color: #e0c097;
}

.piece-preview {
    font-size: 1.6rem;
    line-height: 1;
}

.piece-preview-outline {
    color: #fff;
    -webkit-text-stroke: 0.5px #888;
    filter: drop-shadow(0 1px 1px rgba(0,0,0,0.3));
}

.piece-preview-filled {
    color: #fff;
    -webkit-text-stroke: 1px #b0a080;
    filter: drop-shadow(1px 1px 2px rgba(0,0,0,0.4));
}

/* --- Side panel --- */
.side-panel {
    flex: 1;
    min-width: 200px;
}

.status-box {
    padding: 14px 18px;
    border-radius: 10px;
    background: #16213e;
    font-size: 1.05rem;
    text-align: center;
    margin-bottom: 14px;
    font-weight: 600;
}

.status-box.your-turn {
    background: #1b5e20;
    color: #a5d6a7;
}

.status-box.check {
    background: #e65100;
    color: #ffe0b2;
}

.status-box.checkmate {
    background: #b71c1c;
    color: #ffcdd2;
    font-weight: 700;
}

.status-box.draw {
    background: #4a148c;
    color: #e1bee7;
}

.moves-panel h3 {
    color: #e0c097;
    margin-bottom: 8px;
    font-size: 1rem;
}

.moves-list {
    background: #16213e;
    border-radius: 10px;
    padding: 14px;
    max-height: 400px;
    overflow-y: auto;
    font-family: 'Courier New', monospace;
    font-size: 0.9rem;
}

.move-row {
    padding: 4px 0;
    border-bottom: 1px solid rgba(255,255,255,0.04);
}

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

.move-num {
    color: #666;
    min-width: 30px;
    display: inline-block;
}

.move-white {
    color: #f0d9b5;
    min-width: 65px;
    display: inline-block;
    font-weight: 600;
}

.move-black {
    color: #b58863;
    font-weight: 600;
}

/* ============================================
   CHAT
   ============================================ */
.chat-panel {
    margin-top: 16px;
}

.chat-panel h3 {
    color: #e0c097;
    margin-bottom: 8px;
    font-size: 1rem;
}

.chat-messages {
    background: #16213e;
    border-radius: 10px 10px 0 0;
    padding: 12px;
    height: 180px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.chat-msg {
    font-size: 0.85rem;
    line-height: 1.4;
    word-break: break-word;
}

.chat-msg-mine .chat-nick {
    color: #a5d6a7;
}

.chat-nick {
    color: #e0c097;
    font-weight: 700;
}

.chat-text {
    color: #ddd;
}

.chat-form {
    display: flex;
    flex-direction: row;
    gap: 0;
}

.chat-form input {
    flex: 1;
    padding: 10px 12px;
    border: none;
    border-radius: 0 0 0 10px;
    background: #1a1a2e;
    color: #eee;
    font-size: 0.9rem;
    outline: none;
    border-top: 1px solid #0f3460;
}

.chat-form button {
    padding: 10px 16px;
    border: none;
    border-radius: 0 0 10px 0;
    background: #e0c097;
    color: #1a1a2e;
    font-size: 1rem;
    font-weight: 700;
    cursor: pointer;
    border-top: 1px solid #0f3460;
}

.chat-form button:hover {
    background: #c9a46c;
}

/* ============================================
   FOOTER
   ============================================ */
.app-footer {
    position: fixed;
    bottom: 12px;
    right: 16px;
    font-size: 0.75rem;
    color: #555;
}

.app-footer a {
    color: #e0c097;
    text-decoration: none;
}

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

/* ============================================
   MATRIX EASTER EGG
   ============================================ */
#matrix-rain {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    pointer-events: none;
}

/* Matrix mode overrides */
.matrix-mode {
    background: #000 !important;
}

.matrix-mode .game-container,
.matrix-mode .game-header h1,
.matrix-mode .player-info,
.matrix-mode .status-box,
.matrix-mode .moves-panel h3,
.matrix-mode .chat-panel h3,
.matrix-mode #room-info {
    background: rgba(0, 10, 0, 0.85) !important;
    color: #0f0 !important;
}

.matrix-mode .game-header h1 {
    color: #0f0 !important;
    text-shadow: 0 0 10px #0f0;
}

.matrix-mode .chess-board {
    box-shadow: 0 0 30px rgba(0, 255, 0, 0.3), 0 0 0 4px #0a0 !important;
}

.matrix-mode .square.light {
    background: #0a1a0a !important;
}

.matrix-mode .square.dark {
    background: #001400 !important;
}

.matrix-mode .square.selected {
    background: #0a3a0a !important;
    box-shadow: inset 0 0 12px rgba(0, 255, 0, 0.4) !important;
}

.matrix-mode .square.last-move {
    background: rgba(0, 255, 0, 0.1) !important;
}

.matrix-mode .square.legal-target::after {
    background: rgba(0, 255, 0, 0.3);
}

.matrix-mode .square.legal-target.has-piece::after {
    border-color: rgba(0, 255, 0, 0.4);
}

.matrix-mode .square:hover {
    filter: brightness(1.3);
}

/* Matrix pieces - letters instead of chess symbols */
/* Matrix SVG piece images */
.matrix-mode .piece-matrix {
    position: absolute;
    width: 85%;
    height: 85%;
    object-fit: contain;
    pointer-events: none;
    filter: drop-shadow(0 0 4px rgba(0, 255, 0, 0.4));
}

/* Coordinates */
.matrix-mode .coord-rank,
.matrix-mode .coord-file {
    color: #050 !important;
}

/* Side panel in matrix mode */
.matrix-mode .status-box {
    border: 1px solid #0a0;
}

.matrix-mode .status-box.your-turn {
    background: rgba(0, 40, 0, 0.9) !important;
    color: #0f0 !important;
    border-color: #0f0;
    box-shadow: 0 0 10px rgba(0, 255, 0, 0.2);
}

.matrix-mode .status-box.check {
    background: rgba(40, 20, 0, 0.9) !important;
    color: #fa0 !important;
    border-color: #fa0;
}

.matrix-mode .status-box.checkmate {
    background: rgba(40, 0, 0, 0.9) !important;
    color: #f44 !important;
    border-color: #f44;
    text-shadow: 0 0 10px #f00;
}

.matrix-mode .moves-list {
    background: rgba(0, 10, 0, 0.85) !important;
    border: 1px solid #0a0;
}

.matrix-mode .move-num { color: #050 !important; }
.matrix-mode .move-white { color: #0f0 !important; }
.matrix-mode .move-black { color: #f44 !important; }

.matrix-mode .chat-messages {
    background: rgba(0, 10, 0, 0.85) !important;
    border: 1px solid #0a0;
}

.matrix-mode .chat-nick { color: #0f0 !important; }
.matrix-mode .chat-text { color: #0a0 !important; }
.matrix-mode .chat-msg-mine .chat-nick { color: #5f5 !important; }

.matrix-mode .chat-form input {
    background: rgba(0, 10, 0, 0.9) !important;
    color: #0f0 !important;
    border-color: #0a0 !important;
}

.matrix-mode .chat-form button {
    background: #0a0 !important;
    color: #000 !important;
}

.matrix-mode .chat-form button:hover {
    background: #0f0 !important;
}

.matrix-mode .app-footer { color: #050 !important; }
.matrix-mode .app-footer a { color: #0a0 !important; }

.matrix-mode #settings-btn {
    background: #0a0 !important;
    color: #000 !important;
}

.matrix-mode #logout-btn {
    background: #300 !important;
    color: #f44 !important;
}
