/* ===== CSS Variables / Themes ===== */
:root, [data-theme="dark"] {
  --bg: #0f1923;
  --bg2: #1a2a3a;
  --bg3: #162232;
  --card: linear-gradient(135deg, #1a2a3a, #162232);
  --border: rgba(79,195,247,0.1);
  --text: #e8edf2;
  --text2: #8899aa;
  --text3: #556677;
  --accent: #4fc3f7;
  --accent2: #0288d1;
  --green: #4caf50;
  --orange: #ff9800;
  --purple: #9c27b0;
  --shadow: rgba(0,0,0,0.3);
  --sheet-bg: #141e2a;
  --input-bg: #1a2a3a;
}

[data-theme="light"] {
  --bg: #f5f7fa;
  --bg2: #ffffff;
  --bg3: #f8f9fb;
  --card: linear-gradient(135deg, #ffffff, #f8f9fb);
  --border: rgba(0,0,0,0.08);
  --text: #1a1a2e;
  --text2: #888888;
  --text3: #aaaaaa;
  --accent: #1976d2;
  --accent2: #1565c0;
  --green: #22c55e;
  --orange: #f59e0b;
  --purple: #9c27b0;
  --shadow: rgba(0,0,0,0.08);
  --sheet-bg: #ffffff;
  --input-bg: #f0f2f5;
}

[data-theme="minimal"] {
  --bg: #fafbfc;
  --bg2: #ffffff;
  --bg3: #f0f0f5;
  --card: linear-gradient(135deg, #ffffff, #fafbfc);
  --border: rgba(0,0,0,0.04);
  --text: #1a1a1a;
  --text2: #888888;
  --text3: #bbbbbb;
  --accent: #667eea;
  --accent2: #764ba2;
  --green: #22c55e;
  --orange: #f59e0b;
  --purple: #9c27b0;
  --shadow: rgba(0,0,0,0.04);
  --sheet-bg: #ffffff;
  --input-bg: #f0f0f5;
}

/* ===== Reset ===== */
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* ===== Views ===== */
.view { display: none; height: 100vh; flex-direction: column; }
.view.active { display: flex; }

/* ===== Login Screen ===== */
#login-view {
  align-items: center;
  justify-content: center;
  gap: 24px;
  padding: 32px;
  text-align: center;
}
.login-logo { font-size: 64px; margin-bottom: 8px; }
.login-title { font-size: 28px; font-weight: 800; }
.login-title span { color: var(--accent); }
.login-subtitle { color: var(--text2); font-size: 14px; margin-top: 4px; }

.pin-form { width: 100%; max-width: 300px; }
.pin-input {
  width: 100%;
  padding: 16px;
  font-size: 24px;
  text-align: center;
  letter-spacing: 12px;
  border: 2px solid var(--border);
  border-radius: 14px;
  background: var(--input-bg);
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
}
.pin-input:focus { border-color: var(--accent); }
.pin-input::placeholder { letter-spacing: 4px; font-size: 16px; color: var(--text3); }

.pin-btn {
  width: 100%;
  padding: 16px;
  margin-top: 12px;
  border: none;
  border-radius: 14px;
  background: var(--accent);
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
}
.pin-btn:active { opacity: 0.8; }
.pin-btn:disabled { opacity: 0.5; cursor: default; }

.login-error {
  color: #ef4444;
  font-size: 14px;
  min-height: 20px;
}

.login-divider {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  max-width: 300px;
  color: var(--text3);
  font-size: 12px;
}
.login-divider::before, .login-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

#telegram-login-container { min-height: 44px; }

/* ===== Map View ===== */
#map-view { position: relative; }
#map {
  flex: 1;
  z-index: 0;
}

/* Map controls overlay */
.map-topbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  padding: 48px 16px 12px;
  background: linear-gradient(180deg, var(--bg) 0%, transparent 100%);
  display: flex;
  align-items: center;
  justify-content: space-between;
  pointer-events: none;
}
.map-topbar > * { pointer-events: auto; }

.map-title {
  font-size: 20px;
  font-weight: 700;
}
.map-title span { color: var(--accent); }

.map-actions { display: flex; gap: 8px; }
.map-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--bg2);
  border: 1px solid var(--border);
  color: var(--text2);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px var(--shadow);
}
.map-btn:active { background: var(--bg3); }

/* FAB */
.fab {
  position: absolute;
  z-index: 1000;
  right: 16px;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  color: white;
  font-size: 22px;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(79,195,247,0.3);
  transition: transform 0.2s;
}
.fab:active { transform: scale(0.9); }
.fab-refresh { bottom: 260px; }
.fab-center { bottom: 320px; right: 16px; }

/* ===== Bottom Sheet ===== */
.sheet {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background: var(--sheet-bg);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -4px 20px var(--shadow);
  max-height: 60vh;
  min-height: 80px;
  transition: max-height 0.3s ease;
  display: flex;
  flex-direction: column;
}
.sheet.collapsed { max-height: 80px; overflow: hidden; }
.sheet-handle {
  width: 36px;
  height: 4px;
  background: var(--text3);
  border-radius: 2px;
  margin: 10px auto;
  cursor: pointer;
  flex-shrink: 0;
}
.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 20px 12px;
  flex-shrink: 0;
}
.sheet-title { font-size: 18px; font-weight: 700; }
.sheet-badge {
  background: rgba(79,195,247,0.15);
  color: var(--accent);
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
}

