/* Premium Modern Design with Glassmorphism */
:root {
    --primary: #2ECC71;
    --primary-hover: #27AE60;
    --bg-glass: rgba(20, 25, 30, 0.7);
    --border-glass: rgba(255, 255, 255, 0.1);
    --text-light: #F8FAFC;
    --text-muted: #94A3B8;
    --font-heading: 'Inter', sans-serif;
    --font-body: 'Noto Sans TC', sans-serif;
}

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

html,
body {
    max-width: 100%;
    overflow-x: hidden;
    /* 關鍵屬性：contain 確保滾動到達邊界時，不會傳遞給瀏覽器觸發重新整理 */
    overscroll-behavior-y: contain;
    /* 確保高度撐滿，讓滾動發生在 body 內部 */
    height: 100%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

body {
    font-family: var(--font-body);
    color: var(--text-light);
    line-height: 1.6;
    min-height: 100vh;
}

.background-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    /* Direct URL for a beautiful mountain scenery from Unsplash */
    background: url('https://images.unsplash.com/photo-1464822759023-fed622ff2c3b?q=80&w=2940&auto=format&fit=crop') no-repeat center center fixed;
    background-size: cover;
    z-index: -2;
}

.background-overlay::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(to bottom, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.8));
    z-index: -1;
}

.navbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.8rem 3%;
    background: rgba(10, 15, 20, 0.9);
    backdrop-filter: blur(20px);
    border-bottom: 1px solid var(--border-glass);
    position: sticky;
    top: 0;
    z-index: 1000;
    transition: all 0.3s ease;
}

