:root {
    --bg: #2a2f24;
    --bg-soft: #3a3f33;
    --card: #1f241b;
    --card-elevated: #252b20;
    --text: #e5e9da;
    --text-muted: #aab39a;
    --accent: #81b64c;
    --accent-strong: #95c86a;
    --danger: #d9655d;
    --warn: #dba84d;
    --ok: #61b88a;
    --north: #5a9bd8;
    --south: #d46b61;
    --radius: 14px;
    --radius-sm: 10px;
    --border: rgba(229, 233, 218, 0.09);
    --shadow: 0 14px 36px rgba(0, 0, 0, 0.35);
}

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

html {
    scroll-behavior: auto !important;
    overflow-anchor: none;
    /* Prevent any auto-scrolling */
    overscroll-behavior: none;
    height: 100%;
}

body {
    font-family: 'Archivo', sans-serif;
    background:
        radial-gradient(circle at 12% 10%, rgba(149, 200, 106, 0.12), transparent 35%),
        radial-gradient(circle at 86% 8%, rgba(212, 107, 97, 0.08), transparent 30%),
        linear-gradient(140deg, #1b2017 0%, #2a2f24 50%, #32392a 100%);
    color: var(--text);
    min-height: 100vh;
    overflow-x: hidden;
    /* Prevent scroll-into-view behavior */
    overflow-anchor: none;
    overscroll-behavior: none;
}

/* Lock scroll when game is active - prevents canvas drift */
body.game-active {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
    width: 100%;
    height: 100vh;
    height: 100dvh;
}

.hidden {
    display: none !important;
}

#game-container {
    max-width: 1780px;
    margin: 0 auto;
    padding: 20px;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 340px;
    grid-template-areas:
        "main side";
    gap: 14px;
}

/* When game is active, ensure container stays in place */
body.game-active #game-container {
    height: 100%;
    max-height: 100vh;
    max-height: 100dvh;
    overflow: auto;
}

#online-status {
    display: flex;
    flex-direction: column;
    gap: 2px;
    text-align: center;
    color: #ced9be;
    font-size: 0.84rem;
    font-family: 'IBM Plex Mono', monospace;
    margin-bottom: 10px;
}

#notifications {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 999;
    width: min(360px, calc(100vw - 32px));
}

.notification {
    border-radius: 10px;
    padding: 12px 14px;
    margin-bottom: 10px;
    border: 1px solid var(--border);
    box-shadow: 0 10px 22px rgba(0, 0, 0, 0.25);
    background: #23281e;
    color: var(--text);
    animation: slideIn 0.2s ease-out;
    font-size: 0.92rem;
    line-height: 1.4;
}

.notification.error {
    background: rgba(217, 101, 93, 0.16);
    border-color: rgba(217, 101, 93, 0.35);
}

.notification.success {
    background: rgba(97, 184, 138, 0.16);
    border-color: rgba(97, 184, 138, 0.35);
}

.notification.warning {
    background: rgba(219, 168, 77, 0.16);
    border-color: rgba(219, 168, 77, 0.35);
}

.notification.info {
    background: rgba(90, 155, 216, 0.16);
    border-color: rgba(90, 155, 216, 0.35);
}

@keyframes slideIn {
    from {
        transform: translateX(26px);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

@keyframes slideOut {
    from {
        transform: translateX(0);
        opacity: 1;
    }
    to {
        transform: translateX(24px);
        opacity: 0;
    }
}

#main-content {
    grid-area: main;
    display: grid;
    grid-template-columns: 1fr;
    grid-template-areas:
        "north"
        "board"
        "south";
    gap: 12px;
    align-items: start;
}

#side-panel-left,
#side-panel-right {
    width: 100%;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

#side-panel-left {
    grid-area: north;
}

#side-panel-right {
    grid-area: south;
}

#game-board {
    grid-area: board;
    display: flex;
    justify-content: center;
    width: 100%;
}

#board-shell {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    width: 100%;
}

#board-middle {
    display: flex;
    align-items: stretch;
    gap: 6px;
}