.device-list {
  padding: 0 16px 24px;
  overflow-y: auto;
  flex: 1;
}

.device-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px;
  background: var(--bg3);
  border-radius: 14px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}
.device-item:active { transform: scale(0.98); background: var(--bg2); }

.device-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  position: relative;
  flex-shrink: 0;
}
.status-dot {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid var(--sheet-bg);
}

.device-info { flex: 1; min-width: 0; }
.device-name { font-weight: 600; font-size: 15px; }
.device-detail {
  font-size: 13px;
  color: var(--text2);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.device-meta { text-align: right; flex-shrink: 0; }
.device-time { font-size: 12px; color: var(--text3); }
.device-speed { font-size: 13px; font-weight: 600; color: var(--accent); margin-top: 2px; }
.device-route-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg2);
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}
.device-route-btn:active { background: var(--accent); }
.device-arrow { color: var(--text3); font-size: 18px; }

/* ===== Route Panel ===== */
.route-panel {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1001;
  padding: 48px 16px 16px;
  background: var(--bg);
  border-radius: 0 0 20px 20px;
  box-shadow: 0 4px 20px var(--shadow);
  display: none;
}
.route-panel.active { display: block; }

.route-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.route-back {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--bg3);
  border: none;
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
}
.route-title { font-size: 18px; font-weight: 700; flex: 1; margin-left: 12px; }

.route-periods {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}
.route-period {
  flex: 1;
  padding: 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text2);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}
.route-period.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.route-stats {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}
.route-stat {
  background: var(--bg3);
  border-radius: 10px;
  padding: 10px;
  text-align: center;
}
.route-stat-value { font-size: 18px; font-weight: 700; color: var(--accent); }
.route-stat-label { font-size: 11px; color: var(--text3); margin-top: 2px; }

/* ===== Settings View ===== */
#settings-view { overflow-y: auto; }

.settings-header {
  padding: 52px 20px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.settings-back {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--bg3);
  border: none;
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
}
.settings-title { font-size: 22px; font-weight: 700; }

.settings-section {
  padding: 0 20px;
  margin-bottom: 24px;
}
.settings-section-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text3);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 12px;
}

.settings-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: var(--bg2);
  border-radius: 12px;
  margin-bottom: 8px;
  cursor: pointer;
}
.settings-item-left { display: flex; align-items: center; gap: 12px; }
.settings-icon { font-size: 20px; }
.settings-label { font-size: 15px; }
.settings-value { font-size: 14px; color: var(--text2); }

.theme-options {
  display: flex;
  gap: 8px;
}
.theme-option {
  flex: 1;
  padding: 12px;
  border-radius: 12px;
  border: 2px solid var(--border);
  background: var(--bg2);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}
.theme-option.active {
  border-color: var(--accent);
  color: var(--accent);
}

.settings-logout {
  margin: 24px 20px;
  padding: 16px;
  border-radius: 14px;
  border: 1px solid #ef4444;
  background: rgba(239,68,68,0.1);
  color: #ef4444;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  width: calc(100% - 40px);
  text-align: center;
}

/* ===== Spinner ===== */
.spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 2px solid var(--text3);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--bg2);
  color: var(--text);
  padding: 12px 24px;
  border-radius: 12px;
  font-size: 14px;
  box-shadow: 0 4px 16px var(--shadow);
  z-index: 9999;
  opacity: 0;
  transition: all 0.3s ease;
  pointer-events: none;
}
.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* ===== Splash ===== */
#splash {
  position: fixed;
  inset: 0;
  z-index: 99999;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  transition: opacity 0.5s;
}
#splash.hide { opacity: 0; pointer-events: none; }
.splash-icon { font-size: 72px; }
.splash-text { font-size: 28px; font-weight: 800; }
.splash-text span { color: var(--accent); }

/* ===== Leaflet overrides ===== */
.leaflet-control-zoom { display: none !important; }
.leaflet-popup-content-wrapper {
  border-radius: 12px !important;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15) !important;
}
.leaflet-popup-content { margin: 12px 16px !important; font-size: 14px; }
.popup-name { font-weight: 700; font-size: 16px; margin-bottom: 4px; }
.popup-info { color: #666; font-size: 13px; line-height: 1.6; }

/* Custom marker */
.custom-marker {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  font-size: 24px;
  border: 3px solid white;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  position: relative;
}
.custom-marker::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 8px solid white;
}
.marker-online { background: rgba(76,175,80,0.2); }
.marker-offline { background: rgba(255,152,0,0.2); }
.marker-pulse {
  animation: marker-pulse 2s infinite;
}
@keyframes marker-pulse {
  0%, 100% { box-shadow: 0 4px 12px rgba(0,0,0,0.2); }
  50% { box-shadow: 0 4px 12px rgba(0,0,0,0.2), 0 0 0 8px rgba(79,195,247,0.15); }
}

/* ===== Pull to refresh ===== */
.ptr-indicator {
  position: absolute;
  top: -40px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1001;
  transition: top 0.2s;
}
.ptr-indicator.active { top: 100px; }

/* ===== Safe area ===== */
@supports (padding-top: env(safe-area-inset-top)) {
  .map-topbar { padding-top: calc(env(safe-area-inset-top) + 8px); }
  .settings-header { padding-top: calc(env(safe-area-inset-top) + 8px); }
  .route-panel { padding-top: calc(env(safe-area-inset-top) + 8px); }
  #login-view { padding-top: env(safe-area-inset-top); }
}
