@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    -webkit-touch-callout: none;
    user-select: none;
}

:root {
    --game-bg: #16213e;
    --border-color: #0f3460;
    --accent: #e94560;
    --accent-dark: #8b1538;
    --blue: #3498db;
    --green: #2ecc71;
    --gold: #f1c40f;
    --container-width: 100vw;
    --container-height: 100vh;
    --container-height: 100dvh;
}

html, body {
    width: 100%;
    height: 100%;
    height: 100dvh;
    overflow: hidden;
    background: #0a0a1a;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'Press Start 2P', monospace;
    -webkit-font-smoothing: antialiased;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ===== GAME CONTAINER ===== */
#gameContainer {
    position: relative;
    width: var(--container-width);
    height: var(--container-height);
    max-height: 100dvh;
    background: var(--game-bg);
    overflow: hidden;
    border: none;
    border-radius: 0;
    box-shadow: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

canvas {
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

/* ===== SCREENS ===== */
.screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(180deg, #0f0f23 0%, #1a1a3e 50%, #16213e 100%);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 50;
    padding: max(clamp(12px, 4vh, 24px), env(safe-area-inset-top)) max(clamp(12px, 4vh, 24px), env(safe-area-inset-right)) max(clamp(12px, 4vh, 24px), env(safe-area-inset-bottom)) max(clamp(12px, 4vh, 24px), env(safe-area-inset-left));
    overflow-y: auto;
    overflow-x: hidden;
}

.screen.hidden { display: none !important; }

/* ===== TYPOGRAPHY ===== */
h1 {
    font-size: clamp(16px, 5vw, 26px);
    color: var(--accent);
    text-shadow: 3px 3px 0 var(--accent-dark), 0 0 20px rgba(233, 69, 96, 0.5);
    margin-bottom: 8px;
    text-align: center;
    line-height: 1.6;
}

h2 {
    font-size: clamp(12px, 4vw, 18px);
    color: var(--gold);
    margin-bottom: clamp(12px, 3vh, 20px);
    text-align: center;
}

.subtitle {
    font-size: clamp(7px, 2.5vw, 10px);
    color: #a0a0c0;
    margin-bottom: clamp(16px, 5vh, 30px);
    text-align: center;
}

/* ===== BUTTONS ===== */
.pixel-btn {
    font-family: 'Press Start 2P', monospace;
    font-size: clamp(9px, 3vw, 12px);
    padding: clamp(12px, 4vw, 16px) clamp(20px, 8vw, 32px);
    background: var(--accent);
    color: #fff;
    border: 3px solid var(--accent-dark);
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 1px;
    transition: all 0.15s;
    box-shadow: 0 4px 0 var(--accent-dark), 0 6px 10px rgba(0,0,0,0.3);
    margin: 6px;
    min-width: min(80vw, 260px);
    text-align: center;
    border-radius: 0;
    outline: none;
    -webkit-appearance: none;
}

.pixel-btn:hover, .pixel-btn:active {
    background: #ff6b81;
    transform: translateY(-2px);
    box-shadow: 0 6px 0 var(--accent-dark), 0 8px 15px rgba(0,0,0,0.3);
}

.pixel-btn:active {
    transform: translateY(2px);
    box-shadow: 0 2px 0 var(--accent-dark), 0 3px 5px rgba(0,0,0,0.3);
}

.pixel-btn.green {
    background: var(--green);
    border-color: #1e8449;
    box-shadow: 0 4px 0 #1e8449, 0 6px 10px rgba(0,0,0,0.3);
}

.pixel-btn.blue {
    background: var(--blue);
    border-color: #1a5276;
    box-shadow: 0 4px 0 #1a5276, 0 6px 10px rgba(0,0,0,0.3);
}

.pixel-btn.small {
    font-size: clamp(7px, 2.5vw, 9px);
    padding: 8px 16px;
    min-width: auto;
}

/* ===== MENU ===== */
.menu-buttons {
    display: flex;
    flex-direction: column;
    gap: clamp(8px, 2vh, 12px);
    margin-bottom: clamp(16px, 4vh, 24px);
    width: 100%;
    align-items: center;
}

.how-to-play {
    font-size: clamp(6px, 2.2vw, 8px);
    color: #777;
    text-align: center;
    line-height: 2;
    max-width: min(90vw, 320px);
}

.how-to-play h3 {
    color: var(--gold);
    margin-bottom: 8px;
    font-size: clamp(8px, 3vw, 10px);
}

/* ===== LOBBY SCREENS ===== */
.code-display {
    text-align: center;
    margin-bottom: clamp(16px, 3vh, 24px);
    width: 100%;
}

.code-display label {
    display: block;
    font-size: clamp(7px, 2.5vw, 9px);
    color: #a0a0c0;
    margin-bottom: 12px;
}

.code {
    font-size: clamp(20px, 7vw, 34px);
    color: var(--gold);
    background: #2c3e50;
    padding: clamp(12px, 3vh, 16px) clamp(24px, 6vw, 32px);
    border-radius: 8px;
    border: 3px solid var(--gold);
    letter-spacing: 6px;
    margin-bottom: 12px;
    text-shadow: 0 0 10px rgba(241, 196, 15, 0.5);
    display: inline-block;
    word-break: keep-all;
}

.lobby-info {
    text-align: center;
    font-size: clamp(7px, 2.5vw, 9px);
    color: #a0a0c0;
    margin-bottom: clamp(16px, 3vh, 24px);
}

.lobby-info p { margin-bottom: 8px; }

.joined-player {
    color: var(--green);
    font-size: clamp(9px, 3vw, 11px);
    margin-top: 10px;
}

.loading {
    font-size: clamp(10px, 3.5vw, 12px);
    color: var(--gold);
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* ===== JOIN SCREEN ===== */
.code-input {
    text-align: center;
    margin-bottom: clamp(16px, 3vh, 24px);
    width: 100%;
    max-width: min(80vw, 280px);
}

.code-input label {
    display: block;
    font-size: clamp(7px, 2.5vw, 9px);
    color: #a0a0c0;
    margin-bottom: 12px;
}

.code-input input {
    font-family: 'Press Start 2P', monospace;
    font-size: clamp(14px, 5vw, 22px);
    padding: 14px;
    background: #2c3e50;
    border: 2px solid #34495e;
    color: #fff;
    text-align: center;
    width: 100%;
    border-radius: 4px;
    text-transform: uppercase;
    letter-spacing: 4px;
    outline: none;
    -webkit-appearance: none;
}

.code-input input:focus {
    border-color: var(--gold);
}

.error {
    color: #e74c3c;
    font-size: clamp(7px, 2.5vw, 9px);
    margin-bottom: 12px;
    text-align: center;
    max-width: min(80vw, 280px);
}

/* ===== TWITCH AUTH ===== */
#authSection {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    margin-bottom: clamp(16px, 4vh, 24px);
}

#menuActions {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    gap: clamp(8px, 2vh, 12px);
}

#menuActions.hidden {
    display: none !important;
}