.coords-strip {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 22px;
    font-family: 'IBM Plex Mono', monospace;
    font-size: 0.74rem;
    color: #d9e4c7;
    letter-spacing: 0.03em;
}

.coords-strip-vertical {
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: 24px;
    font-family: 'IBM Plex Mono', monospace;
    font-size: 0.74rem;
    color: #d9e4c7;
}

.coord-cell {
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0.92;
}

.panel {
    background: linear-gradient(150deg, rgba(37, 43, 32, 0.98), rgba(29, 34, 25, 0.96));
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px;
    box-shadow: var(--shadow);
}

.panel h2,
.panel h3 {
    font-size: 0.95rem;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: #dfe8ce;
    border-bottom: 1px solid rgba(229, 233, 218, 0.16);
    padding-bottom: 8px;
    margin-bottom: 10px;
}

.stat-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 10px;
    margin-bottom: 6px;
    border-radius: 8px;
    font-size: 0.88rem;
    color: var(--text-muted);
    background: rgba(255, 255, 255, 0.03);
}

.stat-row span:last-child {
    color: #eff5e2;
    font-weight: 700;
}

.units-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
    gap: 6px;
    margin-top: 10px;
    width: 100%;
}

.unit-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    padding: 7px 8px;
    border-radius: 8px;
    border: 1px solid transparent;
    background: rgba(255, 255, 255, 0.04);
    font-size: 0.84rem;
    cursor: pointer;
    transition: transform 0.08s ease, background-color 0.12s ease, border-color 0.12s ease;
}

.unit-item:hover {
    transform: translateY(-1px);
    background: rgba(255, 255, 255, 0.1);
}

.unit-item.connected {
    border-color: rgba(97, 184, 138, 0.4);
}

.unit-item.disconnected {
    border-color: rgba(217, 101, 93, 0.4);
    opacity: 0.8;
}

.unit-item.focused {
    border-color: rgba(255, 255, 255, 0.85);
    box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.55) inset;
}

.unit-inline-icon {
    display: inline-block;
    vertical-align: middle;
    object-fit: contain;
}

.unit-inline-symbol {
    display: inline-block;
    line-height: 1;
}

#bottom-panel {
    grid-area: side;
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
    margin-top: 0;
    align-content: start;
}

#turn-indicator {
    text-align: center;
    font-size: 1.1rem;
    margin-bottom: 10px;
}

#turn-info {
    display: grid;
    gap: 8px;
    margin-bottom: 12px;
    font-size: 0.9rem;
    color: var(--text-muted);
}

#turn-info strong {
    color: #eff5e2;
    font-family: 'IBM Plex Mono', monospace;
}

#action-buttons {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
}

#game-controls-buttons {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
}

.game-btn {
    background: linear-gradient(180deg, #88bc54, #72a744);
    color: #17200f;
    border: 0;
    border-radius: 10px;
    padding: 10px 12px;
    font-family: 'Archivo', sans-serif;
    font-size: 0.88rem;
    font-weight: 700;
    cursor: pointer;
    transition: transform 0.08s ease, filter 0.15s ease;
    /* Prevent buttons from causing scroll when focused */
    scroll-margin: 0;
    scroll-padding: 0;
}

.game-btn:hover {
    filter: brightness(1.07);
}

.game-btn:active {
    transform: translateY(1px);
}

.game-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
    filter: saturate(0.6);
    transform: none;
}

.secondary-btn {
    background: linear-gradient(180deg, #5e6c54, #4d5a44);
    color: #e7eedd;
}

.danger-btn {
    background: linear-gradient(180deg, #d87267, #b95147);
    color: #fff4f2;
}

.battle-stat {
    display: flex;
    justify-content: space-between;
    padding: 9px 10px;
    margin: 7px 0;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.04);
    font-size: 0.9rem;
}

.power-value {
    font-family: 'IBM Plex Mono', monospace;
    font-weight: 700;
}

#battle-result {
    margin-top: 10px;
    padding: 9px 10px;
    text-align: center;
    border-radius: 8px;
    font-weight: 700;
    background: rgba(255, 255, 255, 0.05);
}

