:root {
  color-scheme: dark;

  --bg: #0e1a14;
  --bg-elev: #13211a;
  --bg-chat: #0f1914;
  --surface: #16241d;
  --surface-2: #1b2b23;

  --line: rgba(180, 210, 190, 0.12);
  --line-strong: rgba(180, 210, 190, 0.2);

  --text: #eef6f0;
  --muted: #93a89b;

  --accent: #3dba7a;
  --accent-strong: #54d08d;
  --accent-dim: #2a8f5c;
  --accent-soft: rgba(61, 186, 122, 0.12);

  --bubble-out: #1f6b45;
  --bubble-in: #182620;

  --bubble-meta: #c9e2d3;
  --bubble-meta-out: #d6f0e1;

  --danger: #e07070;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);

  --font-brand: system-ui, sans-serif;
  --font-ui: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;

  --topbar-h: 60px;

  --radius-sm: 10px;
  --radius: 16px;
  --radius-lg: 22px;
  --radius-pill: 999px;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;

  --shadow-1: 0 8px 24px rgba(0, 0, 0, 0.28);
  --shadow-2: 0 16px 32px rgba(0, 0, 0, 0.36);
  --shadow-accent: 0 16px 32px rgba(61, 186, 122, 0.32);

  --z-app-bg: 0;
  --z-screen: 1;
  --z-gate-overlay: 2;
  --z-chrome: 5;
  --z-fab: 6;
  --z-sheet-backdrop: 20;
  --z-sheet: 21;
  --z-toast: 40;
  --z-incoming: 45;
  --z-module-backdrop: 50;
  --z-module: 51;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background: #000;
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: manipulation;
}

button,
input,
textarea {
  font: inherit;
}

button {
  touch-action: manipulation;
  user-select: none;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

#app {
  height: 100vh;
  height: var(--app-height, 100dvh);
  max-width: 520px;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
  background: var(--bg);
  box-shadow: 0 0 80px rgba(0, 0, 0, 0.45);
}

.app-bg {
  position: absolute;
  inset: 0;
  z-index: var(--z-app-bg);
  pointer-events: none;
  background:
    url("./assets/icon-192.png") center center / min(48%, 192px) no-repeat,
    var(--bg);
}

.app-bg::after {
  content: "";
  position: absolute;
  inset: 0;
  background:
    radial-gradient(circle at 50% 42%, rgba(14, 26, 20, 0.35), rgba(14, 26, 20, 0.82) 58%, rgba(14, 26, 20, 0.92) 100%);
}

@media (min-width: 521px) {
  #app {
    border-left: 1px solid var(--line);
    border-right: 1px solid var(--line);
  }
}

.screen {
  position: absolute;
  inset: 0;
  z-index: var(--z-screen);
  display: none;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  background: transparent;
}

