/* ===========================================
   UBDOS ATLAS - Main Stylesheet
   =========================================== */

/* -------------------------------------------
   CSS Variables (Theme)
   ------------------------------------------- */
:root {
    --sidebar-width: 420px;
    --primary-gold: #d4af37;
    --dark-bg: #121212;
    --hover-bg: #252525;
    --villainous: #8B0000;
    --intrigue: #4B0082;
    --wealthy: #DAA520;
    --ruler: #FFD700;
    --edit-color: #00bcd4;
    --success-color: #4caf50;
    --danger-color: #f44336;
    --warning-color: #ff9800;
}

/* Neutral grey borders for all territories */
path.leaflet-interactive {
    stroke: #666 !important;
}

/* -------------------------------------------
   Base Styles
   ------------------------------------------- */
* {
    box-sizing: border-box;
}

body, html {
    margin: 0;
    padding: 0;
    height: 100%;
    font-family: 'Segoe UI', sans-serif;
    background: #000;
    color: #eee;
    overflow: hidden;
    /* Prevent pull-to-refresh and overscroll */
    overscroll-behavior: none;
}

/* Global touch fixes for buttons */
button {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

/* -------------------------------------------
   Layout
   ------------------------------------------- */
#app {
    display: flex;
    height: 100vh;
    width: 100vw;
}

#map {
    flex-grow: 1;
    height: 100%;
    background: #1a1a2e;
}

/* Leaflet touch handling fix */
.leaflet-container {
    touch-action: pan-x pan-y;
}

/* Ensure Leaflet controls don't block touches */
.leaflet-control-container {
    pointer-events: none;
}

.leaflet-control-container .leaflet-control {
    pointer-events: auto;
}

/* -------------------------------------------
   Loading Overlay
   ------------------------------------------- */
#loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.95);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    transition: opacity 0.5s ease;
}

#loading-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.loading-spinner {
    width: 60px;
    height: 60px;
    border: 4px solid #333;
    border-top: 4px solid var(--primary-gold);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

.loading-text {
    margin-top: 20px;
    color: var(--primary-gold);
    font-size: 1.1rem;
}

/* -------------------------------------------
   Status Bars
   ------------------------------------------- */
#data-status {
    position: absolute;
    bottom: 10px;
    left: 10px;
    background: rgba(0,0,0,0.8);
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 0.75rem;
    color: #888;
    z-index: 1000;
    display: flex;
    align-items: center;
    gap: 8px;
}

#data-status .status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--success-color);
}

#data-status .status-dot.error {
    background: var(--danger-color);
}

#data-status .status-dot.modified {
    background: var(--warning-color);
}

#refresh-btn {
    background: none;
    border: 1px solid #555;
    color: #888;
    padding: 4px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.7rem;
}

#refresh-btn:hover {
    border-color: var(--primary-gold);
    color: var(--primary-gold);
}

/* Edit Status */
#edit-status {
    position: absolute;
    bottom: 10px;
    left: 200px;
    background: rgba(0,0,0,0.8);
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 0.75rem;
    z-index: 1000;
    display: none;
    align-items: center;
    gap: 8px;
    border: 1px solid var(--edit-color);
}

#edit-status.active {
    display: flex;
}

#edit-status .edit-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--edit-color);
    animation: pulse 1.5s infinite;
}

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

#changes-count {
    color: var(--edit-color);
    font-weight: bold;
}

#export-btn {
    background: var(--edit-color);
    border: none;
    color: black;
    padding: 4px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.7rem;
    font-weight: bold;
    margin-left: 8px;
}

#export-btn:hover {
    background: #00acc1;
}

#discard-btn {
    background: var(--danger-color);
    border: none;
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.7rem;
    margin-left: 4px;
}

#discard-btn:hover {
    background: #d32f2f;
}

/* Edit Tips */
#edit-tips {
    position: absolute;
    bottom: 50px;
    left: 10px;
    background: rgba(0,0,0,0.9);
    padding: 10px 14px;
    border-radius: 6px;
    font-size: 0.7rem;
    z-index: 1000;
    display: none;
    border: 1px solid #444;
    max-width: 280px;
}

#edit-tips.active {
    display: block;
}

#edit-tips h4 {
    margin: 0 0 8px 0;
    color: var(--edit-color);
    font-size: 0.75rem;
}

#edit-tips ul {
    margin: 0;
    padding-left: 16px;
    color: #aaa;
}

#edit-tips li {
    margin-bottom: 4px;
}

#edit-tips .key {
    background: #333;
    padding: 1px 5px;
    border-radius: 3px;
    font-family: monospace;
}

/* -------------------------------------------
   Level Selector
   ------------------------------------------- */
#level-selector {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.level-btn {
    width: 48px;
    height: 36px;
    background: #1a1a1a;
    border: 2px solid #444;
    color: #888;
    font-weight: bold;
    cursor: pointer;
    border-radius: 6px;
    transition: all 0.2s ease;
    font-size: 0.75rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    touch-action: manipulation;
}

.level-btn:hover {
    border-color: var(--primary-gold);
    color: #ccc;
}

.level-btn.active {
    background: var(--primary-gold);
    color: black;
    border-color: white;
    box-shadow: 0 0 10px var(--primary-gold);
}

.level-btn .count {
    font-size: 0.55rem;
    font-weight: normal;
    opacity: 0.8;
}

.level-btn .loader {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 8px;
    height: 8px;
    border: 1px solid transparent;
    border-top-color: var(--primary-gold);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
    display: none;
}

.level-btn.loading .loader {
    display: block;
}

/* -------------------------------------------
   Sidebar
   ------------------------------------------- */
#sidebar {
    width: var(--sidebar-width);
    background: var(--dark-bg);
    border-left: 2px solid var(--primary-gold);
    display: flex;
    flex-direction: column;
    z-index: 1000;
}

#sidebar-header {
    padding: 12px 15px;
    background: #1a1a1a;
    border-bottom: 1px solid #333;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
}

#sidebar-header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