#battle-result.capture {
    background: rgba(217, 101, 93, 0.23);
    color: #f3d6d2;
}

#battle-result.retreat {
    background: rgba(219, 168, 77, 0.24);
    color: #f5e5c2;
}

#battle-result.defend {
    background: rgba(97, 184, 138, 0.24);
    color: #d8f1e4;
}

#battle-breakdown {
    margin-top: 10px;
    border-top: 1px solid rgba(229, 233, 218, 0.18);
    padding-top: 10px;
}

.battle-breakdown-section {
    display: grid;
    gap: 8px;
}

.battle-units-list h4 {
    margin-bottom: 6px;
    font-size: 0.8rem;
    color: #d9e9bf;
}

.battle-unit-item {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.8rem;
    padding: 6px;
    border-radius: 7px;
    background: rgba(255, 255, 255, 0.04);
}

.battle-unit-item .unit-type {
    flex: 1;
}

.battle-unit-item .unit-power {
    font-family: 'IBM Plex Mono', monospace;
}

#unit-details {
    display: grid;
    gap: 5px;
    font-size: 0.85rem;
    color: var(--text-muted);
}

#unit-details strong {
    color: #f0f5e5;
    font-weight: 700;
}

#no-selection {
    color: var(--text-muted);
}

canvas {
    border-radius: 10px;
    border: 1px solid rgba(229, 233, 218, 0.2);
    box-shadow: 0 16px 30px rgba(0, 0, 0, 0.36);
    background: #f2f5ea;
    display: block;
    margin: 0 auto;
    outline: none !important;
    /* Prevent canvas from causing page scroll when focused */
    scroll-margin: 0 !important;
    scroll-padding: 0 !important;
    /* Additional prevention */
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

/* Prevent ALL focusable elements from causing scroll */
button, input, select, textarea, a, canvas, [tabindex] {
    scroll-margin-block: 0 !important;
    scroll-margin-inline: 0 !important;
}

/* Prevent any element from triggering scroll adjustments */
* {
    scroll-margin: 0 !important;
    scroll-padding: 0 !important;
}

#move-history-panel {
    min-height: 220px;
}

.move-history-scroll {
    max-height: 215px;
    overflow-y: auto;
    padding-right: 4px;
}

.move-history-scroll::-webkit-scrollbar,
.tutorial-content::-webkit-scrollbar {
    width: 8px;
}

.move-history-scroll::-webkit-scrollbar-track,
.tutorial-content::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.08);
    border-radius: 8px;
}

.move-history-scroll::-webkit-scrollbar-thumb,
.tutorial-content::-webkit-scrollbar-thumb {
    background: rgba(129, 182, 76, 0.85);
    border-radius: 8px;
}

.move-entry {
    display: grid;
    grid-template-columns: auto auto auto 1fr auto;
    gap: 6px;
    align-items: center;
    padding: 7px 8px;
    margin-bottom: 6px;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.04);
    font-size: 0.76rem;
}

.move-turn,
.move-coords,
.tutorial-progress {
    font-family: 'IBM Plex Mono', monospace;
}

.move-result {
    padding: 2px 6px;
    border-radius: 999px;
    font-weight: 700;
    text-transform: uppercase;
    font-size: 0.65rem;
}

.move-result.capture {
    background: rgba(217, 101, 93, 0.3);
}

.move-result.retreat {
    background: rgba(219, 168, 77, 0.3);
}

.move-result.defend {
    background: rgba(97, 184, 138, 0.3);
}

.modal,
.tutorial-overlay {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    background: rgba(4, 8, 5, 0.72);
    backdrop-filter: blur(5px);
}

.modal.hidden,
.tutorial-overlay.hidden,
.tutorial-game-overlay.hidden {
    display: none;
}

/* Interactive Tutorial Overlay (on game board) */
.tutorial-game-overlay {
    position: fixed;
    top: 10px;
    right: 10px;
    z-index: 900;
    pointer-events: none;
}

