/* ── Reset & base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    font-size: 13px;
    color: #333;
    background: #f5f5f5;
    overflow: hidden;
}

body {
    display: flex;
    flex-direction: column;
}

/* ── Toolbar ── */
#toolbar {
    background: #fff;
    border-bottom: 1px solid #ccc;
    padding: 0 8px;
    flex-shrink: 0;
}

.menu-group {
    display: flex;
    gap: 0;
    align-items: center;
}

#trail-selector {
    margin: 0 8px 0 0;
    padding: 3px 6px;
    font-size: 12px;
    border: 1px solid #ccc;
    border-radius: 3px;
    background: #f0f0f0;
    font-weight: 600;
}

.menu-item {
    position: relative;
    padding: 6px 12px;
    cursor: pointer;
    user-select: none;
}

.menu-item:hover {
    background: #e8e8e8;
}

.menu-dropdown {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    background: #fff;
    border: 1px solid #ccc;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    min-width: 180px;
    z-index: 1100;
}

.menu-item:hover .menu-dropdown,
.menu-item.active .menu-dropdown {
    display: block;
}

.menu-dropdown a {
    display: block;
    padding: 6px 14px;
    color: #333;
    text-decoration: none;
    white-space: nowrap;
}

.menu-dropdown a:hover {
    background: #0078d4;
    color: #fff;
}

.checkbox-label,
.radio-label {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    cursor: pointer;
    white-space: nowrap;
}

/* ── Database error banner ── */
#db-error-banner {
    background: #f8d7da;
    color: #721c24;
    border-bottom: 2px solid #f5c6cb;
    padding: 10px 16px;
    font-size: 13px;
    font-weight: 600;
    flex-shrink: 0;
}

/* ── Main layout ── */
#main-layout {
    flex: 1;
    display: grid;
    grid-template-columns: 220px 1fr 280px;
    min-height: 0;
    overflow: hidden;
}

/* ── Control panel ── */
#control-panel {
    display: flex;
    flex-direction: column;
    border-right: 1px solid #ccc;
    overflow-y: auto;
    background: #fff;
}

.control-section {
    padding: 10px;
    border-bottom: 1px solid #ddd;
}

.control-section h3 {
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    color: #666;
    margin-bottom: 8px;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 2px;
    margin-bottom: 6px;
}

.control-group:last-child { margin-bottom: 0; }

.control-group > label:not(.checkbox-label) {
    font-size: 11px;
    font-weight: 600;
    color: #555;
}

.control-group select,
.control-group input[type="text"] {
    width: 100%;
    padding: 3px 6px;
    border: 1px solid #bbb;
    border-radius: 3px;
    font-size: 13px;
}

.search-row {
    display: flex;
    gap: 4px;
}

.search-row input[type="text"] {
    flex: 1;
    padding: 3px 6px;
    border: 1px solid #bbb;
    border-radius: 3px;
    font-size: 13px;
}

.search-row button {
    padding: 3px 10px;
    border: 1px solid #bbb;
    border-radius: 3px;
    background: #fff;
    cursor: pointer;
    font-size: 13px;
    white-space: nowrap;
}

.search-row button:hover { background: #e8e8e8; }

/* ── Map panel ── */
#map-panel {
    position: relative;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

#map-toolbar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 4px 8px;
    background: #f8f8f8;
    border-bottom: 1px solid #ddd;
    flex-shrink: 0;
}

#mode-label {
    color: #c00;
    font-weight: 600;
    font-size: 12px;
}

#segment-context-menu {
    position: absolute;
    z-index: 1100;
    background: #fff;
    border: 1px solid #ccc;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    min-width: 180px;
}

#segment-context-menu a {
    display: block;
    padding: 6px 14px;
    color: #333;
    text-decoration: none;
    white-space: nowrap;
    font-size: 12px;
}

#segment-context-menu a:hover {
    background: #0078d4;
    color: #fff;
}

#route-distance {
    margin-left: auto;
    font-size: 12px;
    font-weight: 600;
    color: #0066cc;
}

#map {
    flex: 1;
    min-height: 0;
}

/* ── Detail panel ── */
#detail-panel {
    border-left: 1px solid #ccc;
    overflow-y: auto;
    background: #fff;
    padding: 10px;
    display: flex;
    flex-direction: column;
}

#detail-view {
    flex: 1;
    overflow-y: auto;
}