.screen.active {
  display: flex;
  animation: fadeIn 180ms ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes sheetUp {
  from {
    opacity: 0.6;
    transform: translateY(24px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* =======================
   Gate
======================= */

#screen-gate {
  justify-content: center;
  padding: calc(var(--space-6) + var(--safe-top)) var(--space-6)
    calc(var(--space-8) + var(--safe-bot));
  overflow: auto;
}

.gate-bg {
  position: absolute;
  inset: 0;
  background:
    linear-gradient(180deg, transparent 28%, var(--bg) 78%),
    radial-gradient(120% 70% at 50% -10%, #1f4a32 0%, transparent 55%),
    radial-gradient(90% 60% at 100% 100%, rgba(61, 186, 122, 0.08), transparent 50%),
    var(--bg);
}

.demo-banner {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: var(--z-gate-overlay);
  appearance: none;
  border: 0;
  width: 100%;
  margin: 0;
  padding: calc(10px + var(--safe-top)) var(--space-4) 10px;
  background: linear-gradient(180deg, rgba(61, 186, 122, 0.22), rgba(61, 186, 122, 0.1));
  border-bottom: 1px solid rgba(61, 186, 122, 0.28);
  color: var(--accent-strong);
  font-family: var(--font-brand);
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: 0.01em;
  cursor: pointer;
  text-align: center;
  transition: background 140ms ease, color 140ms ease;
}

.demo-banner:active {
  background: rgba(61, 186, 122, 0.28);
  color: #b8f0cf;
}

.gate-inner {
  position: relative;
  z-index: 1;
  width: min(100%, 420px);
  margin: 0 auto;
  padding-top: 56px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  animation: rise 420ms ease;
}

.gate-logo {
  width: min(72vw, 280px);
  height: min(72vw, 280px);
  border-radius: 28px;
  display: grid;
  place-items: center;
  margin: 0 auto var(--space-5);
  overflow: hidden;
  background: #13211a;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.08),
    0 18px 40px rgba(0, 0, 0, 0.32);
}

.gate-logo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
  display: block;
  border-radius: inherit;
}

.brand {
  margin: 0 0 var(--space-4);
  font-family: var(--font-brand);
  font-weight: 700;
  font-size: clamp(0.95rem, 3.6vw, 1.1rem);
  line-height: 1.35;
  color: var(--accent);
  letter-spacing: 0.02em;
  max-width: 24ch;
}

.gate-sub {
  margin: 0 0 var(--space-6);
  color: var(--muted);
  font-size: 1.05rem;
  line-height: 1.5;
  max-width: 30ch;
}

.gate-hint {
  margin: var(--space-4) 0 0;
  color: var(--danger);
  font-size: 0.92rem;
  line-height: 1.4;
}

.btn-primary {
  appearance: none;
  border: 0;
  width: 100%;
  min-height: 56px;
  padding: var(--space-4) var(--space-5);
  border-radius: 18px;
  background: linear-gradient(180deg, var(--accent-strong), var(--accent-dim));
  color: #04140c;
  font-family: var(--font-brand);
  font-weight: 800;
  font-size: 1.05rem;
  cursor: pointer;
  box-shadow: var(--shadow-accent);
  transition: transform 120ms ease, filter 120ms ease;
}

.btn-primary:active {
  transform: translateY(1px) scale(0.99);
}

.btn-primary:disabled {
  filter: grayscale(0.4) brightness(0.7);
  cursor: not-allowed;
}

.btn-primary.btn-open-chrome {
  margin-top: var(--space-3);
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  box-sizing: border-box;
}

.chrome-gate {
  width: 100%;
  margin-top: var(--space-2);
  text-align: center;
}

.chrome-gate-title {
  margin: 0 0 var(--space-2);
  font-family: var(--font-brand);
  font-weight: 800;
  font-size: 1.25rem;
  color: var(--text);
}

.chrome-gate-text {
  margin: 0 0 var(--space-4);
  color: var(--muted);
  font-size: 0.98rem;
  line-height: 1.45;
}

.chrome-gate-url {
  margin: 0 0 var(--space-4);
  padding: 0.75rem 0.9rem;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid rgba(196, 165, 116, 0.35);
  color: #f0d9a8;
  font-size: 1.02rem;
  word-break: break-all;
}

.chrome-gate-steps {
  margin: var(--space-4) 0 0;
  padding-left: 1.25rem;
  text-align: left;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.55;
}

.chrome-gate-note {
  margin: var(--space-3) 0 0;
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.4;
  opacity: 0.9;
}

.btn-secondary {
  appearance: none;
  border: 1px solid rgba(61, 186, 122, 0.35);
  width: 100%;
  min-height: 52px;
  margin-top: var(--space-3);
  padding: var(--space-3) var(--space-5);
  border-radius: 18px;
  background: transparent;
  color: var(--accent-strong);
  font-family: var(--font-brand);
  font-weight: 700;
  font-size: 1rem;
  cursor: pointer;
  transition: background 140ms ease, transform 120ms ease;
}

.btn-secondary:active {
  background: var(--accent-soft);
  transform: translateY(1px);
}

.btn-chip {
  appearance: none;
  border: 1px solid var(--line);
  min-height: 44px;
  padding: 0 var(--space-4);
  border-radius: 14px;
  background: #0c1611;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.btn-chip:active {
  background: var(--accent-soft);
}

.muted {
  color: var(--muted);
}

/* =======================
   Install module
======================= */

.install-scroll {
  padding-bottom: calc(var(--space-8) + var(--safe-bot));
}

.install-step {
  display: flex;
  flex-direction: column;
}

.install-demo {
  position: relative;
  top: auto;
  left: auto;
  right: auto;
  margin-bottom: var(--space-3);
}

.install-block {
  padding: var(--space-4);
  border-bottom: 1px solid var(--line);
}

.install-lead {
  margin: 0 0 var(--space-4);
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.45;
}

.install-h {
  margin: 0 0 var(--space-2);
  font-family: var(--font-brand);
  font-size: 1.05rem;
  font-weight: 800;
}

.install-block label {
  display: block;
  color: var(--muted);
  font-size: 0.88rem;
  margin: var(--space-3) 0 var(--space-2);
}

.install-block input[type="text"],
.install-block input[type="number"],
.install-block textarea {
  width: 100%;
  min-height: 48px;
  padding: 12px var(--space-4);
  border-radius: 14px;
  border: 1px solid var(--line);
  background: #0c1611;
  color: var(--text);
  font: inherit;
  font-size: 16px;
  outline: none;
  resize: vertical;
}

.install-block textarea {
  min-height: 84px;
  line-height: 1.4;
}

.install-block input:focus,
.install-block textarea:focus {
  border-color: rgba(61, 186, 122, 0.45);
}

.install-device-head {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.install-device-meta {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.install-device-meta strong {
  font-size: 1.05rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ble-device-list {
  padding: var(--space-2) 0;
}

.ble-empty {
  padding: 48px var(--space-6);
  text-align: center;
  color: var(--muted);
}

.ble-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  min-height: 72px;
  padding: var(--space-3) var(--space-4);
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  color: inherit;
  text-align: left;
  cursor: pointer;
  font: inherit;
}

.ble-row:active {
  background: rgba(255, 255, 255, 0.045);
}

.ble-row .ble-meta {
  flex: 1;
  min-width: 0;
}

.ble-row .ble-name {
  font-weight: 700;
  margin-bottom: 2px;
}

.ble-row .ble-sub {
  color: var(--muted);
  font-size: 0.85rem;
}

.ble-rssi {
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 700;
  flex-shrink: 0;
}

.install-mast-list {
  display: grid;
  gap: var(--space-2);
}

.mast-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-height: 48px;
  padding: var(--space-2) var(--space-3);
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid rgba(255, 255, 255, 0.04);
}

.mast-row .mast-id {
  font-weight: 700;
  flex: 1;
}

.mast-row .mast-db {
  color: var(--muted);
  font-size: 0.8rem;
}

.recipient-chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
  min-height: 36px;
}

.recipient-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 8px 0 14px;
  border-radius: var(--radius-pill);
  background: var(--accent-soft);
  border: 1px solid rgba(61, 186, 122, 0.22);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 600;
}

.recipient-chip.me {
  background: rgba(61, 186, 122, 0.22);
}

.recipient-chip button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1.2rem;
  line-height: 1;
}

.recipient-chip button:disabled {
  opacity: 0.35;
  cursor: default;
}

.recipient-add-row {
  display: flex;
  gap: var(--space-2);
}

.recipient-add-row input {
  flex: 1;
  margin: 0 !important;
}

.install-map {
  height: 240px;
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: #0a120e;
  margin-bottom: var(--space-3);
  z-index: 0;
}

.install-map .leaflet-container {
  width: 100%;
  height: 100%;
  background: #0a120e;
  font: inherit;
}

.install-map .leaflet-control-attribution {
  display: none !important;
}

.install-map .leaflet-bar a {
  background: var(--bg-elev) !important;
  color: var(--text) !important;
  border-bottom-color: var(--line) !important;
  width: 44px !important;
  height: 44px !important;
  line-height: 44px !important;
  font-size: 18px !important;
}

.module-event-map .leaflet-bar a {
  background: var(--bg-elev) !important;
  color: var(--text) !important;
  border-bottom-color: var(--line) !important;
  width: 44px !important;
  height: 44px !important;
  line-height: 44px !important;
  font-size: 18px !important;
}

.coord-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
}

