/* ============================================================
   RESET & BASE
   ============================================================ */

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    overflow-x: hidden;
}

body {
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
    background: #1a1a2e;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    color: #e0e0e0;
    padding: 10px;
    overflow-x: hidden;
}

.hidden { display: none !important; }

/* ============================================================
   LOBBY
   ============================================================ */

#lobby {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    min-height: 100vh;
    padding: 20px;
}

#lobby h1 {
    font-size: 2rem;
    color: #e0c068;
    text-shadow: 1px 1px 3px rgba(0,0,0,0.5);
    margin-bottom: 5px;
}

.subtitle {
    color: #aaa;
    margin-bottom: 25px;
    font-size: 1rem;
}

.lobby-section {
    margin-bottom: 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: center;
}

.lobby-section button {
    width: 100%;
    max-width: 280px;
}

.divider {
    color: #666;
    margin: 10px 0;
    font-size: 0.9rem;
}

.join-row {
    display: flex;
    gap: 10px;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
}

#join-code {
    padding: 10px 15px;
    font-size: 1.2rem;
    text-align: center;
    text-transform: uppercase;
    letter-spacing: 3px;
    width: 170px;
    border: 2px solid #e0c068;
    border-radius: 8px;
    background: #16213e;
    color: #e0c068;
    font-family: 'Courier New', monospace;
}

#join-code::placeholder {
    color: #555;
    letter-spacing: 1px;
    font-size: 0.85rem;
}

#lobby-status {
    margin-top: 12px;
    color: #e87272;
    min-height: 24px;
    font-size: 0.95rem;
}

/* Waiting area */
#waiting-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    width: 100%;
    max-width: 500px;
    margin: 20px auto;
    padding: 30px;
    background: #16213e;
    border: 2px solid #e0c068;
    border-radius: 16px;
    box-shadow: 0 0 20px rgba(224, 192, 104, 0.15);
}

.room-code-display {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    margin: 10px 0;
}

#room-code-big {
    font-family: 'Courier New', monospace;
    font-size: 3rem;
    font-weight: bold;
    color: #e0c068;
    letter-spacing: 8px;
    padding: 15px 30px;
    background: #0a0e1a;
    border: 2px solid #e0c068;
    border-radius: 12px;
    user-select: all;
}

.waiting-spinner {
    color: #aaa;
    font-size: 1rem;
    margin: 15px 0;
    animation: pulse-text 2s infinite;
}

@keyframes pulse-text {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.cancel-btn {
    margin-top: 10px;
}

.waiting-spinner {
    color: #aaa;
    margin: 12px 0;
    animation: pulse 1.5s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

.cancel-btn {
    margin-top: 8px;
    background: transparent;
    border: 1px solid #e87272;
    color: #e87272;
    padding: 8px 20px;
    border-radius: 8px;
    cursor: pointer;
}

.cancel-btn:hover {
    background: #e87272;
    color: #fff;
}

/* ============================================================
   GAME SCREEN
   ============================================================ */

#game-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
}

.game-title {
    font-size: 1.6rem;
    color: #e0c068;
    margin-bottom: 5px;
    text-align: center;
}

#game-info-bar {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-bottom: 8px;
    padding: 6px 12px;
    background: #16213e;
    border: 1px solid #e0c068;
    border-radius: 8px;
    font-size: 0.85rem;
    flex-wrap: wrap;
    justify-content: center;
    max-width: 100%;
}

#turn-indicator {
    font-weight: bold;
}

#player-color-label {
    color: #aaa;
}

#room-code-small {
    font-family: 'Courier New', monospace;
    color: #e0c068;
    letter-spacing: 2px;
}

#connection-status {
    font-size: 1rem;
}

#connection-status.connected { color: #4caf50; }
#connection-status.disconnected { color: #e87272; }

/* ============================================================
   BOARD — Desktop default (60px squares)
   ============================================================ */

#board-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
}

#board-container {
    display: flex;
    flex-direction: row;
    align-items: flex-start;
}

#rank-labels {
    display: grid;
    grid-template-rows: repeat(10, var(--sq));
    align-items: center;
    justify-items: center;
    margin-right: 3px;
    font-size: var(--label-size);
    color: #aaa;
    font-weight: bold;
    width: 20px;
}

