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

:root {
  --bg: #1a1a2e;
  --bg2: #16213e;
  --surface: #0f3460;
  --accent: #e94560;
  --accent-glow: rgba(233, 69, 96, 0.4);
  --green: #00d474;
  --green-glow: rgba(0, 212, 116, 0.3);
  --text: #f0f0f0;
  --text2: #a0a0b0;
  --radius: 16px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body { 
  height: 100%; 
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

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

/* Screens */
.screen { display: none; flex-direction: column; height: 100%; }
.screen.active { display: flex; }

/* Auth screen */
.auth-screen {
  justify-content: center;
  align-items: center;
  padding: 32px;
  text-align: center;
}
.auth-logo {
  width: 80px; height: 80px;
  background: var(--accent);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 36px;
  margin-bottom: 24px;
  box-shadow: 0 0 30px var(--accent-glow);
}
.auth-title { font-size: 28px; font-weight: 700; margin-bottom: 8px; }
.auth-subtitle { color: var(--text2); margin-bottom: 32px; font-size: 14px; }

.input-group {
  width: 100%; max-width: 320px;
  margin-bottom: 16px;
}
.input-group label {
  display: block; text-align: left;
  font-size: 12px; color: var(--text2);
  margin-bottom: 6px; text-transform: uppercase;
  letter-spacing: 0.5px;
}
.input-group input {
  width: 100%; padding: 14px 16px;
  background: var(--bg2);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 12px;
  color: var(--text);
  font-size: 16px;
  outline: none;
  transition: border-color 0.2s;
}
.input-group input:focus { border-color: var(--accent); }

.btn {
  padding: 14px 32px;
  border: none; border-radius: 12px;
  font-size: 16px; font-weight: 600;
  cursor: pointer; transition: all 0.2s;
  width: 100%; max-width: 320px;
}
.btn-primary {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 4px 20px var(--accent-glow);
}
.btn-primary:active { transform: scale(0.97); }
.btn-secondary {
  background: transparent;
  color: var(--accent);
  margin-top: 12px;
}

.auth-error {
  color: var(--accent);
  font-size: 13px;
  margin-top: 8px;
  min-height: 20px;
}
.auth-toggle { margin-top: 24px; color: var(--text2); font-size: 14px; }
.auth-toggle a { color: var(--accent); text-decoration: none; cursor: pointer; }

/* Home screen */
.home-screen { position: relative; }
.home-header {
  padding: 20px 24px 16px;
  display: flex; align-items: center; justify-content: space-between;
}
.home-user { display: flex; align-items: center; gap: 12px; }
.home-avatar {
  width: 40px; height: 40px;
  background: var(--surface);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
}
.home-name { font-weight: 600; font-size: 16px; }
.home-status { font-size: 12px; color: var(--green); }
.home-settings {
  width: 36px; height: 36px;
  background: var(--surface);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; font-size: 18px;
  border: none; color: var(--text);
}

.home-body {
  flex: 1;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 24px;
}
.buddy-count {
  background: var(--bg2);
  border-radius: var(--radius);
  padding: 16px 24px;
  text-align: center;
  margin-bottom: 40px;
  width: 100%;
}
.buddy-count .number { font-size: 48px; font-weight: 700; color: var(--green); }
.buddy-count .label { font-size: 13px; color: var(--text2); margin-top: 4px; }

.sos-btn {
  width: 160px; height: 160px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  color: #fff;
  font-size: 32px;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 0 40px var(--accent-glow), 0 0 80px rgba(233,69,96,0.2);
  transition: all 0.2s;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  -webkit-user-select: none; user-select: none;
}
.sos-btn:active { transform: scale(0.93); box-shadow: 0 0 60px var(--accent-glow); }
.sos-btn .icon { font-size: 40px; margin-bottom: 4px; }
.sos-btn .text { font-size: 14px; font-weight: 600; opacity: 0.9; }

.sos-btn.active {
  background: #ff2244;
  animation: pulse-alarm 1s infinite;
}
@keyframes pulse-alarm {
  0%, 100% { box-shadow: 0 0 40px rgba(255,34,68,0.6); }
  50% { box-shadow: 0 0 80px rgba(255,34,68,0.9), 0 0 120px rgba(255,34,68,0.4); }
}

.sos-cancel {
  margin-top: 24px;
  background: rgba(255,255,255,0.1);
  border: none; border-radius: 12px;
  color: var(--text); padding: 12px 24px;
  font-size: 14px; cursor: pointer;
  display: none;
}
.sos-cancel.show { display: block; }

/* Nav bar */
.nav-bar {
  display: flex;
  background: var(--bg2);
  border-top: 1px solid rgba(255,255,255,0.05);
  padding: 8px 0;
  padding-bottom: calc(8px + var(--safe-bottom));
}
.nav-item {
  flex: 1;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 8px 0;
  cursor: pointer;
  color: var(--text2);
  font-size: 10px;
  gap: 4px;
  border: none; background: none;
  transition: color 0.2s;
}
.nav-item.active { color: var(--accent); }
.nav-item .nav-icon { font-size: 22px; }
.nav-badge {
  position: absolute;
  top: 2px; right: calc(50% - 18px);
  width: 8px; height: 8px;
  background: var(--accent);
  border-radius: 50%;
  display: none;
}
.nav-item { position: relative; }

/* Map screen */
.map-screen { position: relative; }
.map-header {
  padding: 16px 24px;
  background: var(--bg);
  display: flex; align-items: center; justify-content: space-between;
  z-index: 1000;
}
.map-header h2 { font-size: 18px; font-weight: 600; }
.map-back {
  background: var(--surface);
  border: none; border-radius: 8px;
  color: var(--text); padding: 8px 12px;
  font-size: 13px; cursor: pointer;
}
#map-container {
  flex: 1;
  z-index: 1;
}

/* Alert overlay */
.alert-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.9);
  z-index: 9999;
  display: none;
  flex-direction: column;
  align-items: center; justify-content: center;
  padding: 32px;
  text-align: center;
  animation: alert-flash 0.5s ease;
}
.alert-overlay.active { display: flex; }
@keyframes alert-flash {
  0%, 50% { background: rgba(233,69,96,0.3); }
  100% { background: rgba(0,0,0,0.9); }
}
.alert-icon { font-size: 64px; margin-bottom: 16px; animation: shake 0.5s infinite; }
@keyframes shake {
  0%, 100% { transform: rotate(0deg); }
  25% { transform: rotate(-10deg); }
  75% { transform: rotate(10deg); }
}
.alert-title { font-size: 24px; font-weight: 700; margin-bottom: 8px; color: var(--accent); }
.alert-distance { font-size: 16px; color: var(--text2); margin-bottom: 8px; }
.alert-name { font-size: 18px; margin-bottom: 32px; }
.alert-actions { display: flex; gap: 16px; width: 100%; max-width: 320px; }
.alert-actions .btn { flex: 1; padding: 16px; }
.btn-respond { background: var(--green); color: #fff; box-shadow: 0 4px 20px var(--green-glow); }
.btn-dismiss { background: var(--surface); color: var(--text); }

/* Chat screen */
.chat-screen { position: relative; }
.chat-header {
  padding: 16px 24px;
  background: var(--bg);
  border-bottom: 1px solid rgba(255,255,255,0.05);
  display: flex; align-items: center; gap: 12px;
}
.chat-header h2 { font-size: 18px; font-weight: 600; flex: 1; }
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex; flex-direction: column;
  gap: 8px;
}
.chat-msg {
  max-width: 80%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.4;
}
.chat-msg.mine {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.chat-msg.theirs {
  align-self: flex-start;
  background: var(--surface);
  border-bottom-left-radius: 4px;
}
.chat-msg .msg-sender { font-size: 11px; color: var(--text2); margin-bottom: 4px; }
.chat-msg .msg-time { font-size: 10px; opacity: 0.6; margin-top: 4px; text-align: right; }
.chat-input-bar {
  display: flex; gap: 8px;
  padding: 12px 16px;
  padding-bottom: calc(12px + var(--safe-bottom));
  background: var(--bg2);
  border-top: 1px solid rgba(255,255,255,0.05);
}
.chat-input-bar input {
  flex: 1;
  background: var(--bg);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 20px;
  padding: 10px 16px;
  color: var(--text);
  font-size: 14px;
  outline: none;
}
.chat-input-bar button {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  color: #fff;
  font-size: 18px;
  cursor: pointer;
}

/* Check-in screen */
.checkin-screen {}
.checkin-header {
  padding: 16px 24px;
  background: var(--bg);
  border-bottom: 1px solid rgba(255,255,255,0.05);
}
.checkin-header h2 { font-size: 18px; font-weight: 600; }
.checkin-list {
  flex: 1; overflow-y: auto;
  padding: 16px;
}
.checkin-item {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 16px;
  background: var(--bg2);
  border-radius: 12px;
  margin-bottom: 10px;
}
.checkin-avatar {
  width: 42px; height: 42px;
  background: var(--surface);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
}
.checkin-info { flex: 1; }
.checkin-name { font-weight: 600; font-size: 14px; }
.checkin-time { font-size: 12px; color: var(--text2); }
.checkin-status {
  padding: 4px 10px;
  border-radius: 8px;
  font-size: 11px;
  font-weight: 600;
}
.checkin-status.safe { background: rgba(0,212,116,0.15); color: var(--green); }
.checkin-status.alert { background: rgba(233,69,96,0.15); color: var(--accent); }

.checkin-btn {
  margin: 16px;
  padding: 14px;
  background: var(--green);
  color: #fff;
  border: none;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 20px var(--green-glow);
}
.checkin-btn:active { transform: scale(0.97); }

/* Settings modal */
.settings-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.8);
  z-index: 5000;
  display: none;
  align-items: flex-end;
}
.settings-overlay.active { display: flex; }
.settings-panel {
  width: 100%;
  background: var(--bg2);
  border-radius: 24px 24px 0 0;
  padding: 24px;
  padding-bottom: calc(24px + var(--safe-bottom));
  max-height: 70vh;
  overflow-y: auto;
}
.settings-panel h3 {
  font-size: 18px; margin-bottom: 20px;
  display: flex; align-items: center; justify-content: space-between;
}
.settings-panel h3 button {
  background: none; border: none;
  color: var(--text2); font-size: 24px; cursor: pointer;
}
.settings-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 0;
  border-bottom: 1px solid rgba(255,255,255,0.05);
}
.settings-item label { font-size: 14px; }
.settings-item .value { color: var(--text2); font-size: 13px; }
.btn-logout {
  width: 100%; margin-top: 20px;
  padding: 14px;
  background: rgba(233,69,96,0.15);
  color: var(--accent);
  border: 1px solid rgba(233,69,96,0.3);
  border-radius: 12px;
  font-size: 15px; font-weight: 600;
  cursor: pointer;
}

/* Toast */
.toast {
  position: fixed;
  top: 60px; left: 50%;
  transform: translateX(-50%) translateY(-100px);
  background: var(--surface);
  color: var(--text);
  padding: 12px 20px;
  border-radius: 12px;
  font-size: 14px;
  z-index: 8000;
  transition: transform 0.3s ease;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}
.toast.show { transform: translateX(-50%) translateY(0); }

/* Loading */
.loading {
  display: flex; align-items: center; justify-content: center;
  padding: 40px; color: var(--text2);
}
.spinner {
  width: 24px; height: 24px;
  border: 3px solid var(--surface);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-right: 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Leaflet overrides */
.leaflet-container { background: var(--bg) !important; }
.buddy-marker {
  background: var(--green);
  border: 3px solid #fff;
  border-radius: 50%;
  width: 16px !important; height: 16px !important;
  margin: -8px 0 0 -8px !important;
}
.buddy-marker.distress {
  background: var(--accent);
  animation: pulse-alarm 1s infinite;
  width: 24px !important; height: 24px !important;
  margin: -12px 0 0 -12px !important;
}
.buddy-marker.me {
  background: #4dabf7;
  border-color: #fff;
}
