:root {
    /* Colors */
    --primary: #ffcc00;
    /* Military accent yellow */
    --primary-hover: #eebb00;
    --dark-bg: #1a1a1a;
    --hud-bg: rgba(20, 20, 20, 0.85);
    /* Glassmorphism base */
    --hud-border: rgba(255, 255, 255, 0.1);
    --text-main: #f0f0f0;
    --text-muted: #aaaaaa;
    --hex-border: rgba(255, 255, 255, 0.2);
    --hex-hover: rgba(255, 204, 0, 0.3);
    --hex-selected: rgba(255, 0, 0, 0.4);

    /* Dimensions */
    --board-ratio: 1.43;
    /* Rough standard board aspect ratio, can adjust */

    /* Z-Indices */
    --z-board: 1;
    --z-hud: 10;
    --z-modal: 100;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Roboto', sans-serif;
    background-color: #000;
    /* Black borders for fullscreen feel */
    color: var(--text-main);
    overflow: hidden;
    /* Prevent scrolling, app is fixed viewport */
    height: 100vh;
    width: 100vw;
}

/* App Container */
#app {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    /* Sidebar layout */
    overflow: hidden;
}

/* Board Area */
#board-container {
    flex-grow: 1;
    /* Take remaining space */
    position: relative;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: var(--z-board);
    transition: all 0.5s ease;
    background-color: #000;
}

#board-background {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    /* Preserve aspect ratio, add black bars if needed */
    background-color: #000;
    /* Black background for letterboxing */
    pointer-events: none;
    z-index: 1;
}

#hex-grid {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    /* Match background scaling behavior exactly */
    pointer-events: auto;
    z-index: 10;
}

#hex-grid polygon {
    cursor: pointer;
    pointer-events: all;
    /* Catch all click events */
    fill: transparent;
    stroke: rgba(255, 255, 255, 0.2);
    /* Visible grid lines */
    stroke-width: 1px;
    transition: stroke 0.2s, fill 0.2s;
}

#hex-grid polygon:hover {
    stroke: var(--primary);
    stroke-width: 2px;
    fill: rgba(255, 204, 0, 0.1);
}

/* Sidebar Shared */
aside {
    width: 300px;
    /* Reduced slightly to fit both on standard screens comfortably */
    height: 100%;
    background: var(--hud-bg);
    backdrop-filter: blur(10px);
    z-index: var(--z-hud);
    display: flex;
    flex-direction: column;
    padding: 20px 10px;
    overflow-y: auto;
    flex-shrink: 0;
}

#map-sidebar {
    border-right: 1px solid var(--hud-border);
}

#tool-sidebar {
    border-left: 1px solid var(--hud-border);
}

.sidebar-header {
    padding-bottom: 20px;
    margin-bottom: 20px;
    border-bottom: 1px solid var(--hud-border);
    text-align: center;
}

.sidebar-header h2 {
    font-family: 'Black Ops One', cursive;
    color: var(--primary);
    text-transform: uppercase;
    font-size: 1.2rem;
}

/* Accordion Groups */
.tool-group {
    background: rgba(0, 0, 0, 0.3);
    margin-bottom: 10px;
    border-radius: 6px;
    border: 1px solid transparent;
    transition: 0.3s;
}

.tool-group.active {
    border-color: var(--primary);
    background: rgba(255, 204, 0, 0.05);
}

.group-label {
    padding: 12px 15px;
    font-size: 0.9rem;
    color: var(--text-main);
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: bold;
    text-transform: uppercase;
}

.group-label:hover {
    color: var(--primary);
}

.group-label .arrow {
    font-size: 0.7rem;
    transition: transform 0.3s;
}

.tool-group.active .arrow {
    transform: rotate(180deg);
}

.group-items {
    display: none;
    /* Hidden by default */
    flex-wrap: wrap;
    gap: 8px;
    padding: 15px;
    border-top: 1px solid rgba(255, 255, 255, 0.05);
    animation: slideDown 0.3s ease-out;
}

.tool-group.active .group-items {
    display: flex;
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Palette Overrides */
#terrain-palette {
    display: flex;
    flex-direction: column;
    gap: 0;
    /* Handled by margins */
}

/* Sidebar Sections */
.panel-section {
    margin-bottom: 20px;
}

.map-controls label {
    display: block;
    font-size: 0.8rem;
    color: #aaa;
    margin-bottom: 5px;
}

#bg-selector {
    width: 100%;
    padding: 8px;
    background: #222;
    color: white;
    border: 1px solid #444;
    border-radius: 4px;
    margin-bottom: 10px;
    cursor: pointer;
}

