html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    background-color: black;
}

@media (orientation: landscape) {
    html,
    body {
        width: 100vw;
        height: 100vh;
        overflow: hidden;
    }
}

/* Disable all text selection and touch callouts */
html, body, body * {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    -webkit-text-size-adjust: none;
}

.game, .game_container, #game-container {
    display: block;
    touch-action: none;
    margin: auto;
    width: 100%;
    height: 100%;
}

@media (orientation: landscape) {
    .game,
    .game_container,
    #game-container {
        width: 100vw;
        height: 100vh;
        overflow: hidden;
    }
}

canvas {
    width: 100% !important;
    height: 100% !important;
    object-fit: cover;
}

* {
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: #1a1a2e;
    margin-top: 0.5px;
}

/* Side Panel Containers */
#left-panel-container {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 200px;
    overflow: visible;
    z-index: 100;
    pointer-events: none;
}

#right-panel-container {
    position: absolute;
    top: 0;
    right: 0;
    height: 100%;
    width: 200px;
    overflow: visible;
    z-index: 100;
    pointer-events: none;
}

@media (orientation: landscape) {
    #left-panel-container,
    #right-panel-container {
        height: 100vh;
    }
}

/* UI Overlay */
#ui-overlay {
    position: absolute;
    top: 0;
    top: calc(0px + env(safe-area-inset-top, 0px));
    left: 0;
    transform: translateX(-100%);
    padding: 10px;
    color: white;
    z-index: 100;
    background: rgba(0, 0, 0, 0.8);
    border-radius: 0 15px 15px 0;
    max-height: 400px;
    pointer-events: auto; /* Panel receives events */
    touch-action: none; /* Suppress default touch events like scroll, pinch */
}

/* Transition only when panel has been interacted with */
#ui-overlay.enable-transition {
    transition: transform 0.3s ease;
}

#ui-overlay.open {
    transform: translateX(0);
    padding-left: calc(10px + env(safe-area-inset-left, 10px))
}

/* Center left panel vertically on mobile portrait */
@media (hover: none) and (pointer: coarse) and (orientation: portrait) {
    #ui-overlay {
        top: 50%;
        transform: translateX(-100%) translateY(-50%);
    }

    #ui-overlay.open {
        transform: translateX(0) translateY(-50%);
    }
}

/* Center left panel vertically on mobile (all orientations) */
@media (hover: none) and (pointer: coarse) {
    #ui-overlay {
        top: 50%;
        transform: translateX(-100%) translateY(-50%);
    }

    #ui-overlay.open {
        transform: translateX(0) translateY(-50%);
    }
}

/* Toggle button and buttons are interactive */
#ui-panel-toggle,
#ui-overlay button {
    pointer-events: auto;
    touch-action: manipulation;
    cursor: pointer;
}

#ui-panel-toggle {
    position: absolute;
    right: calc(-24px - env(safe-area-inset-left, 0px));
    top: 20px;
    width: calc(24px + env(safe-area-inset-left, 0px));
    height: 60px;
    background: rgba(0, 0, 0, 0.8);
    border: none;
    border-radius: 0 8px 8px 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding-left: env(safe-area-inset-left, 0px);
    color: white;
    font-size: 12px;
    transition: background 0.3s;
}

/* When panel is open, use normal size (no inset compensation needed) */
#ui-overlay.open #ui-panel-toggle {
    right: -24px;
    width: 24px;
    padding-left: 0;
}

#ui-panel-toggle:hover {
    background: rgba(0, 0, 0, 1);
}

/* Center drawer handle vertically on mobile */
@media (hover: none) and (pointer: coarse) {
    #ui-panel-toggle {
        top: 50%;
        transform: translateY(-50%);
    }
}

#ui-panel-toggle::after {
    content: '▶';
}

#ui-overlay.open #ui-panel-toggle::after {
    content: '◀';
}

#game-info-popup {
    min-width: 200px;
}

#current-player {
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 10px;
}

.player1-turn {
    color: #ff6b6b;
}

.player2-turn {
    color: #4ecdc4;
}

#fences-info {
    font-size: 16px;
}

#fences-info span {
    margin-right: 20px;
}

#player1-fences {
    color: #ff6b6b;
}

#player2-fences {
    color: #4ecdc4;
}

/* Controls Section */
#controls {
    background: rgba(0, 0, 0, 0.7);
    padding: 15px 25px;
    border-radius: 10px;
    margin-bottom: 10px;
}

#controls h3 {
    margin-bottom: 10px;
}