#sidebar-content {
    flex-grow: 1;
    overflow-y: auto;
    padding: 12px;
}

/* View Toggle */
.view-toggle {
    display: flex;
    background: #2a2a2a;
    border-radius: 4px;
    overflow: hidden;
}

.view-toggle-btn {
    padding: 6px 10px;
    background: transparent;
    border: none;
    color: #888;
    cursor: pointer;
    font-size: 0.75rem;
    transition: all 0.2s;
}

.view-toggle-btn.active {
    background: var(--primary-gold);
    color: black;
}

/* DM & Edit Buttons */
.dm-btn {
    cursor: pointer;
    font-size: 1.2rem;
    color: #444;
    transition: color 0.3s;
    margin-left: 4px;
}

.dm-btn.active {
    color: var(--primary-gold);
    text-shadow: 0 0 10px var(--primary-gold);
}

.edit-mode-btn {
    cursor: pointer;
    font-size: 1.2rem;
    color: #444;
    transition: color 0.3s;
}

.edit-mode-btn.active {
    color: var(--edit-color);
    text-shadow: 0 0 10px var(--edit-color);
}

.sidebar-link-btn {
    color: #666;
    font-size: 1.05rem;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: color 0.2s ease;
}

.sidebar-link-btn:hover {
    color: var(--primary-gold);
}

/* -------------------------------------------
   Search Box
   ------------------------------------------- */
#search-box {
    padding: 12px 15px;
    background: #1a1a1a;
    position: relative;
    flex-shrink: 0;
}

#search-input {
    width: 100%;
    padding: 10px 12px;
    background: #2a2a2a;
    border: 1px solid #444;
    color: white;
    border-radius: 4px;
    font-size: 0.9rem;
}

#search-input:focus {
    outline: none;
    border-color: var(--primary-gold);
}

#suggestions {
    position: absolute;
    top: 55px;
    left: 15px;
    right: 15px;
    background: #2a2a2a;
    border: 1px solid var(--primary-gold);
    list-style: none;
    padding: 0;
    margin: 0;
    z-index: 2000;
    display: none;
    max-height: 300px;
    overflow-y: auto;
    border-radius: 0 0 5px 5px;
}

#suggestions li {
    padding: 10px;
    cursor: pointer;
    border-bottom: 1px solid #333;
    font-size: 0.85rem;
}

#suggestions li:hover {
    background: var(--hover-bg);
}

/* -------------------------------------------
   District Tags
   ------------------------------------------- */