#file-labels {
    display: grid;
    grid-template-columns: repeat(10, var(--sq));
    justify-items: center;
    margin-top: 3px;
    margin-left: 23px;
    font-size: var(--label-size);
    color: #aaa;
    font-weight: bold;
}

:root {
    --sq: 60px;
    --piece-size: 50px;
    --label-size: 0.85rem;
}

#board {
    display: grid;
    grid-template-columns: repeat(10, var(--sq));
    grid-template-rows: repeat(10, var(--sq));
    border: 3px solid #e0c068;
    box-shadow: 0 0 15px rgba(224, 192, 104, 0.3);
}

.square {
    width: var(--sq);
    height: var(--sq);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    user-select: none;
    position: relative;
    transition: background-color 0.15s;
    -webkit-tap-highlight-color: transparent;
}

.square.light { background-color: #f0d9b5; }
.square.dark { background-color: #b58863; }

.square.selected {
    background-color: #7fc97f !important;
    box-shadow: inset 0 0 8px rgba(0, 100, 0, 0.5);
}

.square.valid-move::after {
    content: '';
    position: absolute;
    width: 30%;
    height: 30%;
    background: rgba(0, 0, 0, 0.25);
    border-radius: 50%;
}

.square.valid-capture { background-color: #e87272 !important; }
.square.last-move-from { background-color: #cdd26a !important; }
.square.last-move-to { background-color: #aab834 !important; }

/* Remove hover effect on touch devices */
@media (hover: hover) {
    .square:hover { filter: brightness(1.1); }
}

.piece-img {
    width: var(--piece-size);
    height: var(--piece-size);
    pointer-events: none;
    filter: drop-shadow(1px 1px 1px rgba(0,0,0,0.3));
}

/* ============================================================
   MOVE HISTORY
   ============================================================ */

#move-history-container {
    margin-top: 12px;
    width: 100%;
    max-width: 640px;
    max-height: 120px;
    overflow-y: auto;
    background: #16213e;
    border: 1px solid #e0c068;
    border-radius: 8px;
    padding: 8px 12px;
}

#move-history-container h3 {
    color: #e0c068;
    margin-bottom: 4px;
    font-size: 0.9rem;
}

#move-history {
    font-family: 'Courier New', monospace;
    font-size: 0.8rem;
    color: #ccc;
    line-height: 1.5;
    word-wrap: break-word;
}

/* ============================================================
   CONTROLS
   ============================================================ */

#controls {
    margin-top: 10px;
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    justify-content: center;
    width: 100%;
    max-width: 640px;
}

button {
    padding: 8px 14px;
    font-size: 0.85rem;
    border: 1px solid #e0c068;
    background: #16213e;
    color: #e0c068;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
}

button:hover {
    background: #e0c068;
    color: #1a1a2e;
}

button:active {
    transform: scale(0.95);
}

/* ============================================================
   MODALS
   ============================================================ */

#promotion-modal, #game-over-modal, #draw-modal {
    display: none;
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background: rgba(0,0,0,0.7);
    z-index: 100;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

#promotion-modal.active, #game-over-modal.active, #draw-modal.active {
    display: flex;
}

#promotion-choices, #game-over-content, #draw-content {
    background: #16213e;
    border: 2px solid #e0c068;
    border-radius: 12px;
    padding: 20px;
    text-align: center;
    max-width: 90vw;
}

#promotion-choices h3, #game-over-content h2, #draw-content h2 {
    color: #e0c068;
    margin-bottom: 12px;
}

#promotion-choices h3 {
    font-size: 1rem;
}

#game-over-content h2 {
    font-size: 1.4rem;
}

#draw-content h2 {
    font-size: 1.2rem;
}

.promo-option {
    display: inline-block;
    cursor: pointer;
    padding: 6px;
    margin: 0 3px;
    border-radius: 8px;
    border: 2px solid transparent;
    transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
}

.promo-option:hover {
    border-color: #e0c068;
    background: rgba(224,192,104,0.2);
}

.promo-option .piece-img {
    width: var(--piece-size);
    height: var(--piece-size);
}

.modal-buttons {
    display: flex;
    gap: 10px;
    justify-content: center;
    margin-top: 12px;
    flex-wrap: wrap;
}

/* ============================================================
   RESPONSIVE: TABLETS (portrait, ~768px)
   ============================================================ */