.tutorial-game-box {
    width: 400px;
    padding: 18px 22px;
    background: linear-gradient(150deg, rgba(37, 43, 32, 0.96), rgba(27, 32, 23, 0.94));
    border: 2px solid var(--accent);
    border-radius: 10px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.7), 0 0 20px rgba(118, 150, 86, 0.4);
    pointer-events: auto;
}

.tutorial-game-box h2 {
    font-size: 19px;
    margin-bottom: 10px;
    color: #f7d65b;
}

.tutorial-game-text {
    font-size: 14px;
    line-height: 1.5;
    color: var(--text);
    margin-bottom: 12px;
    max-height: 300px;
    overflow-y: auto;
}

.tutorial-game-text::-webkit-scrollbar {
    width: 6px;
}

.tutorial-game-text::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.05);
    border-radius: 3px;
}

.tutorial-game-text::-webkit-scrollbar-thumb {
    background: rgba(118, 150, 86, 0.5);
    border-radius: 3px;
}

.modal-content,
.tutorial-content,
.login-container,
.lobby-container {
    width: min(680px, calc(100vw - 30px));
    border-radius: 16px;
    border: 1px solid var(--border);
    background: linear-gradient(150deg, rgba(37, 43, 32, 0.98), rgba(27, 32, 23, 0.96));
    color: var(--text);
    box-shadow: var(--shadow);
}

.modal-content,
.tutorial-content {
    padding: 26px;
}

.tutorial-content {
    max-height: 82vh;
    overflow-y: auto;
    position: relative;
    font-size: 16px;
    line-height: 1.7;
}

#tutorial-step-title {
    font-size: 26px;
    margin-bottom: 18px;
    color: #f7d65b;
}

.tutorial-close {
    position: absolute;
    top: 12px;
    right: 12px;
    border: 0;
    background: rgba(255, 255, 255, 0.08);
    color: #eff5e2;
    border-radius: 999px;
    width: 34px;
    height: 34px;
    font-size: 1.15rem;
    cursor: pointer;
}

.tutorial-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    margin-top: 18px;
    padding-top: 14px;
    border-top: 1px solid rgba(229, 233, 218, 0.16);
}

.tutorial-inline-icon {
    width: 22px;
    height: 22px;
    object-fit: contain;
    vertical-align: text-bottom;
    margin: 0 3px;
}

/* Combat Diagram Styles */
.combat-diagram {
    display: inline-block;
    background: #2a2a2a;
    border: 3px solid #769656;
    border-radius: 8px;
    padding: 10px;
    margin: 15px auto;
}

.combat-row {
    display: flex;
}

.combat-cell {
    width: 55px;
    height: 55px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #444;
    background: #1a1a1a;
    position: relative;
    font-size: 24px;
}

.combat-cell img {
    width: 38px;
    height: 38px;
    object-fit: contain;
}

.fort-tile-container {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
}

.fort-background {
    position: absolute;
    font-size: 32px;
    opacity: 0.7;
    z-index: 0;
}

.fort-tile-container img {
    position: relative;
    z-index: 1;
    width: 38px;
    height: 38px;
}

.cell-label {
    position: absolute;
    bottom: 2px;
    font-size: 8px;
    font-weight: bold;
    color: #dab66a;
    text-shadow: 1px 1px 2px #000;
}

.combat-explanation {
    background: rgba(118, 150, 86, 0.15);
    border-left: 4px solid #769656;
    padding: 14px;
    margin-top: 12px;
    border-radius: 4px;
    font-size: 15px;
    line-height: 1.7;
}

.combat-explanation strong {
    color: #f7d65b;
}

.tutorial-stats {
    background: rgba(75, 139, 200, 0.1);
    border: 2px solid #4b8bc8;
    padding: 16px;
    border-radius: 8px;
    font-size: 15px;
    line-height: 1.8;
}

.combat-outcomes {
    font-size: 15px;
}

.outcome-box {
    padding: 12px;
    border-radius: 6px;
    border-left: 5px solid;
    margin: 8px 0;
}