.district-tag-villainous { background: var(--villainous); }
.district-tag-intrigue { background: var(--intrigue); }
.district-tag-wealthy { background: var(--wealthy); color: #000; }
.district-tag-ruler { background: var(--ruler); color: #000; }

/* -------------------------------------------
   List Items
   ------------------------------------------- */
.category-group {
    margin-bottom: 12px;
}

.category-header {
    color: var(--primary-gold);
    font-size: 0.7rem;
    font-weight: bold;
    text-transform: uppercase;
    letter-spacing: 1px;
    border-bottom: 1px solid #333;
    padding-bottom: 4px;
    margin-bottom: 6px;
}

.list-item {
    padding: 8px 10px;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 10px;
    transition: background 0.2s;
    font-size: 0.85rem;
    margin-bottom: 2px;
    border-left: 3px solid transparent;
}

.list-item:hover {
    background: var(--hover-bg);
    color: var(--primary-gold);
}

.list-item.selected {
    background: var(--hover-bg);
    color: var(--primary-gold);
}

.list-item.modified {
    border-left-color: var(--edit-color) !important;
}

.list-item.new-item {
    border-left-color: var(--success-color) !important;
}

/* -------------------------------------------
   Tree View
   ------------------------------------------- */
.tree-level {
    margin-bottom: 4px;
}

.tree-header {
    display: flex;
    align-items: center;
    padding: 8px 10px;
    background: #1a1a1a;
    border-radius: 5px;
    cursor: pointer;
    transition: all 0.2s;
    margin-bottom: 2px;
}

.tree-header:hover {
    background: #252525;
}

.tree-header.current-level {
    border-left: 3px solid var(--primary-gold);
    background: #1f1f1f;
}

.tree-header .expand-icon {
    width: 18px;
    color: #666;
    transition: transform 0.2s;
    font-size: 0.7rem;
}

.tree-header.expanded .expand-icon {
    transform: rotate(90deg);
}

.tree-header .level-name {
    flex-grow: 1;
    font-weight: bold;
    color: #ccc;
    font-size: 0.85rem;
}

.tree-header.current-level .level-name {
    color: var(--primary-gold);
}

.tree-header .count-badge {
    background: #333;
    color: #888;
    padding: 2px 6px;
    border-radius: 8px;
    font-size: 0.65rem;
}

.tree-children {
    display: none;
    padding-left: 12px;
    border-left: 1px solid #333;
    margin-left: 8px;
}

.tree-children.expanded {
    display: block;
}

.tree-district {
    margin: 4px 0;
}

.tree-district-header {
    display: flex;
    align-items: center;
    padding: 5px 8px;
    cursor: pointer;
    color: #999;
    font-size: 0.75rem;
    transition: all 0.2s;
    border-left: 3px solid transparent;
}

.tree-district-header:hover {
    color: var(--primary-gold);
}

.tree-district-header .expand-icon {
    width: 14px;
    font-size: 0.6rem;
    color: #555;
    transition: transform 0.2s;
}

.tree-district-header.expanded .expand-icon {
    transform: rotate(90deg);
}

.tree-district-header .district-name {
    flex-grow: 1;
}

.tree-district-header .count-badge {
    background: #2a2a2a;
    color: #666;
    padding: 1px 5px;
    border-radius: 6px;
    font-size: 0.6rem;
}

.tree-items {
    display: none;
    padding-left: 8px;
}

.tree-items.expanded {
    display: block;
}

.tree-item {
    padding: 5px 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.8rem;
    color: #aaa;
    border-radius: 3px;
    transition: all 0.2s;
}

.tree-item:hover {
    background: var(--hover-bg);
    color: var(--primary-gold);
}

.tree-item.selected {
    background: var(--hover-bg);
    color: var(--primary-gold);
}

/* -------------------------------------------
   Detail View
   ------------------------------------------- */
.detail-header {
    padding: 15px;
    background: linear-gradient(135deg, #1a1a1a, #252525);
    border-radius: 8px;
    margin-bottom: 12px;
}

.detail-title {
    color: var(--primary-gold);
    font-size: 1.3rem;
    font-weight: bold;
    margin: 0 0 8px 0;
}

.detail-subtitle {
    color: #888;
    font-size: 0.8rem;
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.detail-code {
    font-family: monospace;
    background: #333;
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 0.75rem;
}

.tag {
    font-size: 0.6rem;
    background: #444;
    padding: 2px 6px;
    border-radius: 3px;
    text-transform: uppercase;
    color: #fff;
    white-space: nowrap;
}

.level-tag {
    background: #2c5d77;
}

.shape-tag {
    background: #6b5b95;
}

/* Thresholds */
.thresholds {
    display: flex;
    gap: 15px;
    padding: 12px;
    background: #1a1a1a;
    border-radius: 6px;
    margin-bottom: 12px;
}

.threshold-item {
    display: flex;
    align-items: center;
    gap: 6px;
}

.threshold-icon {
    font-size: 1rem;
}

.threshold-icon.villainy { color: var(--villainous); }
.threshold-icon.wealth { color: var(--wealthy); }
.threshold-icon.intrigue { color: var(--intrigue); }

.threshold-value {
    font-weight: bold;
    font-size: 1rem;
}

.threshold-label {
    font-size: 0.7rem;
    color: #888;
}

/* -------------------------------------------
   Accordion
   ------------------------------------------- */
.accordion {
    margin-bottom: 8px;
}

.accordion-header {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    background: #1a1a1a;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s;
    border-left: 3px solid transparent;
}

.accordion-header:hover {
    background: #252525;
}

.accordion-header.expanded {
    border-left-color: var(--primary-gold);
}

.accordion-header .expand-icon {
    width: 20px;
    color: #666;
    transition: transform 0.2s;
}

.accordion-header.expanded .expand-icon {
    transform: rotate(90deg);
    color: var(--primary-gold);
}

.accordion-header .title {
    flex-grow: 1;
    font-weight: 600;
    color: #ccc;
    font-size: 0.9rem;
}

.accordion-header .badge {
    background: #333;
    color: #888;
    padding: 2px 8px;
    border-radius: 10px;
    font-size: 0.7rem;
}

.accordion-content {
    display: none;
    padding: 12px;
    background: #161616;
    border-radius: 0 0 6px 6px;
    margin-top: -4px;
}

.accordion-content.expanded {
    display: block;
}

/* -------------------------------------------
   Connections
   ------------------------------------------- */
.connections-group {
    margin-bottom: 10px;
}

.connections-label {
    font-size: 0.7rem;
    font-weight: bold;
    text-transform: uppercase;
    color: #888;
    margin-bottom: 4px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.connections-label .dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
}

.connections-label .dot.open { background: var(--success-color); }
.connections-label .dot.restricted { background: var(--warning-color); }
.connections-label .dot.none { background: var(--danger-color); }

.connection-item {
    display: block;
    background: #2a2a2a;
    padding: 4px 8px;
    border-radius: 4px;
    margin: 2px 0;
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.2s;
    line-height: 1.5;
}

.connection-item:hover {
    background: var(--primary-gold);
    color: black;
}

.connection-item .dir {
    font-size: 0.7rem;
    color: #888;
}

.connection-item .req {
    font-size: 0.65rem;
    background: #444;
    padding: 1px 4px;
    border-radius: 2px;
    margin-left: 4px;
}

.access-badge {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    font-size: 0.7rem;
    background: #e8e0d0;
    padding: 2px 5px;
    border-radius: 3px;
    margin-left: 4px;
    border: 1px solid #c8c0b0;
    color: #444;
    white-space: nowrap;
}

.access-badge i {
    font-size: 0.65rem;
}

/* Sidebar connections table (dark theme) */
.sidebar-conn-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
    table-layout: fixed;
}

.sidebar-conn-table .slct-hdr {
    font-size: 0.65rem;
    font-weight: 600;
    color: #888;
    padding: 2px 4px;
    text-align: left;
    border-bottom: 1px solid #444;
    white-space: nowrap;
    text-transform: uppercase;
}

.sidebar-conn-table .slct-level-hdr {
    padding: 2px 4px;
    border-bottom: 1px solid #444;
    width: 30px;
}

.sidebar-conn-table .slct-hdr:nth-child(2),
.sidebar-conn-table .slct-hdr:nth-child(3),
.sidebar-conn-table .slct-hdr:nth-child(4) {
    width: calc((100% - 30px) / 3);
}

.sidebar-conn-table .slct-dot {
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 3px;
    vertical-align: middle;
}

.sidebar-conn-table .slct-level {
    font-weight: 700;
    font-size: 0.7rem;
    color: #aaa;
    white-space: nowrap;
    padding: 4px 6px 4px 2px;
    vertical-align: top;
    border-right: 1px solid #333;
}

.sidebar-conn-table .slct-cell {
    padding: 3px 4px;
    vertical-align: top;
    overflow-wrap: break-word;
    word-break: break-word;
}

.sidebar-conn-table tbody tr:not(:last-child) td {
    border-bottom: 1px solid #2a2a2a;
}

/* Current-level row in sidebar */
.sidebar-conn-table .slct-current {
    background: rgba(255, 255, 255, 0.04);
}

.sidebar-conn-table .slct-current.slct-level {
    color: #7e57c2;
}

/* Sites */
.site-card {
    background: linear-gradient(135deg, #1f1f1f, #2a2a2a);
    border: 1px solid #333;
    border-radius: 6px;
    padding: 12px;
    margin-bottom: 8px;
    border-left: 3px solid var(--primary-gold);
}

.site-name {
    font-weight: bold;
    color: var(--primary-gold);
    font-size: 0.9rem;
    margin-bottom: 6px;
}

.site-effect {
    font-size: 0.8rem;
    color: #ccc;
    line-height: 1.5;
}

/* -------------------------------------------
   Action Buttons
   ------------------------------------------- */
.action-btn {
    width: 100%;
    margin-top: 8px;
    padding: 12px;
    background: var(--primary-gold);
    border: none;
    cursor: pointer;
    font-weight: bold;
    border-radius: 6px;
    color: black;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all 0.2s;
}

.action-btn:hover {
    background: #e5c040;
    transform: scale(1.02);
}

.action-btn.edit-btn {
    background: var(--edit-color);
}

.action-btn.edit-btn:hover {
    background: #00acc1;
}

.action-btn.delete-btn {
    background: var(--danger-color);
}

.action-btn.delete-btn:hover {
    background: #d32f2f;
}

.close-btn {
    float: right;
    cursor: pointer;
    font-size: 1.3rem;
    color: #888;
}

.close-btn:hover {
    color: white;
}

/* -------------------------------------------
   Popups (Leaflet)
   ------------------------------------------- */
.leaflet-popup-content-wrapper {
    background-color: #f5f5f0 !important;
    border-radius: 8px !important;
}

.leaflet-popup-content {
    margin: 0 !important;
    min-width: 300px;
    max-width: 400px;
    overflow-x: hidden;
}

.popup-header {
    background: linear-gradient(135deg, #2c5d77, #1a3a4a);
    color: white;
    padding: 12px 15px;
    border-radius: 8px 8px 0 0;
    cursor: grab;
    user-select: none;
}

.popup-header:active {
    cursor: grabbing;
}

.popup-title {
    font-size: 1.1rem;
    font-weight: bold;
}

.popup-subtitle {
    font-size: 0.75rem;
    opacity: 0.8;
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-top: 4px;
}

.popup-tabs {
    display: flex;
    background: #e8e8e0;
}

.popup-tab {
    flex: 1;
    padding: 8px;
    text-align: center;
    cursor: pointer;
    font-size: 0.75rem;
    font-weight: 600;
    color: #666;
    border: none;
    background: transparent;
}

.popup-tab:hover {
    background: #ddd;
}

.popup-tab.active {
    background: white;
    color: #2c5d77;
}

.popup-tab-content {
    display: none;
    padding: 12px 15px;
    max-height: 200px;
    overflow-y: auto;
}

.popup-tab-content.active {
    display: block;
}

.popup-text {
    font-size: 0.85rem;
    line-height: 1.5;
    color: #444;
}

.popup-thresholds {
    display: flex;
    gap: 12px;
    margin: 8px 0;
}

.popup-threshold {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.85rem;
    font-weight: bold;
}

.popup-threshold.v { color: #8B0000; }
.popup-threshold.w { color: #B8860B; }
.popup-threshold.i { color: #4B0082; }

.popup-connection {
    display: block;
    background: #e0e0d8;
    padding: 4px 8px;
    border-radius: 4px;
    margin: 2px 0;
    font-size: 0.75rem;
    cursor: pointer;
    line-height: 1.5;
}

.popup-connection:hover {
    background: #2c5d77;
    color: white;
}

.popup-connection:hover .popup-access-badge {
    border-color: rgba(255, 255, 255, 0.3);
}

.popup-access-badge {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    font-size: 0.7rem;
    background: #e8e0d0;
    padding: 1px 5px;
    border-radius: 3px;
    margin-left: 4px;
    border: 1px solid #c8c0b0;
    color: #444;
    white-space: nowrap;
    vertical-align: middle;
    line-height: 1.3;
}

.popup-access-badge i {
    font-size: 0.6rem;
}

/* -------------------------------------------
   Mobile Drag Handle
   ------------------------------------------- */
.drag-handle {
    display: none;
    width: 40px;
    height: 5px;
    background: #555;
    border-radius: 3px;
    margin: 8px auto;
    cursor: grab;
}

/* -------------------------------------------
   Geoman Toolbar
   ------------------------------------------- */
.leaflet-pm-toolbar,
.leaflet-pm-draw,
.leaflet-pm-edit,
.leaflet-pm-custom-action,
.leaflet-pm-actions-container {
    display: none !important;
}

body.editing-mode .leaflet-pm-toolbar,
body.editing-mode .leaflet-pm-draw,
body.editing-mode .leaflet-pm-edit,
body.editing-mode .leaflet-pm-custom-action,
body.editing-mode .leaflet-pm-actions-container {
    display: block !important;
}

/* -------------------------------------------
   Custom Marker Icons
   ------------------------------------------- */
.custom-marker-icon {
    background: var(--primary-gold);
    border: 3px solid white;
    border-radius: 50%;
    box-shadow: 0 2px 5px rgba(0,0,0,0.4);
}

.custom-marker-icon.modified {
    border-color: var(--edit-color);
}

.custom-marker-icon.new {
    border-color: var(--success-color);
}

/* -------------------------------------------
   Modals
   ------------------------------------------- */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.8);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 10000;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: #1a1a1a;
    border: 2px solid var(--primary-gold);
    border-radius: 12px;
    padding: 24px;
    width: 90%;
    max-width: 500px;
    max-height: 85vh;
    overflow-y: auto;
}

.modal h3 {
    margin: 0 0 16px 0;
    color: var(--primary-gold);
    display: flex;
    align-items: center;
    gap: 10px;
}

.modal-buttons {
    display: flex;
    gap: 12px;
    margin-top: 20px;
}

.modal-buttons button {
    flex: 1;
    padding: 12px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-weight: bold;
    font-size: 0.9rem;
}

.modal-save {
    background: var(--primary-gold);
    color: black;
}

.modal-save:hover {
    background: #e5c040;
}

.modal-cancel {
    background: #444;
    color: white;
}

.modal-cancel:hover {
    background: #555;
}

/* -------------------------------------------
   Form Elements
   ------------------------------------------- */
.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    color: #888;
    font-size: 0.8rem;
    margin-bottom: 6px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.form-group input,
.form-group select,
.form-group textarea {
    width: 100%;
    padding: 10px 12px;
    background: #2a2a2a;
    border: 1px solid #444;
    color: white;
    border-radius: 6px;
    font-size: 0.9rem;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--primary-gold);
}

.form-group textarea {
    min-height: 80px;
    resize: vertical;
}

.form-group input[type="color"] {
    padding: 4px;
    height: 40px;
    cursor: pointer;
}

.form-row {
    display: flex;
    gap: 12px;
}

.form-row .form-group {
    flex: 1;
}

.form-group select {
    cursor: pointer;
}

/* Level checkboxes group */
.checkbox-group {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 8px;
    padding: 8px 10px;
    background: #2a2a2a;
    border: 1px solid #444;
    border-radius: 6px;
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: 4px;
    color: #ccc;
    font-size: 0.85rem;
    cursor: pointer;
    padding: 2px 4px;
    border-radius: 4px;
    text-transform: none;
    letter-spacing: 0;
}

.checkbox-label:hover {
    background: rgba(255, 255, 255, 0.08);
}

.checkbox-label input[type="checkbox"] {
    width: auto;
    margin: 0;
    cursor: pointer;
}

/* Threshold split input (type dropdown + number) */
.threshold-input {
    display: flex;
    gap: 6px;
}

.threshold-input .threshold-type {
    flex: 1.2;
    padding: 10px 8px;
    background: #2a2a2a;
    border: 1px solid #444;
    color: white;
    border-radius: 6px;
    font-size: 0.85rem;
    cursor: pointer;
}

.threshold-input .threshold-value {
    flex: 0.8;
    padding: 10px 8px;
    background: #2a2a2a;
    border: 1px solid #444;
    color: white;
    border-radius: 6px;
    font-size: 0.9rem;
    text-align: center;
}

.threshold-input .threshold-value::-webkit-inner-spin-button {
    opacity: 1;
}

/* -------------------------------------------
   Mobile Responsive - Portrait
   ------------------------------------------- */
@media (max-width: 768px) and (orientation: portrait) {
    #app {
        flex-direction: column;
    }
    
    #sidebar {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        height: 45vh;
        border-left: none;
        border-top: 2px solid var(--primary-gold);
        border-radius: 16px 16px 0 0;
    }
    
    #sidebar.collapsed {
        height: 80px;
    }
    
    .drag-handle {
        display: block;
    }
    
    #data-status {
        top: 60px;
        bottom: auto;
    }
    
    #edit-status {
        top: 100px;
        bottom: auto;
        left: 10px;
    }
    
    #edit-tips {
        display: none !important;
    }
}