@media screen and (max-width: 768px) {
    :root {
        --sq: 52px;
        --piece-size: 44px;
        --label-size: 0.75rem;
    }

    body {
        padding: 8px;
    }

    .game-title {
        font-size: 1.3rem;
    }

    #game-info-bar {
        font-size: 0.8rem;
        padding: 5px 10px;
        gap: 6px;
    }

    #move-history-container {
        max-height: 100px;
    }

    #controls button {
        padding: 7px 12px;
        font-size: 0.8rem;
    }
}

/* ============================================================
   RESPONSIVE: LARGE PHONES (landscape or wide, ~600px)
   ============================================================ */

@media screen and (max-width: 640px) {
    :root {
        --sq: 46px;
        --piece-size: 38px;
        --label-size: 0.7rem;
    }

    #lobby h1 {
        font-size: 1.6rem;
    }

    .subtitle {
        font-size: 0.9rem;
    }

    #room-code-big {
        font-size: 2rem;
        letter-spacing: 4px;
    }

    .game-title {
        font-size: 1.1rem;
    }

    #controls {
        gap: 5px;
    }

    #controls button {
        padding: 6px 10px;
        font-size: 0.75rem;
    }
}

/* ============================================================
   RESPONSIVE: PHONES (portrait, ~480px)
   ============================================================ */

@media screen and (max-width: 500px) {
    :root {
        --sq: 36px;
        --piece-size: 30px;
        --label-size: 0.6rem;
    }

    body {
        padding: 5px;
    }

    #lobby h1 {
        font-size: 1.4rem;
    }

    .subtitle {
        font-size: 0.8rem;
        margin-bottom: 15px;
    }

    #lobby {
        padding: 10px;
    }

    .lobby-section button {
        max-width: 240px;
        font-size: 0.9rem;
    }

    #join-code {
        width: 140px;
        font-size: 1rem;
        padding: 8px 10px;
    }

    #room-code-big {
        font-size: 1.8rem;
        letter-spacing: 4px;
    }

    #waiting-area {
        padding: 15px;
    }

    .game-title {
        font-size: 1rem;
        margin-bottom: 3px;
    }

    #game-info-bar {
        font-size: 0.7rem;
        padding: 4px 8px;
        gap: 4px;
        margin-bottom: 5px;
    }

    #rank-labels {
        width: 16px;
        margin-right: 2px;
    }

    #file-labels {
        margin-left: 18px;
        margin-top: 2px;
    }

    #board {
        border-width: 2px;
    }

    #move-history-container {
        max-height: 80px;
        padding: 6px 8px;
        margin-top: 8px;
    }

    #move-history-container h3 {
        font-size: 0.8rem;
    }

    #move-history {
        font-size: 0.7rem;
    }

    #controls {
        margin-top: 8px;
        gap: 4px;
    }

    #controls button {
        padding: 6px 8px;
        font-size: 0.7rem;
    }

    #promotion-choices h3 {
        font-size: 0.85rem;
    }

    #game-over-content h2 {
        font-size: 1.1rem;
    }

    .promo-option {
        padding: 4px;
        margin: 0 2px;
    }
}

/* ============================================================
   RESPONSIVE: SMALL PHONES (~380px and below)
   ============================================================ */

@media screen and (max-width: 400px) {
    :root {
        --sq: 32px;
        --piece-size: 26px;
        --label-size: 0.55rem;
    }

    #lobby h1 {
        font-size: 1.2rem;
    }

    .game-title {
        font-size: 0.9rem;
    }

    #game-info-bar {
        font-size: 0.65rem;
    }

    #controls button {
        padding: 5px 6px;
        font-size: 0.65rem;
    }
}

/* ============================================================
   RESPONSIVE: VERY SMALL PHONES (~340px)
   ============================================================ */

@media screen and (max-width: 360px) {
    :root {
        --sq: 28px;
        --piece-size: 23px;
        --label-size: 0.5rem;
    }

    #rank-labels {
        width: 14px;
        margin-right: 1px;
    }

    #file-labels {
        margin-left: 15px;
    }

    .game-title {
        font-size: 0.85rem;
    }
}

/* ============================================================
   LANDSCAPE MODE: Optimize layout when phone is sideways
   ============================================================ */