/* Ensure navbar is strictly fixed and on top during AR modes as requested by user */
/* We use a very high z-index (50000) because AR peak labels can go up to 10000 */
body:has(#ar-page.active) .navbar,
body:has(#ghost-path.active) .navbar {
    position: fixed;
    width: 100%;
    z-index: 50000;
    background: rgba(10, 15, 20, 0.95);
    backdrop-filter: blur(25px);
    box-shadow: 0 4px 30px rgba(0, 0, 0, 0.5);
    opacity: 1 !important;
    visibility: visible !important;
    pointer-events: auto !important;
}

body:has(#ar-page.active) .navbar *,
body:has(#ghost-path.active) .navbar * {
    pointer-events: auto !important;
}

.nav-menu {
    display: flex;
    align-items: center;
    flex: 2;
    justify-content: flex-end;
}

.nav-links {
    list-style: none;
    display: flex;
    gap: 0.5rem;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

.nav-links li {
    perspective: 1000px;
}

.nav-links a {
    color: var(--text-light);
    text-decoration: none;
    font-weight: 600;
    font-size: 0.9rem;
    padding: 0.6rem 0.85rem;
    border-radius: 50px;
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    white-space: nowrap;
    cursor: pointer;
    z-index: 5;
}

.nav-links a:hover {
    background: rgba(46, 204, 113, 0.15);
    border-color: var(--primary);
    color: var(--primary);
    transform: translateY(-3px) scale(1.05);
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
}

.nav-links a.active {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
    box-shadow: 0 4px 15px rgba(46, 204, 113, 0.4);
}

.mobile-menu-toggle {
    display: none;
    background: none;
    border: none;
    color: white;
    font-size: 1.8rem;
    cursor: pointer;
    z-index: 1001;
}

.lang-dropdown {
    position: relative;
    padding-left: 1.2rem;
    border-left: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    align-items: center;
}

.lang-dropbtn {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: white;
    font-size: 1.3rem;
    cursor: pointer;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    outline: none;
}

.lang-dropbtn:hover {
    background: rgba(46, 204, 113, 0.15);
    border-color: var(--primary);
    transform: scale(1.05);
    box-shadow: 0 0 15px rgba(46, 204, 113, 0.2);
}

.lang-dropdown-content {
    display: none;
    position: absolute;
    right: 0;
    top: calc(100% + 15px);
    background: rgba(15, 25, 35, 0.95);
    backdrop-filter: blur(20px);
    min-width: 190px;
    border-radius: 14px;
    border: 1px solid rgba(255, 255, 255, 0.12);
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
    z-index: 2000;
    padding: 0.6rem 0;
    overflow: hidden;
}

.lang-dropdown-content.show {
    display: block;
    animation: dropdownPop 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes dropdownPop {
    from {
        opacity: 0;
        transform: translateY(10px) scale(0.95);
    }

    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.logo {
    flex: 1;
    font-size: 1.4rem;
    font-weight: 800;
    font-family: var(--font-heading);
    letter-spacing: 1px;
    background: linear-gradient(90deg, #F8FAFC, #2ECC71);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    white-space: nowrap;
    cursor: pointer;
    z-index: 10;
}

.lang-flag {
    padding: 0.8rem 1.4rem;
    display: flex;
    align-items: center;
    gap: 12px;
    color: rgba(255, 255, 255, 0.85);
    cursor: pointer;
    transition: all 0.2s ease;
    font-size: 0.95rem;
    white-space: nowrap;
}

.lang-flag:hover {
    background: rgba(255, 255, 255, 0.08);
    color: var(--primary);
    padding-left: 1.6rem;
}

.lang-flag.active {
    background: rgba(46, 204, 113, 0.12);
    color: var(--primary);
    font-weight: 600;
}

main {
    padding: 2rem 2%;
    max-width: 100%;
    margin: 0 auto;
}

.page-section {
    display: none;
    animation: fadeIn 0.5s ease-out forwards;
}

.page-section:not(#calculator):not(#simulation):not(#ar-page) {
    max-width: 1400px;
    margin: 0 auto;
}

#ar-page {
    max-width: none !important;
    padding: 0 !important;
    margin: 0 !important;
    width: 100vw;
    height: 100dvh;
    overflow: hidden;
    animation: none !important;
}

.page-section.active {
    display: block;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(15px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.page-title {
    font-family: var(--font-heading);
    font-size: 2.5rem;
    margin-bottom: 2rem;
    text-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
    border-left: 5px solid var(--primary);
    padding-left: 1rem;
}

.glass-panel {
    background: var(--bg-glass);
    backdrop-filter: blur(16px);
    border: 1px solid var(--border-glass);
    border-radius: 16px;
    padding: 2rem;
    margin-bottom: 2rem;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    transition: transform 0.3s, box-shadow 0.3s;
}

.glass-panel h2 {
    margin-bottom: 1.5rem;
    color: var(--primary);
    font-size: 1.7rem;
    border-bottom: 1px solid var(--border-glass);
    padding-bottom: 0.5rem;
}

.glass-panel ul {
    list-style-position: inside;
    margin-bottom: 1rem;
}

.glass-panel li {
    margin-bottom: 0.8rem;
    font-size: 1.1rem;
}

.glass-panel strong {
    color: #fff;
}

/* Tables */
.table-title {
    margin-top: 2rem;
    margin-bottom: 1rem;
    text-align: center;
    color: #ffd700;
}

.table-container {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 8px;
    overflow: hidden;
}

th,
td {
    padding: 1rem;
    text-align: left;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

th {
    background: rgba(0, 0, 0, 0.4);
    font-weight: 600;
}

tr:hover {
    background: rgba(255, 255, 255, 0.08);
}

td a {
    color: var(--primary);
    text-decoration: none;
}

td a:hover {
    text-decoration: underline;
}

/* Grids */
.layout-grid {
    display: grid;
    grid-template-columns: 350px 1fr;
    gap: 1.5rem;
}

.layout-grid.reverse {
    grid-template-columns: 350px 1fr;
}

.control-panel {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.map-panel {
    min-height: 75vh;
}



.file-input {
    width: 100%;
    padding: 0.5rem;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 6px;
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.2);
    margin-top: 0.5rem;
}

@media (max-width: 1150px) {

    .layout-grid,
    .layout-grid.reverse {
        grid-template-columns: 1fr;
    }
}

/* Forms & Buttons */
.form-group {
    margin-bottom: 1.5rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 600;
}

select {
    width: 100%;
    padding: 0.8rem;
    border-radius: 8px;
    border: 1px solid var(--border-glass);
    background: rgba(0, 0, 0, 0.5);
    color: #fff;
    font-size: 1rem;
    outline: none;
    transition: border 0.3s;
}

select:focus {
    border-color: var(--primary);
}

.btn {
    padding: 0.8rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
}

.primary-btn {
    background: var(--primary);
    color: #fff;
    width: 100%;
}

.primary-btn:hover {
    background: var(--primary-hover);
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(46, 204, 113, 0.4);
}

.secondary-btn {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
    border: 1px solid var(--border-glass);
}

.secondary-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* Playback Speed Controls */
.speed-controls {
    display: flex;
    gap: 0.5rem;
}

.speed-btn {
    flex: 1;
    background: rgba(255, 255, 255, 0.05);
    color: #fff;
    border: 1px solid var(--border-glass);
}

.speed-btn.active,
.speed-btn:hover {
    background: var(--primary);
    border-color: var(--primary);
}

.mt-4 {
    margin-top: 2rem;
}

/* Map Container */
.map-panel {
    padding: 0;
    overflow: hidden;
}

#map {
    width: 100%;
    height: 60vh;
    min-height: 400px;
    background: #1e1e1e;
}

/* Weather Container */
.weather-container {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.weather-day {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(0, 0, 0, 0.3);
    padding: 1rem;
    border-radius: 8px;
    border-left: 4px solid var(--primary);
}

.weather-icon {
    font-size: 1.8rem;
}

.weather-info {
    flex: 1;
    margin-left: 1rem;
}

.weather-date {
    font-weight: bold;
    display: block;
}

.weather-temp {
    color: var(--text-muted);
    font-size: 0.9rem;
}

.hidden {
    display: none !important;
}

.calc-grid {
    display: grid;
    grid-template-columns: 350px 1fr 350px;
    gap: 1.5rem;
}

/* --- Responsive Media Queries --- */

/* Laptops & Large Tablets */
@media (max-width: 1200px) {
    .calc-grid {
        grid-template-columns: 1fr 1fr;
    }

    /* Move weather to its own row or bottom later */
    .calc-grid>.glass-panel:last-child {
        grid-column: span 2;
    }
}

/* Small Tablets & Large Phones */
@media (max-width: 1150px) {
    main {
        padding: 1.5rem 4%;
    }

    .page-title {
        font-size: 2rem;
        margin-bottom: 1.5rem;
    }

    .navbar {
        padding: 0.8rem 4%;
    }

    .mobile-menu-toggle {
        display: block;
    }

    .nav-menu {
        position: fixed;
        top: 0;
        right: -100%;
        width: 80%;
        max-width: 300px;
        height: 100vh;
        background: rgba(10, 15, 20, 0.98);
        backdrop-filter: blur(25px);
        flex-direction: column;
        justify-content: center;
        align-items: center;
        gap: 2rem;
        transition: right 0.4s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: -10px 0 30px rgba(0, 0, 0, 0.5);
        padding: 2rem;
        z-index: 1000;
    }

    .nav-menu.active {
        right: 0;
    }

    .nav-links {
        position: static;
        transform: none;
        flex-direction: column;
        width: 100%;
        gap: 1.2rem;
        align-items: center;
    }

    .nav-links li {
        width: 100%;
    }

    .nav-links a {
        justify-content: center;
    }

    .lang-dropdown {
        border-left: none;
        padding-left: 0;
        width: 100%;
        justify-content: center;
        margin-top: 1rem;
    }

    .lang-dropdown-content {
        position: fixed;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%) !important;
        width: 85%;
        max-width: 320px;
        max-height: 80vh;
        overflow-y: auto;
        animation: mobilePop 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

    @keyframes mobilePop {
        from {
            opacity: 0;
            transform: translate(-50%, -40%) scale(0.9);
        }

        to {
            opacity: 1;
            transform: translate(-50%, -50%) scale(1);
        }
    }

    .lang-flag {
        padding: 1.2rem 2rem;
        font-size: 1.1rem;
        border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    }

    .lang-flag:last-child {
        border-bottom: none;
    }

    /* Remove the non-interactive pseudo-element */
    .nav-links::after {
        display: none;
    }

    .layout-grid,
    .layout-grid.reverse {
        grid-template-columns: 1fr;
    }

    .calc-grid {
        grid-template-columns: 1fr;
    }

    .calc-grid>.glass-panel:last-child {
        grid-column: span 1;
    }

    .map-panel {
        order: -1;
        /* Show map at top on simulation */
    }

    #map {
        height: 40vh;
    }
}

/* Small Phones */
@media (max-width: 500px) {
    main {
        padding: 1rem 3%;
    }

    .page-title {
        font-size: 1.6rem;
    }

    .glass-panel {
        padding: 1.2rem;
    }

    .logo {
        font-size: 1.2rem;
    }

    .btn {
        width: 100% !important;
        padding: 1rem;
    }

    #calc-peak-map {
        min-height: 250px;
    }
}

/* Leaflet Customizations */
.route-node-tooltip {
    background: rgba(0, 0, 0, 0.8) !important;
    border: 1px solid var(--primary) !important;
    color: white !important;
    font-weight: 600 !important;
    border-radius: 4px !important;
    padding: 2px 6px !important;
    font-size: 0.8rem !important;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5) !important;
    white-space: nowrap !important;
}

.route-node-tooltip::before {
    display: none !important;
    /* Hide the arrow */
}

/* Simulation Info Box */
.sim-wpt-list {
    margin-top: 10px;
    max-height: 200px;
    overflow-y: auto;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    padding-top: 10px;
}

.sim-wpt-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px;
    cursor: pointer;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    transition: background 0.2s;
}

.sim-wpt-item:hover {
    background: rgba(46, 204, 113, 0.2);
}

/* GPX 地圖標記名稱標籤 (Waypoint name label tooltip on map) */
.sim-label-tooltip {
    background: rgba(0, 0, 0, 0.8) !important;
    border: 1px solid rgba(255, 255, 255, 0.3) !important;
    color: #ffffff !important;
    font-size: 12px !important;
    font-weight: 600 !important;
    padding: 3px 8px !important;
    border-radius: 6px !important;
    box-shadow: 0 3px 10px rgba(0, 0, 0, 0.6) !important;
    white-space: nowrap !important;
    letter-spacing: 0.3px;
}

.sim-label-tooltip::before {
    border-right-color: rgba(0, 0, 0, 0.8) !important;
}

.sim-wpt-item .wpt-icon {
    width: 20px;
    text-align: center;
}

.sim-wpt-item .wpt-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.85rem;
}

/* Map Selection Mode Styles */
#map-select-canvas {
    background: #1e1e1e;
}

#map-select-itinerary,
#map-select-weather {
    scrollbar-width: thin;
    scrollbar-color: var(--primary) transparent;
}

/* --- BWMesh Custom Styles --- */
.bw-msg-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

@media (max-width: 600px) {
    .bw-msg-grid {
        grid-template-columns: 1fr;
    }

    .bw-msg-quick-btn {
        padding: 6px 14px !important;
        /* Increase tap area for mobile */
        font-size: 0.8rem !important;
    }
}

.bw-section-title {
    color: #ffd700;
    font-size: 0.85rem;
    font-weight: bold;
    margin-bottom: 8px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    padding-bottom: 4px;
}

#bw-msg-list::-webkit-scrollbar,
#bw-event-log::-webkit-scrollbar {
    width: 4px;
}

