/* ===== Reset & Layout ===== */
* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    display: flex;
    height: 100vh;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    overflow: hidden;
}

/* ===== Sidebar ===== */
#sidebar {
    width: 360px;
    min-width: 360px;
    height: 100vh;
    overflow-y: auto;
    background: #1a1a2e;
    color: #e0e0e0;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    z-index: 1000;
    scrollbar-width: thin;
    scrollbar-color: #444 #1a1a2e;
}

#sidebar::-webkit-scrollbar { width: 6px; }
#sidebar::-webkit-scrollbar-track { background: #1a1a2e; }
#sidebar::-webkit-scrollbar-thumb { background: #444; border-radius: 3px; }

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

#theme-toggle {
    background: transparent;
    border: 1px solid #3a3a5c;
    color: #ccc;
    font-size: 1.2em;
    width: 32px;
    height: 32px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

#theme-toggle:hover {
    border-color: #4fc3f7;
    color: #4fc3f7;
}

.sidebar-header h1 {
    font-size: 1.35em;
    color: #4fc3f7;
    margin-bottom: 2px;
}

.sidebar-header .subtitle {
    font-size: 0.82em;
    color: #777;
    font-style: italic;
}

.sidebar-footer {
    margin-top: auto;
    padding-top: 12px;
    border-top: 1px solid #333;
    font-size: 0.72em;
    color: #555;
    line-height: 1.6;
}

/* ===== Sections ===== */
.section {
    background: rgba(255,255,255,0.04);
    border-radius: 8px;
    padding: 14px 16px;
}

.section h3 {
    font-size: 0.85em;
    color: #4fc3f7;
    margin-bottom: 10px;
    text-transform: uppercase;
    letter-spacing: 1.2px;
    font-weight: 600;
}

.hint {
    font-size: 0.83em;
    color: #777;
}

.coord-display {
    font-family: 'Courier New', Consolas, monospace;
    font-size: 0.92em;
    color: #ccc;
    letter-spacing: 0.5px;
}

/* ===== Compass ===== */
.compass-container {
    display: flex;
    justify-content: center;
    margin-bottom: 8px;
    user-select: none;
}

#compass { cursor: pointer; }

.arc-vis { transition: stroke 0.15s; }
.arc-btn:hover .arc-vis { stroke: #4fc3f7; }

/* Map rotation arrows */
.map-arrows-overlay { background: none !important; border: none !important; pointer-events: auto !important; }
.map-arc-vis { transition: stroke 0.15s; }
.map-arc-btn:hover .map-arc-vis { stroke: #4fc3f7 !important; }

/* ===== Inputs ===== */
.input-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
}

.input-row:last-child { margin-bottom: 0; }

.input-row label {
    font-size: 0.84em;
    color: #bbb;
}

.input-row input[type="number"] {
    width: 80px;
    padding: 6px 8px;
    border: 1px solid #3a3a5c;
    border-radius: 5px;
    background: #0f0f23;
    color: #eee;
    font-size: 0.9em;
    text-align: right;
    transition: border-color 0.2s;
}

.input-row input:focus {
    outline: none;
    border-color: #4fc3f7;
}

/* ===== Buttons ===== */
#calculate-btn {
    width: 100%;
    padding: 13px;
    background: linear-gradient(135deg, #4fc3f7, #29b6f6);
    color: #0d1b2a;
    border: none;
    border-radius: 8px;
    font-size: 1em;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.2s;
    letter-spacing: 0.5px;
}

#calculate-btn:hover:not(:disabled) {
    background: linear-gradient(135deg, #81d4fa, #4fc3f7);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(79,195,247,0.3);
}

#calculate-btn:disabled {
    background: #2a2a4a;
    color: #555;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

#clear-btn {
    width: 100%;
    padding: 10px;
    background: transparent;
    color: #888;
    border: 1px solid #444;
    border-radius: 8px;
    font-size: 0.88em;
    cursor: pointer;
    transition: all 0.2s;
    margin-top: -4px;
}

#clear-btn:hover {
    color: #ff5252;
    border-color: #ff5252;
}

