:root {
  --paper: #f3efe6;
  --paper-2: #ebe5d8;
  --card: #fbf9f4;
  --ink: #1c1b17;
  --ink-2: #5d594f;
  --ink-3: #8f897b;
  --rule: #d9d1bf;
  --red: #c8381e;
  --red-ink: #ffffff;
  --red-soft: #f3d8cf;
  --out: #f0dcd2;
  --in: #fbf9f4;
  --ok: #2f7a4b;
  --warn: #a8641a;
  --read: #1f6fb2;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Noto Sans Arabic", "Noto Sans Devanagari",
    "PingFang SC", "Hiragino Sans", "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Noto Serif", serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #16150f;
    --paper-2: #1f1d16;
    --card: #23211a;
    --ink: #ece6d6;
    --ink-2: #b3ab98;
    --ink-3: #807968;
    --rule: #37342a;
    --red: #e2573a;
    --red-soft: #3a1d15;
    --out: #3b2119;
    --in: #23211a;
    --ok: #5fb07c;
    --warn: #d69a4a;
    --read: #6aa9e0;
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

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

body {
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

button,
input,
textarea,
select {
  font: inherit;
  color: inherit;
}

a {
  color: var(--red);
}

h2,
h3,
h4 {
  margin: 0 0 12px;
  line-height: 1.2;
}

h2 {
  font: 600 24px/1.2 var(--serif);
}

h3 {
  font-size: 17px;
}

h4 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-2);
  margin-top: 18px;
}

code {
  font-family: var(--mono);
  font-size: 0.9em;
}

svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}

.muted {
  color: var(--ink-2);
}

.hint,
.field small {
  display: block;
  color: var(--ink-2);
  font-size: 13px;
  margin: 4px 0 14px;
}

.error {
  color: var(--red);
  font-weight: 600;
  margin: 10px 0;
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

/* Buttons and fields */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 18px;
  border: 1.5px solid var(--ink);
  border-radius: var(--radius);
  background: transparent;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}

.btn:hover {
  background: var(--paper-2);
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--red);
  border-color: var(--red);
  color: var(--red-ink);
}

.btn-primary:hover {
  background: var(--red);
  filter: brightness(1.08);
}

.btn-danger {
  border-color: var(--red);
  color: var(--red);
}

.btn-small {
  min-height: 34px;
  padding: 0 12px;
  font-size: 14px;
}

.link-btn {
  background: none;
  border: 0;
  padding: 8px 0;
  color: var(--ink-2);
  cursor: pointer;
  font-weight: 600;
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ink-2);
  cursor: pointer;
}

.icon-btn:hover {
  background: var(--paper-2);
  color: var(--ink);
}

input,
textarea,
select {
  width: 100%;
  padding: 11px 12px;
  border: 1.5px solid var(--rule);
  border-radius: 8px;
  background: var(--card);
  outline: none;
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--ink);
}

.field {
  display: block;
  margin-bottom: 6px;
}

.field > span {
  display: block;
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 6px;
}

.check {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  font-weight: 600;
  margin: 14px 0 2px;
  cursor: pointer;
}

.check input {
  width: 18px;
  height: 18px;
  margin: 2px 0 0;
  accent-color: var(--red);
  flex: none;
}

/* Stamp / wordmark */

.stamp {
  display: inline-block;
  padding: 2px 10px 3px;
  border: 2px solid var(--red);
  border-radius: 4px;
  color: var(--red);
  font: 700 15px/1.2 var(--serif);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  transform: rotate(-3deg);
}

.stamp-large {
  font-size: 28px;
  padding: 4px 18px 6px;
  border-width: 3px;
  opacity: 0.35;
}

.wordmark {
  font: 700 clamp(64px, 16vw, 112px) / 0.9 var(--serif);
  letter-spacing: -0.03em;
  margin: 0 0 18px;
}

[dir="rtl"] .wordmark {
  text-align: right;
}

.wordmark-dot {
  color: var(--red);
}

/* Entry screens */

.view-entry {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

.entry-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px max(20px, env(safe-area-inset-left));
  gap: 12px;
}

.entry-top .lang-select {
  width: auto;
  min-width: 140px;
  padding: 8px 10px;
}

.entry {
  width: min(560px, 100%);
  margin: 0 auto;
  padding: 24px 20px 48px;
  flex: 1;
}

.tagline {
  font: 400 21px/1.35 var(--serif);
  margin: 0 0 26px;
  max-width: 30ch;
}

.facts {
  list-style: none;
  counter-reset: fact;
  padding: 0;
  margin: 0 0 30px;
  border-top: 1px solid var(--rule);
}

.facts li {
  counter-increment: fact;
  display: flex;
  gap: 14px;
  padding: 12px 0;
  border-bottom: 1px solid var(--rule);
}

.facts li::before {
  content: counter(fact, decimal-leading-zero);
  font: 600 13px/1.6 var(--mono);
  color: var(--red);
}

.entry-actions {
  display: grid;
  gap: 10px;
}

.fineprint {
  margin-top: 28px;
  font-size: 13px;
  color: var(--ink-2);
}