/* -------------------------------------------
   Mobile Responsive - Landscape
   ------------------------------------------- */
@media (max-width: 926px) and (orientation: landscape) {
    #app {
        flex-direction: row;
        position: relative;
    }
    
    #map {
        flex-grow: 1;
        height: 100vh;
        width: 60%;
    }
    
    #sidebar {
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        left: auto;
        width: 40%;
        max-width: 320px;
        height: 100%;
        border-top: none;
        border-left: 2px solid var(--primary-gold);
        border-radius: 0;
        z-index: 1000;
        touch-action: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    #sidebar-content {
        -webkit-overflow-scrolling: touch;
        overflow-y: auto;
    }
    
    #sidebar.collapsed {
        width: 50px;
        min-width: 50px;
        height: 100%;
    }
    
    #sidebar.collapsed #sidebar-header-left > *:not(#sidebar-toggle) {
        display: none;
    }
    
    #sidebar.collapsed .view-toggle,
    #sidebar.collapsed #search-box,
    #sidebar.collapsed #sidebar-content {
        display: none;
    }
    
    #sidebar.collapsed #sidebar-header {
        justify-content: center;
        padding: 12px 8px;
    }
    
    #sidebar.collapsed .sidebar-toggle-btn i {
        transform: rotate(0deg);
    }
    
    .drag-handle {
        display: none;
    }
    
    #data-status {
        position: fixed;  /* Changed from absolute */
        top: auto;
        bottom: 10px;
        left: 10px;
        z-index: 1001;
    }
    
    #edit-status {
        position: fixed;  /* Changed from absolute */
        top: auto;
        bottom: 50px;
        left: 10px;
        z-index: 1001;
    }
    
    #edit-tips {
        display: none !important;
    }
    
    /* Sidebar header compact mode */
    #sidebar-header {
        padding: 8px 10px;
    }
    
    #sidebar-header-left {
        gap: 8px;
    }
    
    #sidebar-header-left span {
        font-size: 0.85rem;
    }
    
    /* Ensure buttons are easily tappable */
    .sidebar-toggle-btn {
        min-width: 36px;
        min-height: 36px;
        /* Prevent double-tap zoom */
        touch-action: manipulation;
    }
}