#controls p {
    font-size: 14px;
    line-height: 1.6;
}

/* Mode Toggle */
#mode-toggle {
    background: rgba(0, 0, 0, 0.7);
    padding: 15px 25px;
    border-radius: 10px;
}

#mode-btn {
    background: #6c5ce7;
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 16px;
    transition: background 0.3s;
}

#mode-btn:hover {
    background: #5b4cdb;
}

/* AI Toggle */
#ai-toggle {
    background: #e17055;
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 16px;
    margin-left: 10px;
    transition: background 0.3s;
}

#ai-toggle:hover {
    background: #d35400;
}

#ai-toggle.active {
    background: #27ae60;
}

#ai-toggle.active:hover {
    background: #219a52;
}

/* Rotate Button */
#rotate-btn {
    background: #00b894;
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 16px;
    margin-left: 10px;
    transition: background 0.3s;
}

#rotate-btn:hover {
    background: #00a884;
}

/* Winner Modal */
#winner-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 200;
    justify-content: center;
    align-items: center;
}

#winner-content {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 50px;
    border-radius: 20px;
    text-align: center;
    color: white;
    position: relative;
}

#winner-content h1 {
    font-size: 48px;
    margin-bottom: 20px;
}

.modal-close-btn {
    position: absolute;
    top: 10px;
    right: 10px;
    background: rgba(255, 255, 255, 0.2);
    color: white;
    border: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 20px;
    line-height: 1;
    transition: background 0.3s;
}

.modal-close-btn:hover {
    background: rgba(255, 255, 255, 0.4);
}

#restart-btn {
    background: white;
    color: #667eea;
    border: none;
    padding: 15px 40px;
    border-radius: 10px;
    cursor: pointer;
    font-size: 20px;
    font-weight: bold;
    transition: transform 0.3s;
}

#restart-btn:hover {
    transform: scale(1.1);
}

/* Fence Panel Styles */
#fence-panel {
    position: absolute;
    top: 0;
    top: calc(0px + env(safe-area-inset-top, 0px));
    right: 0;
    transform: translateX(100%);
    background: rgba(0, 0, 0, 0.8);
    padding: 10px;
    padding-right: calc(20px + env(safe-area-inset-right, 0px));
    border-radius: 15px 0 0 15px;
    z-index: 100;
    color: white;
    max-height: 400px;
    pointer-events: auto; /* Panel receives events */
    touch-action: none; /* Suppress default touch events like scroll, pinch */
}

/* Transition only when panel has been interacted with (has transitioning class) */
#fence-panel.enable-transition {
    transition: transform 0.3s ease;
}

#fence-panel.open {
    transform: translateX(0);
}

/* Toggle button and fences are interactive */
#fence-panel-toggle {
    pointer-events: auto;
    cursor: pointer;
    touch-action: manipulation;
}

#fence-panel .draggable-fence:not(.disabled) {
    pointer-events: auto;
    touch-action: none; /* Allow drag */
}

#fence-panel h3 {
    text-align: center;
    margin-bottom: 10px;
    font-size: 18px;
}

.fence-section {
    text-align: center;
}

/* Default order for fence panel elements */
#player1-section {
    order: 0;
}

.fence-panel-separator {
    border: none;
    border-top: 1px solid rgba(255, 255, 255, 0.3);
    margin: 5px 0;
    padding-top: 5px;
    width: 100%;
    order: 1;
}

#player2-section {
    order: 2;
}


.fence-section h4 {
    margin-bottom: 5px;
    font-size: 14px;
    font-weight: normal;
}

.fence-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    padding: 5px;
}

.draggable-fence {
    cursor: grab;
    transition: transform 0.2s, box-shadow 0.2s;
    border-radius: 4px;
    position: relative;
}

/* Larger touch target for mobile */
.draggable-fence::before {
    content: '';
    position: absolute;
    top: -5px;
    left: -15px;
    right: -15px;
    bottom: -15px;
}

.draggable-fence:hover {
    transform: scale(1.1);
    box-shadow: 0 0 15px rgba(255, 255, 255, 0.5);
}

.draggable-fence:active {
    cursor: grabbing;
    transform: scale(1.15);
}

.draggable-fence.disabled {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
}

.fence-horizontal {
    width: 80px;
    height: 20px;
}

.fence-vertical {
    width: 20px;
    height: 80px;
}

