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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: #1a1a1a;
    color: #e0e0e0;
    overflow: hidden;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Override Bootstrap dark theme for our custom dark theme */
.bg-dark {
    background-color: #2a2a2a !important;
}

.border-secondary {
    border-color: #3a3a3a !important;
}

.text-light {
    color: #e0e0e0 !important;
}

.text-muted {
    color: #888 !important;
}

.form-control, .form-control-sm {
    background-color: #3a3a3a;
    border-color: #4a4a4a;
    color: #e0e0e0;
}

.form-control:focus, .form-control-sm:focus {
    background-color: #3a3a3a;
    border-color: #4a9ac9; /* Color-blind friendly cyan-blue */
    color: #e0e0e0;
    box-shadow: 0 0 0 0.2rem rgba(74, 154, 201, 0.25);
}

.form-range::-webkit-slider-thumb {
    background-color: #4a9ac9; /* Color-blind friendly cyan-blue */
}

.form-range::-moz-range-thumb {
    background-color: #4a9ac9; /* Color-blind friendly cyan-blue */
}

.btn-outline-light {
    border-color: #4a4a4a;
    color: #e0e0e0;
}

.btn-outline-light:hover {
    background-color: #4a4a4a;
    border-color: #4a9ac9; /* Color-blind friendly cyan-blue */
    color: #fff;
}

.nav-tabs .nav-link {
    color: #888;
    border-color: transparent;
}

.nav-tabs .nav-link:hover {
    border-color: #3a3a3a;
    color: #e0e0e0;
}

.nav-tabs .nav-link.active {
    background-color: #2a2a2a;
    border-color: #3a3a3a #3a3a3a #2a2a2a;
    color: #4a9ac9; /* Color-blind friendly cyan-blue */
}

.main-container {
    flex: 1;
    display: grid;
    grid-template-columns: 3fr 9fr; /* 3:9 ratio */
    overflow: hidden;
    position: relative;
}

.controls {
    z-index: 100;
}

.toolbar {
    background: #2a2a2a;
    border-right: 2px solid #3a3a3a;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    height: 100%;
    min-width: 0; /* Allow flex children to shrink */
}

.toolbar .nav-tabs {
    flex-shrink: 0;
    width: 100%;
}

.toolbar .tab-content {
    flex: 1;
    overflow-y: auto;
    background: #2a2a2a;
    width: 100%;
    min-width: 0;
}

.toolbar .tab-pane {
    width: 100%;
    height: 100%;
}

.toolbar .tab-pane > div {
    width: 100%;
    max-width: 100%;
}

/* Ensure all content inside toolbar uses full width */
.toolbar .form-control,
.toolbar .form-range,
.toolbar .btn,
.toolbar .d-grid {
    width: 100%;
    max-width: 100%;
}

.toolbar .d-flex.gap-2 {
    width: 100%; /* Full width for flex containers with gaps */
}

.toolbar .d-flex.align-items-center {
    width: 100%;
}

.toolbar .form-control-sm {
    width: 100%;
}

/* Ensure nav tabs span full width */
.toolbar .nav-tabs {
    width: 100%;
    display: flex;
}

.toolbar .nav-tabs .nav-item {
    flex: 1;
}

.toolbar .nav-tabs .nav-link {
    width: 100%;
    text-align: center;
}

.toolbar-title {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 20px;
    color: #e0e0e0;
    border-bottom: 1px solid #3a3a3a;
    padding-bottom: 10px;
    grid-column: 1 / -1;
}

.toolbar-section {
    margin-bottom: 20px;
    grid-column: 1 / -1;
}

.toolbar-section.two-column {
    grid-column: span 1;
}


.terrain-rules {
    margin-bottom: 10px;
    max-height: 300px;
    overflow-y: auto;
}

.terrain-rule {
    background: #1a1a1a;
    border: 1px solid #3a3a3a;
    border-radius: 4px;
    padding: 10px;
    margin-bottom: 8px;
}

.terrain-rule-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.terrain-rule-name {
    font-weight: 600;
    color: #e0e0e0;
    font-size: 13px;
}

.terrain-rule-delete {
    padding: 4px 8px;
    font-size: 11px;
}

.terrain-rule-field {
    margin-bottom: 6px;
}

.terrain-rule-field label {
    display: block;
    font-size: 11px;
    color: #aaa;
    margin-bottom: 3px;
}