/* Small mobile landscape (phones) */
@media (max-height: 500px) and (orientation: landscape) {
    #sidebar {
        width: 35%;
        max-width: 280px;
    }
    
    #sidebar-content {
        padding: 8px;
    }
    
    .category-header {
        padding: 6px 10px;
        font-size: 0.75rem;
    }
    
    .list-item {
        padding: 6px 10px;
        font-size: 0.8rem;
    }
}

/* ===========================================
   Version History Styles
   =========================================== */

.modal-large {
    max-width: 600px;
    max-height: 80vh;
}

.modal-large h3 {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-large h3 .close-btn {
    font-size: 1.5rem;
    cursor: pointer;
    color: #888;
    line-height: 1;
}

.modal-large h3 .close-btn:hover {
    color: white;
}

#version-history-content {
    max-height: 60vh;
    overflow-y: auto;
}

.version-section {
    margin-bottom: 20px;
}

.version-section-header {
    color: var(--primary-gold);
    font-size: 0.8rem;
    font-weight: bold;
    text-transform: uppercase;
    letter-spacing: 1px;
    padding-bottom: 8px;
    margin-bottom: 10px;
    border-bottom: 1px solid #333;
    display: flex;
    align-items: center;
    gap: 8px;
}

.version-item {
    background: #252525;
    border-radius: 8px;
    padding: 12px;
    margin-bottom: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-left: 3px solid #444;
}