/* ===== Floating Results Window ===== */
#results-window {
    position: fixed;
    top: 80px;
    right: 40px;
    width: 380px;
    z-index: 1500;
    background: #f5f5f5;
    color: #333;
    border-radius: 8px;
    box-shadow: 0 6px 30px rgba(0,0,0,0.3), 0 0 0 1px rgba(0,0,0,0.08);
    display: flex;
    flex-direction: column;
    overflow: visible;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.results-window-titlebar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 14px;
    background: #e0e0e0;
    cursor: grab;
    user-select: none;
    border-bottom: 1px solid #ccc;
    flex-shrink: 0;
}

.results-window-titlebar:active {
    cursor: grabbing;
}

.results-window-title {
    font-size: 0.85em;
    font-weight: 600;
    color: #444;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.results-window-actions {
    display: flex;
    gap: 4px;
    align-items: center;
}

#results-window-minimize,
#results-window-close {
    background: transparent;
    border: none;
    color: #888;
    font-size: 1em;
    cursor: pointer;
    width: 24px;
    height: 24px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
}

#results-window-minimize:hover {
    background: #bbb;
    color: #333;
}

#results-window-close:hover {
    background: #d32f2f;
    color: #fff;
}

/* Minimized state */
#results-window.minimized .results-window-body {
    display: none;
}

#results-window.minimized {
    border-radius: 8px;
}

.results-window-body {
    padding: 14px;
    overflow: visible;
}

/* ===== Results ===== */
.effective-fetch {
    text-align: center;
    padding: 14px;
    background: rgba(2,119,189,0.08);
    border: 1px solid rgba(2,119,189,0.2);
    border-radius: 8px;
    margin-bottom: 14px;
}

.effective-fetch .label {
    font-size: 0.75em;
    color: #888;
    text-transform: uppercase;
    letter-spacing: 1.5px;
    margin-bottom: 4px;
}

.effective-fetch .value {
    font-size: 2em;
    font-weight: 700;
    color: #0277bd;
    line-height: 1.2;
}

.effective-fetch .unit {
    font-size: 0.85em;
    color: #999;
}

.results-table-wrap {
    overflow: visible;
}

#results-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8em;
}

#results-table th {
    text-align: right;
    padding: 6px 8px;
    border-bottom: 1px solid #ddd;
    color: #777;
    font-weight: 500;
    background: rgba(245,245,245,0.95);
}

#results-table th:first-child { text-align: left; }

#results-table td {
    padding: 4px 8px;
    border-bottom: 1px solid rgba(0,0,0,0.06);
    text-align: right;
    font-family: 'Courier New', Consolas, monospace;
    font-size: 0.95em;
    color: #444;
}

#results-table td:first-child { text-align: left; }

#results-table tr.central-ray td {
    color: #d32f2f;
    font-weight: 700;
}

#results-table tr:hover td {
    background: rgba(2,119,189,0.06);
}

#results-table tfoot td {
    border-top: 2px solid #0277bd;
    font-weight: 700;
    color: #0277bd;
    padding: 8px;
}

/* ===== Fetch Formula ===== */
.fetch-formula {
    margin-top: 10px;
    padding: 8px 10px;
    background: rgba(0,0,0,0.03);
    border-radius: 6px;
    border: 1px solid rgba(0,0,0,0.06);
}

.formula-line {
    font-size: 0.75em;
    color: #666;
    line-height: 1.7;
    font-family: 'Courier New', Consolas, monospace;
}

.formula-line strong {
    color: #0277bd;
}

/* ===== Map ===== */
#map {
    flex: 1;
    height: 100vh;
}

/* ===== Base Layer Opacity Slider ===== */
#opacity-control {
    position: fixed;
    top: 12px;
    left: calc(360px + (100vw - 360px) / 2);
    transform: translateX(-50%);
    z-index: 1000;
    background: none;
    padding: 6px 16px;
}

#opacity-control input[type="range"] {
    width: 160px;
    cursor: pointer;
    accent-color: #666;
}

/* ===== Toast Notifications ===== */
#toast-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 3000;
    display: flex;
    flex-direction: column-reverse;
    gap: 8px;
    max-width: 380px;
    pointer-events: none;
}

.toast {
    padding: 12px 16px;
    border-radius: 8px;
    font-size: 0.85em;
    line-height: 1.5;
    color: #fff;
    pointer-events: auto;
    opacity: 0;
    transform: translateX(30px);
    transition: opacity 0.3s, transform 0.3s;
    box-shadow: 0 4px 16px rgba(0,0,0,0.35);
}

.toast-visible {
    opacity: 1;
    transform: translateX(0);
}

