/* Navigation Arrows */
.nav-arrow {
    position: absolute;
    background: rgba(0, 0, 0, 0.5);
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 4px;
    padding: 10px 15px;
    font-size: 20px;
    cursor: pointer;
    z-index: 10;
    transition: background 0.2s, transform 0.1s;
}

.nav-arrow:hover {
    background: var(--primary);
    transform: scale(1.1);
}

#nav-arrow-up {
    top: 10px;
    left: 50%;
    transform: translateX(-50%);
}

#nav-arrow-down {
    bottom: 10px;
    left: 50%;
    transform: translateX(-50%);
}

#nav-arrow-left {
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
}

#nav-arrow-right {
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
}

/* Spinner */
.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid rgba(255, 255, 255, 0.1);
    border-top: 4px solid var(--primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 20px auto;
}

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

    100% {
        transform: rotate(360deg);
    }
}

.palette-loading-overlay {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 200px;
    width: 100%;
    color: var(--text-dim);
    font-size: 14px;
}

/* Map Editor Container Styles */
.map-editor-card {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 120px);
    /* Fill available space */
    padding: 16px;
    gap: 12px;
}

.map-editor-header {
    display: flex;
    flex-direction: column;
    gap: 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    padding-bottom: 12px;
}

.map-editor-topbar {
    display: flex;
    justify-content: flex-start;
    align-items: flex-start;
    gap: 16px;
}

.map-editor-title-block {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.map-editor-kicker {
    display: inline-flex;
    align-items: center;
    width: fit-content;
    padding: 5px 9px;
    border-radius: 999px;
    background: rgba(22, 61, 112, 0.4);
    border: 1px solid rgba(102, 184, 255, 0.24);
    color: #8ccfff;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.12em;
    text-transform: uppercase;
}

.map-editor-title-block h2 {
    margin: 0;
    font-size: 24px;
    line-height: 1.05;
    color: #f4fbff;
}

.map-editor-title-block p {
    margin: 0;
    max-width: 640px;
    color: var(--text-dim);
    font-size: 13px;
    line-height: 1.45;
}

.map-editor-history-cluster {
    display: flex;
    gap: 8px;
    align-items: center;
}

.map-editor-history-inline {
    margin-top: 2px;
    justify-content: flex-end;
}

.map-editor-toolbar-grid {
    display: grid;
    grid-template-columns: minmax(420px, 1.7fr) minmax(240px, 0.95fr) minmax(300px, 1.05fr);
    gap: 10px;
    align-items: stretch;
}

.map-toolbar-card {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 12px;
    border-radius: 16px;
    background:
        linear-gradient(180deg, rgba(11, 23, 46, 0.92), rgba(6, 15, 32, 0.92));
    border: 1px solid rgba(94, 156, 225, 0.18);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04), 0 16px 36px rgba(0, 0, 0, 0.18);
}

.map-toolbar-card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
}

.map-toolbar-label {
    color: #93cfff;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.14em;
    text-transform: uppercase;
}

.map-toolbar-fields {
    display: grid;
    grid-template-columns: minmax(150px, 0.8fr) minmax(210px, 1.1fr) minmax(180px, 0.9fr);
    gap: 10px;
}

.map-toolbar-field {
    display: flex;
    flex-direction: column;
    gap: 7px;
}