.version-item.latest {
    border-left-color: var(--primary-gold);
}

.version-item.autosave {
    border-left-color: var(--warning-color);
    background: #2a2518;
}

.version-info {
    flex: 1;
}

.version-title {
    font-weight: 600;
    color: #eee;
    margin-bottom: 4px;
}

.version-meta {
    font-size: 0.75rem;
    color: #888;
}

.badge-latest {
    background: var(--primary-gold);
    color: black;
    padding: 1px 6px;
    border-radius: 3px;
    font-size: 0.65rem;
    font-weight: bold;
}

.version-actions {
    display: flex;
    gap: 6px;
}

.version-btn {
    padding: 6px 10px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.8rem;
    display: flex;
    align-items: center;
    gap: 4px;
    transition: all 0.2s;
}

.version-btn.restore {
    background: var(--primary-gold);
    color: black;
}

.version-btn.restore:hover {
    background: #e5c040;
}

.version-btn.delete {
    background: #444;
    color: #aaa;
}

.version-btn.delete:hover {
    background: var(--danger-color);
    color: white;
}

.version-btn.github {
    background: #333;
    color: white;
}

.version-btn.github:hover {
    background: #444;
}

.version-empty {
    color: #666;
    font-style: italic;
    text-align: center;
    padding: 20px;
}

/* ===========================================
   Cloud Save Styles
   =========================================== */

#cloud-save-btn {
    background: #2196f3;
    border: none;
    color: white;
    padding: 4px 10px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.7rem;
    font-weight: bold;
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

#cloud-save-btn:hover {
    background: #1976d2;
}

#dm-key-btn {
    background: #555;
    border: none;
    color: #ccc;
    padding: 4px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.7rem;
}

#dm-key-btn:hover {
    background: #777;
    color: white;
}

#history-btn {
    background: #666;
    border: none;
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.7rem;
}

#history-btn:hover {
    background: #888;
}

.save-summary {
    background: #252525;
    border-radius: 6px;
    padding: 12px;
    margin-bottom: 16px;
}

.save-summary-label {
    font-size: 0.75rem;
    color: #888;
    margin-bottom: 6px;
}

.save-summary-changes {
    font-size: 1rem;
    font-weight: 600;
    color: #eee;
}

.modal-save.cloud {
    background: #2196f3;
}

.modal-save.cloud:hover {
    background: #1976d2;
}

/* ===========================================
   Progress Overlay
   =========================================== */

.progress-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.9);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 10001;
}

.progress-overlay.active {
    display: flex;
}

.progress-content {
    text-align: center;
    color: white;
}

.progress-icon {
    color: #2196f3;
    margin-bottom: 20px;
    animation: pulse 1.5s infinite;
}

.progress-bar-container {
    width: 300px;
    height: 6px;
    background: #333;
    border-radius: 3px;
    overflow: hidden;
    margin-top: 20px;
}

.progress-bar {
    height: 100%;
    background: linear-gradient(90deg, #2196f3, var(--primary-gold));
    width: 0%;
    transition: width 0.3s ease;
}

/* ===========================================
   Success Modal
   =========================================== */

.modal-success {
    text-align: center;
}

.modal-success h3 {
    justify-content: center;
}

.success-icon {
    font-size: 4rem;
    color: var(--success-color);
    margin-bottom: 16px;
}

.success-sha {
    font-family: monospace;
    background: #333;
    padding: 4px 10px;
    border-radius: 4px;
    font-size: 0.85rem;
    color: #888;
}

.success-note {
    color: #888;
    font-size: 0.85rem;
    margin-top: 16px;
}

/* ===========================================
   Recovery Modal
   =========================================== */

.recovery-info {
    background: #252525;
    padding: 12px;
    border-radius: 6px;
    margin: 12px 0;
    border-left: 3px solid var(--warning-color);
}

/* ===========================================
   Loading Small
   =========================================== */

.loading-small {
    color: #888;
    text-align: center;
    padding: 20px;
}

/* ===========================================
   Local Power Styles (NEW)
   =========================================== */

/* Popup local power section */
.popup-local-power {
    margin-top: 12px;
    padding: 10px;
    background: linear-gradient(135deg, #3d2914 0%, #2a1d0e 100%);
    border-radius: 6px;
    border-left: 3px solid #d4af37;
}

.popup-local-power .local-power-label {
    font-size: 0.75rem;
    font-weight: bold;
    color: #d4af37;
    margin-bottom: 6px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.popup-local-power .local-power-text {
    font-size: 0.85rem;
    color: #e0d5c0;
    line-height: 1.4;
}

/* Sidebar detail local power section */
.local-power-section {
    background: linear-gradient(135deg, #2a2010 0%, #1a1508 100%);
    border: 1px solid #3d2914;
    border-left: 3px solid var(--primary-gold);
    border-radius: 6px;
    padding: 12px;
    margin: 12px 0;
}

.local-power-header {
    font-size: 0.8rem;
    font-weight: bold;
    color: var(--primary-gold);
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.local-power-header i {
    font-size: 0.9rem;
}

.local-power-content {
    font-size: 0.9rem;
    color: #ccc;
    line-height: 1.5;
}

/* ===========================================
   Territory Label Centering Fix
   =========================================== */

.territory-label {
    background: transparent !important;
    border: none !important;
}

/* ===========================================
   Sidebar Toggle Button
   =========================================== */

.sidebar-toggle-btn {
    background: transparent;
    border: 1px solid #444;
    color: var(--primary-gold);
    width: 28px;
    height: 28px;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    flex-shrink: 0;
    /* Touch fixes */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

.sidebar-toggle-btn:hover {
    background: rgba(212, 175, 55, 0.1);
    border-color: var(--primary-gold);
}

.sidebar-toggle-btn i {
    font-size: 0.8rem;
    transition: transform 0.3s ease;
}

/* ===========================================
   Sidebar Collapsed State
   =========================================== */

/* Desktop collapsed state */
@media (min-width: 769px) {
    #sidebar.collapsed {
        width: 50px !important;
        min-width: 50px;
    }
    
    #sidebar.collapsed #sidebar-header-left > *:not(#sidebar-toggle) {
        display: none;
    }
    
    #sidebar.collapsed .view-toggle,
    #sidebar.collapsed #search-box,
    #sidebar.collapsed #sidebar-content {
        display: none;
    }
    
    #sidebar.collapsed #sidebar-header {
        justify-content: center;
        padding: 12px 8px;
    }
    
    #sidebar.collapsed #sidebar-header-left {
        justify-content: center;
    }
    
    #sidebar.collapsed .sidebar-toggle-btn i {
        transform: rotate(180deg);
    }
}