.detail-view-name {
    font-size: 15px;
    font-weight: 700;
    margin-bottom: 4px;
    word-break: break-word;
    color: #0078d4;
}

.detail-view-coords {
    font-size: 11px;
    color: #888;
    margin-bottom: 8px;
}

.detail-view-section {
    margin-bottom: 6px;
}

.detail-view-label {
    font-size: 11px;
    font-weight: 600;
    color: #0078d4;
    text-transform: uppercase;
    margin-bottom: 1px;
}

.detail-view-value {
    font-size: 13px;
    word-break: break-word;
}

.detail-view-value a {
    color: #0078d4;
    text-decoration: none;
}

.detail-view-value a:hover {
    text-decoration: underline;
}

.detail-view-desc {
    font-size: 12px;
    white-space: pre-wrap;
    max-height: 200px;
    overflow-y: auto;
    word-break: break-word;
}

.detail-view-badge {
    display: inline-block;
    padding: 1px 6px;
    border-radius: 3px;
    font-size: 11px;
    font-weight: 600;
}

.detail-view-badge-ai { background: #d6e9f8; color: #1a4971; }
.detail-view-badge-desk { background: #fff3cd; color: #856404; }
.detail-view-badge-field { background: #d4edda; color: #155724; }
.detail-view-badge-false { background: #f8d7da; color: #721c24; }
.detail-view-badge-green { background: #d4edda; color: #000; }

.detail-view-indicators {
    display: flex;
    gap: 8px;
    margin-top: 2px;
}


.detail-view-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 3px;
}

.detail-view-tag {
    font-size: 11px;
    padding: 1px 5px;
    background: #f0f0f0;
    border-radius: 3px;
    color: #555;
}

.detail-view-sep {
    border: none;
    border-top: 1px solid #eee;
    margin: 8px 0;
}

.detail-view-actions {
    display: flex;
    gap: 6px;
    padding-top: 8px;
    border-top: 1px solid #eee;
    flex-shrink: 0;
}

.detail-view-actions button {
    padding: 5px 14px;
    border: 1px solid #bbb;
    border-radius: 3px;
    cursor: pointer;
    font-size: 13px;
    background: #fff;
}

/* ── Edit overlay ── */
#edit-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
}

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

#edit-content {
    background: #fff;
    border-radius: 6px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.3);
    width: calc(100vw - 40px);
    height: calc(100vh - 40px);
    display: flex;
    flex-direction: column;
}

#edit-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 16px;
    border-bottom: 1px solid #ddd;
    font-weight: 600;
    font-size: 14px;
    flex-shrink: 0;
}

#edit-close {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    padding: 0 4px;
    color: #666;
}

#edit-close:hover { color: #000; }

#edit-body {
    padding: 16px;
    overflow-y: auto;
    flex: 1;
    min-height: 0;
}

.edit-columns {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1.5fr 1fr;
    gap: 20px;
    height: 100%;
}

.edit-col {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.edit-col label {
    display: block;
    margin-top: 6px;
    margin-bottom: 2px;
    font-weight: 600;
    font-size: 12px;
    flex-shrink: 0;
}

.edit-col input[type="text"],
.edit-col select,
.edit-col textarea {
    width: 100%;
    padding: 4px 6px;
    border: 1px solid #bbb;
    border-radius: 3px;
    font-size: 13px;
    font-family: inherit;
}

.edit-col textarea { resize: vertical; }

.edit-col hr {
    border: none;
    border-top: 1px solid #ddd;
    margin: 8px 0;
}

.edit-col #detail-categories,
.edit-col #detail-canon-vensters {
    flex: 1;
    max-height: none;
}

.edit-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    padding: 10px 16px;
    border-top: 1px solid #ddd;
    flex-shrink: 0;
}

.edit-actions button {
    padding: 6px 16px;
    border: 1px solid #bbb;
    border-radius: 3px;
    cursor: pointer;
    font-size: 13px;
    background: #fff;
}

.checkbox-group {
    display: flex;
    flex-direction: column;
    gap: 3px;
    margin: 6px 0;
}

.button-group {
    display: flex;
    gap: 6px;
    margin-top: 12px;
}

.button-group button {
    padding: 5px 14px;
    border: 1px solid #bbb;
    border-radius: 3px;
    cursor: pointer;
    font-size: 13px;
    background: #fff;
}

.btn-primary {
    background: #0078d4 !important;
    color: #fff !important;
    border-color: #0068b8 !important;
}