.terrain-rule-field input[type="color"] {
    height: 30px;
    padding: 2px;
    cursor: pointer;
}

.terrain-ramp-container {
    margin-bottom: 15px;
    padding: 10px;
    background: #1a1a1a;
    border: 1px solid #3a3a3a;
    border-radius: 4px;
}

.terrain-ramp {
    position: relative;
    height: 60px;
    margin-bottom: 5px;
}

.ramp-scale {
    display: flex;
    justify-content: space-between;
    font-size: 10px;
    color: #888;
    margin-bottom: 5px;
}

.ramp-label {
    font-size: 10px;
}

.ramp-gradient {
    height: 30px;
    border-radius: 3px;
    position: relative;
    border: 1px solid #3a3a3a;
}

.ramp-handles {
    position: absolute;
    top: 35px;
    left: 0;
    right: 0;
    height: 25px;
}

.ramp-handle {
    position: absolute;
    width: 4px;
    height: 20px;
    background: #fff;
    border: 1px solid #000;
    cursor: ew-resize;
    transform: translateX(-50%);
    z-index: 10;
}

.ramp-handle:hover {
    background: #4a9ac9; /* Color-blind friendly cyan-blue */
}

.ramp-handle-label {
    position: absolute;
    top: -18px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 9px;
    color: #fff;
    background: rgba(0, 0, 0, 0.7);
    padding: 2px 4px;
    border-radius: 2px;
    white-space: nowrap;
    pointer-events: none;
}

.ramp-handle.min {
    border-left: 2px solid #fff;
}

.ramp-handle.max {
    border-right: 2px solid #fff;
}



.placement-icon {
    font-size: 18px;
    margin-right: 5px;
}

.btn.active {
    background-color: #4a9ac9 !important; /* Color-blind friendly cyan-blue */
    border-color: #5baad9 !important;
    color: #fff !important;
}

/* Wealth ticker above settlements - Layer 1: Data (highest) */
.wealth-ticker {
    position: absolute;
    /* Position is set by JavaScript, don't set top/left here */
    background: rgba(0, 0, 0, 0.8);
    color: #ffd700;
    font-size: 7px;
    font-weight: bold;
    padding: 1.4px 4.2px;
    border-radius: 2.8px;
    white-space: nowrap;
    pointer-events: none;
    z-index: 500;
    border: 1px solid rgba(255, 215, 0, 0.5);
    text-shadow: 0 0 2px rgba(0, 0, 0, 0.8);
    /* Ensure text doesn't wrap */
    min-width: fit-content;
    box-sizing: border-box;
}

/* Population ticker above settlements - Layer 1: Data (highest) */
.population-ticker {
    position: absolute;
    /* Position is set by JavaScript, don't set top/left here */
    background: rgba(0, 0, 0, 0.8);
    color: #4a9ac9;
    font-size: 7px;
    font-weight: bold;
    padding: 1.4px 4.2px;
    border-radius: 2.8px;
    white-space: nowrap;
    pointer-events: none;
    z-index: 500;
    border: 1px solid rgba(74, 154, 201, 0.5);
    text-shadow: 0 0 2px rgba(0, 0, 0, 0.8);
    /* Ensure text doesn't wrap */
    min-width: fit-content;
    box-sizing: border-box;
}

/* Danger ticker above lairs - Layer 1: Data (highest) */
.danger-ticker {
    position: absolute;
    /* Position is set by JavaScript, don't set top/left here */
    background: rgba(0, 0, 0, 0.8);
    color: #ff4444;
    font-size: 7px;
    font-weight: bold;
    padding: 1.4px 4.2px;
    border-radius: 2.8px;
    white-space: nowrap;
    pointer-events: none;
    z-index: 500;
    border: 1px solid rgba(255, 68, 68, 0.5);
    text-shadow: 0 0 2px rgba(0, 0, 0, 0.8);
    /* Ensure text doesn't wrap */
    min-width: fit-content;
    box-sizing: border-box;
}

/* Coordinate label styling - Layer 1: Data (highest) */
.coordinate-label {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: #ffffff;
    font-size: 11px;
    font-weight: 500;
    text-shadow: 0 0 2px rgba(0, 0, 0, 0.8);
    pointer-events: none;
    z-index: 500;
}

