/* CSS Variables */
:root {
    --primary-color: #2196F3;
    --primary-dark: #1976D2;
    --secondary-color: #4CAF50;
    --danger-color: #f44336;
    --warning-color: #FF9800;
    --background-dark: #1a1a2e;
    --background-card: #16213e;
    --text-primary: #ffffff;
    --text-secondary: #b0b0b0;
    --border-color: #2d3a5a;
    --shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    --radius: 12px;
    --radius-small: 8px;
}

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

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    background-color: var(--background-dark);
    color: var(--text-primary);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* App Container */
#app {
    height: 100%;
    display: flex;
    flex-direction: column;
    position: relative;
}

/* Header */
#header {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    padding: 12px 16px;
    background: var(--background-card);
    border-bottom: 1px solid var(--border-color);
    z-index: 100;
    gap: 16px;
}

#header h1 {
    font-size: 1.25rem;
    font-weight: 600;
    justify-self: start;
}

.btn-icon {
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 8px;
    border-radius: var(--radius-small);
    transition: background-color 0.2s;
}

.btn-icon:hover,
.btn-icon:active {
    background: rgba(255, 255, 255, 0.1);
}

#header .btn-icon {
    justify-self: end;
}

/* Map */
#map {
    flex: 1;
    width: 100%;
    z-index: 1;
}

/* Stats Panel */
#stats-panel {
    display: flex;
    justify-content: space-around;
    padding: 12px 8px;
    background: var(--background-card);
    border-top: 1px solid var(--border-color);
    z-index: 100;
}

#stats-panel.hidden {
    display: none;
}

/* Live Coordinates Panel */
#coords-panel {
    padding: 8px 16px;
    background: var(--background-card);
    border-top: 1px solid var(--border-color);
    z-index: 100;
    font-size: 0.82rem;
}

#coords-panel.hidden {
    display: none;
}

.coords-content {
    display: flex;
    align-items: center;
}

.coords-rows {
    flex: 1;
    min-width: 0;
}

.coords-row {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 2px 0;
}

.coords-label {
    font-weight: 600;
    color: var(--text-secondary);
    min-width: 65px;
    flex-shrink: 0;
}

.coords-value {
    color: var(--primary-color);
    font-variant-numeric: tabular-nums;
    font-family: monospace;
    word-break: break-all;
}

.btn-coords-toggle {
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 50%;
    background: rgba(255,255,255,0.1);
    color: var(--text-secondary);
    font-size: 1.1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-left: 8px;
    transition: background 0.2s;
}

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

.stat {
    text-align: center;
    padding: 4px 8px;
}

.stat-value {
    display: block;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary-color);
    font-variant-numeric: tabular-nums;
}

.stat-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-transform: uppercase;
}

/* GPS Status */
.gps-status {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 8px 24px;
    background: rgba(0, 0, 0, 0.8);
    border-radius: 20px;
    font-size: 0.875rem;
    backdrop-filter: blur(4px);
    min-width: 200px;
    white-space: nowrap;
    justify-self: center;
    cursor: pointer;
    transition: background 0.2s;
}

.gps-status:hover {
    background: rgba(0, 0, 0, 0.9);
}

.gps-dot {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background: var(--danger-color);
    flex-shrink: 0;
}

.gps-dot.fix-rtk-fixed {
    background: #00e676;
}

.gps-dot.fix-rtk-float {
    background: #ffd600;
}

.gps-dot.active {
    background: var(--danger-color);
}

.gps-dot.error {
    background: var(--danger-color);
}

.gps-dot.gps-flash {
    animation: gps-blink 0.3s ease-out;
}

.ntrip-dot {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background: #555;
    flex-shrink: 0;
    transition: background 0.3s;
}

.ntrip-dot.ntrip-connected {
    background: #2196F3;
    animation: none;
}

.ntrip-dot.ntrip-flash {
    animation: ntrip-blink 0.3s ease-out;
}