.btn-primary:hover { background: #006abc !important; }

.btn-danger {
    background: #d42828 !important;
    color: #fff !important;
    border-color: #b01c1c !important;
}

.btn-danger:hover { background: #b82020 !important; }

.button-group button:not(.btn-primary):not(.btn-danger):hover {
    background: #e8e8e8;
}

/* ── Status bar ── */
#status-bar {
    padding: 4px 10px;
    background: #e8e8e8;
    border-top: 1px solid #ccc;
    font-size: 11px;
    flex-shrink: 0;
}

/* ── Modal ── */
#modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2500;
}

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

#modal-content {
    background: #fff;
    border-radius: 6px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.3);
    min-width: 400px;
    max-width: 90vw;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
}

#modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 16px;
    border-bottom: 1px solid #ddd;
    font-weight: 600;
    font-size: 14px;
}

#modal-close {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    padding: 0 4px;
    color: #666;
}

#modal-close:hover { color: #000; }

#modal-body {
    padding: 16px;
    overflow-y: auto;
    flex: 1;
}

/* ── Modal form elements ── */
.modal-form-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}

.modal-form-row label {
    min-width: 100px;
    font-weight: 600;
    font-size: 12px;
}

.modal-form-row input,
.modal-form-row select {
    flex: 1;
    padding: 4px 6px;
    border: 1px solid #bbb;
    border-radius: 3px;
    font-size: 13px;
}

.modal-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    margin-top: 16px;
}

.modal-actions button {
    padding: 6px 16px;
    border: 1px solid #bbb;
    border-radius: 3px;
    cursor: pointer;
    font-size: 13px;
    background: #fff;
}

.modal-actions button.primary {
    background: #0078d4;
    color: #fff;
    border-color: #0068b8;
}