.toast b { display: block; margin-bottom: 2px; }
.toast code {
    background: rgba(255,255,255,0.15);
    padding: 1px 5px;
    border-radius: 3px;
    font-size: 0.92em;
}
.toast small { opacity: 0.8; }

.toast-info {
    background: rgba(25,118,210,0.92);
    border-left: 4px solid #64b5f6;
}

.toast-warn {
    background: rgba(230,126,34,0.92);
    border-left: 4px solid #f39c12;
}

.toast-error {
    background: rgba(211,47,47,0.92);
    border-left: 4px solid #ff5252;
}

.toast-success {
    background: rgba(46,125,50,0.92);
    border-left: 4px solid #66bb6a;
}

/* ===== Loading Overlay ===== */
#loading-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.75);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    color: #fff;
    font-size: 1.1em;
}

.spinner {
    width: 48px;
    height: 48px;
    border: 4px solid rgba(79,195,247,0.25);
    border-top-color: #4fc3f7;
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
    margin-bottom: 18px;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Map Labels ===== */
.fetch-label-anchor {
    background: none !important;
    border: none !important;
    overflow: visible !important;
}

.fetch-label {
    position: absolute !important;
    transform-origin: center center;
    left: 50%;
    top: 50%;
    translate: -50% -50%;
    background: rgba(255,255,255,0.93) !important;
    border: 1px solid #bbb !important;
    color: #222 !important;
    font-size: 12px !important;
    font-weight: 700 !important;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
    padding: 2px 7px !important;
    border-radius: 4px !important;
    white-space: nowrap !important;
    box-shadow: 0 1px 3px rgba(0,0,0,0.15) !important;
    pointer-events: none !important;
    line-height: 1.4 !important;
}

.fetch-label-central {
    background: rgba(255,82,82,0.9) !important;
    border: 1px solid #ff5252 !important;
    color: #fff !important;
    font-weight: 700 !important;
    font-size: 13px !important;
}

.fetch-label-effective {
    background: rgba(79,195,247,0.9) !important;
    border: 1px solid #4fc3f7 !important;
    color: #fff !important;
    font-weight: 700 !important;
    font-size: 12px !important;
}

.angle-label {
    background: none !important;
    border: none !important;
    color: rgba(255,255,255,0.6) !important;
    font-size: 10px !important;
    font-family: 'Courier New', Consolas, monospace !important;
    box-shadow: none !important;
    pointer-events: none !important;
}

/* ===== Leaflet Overrides ===== */
.leaflet-control-layers {
    border-radius: 8px !important;
}

.leaflet-popup-content-wrapper {
    border-radius: 8px;
}

/* ===== Responsive ===== */
/* ===== Light Mode Sidebar ===== */
#sidebar.light {
    background: #f5f5f5;
    color: #333;
    scrollbar-color: #ccc #f5f5f5;
}

#sidebar.light::-webkit-scrollbar-track { background: #f5f5f5; }
#sidebar.light::-webkit-scrollbar-thumb { background: #ccc; }

#sidebar.light .sidebar-header h1 { color: #0277bd; }
#sidebar.light .sidebar-header .subtitle { color: #999; }

#sidebar.light #theme-toggle {
    border-color: #ccc;
    color: #666;
}
#sidebar.light #theme-toggle:hover {
    border-color: #0277bd;
    color: #0277bd;
}

#sidebar.light .section {
    background: rgba(0,0,0,0.04);
}

#sidebar.light .section h3 { color: #0277bd; }
#sidebar.light .hint { color: #888; }
#sidebar.light .coord-display { color: #444; }

#sidebar.light .input-row label { color: #555; }

#sidebar.light .input-row input[type="number"] {
    border-color: #ccc;
    background: #fff;
    color: #333;
}

#sidebar.light .input-row input:focus { border-color: #0277bd; }

#sidebar.light #calculate-btn {
    background: linear-gradient(135deg, #0288d1, #0277bd);
    color: #fff;
}
#sidebar.light #calculate-btn:hover:not(:disabled) {
    background: linear-gradient(135deg, #039be5, #0288d1);
    box-shadow: 0 4px 12px rgba(2,119,189,0.3);
}
#sidebar.light #calculate-btn:disabled {
    background: #e0e0e0;
    color: #999;
}

#sidebar.light #clear-btn {
    color: #888;
    border-color: #ccc;
}
#sidebar.light #clear-btn:hover {
    color: #d32f2f;
    border-color: #d32f2f;
}