.outcome-box.capture {
    background: rgba(201, 99, 92, 0.15);
    border-color: #c9635c;
}

.outcome-box.retreat {
    background: rgba(247, 214, 91, 0.15);
    border-color: #f7d65b;
}

.outcome-box.defend {
    background: rgba(118, 150, 86, 0.15);
    border-color: #769656;
}

.turn-structure, .victory-conditions, .final-tips {
    background: rgba(42, 42, 42, 0.5);
    padding: 15px;
    border-radius: 8px;
    border: 2px solid #555;
    line-height: 1.8;
}

.turn-structure strong {
    color: #4b8bc8;
}

.victory-conditions strong {
    color: #f7d65b;
}

.final-tips {
    font-size: 15px;
}

.final-tips strong {
    color: #b5df90;
}

.screen {
    width: 100%;
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

.screen.hidden,
.auth-panel.hidden {
    display: none;
}

.login-container,
.lobby-container {
    padding: 32px;
}

.login-container h1,
.lobby-header h2,
.auth-panel h3 {
    letter-spacing: 0.09em;
}

.login-options,
.auth-buttons,
.lobby-actions {
    display: grid;
    gap: 10px;
    margin-top: 18px;
}

.big-btn {
    min-height: 48px;
    font-size: 0.95rem;
}

.divider {
    margin: 14px 0;
    color: var(--text-muted);
    text-transform: uppercase;
    font-size: 0.72rem;
    letter-spacing: 0.1em;
}

.input-field,
.share-link {
    width: 100%;
    background: rgba(255, 255, 255, 0.07);
    border: 1px solid rgba(229, 233, 218, 0.2);
    color: var(--text);
    border-radius: 10px;
    padding: 10px 12px;
    margin-bottom: 8px;
    font-family: 'Archivo', sans-serif;
}

.input-field::placeholder {
    color: rgba(229, 233, 218, 0.5);
}

.games-list {
    margin-top: 12px;
    display: grid;
    gap: 10px;
}

.game-item,
.waiting-panel {
    border-radius: 10px;
    border: 1px solid rgba(229, 233, 218, 0.16);
    background: rgba(255, 255, 255, 0.04);
    padding: 12px;
}

.game-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.no-games,
.no-moves {
    color: var(--text-muted);
    font-size: 0.9rem;
    text-align: center;
    padding: 10px;
}

.spinner {
    margin: 10px auto 0;
    width: 26px;
    height: 26px;
    border: 3px solid rgba(255, 255, 255, 0.2);
    border-top-color: var(--accent-strong);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

@media (max-width: 1440px) {
    #game-container {
        grid-template-columns: minmax(0, 1fr) 320px;
    }
}

@media (max-width: 1180px) {
    #game-container {
        grid-template-columns: 1fr;
        grid-template-areas:
            "main"
            "side";
    }

    #main-content {
        grid-template-columns: 1fr;
    }

    #side-panel-left,
    #side-panel-right {
        width: 100%;
    }

    #game-board {
        overflow-x: auto;
        padding-bottom: 4px;
    }
}

@media (max-width: 760px) {
    #game-container {
        padding: 12px;
    }

    #bottom-panel {
        grid-template-columns: 1fr;
    }

    .panel {
        padding: 12px;
    }

    .move-entry {
        grid-template-columns: 1fr;
        gap: 4px;
    }
}

