:root {
    --bg-primary: #0a0a0f;
    --bg-secondary: #111118;
    --bg-tertiary: #1a1a24;
    --bg-panel: rgba(17, 17, 24, 0.95);
    
    --accent-cyan: #00ffff;
    --accent-magenta: #ff00ff;
    --accent-green: #00ff88;
    --accent-orange: #ffaa00;
    --accent-red: #ff4444;
    --accent-blue: #8888ff;
    --accent-yellow: #ffff00;
    
    --text-primary: #ffffff;
    --text-secondary: #a0a0b0;
    --text-muted: #606070;
    
    --border-color: #2a2a3a;
    --grid-line: rgba(0, 255, 255, 0.1);
    
    --font-mono: 'Courier New', monospace;
    --font-sans: system-ui, -apple-system, sans-serif;
    
    --tile-size: 24px;
    --tile-size-min: 8px;
    --tile-size-max: 48px;
}

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

body {
    font-family: var(--font-mono);
    background: var(--bg-primary);
    color: var(--text-primary);
    overflow: hidden;
    height: 100vh;
    width: 100vw;
}

/* Main Layout */
#app {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    position: relative;
}

/* Header */
#header {
    height: 48px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    z-index: 100;
}

.logo {
    display: flex;
    align-items: center;
    gap: 10px;
}

.logo-icon {
    font-size: 24px;
    color: var(--accent-cyan);
    text-shadow: 0 0 10px var(--accent-cyan);
}

.logo-text {
    font-size: 18px;
    font-weight: bold;
    letter-spacing: 3px;
    background: linear-gradient(90deg, var(--accent-cyan), var(--accent-magenta));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

#connection-status {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    border-radius: 4px;
    font-size: 12px;
    letter-spacing: 1px;
}

#connection-status.connected {
    background: rgba(0, 255, 136, 0.1);
    border: 1px solid var(--accent-green);
    color: var(--accent-green);
}

#connection-status.disconnected {
    background: rgba(255, 68, 68, 0.1);
    border: 1px solid var(--accent-red);
    color: var(--accent-red);
}

#connection-status.connecting {
    background: rgba(255, 170, 0, 0.1);
    border: 1px solid var(--accent-orange);
    color: var(--accent-orange);
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    animation: pulse 2s infinite;
}

.connected .status-dot {
    background: var(--accent-green);
    box-shadow: 0 0 8px var(--accent-green);
}

.disconnected .status-dot {
    background: var(--accent-red);
}

.connecting .status-dot {
    background: var(--accent-orange);
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.tick-display {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
}

.tick-display .label {
    color: var(--text-muted);
    font-size: 11px;
    letter-spacing: 1px;
}

#tick-count {
    color: var(--accent-cyan);
    font-weight: bold;
    min-width: 60px;
    text-align: right;
}

.agent-display {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
}

.agent-display .label {
    color: var(--text-muted);
    font-size: 11px;
    letter-spacing: 1px;
}

#agent-count {
    color: var(--accent-magenta);
    font-weight: bold;
    min-width: 30px;
    text-align: right;
}

/* Game Container */
#game-container {
    flex: 1;
    position: relative;
    overflow: hidden;
    background: var(--bg-primary);
    cursor: crosshair;
    touch-action: none; /* custom pan/zoom on touch devices */
}

#game-canvas {
    position: absolute;
    top: 0;
    left: 0;
    transform: none;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

#grid-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    background-image: 
        linear-gradient(var(--grid-line) 1px, transparent 1px),
        linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
    background-size: var(--tile-size) var(--tile-size);
    opacity: 0.5;
}

/* Selection Box */
#selection-box {
    position: absolute;
    pointer-events: none;
    border: 2px solid var(--accent-cyan);
    box-shadow: 
        0 0 10px var(--accent-cyan),
        inset 0 0 10px rgba(0, 255, 255, 0.2);
    z-index: 50;
}

#selection-box.hidden {
    display: none;
}

.selection-border {
    position: absolute;
    top: -4px;
    left: -4px;
    right: -4px;
    bottom: -4px;
    border: 1px dashed var(--accent-cyan);
    animation: rotate-border 4s linear infinite;
}

@keyframes rotate-border {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* HUD Panels */
#hud {
    position: absolute;
    top: 48px;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    z-index: 90;
}

.hud-panel {
    position: absolute;
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    padding: 12px;
    pointer-events: auto;
    backdrop-filter: blur(4px);
}

.hud-panel h3 {
    font-size: 11px;
    letter-spacing: 2px;
    color: var(--text-muted);
    margin-bottom: 10px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 6px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.hud-panel h4 {
    font-size: 10px;
    letter-spacing: 1px;
    color: var(--text-muted);
    margin-bottom: 8px;
}

.hud-panel.hidden {
    display: none;
}

/* Resource Panel */
#resource-panel {
    top: 20px;
    right: 20px;
    min-width: 180px;
}