#sidebar.light .sidebar-footer {
    border-top-color: #ddd;
    color: #aaa;
}

.sidebar-brand {
    font-size: 10px;
    color: rgba(255,255,255,0.4);
    text-align: center;
    padding: 10px;
    border-top: 1px solid rgba(255,255,255,0.08);
    flex-shrink: 0;
    letter-spacing: 0.5px;
}

#sidebar.light .sidebar-brand {
    color: rgba(0,0,0,0.35);
    border-top-color: rgba(0,0,0,0.08);
}

/* ===== Mobile Bottom Sheet Redesign ===== */
@media (max-width: 800px) {
    body {
        flex-direction: column;
        position: relative;
    }

    /* Map takes full screen */
    #map {
        position: fixed !important;
        top: 0;
        left: 0;
        width: 100vw;
        height: 100vh;
        z-index: 1;
    }

    /* Sidebar becomes bottom sheet */
    #sidebar {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        min-width: 100%;
        height: auto;
        max-height: 85vh;
        z-index: 1100;
        border-radius: 18px 18px 0 0;
        padding: 0 16px calc(16px + env(safe-area-inset-bottom, 0px));
        gap: 10px;
        transform: translateY(calc(100% - 64px));
        transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
        box-shadow: 0 -4px 30px rgba(0,0,0,0.25);
        overflow: hidden;
        will-change: transform;
    }

    #sidebar.sheet-peek {
        transform: translateY(calc(100% - 64px));
        overflow: hidden;
    }

    #sidebar.sheet-half {
        transform: translateY(calc(100% - 55vh));
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    #sidebar.sheet-full {
        transform: translateY(0);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    #sidebar.sheet-dragging {
        transition: none;
    }

    /* Light mode sheet adjustments */
    #sidebar.light {
        box-shadow: 0 -4px 30px rgba(0,0,0,0.15);
    }

    /* Drag handle */
    .sheet-handle {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 10px 0 6px;
        cursor: grab;
        position: sticky;
        top: 0;
        z-index: 10;
        -webkit-user-select: none;
        user-select: none;
        touch-action: none;
    }

    .sheet-handle-bar {
        width: 36px;
        height: 4px;
        background: rgba(128,128,128,0.4);
        border-radius: 2px;
    }

    #sidebar.light .sheet-handle-bar {
        background: rgba(0,0,0,0.2);
    }

    /* Peek bar content */
    .sheet-peek-content {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 4px 0 8px;
        gap: 10px;
    }

    .sheet-peek-content .peek-title {
        font-size: 0.9em;
        font-weight: 600;
        color: #4fc3f7;
        white-space: nowrap;
    }

    #sidebar.light .sheet-peek-content .peek-title {
        color: #0277bd;
    }

    .sheet-peek-content .peek-coords {
        font-family: 'Courier New', Consolas, monospace;
        font-size: 0.78em;
        color: #888;
        flex: 1;
        text-align: right;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .sheet-peek-content .peek-expand-btn {
        background: none;
        border: 1px solid rgba(128,128,128,0.3);
        color: #888;
        width: 32px;
        height: 32px;
        border-radius: 8px;
        font-size: 1.1em;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
        transition: all 0.2s;
    }

    .sheet-peek-content .peek-expand-btn:active {
        background: rgba(79,195,247,0.15);
    }

    /* Mobile backdrop */
    .sheet-backdrop {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0,0,0,0.4);
        z-index: 1050;
        opacity: 0;
        transition: opacity 0.3s;
    }

    .sheet-backdrop.visible {
        display: block;
        opacity: 1;
    }

    /* Hide desktop header on mobile */
    .sidebar-header {
        display: none;
    }

    /* Compact sections on mobile */
    .section {
        padding: 10px 12px;
    }

    .section h3 {
        font-size: 0.78em;
        margin-bottom: 6px;
    }

    /* 2-column layout: compass + parameters side by side */
    .mobile-two-col {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 10px;
    }

    .mobile-two-col .section {
        min-width: 0;
    }

    /* Compass smaller on mobile to fit in column */
    .compass-container svg {
        width: 110px !important;
        height: 110px !important;
    }

    .compass-container {
        margin-bottom: 4px;
    }

    /* Tighter input rows */
    .input-row {
        margin-bottom: 6px;
    }

    .input-row label {
        font-size: 0.8em;
    }

    .input-row input[type="number"] {
        width: 65px;
        padding: 5px 6px;
        font-size: 0.85em;
    }

    /* Buttons compact */
    #calculate-btn {
        padding: 11px;
        font-size: 0.95em;
    }

    #clear-btn {
        padding: 8px;
        font-size: 0.84em;
    }

    /* Footer compact */
    .sidebar-footer {
        font-size: 0.65em;
        padding-top: 8px;
    }

    .sidebar-brand {
        font-size: 9px;
        padding: 6px;
    }

    /* Results window as bottom sheet */
    #results-window {
        position: fixed !important;
        top: auto !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100% !important;
        max-height: 75vh;
        border-radius: 18px 18px 0 0;
        z-index: 1600;
        box-shadow: 0 -4px 30px rgba(0,0,0,0.3);
        overflow: hidden;
        transition: max-height 0.3s ease, border-radius 0.3s ease;
    }

    #results-window.minimized {
        max-height: none;
        border-radius: 18px 18px 0 0;
    }

    #results-window .results-window-titlebar {
        cursor: default;
        border-radius: 18px 18px 0 0;
        padding: 6px 14px;
    }

    #results-window .results-window-titlebar::before {
        content: '';
        display: block;
        width: 36px;
        height: 4px;
        background: rgba(0,0,0,0.15);
        border-radius: 2px;
        margin: 4px auto 8px;
    }

    #results-window .results-window-body {
        overflow-y: auto;
        max-height: calc(75vh - 50px);
        padding: 10px 14px 20px;
        -webkit-overflow-scrolling: touch;
    }

    /* Effective fetch value smaller */
    .effective-fetch {
        padding: 10px;
        margin-bottom: 10px;
    }

    .effective-fetch .value {
        font-size: 1.7em;
    }

    /* Results table compact */
    #results-table {
        font-size: 0.72em;
    }

    #results-table th,
    #results-table td {
        padding: 3px 5px;
    }

    .fetch-formula {
        margin-top: 8px;
        padding: 6px 8px;
    }

    .formula-line {
        font-size: 0.7em;
    }

    /* Opacity control repositioned */
    #opacity-control {
        left: 50%;
        top: 8px;
    }

    /* Toast repositioned above sheet */
    #toast-container {
        bottom: 76px;
        left: 10px;
        right: 10px;
        max-width: 100%;
    }

    .toast {
        font-size: 0.8em;
        padding: 10px 12px;
    }

    /* Leaflet controls repositioned */
    .leaflet-top.leaflet-right {
        top: 8px;
        right: 8px;
    }

    .leaflet-bottom.leaflet-right {
        bottom: 72px;
    }

    /* Mobile floating calculate button */
    #mobile-calc-fab {
        display: flex;
    }
}