/* Mobile-specific styles */
@media (max-width: 768px) {
    /* Make body scrollable on mobile */
    body {
        overflow-y: auto;
        overflow-x: hidden;
    }

    /* Game container takes full width, vertical layout */
    #game-container {
        display: flex;
        flex-direction: column;
        padding: 0;
        max-width: 100%;
        gap: 0;
    }

    /* Main content (board) fills viewport height */
    #main-content {
        position: relative;
        min-height: 100vh;
        min-height: 100dvh; /* Dynamic viewport height for mobile browsers */
        display: flex;
        flex-direction: column;
        gap: 0;
        padding: 0;
    }

    /* Hide side unit panels on mobile - they'll be in bottom panel */
    #side-panel-left,
    #side-panel-right {
        display: none;
    }

    /* Board takes all available space */
    #board-shell {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        width: 100%;
        padding: 8px;
    }

    #game-board {
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        touch-action: none; /* Disable default touch actions for pinch zoom */
    }

    /* Floating control buttons at bottom of viewport */
    #mobile-floating-controls {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        display: flex;
        gap: 8px;
        padding: 12px;
        background: linear-gradient(to top, rgba(27, 32, 23, 0.98), rgba(27, 32, 23, 0.92));
        backdrop-filter: blur(10px);
        border-top: 1px solid rgba(229, 233, 218, 0.15);
        z-index: 800;
        box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.4);
    }

    #mobile-floating-controls .game-btn {
        flex: 1;
        padding: 14px 10px;
        font-size: 0.9rem;
    }

    /* Tutorial button on mobile */
    #mobile-tutorial-btn {
        position: fixed;
        top: 10px;
        left: 10px;
        z-index: 850;
        background: rgba(37, 43, 32, 0.95);
        border: 1px solid rgba(229, 233, 218, 0.3);
        border-radius: 8px;
        padding: 10px 12px;
        font-size: 1.1rem;
        cursor: pointer;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
    }

    /* Bottom panel becomes scrollable area below the board */
    #bottom-panel {
        position: relative;
        display: grid;
        grid-template-columns: 1fr;
        gap: 12px;
        padding: 16px;
        margin-top: 0;
        background: var(--bg);
        z-index: 1;
    }

    /* Add unit panels to bottom panel on mobile */
    #mobile-unit-panels {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 12px;
    }

    /* Notifications adjust for mobile */
    #notifications {
        top: 70px; /* Below fullscreen button */
        right: 10px;
        left: 10px;
        width: auto;
    }

    /* Tutorial overlay adjustments for mobile */
    .tutorial-game-overlay {
        top: auto;
        bottom: 80px; /* Above floating controls */
        right: 10px;
        left: 10px;
    }

    .tutorial-game-box {
        width: 100%;
        max-width: 100%;
        padding: 14px 16px;
    }

    .tutorial-game-text {
        max-height: 200px;
        font-size: 13px;
    }

    /* Make modals mobile-friendly */
    .modal-content,
    .tutorial-content,
    .login-container,
    .lobby-container {
        width: calc(100vw - 20px);
        max-height: 90vh;
        overflow-y: auto;
        margin: 10px;
    }

    /* Coordinates smaller on mobile */
    .coords-strip,
    .coords-strip-vertical {
        font-size: 0.65rem;
    }

    .coords-strip-vertical {
        width: 18px;
    }
}

/* Fullscreen mode styles */
body.fullscreen-mode {
    overflow: hidden;
}

body.fullscreen-mode #game-container {
    height: 100vh;
    height: 100dvh;
}

body.fullscreen-mode #main-content {
    height: 100vh;
    height: 100dvh;
}

body.fullscreen-mode #bottom-panel {
    display: none; /* Hide scrollable content in fullscreen */
}

/* Mobile Portrait */
@media (max-width: 768px) and (orientation: portrait) {

    /* Game content fills entire viewport in portrait */
    #main-content {
        padding-top: 0;
        min-height: calc(100vh - 80px); /* Reserve space for floating controls */
        min-height: calc(100dvh - 80px);
        display: flex;
        flex-direction: column;
        width: 100vw;
        overflow-x: hidden;
    }

    /* Board fills all available space */
    #board-shell {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0;
        width: 100%;
        height: 100%;
    }

    #board-middle {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100%;
    }

    #game-board {
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* Canvas fills available height */
    canvas {
        max-width: none !important;
        max-height: calc(100vh - 80px) !important;
        max-height: calc(100dvh - 80px) !important;
        height: auto !important;
        width: auto !important;
        display: block;
    }

    /* Hide coordinate strips on small portrait screens */
    #coords-top,
    #coords-left,
    #coords-right {
        display: none;
    }

    /* Side panels already hidden via earlier rules */
    #side-panel-left,
    #side-panel-right {
        display: none !important;
    }
}