.ntrip-dot.ntrip-connecting {
    background: #FF9800;
    animation: pulse 1s infinite;
}

.ntrip-dot.ntrip-disconnected {
    background: #555;
    animation: none;
}

@keyframes ntrip-blink {
    0%, 50% { opacity: 0; }
    100% { opacity: 1; }
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

@keyframes sharp-blink {
    0%, 49% { opacity: 1; }
    50%, 100% { opacity: 0; }
}

@keyframes gps-blink {
    0% { opacity: 0; }
    50% { opacity: 0; }
    100% { opacity: 1; }
}

/* Map Info Overlay (Leaflet control) */
.map-info-overlay {
    background: transparent !important;
    color: rgba(255, 255, 255, 0.85);
    padding: 5px 12px;
    border-radius: 12px;
    font-size: 0.85rem;
    font-weight: 500;
    white-space: nowrap;
    letter-spacing: 0.3px;
    border: none !important;
    box-shadow: none;
    text-shadow: 0 1px 4px rgba(0,0,0,0.8);
    margin: 0 !important;
    width: 100%;
    text-align: center;
    pointer-events: none;
}

.sat-icon {
    font-size: 1.4rem;
    vertical-align: middle;
}

/* Control Buttons */
#controls {
    display: flex;
    justify-content: center;
    gap: 16px;
    padding: 16px;
    background: var(--background-card);
    border-top: 1px solid var(--border-color);
    z-index: 100;
}

.btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 16px 32px;
    border: none;
    border-radius: var(--radius);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    min-width: 100px;
}

.btn-icon-large {
    font-size: 1.5rem;
}

.btn-start {
    background: var(--secondary-color);
    color: white;
}

.btn-start:hover,
.btn-start:active {
    background: #43A047;
    transform: scale(1.02);
}

.btn-stop {
    background: var(--danger-color);
    color: white;
}

.btn-stop:hover,
.btn-stop:active {
    background: #E53935;
    transform: scale(1.02);
}

.btn-pause {
    background: var(--warning-color);
    color: white;
}

.btn-pause:hover,
.btn-pause:active {
    background: #FB8C00;
    transform: scale(1.02);
}

.btn-polygon {
    background: #9C27B0;
    color: white;
}

.btn-polygon:hover,
.btn-polygon:active {
    background: #7B1FA2;
    transform: scale(1.02);
}

.btn.hidden {
    display: none;
}

/* Side Menu */
.side-menu {
    position: fixed;
    top: 0;
    right: 0;
    width: 85%;
    max-width: 350px;
    height: 100%;
    background: var(--background-card);
    z-index: 1000;
    transform: translateX(100%);
    transition: transform 0.3s ease;
    display: flex;
    flex-direction: column;
    box-shadow: -4px 0 20px rgba(0, 0, 0, 0.5);
}

.side-menu.visible {
    transform: translateX(0);
}

.side-menu.hidden {
    transform: translateX(100%);
}

.menu-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
    border-bottom: 1px solid var(--border-color);
}

.menu-header h2 {
    font-size: 1.125rem;
}