.word-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 0;
  margin: 0 0 12px;
  list-style: none;
  counter-reset: word;
}

.word-grid li {
  counter-increment: word;
  display: flex;
  gap: 8px;
  align-items: baseline;
  padding: 9px 10px;
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: 8px;
  font: 600 15px var(--mono);
  direction: ltr;
}

.word-grid li::before {
  content: counter(word);
  color: var(--ink-3);
  font-size: 12px;
  min-width: 1.4em;
  text-align: end;
}

.warning {
  border-inline-start: 4px solid var(--red);
  background: var(--red-soft);
  padding: 12px 14px;
  border-radius: 4px;
  margin: 16px 0;
}

#create-next,
#login-form .btn-primary {
  width: 100%;
  margin-top: 18px;
}

.verify-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 18px;
}

.verify-row label span {
  display: block;
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 6px;
}

#create-step-verify .row {
  margin-top: 18px;
}

#create-step-verify .row .btn-primary {
  flex: 1;
}

#login-words {
  font-family: var(--mono);
  direction: ltr;
}

.login-meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 13px;
  margin-top: 6px;
}

/* Messenger layout */

#view-app {
  height: 100%;
  display: grid;
  grid-template-columns: minmax(280px, 340px) 1fr;
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-inline-end: 1px solid var(--rule);
  background: var(--paper);
}

.side-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 14px 10px;
  padding-top: max(14px, env(safe-area-inset-top));
}

.me {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  padding: 4px;
  border: 0;
  background: none;
  cursor: pointer;
  text-align: start;
}

.me-text {
  display: grid;
  min-width: 0;
}

.me-text strong,
.me-text code {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.me-text code {
  color: var(--ink-2);
  font-size: 12px;
}

.net {
  flex: none;
  font: 600 12px var(--mono);
  color: var(--ink-2);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.net::before {
  content: '';
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--ink-3);
}

.net.ok::before {
  background: var(--ok);
}

.net.bad::before {
  background: var(--red);
}

.side-actions {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  padding: 0 14px 12px;
  border-bottom: 1px solid var(--rule);
}

.side-actions button {
  display: grid;
  justify-items: center;
  gap: 4px;
  padding: 10px 4px 8px;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--card);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.side-actions button:hover {
  border-color: var(--ink);
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px 0;
}

.chat-list h4 {
  padding: 0 16px;
  margin: 12px 0 6px;
}

.chat-list .empty {
  padding: 24px 18px;
  color: var(--ink-2);
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 16px;
  border: 0;
  background: none;
  text-align: start;
  cursor: pointer;
}

.chat-item:hover {
  background: var(--paper-2);
}

.chat-item.active {
  background: var(--paper-2);
  box-shadow: inset 3px 0 0 var(--red);
}

[dir='rtl'] .chat-item.active {
  box-shadow: inset -3px 0 0 var(--red);
}

.chat-item-text {
  flex: 1;
  min-width: 0;
  display: grid;
  gap: 2px;
}

.chat-item-top,
.chat-item-bottom {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: baseline;
}

.chat-item-name,
.chat-item-preview {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-item-preview,
.chat-item-time {
  color: var(--ink-2);
  font-size: 13px;
}

.chat-item-time {
  flex: none;
}

.badge {
  flex: none;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--red);
  color: var(--red-ink);
  font: 700 12px/20px var(--font);
  text-align: center;
}

.avatar {
  position: relative;
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: hsl(var(--hue, 20) 35% 82%);
  color: hsl(var(--hue, 20) 45% 22%);
  font-weight: 700;
  font-size: 17px;
  text-transform: uppercase;
}

@media (prefers-color-scheme: dark) {
  .avatar {
    background: hsl(var(--hue, 20) 25% 26%);
    color: hsl(var(--hue, 20) 50% 85%);
  }
}

.avatar.online::after {
  content: '';
  position: absolute;
  inset-inline-end: 0;
  bottom: 0;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--ok);
  border: 2px solid var(--paper);
}

.avatar.nearby::after {
  background: var(--warn);
}

.carrying {
  margin: 0;
  padding: 10px 16px;
  border-top: 1px solid var(--rule);
  font-size: 13px;
  color: var(--ink-2);
}

/* Chat */

.chat {
  position: relative;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  background: var(--paper-2);
}

.chat-empty {
  margin: auto;
  text-align: center;
  color: var(--ink-2);
  display: grid;
  gap: 18px;
  justify-items: center;
  padding: 24px;
}

.chat-view {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

.chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--paper);
  border-bottom: 1px solid var(--rule);
}

.chat-title {
  flex: 1;
  min-width: 0;
  display: grid;
}

.chat-title strong,
.chat-title span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#chat-status {
  font-size: 13px;
  color: var(--ink-2);
}

#chat-status.on {
  color: var(--ok);
}

.menu-wrap {
  position: relative;
}

.menu {
  position: absolute;
  inset-inline-end: 0;
  top: 44px;
  z-index: 5;
  min-width: 190px;
  padding: 6px;
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.12);
}