#logoutBtn {
    margin-top: 4px;
}

.pixel-btn.twitch {
    background: #9146ff;
    border-color: #5a2ea6;
    box-shadow: 0 4px 0 #5a2ea6, 0 6px 10px rgba(0,0,0,0.3);
}

.pixel-btn.twitch:hover, .pixel-btn.twitch:active {
    background: #b088ff;
    box-shadow: 0 6px 0 #5a2ea6, 0 8px 15px rgba(0,0,0,0.3);
}

.auth-hint {
    font-size: clamp(6px, 2vw, 8px);
    color: #777;
    text-align: center;
}

.user-profile {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: clamp(12px, 3vh, 20px);
    background: rgba(0,0,0,0.4);
    padding: 8px 16px;
    border-radius: 6px;
    border: 1px solid #333;
}

.user-profile img {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 2px solid var(--gold);
    image-rendering: auto;
}

.user-profile span {
    font-size: clamp(9px, 3vw, 11px);
    color: var(--gold);
}

/* ===== LEADERBOARD ===== */
.leaderboard-tabs {
    display: flex;
    gap: 6px;
    margin-bottom: clamp(12px, 3vh, 20px);
    z-index: 20;
}

.tab-btn {
    font-family: 'Press Start 2P', monospace;
    font-size: clamp(6px, 2vw, 8px);
    padding: 8px 12px;
    background: #2c3e50;
    border: 2px solid #34495e;
    color: #a0a0c0;
    cursor: pointer;
    text-transform: uppercase;
    transition: all 0.15s;
    outline: none;
    -webkit-appearance: none;
}

