/* ============================
   Forayz Sidebar Layout
   ============================ */

/* ============================
   1. Layout Structure
   ============================ */

/* Ensure full-height layout */
html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    overflow: hidden;
}

body.standalone-map {
    display: flex;
    flex-direction: column;
}

/* ============================
   2. Top Header
   ============================ */
.forayz-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 60px;
    background: #ffffff;
    border-bottom: 1px solid #e0e0e0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    z-index: 1000;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 15px;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

.sidebar-toggle {
    background: none;
    border: none;
    font-size: 20px;
    color: #333;
    cursor: pointer;
    padding: 8px 12px;
    border-radius: 4px;
    transition: background 0.2s, transform 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.sidebar-toggle:hover {
    background: #f5f5f5;
}

.sidebar-toggle i {
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Rotate icon when sidebar is collapsed */
body.sidebar-collapsed .sidebar-toggle i {
    transform: rotate(180deg);
}

.header-logo {
    display: flex;
    align-items: center;
}

.logo-text {
    font-size: 24px;
    font-weight: 700;
    color: #007cba;
    letter-spacing: -0.5px;
}

/* User menu in header */
.forayz-header .user-menu-container {
    position: static;
}

/* ============================
   3. Left Sidebar
   ============================ */
.forayz-sidebar {
    position: fixed;
    top: 60px;
    left: 0;
    width: 220px;
    height: calc(100vh - 60px);
    background: #ffffff;
    border-right: 1px solid #e0e0e0;
    z-index: 999;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 2px 0 8px rgba(0, 0, 0, 0.05);
    -webkit-overflow-scrolling: touch;
}

.sidebar-content {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 0;
    -webkit-overflow-scrolling: touch;
}

/* ============================
   4. Sidebar Sections
   ============================ */
.sidebar-section {
    border-bottom: 1px solid #f0f0f0;
}

.sidebar-section-title {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px 20px;
    margin: 0;
    font-size: 15px;
    font-weight: 600;
    color: #333;
    background: #fafafa;
    border-bottom: 1px solid #e8e8e8;
}

.sidebar-section-title.clickable {
    cursor: pointer;
    user-select: none;
    transition: background 0.2s;
}

.sidebar-section-title.clickable:hover {
    background: #f0f0f0;
}

.sidebar-section-title i:first-child {
    width: 20px;
    min-width: 20px;
    text-align: center;
    color: #007cba;
    font-size: 16px;
    flex-shrink: 0;
}

.sidebar-section-title .collapse-icon {
    margin-left: auto;
    font-size: 12px;
    color: #999;
    transition: transform 0.3s;
}

.sidebar-section-title.expanded .collapse-icon {
    transform: rotate(180deg);
}

.sidebar-section-content {
    padding: 16px 20px;
    overflow: visible;
    transition: max-height 0.3s ease, padding 0.3s ease, opacity 0.3s ease;
    max-height: 2000px;
    opacity: 1;
}

.sidebar-section-content.collapsed {
    max-height: 0;
    padding: 0 20px;
    overflow: hidden;
    opacity: 0;
}

/* ============================
   5. Basemap Selector (Compact Segmented Control)
   ============================ */
.basemap-selector-compact {
    display: flex;
    background: #f0f0f0;
    border-radius: 6px;
    padding: 3px;
    gap: 0;
}

.basemap-option-compact {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 10px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s;
    background: transparent;
}

.basemap-option-compact:hover {
    background: rgba(0, 124, 186, 0.1);
}

.basemap-option-compact input {
    display: none;
}

/* Selected state - white background with shadow */
.basemap-option-compact:has(input:checked) {
    background: #ffffff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
}

.basemap-option-compact .basemap-icon {
    width: 20px;
    height: 20px;
    border-radius: 3px;
    border: none;
    flex-shrink: 0;
}

.basemap-icon.streets {
    background: linear-gradient(135deg, #4CAF50, #2E7D32);
}

.basemap-icon.satellite {
    background: linear-gradient(135deg, #2196F3, #1565C0);
}

.basemap-label {
    font-size: 12px;
    font-weight: 500;
    color: #555;
    white-space: nowrap;
}

.basemap-option-compact:has(input:checked) .basemap-label {
    color: #007cba;
    font-weight: 600;
}

/* ============================
   6. Layers Container
   ============================ */
.sidebar-layers-container {
    max-height: none;
    overflow-y: visible;
}

.loading-text {
    text-align: center;
    color: #999;
    font-size: 14px;
    padding: 20px;
    font-style: italic;
}

.layer-subsection {
    margin-bottom: 20px;
}

.layer-subsection:last-child {
    margin-bottom: 0;
}

.layer-subsection-title {
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: #6c757d;
    margin: 0 0 10px 0;
    padding-bottom: 6px;
    border-bottom: 1px solid #e9ecef;
}

/* Sidebar Layer Items */
.sidebar-layer-item {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    border: 1px solid #e9ecef;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s ease;
    background: #ffffff;
    margin-bottom: 8px;
    position: relative;
    min-height: 40px;
}

.sidebar-layer-item:hover {
    border-color: #007cba;
    background: #f8fbff;
    box-shadow: 0 1px 3px rgba(0, 124, 186, 0.1);
}

.sidebar-layer-item.selected {
    border-color: #007cba;
    background: #f0f8ff;
    box-shadow: 0 2px 6px rgba(0, 124, 186, 0.15);
}

.sidebar-layer-item.pro-locked {
    opacity: 0.6;
    cursor: not-allowed;
}

.sidebar-layer-item input[type="radio"],
.sidebar-layer-item input[type="checkbox"] {
    display: none;
}

.layer-item-content {
    display: flex;
    align-items: center;
    gap: 10px;
    flex: 1;
    min-width: 0;
}

.layer-icon {
    width: 16px;
    color: #495057;
    font-size: 14px;
    flex-shrink: 0;
    text-align: center;
}

.sidebar-layer-item.selected .layer-icon {
    color: #007cba;
}

.layer-name {
    font-weight: 500;
    font-size: 14px;
    color: #212529;
    word-wrap: break-word;
}

.sidebar-layer-item.selected .layer-name {
    font-weight: 600;
    color: #007cba;
}

/* Checkbox visual indicator */
.checkbox-item .checkbox-visual {
    width: 18px;
    height: 18px;
    border: 2px solid #ccc;
    border-radius: 4px;
    position: relative;
    flex-shrink: 0;
    transition: all 0.2s ease;
}

.checkbox-item input:checked ~ .layer-item-content .checkbox-visual {
    background: #007cba;
    border-color: #007cba;
}

.checkbox-item input:checked ~ .layer-item-content .checkbox-visual::after {
    content: '✓';
    position: absolute;
    color: white;
    font-size: 12px;
    font-weight: bold;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.checkbox-item:hover .checkbox-visual {
    border-color: #007cba;
}

/* Pro indicator */
.pro-indicator {
    color: #ffd700;
    font-size: 12px;
    margin-left: 4px;
}

.pro-lock-icon {
    position: absolute;
    right: 12px;
    color: #999;
    font-size: 14px;
}

/* ============================
   7. Saved Items & Preferences
   ============================ */
.sidebar-placeholder {
    text-align: center;
    color: #999;
    font-size: 14px;
    font-style: italic;
}

/* ============================
   8. Sidebar Footer
   ============================ */
.sidebar-footer {
    padding: 16px 20px;
    /* Safe area inset for bottom - covers notches and home indicators */
    padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid #e0e0e0;
    background: #fafafa;
    /* Prevent footer from shrinking in flexbox layout */
    flex-shrink: 0;
}

.btn-upgrade {
    width: 100%;
    padding: 12px 16px;
    background: linear-gradient(135deg, #ffd700, #ffb300);
    color: #333;
    border: none;
    border-radius: 6px;
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: transform 0.2s, box-shadow 0.2s;
}

.btn-upgrade:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(255, 215, 0, 0.3);
}

.btn-upgrade i {
    font-size: 14px;
}

/* ============================
   9. Map Container
   ============================ */
#map {
    position: fixed;
    top: 60px;
    left: 0;  /* Full width - no sidebar */
    right: 0;
    bottom: 0;
    width: auto;
    height: auto;
}

/* ============================
   10. Sidebar Collapsed State (Mini Mode)
   COMMENTED OUT - Sidebar removed in favor of floating button + modal
   ============================ */
/*
body.sidebar-collapsed .forayz-sidebar {
    width: 60px;
}

body.sidebar-collapsed #map {
    left: 60px;
}
*/

/* Hide text labels in collapsed state - but NOT the icons */
body.sidebar-collapsed .sidebar-section-title > span,
body.sidebar-collapsed .basemap-label,
body.sidebar-collapsed .sidebar-placeholder,
body.sidebar-collapsed .layer-name,
body.sidebar-collapsed .collapse-icon,
body.sidebar-collapsed .btn-upgrade > span {
    display: none !important;
    opacity: 0;
    visibility: hidden;
    width: 0;
    overflow: hidden;
}

/* Center icons when collapsed */
body.sidebar-collapsed .sidebar-section-title {
    justify-content: center;
    align-items: center;
    padding: 16px 8px;
    display: flex;
    cursor: pointer;
    position: relative;
    gap: 0 !important;
}

body.sidebar-collapsed .sidebar-section-title i:first-child {
    margin: 0 !important;
    opacity: 1 !important;
    visibility: visible !important;
    width: auto !important;
    min-width: auto !important;
    display: block !important;
    color: #007cba;
    font-size: 18px;
    line-height: 1;
    text-align: center;
}

/* Hide section content when collapsed */
body.sidebar-collapsed .sidebar-section-content {
    display: none;
}

/* Hide footer text, show only icon */
body.sidebar-collapsed .sidebar-footer {
    padding: 16px 8px;
    display: flex;
    justify-content: center;
}

body.sidebar-collapsed .btn-upgrade {
    width: 44px;
    height: 44px;
    padding: 0;
    justify-content: center;
    align-items: center;
    display: flex;
}

body.sidebar-collapsed .btn-upgrade i {
    margin: 0;
    opacity: 1 !important;
    visibility: visible !important;
    display: block !important;
    font-size: 16px;
    line-height: 1;
}

/* Hover effect for collapsed sidebar items - merged with centering above */
body.sidebar-collapsed .sidebar-section-title:hover {
    background: #f0f0f0;
}

/* Tooltips for collapsed state */
body.sidebar-collapsed .sidebar-section-title::after {
    content: attr(data-tooltip);
    position: absolute;
    left: 65px;
    top: 50%;
    transform: translateY(-50%);
    background: #333;
    color: white;
    padding: 6px 12px;
    border-radius: 4px;
    white-space: nowrap;
    font-size: 13px;
    font-weight: 500;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s, visibility 0.2s;
    pointer-events: none;
    z-index: 10000;
}

body.sidebar-collapsed .sidebar-section-title:hover::after {
    opacity: 1;
    visibility: visible;
}

/* ============================
   11. Mobile Responsiveness
   ============================ */

/* Mobile Overlay - Only active on mobile, not when details sidebar is open */
.sidebar-overlay {
    position: fixed;
    top: 60px;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 998;
    opacity: 0;
    visibility: hidden;
    pointer-events: none; /* Allow clicks to pass through by default */
    transition: opacity 0.3s, visibility 0.3s;
}

/* Only show overlay on mobile when left sidebar is open */
@media only screen and (max-width: 768px) {
    body.sidebar-open:not(.details-sidebar-open) .sidebar-overlay {
        opacity: 1;
        visibility: visible;
        pointer-events: auto; /* Block clicks only on mobile for left sidebar */
    }
}

/* Mobile styles */
@media only screen and (max-width: 768px) {
    .forayz-header {
        height: 50px;
        padding: 0 12px;
    }

    .logo-text {
        font-size: 20px;
    }

    /* Sidebar on mobile - DEFAULT to collapsed (60px mini mode) until JS loads */
    /* This prevents the sidebar from covering the screen before JavaScript runs */
    .forayz-sidebar {
        top: 50px;
        /* Use dvh (dynamic viewport height) for mobile Safari - respects browser chrome */
        /* Fallback to vh for older browsers, then override with dvh if supported */
        height: calc(100vh - 50px);
        height: calc(100dvh - 50px);
        width: 60px; /* Start collapsed by default on mobile */
        box-shadow: 4px 0 16px rgba(0, 0, 0, 0.15);
        transform: translateX(0);
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

    /* When sidebar is collapsed on mobile, show mini mode (60px width with icons) */
    body.sidebar-collapsed .forayz-sidebar {
        width: 60px;
        transform: translateX(0);
        visibility: visible;
    }

    /* When sidebar is open on mobile, show narrower width so map is visible */
    body.sidebar-open .forayz-sidebar {
        width: 50%;
        max-width: 200px;
        transform: translateX(0);
    }

    /* Default: Hide section content on mobile until sidebar is opened */
    .sidebar-section-content {
        display: none;
    }

    /* Show content only when sidebar is explicitly opened */
    body.sidebar-open .sidebar-section-content {
        display: block;
    }

    /* Map takes full width on mobile - no sidebar */
    #map {
        top: 50px;
        left: 0;  /* Full width */
        right: 0;
        bottom: 0;
    }

    /* Keep mini-sidebar styles on mobile when collapsed - but show icons */
    body.sidebar-collapsed .sidebar-section-title > span,
    body.sidebar-collapsed .basemap-label,
    body.sidebar-collapsed .layer-name,
    body.sidebar-collapsed .collapse-icon,
    body.sidebar-collapsed .btn-upgrade > span {
        display: none !important;
        opacity: 0;
        visibility: hidden;
        width: 0;
        overflow: hidden;
    }

    /* Center icons when collapsed on mobile */
    body.sidebar-collapsed .sidebar-section-title {
        justify-content: center;
        align-items: center;
        padding: 14px 8px;
        background: #fafafa;
        display: flex;
        gap: 0 !important;
    }

    body.sidebar-collapsed .sidebar-section-title i:first-child {
        margin: 0 !important;
        opacity: 1 !important;
        visibility: visible !important;
        width: auto !important;
        min-width: auto !important;
        display: block !important;
        color: #007cba;
        font-size: 18px;
        line-height: 1;
        text-align: center;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }

    /* Hide section content when collapsed on mobile */
    body.sidebar-collapsed .sidebar-section-content {
        display: none;
    }

    /* Footer icon-only when collapsed */
    body.sidebar-collapsed .sidebar-footer {
        padding: 14px 8px;
        display: flex;
        justify-content: center;
    }

    body.sidebar-collapsed .btn-upgrade {
        width: 44px;
        height: 44px;
        padding: 0;
        justify-content: center;
        align-items: center;
        display: flex;
    }

    body.sidebar-collapsed .btn-upgrade i {
        margin: 0;
        opacity: 1 !important;
        visibility: visible !important;
        display: block !important;
        font-size: 16px;
        line-height: 1;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }

    /* When sidebar is open (full), show all content normally */
    body.sidebar-open .sidebar-section-title,
    body.sidebar-open .sidebar-section-content,
    body.sidebar-open .sidebar-section-title span,
    body.sidebar-open .basemap-label,
    body.sidebar-open .btn-upgrade span {
        opacity: 1;
        visibility: visible;
        width: auto;
        display: block;
    }

    body.sidebar-open .sidebar-section-title {
        justify-content: flex-start;
        padding: 14px 16px;
    }

    body.sidebar-open .sidebar-section-content {
        display: block;
    }

    body.sidebar-open .sidebar-footer {
        padding: 14px 16px;
    }

    body.sidebar-open .btn-upgrade {
        width: 100%;
        height: auto;
        padding: 12px 16px;
    }

    /* No tooltips on mobile */
    body.sidebar-collapsed .sidebar-section-title::after {
        display: none;
    }

    /* Sidebar sections more compact when open */
    .sidebar-section-title {
        padding: 14px 16px;
        font-size: 14px;
    }

    .sidebar-section-content {
        padding: 14px 16px;
    }

    /* Stack basemap buttons vertically on mobile to save horizontal space */
    .basemap-selector-compact {
        flex-direction: column;
        gap: 6px;
    }

    .basemap-option-compact {
        justify-content: flex-start;
        padding: 10px 12px;
    }

    .sidebar-footer {
        padding: 14px 16px;
        /* Extra bottom padding for Safari navigation bar - needs ~100px to clear nav bar */
        padding-bottom: calc(14px + max(100px, env(safe-area-inset-bottom, 100px)));
    }

    /* Header user menu adjustments */
    .forayz-header #user-menu #login-btn,
    .forayz-header #user-menu #signup-btn {
        padding: 6px 12px;
        font-size: 13px;
    }
}

/* Tablet - REMOVED: Left sidebar was removed in favor of floating button + modal
   The forayz-sidebar element no longer exists, so these rules were causing
   a blank left gap on the map at tablet widths (769-1024px). */

/* ============================
   12. Details Sidebar Adjustments
   ============================ */
/* Ensure details sidebar appears above map */
#details-sidebar {
    top: 60px;
    max-height: calc(100vh - 60px);
    z-index: 1001;
}

@media only screen and (max-width: 768px) {
    #details-sidebar {
        top: 50px;
        max-height: calc(100vh - 50px);
    }
}

/* Summary text preserves line breaks from \n in data */
.summary-text {
    white-space: pre-line;
}

/* ============================
   13. Animation & Transitions
   ============================ */
@keyframes slideInFromLeft {
    from {
        transform: translateX(-100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

.forayz-sidebar.animating {
    animation: slideInFromLeft 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ============================
   14. Accessibility
   ============================ */
.sidebar-toggle:focus,
.sidebar-section-title.clickable:focus {
    outline: 2px solid #007cba;
    outline-offset: 2px;
}

/* High contrast mode */
@media (prefers-contrast: high) {
    .forayz-header,
    .forayz-sidebar {
        border-color: #000;
    }

    .sidebar-section-title {
        border-color: #000;
    }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
    .forayz-sidebar,
    #map,
    .sidebar-overlay,
    .sidebar-section-content {
        transition: none;
    }
}