.resource-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.resource {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
}

.resource-icon {
    font-size: 10px;
}

.resource-name {
    color: var(--text-secondary);
    flex: 1;
}

.resource-value {
    font-weight: bold;
    color: var(--text-primary);
    font-family: var(--font-mono);
}

/* Unit Info Panel */
#unit-info {
    bottom: 100px;
    left: 20px;
    min-width: 220px;
}

#tile-info {
    bottom: 100px;
    left: 20px;
    min-width: 180px;
}

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

.unit-type {
    font-size: 14px;
    font-weight: bold;
    color: var(--accent-cyan);
    letter-spacing: 1px;
}

.unit-id {
    font-size: 10px;
    color: var(--text-muted);
    font-family: var(--font-mono);
}

.unit-owner, .unit-position, .tile-position, .tile-type, .unit-status, .tile-owner {
    display: flex;
    justify-content: space-between;
    font-size: 11px;
    margin-bottom: 6px;
}

.label {
    color: var(--text-muted);
}

.value {
    color: var(--text-primary);
    font-family: var(--font-mono);
}

.status-idle { color: var(--accent-green); }
.status-moving { color: var(--accent-cyan); }
.status-working { color: var(--accent-orange); }
.status-depleted { color: var(--accent-red); }

.stat-row {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin: 10px 0;
    padding: 8px;
    background: var(--bg-tertiary);
    border-radius: 3px;
}

.stat {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 10px;
}

.stat-label {
    width: 20px;
    color: var(--text-muted);
}

.bar {
    flex: 1;
    height: 6px;
    background: var(--bg-primary);
    border-radius: 3px;
    overflow: hidden;
}

.bar-fill {
    height: 100%;
    border-radius: 3px;
    transition: width 0.3s ease;
}

.bar-fill.hp { background: linear-gradient(90deg, #ff4444, #ff8888); }
.bar-fill.energy { background: linear-gradient(90deg, #ffaa00, #ffcc00); }

.stat-value {
    width: 50px;
    text-align: right;
    color: var(--text-secondary);
    font-family: var(--font-mono);
}

.unit-inventory, .tile-deposit {
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid var(--border-color);
    font-size: 11px;
}

.tile-deposit.hidden, .tile-owner.hidden {
    display: none;
}

/* Minimap */
#minimap-panel {
    bottom: 20px;
    right: 20px;
}

#minimap-canvas {
    border: 1px solid var(--border-color);
    border-radius: 2px;
    background: var(--bg-primary);
    cursor: pointer;
}

.minimap-info {
    margin-top: 8px;
    font-size: 10px;
    color: var(--text-muted);
    text-align: center;
    font-family: var(--font-mono);
}

/* Agent Panel */
#agent-panel {
    top: 20px;
    left: 20px;
    min-width: 150px;
    max-width: 200px;
}

#agent-panel.collapsed .agent-list {
    display: none;
}

#agent-panel h3 {
    cursor: pointer;
    user-select: none;
}

#agent-panel h3:hover {
    color: var(--accent-cyan);
}

.toggle-icon {
    font-size: 10px;
    transition: transform 0.2s;
}

#agent-panel.collapsed .toggle-icon {
    transform: rotate(-90deg);
}

.agent-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
    max-height: 200px;
    overflow-y: auto;
}

.agent-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px;
    border-radius: 3px;
    font-size: 11px;
    cursor: pointer;
    transition: background 0.2s;
}

.agent-item:hover {
    background: var(--bg-tertiary);
}

.agent-item.active {
    background: rgba(0, 255, 255, 0.12);
    border: 1px solid rgba(0, 255, 255, 0.25);
}

.agent-item.loading {
    color: var(--text-muted);
    cursor: default;
}

.agent-color {
    width: 10px;
    height: 10px;
    border-radius: 2px;
    box-shadow: 0 0 4px currentColor;
}

.agent-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Controls / Legend */
#controls {
    bottom: 20px;
    left: 20px;
    min-width: 200px;
}

.legend-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 6px;
    margin-bottom: 10px;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 10px;
    color: var(--text-secondary);
}

.legend-color {
    width: 12px;
    height: 12px;
    border-radius: 2px;
}

.controls-help {
    font-size: 9px;
    color: var(--text-muted);
    text-align: center;
    letter-spacing: 0.5px;
}

/* Error Overlay */
#error-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(10, 10, 15, 0.95);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

#error-overlay.hidden {
    display: none;
}