.player1-fence {
    background: linear-gradient(135deg, #c9a66b 0%, #b8956a 100%);
    border: 2px solid #d4b87a;
}

.player2-fence {
    background: linear-gradient(135deg, #c9a66b 0%, #b8956a 100%);
    border: 2px solid #d4b87a;
}

.fence-count {
    font-size: 12px;
    color: white;
    margin: 8px 0;
    font-weight: bold;
}

/* Drag Preview */
#drag-preview {
    position: fixed;
    pointer-events: none;
    z-index: 1000;
    opacity: 0.8;
    display: none;
    border-radius: 4px;
}


/* Collapsible Header */
.collapsible-header {
    cursor: pointer;
    font-weight: bold;
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
    user-select: none;
}

.collapsible-icon {
    font-size: 16px;
    transition: transform 0.2s;
}

.collapsible-header:hover .collapsible-icon {
    transform: scale(1.2);
}

/* Top bar with game-info and controls button */
.top-bar {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 5px;
}

#game-info-btn, #controls-btn, #ai-btn, #rules-btn, #switch-btn, #train-btn, #restart-game-btn, #view-btn {
    background: rgba(0, 0, 0, 0.7);
    color: white;
    border: none;
    padding: 15px 20px;
    border-radius: 10px;
    cursor: pointer;
    font-size: 16px;
    transition: background 0.3s;
    width: 140px;
    text-align: left;
}

#game-info-btn:hover, #controls-btn:hover, #ai-btn:hover, #rules-btn:hover, #switch-btn:hover, #train-btn:hover, #restart-game-btn:hover, #view-btn:hover {
    background: rgba(0, 0, 0, 0.9);
}

#game-info-btn.active, #controls-btn.active {
    background: #6c5ce7;
}

#game-info-btn.active:hover, #controls-btn.active:hover {
    background: #5b4cdb;
}

#ai-btn.active {
    background: #27ae60;
}

#ai-btn.active:hover {
    background: #219a52;
}

#switch-btn.disabled,
#switch-btn:disabled {
    opacity: 0.25;
    cursor: not-allowed;
    pointer-events: none;
}

#train-btn.active {
    background: #f39c12;
}

#train-btn.active:hover {
    background: #d68910;
}

#view-btn.active {
    background: #3498db;
}

#view-btn.active:hover {
    background: #2980b9;
}

/* Button Wrapper */
.button-wrapper {
    position: relative;
}

/* Game Info and Controls Popup */
#game-info-popup, #controls-popup {
    display: none;
    position: absolute;
    top: 0;
    left: 100%;
    margin-left: 10px;
    background: rgba(0, 0, 0, 0.9);
    padding: 15px 25px;
    border-radius: 10px;
    border: 2px solid #6c5ce7;
    z-index: 150;
    color: white;
    white-space: nowrap;
}

#game-info-popup.visible, #controls-popup.visible {
    display: block;
}

#controls-popup h3 {
    margin-bottom: 10px;
}

#controls-popup p {
    font-size: 14px;
    line-height: 1.6;
}

/* Fence Panel Toggle */
#fence-panel-toggle {
    position: absolute;
    left: -24px;
    top: 20px;
    width: 24px;
    height: 60px;
    background: rgba(0, 0, 0, 0.8);
    border: none;
    border-radius: 8px 0 0 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 12px;
    transition: background 0.3s;
}

#fence-panel-toggle:hover {
    background: rgba(0, 0, 0, 1);
}

#fence-panel-toggle::after {
    content: '◀';
}

#fence-panel.open #fence-panel-toggle::after {
    content: '▶';
}

/* Center right panel vertically on mobile (all orientations) */
@media (hover: none) and (pointer: coarse) {
    #fence-panel {
        top: 50%;
        transform: translateX(100%) translateY(-50%);
    }

    #fence-panel.open {
        transform: translateX(0) translateY(-50%);
    }

    /* Center drawer handle vertically */
    #fence-panel-toggle {
        top: 50%;
        transform: translateY(-50%);
    }
}

/* Confirm Restart Modal */
#confirm-restart-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 250;
    justify-content: center;
    align-items: center;
}

#confirm-restart-modal.visible {
    display: flex;
}

#confirm-restart-content {
    background: linear-gradient(135deg, #2d3436 0%, #1a1a2e 100%);
    padding: 40px;
    border-radius: 20px;
    text-align: center;
    color: white;
    border: 2px solid #6c5ce7;
}

#confirm-restart-content h2 {
    font-size: 28px;
    margin-bottom: 15px;
}

#confirm-restart-content p {
    font-size: 16px;
    margin-bottom: 25px;
    color: #aaa;
}

.confirm-buttons {
    display: flex;
    gap: 15px;
    justify-content: center;
}