.menu-body {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

/* Menu Sections */
.menu-section {
    border-bottom: 1px solid var(--border-color);
}

.section-title {
    font-size: 0.9rem;
    font-weight: 600;
    padding: 12px 16px 8px 16px;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.section-title.collapsible {
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
    user-select: none;
    transition: color 0.2s;
}

.section-title.collapsible:hover {
    color: var(--text-primary);
}

.collapse-icon {
    font-size: 0.8rem;
    transition: transform 0.3s ease;
}

.section-title.collapsed .collapse-icon {
    transform: rotate(-90deg);
}

.collapsible-content {
    max-height: 1000px;
    overflow: hidden;
    transition: max-height 0.3s ease, opacity 0.3s ease;
    opacity: 1;
}

.collapsible-content.collapsed {
    max-height: 0;
    opacity: 0;
}

/* Settings Group */
.settings-group {
    padding: 8px 16px 16px 16px;
}

.settings-group label {
    display: block;
    font-size: 0.9rem;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.frequency-select {
    width: 100%;
    padding: 10px 12px;
    background: var(--background-dark);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-small);
    font-size: 0.95rem;
    cursor: pointer;
    transition: border-color 0.2s;
}

.frequency-select:focus {
    outline: none;
    border-color: var(--primary-color);
}

.frequency-select option {
    background: var(--background-dark);
    color: var(--text-primary);
}

.settings-input {
    width: 100%;
    padding: 10px 12px;
    background: var(--background-dark);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-small);
    font-size: 0.95rem;
    transition: border-color 0.2s;
    box-sizing: border-box;
}

.settings-input:focus {
    outline: none;
    border-color: var(--primary-color);
}

/* Toggle Switch for Polygon Mode */
.toggle-container {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 8px 0;
}

.toggle-label {
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.toggle-switch {
    position: relative;
    width: 50px;
    height: 26px;
    flex-shrink: 0;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: var(--border-color);
    transition: 0.3s;
    border-radius: 26px;
}

.toggle-slider:before {
    position: absolute;
    content: "";
    height: 20px;
    width: 20px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: 0.3s;
    border-radius: 50%;
}

.toggle-switch input:checked + .toggle-slider {
    background-color: var(--warning-color);
}

.toggle-switch input:checked + .toggle-slider:before {
    transform: translateX(24px);
}

.settings-note {
    font-size: 0.8rem;
    color: var(--warning-color);
    margin-top: 8px;
    line-height: 1.4;
}

.settings-divider {
    border: none;
    border-top: 1px solid var(--border-color);
    margin: 16px 0;
}

/* Traces List */
.traces-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.empty-message {
    text-align: center;
    color: var(--text-secondary);
    padding: 32px 16px;
}

/* Drift Test */
.drift-progress {
    margin-top: 12px;
}

.drift-progress-bar {
    width: 100%;
    height: 8px;
    background: var(--background-dark);
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 4px;
}

.drift-bar-fill {
    height: 100%;
    width: 0%;
    background: var(--primary-color);
    border-radius: 4px;
    transition: width 0.3s ease;
}

.drift-progress-text {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.drift-results {
    margin-top: 12px;
    padding: 12px;
    background: var(--background-dark);
    border-radius: var(--radius-small);
    border: 1px solid var(--border-color);
}

.drift-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.drift-table td {
    padding: 4px 0;
    color: var(--text-primary);
}

.drift-table td:first-child {
    color: var(--text-secondary);
    padding-right: 12px;
    white-space: nowrap;
}

.drift-table td:last-child {
    font-weight: 600;
    text-align: right;
}

/* Drift Help */
.help-section {
    margin-bottom: 12px;
}

/* Coordinate Converter */
.conv-result {
    margin-top: 12px;
    padding: 12px;
    background: var(--background-dark);
    border-radius: var(--radius-small);
    border: 1px solid var(--primary-color);
}

.help-section h4 {
    font-size: 0.95rem;
    margin: 8px 0 4px 0;
    color: var(--text-primary);
}

.help-section p {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin: 4px 0;
    line-height: 1.4;
}

.help-section strong {
    color: var(--text-primary);
}

.trace-item {
    background: var(--background-dark);
    border-radius: var(--radius-small);
    padding: 12px;
    margin-bottom: 8px;
    border: 1px solid var(--border-color);
}

.trace-item-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 8px;
}

.trace-name {
    font-weight: 600;
    font-size: 1rem;
    word-break: break-word;
}

.trace-date {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.trace-stats {
    display: flex;
    gap: 16px;
    font-size: 0.875rem;
    color: var(--text-secondary);
    margin-bottom: 12px;
}

.trace-actions {
    display: flex;
    gap: 8px;
}

.trace-actions button {
    flex: 1;
    padding: 8px 12px;
    border: none;
    border-radius: var(--radius-small);
    font-size: 0.875rem;
    cursor: pointer;
    transition: background-color 0.2s;
}

.btn-view {
    background: var(--primary-color);
    color: white;
}

.btn-export {
    background: var(--secondary-color);
    color: white;
}

.btn-export-kml {
    background: #1a73e8;
    color: white;
}

.btn-delete {
    background: var(--danger-color);
    color: white;
}

/* Overlay */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    z-index: 900;
    opacity: 0;
    transition: opacity 0.3s;
    pointer-events: none;
}

.overlay.visible {
    opacity: 1;
    pointer-events: auto;
}

.overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

/* Dialog */
.dialog {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.7);
    z-index: 1100;
    padding: 20px;
}

.dialog.hidden {
    display: none;
}

.dialog-content {
    background: var(--background-card);
    border-radius: var(--radius);
    padding: 24px;
    width: 100%;
    max-width: 320px;
    max-height: 85vh;
    overflow-y: auto;
    box-shadow: var(--shadow);
}

.dialog-content h3 {
    margin-bottom: 16px;
    text-align: center;
}

.dialog-content input {
    width: 100%;
    padding: 12px 16px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-small);
    background: var(--background-dark);
    color: var(--text-primary);
    font-size: 1rem;
    margin-bottom: 16px;
}