.coord-row label {
  margin-top: 0;
}

.field-grid {
  display: grid;
  gap: var(--space-3);
}

.field-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  min-height: 48px;
  padding: var(--space-2) 0;
  border-bottom: 1px solid var(--line);
  color: var(--text);
  font-size: 0.95rem;
}

.field-toggle:last-child {
  border-bottom: 0;
}

.field-toggle input[type="checkbox"] {
  width: 24px;
  height: 24px;
  min-width: 24px;
  accent-color: var(--accent);
}

.seg-group {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

.seg-btn {
  appearance: none;
  border: 1px solid var(--line);
  min-height: 44px;
  padding: 0 var(--space-3);
  border-radius: 12px;
  background: #0c1611;
  color: var(--muted);
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}

.seg-btn.active {
  background: var(--accent-soft);
  border-color: rgba(61, 186, 122, 0.45);
  color: var(--accent-strong);
  font-weight: 700;
}

.range-row {
  display: grid;
  gap: var(--space-2);
}

.range-row input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.range-val {
  color: var(--accent-strong);
  font-weight: 700;
  font-size: 0.9rem;
}

.echo-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
}

.echo-result {
  color: var(--muted);
  font-size: 0.9rem;
}

.echo-result.ok {
  color: var(--accent-strong);
}

