:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #273549;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #3b82f6;
  --accent-2: #22d3ee;
  --danger: #ef4444;
  --ok: #22c55e;
  --border: #334155;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { font-weight: 700; font-size: 18px; }
.ctx { color: var(--muted); font-size: 14px; }

.screen { padding: 20px; max-width: 1000px; margin: 0 auto; }
.hidden { display: none !important; }

.card.hero {
  background: linear-gradient(135deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 40px;
  margin-top: 40px;
  text-align: center;
}
.hero h1 { margin-top: 0; font-size: 28px; }
.hero p { color: var(--muted); }
.hero-actions { display: flex; flex-direction: column; gap: 14px; align-items: center; }
.join-row { display: flex; gap: 8px; }

input, select {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 14px;
}
input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.name-hint { font-size: 13px; }
.name-hint input { max-width: 160px; }

.btn {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 14px;
  cursor: pointer;
  transition: background .15s, transform .05s;
}
.btn:hover { background: #31445e; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { background: #2563eb; }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn.sos { background: var(--danger); border-color: var(--danger); color: #fff; font-weight: 700; width: 100%; }
.btn.sos:hover { background: #b91c1c; }

.msg { min-height: 20px; color: var(--ok); font-size: 14px; }
.msg.error { color: var(--danger); }

/* Layout */
.layout { display: grid; grid-template-columns: 1fr 340px; gap: 16px; }

.map-wrap { position: relative; border: 1px solid var(--border); border-radius: 12px; overflow: hidden; background: #0b1220; }
#map { width: 100%; height: calc(100vh - 160px); min-height: 420px; background: #0b1220; z-index: 1; }

.map-top-actions { position: absolute; right: 12px; top: 12px; display: flex; flex-direction: column; gap: 8px; z-index: 5; }
.map-top-actions .btn.round { width: 44px; height: 44px; border-radius: 50%; font-size: 20px; padding: 0; display: flex; align-items: center; justify-content: center; box-shadow: 0 2px 8px rgba(0,0,0,.4); }

/* ============ Mobile layout ============ */
.mobile-only { display: none; }

@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; display: block; }
  .screen { padding: 0; max-width: none; }

  /* App-like header: colorful status region + compact title bar */
  .topbar {
    padding-top: calc(8px + env(safe-area-inset-top, 0));
    padding-bottom: 10px;
    background: linear-gradient(180deg, rgba(59,130,246,.18), rgba(15,23,42,0) 120%), var(--panel);
    border-bottom: 1px solid rgba(255,255,255,.04);
  }
  .brand { font-size: 17px; }
  .ctx { font-size: 12px; max-width: 60%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  /* Full-screen map with rounded corners floating above docked bottom bar */
  .map-wrap {
    border-radius: 0;
    border: none;
    height: calc(100vh - env(safe-area-inset-top, 0) - 144px);
    min-height: 0;
    background: #0b1220;
  }
  #map { height: 100%; min-height: 0; }

  /* ---- Floating bottom docked tab bar ---- */
  .bottom-nav {
    position: fixed; left: 10px; right: 10px; bottom: calc(8px + env(safe-area-inset-bottom, 0));
    z-index: 30;
    display: flex; justify-content: space-around; align-items: stretch;
    background: rgba(30,41,59,.92);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    border: 1px solid rgba(255,255,255,.08);
    border-radius: 20px;
    padding: 6px 4px;
    box-shadow: 0 10px 30px rgba(0,0,0,.5);
  }
  .bnav-item {
    flex: 1; min-width: 0; background: transparent; border: none;
    color: var(--muted); cursor: pointer;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 3px; padding: 6px 2px 5px; border-radius: 14px;
    transition: color .15s, background .15s;
    -webkit-tap-highlight-color: transparent;
  }
  .bnav-ic { font-size: 21px; line-height: 1; }
  .bnav-lb { font-size: 10px; font-weight: 600; }
  .bnav-item:active { transform: scale(.92); }
  .bnav-item.active { color: #fff; background: rgba(59,130,246,.22); }

  /* ---- Native bottom-sheet panel ---- */
  .panel {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto;
    max-height: 70vh;
    background: var(--panel);
    border-radius: 22px 22px 0 0;
    border: 1px solid rgba(255,255,255,.06); border-bottom: none;
    padding: 0 18px 24px;
    z-index: 25; display: none;
    box-shadow: 0 -10px 40px rgba(0,0,0,.6);
    -webkit-overflow-scrolling: touch;
    animation: sheetUp .28s cubic-bezier(.32,.72,.35,1);
  }
  @keyframes sheetUp {
    from { transform: translateY(40px); opacity: .4; }
    to { transform: translateY(0); opacity: 1; }
  }
  .panel.open { display: flex; flex-direction: column; overflow-y: auto; }
  .sheet-handle { width: 44px; height: 5px; border-radius: 3px; background: rgba(148,163,184,.35); margin: 10px auto 6px; flex-shrink: 0; }
  .sheet-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 4px 0 12px; border-bottom: 1px solid var(--border); margin-bottom: 8px;
  }
  .sheet-title { font-size: 19px; font-weight: 800; color: var(--text); }
  .sheet-close {
    background: rgba(148,163,184,.14); border: none; color: var(--text);
    width: 32px; height: 32px; border-radius: 50%; font-size: 15px; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
  }
  .sheet-close:active { background: rgba(148,163,184,.3); }
  .panel-section { max-height: none; background: var(--panel); border: none; border-radius: 0; padding: 10px 2px; }
  .panel-section h2 { font-size: 13px; color: var(--accent-2); letter-spacing: .4px; text-transform: uppercase; }
  .panel-section + .panel-section { border-top: 1px solid var(--border); }

  .mobile-only { display: flex; }

  /* Floating map actions (FABs) */
  .map-top-actions { top: 12px; right: 12px; gap: 10px; }
  .map-top-actions .btn.round {
    width: 48px; height: 48px; font-size: 21px;
    background: rgba(30,41,59,.92);
    backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
    border: 1px solid rgba(255,255,255,.1);
    box-shadow: 0 6px 18px rgba(0,0,0,.45);
  }
  .map-top-actions .btn.round.active { background: var(--accent); color: #fff; }
  .map-top-actions .btn.round:active { transform: scale(.92); }

  /* Typography scale for lists / forms */
  .btn { padding: 14px 18px; font-size: 16px; border-radius: 12px; }
  .btn.small { padding: 12px 16px; font-size: 15px; border-radius: 10px; }
  input, select { padding: 13px 14px; font-size: 16px; border-radius: 10px; }

  .hero { margin-top: 24px; padding: 30px 22px; border-radius: 20px; }
  .hero h1 { font-size: 24px; }
  .name-hint input { max-width: 100%; width: 100%; }

  /* Cards are full-bleed separated rows */
  .member-list, .place-list, .sos-list { margin: 8px 0 0; }
  .member-list li, .place-list li {
    background: var(--panel-2);
    border: 1px solid rgba(255,255,255,.05);
    border-bottom: 1px solid rgba(255,255,255,.05);
    border-radius: 14px;
    padding: 14px 14px; margin-bottom: 8px; font-size: 15px;
  }
  .sos-list li { font-size: 14px; }

  .chat-toggle { bottom: calc(88px + env(safe-area-inset-bottom, 0)); right: 18px; width: 58px; height: 58px; }
  .chat-panel { width: 100vw; height: 68vh; border-radius: 22px 22px 0 0; bottom: 0; }

  .code-box { font-size: 36px; border-radius: 14px; }
  .toast { bottom: calc(100px + env(safe-area-inset-bottom, 0)); }
}

/* bottom-nav hidden on desktop (base .mobile-only rule) */
.bottom-nav { display: none; }
@media (max-width: 820px) { .bottom-nav { display: flex; } }

/* Sheet handle + header are mobile-only chrome (hidden on desktop panel) */
.sheet-handle, .sheet-header { display: none; }
@media (max-width: 820px) { .sheet-handle, .sheet-header { display: flex; } }
.sheet-handle { align-self: center; }

.locs-marker { background: transparent; border: none; }
.locs-dot {
  width: 18px; height: 18px; border-radius: 50%;
  border: 3px solid #fff; box-shadow: 0 1px 5px rgba(0,0,0,.5);
}
.locs-dot.me { background: var(--accent); }
.locs-dot.other { background: var(--danger); }
.locs-label { background: var(--panel); border: 1px solid var(--border); border-radius: 6px; padding: 2px 6px; font-weight: 600; color: var(--text); font-size: 12px; }
.locs-avatar {
  display: inline-block; width: 24px; height: 24px; border-radius: 50%;
  color: #fff; font-weight: 700; text-align: center; line-height: 24px;
  margin-right: 6px; vertical-align: -6px; font-size: 13px;
}
.locs-avatar + strong { font-size: 14px; }

.g-label {
  position: absolute;
  white-space: nowrap;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 11px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 6px;
  transform: translate(-50%, calc(-100% - 12px));
  pointer-events: none;
  z-index: 10;
}
.maploading, .maperror, .maplabel {
  position: absolute; left: 12px; right: 12px; text-align: center;
  background: rgba(15,23,42,.85); padding: 10px; border-radius: 8px; z-index: 5;
}
.maploading { top: 12px; }
.maperror { top: 12px; color: var(--danger); }
.legend {
  position: absolute; bottom: 12px; left: 12px;
  background: rgba(30,41,59,.9); border-radius: 8px; padding: 8px 10px; font-size: 12px; z-index: 5;
}

.panel { display: flex; flex-direction: column; gap: 14px; }
.panel-section {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 12px; padding: 14px;
}
.panel-section h2 { margin: 0 0 10px; font-size: 15px; color: var(--accent-2); border-bottom: 1px solid var(--border); padding-bottom: 6px; }

.code-box {
  font-family: ui-monospace, monospace; font-size: 28px; letter-spacing: 6px;
  text-align: center; background: var(--bg); border: 1px dashed var(--border);
  border-radius: 8px; padding: 12px; color: var(--accent-2); margin: 6px 0;
  cursor: pointer; user-select: all;
}
.code-label { margin: 0 0 6px; }
.hint { color: var(--muted); font-size: 12px; margin: 6px 0 0; }

.toggle { display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; margin-bottom: 6px; }
.toggle input { width: 18px; height: 18px; accent-color: var(--accent); }
#tempControls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; font-size: 13px; }
#tempControls input { width: 80px; }

.place-form { display: flex; flex-direction: column; gap: 8px; }
.place-list, .member-list, .sos-list { list-style: none; padding: 0; margin: 10px 0 0; }
.place-list li { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.del { background: transparent; border: none; color: var(--danger); cursor: pointer; font-size: 16px; }

.member-list li { display: flex; justify-content: space-between; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.member-list .dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; }
.dot.on { background: var(--ok); }
.dot.off { background: var(--muted); }
.member-list li.stale { opacity: .55; }
.member-list li .del { margin-left: auto; }
.rename-row { display: flex; gap: 6px; margin-top: 10px; }
.rename-row input { flex: 1; }
.offline-banner { position: absolute; top: 8px; left: 50%; transform: translateX(-50%); background: rgba(234,88,12,.9); color: #fff; font-size: 12px; padding: 4px 12px; border-radius: 999px; z-index: 900; pointer-events: none; }
.map-top-actions { position: absolute; top: 10px; right: 10px; display: flex; flex-direction: column-reverse; gap: 8px; z-index: 810; }
.map-top-actions .btn.round.active { background: var(--accent-2); color: #000; }
.badge { font-size: 11px; color: var(--muted); background: var(--bg); padding: 2px 6px; border-radius: 6px; }

.sos-list li { padding: 8px; margin-bottom: 8px; background: rgba(239,68,68,.12); border: 1px solid rgba(239,68,68,.4); border-radius: 8px; font-size: 13px; }
.sos-list .sos-time { color: var(--muted); font-size: 11px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--accent); color: var(--text);
  padding: 12px 18px; border-radius: 10px; z-index: 100; max-width: 90%;
  box-shadow: 0 6px 20px rgba(0,0,0,.4);
}
.toast.danger { border-color: var(--danger); }

.chat-toggle {
  position: fixed; bottom: 80px; right: 20px; z-index: 50;
  width: 52px; height: 52px; border-radius: 50%;
  background: var(--accent); border: none; color: #fff;
  font-size: 22px; cursor: pointer;
  box-shadow: 0 4px 14px rgba(59,130,246,.5);
  display: flex; align-items: center; justify-content: center;
}
.chat-toggle:hover { background: #2563eb; }

.chat-panel {
  position: fixed; bottom: 0; right: 0; z-index: 40;
  width: 380px; max-width: 100vw; height: 55vh;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 16px 0 0 0;
  display: flex; flex-direction: column;
  box-shadow: 0 -4px 24px rgba(0,0,0,.5);
}
@media (max-width: 500px) { .chat-panel { width: 100vw; height: 60vh; border-radius: 16px 16px 0 0; bottom: 0; } }

.chat-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  font-weight: 700; font-size: 15px; color: var(--accent-2);
}
.chat-close {
  background: transparent; border: none; color: var(--muted);
  font-size: 18px; cursor: pointer; padding: 4px 8px;
}
.chat-close:hover { color: var(--text); }

.chat-messages {
  flex: 1; overflow-y: auto; padding: 12px 14px;
  display: flex; flex-direction: column; gap: 8px;
}
.chat-msg {
  max-width: 85%; padding: 8px 12px; border-radius: 12px;
  font-size: 13px; line-height: 1.45; word-break: break-word;
  background: var(--bg); border: 1px solid var(--border);
}
.chat-msg.me { align-self: flex-end; background: var(--accent); border-color: var(--accent); color: #fff; }
.chat-msg .chat-msg-name { font-size: 11px; font-weight: 700; color: var(--accent-2); margin-bottom: 2px; }
.chat-msg.me .chat-msg-name { color: rgba(255,255,255,.7); }
.chat-msg .chat-msg-text { margin: 0; }
.chat-msg .chat-msg-time { font-size: 10px; color: var(--muted); margin-top: 3px; text-align: right; }
.chat-msg.me .chat-msg-time { color: rgba(255,255,255,.6); }
.chat-date-sep { text-align: center; font-size: 11px; color: var(--muted); padding: 4px 0; }

.chat-input-row {
  display: flex; gap: 8px; padding: 10px 14px;
  border-top: 1px solid var(--border);
}
.chat-input-row input { flex: 1; }
