/* === Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg: #0f1525;
    --bg-light: #1a2235;
    --bg-lighter: #243050;
    --border: #2a3a55;
    --text: #c8d6e5;
    --text-dim: #7f8fa6;
    --accent: #3498db;
    --accent-hover: #2980b9;
    --port: #e74c3c;
    --starboard: #2ecc71;
    --warning: #f39c12;
}

html, body {
    height: 100%;
    height: 100dvh;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    overflow: hidden;
    font-size: 13px;
}

#app { height: 100%; display: flex; flex-direction: column; }

/* === Buttons === */
.btn {
    background: var(--bg-lighter);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 6px 14px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
    transition: background 0.15s;
}
.btn:hover { background: var(--accent-hover); }
.btn-primary { background: var(--accent); border-color: var(--accent); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-small { padding: 4px 10px; font-size: 12px; }
.btn.active { background: var(--accent); border-color: var(--accent); }

/* === Upload Screen === */
#upload-screen {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.upload-area {
    text-align: center;
    padding: 60px 80px;
    border: 2px dashed var(--border);
    border-radius: 16px;
    background: var(--bg-light);
    transition: border-color 0.2s, background 0.2s;
}
.upload-area.drag-over {
    border-color: var(--accent);
    background: var(--bg-lighter);
}
.upload-icon { font-size: 64px; margin-bottom: 16px; }
.upload-area h1 { font-size: 24px; margin-bottom: 8px; font-weight: 600; }
.upload-area p { color: var(--text-dim); margin-bottom: 4px; }
.upload-or { margin: 12px 0 !important; font-size: 12px; }

/* === Drop Overlay === */
#drop-overlay {
    position: fixed; inset: 0;
    background: rgba(15, 21, 37, 0.92);
    z-index: 9999;
    display: flex; align-items: center; justify-content: center;
}
.drop-message {
    font-size: 28px; font-weight: 600;
    padding: 40px 60px;
    border: 3px dashed var(--accent);
    border-radius: 16px;
    color: var(--accent);
}

/* === Viewer Screen === */
#viewer-screen {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
}

/* Header */
#header-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 6px 12px;
    background: var(--bg-light);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    min-height: 40px;
    flex-wrap: wrap;
}
.file-info { color: var(--text-dim); font-size: 12px; white-space: nowrap; }
.gps-info {
    font-family: 'SF Mono', 'Consolas', monospace;
    font-size: 11px;
    color: var(--text);
    background: var(--bg-lighter);
    padding: 3px 8px;
    border-radius: 3px;
    white-space: nowrap;
}

/* Layout buttons */
.layout-buttons { display: flex; gap: 3px; align-items: center; }
.layout-btn { padding: 3px 5px !important; display: flex; align-items: center; justify-content: center; }
.layout-btn svg { display: block; }

.header-controls { display: flex; align-items: center; gap: 10px; margin-left: auto; }
.gain-label {
    display: flex; align-items: center; gap: 6px;
    font-size: 12px; color: var(--text-dim); white-space: nowrap;
}
#gain-slider { width: 80px; accent-color: var(--accent); }
#gain-value { width: 36px; text-align: right; }
#palette-select {
    background: var(--bg-lighter);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 3px 6px;
    border-radius: 4px;
    font-size: 12px;
}

/* === Main content — panel grid === */
#main-content {
    flex: 1;
    display: grid;
    overflow: hidden;
    min-height: 0;
}

/* === Panel === */
.panel {
    position: relative;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    border: 1px solid var(--border);
}

.panel-header {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 2px 6px;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    min-height: 22px;
    z-index: 5;
    cursor: grab;
}
.panel-header:active { cursor: grabbing; }

.panel-drag-grip {
    opacity: 0.35;
    font-size: 10px;
    user-select: none;
    line-height: 1;
}
.panel-header:hover .panel-drag-grip { opacity: 0.7; }