.action-buttons {
    display: flex;
    gap: 10px;
}

.action-buttons button,
.sys-btn {
    flex: 1;
    padding: 8px;
    background: #333;
    color: white;
    border: 1px solid #555;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: 0.2s;
}

.action-buttons button:hover,
.sys-btn:hover {
    background: #444;
    border-color: var(--primary);
}

.action-buttons button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.system-controls {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* Calibration in Sidebar */
#calibration-panel {
    /* Visible state styling */
    background: rgba(0, 0, 0, 0.3);
    padding: 15px;
    border-radius: 8px;
    border: 1px solid var(--hud-border);
    color: white;
    width: 100%;
    margin-top: 10px;
    pointer-events: auto;
}

.hidden {
    display: none !important;
}

.cal-control {
    margin-bottom: 8px;
}

.cal-control label {
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    color: #ccc;
}

#cal-close {
    background: #e74c3c;
    color: white;
    border: none;
    border-radius: 3px;
    cursor: pointer;
}

/* Palette Overrides */
#terrain-palette {
    display: flex;
    flex-direction: column;
    gap: 0;
    flex-grow: 1;
    /* Allow palette to take available space */
    overflow-y: auto;
    /* Scroll if needed */
}

/* Rotation Controls in Sidebar */
.rotation-controls {
    margin-bottom: 20px;
    padding: 15px;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 8px;
    border: 1px solid var(--hud-border);
}

/* HUD Positioning Updates */
#hud-layer {
    /* Only contain Top Bar now */
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 50;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

#top-bar {
    pointer-events: auto;
}

/* Fullscreen Mode */
body.fullscreen #tool-sidebar,
body.fullscreen #map-sidebar {
    display: none;
    /* Hide interactive sidebars */
}

body.fullscreen #hud-layer {
    opacity: 0;
    pointer-events: none;
}

/* Hide hex grid lines in fullscreen, but keep placed tiles */
body.fullscreen #hex-grid polygon {
    stroke: none;
    fill: none;
    pointer-events: none;
}

/* Modal Dialog Styles */
dialog {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: var(--hud-bg);
    backdrop-filter: blur(10px);
    border: 2px solid var(--hud-border);
    border-radius: 12px;
    padding: 30px;
    min-width: 400px;
    max-width: 600px;
    color: var(--text-main);
    z-index: var(--z-modal);
    box-shadow: 0 10px 50px rgba(0, 0, 0, 0.8);
    pointer-events: auto;
    /* Ensure dialog receives events */
}

dialog * {
    pointer-events: auto;
    /* Ensure all children can be interacted with */
}

dialog::backdrop {
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(5px);
}

dialog h2 {
    color: var(--primary);
    margin-bottom: 20px;
    font-family: 'Black Ops One', cursive;
}

dialog ul {
    list-style: none;
    max-height: 300px;
    overflow-y: auto;
    margin-bottom: 20px;
}

dialog ul li {
    padding: 12px;
    margin-bottom: 8px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 6px;
    cursor: pointer;
    transition: 0.2s;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}

dialog ul li:hover {
    background: rgba(255, 204, 0, 0.2);
    border-left: 3px solid var(--primary);
}

.delete-map-btn {
    padding: 4px 8px !important;
    margin: 0 !important;
    background: #d32f2f !important;
    color: white !important;
    font-size: 16px;
    min-width: 36px;
    flex-shrink: 0;
}

.delete-map-btn:hover {
    background: #f44336 !important;
    transform: scale(1.1) !important;
}

dialog input[type="text"] {
    width: 100%;
    padding: 10px;
    background: rgba(0, 0, 0, 0.5);
    border: 1px solid var(--hud-border);
    border-radius: 6px;
    color: var(--text-main);
    font-size: 1rem;
    margin-bottom: 10px;
}

dialog button {
    padding: 10px 20px;
    background: var(--primary);
    color: #000;
    border: none;
    border-radius: 6px;
    font-weight: bold;
    cursor: pointer;
    transition: 0.2s;
    margin-right: 10px;
}

dialog button:hover {
    background: var(--primary-hover);
    transform: translateY(-2px);
}

dialog button.close-btn {
    background: #666;
    color: white;
}

dialog button.close-btn:hover {
    background: #777;
}

.modal-actions {
    margin-top: 20px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

/* Terrain Btn Images */
.terrain-btn img {
    max-width: 80%;
    max-height: 80%;
    pointer-events: none;
}