.menu button {
  display: block;
  width: 100%;
  padding: 10px 12px;
  border: 0;
  border-radius: 6px;
  background: none;
  text-align: start;
  cursor: pointer;
}

.menu button:hover {
  background: var(--paper-2);
}

.menu .danger {
  color: var(--red);
}

.request-banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: var(--red-soft);
  border-bottom: 1px solid var(--rule);
  font-weight: 600;
}

.request-banner span {
  flex: 1;
  min-width: 160px;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px max(14px, 4%);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.day {
  align-self: center;
  margin: 14px 0 8px;
  padding: 2px 10px;
  font: 600 12px var(--mono);
  color: var(--ink-2);
  border: 1px solid var(--rule);
  border-radius: 999px;
  background: var(--paper);
}

.msg {
  max-width: min(78%, 560px);
  padding: 8px 12px 6px;
  border-radius: 12px;
  background: var(--in);
  border: 1px solid var(--rule);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  align-self: flex-start;
}

.msg.out {
  align-self: flex-end;
  background: var(--out);
  border-color: transparent;
}

.msg.out + .msg.out,
.msg.in + .msg.in {
  margin-top: -1px;
}

.msg-meta {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
  font: 11px var(--mono);
  color: var(--ink-2);
}

.tick {
  font-family: var(--font);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: -0.15em;
  padding-inline-end: 0.15em;
}

.tick.pending {
  letter-spacing: 0;
  color: var(--warn);
}

.tick.read {
  color: var(--read);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--paper);
  border-top: 1px solid var(--rule);
}

.composer textarea {
  resize: none;
  max-height: 160px;
  border-radius: 20px;
  padding: 10px 16px;
}

.send-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  flex: none;
  border: 0;
  border-radius: 50%;
  background: var(--red);
  color: var(--red-ink);
  cursor: pointer;
}

[dir='rtl'] .send-btn svg,
[dir='rtl'] #chat-back svg {
  transform: scaleX(-1);
}

/* Dialogs */

dialog {
  width: min(480px, calc(100% - 24px));
  max-height: calc(100% - 24px);
  padding: 0;
  border: 1px solid var(--rule);
  border-radius: 14px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 20px 60px rgb(0 0 0 / 0.25);
}

dialog::backdrop {
  background: rgb(20 18 12 / 0.45);
}

.dlg-head {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 12px 12px 18px;
  background: var(--paper);
  border-bottom: 1px solid var(--rule);
}

[dir='rtl'] .dlg-head {
  padding: 12px 18px 12px 12px;
}

.dlg-head h3 {
  margin: 0;
}

.dlg-section {
  padding: 16px 18px;
  border-bottom: 1px solid var(--rule);
}

.dlg-section:last-child {
  border-bottom: 0;
}

.dlg-section h4:first-child {
  margin-top: 0;
}

.qr-box {
  width: 200px;
  margin: 6px auto 12px;
  padding: 10px;
  background: #fff;
  border-radius: 8px;
  border: 1px solid var(--rule);
}

.qr-box svg {
  display: block;
  width: 100%;
  height: auto;
}

.qr-large {
  width: min(320px, 100%);
}

.id-text {
  display: block;
  padding: 10px;
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: 8px;
  word-break: break-all;
  direction: ltr;
  text-align: center;
  margin-bottom: 8px;
}

.steps {
  padding-inline-start: 20px;
  color: var(--ink-2);
}

.steps li {
  margin-bottom: 4px;
}

.lan-code {
  margin-top: 14px;
  text-align: center;
}

.lan-code .row {
  justify-content: center;
}

.lan-peers {
  list-style: none;
  padding: 0;
  margin: 0;
}

.lan-peers li {
  padding: 8px 0;
  border-bottom: 1px solid var(--rule);
}

.lan-peers li:last-child {
  border-bottom: 0;
}

.pager {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 0;
  font: 600 14px var(--mono);
}

.safety-words {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin: 0 0 14px;
  font: 700 18px var(--mono);
  text-align: center;
  direction: ltr;
}

.safety-words span {
  padding: 10px;
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: 8px;
}

.scanner {
  position: relative;
  aspect-ratio: 1;
  max-width: 320px;
  margin: 0 auto 10px;
  overflow: hidden;
  border-radius: 10px;
  background: #000;
}

.scanner video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.scanner-frame {
  position: absolute;
  inset: 14%;
  border: 2px solid rgb(255 255 255 / 0.8);
  border-radius: 8px;
}

#scan-use {
  width: 100%;
  margin-top: 6px;
}

#dlg-settings .dlg-section.row {
  justify-content: space-between;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  z-index: 100;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 8px;
  background: var(--ink);
  color: var(--paper);
  font-weight: 600;
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.2);
}

.only-narrow {
  display: none;
}

@media (max-width: 760px) {
  #view-app {
    grid-template-columns: 1fr;
  }

  .chat {
    display: none;
  }

  body.chat-open .sidebar {
    display: none;
  }

  body.chat-open .chat {
    display: flex;
    height: 100%;
  }

  .only-narrow {
    display: inline-grid;
  }

  .msg {
    max-width: 86%;
  }

  .word-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}