#confirm-restart-yes {
    background: #e74c3c;
    color: white;
    border: none;
    padding: 12px 30px;
    border-radius: 10px;
    cursor: pointer;
    font-size: 16px;
    font-weight: bold;
    transition: background 0.3s, transform 0.2s;
}

#confirm-restart-yes:hover {
    background: #c0392b;
    transform: scale(1.05);
}

#confirm-restart-no {
    background: #636e72;
    color: white;
    border: none;
    padding: 12px 30px;
    border-radius: 10px;
    cursor: pointer;
    font-size: 16px;
    font-weight: bold;
    transition: background 0.3s, transform 0.2s;
}

#confirm-restart-no:hover {
    background: #4a5568;
    transform: scale(1.05);
}

/* Introduction Modal */
#intro-modal {
    display: flex;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    z-index: 300;
    justify-content: center;
    align-items: center;
    pointer-events: auto;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
}

#intro-modal.hidden {
    display: none;
    pointer-events: none;
}

#intro-content {
    background: linear-gradient(135deg, #1a1a2e 0%, #2d3436 100%);
    padding: 40px;
    border-radius: 20px;
    text-align: center;
    color: white;
    border: 2px solid #6c5ce7;
    max-width: 400px;
    margin: 20px;
    pointer-events: auto;
    touch-action: none;
}

@media (orientation: landscape) {
    #intro-content {
        max-width: 600px;
        padding: 20px;
    }
}

#intro-content h1 {
    font-size: 36px;
    margin-bottom: 20px;
    margin-top: -10px;
    color: #6c5ce7;
}

.intro-rules {
    text-align: left;
    margin-bottom: 25px;
}

.intro-rules p {
    font-size: 14px;
    margin-bottom: 10px;
    line-height: 1.4;
}

.intro-rules hr {
    border: none;
    border-top: 1px solid #444;
    margin: 15px 0;
}

#intro-start-btn {
    background: linear-gradient(135deg, #6c5ce7 0%, #a29bfe 100%);
    color: white;
    border: none;
    padding: 10px 40px;
    border-radius: 10px;
    cursor: pointer;
    font-size: 18px;
    font-weight: bold;
    transition: transform 0.2s, box-shadow 0.2s;
    margin-bottom: -5px;
}

#intro-start-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 5px 20px rgba(108, 92, 231, 0.4);
}

/* Portrait Mode - Move fence panel to bottom (all devices) */
body.portrait-mode #right-panel-container {
    top: auto;
    bottom: 0;
    right: 50%;
    transform: translateX(50%);
    width: auto;
    height: auto;
}

body.portrait-mode #fence-panel {
    position: relative;
    top: auto;
    right: auto;
    bottom: 0;
    transform: translateY(100%);
    border-radius: 12px 12px 0 0;
    padding: 3px 10px;
    padding-bottom: calc(3px + env(safe-area-inset-bottom, 0px));
    max-height: none;
    display: flex;
    flex-direction: row;
    gap: 10px;
    align-items: center;
}

body.portrait-mode #fence-panel.open {
    transform: translateY(0);
}

body.portrait-mode #fence-panel-toggle {
    position: absolute;
    top: -24px;
    left: 50%;
    transform: translateX(-50%);
    right: auto;
    width: 60px;
    height: 24px;
    border-radius: 8px 8px 0 0;
    font-size: 12px;
}

body.portrait-mode #fence-panel-toggle::after {
    content: '▲';
}

body.portrait-mode #fence-panel.open #fence-panel-toggle::after {
    content: '▼';
}

body.portrait-mode .fence-section {
    margin-bottom: 0;
}

body.portrait-mode .fence-container {
    flex-direction: row;
    gap: 10px;
}

/* Hide separator in portrait mode - players are side by side */
body.portrait-mode .fence-panel-separator {
    display: none;
}

/* Top Fence Panel - only show on mobile portrait (for Player 2 separate panel) */
/* On mobile portrait: show top panel and hide player 2 in bottom panel */
@media (hover: none) and (pointer: coarse) and (orientation: portrait) {
    body.portrait-mode #top-fence-panel-container {
        display: block;
    }

    body.portrait-mode #player2-section {
        display: none;
    }

    /* Only rotate upside-down when AI is OFF (two-player-mode) */
    body.portrait-mode.two-player-mode #top-player2-section {
        transform: rotate(180deg);
    }
}