.echo-result.fail {
  color: var(--danger);
}

.install-actions {
  padding: var(--space-5) var(--space-4) var(--space-4);
}

.install-actions .btn-secondary {
  margin-top: var(--space-3);
}

/* =======================
   Sticky chrome
======================= */

.sticky-top {
  flex: 0 0 auto;
  position: relative;
  z-index: var(--z-chrome);
  background: color-mix(in srgb, var(--bg-elev) 88%, transparent);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border-bottom: 1px solid var(--line);
  padding-top: var(--safe-top);
}

.topbar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: var(--topbar-h);
  padding: var(--space-2);
}

.topbar-text {
  flex: 1;
  min-width: 0;
}

.brand-sm {
  margin: 0;
  font-family: var(--font-brand);
  font-weight: 800;
  font-size: 1.18rem;
  color: var(--accent);
  letter-spacing: 0.02em;
}

.topbar-sub {
  margin: 2px 0 0;
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text);
  width: 48px;
  height: 48px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 140ms ease, transform 100ms ease;
}

.icon-btn:active {
  background: rgba(255, 255, 255, 0.06);
  transform: scale(0.96);
}

.icon-btn svg {
  width: 24px;
  height: 24px;
  display: block;
}

.text-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--accent);
  font: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
  padding: 0 var(--space-2);
  min-height: 44px;
  border-radius: var(--radius-sm);
}

.link-badge {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: 44px;
  padding: 0 var(--space-3);
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.18);
  border: 1px solid rgba(255, 255, 255, 0.04);
  font-size: 0.75rem;
  color: var(--muted);
  white-space: nowrap;
  max-width: 44%;
}

.link-badge.compact {
  max-width: 34%;
  padding: 0 var(--space-2);
}

.link-badge strong,
.link-badge #mast-label,
.link-badge #mast-label-chat {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.signal {
  display: inline-flex;
  align-items: flex-end;
  gap: 2px;
  height: 14px;
  flex-shrink: 0;
}

.signal i {
  width: 3px;
  border-radius: var(--radius-pill);
  background: rgba(180, 210, 190, 0.24);
  transition: background 180ms ease;
}

.signal i:nth-child(1) {
  height: 5px;
}

.signal i:nth-child(2) {
  height: 8px;
}

.signal i:nth-child(3) {
  height: 11px;
}

.signal i:nth-child(4) {
  height: 14px;
}