/* Layer 2: Settlement icons (above resources, below agent markers) */
.hex.has-town::after {
    /* Base town icon - will be overridden by size-specific classes */
    content: '🏘️';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 20px;
    pointer-events: none;
    z-index: 300 !important; /* Ensure icons are above traversable overlay */
    color: initial; /* Ensure icons are always visible, not affected by parent color */
    display: block !important; /* Ensure icons are displayed */
}

/* Town size indicators - these override the base has-town icon */
.hex.town-settlement::after {
    content: '🏕️'; /* Tent/camping icon for settlements */
    font-size: 16px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 300 !important; /* Ensure icons are above traversable overlay */
    color: initial; /* Ensure icons are always visible */
    display: block !important; /* Ensure icons are displayed */
}

.hex.town-hamlet::after {
    content: '🏘️'; /* Small houses for hamlet */
    font-size: 18px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 300 !important; /* Ensure icons are above traversable overlay */
    color: initial; /* Ensure icons are always visible */
    display: block !important; /* Ensure icons are displayed */
}

.hex.town-town::after {
    content: '🏘️'; /* Houses for town */
    font-size: 22px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 300 !important; /* Ensure icons are above traversable overlay */
    color: initial; /* Ensure icons are always visible */
    display: block !important; /* Ensure icons are displayed */
}

.hex.town-city::after {
    content: '🏙️'; /* City skyline */
    font-size: 24px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 300 !important; /* Ensure icons are above traversable overlay */
    color: initial; /* Ensure icons are always visible */
    display: block !important; /* Ensure icons are displayed */
}

.hex.town-keep::after {
    content: '🏰'; /* Castle/keep */
    font-size: 26px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 300 !important; /* Ensure icons are above traversable overlay */
    color: initial; /* Ensure icons are always visible */
    display: block !important; /* Ensure icons are displayed */
}

.hex.town-ruin::after {
    content: '🗿'; /* Moai statue for ruins */
    font-size: 20px;
    opacity: 0.7;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 300 !important; /* Ensure icons are above traversable overlay */
    color: initial; /* Ensure icons are always visible */
    display: block !important; /* Ensure icons are displayed */
}

.hex.town-ruin-war::after {
    content: '⚔️'; /* Crossed swords for war ruins */
    font-size: 20px;
    opacity: 0.7;
}

.hex.has-lair::after {
    content: '👹'; /* Ogre/monster icon for lairs */
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 20px;
    pointer-events: none;
    z-index: 400 !important; /* Ensure icons are above traversable overlay */
    color: initial; /* Ensure icons are always visible */
    display: block !important; /* Ensure icons are displayed */
}

.hex.has-lair.lair-abandoned::after {
    content: '💀'; /* Skull icon for abandoned/defeated lairs */
    opacity: 0.5;
}

/* Layer 4: Resource icons - positioned in corners to avoid overlap with towns */
.hex.has-resource-fish::before {
    content: '🐟';
    position: absolute;
    top: 15%;
    left: 20%;
    font-size: 16px;
    pointer-events: none;
    z-index: 200;
    filter: drop-shadow(0 0 2px rgba(0, 0, 0, 0.8));
    color: initial; /* Ensure icons are always visible */
}

.hex.has-resource-gold::before {
    content: '⛏️';
    position: absolute;
    top: 15%;
    right: 20%;
    font-size: 16px;
    pointer-events: none;
    z-index: 200;
    filter: drop-shadow(0 0 2px rgba(0, 0, 0, 0.8));
    color: initial; /* Ensure icons are always visible */
}

.hex.has-resource-timber::before {
    content: '🪵';
    position: absolute;
    bottom: 15%;
    left: 20%;
    font-size: 16px;
    pointer-events: none;
    z-index: 200;
    filter: drop-shadow(0 0 2px rgba(0, 0, 0, 0.8));
    color: initial; /* Ensure icons are always visible */
}

.hex.has-resource-coal::before {
    content: '⚫';
    position: absolute;
    bottom: 15%;
    right: 20%;
    font-size: 16px;
    pointer-events: none;
    z-index: 200;
    filter: drop-shadow(0 0 2px rgba(0, 0, 0, 0.8));
    color: initial; /* Ensure icons are always visible */
}