/* Mobile collapsed state */
@media (max-width: 768px) {
    #sidebar.collapsed {
        height: 50px !important;
        min-height: 50px;
    }
    
    #sidebar.collapsed #sidebar-header-left > *:not(#sidebar-toggle) {
        display: none;
    }
    
    #sidebar.collapsed .view-toggle,
    #sidebar.collapsed #search-box,
    #sidebar.collapsed #sidebar-content,
    #sidebar.collapsed .drag-handle {
        display: none;
    }
    
    #sidebar.collapsed #sidebar-header {
        justify-content: center;
    }
    
    #sidebar.collapsed #sidebar-header-left {
        justify-content: center;
    }
    
    #sidebar.collapsed .sidebar-toggle-btn i {
        transform: rotate(90deg);
    }
}

/* ===========================================
   Map Controls - Right Side (Labels Toggle, Level Selector)
   =========================================== */

#map-controls-right {
    position: fixed;
    top: 10px;
    right: calc(var(--sidebar-width) + 10px);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: flex-end;
}

#map-controls-right #level-selector {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

/* Desktop: When sidebar is collapsed */
@media (min-width: 769px) {
    #sidebar.collapsed ~ #map-controls-right,
    body:has(#sidebar.collapsed) #map-controls-right {
        right: 60px;
    }
}

/* ===========================================
   Map Controls - Bottom Right (Legend Toggle)
   =========================================== */

#map-controls-legend {
    position: fixed;
    bottom: 10px;
    right: calc(var(--sidebar-width) + 10px);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* Desktop: When sidebar is collapsed */
@media (min-width: 769px) {
    #sidebar.collapsed ~ #map-controls-legend,
    body:has(#sidebar.collapsed) #map-controls-legend {
        right: 60px;
    }
}

/* ===========================================
   Map Control Buttons (shared styles)
   =========================================== */

.map-control-btn {
    width: 36px;
    height: 36px;
    background: rgba(30, 30, 30, 0.9);
    border: 1px solid #444;
    border-radius: 6px;
    color: #888;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

.map-control-btn:hover {
    background: rgba(50, 50, 50, 0.95);
    border-color: #666;
    color: #ccc;
}

.map-control-btn.active {
    color: var(--primary-gold);
    border-color: var(--primary-gold);
}

.map-control-btn.active:hover {
    background: rgba(212, 175, 55, 0.1);
}

.map-control-btn i {
    font-size: 0.9rem;
}

/* ===========================================
   Organization Color Editor Panel
   =========================================== */

#org-color-panel {
    position: fixed;
    bottom: 60px;
    right: calc(var(--sidebar-width) + 50px);
    width: 280px;
    max-height: 400px;
    background: rgba(20, 20, 20, 0.95);
    border: 1px solid #444;
    border-radius: 8px;
    z-index: 1001;
    overflow: hidden;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
}

#org-color-panel .legend-content {
    max-height: 300px;
    overflow-y: auto;
    padding: 8px 0;
}

.org-color-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 5px 12px;
}

.org-color-row:hover {
    background: rgba(255, 255, 255, 0.05);
}

.org-color-row input[type="color"] {
    -webkit-appearance: none;
    border: 1px solid #555;
    border-radius: 4px;
    width: 28px;
    height: 22px;
    cursor: pointer;
    padding: 0;
    background: transparent;
    flex-shrink: 0;
}

.org-color-row input[type="color"]::-webkit-color-swatch-wrapper {
    padding: 1px;
}

.org-color-row input[type="color"]::-webkit-color-swatch {
    border: none;
    border-radius: 2px;
}

.org-color-name {
    color: #ccc;
    font-size: 0.8rem;
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.org-color-reset {
    color: #666;
    cursor: pointer;
    font-size: 0.7rem;
    flex-shrink: 0;
}

.org-color-reset:hover {
    color: var(--primary-gold);
}

.org-color-delete {
    color: #555;
    cursor: pointer;
    font-size: 0.75rem;
    flex-shrink: 0;
    padding: 2px;
}

.org-color-delete:hover {
    color: var(--danger-color);
}

.org-color-add-row {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    border-top: 1px solid #333;
    margin-top: 4px;
}

.org-add-input {
    flex: 1;
    background: #1e1e1e;
    border: 1px solid #444;
    border-radius: 4px;
    color: #ccc;
    padding: 4px 8px;
    font-size: 0.8rem;
    outline: none;
}

.org-add-input:focus {
    border-color: var(--primary-gold);
}

.org-add-btn {
    background: rgba(76, 175, 80, 0.3);
    border: 1px solid rgba(76, 175, 80, 0.5);
    color: #81c784;
    border-radius: 4px;
    padding: 4px 8px;
    cursor: pointer;
    font-size: 0.75rem;
}

.org-add-btn:hover {
    background: rgba(76, 175, 80, 0.5);
}

/* Desktop: When sidebar is collapsed */
@media (min-width: 769px) {
    #sidebar.collapsed ~ #org-color-panel,
    body:has(#sidebar.collapsed) #org-color-panel {
        right: 100px;
    }
}

/* ===========================================
   Legend Panel
   =========================================== */

#legend-panel {
    position: fixed;
    bottom: 60px;
    right: calc(var(--sidebar-width) + 10px);
    width: 280px;
    max-height: 400px;
    background: rgba(20, 20, 20, 0.95);
    border: 1px solid #444;
    border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
    z-index: 999;
    display: none;
    flex-direction: column;
    overflow: hidden;
}