/* Mobile portrait: wider panels with label on the left */
@media (hover: none) and (pointer: coarse) and (orientation: portrait) {
    body.mobile-portrait #fence-panel,
    body.mobile-portrait #top-fence-panel {
        flex-direction: row;
        align-items: center;
        gap: 10px;
        padding: 3px 10px;
    }

    body.mobile-portrait .fence-section,
    body.mobile-portrait #top-player2-section {
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: 8px;
        text-align: left;
    }

    body.mobile-portrait .fence-section h4,
    body.mobile-portrait #top-player2-section h4,
    body.mobile-portrait .fence-section > div:first-child,
    body.mobile-portrait #top-player2-section > h4 {
        margin-bottom: 0;
        white-space: nowrap;
        min-width: 50px;
        font-size: 13px;
    }

    body.mobile-portrait .fence-container,
    body.mobile-portrait #top-player2-section .fence-container {
        flex-direction: row;
        gap: 6px;
        align-items: center;
        padding: 2px;
    }

    /* Smaller fences in top/bottom panels */
    body.mobile-portrait .draggable-fence.fence-horizontal {
        width: 50px;
        height: 12px;
    }

    body.mobile-portrait .draggable-fence.fence-vertical {
        width: 12px;
        height: 50px;
    }

    body.mobile-portrait .fence-count {
        font-size: 10px;
        margin: 0 5px;
    }
}

/* Landscape Mode on Mobile - Swap player positions and rotate player 2 */
@media (hover: none) and (pointer: coarse) {
    body.mobile-landscape #fence-panel {
        display: flex;
        flex-direction: column;
        padding-bottom: 12px;
    }

    /* Swap players: Player2 first, then separator, then Player1 */
    body.mobile-landscape #player1-section {
        order: 2;
    }

    body.mobile-landscape #player2-section {
        order: 0;
        margin-bottom: 5px;
    }

    /* Only rotate player 2 in two-player mode (not AI) */
    body.mobile-landscape.two-player-mode #player2-section {
        transform: rotate(180deg);
    }
}

/* Top Fence Panel for Player 2 in Portrait Top View */
#top-fence-panel-container {
    display: none;
    position: absolute;
    top: 0;
    top: env(safe-area-inset-top, 0px);
    left: 50%;
    transform: translateX(-50%);
    z-index: 100;
}

#top-fence-panel {
    position: relative;
    top: 0;
    transform: translateY(-100%);
    background: rgba(0, 0, 0, 0.8);
    padding: 3px 10px;
    border-radius: 0 0 12px 12px;
    color: white;
    display: flex;
    flex-direction: row;
    gap: 10px;
    align-items: center;
    pointer-events: auto; /* Panel receives events */
    touch-action: none; /* Suppress default touch events like scroll, pinch */
}

/* Transition only when panel has been interacted with */
#top-fence-panel.enable-transition {
    transition: transform 0.3s ease;
}

/* Toggle button and fences are interactive */
#top-fence-panel-toggle {
    pointer-events: auto;
    cursor: pointer;
    touch-action: manipulation;
}

#top-fence-panel .draggable-fence:not(.disabled) {
    pointer-events: auto;
    touch-action: none; /* Allow drag */
}

#top-fence-panel.open {
    transform: translateY(0);
}

#top-fence-panel-toggle {
    position: absolute;
    bottom: -24px;
    left: 50%;
    transform: translateX(-50%);
    width: 60px;
    height: 24px;
    background: rgba(0, 0, 0, 0.8);
    border: none;
    border-radius: 0 0 8px 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 12px;
    transition: background 0.3s;
}

#top-fence-panel-toggle:hover {
    background: rgba(0, 0, 0, 1);
}

#top-fence-panel-toggle::after {
    content: '▼';
}

#top-fence-panel.open #top-fence-panel-toggle::after {
    content: '▲';
}

#top-player2-section {
    margin-bottom: 0;
    text-align: center;
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 10px;
}

#top-player2-section h4 {
    font-weight: normal;
    margin: 0;
}

#top-player2-section .fence-count {
    margin: 0 5px;
}

#top-player2-section .fence-container {
    flex-direction: row;
    gap: 10px;
}

/* AI Thinking Indicator Animation */
@keyframes ai-thinking-pulse {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.15);
    }
    100% {
        transform: scale(1);
    }
}

.ai-thinking-indicator {
    display: inline-block;
    animation: ai-thinking-pulse 0.8s ease-in-out infinite;
}

/* AI thinking state for player name */
#player1-name.ai-thinking,
#player2-name.ai-thinking,
#top-player2-name.ai-thinking {
    display: inline-block;
    animation: ai-thinking-pulse 0.8s ease-in-out infinite;
}