.map-toolbar-field label {
    color: var(--text-dim);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.map-toolbar-field input,
.map-toolbar-field select {
    width: 100%;
    min-height: 40px;
    padding: 9px 12px;
    border-radius: 10px;
    border: 1px solid rgba(120, 186, 255, 0.18);
    background: rgba(2, 8, 20, 0.72);
    color: #f4fbff;
    font-size: 14px;
    outline: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.map-toolbar-field input:focus,
.map-toolbar-field select:focus {
    border-color: rgba(106, 204, 255, 0.55);
    box-shadow: 0 0 0 3px rgba(45, 136, 221, 0.16);
    background: rgba(4, 12, 30, 0.88);
}

.map-toolbar-field input::placeholder {
    color: rgba(197, 220, 248, 0.42);
}

.map-toolbar-toggles {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.map-toggle-chip {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-height: 38px;
    padding: 0 12px;
    border-radius: 999px;
    background: rgba(8, 19, 38, 0.78);
    border: 1px solid rgba(119, 174, 234, 0.18);
    color: #dcecff;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
}

.map-toggle-chip input {
    width: 16px;
    height: 16px;
    margin: 0;
    accent-color: #27c3ee;
}

.map-editor-mode-tabs {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.map-editor-mode-tabs .tool-btn {
    min-height: 38px;
    padding: 0 12px;
    border-radius: 10px;
}

.map-phase-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
}

.map-phase-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.map-phase-actions .secondary-btn,
.map-phase-actions .primary-btn,
.map-editor-history-cluster .secondary-btn,
.map-detail-actions .secondary-btn,
.map-detail-actions .primary-btn {
    min-height: 38px;
    padding: 0 14px;
}

.map-phase-actions-primary {
    justify-content: flex-end;
}

.map-phase-confirm {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: var(--text-dim);
    font-size: 13px;
    font-weight: 600;
}

.map-phase-confirm input {
    width: 16px;
    height: 16px;
    margin: 0;
    accent-color: #27c3ee;
}

/* Map Editor Workspace */
.map-editor-workspace {
    display: flex;
    flex: 1;
    gap: 20px;
    overflow: hidden;
}

.map-viewport {
    flex: 1;
    position: relative;
    background: #000;
    border: 2px solid rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    overflow: hidden;
    /* Prevent scrollbars from neighbors */
    display: flex;
    justify-content: center;
    align-items: center;
}

.map-grid-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    background-image:
        linear-gradient(to right, rgba(255, 255, 255, 0.05) 1px, transparent 1px),
        linear-gradient(to bottom, rgba(255, 255, 255, 0.05) 1px, transparent 1px);
    background-size: 32px 32px;
    z-index: 10;
    /* above canvas by default but we'll manage z-index carefully */
    display: none;
    /* toggleable later if wanted */
}

#me-canvas {
    image-rendering: pixelated;
    background-color: transparent;
    cursor: crosshair;
    display: block;
    margin: auto;
    /* Removed fixed dimensions to allow natural scaling with neighbors */
}

.tile-item {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 48px !important;
    height: 48px !important;
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
    cursor: pointer;
    background: #222;
    overflow: hidden;
}

.tile-item.selected {
    border: 2px solid yellow !important;
}

.tile-item canvas {
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
}

/* Tooltip */
.me-tooltip {
    position: absolute;
    background: rgba(0, 0, 0, 0.8);
    color: #fff;
    padding: 8px 12px;
    border-radius: 4px;
    font-size: 13px;
    pointer-events: none;
    z-index: 100;
    border: 1px solid rgba(255, 255, 255, 0.2);
    white-space: pre-wrap;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(4px);
}

.me-tooltip.hidden {
    display: none;
}

/* Tools Sidebar */
.map-tools-sidebar {
    width: 300px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    padding: 15px;
    display: flex;
    flex-direction: column;
    gap: 20px;
    overflow-y: auto;
}

.tool-section h3 {
    font-size: 14px;
    color: var(--text-dim);
    margin-bottom: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    padding-bottom: 5px;
}

.mode-tabs {
    display: flex;
    gap: 10px;
    align-items: center;
    background: rgba(0, 0, 0, 0.4);
    padding: 6px;
    border-radius: 6px;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.tool-btn {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: var(--text-main);
    padding: 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
    transition: all 0.2s;
}

.tool-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

.tool-btn.active {
    background: var(--primary-color);
    border-color: var(--primary-hover);
    font-weight: 500;
}

/* Object Picker */
#object-search {
    width: 100%;
    background: rgba(0, 0, 0, 0.4);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: var(--text-main);
    padding: 8px;
    border-radius: 4px;
    margin-bottom: 15px;
    outline: none;
}

#object-list-container {
    max-height: 400px;
    overflow-y: auto;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 4px;
}

.object-list-item {
    padding: 6px 8px;
    cursor: pointer;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    font-size: 12px;
}

.object-list-item:hover {
    background: rgba(255, 255, 255, 0.1);
}

.object-list-item.selected {
    background: var(--primary-color);
}


.tool-btn.active {
    background: var(--primary-color);
    border-color: var(--primary-hover);
    font-weight: 500;
}

/* Layer Toggles */
#layer-controls label {
    display: block;
    margin-bottom: 8px;
    font-size: 14px;
    cursor: pointer;
    color: var(--text-main);
}

#layer-controls input[type="checkbox"] {
    margin-right: 8px;
    accent-color: var(--primary-color);
}

#target-layer-controls select {
    width: 100%;
    background: rgba(0, 0, 0, 0.4);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: var(--text-main);
    padding: 8px;
    border-radius: 4px;
    outline: none;
}

/* Tile Picker */
#tile-search {
    width: 100%;
    background: rgba(0, 0, 0, 0.4);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: var(--text-main);
    padding: 8px;
    border-radius: 4px;
    margin-bottom: 15px;
    outline: none;
}