#legend-panel.active {
    display: flex;
}

.legend-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    background: #1a1a1a;
    border-bottom: 1px solid #333;
    font-weight: bold;
    color: var(--primary-gold);
}

.legend-close {
    background: none;
    border: none;
    color: #888;
    font-size: 1.2rem;
    cursor: pointer;
    padding: 0;
    line-height: 1;
}

.legend-close:hover {
    color: #fff;
}

.legend-content {
    padding: 12px 16px;
    overflow-y: auto;
    flex-grow: 1;
}

.legend-section {
    margin-bottom: 16px;
}

.legend-section:last-child {
    margin-bottom: 0;
}

.legend-section-title {
    font-size: 0.75rem;
    font-weight: bold;
    color: #888;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 4px 0;
    font-size: 0.85rem;
    color: #ccc;
}

.legend-item i {
    width: 18px;
    text-align: center;
}

.legend-item strong {
    color: var(--primary-gold);
    min-width: 24px;
}

.legend-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
}

.legend-swatch {
    display: inline-block;
    width: 14px;
    height: 14px;
    border-radius: 3px;
    border: 1px solid rgba(255,255,255,0.2);
    flex-shrink: 0;
}

.legend-symbol {
    display: inline-block;
    width: 18px;
    text-align: center;
    font-size: 1rem;
    color: #ccc;
}

#legend-control-colors,
#legend-district-colors {
    transition: opacity 0.2s ease;
}

#legend-control-colors.legend-inactive,
#legend-district-colors.legend-inactive {
    opacity: 0.4;
}

#legend-control-colors .legend-section-title,
#legend-district-colors .legend-section-title {
    display: flex;
    align-items: center;
    gap: 6px;
}

.legend-active-badge {
    font-size: 0.6rem;
    background: var(--primary-gold);
    color: #1a1a2e;
    padding: 1px 5px;
    border-radius: 8px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

.legend-dot.open {
    background: #4caf50;
}

.legend-dot.restricted {
    background: #ff9800;
}

.legend-dot.none {
    background: #f44336;
}

.legend-edit-btn {
    background: transparent;
    border: 1px solid #444;
    color: #888;
    width: 22px;
    height: 22px;
    border-radius: 4px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 0.65rem;
}

.legend-edit-btn:hover {
    border-color: var(--primary-gold);
    color: var(--primary-gold);
}

/* Desktop: When sidebar is collapsed */
@media (min-width: 769px) {
    #sidebar.collapsed ~ #legend-panel,
    body:has(#sidebar.collapsed) #legend-panel {
        right: 60px;
    }
}

/* ===========================================
   Control/HQ Tags
   =========================================== */

.control-tag {
    background: #2d4a5c !important;
    color: #8ec8e8 !important;
}

.hq-tag {
    background: #5c2d2d !important;
    color: #e88e8e !important;
    font-weight: bold;
}

/* ===========================================
   Mobile Portrait adjustments
   =========================================== */
@media (max-width: 768px) and (orientation: portrait) {
    #map-controls-right {
        position: fixed;
        top: 10px;
        right: 10px;
    }
    
    #map-controls-right #level-selector {
        flex-direction: row;
        flex-wrap: wrap;
        max-width: 160px;
    }
    
    #map-controls-right #level-selector .level-btn {
        width: 38px;
        height: 32px;
        font-size: 0.7rem;
    }
    
    #map-controls-right #level-selector .level-btn .count {
        display: none;
    }
    
    #map-controls-legend {
        position: fixed;
        bottom: calc(45vh + 10px);
        right: 10px;
    }
    
    .map-control-btn {
        width: 40px;
        height: 40px;
    }
    
    .map-control-btn i {
        font-size: 1rem;
    }
    
    #legend-panel {
        bottom: calc(45vh + 60px);
        right: 10px;
        width: 260px;
        max-height: 300px;
    }
}

/* Mobile Landscape adjustments */
@media (max-width: 926px) and (orientation: landscape) {
    #map-controls-right {
        position: fixed;
        top: 10px;
        right: calc(40% + 10px);
        z-index: 1002;
    }
    
    #map-controls-right #level-selector {
        flex-direction: column;
    }
    
    #map-controls-right #level-selector .level-btn {
        width: 42px;
        height: 28px;
        font-size: 0.7rem;
    }
    
    #map-controls-right #level-selector .level-btn .count {
        display: none;
    }
    
    #map-controls-legend {
        position: fixed;
        bottom: 10px;
        right: calc(40% + 10px);
        z-index: 1002;
    }
    
    .map-control-btn {
        width: 36px;
        height: 36px;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }
    
    #legend-panel {
        bottom: 60px;
        right: calc(40% + 10px);
        width: 250px;
        max-height: 280px;
    }
}

/* Mobile Landscape: When sidebar is collapsed */
@media (max-width: 926px) and (orientation: landscape) {
    #sidebar.collapsed ~ #map-controls-right,
    body:has(#sidebar.collapsed) #map-controls-right {
        right: 60px;
    }
    
    #sidebar.collapsed ~ #map-controls-legend,
    body:has(#sidebar.collapsed) #map-controls-legend {
        right: 60px;
    }
    
    #sidebar.collapsed ~ #legend-panel,
    body:has(#sidebar.collapsed) #legend-panel {
        right: 60px;
    }
}

/* ===========================================
   Sidebar Header Adjustments
   =========================================== */

#sidebar-header-left {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: nowrap;
    overflow: hidden;
}

/* Ensure proper spacing when toggle is present */
#sidebar-header-left #sidebar-toggle {
    margin-right: 4px;
}

/* Multi-Polygon Tools â†’ see css/editing.css */
/* Border Access Indicators â†’ see css/borders.css */
