/* Dark is the default. `data-theme` on <html> overrides it, and the "auto"
   setting removes the attribute so the system preference decides. */
:root,
:root[data-theme='dark'] {
  --bg: #0b0f14;
  --panel: #121820;
  --panel-2: #172029;
  --line: #22303c;
  --text: #e6edf3;
  --muted: #8ba0b3;
  --accent: #4db6ac;
  --accent-ink: #06231f;
  --danger: #e5675f;
  --ok: #7bc96f;
  --warn: #e3b341;
  --shadow: rgba(0, 0, 0, .5);
}

:root[data-theme='light'] {
  --bg: #f2f5f8;
  --panel: #ffffff;
  --panel-2: #eef2f6;
  --line: #d7e0e8;
  --text: #12212e;
  --muted: #5b7186;
  --accent: #00796b;
  --accent-ink: #ffffff;
  --danger: #c33c34;
  --ok: #2f7d32;
  --warn: #9a6b00;
  --shadow: rgba(16, 32, 48, .18);
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    --bg: #f2f5f8;
    --panel: #ffffff;
    --panel-2: #eef2f6;
    --line: #d7e0e8;
    --text: #12212e;
    --muted: #5b7186;
    --accent: #00796b;
    --accent-ink: #ffffff;
    --danger: #c33c34;
    --ok: #2f7d32;
    --warn: #9a6b00;
    --shadow: rgba(16, 32, 48, .18);
  }
}

:root {
  --radius: 12px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --tap: 44px; /* comfortable touch target */
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}

.hidden { display: none !important; }

/* One stroke weight for every icon, inheriting the surrounding colour. */
.icon {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}
button .icon { vertical-align: -3px; }
button.block { display: flex; align-items: center; justify-content: center; gap: 8px; }
h1 .icon, .brand .icon { vertical-align: -4px; color: var(--accent); }
.muted { color: var(--muted); }
.error { color: var(--danger); font-size: 13px; }
.ok { color: var(--ok); font-size: 13px; }
.hint { color: var(--muted); font-size: 13px; margin: 6px 0 0; }
.mobile-only { display: none; }

h1 { font-size: 22px; margin: 0 0 8px; }
h2 { font-size: 18px; margin: 0; }
h3 { font-size: 13px; margin: 20px 0 8px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }

button {
  font: inherit;
  cursor: pointer;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  padding: 9px 14px;
  min-height: var(--tap);
  transition: filter .12s ease, background .12s ease;
  touch-action: manipulation;
}
button:hover:not(:disabled) { filter: brightness(1.12); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
button.danger { background: transparent; color: var(--danger); border-color: var(--danger); }
button.ghost { background: transparent; }
button.block { display: block; width: 100%; margin-top: 10px; }
button.icon { min-width: var(--tap); padding: 8px 10px; display: grid; place-items: center; }
button.close { border: none; font-size: 20px; }

input, textarea, select {
  font: max(16px, 1em) inherit; /* 16px stops iOS zooming on focus */
  width: 100%;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  margin-top: 6px;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
textarea { resize: vertical; }
label { display: block; margin-top: 14px; font-size: 13px; color: var(--muted); }
label.checkbox { display: flex; align-items: center; gap: 10px; color: var(--text); min-height: var(--tap); }
label.checkbox input { width: auto; margin: 0; }

code { font-family: var(--mono); font-size: 12px; }

.segmented { display: flex; gap: 6px; }
.segmented button { flex: 1; }
.segmented button[aria-pressed='true'] { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }

/* ------------------------------------------------------------- screens -- */

.screen {
  display: grid;
  place-items: center;
  min-height: 100%;
  padding: calc(24px + var(--safe-top)) 16px calc(24px + var(--safe-bottom));
}

.card {
  width: 100%;
  max-width: 420px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px;
}

/* ---------------------------------------------------------------- app --- */

#app {
  display: grid;
  grid-template-columns: 300px 1fr;
  height: 100%;
}

#sidebar {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: calc(16px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
  background: var(--panel);
  border-right: 1px solid var(--line);
  min-height: 0;
}