@media screen and (max-height: 500px) and (orientation: landscape) {
    :root {
        --sq: 38px;
        --piece-size: 32px;
        --label-size: 0.6rem;
    }

    body {
        padding: 3px;
    }

    .game-title {
        font-size: 0.9rem;
        margin-bottom: 2px;
    }

    #game-info-bar {
        margin-bottom: 3px;
        padding: 3px 8px;
        font-size: 0.7rem;
    }

    #move-history-container {
        max-height: 50px;
        margin-top: 5px;
        padding: 4px 6px;
    }

    #move-history-container h3 {
        font-size: 0.7rem;
    }

    #move-history {
        font-size: 0.65rem;
    }

    #controls {
        margin-top: 5px;
    }

    #controls button {
        padding: 4px 6px;
        font-size: 0.65rem;
    }
}

@media screen and (max-height: 400px) and (orientation: landscape) {
    :root {
        --sq: 32px;
        --piece-size: 27px;
        --label-size: 0.5rem;
    }

    .game-title { display: none; }

    #game-info-bar {
        margin-bottom: 2px;
        padding: 2px 6px;
        font-size: 0.6rem;
    }

    #move-history-container { display: none; }
}

/* ============================================================
   UTILITY: Prevent text selection on board during play
   ============================================================ */

#board-wrapper {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    touch-action: manipulation;
}

/* Prevent double-tap zoom on the board */
#board {
    touch-action: manipulation;
}

.square {
    touch-action: manipulation;
}

/* ============================================================
   SAFE AREA (for phones with notches like iPhone X+)
   ============================================================ */

@supports (padding: env(safe-area-inset-bottom)) {
    body {
        padding-bottom: env(safe-area-inset-bottom);
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }
}

/* ============================================================
   TIMER SELECT (Lobby)
   ============================================================ */

.timer-label {
    color: #e0c068;
    font-size: 1rem;
    font-weight: bold;
}

#time-control {
    padding: 10px 15px;
    font-size: 1rem;
    border: 2px solid #e0c068;
    border-radius: 8px;
    background: #16213e;
    color: #e0c068;
    cursor: pointer;
    width: 100%;
    max-width: 280px;
    appearance: none;
    -webkit-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M0 0 L6 8 L12 0' fill='%23e0c068'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 35px;
}

#time-control option,
#time-control optgroup {
    background: #16213e;
    color: #e0c068;
}

#time-control optgroup {
    font-weight: bold;
    font-style: normal;
}

#waiting-time-control {
    color: #aaa;
    font-size: 0.9rem;
    margin: 8px 0;
}

/* ============================================================
   CHESS CLOCKS
   ============================================================ */

.chess-clock {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 24px;
    margin: 6px 0;
    border-radius: 12px;
    border: 3px solid #333;
    background: #16213e;
    width: 100%;
    max-width: 640px;
    min-height: 64px;
    transition: all 0.3s;
}

.chess-clock.hidden {
    display: none !important;
}

.chess-clock.active {
    border-color: #e0c068;
    background: #1c2a4a;
    box-shadow: 0 0 15px rgba(224, 192, 104, 0.25);
}

.chess-clock.low-time {
    border-color: #e87272;
    animation: clock-pulse 1s infinite;
}