.dialog-content input:focus {
    outline: none;
    border-color: var(--primary-color);
}

.dialog-buttons {
    display: flex;
    gap: 12px;
}

.dialog-buttons .btn {
    flex: 1;
    padding: 12px;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
}

.btn-danger {
    background: var(--danger-color);
    color: white;
}

.btn-secondary {
    background: var(--border-color);
    color: var(--text-primary);
}

/* GPS Source Dialog */
.gps-source-buttons {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-bottom: 16px;
}

.btn-gps-source {
    width: 100%;
    padding: 16px;
    background: var(--background-dark);
    color: var(--text-primary);
    border: 2px solid var(--border-color);
    font-size: 1.1rem;
    transition: all 0.2s;
}

.btn-gps-source:hover {
    border-color: var(--primary-color);
    background: var(--background-card);
}

.btn-gps-source.active {
    background: var(--primary-color);
    border-color: var(--primary-color);
    color: white;
}

/* Export Offset Dialog */
.dialog-desc {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 16px;
    text-align: center;
    line-height: 1.4;
}

.dialog-label {
    display: block;
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.offset-direction {
    display: flex;
    gap: 12px;
    margin-bottom: 16px;
}

.radio-label {
    flex: 1;
    cursor: pointer;
}

.radio-label input[type="radio"] {
    display: none;
}

.radio-btn {
    display: block;
    text-align: center;
    padding: 10px 12px;
    background: var(--background-dark);
    border: 2px solid var(--border-color);
    border-radius: var(--radius-small);
    font-size: 0.95rem;
    transition: all 0.2s;
}

.radio-label input[type="radio"]:checked + .radio-btn {
    border-color: var(--primary-color);
    background: rgba(33, 150, 243, 0.15);
    color: var(--primary-color);
    font-weight: 600;
}

/* Toast Notifications */
.toast {
    position: fixed;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.9);
    color: white;
    padding: 12px 24px;
    border-radius: 24px;
    font-size: 0.875rem;
    z-index: 1200;
    opacity: 0;
    transition: opacity 0.3s;
    max-width: 90%;
    text-align: center;
}

.toast.visible {
    opacity: 1;
}

.toast.hidden {
    opacity: 0;
    pointer-events: none;
}

/* Leaflet Customization */
.leaflet-control-zoom {
    border: none !important;
    box-shadow: var(--shadow) !important;
}

.leaflet-control-zoom a {
    background: var(--background-card) !important;
    color: var(--text-primary) !important;
    border: none !important;
}

.leaflet-control-zoom a:hover {
    background: var(--background-dark) !important;
}

.leaflet-control-attribution {
    background: rgba(0, 0, 0, 0.6) !important;
    color: var(--text-secondary) !important;
    font-size: 10px !important;
}