.panel-close-btn {
    margin-left: auto;
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
    padding: 0 4px;
    opacity: 0;
    transition: opacity 0.15s, color 0.15s;
}
.panel-header:hover .panel-close-btn { opacity: 0.6; }
.panel-close-btn:hover { opacity: 1 !important; color: #e74c3c; }

/* Panel rows (flex layout) */
.panel-row {
    display: flex;
    flex: 1;
    min-height: 0;
}

/* Drag-and-drop states */
.panel-dragging { opacity: 0.35; }
.panel-drop-target { }
.drop-zone-top    { box-shadow: inset 0  4px 0 var(--accent); }
.drop-zone-bottom { box-shadow: inset 0 -4px 0 var(--accent); }
.drop-zone-left   { box-shadow: inset  4px 0 0 var(--accent); }
.drop-zone-right  { box-shadow: inset -4px 0 0 var(--accent); }

.panel-source-select {
    background: var(--bg-lighter);
    color: var(--accent);
    border: 1px solid var(--border);
    padding: 1px 4px;
    border-radius: 3px;
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    max-width: 140px;
}
.panel-source-select:hover { border-color: var(--accent); }

.panel-content {
    flex: 1;
    position: relative;
    overflow: hidden;
    min-height: 0;
}

/* Map inside a panel */
.panel-content .map-instance {
    width: 100%;
    height: 100%;
    background: var(--bg);
}

/* === Grid resize handles === */
.grid-handle-v {
    cursor: col-resize;
    background: var(--border);
    transition: background 0.15s;
    touch-action: none;
    z-index: 50;
    min-width: 6px;
}
.grid-handle-v:hover, .grid-handle-v:active { background: var(--accent); }

.grid-handle-h {
    cursor: row-resize;
    background: var(--border);
    transition: background 0.15s;
    touch-action: none;
    z-index: 50;
    min-height: 6px;
}
.grid-handle-h:hover, .grid-handle-h:active { background: var(--accent); }

/* === Sonar channel widget (inside panel-content) === */
.sonar-channel {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    position: relative;
}
.sonar-channel-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 3px 8px;
    background: var(--bg-light);
    border-bottom: 1px solid var(--border);
    font-size: 11px;
    flex-shrink: 0;
}
.sonar-channel-header .ch-name { font-weight: 600; color: var(--accent); }
.sonar-channel-header .ch-stat { color: var(--text-dim); }
.ch-zoom-indicator {
    color: var(--warning);
    font-size: 10px;
    font-family: 'SF Mono', 'Consolas', monospace;
    margin-left: auto;
}
.ch-zoom-reset {
    background: none;
    border: 1px solid var(--border);
    color: var(--text-dim);
    cursor: pointer;
    font-size: 10px;
    padding: 0 4px;
    border-radius: 3px;
    line-height: 1.4;
    font-family: 'SF Mono', 'Consolas', monospace;
}
.ch-zoom-reset:hover { color: var(--warning); border-color: var(--warning); }
.ch-rotate-btn {
    background: none;
    border: 1px solid var(--border);
    color: var(--text-dim);
    cursor: pointer;
    font-size: 13px;
    padding: 0 5px;
    border-radius: 3px;
    line-height: 1.4;
}
.ch-rotate-btn:hover { color: var(--text); border-color: var(--accent); }
.ch-rotate-btn.active { color: var(--accent); border-color: var(--accent); }
.ch-gain-ctrl {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    margin-left: 4px;
}
.ch-gain-icon {
    font-size: 11px;
    color: var(--text-dim);
}
.ch-gain-slider {
    width: 50px;
    height: 3px;
    cursor: pointer;
    accent-color: var(--accent);
    opacity: 0.7;
}
.ch-gain-slider:hover { opacity: 1; }
.ch-gain-value {
    font-size: 9px;
    color: var(--text-dim);
    min-width: 28px;
    font-family: 'SF Mono', 'Consolas', monospace;
}

.sonar-canvas-wrap {
    flex: 1;
    position: relative;
    overflow: hidden;
    display: flex;
    min-height: 0;
}
.sonar-canvas-wrap canvas {
    display: block;
}
.depth-ruler {
    width: 52px;
    flex-shrink: 0;
    background: var(--bg-light);
    border-left: 1px solid var(--border);
    position: relative;
    overflow: hidden;
}
.depth-ruler canvas { display: block; }