/* Mobile FAB (hidden on desktop) */
#mobile-calc-fab {
    display: none;
    position: fixed;
    bottom: 76px;
    left: 16px;
    z-index: 1090;
    height: 44px;
    padding: 0 20px;
    border-radius: 22px;
    background: linear-gradient(135deg, #4fc3f7, #29b6f6);
    color: #0d1b2a;
    border: none;
    box-shadow: 0 4px 16px rgba(79,195,247,0.4);
    cursor: pointer;
    align-items: center;
    justify-content: center;
    font-size: 0.9em;
    font-weight: 700;
    letter-spacing: 0.5px;
    transition: all 0.3s, opacity 0.35s, visibility 0.35s;
    opacity: 1;
    visibility: visible;
}

#mobile-calc-fab.fab-hidden {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

#mobile-calc-fab:active {
    transform: scale(0.95);
}

#mobile-calc-fab:disabled {
    background: rgba(100,100,100,0.6);
    color: #999;
    box-shadow: none;
    pointer-events: none;
}

#sidebar.light ~ #mobile-calc-fab {
    background: linear-gradient(135deg, #0288d1, #0277bd);
    color: #fff;
    box-shadow: 0 4px 16px rgba(2,119,189,0.4);
}

#sidebar.light ~ #mobile-calc-fab:disabled {
    background: rgba(200,200,200,0.8);
    color: #aaa;
    box-shadow: none;
}

/* Desktop overrides - hide mobile-only elements */
@media (min-width: 801px) {
    .sheet-handle,
    .sheet-peek-content,
    .sheet-backdrop {
        display: none !important;
    }

    .mobile-two-col {
        display: contents;
    }
}