.modal-actions button.primary:hover { background: #006abc; }
.modal-actions button:not(.primary):hover { background: #e8e8e8; }

/* ── Book/Stage management ── */
.section-header {
    font-weight: 600;
    margin: 10px 0 6px;
    font-size: 13px;
}

.item-list {
    border: 1px solid #ddd;
    border-radius: 3px;
    max-height: 150px;
    overflow-y: auto;
    margin-bottom: 8px;
}

.item-list .item {
    padding: 4px 8px;
    cursor: pointer;
    border-bottom: 1px solid #eee;
    font-size: 12px;
}

.item-list .item:last-child { border-bottom: none; }
.item-list .item:hover { background: #e8f0fe; }
.item-list .item.selected { background: #cce0ff; }

/* ── CSV mapping form ── */
.mapping-grid {
    display: grid;
    grid-template-columns: 140px 1fr;
    gap: 4px 8px;
    margin: 8px 0;
}

.mapping-grid label {
    font-size: 12px;
    font-weight: 600;
    align-self: center;
}

.mapping-grid select {
    padding: 3px 6px;
    border: 1px solid #bbb;
    border-radius: 3px;
    font-size: 12px;
}

/* ── Inline add forms ── */
.inline-form {
    display: flex;
    gap: 4px;
    align-items: center;
    margin-bottom: 6px;
    flex-wrap: wrap;
}

.inline-form input {
    padding: 3px 6px;
    border: 1px solid #bbb;
    border-radius: 3px;
    font-size: 12px;
}

.inline-form button {
    padding: 3px 10px;
    border: 1px solid #bbb;
    border-radius: 3px;
    background: #fff;
    cursor: pointer;
    font-size: 12px;
    white-space: nowrap;
}

.inline-form button:hover { background: #e8e8e8; }

/* ── Category checklist ── */
#detail-categories,
#detail-canon-vensters {
    max-height: 140px;
    overflow-y: auto;
    border: 1px solid #bbb;
    border-radius: 3px;
    padding: 4px;
}

.cat-row {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 1px 0;
    font-size: 12px;
}

.cat-row label {
    margin: 0 !important;
    font-weight: normal !important;
    cursor: pointer;
}

.cat-checkbox {
    margin: 0;
    cursor: pointer;
}

.cat-group-header {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    color: #888;
    padding: 4px 0 1px;
    border-bottom: 1px solid #eee;
    margin-top: 4px;
}

.cat-group-header:first-child {
    margin-top: 0;
}

/* ── Utility ── */
.hidden { display: none !important; }
.text-muted { color: #888; font-size: 11px; }
.mt-8 { margin-top: 8px; }
.mb-8 { margin-bottom: 8px; }

/* ── Leaflet label overrides ── */
.poi-label {
    background: none !important;
    border: none !important;
    box-shadow: none !important;
    font-size: 11px;
    font-weight: 600;
    color: #333;
    text-shadow: 1px 1px 2px #fff, -1px -1px 2px #fff, 1px -1px 2px #fff, -1px 1px 2px #fff;
    padding: 0 !important;
    margin: 0 !important;
}

.poi-label::before {
    display: none !important;
}

#map.hide-labels .poi-label {
    display: none !important;
}

/* ── Route edit markers ── */
.route-point {
    width: 10px; height: 10px;
    background: #fff;
    border: 2px solid #0066cc;
    border-radius: 50%;
    cursor: grab;
}
.route-point:active { cursor: grabbing; }
.route-point-readonly { cursor: default; pointer-events: none; }

/* ── Stage list (right panel in design mode) ── */
#stages-list { padding: 0; }
.stages-list-header { font-weight: 600; padding: 8px 10px; border-bottom: 1px solid #ddd; }
.stage-item { padding: 8px 10px; border-bottom: 1px solid #eee; cursor: pointer; }
.stage-item:hover { background: #f0f4ff; }
.stage-item-title { font-weight: 600; font-size: 13px; }
.stage-item-detail { font-size: 11px; color: #666; margin-top: 2px; }
.route-type-badge { font-size: 10px; padding: 1px 5px; border-radius: 3px; background: #e0e0e0; color: #555; margin-left: 4px; }
.route-type-badge.alt { background: #d4edda; color: #155724; }

/* ── Stage edit form (right panel) ── */
.stage-edit-header { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-bottom: 1px solid #ddd; }
.stage-edit-back { color: #0078d4; cursor: pointer; font-size: 12px; text-decoration: none; }
.stage-edit-back:hover { text-decoration: underline; }
.stage-edit-title { font-weight: 600; font-size: 13px; }
.stage-edit-form { padding: 10px; }
.stage-edit-form label { display: block; margin-top: 6px; margin-bottom: 2px; font-weight: 600; font-size: 12px; }
.stage-edit-form label.checkbox-label { display: flex; font-weight: normal; }
.stage-edit-form input, .stage-edit-form textarea, .stage-edit-form select { width: 100%; padding: 4px 6px; border: 1px solid #bbb; border-radius: 3px; font-size: 13px; font-family: inherit; }
.stage-edit-form input[type="checkbox"] { width: auto; }
.stage-edit-form textarea { resize: vertical; }
.stage-edit-form input[readonly] { background: #f0f0f0; color: #888; }

/* ── Stage edit two-column layout ── */
#main-layout.stage-edit-wide { grid-template-columns: 220px 1fr 560px; }
.stage-edit-columns { display: grid; grid-template-columns: 1fr 1fr; min-height: 0; flex: 1; overflow: hidden; }
.stage-edit-col-info { overflow-y: auto; border-right: 1px solid #ddd; }
.stage-edit-col-pois { overflow-y: auto; }

/* ── Stage POI list (within stage edit) ── */
.stage-pois-section { margin-top: 0; }
.stage-pois-header { font-weight: 600; font-size: 12px; padding: 8px 10px; border-bottom: 1px solid #eee; }
.poi-group { margin-bottom: 2px; }
.poi-group-header { font-size: 11px; font-weight: 700; text-transform: uppercase; color: #555; padding: 6px 10px 3px; border-left: 3px solid #999; margin-left: 6px; }
.poi-group-items { padding: 0 6px; }
.poi-order-item { display: flex; align-items: center; gap: 6px; padding: 4px 6px; border-bottom: 1px solid #f0f0f0; cursor: grab; font-size: 12px; }
.poi-order-item:active { cursor: grabbing; }
.poi-drag-handle { color: #bbb; font-size: 14px; cursor: grab; flex-shrink: 0; }
.poi-order-name { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.poi-order-cat { font-size: 10px; flex-shrink: 0; }
.poi-dragging { opacity: 0.4; }
.stage-poi-empty { padding: 10px; color: #888; font-size: 12px; }

/* ── Stage design mode markers ── */
.stage-trackpoint {
    width: 10px; height: 10px;
    background: #fff;
    border: 2px solid #0066cc;
    border-radius: 50%;
    cursor: pointer;
}
.stage-trackpoint-disabled { cursor: default; pointer-events: none; opacity: 0.4; }

/* ── Trail management ── */
.trail-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.trail-table th, .trail-table td { padding: 4px 8px; border-bottom: 1px solid #eee; text-align: left; }
.trail-table th { font-weight: 600; background: #f5f5f5; }
.btn-trail-edit, .btn-trail-delete { font-size: 11px; padding: 2px 8px; cursor: pointer; }
.btn-trail-delete { color: #c00; }
.trail-form label { display: block; margin-top: 8px; font-size: 12px; font-weight: 600; }
.trail-form input[type="text"], .trail-form input[type="password"] { width: 100%; padding: 4px; font-size: 12px; }
.trail-form h4 { margin-top: 12px; font-size: 13px; }
.trail-form hr { margin: 12px 0; border: none; border-top: 1px solid #ddd; }

.stage-boundary-point {
    width: 14px; height: 14px;
    background: #ff8800;
    border: 2px solid #cc6600;
    transform: rotate(45deg);
    cursor: pointer;
}

/* ── Stage chips (edit form) ── */
.stages-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin: 4px 0;
    min-height: 24px;
}

.stage-chip {
    display: inline-block;
    background: #e0ecff;
    border: 1px solid #b0c8f0;
    border-radius: 3px;
    padding: 2px 6px;
    font-size: 11px;
    white-space: nowrap;
}

.btn-stage-select {
    padding: 4px 10px;
    border: 1px solid #bbb;
    border-radius: 3px;
    background: #fff;
    cursor: pointer;
    font-size: 12px;
    margin-top: 2px;
}

.btn-stage-select:hover { background: #e8e8e8; }

/* ── Stage selection dialog ── */
.stage-dialog { display: flex; flex-direction: column; gap: 8px; }

.stage-dialog-search {
    width: 100%;
    padding: 6px 8px;
    border: 1px solid #bbb;
    border-radius: 3px;
    font-size: 13px;
}

.stage-dialog-list {
    max-height: 350px;
    overflow-y: auto;
    border: 1px solid #ddd;
    border-radius: 3px;
    padding: 4px;
}

.stage-dialog-item {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 3px 4px;
    font-size: 12px;
    cursor: pointer;
    font-weight: normal !important;
}

.stage-dialog-item:hover { background: #f0f4ff; }

.stage-dialog-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    margin-top: 4px;
}

.stage-dialog-actions button {
    padding: 6px 16px;
    border: 1px solid #bbb;
    border-radius: 3px;
    cursor: pointer;
    font-size: 13px;
    background: #fff;
}

.stage-dialog-actions button:not(.btn-primary):hover { background: #e8e8e8; }

/* ── AI Enrichment progress ── */
.ai-progress { display: flex; flex-direction: column; gap: 8px; }
.ai-progress-status { font-weight: 600; font-size: 13px; }

.ai-progress-bar-container {
    height: 8px;
    background: #e0e0e0;
    border-radius: 4px;
    overflow: hidden;
}

.ai-progress-bar {
    height: 100%;
    background: #0078d4;
    border-radius: 4px;
    transition: width 0.3s ease;
}

.ai-progress-log {
    max-height: 300px;
    overflow-y: auto;
    font-family: "Consolas", "Monaco", monospace;
    font-size: 12px;
    border: 1px solid #ddd;
    border-radius: 3px;
    padding: 6px;
    background: #fafafa;
}

.ai-log-success { color: #155724; padding: 2px 0; }
.ai-log-error { color: #721c24; padding: 2px 0; }
.ai-log-done { color: #0078d4; font-weight: 600; padding: 4px 0; }

/* ── Redact navigation bar ── */
.redact-nav {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-left: auto;
    margin-right: 8px;
}

.redact-nav-btn {
    padding: 3px 10px;
    border: 1px solid #bbb;
    border-radius: 3px;
    background: #fff;
    cursor: pointer;
    font-size: 12px;
}

.redact-nav-btn:hover { background: #e8e8e8; }

.redact-counter {
    font-size: 12px;
    font-weight: 600;
    color: #555;
    min-width: 60px;
    text-align: center;
}

.redact-stop {
    padding: 3px 10px;
    border: 1px solid #c00;
    border-radius: 3px;
    background: #fff;
    color: #c00;
    cursor: pointer;
    font-size: 12px;
    margin-left: 8px;
}

.redact-stop:hover { background: #fee; }