/* Hide resource icons when checkbox is unchecked */
.hex.hide-resource-icons.has-resource-fish::before,
.hex.hide-resource-icons.has-resource-gold::before,
.hex.hide-resource-icons.has-resource-timber::before,
.hex.hide-resource-icons.has-resource-coal::before {
    display: none;
}

/* Hide settlement icons when checkbox is unchecked */
.hex.hide-settlement-icons.has-town::after,
.hex.hide-settlement-icons.town-settlement::after,
.hex.hide-settlement-icons.town-hamlet::after,
.hex.hide-settlement-icons.town-town::after,
.hex.hide-settlement-icons.town-city::after,
.hex.hide-settlement-icons.town-keep::after,
.hex.hide-settlement-icons.town-ruin::after {
    /* Always show ruin emoji even when settlement icons are hidden */
    display: block !important;
}

.hex.has-forest {
    background: #4a7c3a !important; /* Medium-dark green (distinct from hills brown) */
}

.hex.has-swamp {
    background: #3a4a3a !important; /* Dark grey-green (distinct from mountains grey) */
}

/* Traversability Histogram */
.traversability-histogram {
    background: #1a1a1a;
    border: 1px solid #444;
    border-radius: 4px;
    padding: 10px;
    min-height: 200px;
}

.histogram-bars {
    display: flex;
    align-items: flex-end;
    justify-content: space-around;
    height: 150px;
    gap: 2px;
    margin-bottom: 10px;
}

.histogram-bar-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 100%;
    min-width: 30px;
}

.histogram-bar {
    width: 100%;
    background: linear-gradient(to top, #4a90e2, #6bb3ff);
    border: 1px solid #2a5a8a;
    border-radius: 2px 2px 0 0;
    position: relative;
    transition: all 0.2s ease;
    min-height: 2px;
}

.histogram-bar:hover {
    background: linear-gradient(to top, #5aa0f2, #7bc3ff);
    border-color: #3a6a9a;
}

.histogram-count {
    position: absolute;
    top: -18px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 10px;
    color: #fff;
    white-space: nowrap;
    text-shadow: 0 0 2px rgba(0, 0, 0, 0.8);
    opacity: 0;
    transition: opacity 0.2s ease;
}

.histogram-bar-container:hover .histogram-count {
    opacity: 1;
}

.histogram-label {
    font-size: 9px;
    color: #aaa;
    margin-top: 4px;
    text-align: center;
    writing-mode: horizontal-tb;
    transform: rotate(0deg);
}

.histogram-stats {
    text-align: center;
    padding-top: 8px;
    border-top: 1px solid #333;
}

/* Layer 5: Terrain (lowest) - hex background z-index for stacking context */
/* Note: z-index on hex elements affects stacking of entire hex, not just background */
.hex.has-town {
    z-index: 100;
}

.hex.has-lair {
    z-index: 100;
}

.hex.has-road {
    z-index: 100;
}

.hex.has-forest,
.hex.has-swamp {
    z-index: 100;
}

/* Ensure base hex background is visible - terrain colors are applied via inline styles */
.hex {
    /* Background is set via inline style from terrainData, default is #2a2a2a */
}

.hex.has-road {
    /* Road data is stored but visual is rendered via SVG */
}

.hex.has-traversable {
    position: relative;
}

.hex.has-traversable::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(200, 180, 120, var(--traversable-opacity, 0.3));
    pointer-events: none;
    z-index: 0; /* Below all other content, only slightly above terrain background */
    clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
}

/* Ensure settlement and lair icons override traversable overlay when both are present */
.hex.has-traversable.has-town::after {
    /* Settlement icons override traversable overlay */
    content: '🏘️'; /* Base town icon - will be overridden by size-specific classes */
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 20px;
    pointer-events: none;
    z-index: 300 !important;
    color: initial;
    display: block !important;
    background: none !important; /* Remove traversable background */
    clip-path: none !important; /* Remove traversable clip-path */
}

.hex.has-traversable.has-town.town-settlement::after {
    content: '🏕️';
    font-size: 16px;
}

.hex.has-traversable.has-town.town-hamlet::after {
    content: '🏘️';
    font-size: 18px;
}

.hex.has-traversable.has-town.town-town::after {
    content: '🏘️';
    font-size: 22px;
}

.hex.has-traversable.has-town.town-city::after {
    content: '🏙️';
    font-size: 24px;
}

.hex.has-traversable.has-town.town-keep::after {
    content: '🏰';
    font-size: 26px;
}

.hex.has-traversable.has-town.town-ruin::after {
    content: '🗿';
    font-size: 20px;
    opacity: 0.7;
}

.hex.has-traversable.has-town.town-ruin-war::after {
    content: '⚔️'; /* Crossed swords for war ruins */
    font-size: 20px;
    opacity: 0.7;
}

.hex.has-traversable.has-lair::after {
    /* Lair icons override traversable overlay */
    content: '👹';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 20px;
    pointer-events: none;
    z-index: 400 !important;
    color: initial;
    display: block !important;
    background: none !important; /* Remove traversable background */
    clip-path: none !important; /* Remove traversable clip-path */
}

.hex.has-traversable.has-lair.lair-abandoned::after {
    content: '💀'; /* Skull icon for abandoned/defeated lairs */
    opacity: 0.5;
}

/* Layer 3: Agent markers */
/* Layer 3: Agent markers (above settlement icons) */
.agent-marker {
    position: absolute;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    border: 2px solid #fff;
    pointer-events: none;
    z-index: 400;
    transform: translate(-50%, -50%);
    top: 30%;
    left: 50%;
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.8);
    /* Smooth transitions for position changes */
    transition: top 0.3s ease-out, left 0.3s ease-out, opacity 0.2s ease-out, transform 0.3s ease-out;
}

