/* MeshList PWA — mobile-first dark theme. No inline styles anywhere (CSP style-src 'self'). */

:root {
  --bg: #0f172a;
  --bg-2: #111c33;
  --panel: #1e293b;
  --panel-2: #273449;
  --line: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-2: #0ea5e9;
  --mine: #0369a1;
  --theirs: #1e293b;
  --ok: #22c55e;
  --warn: #f59e0b;
  --danger: #ef4444;
  --node: #22c55e;
  --online: #38bdf8;
  --away: #64748b;
  --radius: 14px;
  --tap: 44px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --topbar-h: 52px;
  --tabbar-h: 56px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
#app { height: 100%; display: flex; flex-direction: column; }
a { color: var(--accent); }
h1, h2, h3 { margin: 0 0 .5rem; line-height: 1.2; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.05rem; margin-top: 1.25rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75rem; }
code, .code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
[hidden] { display: none !important; }

/* ---------------------------------------------------------------- shell */
.topbar {
  position: sticky; top: 0; z-index: 10;
  height: calc(var(--topbar-h) + var(--sat));
  padding: var(--sat) calc(12px + var(--sar)) 0 calc(16px + var(--sal));
  display: flex; align-items: center; justify-content: space-between;
  background: rgba(15, 23, 42, .92); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar-title { font-size: 1.1rem; margin: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-status { display: flex; align-items: center; gap: 8px; }
.conn { width: 12px; height: 12px; border-radius: 50%; background: var(--away); box-shadow: 0 0 0 3px rgba(255,255,255,.06); }
.conn-online { background: var(--online); }
.conn-node { background: var(--node); box-shadow: 0 0 0 3px rgba(34,197,94,.25); }
.conn-connecting { background: var(--warn); animation: pulse 1.2s ease-in-out infinite; }
.conn-off { background: var(--danger); }
@keyframes pulse { 50% { opacity: .35; } }

.view {
  flex: 1; min-height: 0; display: flex; flex-direction: column;
  padding-left: var(--sal); padding-right: var(--sar);
}
.page { padding: 12px 16px calc(16px + var(--tabbar-h) + var(--sab)); overflow-y: auto; -webkit-overflow-scrolling: touch; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  height: calc(var(--tabbar-h) + var(--sab));
  padding: 0 var(--sar) var(--sab) var(--sal);
  display: flex; background: rgba(17, 28, 51, .96); border-top: 1px solid var(--line);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.tab {
  flex: 1; min-height: var(--tap); display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; color: var(--muted); text-decoration: none; font-size: .78rem; position: relative;
}
.tab::before { content: ""; width: 22px; height: 22px; border-radius: 6px; background: currentColor; opacity: .25; }
.tab[data-tab="global"]::before { border-radius: 50%; }
.tab[data-tab="chats"]::before { border-radius: 11px 11px 11px 2px; }
.tab[data-tab="lists"]::before { border-radius: 4px; }
.tab[data-tab="store"]::before { border-radius: 2px 2px 8px 8px; }
.tab[data-tab="settings"]::before { border-radius: 50%; width: 18px; height: 18px; outline: 3px dotted currentColor; outline-offset: 1px; }
.tab.active { color: var(--accent); }
.tab.active::before { opacity: 1; }
.tab-badge { position: absolute; top: 4px; right: calc(50% - 22px); min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--danger); color: #fff; font-size: .7rem; line-height: 18px; text-align: center; }

/* ---------------------------------------------------------------- auth / cards */
.auth { flex: 1; display: flex; align-items: flex-start; justify-content: center; padding: calc(24px + var(--sat)) 16px calc(24px + var(--sab)); overflow-y: auto; }
.card { width: 100%; max-width: 440px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; }
.card-sub { background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
.logo { display: block; margin: 0 auto 12px; border-radius: 16px; }
.auth h1 { text-align: center; }
.auth h2 { text-transform: none; letter-spacing: 0; color: var(--text); font-size: 1.1rem; margin-top: .5rem; }
.auth-links { display: flex; flex-direction: column; gap: 4px; margin-top: 12px; align-items: center; }
.steps { padding-left: 1.2rem; }
.steps li { margin-bottom: .6rem; }
details summary { cursor: pointer; color: var(--muted); min-height: var(--tap); display: flex; align-items: center; }

/* ---------------------------------------------------------------- forms */
.form { display: flex; flex-direction: column; gap: 10px; }
.row { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; margin-bottom: 8px; }
.row .field { flex: 1 1 160px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field-label { font-size: .85rem; color: var(--muted); }
.input {
  width: 100%; min-height: var(--tap); padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit; font-size: 16px;
}
.input:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.btn {
  min-height: var(--tap); padding: 10px 16px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel-2); color: var(--text); font: inherit; font-weight: 600; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; text-decoration: none;
}
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }
.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-link { background: none; border: none; color: var(--accent); font-weight: 500; padding: 8px; }
.btn-link.pulse { animation: pulse 1s ease-in-out 3; }
.btn-small { min-height: 36px; padding: 6px 10px; font-size: .85rem; }
.notice { padding: 10px 12px; border-radius: 10px; font-size: .92rem; margin: 0 0 8px; }
.notice-error { background: rgba(239,68,68,.12); color: #fca5a5; border: 1px solid rgba(239,68,68,.4); }
.notice-info { background: rgba(56,189,248,.12); color: #bae6fd; border: 1px solid rgba(56,189,248,.4); }
.check, .switch { display: flex; align-items: center; gap: 10px; min-height: var(--tap); cursor: pointer; }
.check input, .switch input { width: 22px; height: 22px; accent-color: var(--accent-2); }
.switch input { width: 44px; height: 24px; }
.code-box { display: flex; align-items: center; gap: 10px; background: var(--bg); border: 1px dashed var(--accent); border-radius: 10px; padding: 12px; margin: 10px 0; }
.code-box .code { flex: 1; font-size: 1.1rem; letter-spacing: .08em; word-break: break-all; user-select: all; -webkit-user-select: all; }

/* ---------------------------------------------------------------- badges & chips */
.badge, .chip { display: inline-flex; align-items: center; padding: 1px 8px; border-radius: 999px; font-size: .72rem; font-weight: 600; line-height: 1.6; white-space: nowrap; }
.badge-node { background: rgba(34,197,94,.18); color: #86efac; }
.badge-online { background: rgba(56,189,248,.18); color: #7dd3fc; }
.badge-away { background: rgba(100,116,139,.25); color: #cbd5e1; }
.chip-net { border: 1px solid transparent; }
.chip-net-wifi { background: rgba(20,184,166,.16); color: #5eead4; }
.chip-net-cellular { background: rgba(245,158,11,.18); color: #fcd34d; }
.chip-net-ethernet { background: rgba(148,163,184,.18); color: #e2e8f0; }
.chip-net-remote { background: rgba(168,85,247,.18); color: #d8b4fe; }
.net-summary { font-size: .78rem; color: #94a3b8; padding: 2px 0 6px; }
.chip { background: var(--panel-2); color: var(--muted); }
.chip-direct { background: rgba(34,197,94,.18); color: #86efac; }
.chip-relay { background: rgba(245,158,11,.18); color: #fcd34d; }
.chip-server { background: rgba(148,163,184,.18); color: #cbd5e1; }
.chip-on { background: rgba(56,189,248,.18); color: #7dd3fc; }
.chip-off { background: rgba(245,158,11,.18); color: #fcd34d; }
.unread { min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: var(--danger); color: #fff; font-size: .72rem; line-height: 20px; text-align: center; }

/* ---------------------------------------------------------------- presence strip */
.presence-strip { display: flex; gap: 8px; overflow-x: auto; padding: 8px 12px; border-bottom: 1px solid var(--line); background: var(--bg-2); scrollbar-width: none; }
.presence-strip::-webkit-scrollbar { display: none; }
.pres { display: inline-flex; align-items: center; gap: 6px; padding: 6px 10px; border-radius: 10px; background: var(--panel); white-space: nowrap; font-size: .85rem; }
.pres.me { border: 1px solid var(--line); }
.pres-platform { color: var(--muted); font-size: .75rem; }

/* ---------------------------------------------------------------- conversation header */
.conv-header { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-bottom: 1px solid var(--line); background: var(--bg-2); }
.back { width: var(--tap); height: var(--tap); display: inline-flex; align-items: center; justify-content: center; font-size: 1.8rem; text-decoration: none; color: var(--accent); border-radius: 10px; }
.conv-title { flex: 1; min-width: 0; }
.peer-name { font-weight: 600; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-sub { display: flex; gap: 6px; align-items: center; margin-top: 2px; }
.banner { padding: 10px 12px; font-size: .9rem; }
.banner-warn { background: rgba(245,158,11,.14); color: #fde68a; border-bottom: 1px solid rgba(245,158,11,.4); }

/* ---------------------------------------------------------------- chat */
.chat { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.msgs-scroll { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 10px 12px; overscroll-behavior: contain; }
.msgs { display: flex; flex-direction: column; gap: 6px; }
.day-sep { align-self: center; font-size: .75rem; color: var(--muted); background: var(--panel); padding: 2px 10px; border-radius: 999px; margin: 8px 0; }
.msg { max-width: 82%; padding: 8px 12px; border-radius: 16px; background: var(--theirs); word-wrap: break-word; overflow-wrap: anywhere; }
.msg.theirs { align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.mine { align-self: flex-end; background: var(--mine); border-bottom-right-radius: 4px; }
.msg-from { font-size: .75rem; color: var(--accent); font-weight: 600; margin-bottom: 2px; }
.msg-body { white-space: pre-wrap; }
/* Global Chat's "this room is not encrypted" line (spec 2026-09-08 §2). */
.notice-plain { padding: .35rem .6rem; opacity: .75; }
/* A message this device holds but cannot decrypt yet (spec 2026-09-08 §7). */
.msg-locked { font-style: italic; opacity: .7; }
.msg.locked { border-left: 2px solid var(--warn, #b8860b); }
.msg-meta { display: flex; align-items: center; justify-content: flex-end; gap: 6px; margin-top: 4px; font-size: .7rem; color: rgba(226,232,240,.7); }
.msg-status { font-weight: 700; }
.status-delivered { color: #86efac; }
.status-held { color: #fcd34d; }
.status-failed { color: #fca5a5; }

.composer-wrap { position: sticky; bottom: 0; padding: 8px 12px calc(8px + var(--tabbar-h) + var(--sab)); background: rgba(15,23,42,.96); border-top: 1px solid var(--line); }
.composer { display: flex; gap: 8px; align-items: flex-end; }
.composer-input { flex: 1; resize: none; min-height: var(--tap); max-height: 140px; padding: 10px 12px; border-radius: 22px; border: 1px solid var(--line); background: var(--bg-2); color: var(--text); font: inherit; font-size: 16px; line-height: 1.35; }
.composer-input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.btn-send { border-radius: 22px; background: var(--accent-2); border-color: var(--accent-2); color: #fff; }
.composer-count { display: block; text-align: right; font-size: .72rem; color: var(--muted); min-height: 0; }
.composer-count.over { color: var(--danger); }

/* ---------------------------------------------------------------- lists */
.conv-list, .plain-list { list-style: none; margin: 0 0 12px; padding: 0; }
.conv-item { display: flex; align-items: center; gap: 10px; min-height: 64px; padding: 10px 12px; margin-bottom: 8px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); color: inherit; text-decoration: none; }
.conv-main { flex: 1; min-width: 0; }
.conv-head { display: flex; align-items: center; gap: 8px; }
.conv-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-preview { font-size: .88rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.conv-time { white-space: nowrap; }
.empty { padding: 20px 8px; text-align: center; }
.member-row { display: flex; align-items: center; gap: 10px; min-height: var(--tap); padding: 6px 0; border-bottom: 1px solid var(--line); }
.member-row > span:first-child { flex: 1; }
.plain-list li { padding: 4px 0; }

/* ---------------------------------------------------------------- settings */
.kv { display: flex; justify-content: space-between; gap: 12px; align-items: center; min-height: var(--tap); border-bottom: 1px solid var(--line); }
.kv .k { color: var(--muted); }
.kv .v { display: inline-flex; align-items: center; gap: 8px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .9rem; word-break: break-all; text-align: right; }
.settings .btn { margin-bottom: 8px; }
.version { margin-top: 20px; text-align: center; }

/* ---------------------------------------------------------------- toast */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--sab) + 16px); transform: translate(-50%, 20px);
  max-width: min(92vw, 480px); padding: 10px 16px; border-radius: 12px; background: #f8fafc; color: #0f172a;
  font-size: .9rem; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 50; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------------------------------------------------------------- onboarding page (port 80 / /setup) */
.setup { max-width: 640px; margin: 0 auto; padding: 24px 16px calc(24px + var(--sab)); }
.setup header { text-align: center; margin-bottom: 20px; }
.setup .qr { display: flex; justify-content: center; margin: 12px 0; }
.setup .qr svg { width: 200px; height: 200px; background: #fff; padding: 8px; border-radius: 12px; }
.setup .url { text-align: center; font-size: 1.1rem; word-break: break-all; }
.os-tabs { display: flex; gap: 6px; margin: 16px 0 10px; flex-wrap: wrap; }
.os-tabs .btn.active { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }
.os-panel { display: none; }
.os-panel.active { display: block; }
.fingerprint { font-size: .85rem; word-break: break-all; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px; }
.trust-ok { color: #86efac; }
.trust-bad { color: #fca5a5; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; margin: 12px 0; }

@media (min-width: 760px) {
  .view, .auth { max-width: 720px; width: 100%; margin: 0 auto; }
  .tabbar { max-width: 720px; margin: 0 auto; border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
  .topbar { max-width: 720px; margin: 0 auto; }
  .msg { max-width: 70%; }
}
.read-only-note { text-align: center; padding: 10px 12px; margin: 0; }

/* ---------------------------------------------------------------- media */
/* Reachable by keyboard and screen readers but not drawn: the file input is
   driven by .btn-attach, so hiding it with display:none would take it out of
   the accessibility tree along with its label. */
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.btn-attach { flex: 0 0 auto; width: var(--tap); height: var(--tap); border-radius: 50%; font-size: 1.4rem; line-height: 1; padding: 0; }
.msg-media { display: flex; flex-direction: column; gap: 6px; }
/* Capped by viewport height as well as width: a 9:16 phone video at 100% width
   would otherwise be taller than the screen and push the composer off it. */
.media-item { display: block; max-width: 100%; max-height: 60vh; width: auto; height: auto; border-radius: 10px; background: var(--bg-2); }
/* A sensible box before the blob has decrypted, so the thread does not jump
   when each photo lands. */
.media-loading, .media-failed { display: flex; align-items: center; justify-content: center; min-height: 120px; padding: 12px; border-radius: 10px; background: var(--bg-2); border: 1px dashed var(--line); text-align: center; }
.media-failed { border-style: solid; border-color: var(--danger); color: var(--danger); }
.media-caption { white-space: pre-wrap; }

@media (prefers-reduced-motion: reduce) { .conn-connecting, .btn-link.pulse { animation: none; } }