/* Cursor line */
.cursor-line {
    position: absolute;
    top: 0; bottom: 0;
    width: 2px;
    background: #f1c40f;
    pointer-events: none;
    z-index: 10;
}

/* Depth ruler at bottom for vertical (rotated) channels */
.sonar-channel.ch-rotated .sonar-canvas-wrap {
    flex-direction: column;
}
.sonar-channel.ch-rotated .depth-ruler {
    width: auto;
    height: 52px;
    flex-shrink: 0;
    border-left: none;
    border-top: 1px solid var(--border);
}
/* Horizontal cursor line for rotated channels */
.sonar-channel.ch-rotated .cursor-line {
    top: auto; bottom: auto;
    left: 0; right: 0;
    width: auto;
    height: 2px;
}

/* === Playback Bar === */
#playback-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 12px;
    background: var(--bg-light);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}
.time-display {
    font-family: 'SF Mono', 'Consolas', monospace;
    font-size: 12px;
    color: var(--text-dim);
    min-width: 60px;
}
.btn-play {
    width: 32px; height: 28px;
    display: flex; align-items: center; justify-content: center;
    font-size: 14px; padding: 0;
}
.scrubber {
    flex: 1;
    accent-color: var(--accent);
    height: 6px;
}
.speed-select {
    background: var(--bg-lighter);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 3px 6px;
    border-radius: 4px;
    font-size: 12px;
}
.frame-counter {
    font-family: 'SF Mono', 'Consolas', monospace;
    font-size: 11px;
    color: var(--text-dim);
    min-width: 80px;
    text-align: right;
}

/* Bow-up map rotation */
.bow-up-map {
    transform-origin: center center;
}

/* Leaflet overrides */
.leaflet-container { background: var(--bg) !important; }
.leaflet-control-zoom a {
    background: var(--bg-light) !important;
    color: var(--text) !important;
    border-color: var(--border) !important;
}

/* Utility */
.hidden { display: none !important; }

/* Boat icon */
.boat-icon {
    width: 0; height: 0;
    border-left: 6px solid transparent;
    border-right: 6px solid transparent;
    border-bottom: 16px solid var(--accent);
    filter: drop-shadow(0 0 3px rgba(52, 152, 219, 0.6));
}

/* Scrollbar */
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--bg-lighter); border-radius: 4px; }

/* === Export Dialog === */
#export-overlay {
    position: fixed; inset: 0;
    background: rgba(15, 21, 37, 0.85);
    z-index: 10000;
    display: flex; align-items: center; justify-content: center;
}
.export-dialog {
    background: var(--bg-light);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 24px;
    min-width: 420px;
    max-width: 500px;
    color: var(--text);
}
.export-dialog h2 { font-size: 18px; margin-bottom: 16px; font-weight: 600; }
.export-dialog label {
    display: block; font-size: 12px; color: var(--text-dim);
    margin-bottom: 4px; margin-top: 12px;
}
.export-dialog .export-row {
    display: flex; align-items: center; gap: 10px;
    margin-top: 4px;
}
.export-dialog input[type="range"] { flex: 1; accent-color: var(--accent); }
.export-dialog input[type="number"],
.export-dialog select {
    background: var(--bg-lighter);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 13px;
    width: 70px;
}
.export-dialog .export-info {
    font-size: 11px;
    color: var(--text-dim);
    font-family: 'SF Mono', 'Consolas', monospace;
    margin-top: 4px;
}
.export-meta {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-top: 14px;
    padding: 10px 12px;
    background: var(--bg);
    border-radius: 6px;
    border: 1px solid var(--border);
}
.export-duration {
    font-size: 16px;
    font-weight: 600;
    color: var(--accent);
    font-family: 'SF Mono', 'Consolas', monospace;
    margin-top: 2px;
}
.export-dialog .export-actions {
    display: flex; gap: 8px; margin-top: 20px; justify-content: flex-end;
}
.export-progress {
    margin-top: 16px;
}
.export-progress-bar {
    height: 6px;
    background: var(--bg-lighter);
    border-radius: 3px;
    overflow: hidden;
    margin-top: 6px;
}
.export-progress-fill {
    height: 100%;
    background: var(--accent);
    width: 0%;
    transition: width 0.1s;
}
.export-progress-text {
    font-size: 11px;
    color: var(--text-dim);
    margin-top: 4px;
    font-family: 'SF Mono', 'Consolas', monospace;
}