.tab-btn:hover {
    border-color: var(--gold);
    color: #fff;
}

.tab-btn.active {
    background: var(--gold);
    border-color: var(--gold);
    color: #0a0a1a;
}

.leaderboard-list {
    width: 100%;
    max-width: min(90vw, 360px);
    max-height: calc(60vh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: clamp(12px, 3vh, 20px);
}

.leaderboard-row {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(0,0,0,0.5);
    padding: 8px 10px;
    border-radius: 4px;
    border: 1px solid #333;
    font-size: clamp(7px, 2.2vw, 9px);
}

.leaderboard-row.top-1 { border-color: var(--gold); background: rgba(241,196,15,0.08); }
.leaderboard-row.top-2 { border-color: #c0c0c0; background: rgba(192,192,192,0.08); }
.leaderboard-row.top-3 { border-color: #cd7f32; background: rgba(205,127,50,0.08); }

.lb-rank {
    min-width: 28px;
    text-align: center;
    color: var(--gold);
    font-weight: bold;
}

.lb-avatar {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 2px solid var(--gold);
    image-rendering: auto;
    flex-shrink: 0;
}

.lb-name {
    flex: 1;
    color: #fff;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.lb-score {
    color: var(--gold);
    min-width: 50px;
    text-align: right;
}

.lb-stats {
    font-size: clamp(5px, 1.6vw, 7px);
    color: #777;
    min-width: 60px;
    text-align: right;
}

/* ===== GAME SCREEN ===== */
#gameScreen {
    background: transparent;
    pointer-events: none;
    padding: 0;
}

#gameScreen > * {
    pointer-events: auto;
}

/* ===== ROUND WINS ===== */
#roundWins {
    position: absolute;
    top: calc(max(8px, env(safe-area-inset-top)) + 30px);
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 16px;
    z-index: 20;
    background: rgba(0,0,0,0.65);
    padding: 4px 12px;
    border-radius: 4px;
    border: 1px solid #333;
    white-space: nowrap;
}

.wins-row {
    display: flex;
    gap: 4px;
    align-items: center;
}

.win-dot {
    width: 8px;
    height: 8px;
    border-radius: 2px;
    border: 1px solid rgba(255,255,255,0.25);
}

.win-dot.host-filled { background: var(--blue); border-color: var(--blue); box-shadow: 0 0 4px rgba(52,152,219,0.5); }
.win-dot.guest-filled { background: #e74c3c; border-color: #e74c3c; box-shadow: 0 0 4px rgba(231,76,60,0.5); }
.win-dot.empty { background: transparent; }

#scoreBoard {
    position: absolute;
    top: max(8px, env(safe-area-inset-top));
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 10px;
    z-index: 20;
    background: rgba(0,0,0,0.75);
    padding: 5px 12px;
    border-radius: 4px;
    border: 2px solid #333;
    white-space: nowrap;
}

.score-item {
    font-size: clamp(7px, 2.2vw, 9px);
    color: #fff;
}