.agent-marker.agent-trader {
    background: #4a9ac9; /* Cyan-blue for traders */
}

.agent-marker.agent-settler {
    background: #8b7355; /* Brown for settlers */
}

.agent-marker.agent-explorer {
    background: #d4b896; /* Tan for explorers */
}

.agent-marker.agent-merchant {
    background: #f5e6d3; /* Light cream for merchants */
}

.roads-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 100; /* Layer 5: Terrain */
}


.json-editor-info {
    margin-bottom: 10px;
    font-size: 12px;
    color: #888;
    padding: 8px;
    background: #1a1a1a;
    border-radius: 4px;
}

#jsonEditor {
    width: 100%;
    min-height: 400px;
    background: #1a1a1a;
    border: 1px solid #3a3a3a;
    border-radius: 4px;
    color: #e0e0e0;
    padding: 12px;
    font-family: 'Courier New', monospace;
    font-size: 12px;
    line-height: 1.5;
    resize: vertical;
    margin-bottom: 10px;
}

#jsonEditor:focus {
    outline: none;
    border-color: #4a9ac9; /* Color-blind friendly cyan-blue */
}

.json-editor-buttons {
    display: flex;
    gap: 10px;
}


.viewport-container {
    overflow: hidden;
    position: relative;
    background: #0f0f0f;
    cursor: grab;
}

.viewport-container:active {
    cursor: grabbing;
}

.hexgrid-container {
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
}

.hexgrid {
    position: absolute;
    top: 0;
    left: 0;
    transform-origin: 0 0;
    display: flex;
    flex-wrap: wrap;
    gap: 0;
    align-content: flex-start;
    will-change: transform;
}

.ticker-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 500; /* Layer 1: Data (highest) */
    transform-origin: 0 0;
}

.hex {
    position: relative;
    background: #2a2a2a;
    border: none;
    cursor: pointer;
    transition: background-color 0.15s;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #ffffff;
    font-size: 11px;
    font-weight: 500;
    user-select: none;
    text-shadow: 0 0 2px rgba(0, 0, 0, 0.8);
    overflow: visible; /* Allow tickers to extend beyond hex boundaries */
    /* No z-index on base hex - terrain background should be visible */
}

.hex:hover {
    filter: brightness(1.2);
    z-index: 150; /* Slightly above terrain for hover effect */
}

/* Hexagon shape using clip-path for flat-top hexagons */
/* For a regular flat-top hexagon: width/height = 2/sqrt(3) ≈ 1.155 */
/* So if height = 100%, width should be 115.5%, but we'll use the bounding box */
/* The clip-path creates a regular hexagon when width = 2/sqrt(3) * height */
.hex {
    clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
    /* The aspect ratio is handled by JavaScript setting width and height */
}

/* Fallback for browsers that support corner-shape */
@supports (corner-shape: bevel) {
    .hex {
        clip-path: none;
        border-radius: 25% / 50%;
        corner-shape: bevel;
    }
}