/* === Safe area (notch) support === */
body {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
}

/* === Mobile header toggle === */
#mobile-menu-btn {
    display: none;
    background: none;
    border: 1px solid var(--border);
    color: var(--text);
    font-size: 18px;
    padding: 2px 8px;
    border-radius: 4px;
    cursor: pointer;
    line-height: 1;
}

/* === Mobile responsive === */
@media (max-width: 768px) {
    html, body { font-size: 12px; }

    /* Upload screen */
    .upload-area {
        padding: 40px 24px;
        margin: 16px;
        border-radius: 12px;
    }
    .upload-icon { font-size: 48px; }
    .upload-area h1 { font-size: 20px; }

    /* Header bar — two-row collapsible layout */
    #header-bar {
        gap: 6px;
        padding: 4px 8px;
        min-height: unset;
    }

    #mobile-menu-btn { display: inline-flex; }

    .file-info { display: none; }

    .gps-info {
        font-size: 10px;
        padding: 2px 6px;
        flex: 1;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .header-controls {
        width: 100%;
        gap: 6px 10px;
        flex-wrap: wrap;
        margin-left: 0;
        display: none;
        align-items: center;
    }
    .header-controls.mobile-open {
        display: flex;
    }
    .header-controls .layout-buttons {
        display: flex;
        margin-right: auto;
    }

    #export-btn { display: none; }

    .gain-label {
        flex: 1 1 100%;
        min-width: 0;
    }
    #gain-slider { flex: 1; min-width: 60px; }

    #palette-select { flex-shrink: 0; }

    /* Panels — sonar channel header */
    .sonar-channel-header {
        gap: 4px;
        padding: 2px 6px;
        flex-wrap: wrap;
    }
    .sonar-channel-header .ch-stat { font-size: 10px; }
    .ch-gain-ctrl { display: none; }

    /* Panel header */
    .panel-header {
        padding: 2px 4px;
        min-height: 26px;
    }
    .panel-drag-grip { display: none; }
    .panel-close-btn { opacity: 0.5; }

    /* Depth ruler — narrower on mobile */
    .depth-ruler { width: 36px; }

    /* Playback bar */
    #playback-bar {
        gap: 6px;
        padding: 4px 8px;
        flex-wrap: wrap;
    }
    .time-display { font-size: 11px; min-width: 50px; }
    .btn-play {
        width: 36px;
        height: 32px;
        font-size: 16px;
    }
    .scrubber {
        height: 12px;
        flex: 1 1 100%;
        order: -1;
    }
    .speed-select { font-size: 11px; }
    .frame-counter { font-size: 10px; min-width: 60px; }

    /* Export dialog */
    .export-dialog {
        min-width: unset;
        max-width: 95vw;
        margin: 8px;
        padding: 16px;
    }

    /* Drop overlay */
    .drop-message {
        font-size: 20px;
        padding: 24px 32px;
    }
}

/* Touch-friendly resize handles */
@media (pointer: coarse) {
    .grid-handle-v { min-width: 10px; }
    .grid-handle-h { min-height: 10px; }
}

/* Extra small screens (phones in portrait) */
@media (max-width: 480px) {
    .gps-info { display: none; }

    .sonar-channel-header .ch-speed,
    .sonar-channel-header .ch-freq { display: none; }

    .depth-ruler { width: 28px; }

    .time-display:first-child { display: none; }
    .frame-counter { display: none; }
}