@keyframes clock-pulse {
    0%, 100% { background: #1c2a4a; }
    50% { background: #2a1a1a; }
}

.chess-clock.flagged {
    border-color: #e87272;
    background: #2a1111;
}

.clock-player-label {
    font-size: 1.2rem;
    color: #aaa;
    font-weight: bold;
    min-width: 100px;
}

.chess-clock.active .clock-player-label {
    color: #ccc;
}

.clock-time {
    font-family: 'Courier New', monospace;
    font-size: 2.8rem;
    font-weight: bold;
    color: #e0e0e0;
    letter-spacing: 3px;
    min-width: 140px;
    text-align: right;
}

.chess-clock.active .clock-time {
    color: #e0c068;
    text-shadow: 0 0 8px rgba(224, 192, 104, 0.3);
}

.chess-clock.low-time .clock-time {
    color: #e87272;
    text-shadow: 0 0 10px rgba(232, 114, 114, 0.4);
}

.chess-clock.flagged .clock-time {
    color: #e87272;
    text-shadow: 0 0 10px rgba(232, 114, 114, 0.5);
}

/* ============================================================
   RESPONSIVE CLOCK: Tablets (~768px)
   ============================================================ */

@media screen and (max-width: 768px) {
    .chess-clock {
        padding: 12px 20px;
        min-height: 56px;
    }

    .clock-player-label {
        font-size: 1.05rem;
        min-width: 90px;
    }

    .clock-time {
        font-size: 2.4rem;
        min-width: 120px;
    }
}

/* ============================================================
   RESPONSIVE CLOCK: Large phones (~640px)
   ============================================================ */

@media screen and (max-width: 640px) {
    .chess-clock {
        padding: 10px 16px;
        min-height: 50px;
        border-width: 2px;
    }

    .clock-player-label {
        font-size: 0.95rem;
        min-width: 80px;
    }

    .clock-time {
        font-size: 2rem;
        min-width: 100px;
        letter-spacing: 2px;
    }
}

/* ============================================================
   RESPONSIVE CLOCK: Phones (~500px)
   ============================================================ */

@media screen and (max-width: 500px) {
    .chess-clock {
        padding: 8px 12px;
        min-height: 44px;
        border-radius: 8px;
        margin: 4px 0;
    }

    .clock-player-label {
        font-size: 0.8rem;
        min-width: 65px;
    }

    .clock-time {
        font-size: 1.6rem;
        min-width: 85px;
    }
}

/* ============================================================
   RESPONSIVE CLOCK: Small phones (~400px)
   ============================================================ */

@media screen and (max-width: 400px) {
    .chess-clock {
        padding: 6px 10px;
        min-height: 38px;
    }

    .clock-player-label {
        font-size: 0.7rem;
        min-width: 55px;
    }

    .clock-time {
        font-size: 1.3rem;
        min-width: 70px;
        letter-spacing: 1px;
    }
}

/* ============================================================
   RESPONSIVE CLOCK: Tiny phones (~360px)
   ============================================================ */

@media screen and (max-width: 360px) {
    .clock-player-label {
        font-size: 0.65rem;
        min-width: 50px;
    }

    .clock-time {
        font-size: 1.1rem;
        min-width: 60px;
    }
}

/* ============================================================
   RESPONSIVE CLOCK: Landscape
   ============================================================ */

@media screen and (max-height: 500px) and (orientation: landscape) {
    .chess-clock {
        padding: 4px 10px;
        min-height: 34px;
        margin: 2px 0;
    }

    .clock-player-label {
        font-size: 0.75rem;
    }

    .clock-time {
        font-size: 1.3rem;
    }
}

@media screen and (max-height: 400px) and (orientation: landscape) {
    .chess-clock {
        padding: 3px 8px;
        min-height: 28px;
    }

    .clock-player-label {
        font-size: 0.65rem;
    }

    .clock-time {
        font-size: 1rem;
    }
}

/* ============================================================
   DISCONNECT BANNER
   ============================================================ */

#disconnect-banner {
    width: 100%;
    max-width: 640px;
    padding: 10px 20px;
    background: #8b0000;
    border: 2px solid #ff4444;
    border-radius: 8px;
    color: #fff;
    text-align: center;
    font-weight: bold;
    font-size: 1rem;
    margin: 4px 0;
    animation: banner-flash 1s infinite;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
}

#disconnect-banner.hidden {
    display: none !important;
}

#disconnect-banner.reconnected {
    background: #2e7d32;
    border-color: #4caf50;
    animation: none;
}

@keyframes banner-flash {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

#disconnect-countdown {
    font-family: 'Courier New', monospace;
    font-size: 1.4rem;
    color: #ffcccc;
    min-width: 30px;
}

#disconnect-banner.reconnected #disconnect-countdown {
    color: #c8e6c9;
}

/* Responsive disconnect banner */
@media screen and (max-width: 500px) {
    #disconnect-banner {
        padding: 7px 12px;
        font-size: 0.8rem;
        gap: 6px;
    }

    #disconnect-countdown {
        font-size: 1.1rem;
    }
}

@media screen and (max-width: 400px) {
    #disconnect-banner {
        padding: 5px 8px;
        font-size: 0.7rem;
    }

    #disconnect-countdown {
        font-size: 0.95rem;
    }
}

@media screen and (max-height: 500px) and (orientation: landscape) {
    #disconnect-banner {
        padding: 4px 8px;
        font-size: 0.7rem;
        margin: 2px 0;
    }
}