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

:root {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-card: #0f3460;
  --bg-overlay: rgba(26, 26, 46, 0.95);
  --text-primary: #e8e8e8;
  --text-secondary: #a0a0b8;
  --text-muted: #6c6c8a;
  --accent: #4ECDC4;
  --accent-hover: #45b7aa;
  --danger: #FF6B6B;
  --danger-hover: #e05555;
  --success: #82E0AA;
  --warning: #F7DC6F;
  --border: rgba(255, 255, 255, 0.08);
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
  --radius: 12px;
  --radius-sm: 8px;
  --transition: 0.2s ease;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

/* Map container */
#map {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
}

/* Status bar at top */
.status-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background: var(--bg-overlay);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  display: flex;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  transition: transform var(--transition);
}

.status-bar.hidden {
  transform: translateY(-100%);
}

.status-item {
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--text-secondary);
}

.status-item .icon {
  font-size: 14px;
}

.status-item .value {
  color: var(--text-primary);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.status-item.active .value {
  color: var(--accent);
}

.status-item.warning .value {
  color: var(--warning);
}

.status-item.error .value {
  color: var(--danger);
}

.gps-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: none;
}

.gps-dot.active {
  background: var(--success);
  animation: pulse 2s infinite;
}

.gps-dot.poor {
  background: var(--warning);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.5; transform: scale(0.8); }
}

/* Control buttons */
.controls {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  align-items: center;
  gap: 12px;
  padding-bottom: var(--safe-bottom);
}

.btn-track {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.2);
  background: var(--accent);
  color: var(--bg-primary);
  font-size: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all var(--transition);
  box-shadow: var(--shadow), 0 0 20px rgba(78, 205, 196, 0.3);
  -webkit-tap-highlight-color: transparent;
}

.btn-track:active {
  transform: scale(0.92);
}

.btn-track.recording {
  background: var(--danger);
  box-shadow: var(--shadow), 0 0 20px rgba(255, 107, 107, 0.4);
  animation: recordPulse 1.5s infinite;
}

@keyframes recordPulse {
  0%, 100% { box-shadow: var(--shadow), 0 0 20px rgba(255, 107, 107, 0.4); }
  50% { box-shadow: var(--shadow), 0 0 35px rgba(255, 107, 107, 0.6); }
}

.btn-circle {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: none;
  background: var(--bg-overlay);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  color: var(--text-primary);
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all var(--transition);
  box-shadow: var(--shadow);
}

.btn-circle:active {
  transform: scale(0.9);
  background: var(--bg-card);
}

.btn-circle.active {
  background: var(--accent);
  color: var(--bg-primary);
}

/* Right-side map controls */
.map-controls {
  position: fixed;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.btn-map-ctrl {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  border: none;
  background: var(--bg-overlay);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  color: var(--text-primary);
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all var(--transition);
  box-shadow: var(--shadow);
}

.btn-map-ctrl:active {
  transform: scale(0.9);
}

.btn-map-ctrl.active {
  background: var(--accent);
  color: var(--bg-primary);
}

/* Panels */
.panel-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1999;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
}

.panel-overlay.visible {
  opacity: 1;
  visibility: visible;
}

.panel {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 85vh;
  z-index: 2000;
  background: var(--bg-secondary);
  border-radius: 20px 20px 0 0;
  transform: translateY(100%);
  transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
  display: flex;
  flex-direction: column;
  padding-bottom: var(--safe-bottom);
}

.panel.open {
  transform: translateY(0);
}

