:root {
  --bg: #0f1b2d;
  --surface: #16263d;
  --surface-2: #1f3350;
  --line: #2a3f5f;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --orange: #f97316;
  --amber: #f59e0b;
  --green: #22c55e;
  --red: #ef4444;
  --blue: #3b82f6;
  --purple: #a855f7;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
  font-family: system-ui, -apple-system, "SF Pro Text", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { overscroll-behavior-y: none; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
h1 { margin: 0; font-size: 1.6rem; letter-spacing: -0.02em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
svg { width: 24px; height: 24px; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; fill: currentColor; }
svg path[fill=none], svg circle[fill=none] { fill: none; }

.screen { min-height: 100dvh; display: flex; flex-direction: column; padding-bottom: var(--safe-b); }

/* ---------- botones ---------- */
.btn { font: inherit; font-weight: 600; border: 1px solid transparent; border-radius: 14px; padding: .8rem 1.1rem; cursor: pointer; touch-action: manipulation; }
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--orange); color: #fff; }
.btn-block { width: 100%; padding: 1rem; font-size: 1.05rem; }
.btn-danger-outline { background: none; color: var(--red); border-color: color-mix(in srgb, var(--red) 50%, transparent); }
.icon-btn { flex: none; width: 44px; height: 44px; display: grid; place-items: center; border-radius: 50%; border: 0; background: none; color: var(--text); cursor: pointer; font-size: 1.2rem; touch-action: manipulation; }
.icon-btn:active { background: var(--surface-2); }
:is(.btn, .icon-btn, .chip-btn, .ptt, .row, .sos, .tabs button):focus-visible, input:focus-visible { outline: 3px solid color-mix(in srgb, var(--orange) 60%, transparent); outline-offset: 2px; }
.glass { background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); }

/* ---------- formularios ---------- */
.field { display: grid; gap: .35rem; margin-bottom: 1rem; }
label { font-weight: 600; font-size: .92rem; }
input:not([type=checkbox], [type=radio]) {
  font: inherit; font-size: 16px; /* evita el zoom automático de iOS */
  color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: .85rem .9rem; width: 100%;
}
input:user-invalid { border-color: var(--red); }
.error-msg { display: none; color: #fca5a5; font-size: .85rem; margin: 0; }
input:user-invalid + .error-msg { display: block; }
.form-error { color: #fca5a5; font-weight: 600; min-height: 1.3em; margin: .25rem 0 .75rem; }

/* ---------- login ---------- */
.login { justify-content: center; align-items: center; padding: calc(var(--safe-t) + 1.5rem) 1.5rem 1.5rem; background: radial-gradient(900px 500px at 50% -10%, #1f3350, var(--bg)); }
.login-card { width: min(380px, 100%); text-align: center; }
.login-card form { text-align: left; margin-top: 2rem; }
.login-title { margin: 0; }
.login-logo { display: block; width: min(320px, 86%); height: auto; margin: 0 auto .5rem; filter: drop-shadow(0 10px 30px rgb(37 99 235 / .35)); }

/* ---------- barra superior ---------- */
.topbar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: .4rem; padding: calc(var(--safe-t) + .5rem) .6rem .5rem; background: var(--bg); border-bottom: 1px solid var(--line); }
.topbar-title { flex: 1; display: grid; padding-left: .5rem; }
.topbar-title strong { font-size: 1.15rem; }
.topbar-logo { display: block; height: 52px; width: auto; }
/* Inicio: cabecera blanca con el logo; la zona de la barra de estado sigue oscura para que se lea la hora en iPhone */
#screen-home .topbar { background: linear-gradient(var(--bg) var(--safe-t), #fff var(--safe-t)); border-bottom-color: #e2e8f0; color: #0b1b3a; }
#screen-home .topbar .conn { color: #475569; }
#screen-home .topbar .icon-btn { color: #0b1b3a; }
.conn { display: inline-flex; align-items: center; gap: .35rem; font-size: .78rem; color: var(--muted); }
.conn i { width: 8px; height: 8px; border-radius: 50%; background: var(--amber); }
.conn[data-state=online] i { background: var(--green); }
.conn[data-state=off] i { background: var(--red); }
.presence-dot { width: 18px; height: 18px; border-radius: 50%; background: var(--green); border: 3px solid var(--surface-2); }
.presence-dot[data-p=busy] { background: var(--amber); }
.presence-dot[data-p=solo] { background: var(--purple); }
#home-map { color: var(--orange); }

.banner { margin: .6rem .8rem 0; padding: .8rem 1rem; border-radius: 12px; font-size: .9rem; }
.banner-sos { background: var(--red); color: #fff; font-weight: 700; cursor: pointer; }
.banner-warn { background: #422006; color: #fed7aa; }

/* ---------- pestañas y listas ---------- */
.tabs { display: grid; grid-template-columns: 1fr 1fr; margin: .6rem .8rem 0; background: var(--surface); border-radius: 12px; padding: 4px; }
.tabs button { font: inherit; font-weight: 600; border: 0; background: none; color: var(--muted); padding: .6rem; border-radius: 9px; cursor: pointer; }
.tabs button[aria-selected=true] { background: var(--surface-2); color: var(--text); }
.list { list-style: none; margin: 0; padding: .4rem 0 1rem; }
.row { width: 100%; display: flex; align-items: center; gap: .85rem; padding: .75rem 1rem; background: none; border: 0; color: inherit; font: inherit; text-align: left; cursor: pointer; touch-action: manipulation; }
.row:active { background: var(--surface); }
.row-main { flex: 1; min-width: 0; }
.row-title, .row-sub { display: block; }
.row-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { font-size: .85rem; color: var(--muted); }
.row.offline { opacity: .5; }
.badge { min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px; background: var(--red); color: #fff; font-size: .75rem; font-weight: 700; display: grid; place-items: center; }
.empty { text-align: center; color: var(--muted); padding: 3rem 2rem; }
.empty img { width: 72px; opacity: .8; }

.avatar { position: relative; flex: none; width: 46px; height: 46px; border-radius: 50%; background: var(--surface-2); display: grid; place-items: center; }
.avatar img { width: 86%; height: 86%; }
.avatar.channel { color: var(--orange); font-size: 1.3rem; }
.avatar[data-p]::after { content: ""; position: absolute; right: -1px; bottom: -1px; width: 13px; height: 13px; border-radius: 50%; border: 2.5px solid var(--bg); background: var(--green); }
.avatar[data-p=busy]::after { background: var(--amber); }
.avatar[data-p=solo]::after { background: var(--purple); }
.avatar[data-p=offline]::after { background: #64748b; }
.avatar.sos { box-shadow: 0 0 0 3px var(--red); }

/* ---------- hablar (tema claro, como la referencia) ---------- */
.talk, .chat {
  --t-bg: #ffffff;
  --t-head: #eef3f6;
  --t-ink: #1c2530;
  --t-muted: #8a96a3;
  --t-blue: #1f6feb;
  --t-blue-soft: #e8f1fd;
  --t-orange: #f0883e;
  --t-red: #e5484d;
  --t-red-soft: #fdecec;
  --t-green: #1fa254;
  --t-green-soft: #e6f6ec;
  color-scheme: light;
  background: var(--t-bg);
  color: var(--t-ink);
  height: 100dvh;
}
.t-head { background: var(--t-head); padding: calc(var(--safe-t) + .35rem) .9rem 1rem; }
.t-row1 { display: flex; justify-content: space-between; align-items: center; }
.t-row2 { display: flex; align-items: center; gap: .85rem; margin-top: .55rem; }
.t-ico { position: relative; width: 44px; height: 44px; display: grid; place-items: center; border: 0; background: none; color: var(--t-blue); cursor: pointer; border-radius: 50%; touch-action: manipulation; flex: none; }
.t-ico svg { width: 26px; height: 26px; stroke-width: 2.4; }
.t-ico:active { background: rgb(31 111 235 / .1); }
.t-badge { position: absolute; top: 4px; right: 2px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--t-red); color: #fff; font-size: .7rem; font-weight: 700; display: grid; place-items: center; }
.t-avatar { position: relative; flex: none; width: 66px; height: 66px; border-radius: 50%; background: #6f7d8b; display: grid; place-items: center; color: #fff; }
.t-avatar svg { width: 38px; height: 38px; }
.t-avatar img { width: 82%; height: 82%; }
.t-avatar.user { background: #dfe7ee; }
.t-avatar::after { content: ""; position: absolute; right: 1px; bottom: 3px; width: 15px; height: 15px; border-radius: 50%; background: var(--t-green); border: 3px solid var(--t-head); }
.t-avatar[data-p=busy]::after { background: #f59e0b; }
.t-avatar[data-p=solo]::after { background: #a855f7; }
.t-avatar[data-p=offline]::after { background: #9aa5b1; }
.t-avatar.sos { box-shadow: 0 0 0 3px var(--t-red); }
.t-names { flex: 1; min-width: 0; display: grid; }
.t-names strong { font-size: 1.12rem; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.t-names small { color: var(--t-muted); font-size: .9rem; }
.t-switch { position: relative; width: 51px; height: 31px; flex: none; cursor: pointer; }
.t-switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.t-switch span { position: absolute; inset: 0; border-radius: 16px; background: #c9d2db; transition: background-color .2s; pointer-events: none; }
.t-switch span::after { content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%; background: #fff; box-shadow: 0 2px 4px rgb(0 0 0 / .2); transition: translate .2s; }
.t-switch input:checked + span { background: #34c759; }
.t-switch input:checked + span::after { translate: 20px 0; }
.t-switch input:focus-visible + span { outline: 3px solid rgb(31 111 235 / .45); outline-offset: 2px; }
.t-more { width: 30px; height: 30px; border-radius: 50%; border: 0; background: #b9c3cd; color: #fff; display: grid; place-items: center; cursor: pointer; flex: none; }
.t-more svg { width: 20px; height: 20px; }

.t-tools { display: flex; align-items: center; justify-content: space-between; padding: 1rem 1.1rem .2rem; }
.t-tool { width: 50px; height: 50px; border-radius: 50%; border: 0; background: var(--t-blue-soft); color: var(--t-blue); display: grid; place-items: center; cursor: pointer; flex: none; touch-action: manipulation; }
.t-tool svg { width: 25px; height: 25px; }
.t-tool.sm { width: 42px; height: 42px; }
.t-tool.sm svg { width: 21px; height: 21px; }
.t-tool:active { filter: brightness(.95); }
.t-emerg { display: inline-flex; align-items: center; gap: .4rem; border: 0; border-radius: 999px; background: var(--t-red-soft); color: var(--t-red); font: inherit; font-weight: 700; font-size: 1.02rem; padding: .45rem .95rem; cursor: pointer; touch-action: manipulation; }
.t-emerg svg { width: 19px; height: 19px; }
.t-emerg.active { background: var(--t-red); color: #fff; animation: sos-blink 1s infinite; }
.t-emerg.active svg path:first-child { fill: #fff; }
.t-emerg.active svg path:last-child { stroke: var(--t-red); }
@keyframes sos-blink { 50% { opacity: .75; } }

.t-ptt-zone { flex: 1; display: grid; justify-items: center; align-items: start; padding: clamp(.75rem, 5dvh, 3rem) 0 .5rem; }
.t-halo { width: min(86vw, 340px); aspect-ratio: 1; border-radius: 50%; display: grid; place-items: center;
  background: #fff; box-shadow: 0 10px 34px rgb(240 136 62 / .10), 0 2px 10px rgb(16 24 40 / .05), inset 0 0 0 1px rgb(240 136 62 / .06); }
.ptt {
  --ring: var(--t-orange); --ic: var(--t-blue); --fill: #fff;
  width: 78%; aspect-ratio: 1; border-radius: 50%; cursor: pointer; position: relative;
  border: 3px solid var(--ring); background: var(--fill); color: var(--ic);
  display: grid; place-items: center; align-content: center;
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  transition: border-color .15s, background-color .15s, transform .12s, border-width .15s;
}
.ptt svg { width: 34%; height: auto; stroke-width: 2.1; }
.ptt .ic-spk { display: none; }
.ptt-label { position: absolute; bottom: 20%; left: 0; right: 0; text-align: center; font-size: .95rem; font-weight: 700; color: var(--ic); padding: 0 1rem; }
.ptt[data-state=requesting] { --ring: #f59e0b; }
.ptt[data-state=talking] { --ring: var(--t-red); --ic: var(--t-red); --fill: #fff4f4; border-width: 6px; transform: scale(1.03); }
.ptt[data-state=receiving] { --ring: var(--t-green); --ic: var(--t-green); --fill: #f2fbf5; border-width: 6px; }
.ptt[data-state=receiving] .ic-mic { display: none; }
.ptt[data-state=receiving] .ic-spk { display: block; }
.ptt[data-state=busy] { --ring: #9aa5b1; --ic: #9aa5b1; }
.ptt:focus-visible { outline: 3px solid rgb(31 111 235 / .45); outline-offset: 6px; }
.t-halo:has(.ptt[data-state=talking]), .t-halo:has(.ptt[data-state=receiving]) { animation: halo 1.1s ease-in-out infinite; }
@keyframes halo { 50% { transform: scale(1.03); } }
@media (prefers-reduced-motion: reduce) { .t-halo, .ptt, .t-emerg { animation: none !important; transition: none; } }

.t-foot { display: flex; align-items: center; justify-content: space-between; padding: .4rem .6rem calc(.6rem + var(--safe-b)); }
.t-foot-l, .t-foot-r { display: flex; align-items: center; }
.t-status { display: inline-flex; align-items: center; gap: .4rem; border: 0; border-radius: 999px; background: var(--t-green-soft); color: var(--t-green); font: inherit; font-weight: 700; padding: .45rem .9rem; cursor: pointer; }
.t-status svg { width: 19px; height: 19px; }
.t-status[data-p=busy] { background: #fef3e2; color: #c77700; }
.t-status[data-p=solo] { background: #f4ecfd; color: #8a3fd1; }
.t-status[data-p=offline] { background: #eef1f4; color: var(--t-muted); }
#talk-bt[aria-pressed=false] { color: #b9c3cd; }
.t-signal { cursor: default; }
.t-signal rect { transition: opacity .2s; }
.t-signal[data-level="2"] rect:nth-child(n+3), .t-signal[data-level="0"] rect { opacity: .22; }
#talk-reconnect.spin svg { animation: spin 1s linear infinite; }
@keyframes spin { to { rotate: 360deg; } }

/* ---------- mensajes ---------- */
.t-head-chat { display: flex; align-items: center; gap: .5rem; padding-bottom: .6rem; }
.t-speed { border: 1px solid #cdd6df; background: #fff; color: var(--t-blue); font: inherit; font-weight: 700; border-radius: 999px; padding: .3rem .75rem; cursor: pointer; }
.log { flex: 1; overflow-y: auto; padding: .9rem; display: flex; flex-direction: column; gap: .5rem; overscroll-behavior: contain; background: #f6f8fa; }
.log .empty { color: var(--t-muted); }
.msg { max-width: 82%; background: #fff; border-radius: 16px; padding: .55rem .8rem; align-self: flex-start; box-shadow: 0 1px 2px rgb(0 0 0 / .06); }
.msg.mine { align-self: flex-end; background: #dcebff; }
.msg.sos { background: var(--t-red); color: #fff; }
.msg-from { font-size: .75rem; color: var(--t-blue); font-weight: 700; }
.msg-time { font-size: .7rem; color: var(--t-muted); text-align: right; }
.msg.sos .msg-time, .msg.sos .msg-from { color: #fff; }
.msg-photo { display: block; max-width: 100%; max-height: 260px; border-radius: 10px; margin-top: .2rem; cursor: zoom-in; }
.voice { display: flex; align-items: center; gap: .6rem; background: none; border: 0; color: inherit; font: inherit; cursor: pointer; padding: 0; }
.voice b { width: 34px; height: 34px; border-radius: 50%; background: var(--t-green); color: #fff; display: grid; place-items: center; font-size: .9rem; }
.link { color: var(--t-blue); background: none; border: 0; font: inherit; padding: 0; cursor: pointer; text-decoration: underline; }
.msg.sos .link { color: #fff; }
.composer { display: flex; align-items: center; gap: .45rem; padding: .55rem .7rem calc(.55rem + var(--safe-b)); border-top: 1px solid #e3e8ed; background: #fff; }
.composer input { background: #f1f4f7 !important; border-color: #e3e8ed !important; color: var(--t-ink) !important; border-radius: 22px !important; padding: .65rem 1rem !important; }
.t-send { width: 44px; height: 44px; flex: none; border-radius: 50%; border: 0; background: var(--t-blue); color: #fff; display: grid; place-items: center; cursor: pointer; }
.t-send svg { width: 22px; height: 22px; }

/* hojas en tema claro */
.sheet-light { background: #fff; color: #1c2530; }
.sheet-light h1 { margin-bottom: .8rem; font-size: 1.3rem; }
.menu-item { display: block; width: 100%; text-align: left; font: inherit; font-size: 1.02rem; background: #f3f6f9; color: #1c2530; border: 0; border-radius: 12px; padding: .9rem 1rem; margin-bottom: .5rem; cursor: pointer; }
.muted-d { color: #6b7785; }
.btn-ghost { background: #eef2f6; color: #1c2530; }
.btn-sos { background: #e5484d; color: #fff; }
.photo-view { padding: 0; background: #000; width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border: 0; }
.photo-view form, .photo-close { width: 100%; height: 100%; margin: 0; padding: 0; border: 0; background: none; cursor: zoom-out; }
.photo-view img { width: 100%; height: 100%; object-fit: contain; }
.msg .photo-close { width: auto; height: auto; display: block; cursor: zoom-in; }
.photo-view::backdrop { background: #000; }

/* ---------- mapa ---------- */
.mapscreen { position: fixed; inset: 0; padding: 0; }
.map { position: absolute; inset: 0; background: #dde3ea; }
.map-top { position: absolute; z-index: 500; top: calc(var(--safe-t) + .6rem); left: .6rem; right: .6rem; display: flex; gap: .5rem; align-items: center; }
.map-title { border-radius: 22px; padding: .45rem 1rem; display: grid; }
.map-title small { font-size: .78rem; }
.map-fabs { position: absolute; z-index: 500; right: .7rem; top: 40%; display: grid; gap: .6rem; }
.map-fabs .icon-btn { width: 48px; height: 48px; color: var(--blue); }
.map-bottom { position: absolute; z-index: 500; left: 0; right: 0; bottom: 0; padding: 0 .7rem calc(.7rem + var(--safe-b)); display: grid; gap: .5rem; }
.chips-row { display: flex; gap: .5rem; overflow-x: auto; padding: .2rem 0; scrollbar-width: none; }
.chips-row button { flex: none; display: flex; align-items: center; gap: .5rem; font: inherit; font-size: .88rem; color: var(--text); border: 0; border-radius: 999px; padding: .3rem .9rem .3rem .3rem; cursor: pointer; }
.chips-row .avatar { width: 30px; height: 30px; }
.chips-row button.sos-chip { background: var(--red); }
.sel-card { border-radius: 18px; padding: 1rem; display: grid; gap: .8rem; }
.sel-head { display: flex; align-items: center; gap: .75rem; }
.sel-head > div { flex: 1; }
.sel-actions { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; }
.sel-actions a { text-align: center; text-decoration: none; color: var(--orange); border: 1px solid var(--line); }
.sos { position: relative; overflow: hidden; height: 56px; border-radius: 28px; border: 0; font: inherit; font-weight: 800; color: #fff; background: color-mix(in srgb, var(--red) 85%, transparent); cursor: pointer; touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.sos span { position: relative; }
.sos-fill { position: absolute !important; inset: 0; background: #b91c1c; transform-origin: left; transform: scaleX(0); }
.sos.holding .sos-fill { transform: scaleX(1); transition: transform 1.2s linear; }
.sos.active { background: var(--red); animation: ring 1s infinite; --c: var(--red); }
@keyframes ring { 50% { box-shadow: 0 0 0 10px color-mix(in srgb, var(--c) 25%, transparent); } }
.guard-pin { position: relative; width: 46px; height: 56px; }
.guard-pin .ring { position: absolute; inset: 0 0 10px 0; border-radius: 50%; background: #fff; border: 4px solid var(--c); box-shadow: 0 3px 8px rgb(0 0 0 / .4); display: grid; place-items: center; }
.guard-pin .ring img { width: 34px; height: 34px; }
.guard-pin .tip { position: absolute; left: 50%; bottom: 0; border: 8px solid transparent; border-top: 12px solid var(--c); border-bottom: 0; transform: translateX(-50%); }
.guard-pin.me { width: 54px; height: 64px; }
.guard-pin.me .ring img { width: 40px; height: 40px; }
.guard-pin.sos .ring { animation: pulse 1s infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 12px rgb(239 68 68 / .3); } }
.leaflet-container { font-family: inherit; }

/* ---------- ajustes ---------- */
.sheet { border: 0; border-radius: 22px 22px 0 0; margin: auto 0 0; width: 100%; max-width: 100%; padding: 1.4rem 1.2rem calc(1.2rem + var(--safe-b)); background: var(--surface); color: var(--text); }
.sheet:modal { overscroll-behavior: contain; }
.sheet::backdrop { background: rgb(0 0 0 / .55); overflow: hidden; overscroll-behavior: contain; }
@media (min-width: 600px) { .sheet { margin: auto; max-width: 420px; border-radius: 22px; } }
.presence { border: 0; padding: 0; margin: 1rem 0; display: grid; gap: .3rem; }
.presence legend { font-weight: 700; margin-bottom: .4rem; padding: 0; }
.presence label { display: flex; align-items: center; gap: .6rem; font-weight: 500; padding: .55rem .6rem; border-radius: 10px; }
.presence label:has(input:checked) { background: var(--surface-2); }
.dot { width: 12px; height: 12px; border-radius: 50%; display: inline-block; }
.dot-available { background: var(--green); } .dot-busy { background: var(--amber); } .dot-solo { background: var(--purple); }
.switch-row { display: flex; align-items: center; gap: .7rem; font-weight: 500; padding: .55rem 0; }
input[type=checkbox], input[type=radio] { accent-color: var(--orange); width: 1.2rem; height: 1.2rem; }
.sheet-actions { display: flex; justify-content: space-between; gap: .6rem; margin-top: 1rem; }

/* ---------- toast ---------- */
.toast { position: fixed; z-index: 1000; left: 50%; bottom: calc(5.5rem + var(--safe-b)); translate: -50% 1rem; max-width: calc(100vw - 2rem); background: #f8fafc; color: #0f172a; font-weight: 600; padding: .75rem 1.1rem; border-radius: 14px; opacity: 0; pointer-events: none; transition: opacity .2s, translate .2s; box-shadow: 0 10px 30px rgb(0 0 0 / .35); }
.toast.show { opacity: 1; translate: -50% 0; }