.signal[data-level="1"] i:nth-child(-n + 1),
.signal[data-level="2"] i:nth-child(-n + 2),
.signal[data-level="3"] i:nth-child(-n + 3),
.signal[data-level="4"] i:nth-child(-n + 4) {
  background: var(--accent);
}

/* =======================
   Online strip / search
======================= */

.online-strip {
  padding: var(--space-2) var(--space-4) var(--space-3);
}

.online-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--muted);
  font-size: 0.72rem;
  margin-bottom: var(--space-2);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.online-list {
  display: flex;
  gap: var(--space-2);
  overflow-x: auto;
  padding-bottom: 2px;
  scrollbar-width: none;
}

.online-list::-webkit-scrollbar {
  display: none;
}

.online-chip {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: 44px;
  padding: 0 var(--space-4);
  border-radius: var(--radius-pill);
  background: var(--accent-soft);
  border: 1px solid rgba(61, 186, 122, 0.18);
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
  transition: transform 100ms ease, background 140ms ease;
}

.online-chip:active {
  transform: scale(0.97);
  background: rgba(61, 186, 122, 0.18);
}

.online-chip .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 10px rgba(61, 186, 122, 0.7);
}

.online-empty {
  color: var(--muted);
  font-size: 0.9rem;
  padding: var(--space-2) 0;
}

.online-id {
  color: var(--muted);
  font-size: 0.75rem;
  margin-left: auto;
}

.sheet .online-list {
  flex-direction: column;
  overflow: visible;
  margin-bottom: var(--space-4);
}

.sheet .online-chip {
  width: 100%;
  justify-content: flex-start;
  border-radius: 14px;
}

.sheet-hint {
  margin: -0.5rem 0 var(--space-4);
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.35;
}

.search-wrap {
  padding: 0 var(--space-3) var(--space-3);
}

#search-chats {
  width: 100%;
  min-height: 46px;
  border-radius: 16px;
  border: 1px solid var(--line);
  background: rgba(0, 0, 0, 0.18);
  color: var(--text);
  padding: 0 var(--space-4);
  outline: none;
  font-size: 16px;
  transition: border-color 140ms ease, background 140ms ease;
}

#search-chats::placeholder {
  color: var(--muted);
}

#search-chats:focus {
  border-color: rgba(61, 186, 122, 0.45);
  background: rgba(0, 0, 0, 0.24);
}

/* =======================
   Scroll area
======================= */

