:root {
  --amber: #FFBF00;
  --bg: #000;
  --panel: rgba(12, 12, 12, 0.92);
  --line: rgba(255, 191, 0, 0.35);
  --bar-h: 62px;
  /* Android WebView (Capacitor) exposes insets as CSS vars, browsers via env() */
  --sat: max(env(safe-area-inset-top, 0px), var(--safe-area-inset-top, 0px));
  --sab: max(env(safe-area-inset-bottom, 0px), var(--safe-area-inset-bottom, 0px));
  --sal: max(env(safe-area-inset-left, 0px), var(--safe-area-inset-left, 0px));
  --sar: max(env(safe-area-inset-right, 0px), var(--safe-area-inset-right, 0px));
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; height: 100%; background: var(--bg); color: var(--amber);
  font-family: Roboto, system-ui, -apple-system, "Segoe UI", sans-serif; overscroll-behavior: none; }
.hidden { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
input { font: inherit; }

/* ---------- Login ---------- */
.screen { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; padding: 24px; background: radial-gradient(circle at 50% 30%, #1d1600, #000 70%); }
.card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px; text-align: center; }
.card h1 { margin: 0 0 8px; letter-spacing: 2px; }
.logo { width: 96px; height: 96px; margin: 0 auto; border-radius: 22px; }
label { display: flex; flex-direction: column; gap: 6px; text-align: left; font-size: 14px; }
input[type=password], input[type=number], input[type=url], input[type=text] {
  background: #0b0b0b; color: var(--amber); border: 1px solid var(--line); border-radius: 10px; padding: 12px; font-size: 16px; outline: none; width: 100%; }
select { background: #0b0b0b; color: var(--amber); border: 1px solid var(--line); border-radius: 10px; padding: 12px; font-size: 16px; width: 100%; }
code { background: #111; padding: 2px 5px; border-radius: 5px; word-break: break-all; }
input:focus { border-color: var(--amber); }
details { text-align: left; font-size: 14px; }
summary { cursor: pointer; opacity: .8; padding: 4px 0; }
.btn { background: #111; border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; font-size: 15px; }
.btn:active { background: #222; }
.btn.primary { background: var(--amber); color: #000; border-color: var(--amber); font-weight: 600; }
.btn.danger { color: #ff6b6b; border-color: rgba(255, 107, 107, .4); }
.error { color: #ff6b6b; min-height: 1.2em; font-size: 14px; }
.msg { min-height: 1.2em; font-size: 14px; }
.msg.err { color: #ff6b6b; }
.msg.ok { color: #7dff7d; }

/* ---------- Map ---------- */
#app, #map { position: fixed; inset: 0; }
#map { background: #111; }
.leaflet-container { background: #111; font-family: inherit; }
.leaflet-bottom { bottom: calc(var(--bar-h) + var(--sab)); }
.leaflet-top { top: calc(var(--sat) + 44px); }
.leaflet-right { right: var(--sar); }
.leaflet-control-zoom a { background: #000 !important; color: var(--amber) !important; border-color: var(--line) !important; }
.leaflet-control-attribution { background: rgba(0,0,0,.5) !important; color: #999 !important; font-size: 9px !important; }
.leaflet-control-attribution a { color: #bbb !important; }
.leaflet-tooltip { background: rgba(0,0,0,0.55); color: var(--amber); border-color: var(--amber); font-weight: bold; box-shadow: none; }
.leaflet-tooltip-left:before { border-left-color: var(--amber); }
.leaflet-tooltip-right:before { border-right-color: var(--amber); }
.target-tooltip { font-size: 13px; padding: 2px 5px; }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: #111; color: var(--amber); }
.leaflet-popup-content { margin: 12px; }
.leaflet-popup-content .btn { width: 100%; margin-top: 8px; }

/* ---------- Top status ---------- */
#topbar { position: fixed; top: calc(var(--sat) + 8px); left: calc(var(--sal) + 8px); right: calc(var(--sar) + 8px); z-index: 1000; display: flex; flex-direction: column; align-items: flex-start; gap: 6px; pointer-events: none; }
#status-pill { pointer-events: auto; display: flex; align-items: center; gap: 10px; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 7px 12px; font-size: 14px; font-weight: bold; backdrop-filter: blur(6px); }
#status-next { opacity: .7; font-weight: normal; font-size: 12px; }
#status-dots { display: inline-flex; gap: 4px; }
#status-dots i { width: 8px; height: 8px; border-radius: 50%; background: #444; display: inline-block; }
#status-dots i.ok { background: #3ddc3d; }
#status-dots i.err { background: #ff3b3b; }
#status-dots i.busy { background: var(--amber); animation: blink .8s infinite alternate; }
@keyframes blink { to { opacity: .2; } }
#api-error { pointer-events: auto; max-width: min(92vw, 520px); background: rgba(60,0,0,.9); color: #ffb3b3; border: 1px solid #ff5050; border-radius: 12px; padding: 6px 10px; font-size: 12px; word-break: break-word; }
#progress { pointer-events: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 6px 10px; font-size: 12px; }

/* ---------- Panels ---------- */
.panel { position: fixed; z-index: 1000; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; backdrop-filter: blur(6px); }
#legend { left: calc(var(--sal) + 8px); bottom: calc(var(--bar-h) + var(--sab) + 8px); max-width: min(78vw, 420px); max-height: 34vh; overflow-y: auto; padding: 8px 10px; font-size: 12.5px; font-weight: bold; line-height: 1.45; }
#legend:empty { display: none; }
#legend .legend-time { opacity: .7; font-weight: normal; margin-bottom: 2px; }
.muted { opacity: .6; font-weight: normal; }
body.replaying #legend { bottom: calc(var(--bar-h) + var(--sab) + 82px); }
#debug { inset: calc(var(--sat) + 56px) 8px calc(var(--bar-h) + var(--sab) + 8px) 8px; margin: 0; overflow: auto; font-size: 11px; padding: 8px; z-index: 1500; white-space: pre-wrap; word-break: break-all; }

#replay { left: calc(var(--sal) + 8px); right: calc(var(--sar) + 8px); bottom: calc(var(--bar-h) + var(--sab) + 8px); display: flex; align-items: center; gap: 8px; padding: 8px; }
.replay-mid { flex: 1; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
#replay-slider { width: 100%; accent-color: var(--amber); }
#replay-label { font-size: 12px; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.icon-btn { background: transparent; border: 1px solid var(--line); border-radius: 10px; width: 44px; height: 44px; display: inline-flex; align-items: center; justify-content: center; flex: none; }
.icon-btn svg { width: 22px; height: 22px; }

/* ---------- Bottom bar ---------- */
#bottombar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 1200; height: calc(var(--bar-h) + var(--sab)); padding: 0 var(--sar) var(--sab) var(--sal); display: flex; background: rgba(0,0,0,.94); border-top: 1px solid var(--line); }
.nav-btn { flex: 1; background: none; border: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; font-size: 11px; opacity: .75; position: relative; }
.nav-btn svg { width: 24px; height: 24px; }
.nav-btn.active { opacity: 1; }
.nav-btn.active::after { content: ''; position: absolute; top: 0; left: 30%; right: 30%; height: 2px; background: var(--amber); border-radius: 2px; }
.nav-btn:active { background: rgba(255,191,0,.08); }
#btn-refresh.spinning svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Bottom sheet menu ---------- */
.backdrop { position: fixed; inset: 0; z-index: 1900; background: rgba(0,0,0,.5); }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 2000; max-height: 85vh; display: flex; flex-direction: column; background: #0a0a0a; border-top: 1px solid var(--line); border-radius: 18px 18px 0 0; padding-bottom: var(--sab); animation: up .18s ease-out; }
@keyframes up { from { transform: translateY(40px); opacity: .4; } }
.sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: #444; margin: 8px auto 0; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 16px; font-size: 17px; }
.sheet-body { overflow-y: auto; padding: 0 16px 16px; }
.sheet-body h3, .settings-body h3 { font-size: 13px; text-transform: uppercase; letter-spacing: 1px; opacity: .6; margin: 16px 0 8px; font-weight: 600; }
.toggles { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tgl { flex-direction: row; align-items: center; gap: 10px; background: #111; border: 1px solid #222; border-radius: 10px; padding: 10px; font-size: 14px; cursor: pointer; }
.tgl input { width: 18px; height: 18px; accent-color: var(--amber); flex: none; }
.row-btns { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.row-btns .btn { flex: 1; min-width: 140px; }
.hint { font-size: 12px; opacity: .6; margin: 8px 0 0; }

/* ---------- Settings ---------- */
.screen-overlay { position: fixed; inset: 0; z-index: 2100; background: #050505; overflow-y: auto; padding: var(--sat) var(--sar) calc(var(--sab) + 24px) var(--sal); }
.screen-overlay > .sheet-head { position: sticky; top: 0; background: #050505; z-index: 1; border-bottom: 1px solid #1a1a1a; padding: 12px 16px; }
.settings-body { max-width: 560px; margin: 0 auto; padding: 0 16px; display: flex; flex-direction: column; gap: 12px; }
.settings-body + .settings-body { margin-top: 12px; border-top: 1px solid #1a1a1a; padding-top: 4px; }
.settings-body small { opacity: .55; font-size: 12px; }
.settings-body .tgl { background: #0b0b0b; }
.status-box { background: #0b0b0b; border: 1px solid #1a1a1a; border-radius: 10px; padding: 10px; font-size: 12px; white-space: pre-wrap; margin: 0; }

#toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--bar-h) + var(--sab) + 16px); z-index: 3000; background: #1a1a1a; border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px; font-size: 14px; max-width: 90vw; text-align: center; }

#radar-beam-animation { position: absolute; inset: 0; z-index: 450; pointer-events: none; }

@media (min-width: 900px) {
  .sheet { left: auto; right: 16px; bottom: calc(var(--bar-h) + 16px); width: 420px; border-radius: 18px; border: 1px solid var(--line); }
}