.leaflet-control-attribution a {
    color: var(--text-secondary) !important;
}

/* Current Position Marker */
.current-position-marker {
    width: 20px;
    height: 20px;
    background: var(--primary-color);
    border: 3px solid white;
    border-radius: 50%;
    box-shadow: 0 0 10px rgba(33, 150, 243, 0.5);
}

.accuracy-circle {
    fill: rgba(33, 150, 243, 0.15);
    stroke: rgba(33, 150, 243, 0.4);
    stroke-width: 1;
}

/* Measure distance tooltip */
.measure-tooltip {
    background: rgba(0, 0, 0, 0.55);
    border: none;
    border-radius: 6px;
    padding: 3px 8px;
    color: #FF9800;
    font-size: 14px;
    font-weight: 700;
    letter-spacing: 0.3px;
    text-shadow: none;
    box-shadow: none;
    white-space: nowrap;
}

.measure-tooltip::before {
    display: none;
}

/* Recording indicator */
.recording-indicator {
    position: absolute;
    top: 100px;
    right: 16px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: rgba(244, 67, 54, 0.9);
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
    z-index: 500;
    animation: recording-pulse 1.5s infinite;
}

.recording-indicator.hidden {
    display: none;
}

.recording-dot {
    width: 8px;
    height: 8px;
    background: white;
    border-radius: 50%;
}

@keyframes recording-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

/* Media Queries */
@media (min-width: 768px) {
    .side-menu {
        width: 350px;
    }
    
    #controls {
        padding: 20px;
    }
    
    .btn {
        padding: 20px 48px;
        min-width: 140px;
    }
}

/* Safe area for notched devices */
@supports (padding-top: env(safe-area-inset-top)) {
    #header {
        padding-top: calc(12px + env(safe-area-inset-top));
    }
    
    #controls {
        padding-bottom: calc(16px + env(safe-area-inset-bottom));
    }
}

/* Bluetooth GPS */
.bt-panel {
    padding: 12px;
}

.bt-status-box {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    margin-bottom: 8px;
    border-radius: var(--radius-small);
    background: var(--background-dark);
    border: 1px solid var(--border-color);
    font-size: 0.9rem;
    color: var(--text-primary);
}

.bt-status-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}

.bt-status-dot.bt-disconnected {
    background: #f44336;
    box-shadow: 0 0 4px #f44336;
}

.bt-status-dot.bt-connecting {
    background: #FF9800;
    box-shadow: 0 0 4px #FF9800;
    animation: pulse-dot 1s infinite;
}

.bt-status-dot.bt-connected {
    background: #4CAF50;
    box-shadow: 0 0 6px #4CAF50;
}

@keyframes pulse-dot {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.3; }
}

.bt-buttons {
    display: flex;
    gap: 8px;
    margin-top: 10px;
    flex-wrap: wrap;
}

.btn-small {
    padding: 10px 16px;
    font-size: 0.85rem;
    border-radius: var(--radius-small);
    border: 1px solid var(--border-color);
    background: var(--background-dark);
    color: var(--text-primary);
    cursor: pointer;
    flex: 1;
    text-align: center;
    min-width: 100px;
}

.btn-small:active {
    background: var(--primary-dark);
}

.btn-danger {
    background: var(--danger-color);
    color: white;
}

.btn-danger-small {
    border-color: var(--danger-color);
    color: var(--danger-color);
}

.btn-danger-small:active {
    background: var(--danger-color);
    color: white;
}

.bt-device-list {
    max-height: 300px;
    overflow-y: auto;
    margin: 12px 0;
}

.bt-device {
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-small);
    margin-bottom: 8px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.bt-device:active {
    background: var(--primary-dark);
}

.bt-device-name {
    font-weight: bold;
    color: var(--text-primary);
}

.bt-device-addr {
    font-size: 0.8rem;
    color: var(--text-secondary);
}