.scroll-main {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* =======================
   Chat list
======================= */

.chat-list {
  padding: var(--space-2) 0 calc(96px + var(--safe-bot));
}

.chat-row {
  display: flex;
  gap: var(--space-3);
  align-items: center;
  width: 100%;
  padding: var(--space-3) var(--space-4);
  min-height: 78px;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  cursor: pointer;
  font: inherit;
  position: relative;
  transition: background 120ms ease;
}

.chat-row:active {
  background: rgba(255, 255, 255, 0.045);
}

.chat-row::after {
  content: "";
  position: absolute;
  left: 80px;
  right: 0;
  bottom: 0;
  height: 1px;
  background: var(--line);
}

.chat-row:last-child::after {
  display: none;
}

.avatar {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background:
    radial-gradient(circle at 30% 25%, rgba(255, 255, 255, 0.12), transparent 28%),
    linear-gradient(145deg, #2f5a42, #1a3328);
  display: grid;
  place-items: center;
  font-family: var(--font-brand);
  font-weight: 800;
  font-size: 1rem;
  color: #dff7e9;
  flex-shrink: 0;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.05);
}

.chat-row .meta {
  flex: 1;
  min-width: 0;
}

.chat-row .title {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: var(--space-2);
  margin-bottom: var(--space-1);
}

.chat-row .title > span:first-child {
  font-weight: 700;
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-row .time {
  color: var(--muted);
  font-size: 0.75rem;
  flex-shrink: 0;
}

.chat-row .preview {
  color: var(--muted);
  font-size: 0.92rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.empty-chats {
  padding: 72px 32px;
  text-align: center;
  color: var(--muted);
}

.empty-chats strong {
  display: block;
  color: var(--text);
  font-family: var(--font-brand);
  font-size: 1.25rem;
  margin-bottom: var(--space-2);
}

.fab {
  position: absolute;
  right: calc(var(--space-4) + var(--safe-bot) / 2);
  bottom: calc(var(--space-4) + var(--safe-bot));
  width: 60px;
  height: 60px;
  border-radius: 20px;
  border: 0;
  background: linear-gradient(180deg, var(--accent-strong), var(--accent-dim));
  color: #052013;
  cursor: pointer;
  box-shadow: var(--shadow-accent);
  z-index: var(--z-fab);
  display: grid;
  place-items: center;
  transition: transform 120ms ease;
}

.fab:active {
  transform: scale(0.94);
}

.fab svg {
  width: 28px;
  height: 28px;
  display: block;
}

/* =======================
   Chat screen
======================= */

#screen-chat {
  background: transparent;
}

.chat-top .peer-block {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
  flex: 1;
}

.chat-top .avatar {
  width: 44px;
  height: 44px;
  font-size: 0.9rem;
}

.peer-meta {
  min-width: 0;
}

.peer-name {
  margin: 0;
  font-weight: 800;
  font-size: 1.02rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.peer-status {
  margin: 2px 0 0;
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
}

.peer-status.online {
  color: var(--accent);
}

/* =======================
   Messages
======================= */

.messages {
  padding: var(--space-3) var(--space-3) calc(var(--space-3) + var(--safe-bot));
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.day-sep {
  position: sticky;
  top: var(--space-2);
  z-index: 1;
  align-self: center;
  margin: var(--space-2) 0;
  padding: 5px var(--space-3);
  border-radius: var(--radius-pill);
  background: rgba(0, 0, 0, 0.42);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  color: #dbeee2;
  font-size: 0.75rem;
  font-weight: 700;
}

.bubble {
  position: relative;
  max-width: min(84%, 560px);
  padding: var(--space-2) var(--space-3) 6px;
  border-radius: 18px;
  line-height: 1.4;
  font-size: 1rem;
  word-break: break-word;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.12);
  animation: rise 160ms ease;
}

.bubble.in {
  align-self: flex-start;
  background: var(--bubble-in);
  border-bottom-left-radius: 6px;
}

.bubble.out {
  align-self: flex-end;
  background: linear-gradient(180deg, #237a4e, var(--bubble-out));
  border-bottom-right-radius: 6px;
}

.bubble.deleted .body {
  font-style: italic;
  opacity: 0.72;
}

.bubble .body {
  white-space: pre-wrap;
}

.bubble .foot {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 6px;
  margin-top: var(--space-1);
  font-size: 0.72rem;
  color: var(--bubble-meta);
}

.bubble.out .foot {
  color: var(--bubble-meta-out);
}

.ticks.sent {
  color: #c9f0da;
}

.ticks.delivered {
  color: #8fffc0;
  letter-spacing: -0.12em;
}

.ticks.failed {
  color: #ffd9d9;
}

.sys {
  align-self: center;
  max-width: 86%;
  color: var(--muted);
  font-size: 0.82rem;
  padding: 7px var(--space-3);
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.18);
  text-align: center;
}

/* =======================
   Composer
======================= */

.composer {
  flex: 0 0 auto;
  z-index: 5;
  display: flex;
  align-items: flex-end;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3) calc(var(--space-2) + var(--safe-bot));
  background: color-mix(in srgb, var(--bg-elev) 90%, transparent);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border-top: 1px solid var(--line);
}

#input {
  flex: 1;
  resize: none;
  max-height: 140px;
  min-height: 48px;
  border: 0;
  border-radius: 24px;
  padding: 13px var(--space-4);
  background: #0c1611;
  color: var(--text);
  font-size: 16px;
  line-height: 1.35;
  outline: none;
  box-shadow: inset 0 0 0 1px var(--line);
  transition: box-shadow 140ms ease;
}

#input::placeholder {
  color: var(--muted);
}

#input:focus {
  box-shadow: inset 0 0 0 1px rgba(61, 186, 122, 0.35);
}