.panel-handle {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: var(--text-muted);
  margin: 10px auto 6px;
  flex-shrink: 0;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 20px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.panel-header h2 {
  font-size: 18px;
  font-weight: 700;
}

.panel-close {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: var(--border);
  color: var(--text-secondary);
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.panel-body {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 16px 20px;
  flex: 1;
}

/* Session list items */
.session-item {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  transition: all var(--transition);
  border: 1px solid transparent;
}

.session-item:active {
  transform: scale(0.98);
  border-color: var(--accent);
}

.session-color {
  width: 6px;
  height: 40px;
  border-radius: 3px;
  flex-shrink: 0;
}

.session-info {
  flex: 1;
  min-width: 0;
}

.session-name {
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-meta {
  display: flex;
  gap: 12px;
  margin-top: 4px;
  font-size: 12px;
  color: var(--text-secondary);
}

.session-meta span {
  display: flex;
  align-items: center;
  gap: 3px;
}

.session-status {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 600;
  flex-shrink: 0;
}

.session-status.active {
  background: rgba(78, 205, 196, 0.15);
  color: var(--accent);
}

.session-status.completed {
  background: rgba(130, 224, 170, 0.15);
  color: var(--success);
}

.session-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.btn-sm {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: none;
  background: var(--border);
  color: var(--text-secondary);
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all var(--transition);
}

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

/* Stats */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 16px;
}

.stat-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 16px;
  text-align: center;
}

.stat-value {
  font-size: 24px;
  font-weight: 800;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.stat-label {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 4px;
}

/* Session detail */
.detail-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}

.detail-color {
  width: 8px;
  height: 48px;
  border-radius: 4px;
  flex-shrink: 0;
}

.detail-title {
  flex: 1;
}

.detail-title input {
  width: 100%;
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--border);
  color: var(--text-primary);
  font-size: 18px;
  font-weight: 700;
  padding: 4px 0;
  outline: none;
}

.detail-title input:focus {
  border-bottom-color: var(--accent);
}

.detail-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.detail-notes textarea {
  width: 100%;
  min-height: 80px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  padding: 12px;
  font-size: 14px;
  font-family: inherit;
  resize: vertical;
  outline: none;
}

.detail-notes textarea:focus {
  border-color: var(--accent);
}

.detail-notes label {
  display: block;
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.detail-actions {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}

.btn-action {
  flex: 1;
  padding: 12px;
  border-radius: var(--radius-sm);
  border: none;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.btn-action:active {
  transform: scale(0.96);
}

.btn-action.primary {
  background: var(--accent);
  color: var(--bg-primary);
}

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

.btn-action.secondary {
  background: var(--bg-card);
  color: var(--text-primary);
}

/* Sync indicator */
.sync-badge {
  position: fixed;
  top: 52px;
  right: 12px;
  z-index: 1001;
  background: var(--warning);
  color: var(--bg-primary);
  font-size: 11px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 12px;
  display: none;
  animation: fadeIn 0.3s;
}

.sync-badge.visible {
  display: flex;
  align-items: center;
  gap: 4px;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Empty state */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
}

.empty-state .icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.empty-state p {
  font-size: 14px;
  line-height: 1.5;
}

/* Toast notifications */
.toast {
  position: fixed;
  top: 60px;
  left: 50%;
  transform: translateX(-50%) translateY(-20px);
  z-index: 3000;
  background: var(--bg-card);
  color: var(--text-primary);
  padding: 10px 20px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  box-shadow: var(--shadow);
  opacity: 0;
  transition: all 0.3s ease;
  pointer-events: none;
  white-space: nowrap;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast.error {
  border-left: 3px solid var(--danger);
}

.toast.success {
  border-left: 3px solid var(--success);
}

.toast.info {
  border-left: 3px solid var(--accent);
}

/* Leaflet overrides for dark theme */
.leaflet-control-zoom a {
  background: var(--bg-overlay) !important;
  color: var(--text-primary) !important;
  border-color: var(--border) !important;
  backdrop-filter: blur(8px);
}

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

.leaflet-control-attribution {
  background: rgba(26, 26, 46, 0.7) !important;
  color: var(--text-muted) !important;
  font-size: 10px !important;
}

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

/* Scrollbar */
.panel-body::-webkit-scrollbar {
  width: 4px;
}

.panel-body::-webkit-scrollbar-track {
  background: transparent;
}

.panel-body::-webkit-scrollbar-thumb {
  background: var(--text-muted);
  border-radius: 2px;
}

/* Loading spinner */
.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--text-muted);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* Confirm dialog */
.confirm-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.6);
  z-index: 5000;
  display: none;
  align-items: center;
  justify-content: center;
}

.confirm-overlay.show {
  display: flex;
}

.confirm-box {
  background: var(--bg-secondary);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 320px;
  width: 90%;
  text-align: center;
  box-shadow: var(--shadow);
}

.confirm-box p {
  margin-bottom: 20px;
  font-size: 15px;
  line-height: 1.5;
}

.confirm-box .btns {
  display: flex;
  gap: 10px;
}

.confirm-box .btns button {
  flex: 1;
  padding: 10px;
  border-radius: var(--radius-sm);
  border: none;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.confirm-box .btn-cancel {
  background: var(--bg-card);
  color: var(--text-primary);
}

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