.error-box {
    background: var(--bg-secondary);
    border: 2px solid var(--accent-red);
    border-radius: 8px;
    padding: 40px;
    text-align: center;
    box-shadow: 0 0 30px rgba(255, 68, 68, 0.3);
}

.error-box h2 {
    color: var(--accent-red);
    font-size: 24px;
    letter-spacing: 3px;
    margin-bottom: 16px;
}

.error-box p {
    color: var(--text-secondary);
    margin-bottom: 24px;
    font-size: 14px;
}

.error-box button {
    background: var(--accent-red);
    color: var(--bg-primary);
    border: none;
    padding: 12px 32px;
    font-size: 14px;
    font-weight: bold;
    letter-spacing: 2px;
    cursor: pointer;
    border-radius: 4px;
    font-family: var(--font-mono);
    transition: all 0.2s;
}

.error-box button:hover {
    background: #ff6666;
    box-shadow: 0 0 15px rgba(255, 68, 68, 0.5);
}

/* Scrollbar Styling */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: var(--bg-primary);
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* Selection animation */
@keyframes selection-pulse {
    0%, 100% { 
        box-shadow: 0 0 5px var(--accent-cyan), inset 0 0 5px rgba(0, 255, 255, 0.2);
    }
    50% { 
        box-shadow: 0 0 15px var(--accent-cyan), inset 0 0 10px rgba(0, 255, 255, 0.4);
    }
}

#selection-box {
    animation: selection-pulse 2s ease-in-out infinite;
}

/* Hover effects */
.hud-panel {
    transition: border-color 0.2s, box-shadow 0.2s;
}

.hud-panel:hover {
    border-color: rgba(0, 255, 255, 0.3);
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}

/* =========================================================
   Mobile / Responsive Layout
   Breakpoints:
   - Desktop: >1024px
   - Tablet: 768px–1024px
   - Mobile: <768px
   ========================================================= */

/* Touch-friendly defaults */
button, [role="button"], .agent-item, #minimap-canvas {
    touch-action: manipulation;
}

.hud-toggle {
    display: none;
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 120;

    min-width: 44px;
    min-height: 44px;
    padding: 10px 12px;

    background: rgba(17, 17, 24, 0.92);
    color: var(--accent-cyan);
    border: 1px solid rgba(0, 255, 255, 0.35);
    border-radius: 10px;
    font-family: var(--font-mono);
    font-size: 12px;
    letter-spacing: 1px;
}

.hud-toggle:active {
    transform: translateY(1px);
}

/* Make canvas fill the viewport reliably */
#game-canvas {
    width: 100%;
    height: 100%;
}

/* Tablet */
@media (max-width: 1024px) {
    #header {
        padding: 0 12px;
        gap: 10px;
    }

    .logo-text {
        font-size: 14px;
        letter-spacing: 2px;
    }

    #resource-panel {
        min-width: 160px;
    }

    #controls {
        min-width: 180px;
    }

    .agent-list {
        max-height: 160px;
    }
}

/* Mobile: stack HUD panels, reduce overlap */
@media (max-width: 768px) {
    body {
        /* Prevent elastic scroll / accidental page panning while swiping the map */
        overscroll-behavior: none;
    }

    #header {
        height: 52px;
        padding: 0 10px;
    }

    #hud {
        top: 52px;
        padding: 10px;
    }

    .hud-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    /* Put HUD panels into a vertical flow inside the overlay */
    #hud {
        display: flex;
        flex-direction: column;
        gap: 10px;
        align-items: stretch;
        pointer-events: none; /* keep game interactions by default */
    }

    .hud-panel {
        position: relative;
        top: auto;
        right: auto;
        bottom: auto;
        left: auto;
        width: 100%;
        pointer-events: auto;
    }

    /* Improve tap targets */
    .agent-item {
        min-height: 44px;
        padding: 10px;
        font-size: 12px;
    }

    #agent-panel h3 {
        min-height: 44px;
    }

    /* Re-order key panels for mobile */
    #resource-panel { order: 1; }
    #agent-panel { order: 2; }
    #tile-info, #unit-info { order: 3; }
    #controls { order: 4; }
    #minimap-panel { order: 5; }

    /* Don’t hog the screen */
    .agent-list {
        max-height: 30vh;
    }

    /* Make selection box align with canvas sizing */
    #selection-box {
        transform: translateZ(0);
    }

    /* Collapsed HUD mode (toggle button remains) */
    body.hud-collapsed #resource-panel,
    body.hud-collapsed #agent-panel,
    body.hud-collapsed #tile-info,
    body.hud-collapsed #unit-info,
    body.hud-collapsed #controls,
    body.hud-collapsed #minimap-panel {
        display: none;
    }
}

/* Very small phones: hide minimap by default */
@media (max-width: 480px) {
    #minimap-panel {
        display: none;
    }
}