.send-btn {
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 16px;
  background: var(--accent);
  color: #052013;
  cursor: pointer;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  transition: transform 120ms ease, background 140ms ease;
}

.send-btn:disabled {
  background: #22322a;
  color: #9db0a5;
  cursor: not-allowed;
}

.send-btn:active:not(:disabled) {
  transform: scale(0.92);
}

.send-btn svg {
  width: 22px;
  height: 22px;
  display: block;
}

/* =======================
   Bottom sheet
======================= */

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  z-index: var(--z-sheet-backdrop);
  animation: fadeIn 180ms ease;
}

.sheet {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: var(--z-sheet);
  max-height: min(82%, 600px);
  display: flex;
  flex-direction: column;
  background: var(--bg-elev);
  border-radius: 24px 24px 0 0;
  padding-top: var(--space-2);
  border-top: 1px solid rgba(255, 255, 255, 0.06);
  box-shadow: 0 -18px 60px rgba(0, 0, 0, 0.45);
  animation: sheetUp 220ms cubic-bezier(0.2, 0.8, 0.2, 1);
}

.sheet-handle {
  width: 44px;
  height: 4px;
  border-radius: var(--radius-pill);
  background: rgba(255, 255, 255, 0.16);
  margin: 6px auto var(--space-3);
  flex-shrink: 0;
}

#sheet-body {
  overflow-y: auto;
  padding: 0 var(--space-4) calc(var(--space-6) + var(--safe-bot));
  min-height: 0;
}

.sheet h3 {
  margin: 0 0 var(--space-4);
  font-family: var(--font-brand);
  font-size: 1.15rem;
}

.sheet label {
  display: block;
  color: var(--muted);
  font-size: 0.88rem;
  margin-bottom: var(--space-2);
}

.sheet input[type="number"],
.sheet input[type="text"],
.sheet input[type="search"] {
  width: 100%;
  min-height: 52px;
  padding: 14px var(--space-4);
  border-radius: 16px;
  border: 1px solid var(--line);
  background: #0c1611;
  color: var(--text);
  font-size: 16px;
  margin-bottom: var(--space-4);
  outline: none;
}

.sheet input[type="number"]:focus,
.sheet input[type="text"]:focus,
.sheet input[type="search"]:focus {
  border-color: rgba(61, 186, 122, 0.45);
}

.sheet .linkish {
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  min-height: 56px;
  padding: var(--space-4) var(--space-1);
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  cursor: pointer;
  text-align: left;
}

.sheet .linkish:last-child {
  border-bottom: 0;
}

.sheet .linkish.danger {
  color: var(--danger);
}

.log-toolbar {
  display: flex;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}