/* Mobile Landscape - Optimal layout */
@media (max-width: 932px) and (orientation: landscape) {
    /* Game container uses full viewport height in landscape */
    #game-container {
        height: 100vh;
        height: 100dvh;
        overflow: hidden;
    }

    #main-content {
        min-height: 100vh;
        min-height: 100dvh;
        padding: 0;
    }

    /* Board takes maximum possible space */
    #board-shell {
        padding: 4px 8px;
    }

    /* Floating controls more compact in landscape */
    #mobile-floating-controls {
        padding: 8px 12px;
        gap: 6px;
    }

    #mobile-floating-controls .game-btn {
        padding: 10px 8px;
        font-size: 0.85rem;
    }

    /* Notifications adjust position for landscape */
    #notifications {
        top: 10px;
        right: 10px;
        left: auto;
        width: auto;
        max-width: 280px;
    }

    /* Fullscreen button smaller in landscape */
    #fullscreen-btn,
    #mobile-tutorial-btn {
        padding: 8px 10px;
        font-size: 1rem;
    }

    /* Tutorial overlay more compact */
    .tutorial-game-overlay {
        bottom: 60px;
        right: 10px;
        left: 10px;
    }

    .tutorial-game-box {
        padding: 12px 14px;
    }

    .tutorial-game-text {
        max-height: 150px;
        font-size: 12px;
    }

    /* Coordinates even smaller in landscape */
    .coords-strip,
    .coords-strip-vertical {
        font-size: 0.6rem;
    }

    .coords-strip-vertical {
        width: 16px;
    }

    /* Bottom panel scrolls but starts hidden offscreen */
    #bottom-panel {
        padding: 12px;
    }
}

/* Touch-friendly tap targets */
@media (hover: none) and (pointer: coarse) {
    .game-btn,
    .unit-item {
        min-height: 44px; /* iOS recommended tap target size */
    }

    .tutorial-close {
        width: 44px;
        height: 44px;
        font-size: 1.3rem;
    }
}

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

.modal-content {
    background: linear-gradient(150deg, rgba(37, 43, 32, 0.98), rgba(27, 32, 23, 0.96));
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 32px;
    width: min(500px, calc(100vw - 40px));
    box-shadow: var(--shadow);
}

.modal-content h2 {
    margin-top: 0;
    color: var(--text);
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    color: var(--text);
    font-weight: 500;
}

.form-group select {
    width: 100%;
    padding: 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.3);
    color: var(--text);
    font-size: 1rem;
    cursor: pointer;
}

.form-group select option {
    background: #2b2b2b;
    color: #e8e8e8;
    padding: 8px;
}

.form-group select option:hover,
.form-group select option:checked {
    background: #3a4a2f;
    color: #ffffff;
}

.radio-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.radio-group label {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    padding: 8px;
    border-radius: 6px;
    transition: background 0.2s;
}

.radio-group label:hover {
    background: rgba(129, 182, 76, 0.1);
}

.radio-group input[type="radio"] {
    cursor: pointer;
}

.notice-text {
    margin-top: 8px;
    padding: 8px 12px;
    background: rgba(129, 182, 76, 0.1);
    border-left: 3px solid var(--accent);
    border-radius: 4px;
    font-size: 0.9rem;
    color: var(--text-muted);
}

.modal-actions {
    display: flex;
    gap: 12px;
    margin-top: 24px;
}

.modal-actions button {
    flex: 1;
}

/* Clock Panel */
.clock-panel {
    display: flex;
    justify-content: center;
    gap: 32px;
    margin: 16px 0;
    padding: 12px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
}