.sidebar-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.sidebar-id { min-width: 0; }
.brand { font-weight: 700; }
.address { font-family: var(--mono); font-size: 11px; color: var(--muted); word-break: break-all; margin-top: 2px; }
.sidebar-foot { margin-top: auto; }

.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); display: block; }
.dot.online { background: var(--ok); }
.dot.connecting { background: var(--warn); }

.peers { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; min-height: 0; -webkit-overflow-scrolling: touch; }
.peers li { padding: 10px 12px; border-radius: 10px; cursor: pointer; border: 1px solid transparent; min-height: var(--tap); }
.peers li:hover { background: var(--panel-2); }
.peers li.active { background: var(--panel-2); border-color: var(--accent); }
.peer-name { font-weight: 600; display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.peer-sub { font-size: 12px; color: var(--muted); font-family: var(--mono); }
.badge { font-size: 11px; background: var(--accent); color: var(--accent-ink); border-radius: 999px; padding: 1px 8px; font-weight: 700; }
.badge.quiet { background: var(--panel-2); color: var(--muted); }

#chat { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.empty { display: grid; place-items: center; height: 100%; color: var(--muted); padding: 24px; text-align: center; }
#chat-view { display: flex; flex-direction: column; min-height: 0; height: 100%; }

.chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(12px + var(--safe-top)) 16px 12px;
  border-bottom: 1px solid var(--line);
}
.chat-who { flex: 1; min-width: 0; }
.chat-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-sub { font-size: 12px; color: var(--muted); font-family: var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-actions { display: flex; gap: 6px; align-items: center; }

button.route {
  font-size: 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 10px;
  min-height: 32px;
  background: transparent;
  color: var(--muted);
  white-space: nowrap;
}
button.route.p2p { color: var(--ok); border-color: var(--ok); }
button.route.pending { color: var(--warn); border-color: var(--warn); }

.route-detail {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  margin-bottom: 8px;
  font-size: 13px;
}
.route-detail .headline { font-weight: 600; margin-bottom: 4px; }
.route-detail .headline.p2p { color: var(--ok); }
.route-detail .headline.pending { color: var(--warn); }
.route-detail dl { margin: 8px 0 0; display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; }
.route-detail dt { color: var(--muted); }
.route-detail dd { margin: 0; overflow-wrap: anywhere; }

.probe { margin-top: 10px; border: 1px solid var(--line); border-radius: 10px; padding: 12px; font-size: 13px; }
.probe.good { border-color: var(--ok); }
.probe.warn { border-color: var(--warn); }
.probe.bad { border-color: var(--danger); }
.probe .types { font-family: var(--mono); font-size: 12px; color: var(--muted); margin-top: 6px; }

.epoch-bar {
  position: relative;
  min-height: 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  font-size: 11px;
  color: var(--muted);
  display: flex;
  align-items: center;
  padding: 4px 16px;
}
.epoch-bar span { position: relative; z-index: 1; }
.epoch-fill { position: absolute; inset: 0 auto 0 0; background: color-mix(in srgb, var(--accent) 22%, transparent); transition: width .2s ease; }

.messages {
  list-style: none;
  margin: 0;
  padding: 16px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.messages li { max-width: min(560px, 82%); }
.messages li.out { align-self: flex-end; }
.bubble {
  padding: 9px 13px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
li.out .bubble { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
li.queued .bubble { opacity: .6; border-style: dashed; }
.meta { font-size: 11px; color: var(--muted); margin-top: 3px; display: flex; gap: 6px; align-items: center; }
li.out .meta { justify-content: flex-end; }
svg.ticks { display: block; overflow: visible; }
svg.ticks path { fill: none; stroke: var(--muted); stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
svg.ticks.read path { stroke: var(--accent); }
.system { align-self: center; font-size: 12px; color: var(--muted); font-style: italic; display: flex; align-items: center; gap: 6px; }
.system .icon { color: var(--accent); }

/* ------------------------------------------------------------ вложения -- */

.attachment {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  min-width: 220px;
}
.attachment .icon { color: inherit; opacity: .85; }
.attachment-name { font-weight: 600; overflow-wrap: anywhere; }
.attachment-size { font-size: 12px; opacity: .75; }
.attachment button {
  background: transparent;
  border-color: currentColor;
  color: inherit;
  min-height: 36px;
  min-width: 36px;
  padding: 4px 8px;
}

.attachment-preview { margin-top: 8px; }
.attachment-preview img,
.attachment-preview video {
  display: block;
  max-width: 100%;
  max-height: 320px;
  border-radius: 10px;
  background: #000;
}
.attachment-preview audio { width: 100%; }

.progress {
  height: 4px;
  border-radius: 999px;
  background: color-mix(in srgb, currentColor 25%, transparent);
  overflow: hidden;
  margin-top: 8px;
  grid-column: 1 / -1;
}
.progress i { display: block; height: 100%; background: currentColor; transition: width .15s ease; }

.drop-hint {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--accent) 16%, var(--bg));
  border: 2px dashed var(--accent);
  border-radius: var(--radius);
  margin: 12px;
  color: var(--accent);
  font-weight: 600;
  pointer-events: none;
  z-index: 5;
}
#chat-view { position: relative; }
.sender { font-size: 11px; font-weight: 600; color: var(--accent); margin-bottom: 2px; }

.participants { list-style: none; margin: 14px 0 0; padding: 0; display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.participants li {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 12px;
  color: var(--muted);
}
.participants li.active { color: var(--ok); border-color: var(--ok); }
.participants .leg-code { font-family: var(--mono); letter-spacing: .12em; margin-left: 8px; color: var(--accent); }
.participants li.ringing { color: var(--warn); border-color: var(--warn); }

.member-off { opacity: .6; }

.composer {
  display: flex;
  gap: 8px;
  padding: 12px 16px 4px;
  border-top: 1px solid var(--line);
  align-items: flex-end;
}
.composer textarea { margin: 0; max-height: 140px; }
.composer button { flex: none; }
.composer-hint { padding: 0 16px; margin: 4px 0 0; }
#chat-view .composer-hint:empty { display: none; }
#chat-view { padding-bottom: var(--safe-bottom); }

/* -------------------------------------------------------------- modals -- */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .6);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 40;
  overflow-y: auto;
}
.modal-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px;
  width: 100%;
  max-width: 460px;
  max-height: 92vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px var(--shadow);
}
.modal-card.wide { max-width: 760px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }

.steps { list-style: none; margin: 0 0 16px; padding: 0; display: flex; flex-wrap: wrap; gap: 8px; font-size: 13px; }
.step { color: var(--muted); border: 1px solid var(--line); border-radius: 999px; padding: 4px 12px; }
.step.done { color: var(--ok); border-color: var(--ok); }
.step.current { color: var(--accent); border-color: var(--accent); }

.pair-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.pair-side { min-width: 0; }
.qr-holder { background: #fff; border-radius: 10px; padding: 10px; display: grid; place-items: center; }
.qr-holder svg { width: 100%; height: auto; display: block; }
#scan-video { width: 100%; aspect-ratio: 1; object-fit: cover; background: #000; border-radius: 10px; }

.invite-text { margin-top: 10px; font-size: 13px; color: var(--muted); }
.invite-text summary { cursor: pointer; min-height: var(--tap); display: flex; align-items: center; }
.invite-text code {
  display: block;
  margin-top: 8px;
  padding: 8px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow-wrap: anywhere;
  user-select: all;
  color: var(--text);
}

.pair-done { margin-top: 18px; border-top: 1px solid var(--line); padding-top: 16px; }
.sas { font-family: var(--mono); font-size: clamp(26px, 8vw, 34px); letter-spacing: .3em; text-align: center; padding: 12px; color: var(--accent); }

/* ------------------------------------------------------------- servers -- */

.servers { list-style: none; margin: 14px 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.server {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
}
.server-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.server-url { font-family: var(--mono); font-size: 11px; color: var(--muted); overflow-wrap: anywhere; }
.server-actions { display: flex; gap: 4px; }
.server-actions button { min-height: 36px; min-width: 36px; padding: 4px 9px; font-size: 12px; }
.server.off { opacity: .55; }
.server-add { border-top: 1px solid var(--line); padding-top: 12px; }

.graph-body { max-height: 320px; overflow-y: auto; margin: 12px 0; }
.graph-peer { border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin-bottom: 10px; }
.graph-peer h4 { margin: 0 0 8px; font-size: 14px; overflow-wrap: anywhere; }
.chain { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; font-size: 11px; font-family: var(--mono); margin: 4px 0; }
.chain-label { color: var(--muted); width: 84px; flex: none; font-family: inherit; }
.node { border: 1px solid var(--line); border-radius: 6px; padding: 2px 6px; }
.node.private { border-color: var(--accent); color: var(--accent); }
.node.current { background: var(--panel-2); font-weight: 700; }
.arrow { color: var(--muted); }

.key-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; border-top: 1px solid var(--line); padding-top: 14px; }
.key-action { min-width: 0; }
.kv { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.kv code { overflow-wrap: anywhere; text-align: right; }

/* --------------------------------------------------------------- calls -- */

.call-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: grid;
  place-items: center;
  z-index: 60;
  padding: calc(16px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
}
.call-card { text-align: center; width: min(620px, 100%); }
.call-who { font-size: 22px; font-weight: 600; }
.call-state { color: var(--muted); margin-bottom: 14px; }
.call-video { position: relative; }
#call-remote { width: 100%; border-radius: var(--radius); background: #000; aspect-ratio: 16/9; }
#call-local { position: absolute; right: 12px; bottom: 12px; width: 28%; max-width: 160px; border-radius: 10px; background: #000; }
.call-video.audio-only { display: none; }

.call-security { margin-top: 14px; }
.call-sas { font-family: var(--mono); font-size: clamp(22px, 7vw, 30px); letter-spacing: .3em; color: var(--accent); }
.call-security .hint { margin-top: 2px; }

.call-buttons { display: flex; gap: 10px; justify-content: center; margin-top: 18px; flex-wrap: wrap; }
.call-buttons button { min-width: 140px; }

/* Below the header: a notice that hides who you are talking to is worse than
   no notice at all. */
.toast {
  position: fixed;
  left: 50%;
  top: calc(74px + var(--safe-top));
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 16px;
  z-index: 80;
  max-width: min(520px, calc(100vw - 32px));
  box-shadow: 0 8px 28px var(--shadow);
}
.toast.bad { border-color: var(--danger); color: var(--danger); }

/* -------------------------------------------------------------- mobile -- */

@media (max-width: 760px) {
  .mobile-only { display: grid; }

  #app { grid-template-columns: 1fr; }
  #app:not(.chat-open) #chat { display: none; }
  #app.chat-open #sidebar { display: none; }

  .messages li { max-width: 88%; }
  /* The route chip is a real control on a phone, so make it a real target. */
  button.route { min-height: 40px; font-size: 11px; padding: 3px 8px; }
  .pair-grid, .key-actions { grid-template-columns: 1fr; }
  .call-buttons button { flex: 1 1 140px; }

  /* Full-height sheets read better than floating cards on a phone. */
  .modal { padding: 0; place-items: stretch; }
  .modal-card {
    max-width: none;
    max-height: none;
    min-height: 100%;
    border: none;
    border-radius: 0;
    padding: calc(18px + var(--safe-top)) 16px calc(18px + var(--safe-bottom));
  }
  .modal-head { position: sticky; top: calc(-1 * var(--safe-top)); background: var(--panel); padding-top: 4px; z-index: 1; }
  .graph-body { max-height: none; }
}

@media (max-width: 380px) {
  .composer button { padding: 9px 12px; }
  .chain-label { width: 100%; }
}