.log-toolbar button {
  flex: 1;
  min-height: 46px;
  padding: var(--space-2) var(--space-3);
  border-radius: 14px;
  border: 1px solid var(--line);
  background: #0c1611;
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.log-box {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.72rem;
  line-height: 1.4;
  background: #0a120e;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: var(--space-3);
  max-height: 42vh;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  color: #9cb8a8;
}

/* =======================
   Toast / incoming banner
======================= */

.toast {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(96px + var(--safe-bot));
  z-index: var(--z-toast);
  max-width: calc(100% - 32px);
  padding: var(--space-3) var(--space-4);
  border-radius: 16px;
  background: rgba(8, 12, 10, 0.92);
  border: 1px solid var(--line);
  color: var(--text);
  font-size: 0.92rem;
  text-align: center;
  box-shadow: var(--shadow-1);
  animation: rise 200ms ease;
  pointer-events: none;
}

.incoming-banner {
  position: absolute;
  top: calc(10px + var(--safe-top));
  left: 12px;
  right: 12px;
  z-index: var(--z-incoming);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 12px 14px;
  border: 1px solid var(--line-strong);
  border-radius: 18px;
  background: rgba(14, 26, 20, 0.94);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  color: var(--text);
  text-align: left;
  cursor: pointer;
  box-shadow: var(--shadow-2);
  animation: bannerDrop 240ms ease;
}

.incoming-banner:active {
  transform: scale(0.99);
}

.incoming-banner-avatar {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: linear-gradient(145deg, #2f5a42, #163125);
  color: var(--accent-strong);
  font-family: var(--font-brand);
  font-weight: 800;
  font-size: 0.82rem;
}

.incoming-banner-body {
  min-width: 0;
  display: grid;
  gap: 2px;
}

.incoming-banner-title {
  font-weight: 700;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.incoming-banner-text {
  color: var(--muted);
  font-size: 0.86rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

@keyframes bannerDrop {
  from {
    opacity: 0;
    transform: translateY(-12px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* =======================
   Module event overlay
======================= */

.module-event-backdrop {
  position: absolute;
  inset: 0;
  z-index: var(--z-module-backdrop);
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  animation: fadeIn 160ms ease;
}

.module-event {
  position: absolute;
  inset: 0;
  z-index: var(--z-module);
  display: flex;
  flex-direction: column;
  background: var(--bg);
  animation: sheetUp 220ms cubic-bezier(0.2, 0.8, 0.2, 1);
}

.module-event-top {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: var(--topbar-h);
  padding: var(--safe-top) var(--space-2) var(--space-2);
  background: color-mix(in srgb, var(--bg-elev) 90%, transparent);
  border-bottom: 1px solid var(--line);
}

.module-event-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: var(--space-4);
  padding-bottom: calc(var(--space-8) + var(--safe-bot));
}

.module-event-head {
  display: flex;
  gap: var(--space-3);
  align-items: flex-start;
  margin-bottom: var(--space-4);
}

.module-event-meta {
  min-width: 0;
  display: grid;
  gap: 2px;
}

.module-event-meta strong {
  font-size: 1.12rem;
}

.module-event-desc {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.35;
}

.module-event-card {
  margin-bottom: var(--space-4);
  padding: var(--space-4);
  border-radius: 18px;
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid var(--line);
}

.module-event-label {
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: var(--space-2);
  font-weight: 700;
}

.module-event-what {
  font-family: var(--font-brand);
  font-weight: 800;
  font-size: 1.2rem;
  color: var(--accent-strong);
  line-height: 1.3;
}

.module-event-summary {
  margin-top: var(--space-2);
  color: var(--text);
  font-size: 1rem;
  line-height: 1.4;
}

.module-event-map {
  height: 200px;
  border-radius: 14px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: #0a120e;
}

.module-event-map .leaflet-container {
  width: 100%;
  height: 100%;
  background: #0a120e;
  font: inherit;
}

.module-event-map .leaflet-bar a {
  background: var(--bg-elev) !important;
  color: var(--text) !important;
  border-bottom-color: var(--line) !important;
}

.module-event-log {
  display: grid;
  gap: var(--space-2);
  max-height: 240px;
  overflow-y: auto;
}

.module-log-empty {
  color: var(--muted);
  font-size: 0.9rem;
  padding: var(--space-2) 0;
}

.module-log-row {
  padding: var(--space-3);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid transparent;
}

.module-log-row.current {
  border-color: rgba(61, 186, 122, 0.35);
  background: var(--accent-soft);
}

.module-log-time {
  color: var(--muted);
  font-size: 0.72rem;
  margin-bottom: 2px;
}

.module-log-title {
  font-weight: 700;
  font-size: 0.92rem;
}

.module-log-sum {
  color: var(--muted);
  font-size: 0.85rem;
  margin-top: 2px;
}

.bubble.module-ev {
  border: 1px solid rgba(61, 186, 122, 0.28);
  background: linear-gradient(180deg, rgba(61, 186, 122, 0.16), var(--bubble-in));
  cursor: pointer;
}

.bubble.module-ev .module-ev-tag {
  display: inline-block;
  margin-bottom: 4px;
  color: var(--accent-strong);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

/* =======================
   Accessibility / device
======================= */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }

  *:active {
    transform: none !important;
  }
}

@media (min-width: 420px) {
  .bubble {
    max-width: min(76%, 560px);
  }
}