.clock-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.clock-label {
    font-size: 0.85rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.clock-display {
    font-family: 'IBM Plex Mono', 'Courier New', monospace;
    font-size: 1.6rem;
    font-weight: 700;
    color: var(--text);
    min-width: 80px;
    text-align: center;
    padding: 8px 12px;
    border-radius: 6px;
    background: rgba(0, 0, 0, 0.3);
    border: 2px solid transparent;
    transition: all 0.3s ease;
}

.clock-active {
    border-color: rgba(129, 182, 76, 0.5);
    background: rgba(129, 182, 76, 0.08);
    box-shadow: 0 0 12px rgba(129, 182, 76, 0.2);
}

.clock-warning {
    color: #f94949 !important;
    animation: clockPulse 1s ease-in-out infinite;
}

@keyframes clockPulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.6; }
}

/* Mobile adjustments for clock */
@media (max-width: 768px) {
    .clock-panel {
        gap: 16px;
        padding: 8px;
    }

    .clock-display {
        font-size: 1.3rem;
        min-width: 70px;
        padding: 6px 10px;
    }
}

/* Profile Screen */
.profile-container {
    width: min(800px, calc(100vw - 30px));
    max-height: 90vh;
    overflow-y: auto;
    border-radius: 16px;
    border: 1px solid var(--border);
    background: linear-gradient(150deg, rgba(37, 43, 32, 0.98), rgba(27, 32, 23, 0.96));
    color: var(--text);
    box-shadow: var(--shadow);
    padding: 32px;
}

.profile-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--border);
}

.profile-header h2 {
    margin: 0;
}

.profile-user-info {
    display: flex;
    align-items: center;
    gap: 20px;
    margin-bottom: 32px;
    padding: 20px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 12px;
}

.profile-avatar {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    overflow: hidden;
    background: rgba(129, 182, 76, 0.2);
}

.profile-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

#profile-avatar-placeholder {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2.5rem;
}

.profile-details h3 {
    margin: 0 0 8px 0;
    color: var(--text);
}

.profile-email {
    margin: 0;
    color: var(--text-muted);
    font-size: 0.9rem;
}

.profile-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 16px;
    margin-bottom: 32px;
}

.stat-card {
    text-align: center;
    padding: 20px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 12px;
    border: 1px solid var(--border);
    transition: transform 0.2s;
}

.stat-card:hover {
    transform: translateY(-2px);
    border-color: rgba(129, 182, 76, 0.5);
}

.stat-value {
    font-size: 2rem;
    font-weight: 700;
    color: var(--accent);
    margin-bottom: 8px;
}

.stat-label {
    font-size: 0.85rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.profile-history h3 {
    margin-bottom: 16px;
}

.history-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 16px;
}

.history-item {
    display: grid;
    grid-template-columns: 40px 1fr auto auto;
    gap: 16px;
    align-items: center;
    padding: 12px 16px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    border: 1px solid var(--border);
    transition: background 0.2s;
}

.history-item:hover {
    background: rgba(0, 0, 0, 0.3);
}

.history-result {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    font-weight: 700;
    font-size: 0.9rem;
}

.history-result.win {
    background: rgba(129, 182, 76, 0.2);
    color: var(--accent);
    border: 1px solid var(--accent);
}

.history-result.loss {
    background: rgba(217, 101, 93, 0.2);
    color: #d9655d;
    border: 1px solid #d9655d;
}

.history-opponent {
    font-weight: 500;
    color: var(--text);
}

.history-date {
    color: var(--text-muted);
    font-size: 0.9rem;
}

.history-time {
    color: var(--text-muted);
    font-size: 0.85rem;
    padding: 4px 8px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 4px;
}

.no-history {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-muted);
}

.pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 16px;
    margin-top: 16px;
}

#profile-page-info {
    color: var(--text-muted);
    font-size: 0.9rem;
}

/* Mobile Responsive for Profile */
@media (max-width: 768px) {
    .profile-container {
        padding: 20px;
    }

    .profile-stats {
        grid-template-columns: repeat(2, 1fr);
    }

    .history-item {
        grid-template-columns: 40px 1fr;
        gap: 8px;
    }

    .history-date,
    .history-time {
        grid-column: 2;
        font-size: 0.8rem;
    }
}