.score-item.player { color: var(--blue); }
.score-item.enemy { color: #e74c3c; }
.score-item.vs { color: var(--gold); }

.army-info {
    position: absolute;
    top: calc(max(8px, env(safe-area-inset-top)) + 45px);
    z-index: 20;
    background: rgba(0,0,0,0.65);
    padding: 5px 8px;
    border-radius: 4px;
    font-size: clamp(5px, 1.8vw, 7px);
    color: #bdc3c7;
    line-height: 1.8;
    max-width: 100px;
    word-break: break-word;
}

#armyInfoLeft { left: max(6px, env(safe-area-inset-left)); top: calc(max(8px, env(safe-area-inset-top)) + 55px); }
#armyInfoRight { right: max(6px, env(safe-area-inset-right)); text-align: right; top: calc(max(8px, env(safe-area-inset-top)) + 55px); }

/* ===== DRAFT UI ===== */
#draftUI {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    padding-bottom: env(safe-area-inset-bottom);
    background: linear-gradient(0deg, rgba(0,0,0,0.95) 0%, rgba(0,0,0,0.75) 100%);
    padding: 10px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    z-index: 30;
}

#draftUI.hidden { display: none; }

.draft-title {
    font-size: clamp(8px, 2.5vw, 10px);
    color: var(--gold);
    text-align: center;
    margin-bottom: 8px;
}

.draft-turn {
    font-size: clamp(7px, 2.2vw, 8px);
    color: #a0a0c0;
    text-align: center;
    margin-bottom: 6px;
}

.draft-cards {
    display: flex;
    justify-content: center;
    gap: 6px;
    margin-bottom: 8px;
    flex-wrap: wrap;
}

.draft-card {
    width: clamp(75px, 23vw, 110px);
    height: auto;
    min-height: 120px;
    background: #2c3e50;
    border: 3px solid #34495e;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    position: relative;
    overflow: hidden;
    padding: 6px;
}

.draft-card:hover {
    border-color: var(--gold);
    transform: translateY(-4px);
    box-shadow: 0 8px 20px rgba(241, 196, 15, 0.3);
}

.draft-card.disabled {
    opacity: 0.5;
    cursor: default;
    pointer-events: none;
}

.draft-card.selected {
    border-color: var(--green);
    box-shadow: 0 0 15px rgba(46, 204, 113, 0.5);
}

.draft-card .card-icon {
    font-size: clamp(22px, 7vw, 34px);
    margin-bottom: 4px;
}

.draft-card .card-name {
    font-size: clamp(6px, 2vw, 8px);
    color: #fff;
    text-align: center;
    margin-bottom: 3px;
}

.draft-card .card-stats {
    font-size: clamp(5px, 1.6vw, 6px);
    color: #bdc3c7;
    text-align: center;
    line-height: 1.7;
}

.draft-card .card-count {
    position: absolute;
    top: 3px;
    right: 3px;
    background: #e74c3c;
    color: #fff;
    font-size: clamp(7px, 2.2vw, 9px);
    padding: 2px 5px;
    border-radius: 3px;
    font-weight: bold;
}

/* ===== BATTLE CONTROLS ===== */
#battleControls {
    position: absolute;
    bottom: max(16px, env(safe-area-inset-bottom));
    left: 50%;
    transform: translateX(-50%);
    z-index: 25;
}

#battleControls.hidden { display: none; }

/* ===== ROUND RESULT ===== */
#roundResult {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0,0,0,0.92);
    border: 3px solid var(--gold);
    padding: clamp(16px, 5vh, 24px) clamp(24px, 6vw, 36px);
    border-radius: 8px;
    text-align: center;
    z-index: 50;
    min-width: min(70vw, 280px);
    max-width: calc(100vw - 32px);
    max-height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 32px);
    overflow-y: auto;
}

#roundResult.hidden { display: none; }

.result-text {
    font-size: clamp(12px, 4vw, 16px);
    margin-bottom: 16px;
}

.result-text.win { color: var(--green); }
.result-text.lose { color: #e74c3c; }
.result-text.draw { color: var(--gold); }

/* ===== GAME OVER ===== */
#gameOverOverlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    background: rgba(0,0,0,0.94);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 20px;
    padding-top: max(20px, env(safe-area-inset-top));
    padding-bottom: max(20px, env(safe-area-inset-bottom));
    overflow-y: auto;
}