#bw-msg-list::-webkit-scrollbar-thumb,
#bw-event-log::-webkit-scrollbar-thumb {
    background: rgba(155, 89, 182, 0.3);
    border-radius: 2px;
}

#map-select-itinerary::-webkit-scrollbar,
#map-select-weather::-webkit-scrollbar {
    width: 6px;
}

#map-select-itinerary::-webkit-scrollbar-thumb,
#map-select-weather::-webkit-scrollbar-thumb {
    background-color: var(--primary);
    border-radius: 10px;
}

.itinerary-item {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 10px;
    padding: 0.8rem;
    margin-bottom: 0.8rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: relative;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
    animation: slideInMapItem 0.3s ease-out;
}

@keyframes slideInMapItem {
    from {
        opacity: 0;
        transform: translateX(-20px);
    }

    to {
        opacity: 1;
        transform: translateX(0);
    }
}

.itinerary-item:hover {
    background: rgba(255, 255, 255, 0.12);
    border-color: var(--primary);
    transform: translateY(-2px);
}

.itinerary-item .item-info {
    flex: 1;
}

.itinerary-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.itinerary-item .item-name {
    font-weight: bold;
    display: block;
    color: var(--primary);
    font-size: 0.95rem;
}

.itinerary-item .item-type {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.itinerary-item .item-index {
    position: absolute;
    top: -5px;
    left: -5px;
    background: var(--primary);
    color: white;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    font-weight: bold;
    border: 2px solid #000;
    z-index: 5;
}

.delete-item-btn {
    background: rgba(231, 76, 60, 0.6);
    color: white;
    border: none;
    width: 26px;
    height: 26px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    transition: all 0.2s;
    margin-left: 0.5rem;
}

.delete-item-btn:hover {
    background: rgba(231, 76, 60, 1);
    transform: scale(1.1);
}

.map-label-tooltip {
    background: rgba(0, 0, 0, 0.75) !important;
    border: 1px solid var(--primary) !important;
    color: white !important;
    font-weight: 600 !important;
    border-radius: 4px !important;
    padding: 2px 6px !important;
    font-size: 0.75rem !important;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5) !important;
    white-space: nowrap !important;
}

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

.custom-map-marker {
    background: transparent !important;
    border: none !important;
    pointer-events: auto !important;
}

.custom-map-marker div {
    transform: translate(-50%, -100%);
    pointer-events: auto;
    cursor: pointer;
    transition: transform 0.2s;
}

.custom-map-marker div:hover {
    transform: translate(-50%, -110%) scale(1.1);
    z-index: 1000;
}

/* Adjust Map Selection Layer Control Position */
#map-select-canvas .leaflet-top.leaflet-left {
    margin-left: 310px;
    /* Clear the Itinerary Panel (280px + margin) */
    margin-top: 10px;
}

/* 模式切換按鈕 - 啟用狀態強化 */
.active-tab-btn {
    background: var(--primary) !important;
    color: white !important;
    font-weight: 800 !important;
    box-shadow: 0 0 20px rgba(46, 204, 113, 0.5), inset 0 0 10px rgba(255, 255, 255, 0.2) !important;
    border: 2px solid #fff !important;
    transform: translateY(-2px) scale(1.05) !important;
    letter-spacing: 0.5px;
    z-index: 10;
}