*, *::before, *::after { box-sizing: border-box; }

[hidden] { display: none !important; }

:root {
  --fg: #fff;
  --bg: #000;
  --muted: #a3a3a3;
  --hairline: #fff;
  --hairline-soft: #3a3a3a;
  --ring: #fff;
  --danger: #fff;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --radius: 8px;
  --radius-sm: 6px;
}

html { color-scheme: dark; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

body {
  display: flex;
  flex-direction: column;
}

a { color: inherit; }

button {
  font: inherit;
  color: inherit;
  background: var(--bg);
  border: 1px solid var(--fg);
  padding: 0.6rem 1rem;
  cursor: pointer;
  border-radius: var(--radius-sm);
}
button:disabled { opacity: 0.5; cursor: default; }
button:focus-visible { outline: 2px solid var(--ring); outline-offset: 2px; }

input, textarea {
  font: inherit;
  color: inherit;
  background: var(--bg);
  border: 1px solid var(--fg);
  padding: 0.6rem 0.75rem;
  width: 100%;
  border-radius: var(--radius-sm);
}
input:focus-visible, textarea:focus-visible { outline: 2px solid var(--ring); outline-offset: 2px; }

/* ---------- shared ---------- */

.page-center {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}

.card {
  width: 100%;
  max-width: 420px;
  border: 1px solid var(--fg);
  border-radius: var(--radius);
  padding: 1.5rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  align-items: stretch;
}

.brand {
  font-size: 2rem;
  margin: 0;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  text-align: center;
  font-weight: 700;
}

.lede { margin: 0; color: var(--muted); text-align: center; }
.status { color: var(--muted); text-align: center; }
.err { color: var(--fg); border: 1px solid var(--fg); border-radius: var(--radius-sm); padding: 0.5rem 0.75rem; margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

.stack { display: flex; flex-direction: column; gap: 0.75rem; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  border: 1px solid var(--fg);
  background: var(--bg);
  color: var(--fg);
  padding: 0.7rem 1rem;
  min-height: 44px;
  cursor: pointer;
  border-radius: var(--radius-sm);
}
.btn-primary { background: var(--fg); color: var(--bg); }
.btn-danger  { background: var(--fg); color: var(--bg); }

.btn-row { display: flex; gap: 0.5rem; }
.btn-row .btn { flex: 1; }

/* ---------- chat layout ---------- */

.chat-body {
  height: 100dvh;
  overflow: hidden;
}

#app {
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  height: 100dvh;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--fg);
  padding-top: max(0.5rem, env(safe-area-inset-top));
}
.chat-title {
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.chat-actions { display: flex; gap: 0.25rem; }

.icon-btn {
  border: 1px solid transparent;
  background: transparent;
  padding: 0.5rem;
  min-width: 44px;
  min-height: 44px;
  display: inline-grid;
  place-items: center;
  color: var(--fg);
  border-radius: var(--radius-sm);
}
.icon-btn:hover, .icon-btn:focus-visible {
  border-color: var(--fg);
}

.messages {
  overflow-y: auto;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  overscroll-behavior: contain;
}

.msg {
  display: flex;
  flex-direction: column;
  max-width: 80%;
  gap: 0.15rem;
}
.msg-other { align-self: flex-start; align-items: flex-start; }
.msg-me    { align-self: flex-end;   align-items: flex-end; }

.msg-author {
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 0 0.1rem;
}
.msg-bubble {
  border: 1px solid var(--fg);
  border-radius: 14px;
  padding: 0.55rem 0.75rem;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg-me .msg-bubble  { border-bottom-right-radius: 4px; }
.msg-other .msg-bubble { border-bottom-left-radius: 4px; }
.msg-me .msg-bubble { background: var(--fg); color: var(--bg); }
.msg-meta {
  font-size: 0.7rem;
  color: var(--muted);
  display: flex;
  gap: 0.25rem;
  padding: 0 0.1rem;
}
.msg-me .msg-meta { justify-content: flex-end; }
.read-by { color: var(--muted); }

.msg-system {
  align-self: center;
  font-size: 0.8rem;
  color: var(--muted);
  padding: 0.25rem 0.5rem;
  border-top: 1px dashed var(--hairline-soft);
  border-bottom: 1px dashed var(--hairline-soft);
  margin: 0.25rem 0;
}

.typing {
  padding: 0.25rem 0.75rem;
  font-size: 0.8rem;
  color: var(--muted);
  border-top: 1px solid var(--hairline-soft);
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.5rem;
  padding: 0.5rem;
  border-top: 1px solid var(--fg);
  padding-bottom: max(0.5rem, env(safe-area-inset-bottom));
  align-items: end;
}
.composer textarea {
  resize: none;
  min-height: 44px;
  max-height: 160px;
  line-height: 1.3;
}
.composer .btn { min-height: 44px; }

/* ---------- modal ---------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 1rem;
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.65);
}
.modal-panel {
  position: relative;
  background: var(--bg);
  border: 1px solid var(--fg);
  border-radius: var(--radius);
  max-width: 420px;
  width: 100%;
  padding: 1.25rem;
  max-height: calc(100dvh - 2rem);
  overflow: auto;
}
.modal-close {
  position: absolute;
  top: 0.25rem;
  right: 0.25rem;
  border: 1px solid transparent;
  background: transparent;
  font-size: 1.5rem;
  line-height: 1;
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: var(--radius-sm);
}
.modal-stack { display: flex; flex-direction: column; gap: 0.75rem; }
.modal-stack h2 { margin: 0; font-size: 1.2rem; }

.qr-box {
  background: #fff;
  border: 1px solid var(--fg);
  border-radius: var(--radius-sm);
  padding: 0.75rem;
  display: grid;
  place-items: center;
}
.qr-box svg { width: 100%; height: auto; display: block; max-width: 280px; }

.share-link {
  border: 1px solid var(--fg);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.75rem;
  overflow-wrap: anywhere;
}
.share-link code { font-family: var(--mono); font-size: 0.85rem; }

/* ---------- landing ---------- */

.landing {
  height: auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: clamp(1rem, 6vh, 4rem) 1.25rem max(3rem, env(safe-area-inset-bottom));
  gap: 1.75rem;
}
.landing .card { align-self: center; }

.explainer {
  width: 100%;
  max-width: 420px;
  color: var(--fg);
  font-family: "Courier New", Courier, ui-monospace, monospace;
  font-size: 0.9rem;
  line-height: 1.6;
  letter-spacing: 0.01em;
}
.explainer p {
  margin: 0 0 0.9rem 0;
  color: var(--fg);
  text-indent: 2em;
}
.explainer p:last-child { margin-bottom: 0; }
.explainer em {
  color: var(--fg);
  font-style: italic;
  font-weight: 600;
}

.explainer-cursor {
  display: inline-block;
  width: 0.55ch;
  height: 1.05em;
  background: var(--fg);
  vertical-align: -0.18em;
  margin-left: 2px;
  animation: nexus-blink 0.85s step-end infinite;
}
@keyframes nexus-blink {
  0%, 50% { opacity: 1; }
  50.01%, 100% { opacity: 0; }
}

/* ---------- desktop tweaks ---------- */

@media (min-width: 720px) {
  #app {
    max-width: 900px;
    margin: 0 auto;
    border-left: 1px solid var(--fg);
    border-right: 1px solid var(--fg);
  }
}