#gameOverOverlay.hidden { display: none; }

#gameOverOverlay h2 {
    font-size: clamp(18px, 6vw, 28px);
    margin-bottom: 20px;
}

#gameOverOverlay h2.victory { color: var(--green); }
#gameOverOverlay h2.defeat { color: #e74c3c; }

.final-score {
    font-size: clamp(10px, 3vw, 12px);
    color: #a0a0c0;
    margin-bottom: 24px;
}

/* ===== TABLET (600px - 1024px) ===== */
@media (min-width: 600px) and (max-width: 1024px) {
    :root {
        --container-width: min(100vw, 480px);
        --container-height: auto;
    }

    #gameContainer {
        aspect-ratio: 1/2;
        border: 4px solid var(--border-color);
        border-radius: 12px;
        box-shadow: 0 0 50px rgba(15, 52, 96, 0.6), inset 0 0 80px rgba(0,0,0,0.3);
    }

    .menu-buttons {
        max-width: 360px;
    }

    .pixel-btn {
        min-width: 240px;
    }

    .draft-card {
        min-height: 150px;
    }
}

/* ===== DESKTOP (>1024px) ===== */
@media (min-width: 1025px) {
    :root {
        --container-width: 450px;
        --container-height: 900px;
    }

    body {
        background: radial-gradient(circle at center, #16213e 0%, #0a0a1a 100%);
    }

    #gameContainer {
        border: 4px solid var(--border-color);
        border-radius: 16px;
        box-shadow: 0 0 60px rgba(15, 52, 96, 0.7), inset 0 0 100px rgba(0,0,0,0.4);
    }

    /* Decorative side glow */
    body::before, body::after {
        content: '';
        position: fixed;
        top: 0;
        width: max(calc((100vw - 520px) / 2), 0px);
        height: 100vh;
        background: linear-gradient(180deg, rgba(15,52,96,0.08) 0%, rgba(233,69,96,0.03) 50%, rgba(15,52,96,0.08) 100%);
        pointer-events: none;
    }

    body::before { left: 0; }
    body::after { right: 0; }

    .menu-buttons {
        max-width: 360px;
        gap: 14px;
    }

    .pixel-btn {
        min-width: 260px;
        font-size: 11px;
        padding: 16px 32px;
    }

    h1 { font-size: 24px; }
    .how-to-play { font-size: 8px; }

    .draft-card {
        min-height: 160px;
    }
}

/* ===== LANDSCAPE MOBILE (короткая высота) ===== */
@media (max-height: 500px) and (orientation: landscape) {
    :root {
        --container-width: 50vh;
        --container-height: 100vh;
    }

    #gameContainer {
        margin: 0 auto;
    }

    h1 {
        font-size: 14px;
        margin-bottom: 4px;
    }

    .subtitle {
        margin-bottom: 10px;
    }

    .menu-buttons {
        gap: 6px;
        margin-bottom: 10px;
    }

    .pixel-btn {
        padding: 10px 20px;
        font-size: 8px;
        margin: 4px;
        min-width: 180px;
    }

    .how-to-play {
        display: none;
    }

    .player-name-input {
        margin-bottom: 10px;
    }

    .draft-card {
        min-height: 90px;
        width: 70px;
    }

    #roundResult {
        padding: 12px 20px;
    }
}

/* ===== VERY SMALL PHONES ===== */
@media (max-width: 360px) {
    .pixel-btn {
        min-width: 90vw;
        font-size: 8px;
        padding: 10px 16px;
    }

    h1 { font-size: 14px; }

    .draft-card {
        width: 70px;
        min-height: 100px;
    }

    .code {
        font-size: 18px;
        padding: 10px 20px;
        letter-spacing: 3px;
    }
}

/* ===== REDUCED MOTION ===== */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ===== HIDE SCROLLBAR ===== */
::-webkit-scrollbar {
    display: none;
}