.tile-preview-box {
    width: 100%;
    height: 100px;
    background: rgba(0, 0, 0, 0.4);
    border: 1px dashed rgba(255, 255, 255, 0.2);
    border-radius: 4px;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    /* if tile is large */
}

.tile-preview-box img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    image-rendering: pixelated;
    /* sharp preview */
}

/* Entity Form */
#entity-form .form-group {
    margin-bottom: 12px;
}

#entity-form label {
    display: block;
    margin-bottom: 4px;
    font-size: 12px;
    color: var(--text-dim);
}

#entity-form input,
#entity-form select {
    width: 100%;
    background: rgba(0, 0, 0, 0.4);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: var(--text-main);
    padding: 8px;
    border-radius: 4px;
    outline: none;
    font-size: 13px;
}

.entity-action-toggle {
    display: inline-flex;
    gap: 6px;
    align-items: center;
    padding: 4px;
    border-radius: 999px;
    background: rgba(6, 18, 40, 0.88);
    border: 1px solid rgba(100, 165, 255, 0.2);
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.03);
}

.entity-action-btn {
    border: 1px solid rgba(110, 170, 255, 0.24);
    background: rgba(18, 32, 58, 0.88);
    color: var(--text-dim);
    border-radius: 999px;
    padding: 6px 12px;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease, transform 0.15s ease;
}

.entity-action-btn:hover {
    color: var(--text-main);
    border-color: rgba(120, 195, 255, 0.45);
}

.entity-action-btn.active {
    color: #f5fbff;
    background: linear-gradient(180deg, rgba(33, 120, 201, 0.95), rgba(16, 83, 156, 0.95));
    border-color: rgba(124, 210, 255, 0.7);
    box-shadow: 0 0 0 1px rgba(163, 228, 255, 0.22), 0 12px 24px rgba(6, 40, 88, 0.3);
    transform: translateY(-1px);
}

.entity-action-btn.entity-action-danger {
    border-color: rgba(209, 127, 64, 0.24);
}

.entity-action-btn.entity-action-danger.active {
    background: linear-gradient(180deg, rgba(155, 58, 45, 0.96), rgba(114, 27, 25, 0.96));
    border-color: rgba(255, 162, 120, 0.7);
    box-shadow: 0 0 0 1px rgba(255, 173, 135, 0.2), 0 12px 24px rgba(82, 14, 12, 0.3);
}


@media (max-width: 1400px) {
    .map-editor-toolbar-grid {
        grid-template-columns: 1fr 1fr;
    }

    .map-toolbar-card-phase,
    .map-toolbar-card-tools,
    .map-toolbar-card-wide,
    .map-toolbar-card-phase {
        grid-column: span 1;
    }

    .map-toolbar-card-wide {
        grid-column: span 2;
    }
}

@media (max-width: 980px) {
    .map-editor-topbar {
        flex-direction: column;
        align-items: stretch;
    }

    .map-editor-toolbar-grid {
        grid-template-columns: 1fr;
    }

    .map-toolbar-card-tools,
    .map-toolbar-card-wide,
    .map-toolbar-card-phase {
        grid-column: span 1;
    }

    .map-toolbar-fields {
        grid-template-columns: 1fr;
    }

    .map-phase-actions-primary {
        justify-content: stretch;
    }

    .map-phase-actions-primary .secondary-btn,
    .map-phase-actions-primary .primary-btn {
        flex: 1 1 0;
    }
}


.map-detail-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

.layer-button-row {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin-top: 4px;
}

.layer-target-btn {
    min-height: 38px;
    border-radius: 10px;
    border: 1px solid rgba(110, 170, 255, 0.24);
    background: rgba(18, 32, 58, 0.88);
    color: var(--text-dim);
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.04em;
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease, transform 0.15s ease;
}

.layer-target-btn:hover {
    color: var(--text-main);
    border-color: rgba(120, 195, 255, 0.45);
}

.layer-target-btn.active {
    color: #f5fbff;
    background: linear-gradient(180deg, rgba(33, 120, 201, 0.95), rgba(16, 83, 156, 0.95));
    border-color: rgba(124, 210, 255, 0.7);
    box-shadow: 0 0 0 1px rgba(163, 228, 255, 0.22), 0 12px 24px rgba(6, 40, 88, 0.3);
    transform: translateY(-1px);
}


.target-layer-embedded {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 10px;
    margin: 2px 0 10px;
    border-radius: 12px;
    background: rgba(7, 18, 37, 0.68);
    border: 1px solid rgba(94, 156, 225, 0.14);
}

.target-layer-embedded h3 {
    margin-bottom: 0;
    border-bottom: 0;
    padding-bottom: 0;
}
