/* ===== Design tokens ===== */
:root {
  --bg: #eef1f5;
  --surface: #ffffff;
  --surface-alt: #f6f8fa;
  --text: #17202a;
  --muted: #5c6b7a;
  --border: #d9e0e8;
  --accent: #0e7490;
  --accent-hover: #155e75;
  --accent-contrast: #ffffff;
  --sent: #0e7490;
  --sent-text: #ffffff;
  --danger: #b3261e;
  --ok: #1a7f37;
  --warn: #9a6700;
  --font-body:
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    Roboto,
    "Helvetica Neue",
    Arial,
    sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --radius: 0.75rem;
  --shadow:
    0 0.0625rem 0.125rem rgba(15, 23, 42, 0.06),
    0 0.5rem 1.5rem rgba(15, 23, 42, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --surface: #161b22;
    --surface-alt: #1d232b;
    --text: #e6edf3;
    --muted: #8b98a5;
    --border: #2d333b;
    --accent: #22a3bf;
    --accent-hover: #2fb9d6;
    --shadow: none;
  }
}

/* ===== Base ===== */
* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

#app,
.share-overlay {
  min-height: var(--app-height, 100dvh);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

h1 {
  font-size: 1.35rem;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.01em;
}

.muted {
  color: var(--muted);
  font-size: 0.95rem;
  margin: 0;
}

/* ===== Card + layout ===== */
.card {
  width: 100%;
  max-width: 30rem;
  background: var(--surface);
  border: 0.0625rem solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2rem;
}

.stack > * + * {
  margin-top: 1.5rem;
}

/* ===== Buttons ===== */
.btn {
  font: inherit;
  font-weight: 600;
  border: 0.0625rem solid transparent;
  border-radius: 0.5rem;
  padding: 0.625rem 1.25rem;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

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

.btn-primary {
  background: var(--accent);
  color: var(--accent-contrast);
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-quiet {
  background: transparent;
  color: var(--muted);
  border-color: var(--border);
}

.btn-quiet:hover:not(:disabled) {
  color: var(--danger);
  border-color: var(--danger);
}

/* ===== Form fields ===== */
.field {
  display: block;
}

.field-label {
  display: block;
  font-weight: 600;
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
}

.field input[type="text"] {
  width: 100%;
  padding: 0.625rem 0.75rem;
  border: 0.0625rem solid var(--border);
  border-radius: 0.5rem;
  font: inherit;
  background: var(--surface-alt);
  color: var(--text);
}

.field input[type="text"]:focus-visible {
  outline: 0.125rem solid var(--accent);
  outline-offset: 0.0625rem;
}

/* ===== Preset pills (radio groups) ===== */
.presets {
  border: 0;
  padding: 0;
  margin: 0;
}

.presets legend {
  font-weight: 600;
  font-size: 0.9rem;
  padding: 0;
  margin-bottom: 0.5rem;
}

.preset-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.preset-row label {
  position: relative;
}

.preset-row input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.preset-row span {
  display: inline-block;
  padding: 0.5rem 0.875rem;
  border: 0.0625rem solid var(--border);
  border-radius: 62.4375rem;
  font-size: 0.9rem;
  background: var(--surface-alt);
  cursor: pointer;
  user-select: none;
}

.preset-row input:checked + span {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
}

.preset-row input:focus-visible + span {
  outline: 0.125rem solid var(--accent);
  outline-offset: 0.125rem;
}

/* ===== Share overlay ===== */
/* Covers the (empty) chat rendered underneath it; sizing/centering/padding
   are shared with #app above so the two always match. */
.share-overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  background: var(--bg);
  overflow-y: auto;
}

.share-card {
  text-align: center;
}

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

.share-link input {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  padding: 0.625rem;
  border: 0.0625rem solid var(--border);
  border-radius: 0.5rem;
  background: var(--surface-alt);
  color: var(--text);
}

.qr {
  width: 11.875rem;
  margin: 0 auto;
}

.stack > .qr {
  margin-top: 2.25rem;
}

.qr svg {
  display: block;
  width: 100%;
  height: auto;
  background: #ffffff;
  border-radius: 0.5rem;
}

.countdown {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  color: var(--text);
}

.keep-open {
  font-weight: 700;
  color: var(--warn);
  margin: 0;
}

/* ===== Chat ===== */
.chat {
  width: 100%;
  max-width: 45rem;
  height: min(100dvh - 2rem, 51.25rem);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 0.0625rem solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
  padding: 0.75rem 1rem;
  border-bottom: 0.0625rem solid var(--border);
  background: var(--surface-alt);
}

.safety {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.625rem;
}

.safety-toggle {
  flex: none;
  width: 1.625rem;
  height: 1.625rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0.09375rem solid var(--muted);
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 0.85rem;
  font-style: italic;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
}

.safety-toggle:hover,
.safety-toggle[aria-expanded="true"] {
  color: var(--accent);
  border-color: var(--accent);
}

.safety-popover {
  position: absolute;
  top: calc(100% + 0.625rem);
  left: 0;
  z-index: 5;
  width: min(18.75rem, 76vw);
  padding: 0.625rem 0.875rem;
  background: var(--surface);
  color: var(--text);
  border: 0.0625rem solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.safety-code {
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 1.2rem;
  letter-spacing: 0.18em;
}

.safety-hint {
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.4;
  margin: 0;
}

.chat-header-right {
  display: flex;
  align-items: center;
  gap: 0.625rem;
}

.peer-name {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}

.status {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.375rem;
}

.status-pill {
  flex: none;
  width: 1.5rem;
  height: 1.5rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  border: none;
  background: transparent;
  border-radius: 50%;
  cursor: pointer;
}

.status-dot {
  width: 0.625rem;
  height: 0.625rem;
  border-radius: 50%;
  background: var(--muted);
}

.status-pill.secure .status-dot {
  background: var(--ok);
}

.status-pill.connecting .status-dot,
.status-pill.waiting .status-dot,
.status-pill.securing .status-dot {
  background: var(--warn);
  animation: status-pulse 1.6s ease-in-out infinite;
}

.status-pill.securing .status-dot {
  animation-duration: 0.8s;
}

.status-pill.reconnecting .status-dot {
  background: var(--danger);
  animation: status-pulse 0.9s ease-in-out infinite;
}

.status-countdown {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--danger);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

@keyframes status-pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.35;
  }
}

@media (prefers-reduced-motion: reduce) {
  .status-dot {
    animation: none !important;
  }
}

.status-popover {
  position: absolute;
  top: calc(100% + 0.625rem);
  right: 0;
  z-index: 5;
  width: min(16.25rem, 76vw);
  padding: 0.625rem 0.875rem;
  background: var(--surface);
  color: var(--text);
  border: 0.0625rem solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.status-hint {
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.4;
  margin: 0;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.msg {
  max-width: 75%;
  padding: 0.5625rem 0.8125rem;
  border-radius: 0.875rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg.sent {
  align-self: flex-end;
  background: var(--sent);
  color: var(--sent-text);
  border-bottom-right-radius: 0.25rem;
}

.msg.received {
  align-self: flex-start;
  background: var(--surface-alt);
  border: 0.0625rem solid var(--border);
  border-bottom-left-radius: 0.25rem;
}

.msg.system {
  align-self: center;
  max-width: 90%;
  background: transparent;
  color: var(--muted);
  font-size: 0.8rem;
  text-align: center;
}

/* --- Attachments --- */
.attach-btn {
  flex: none;
  align-self: center;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 50%;
  padding: 0;
  font-size: 1.25rem;
  line-height: 1;
}

.msg.attachment {
  max-width: min(75%, 20rem);
}

.attachment-body {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.attachment-body--stacked {
  flex-direction: column;
  align-items: stretch;
}

.attachment-body--stacked audio {
  width: 100%;
}

.progress-ring {
  flex: none;
  width: 2.25rem;
  height: 2.25rem;
  transform: rotate(-90deg);
}

.progress-ring circle {
  fill: none;
  stroke-width: 3;
}

.progress-ring .ring-track {
  stroke: currentColor;
  opacity: 0.2;
}

.progress-ring .ring-fill {
  stroke: currentColor;
  transition: stroke-dashoffset 0.15s linear;
}

.attachment-info {
  flex: 1 1 auto;
  min-width: 0;
}

.attachment-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: start;
}

.attachment-size,
.attachment-fail-note {
  font-size: 0.75rem;
  opacity: 0.7;
}

.attachment-cancel {
  flex: none;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  padding: 0.25rem;
  color: inherit;
  opacity: 0.7;
}

.attachment-preview {
  display: block;
  max-width: 100%;
  border-radius: 0.5rem;
  cursor: pointer;
}

video.attachment-preview {
  cursor: default;
}

.attachment-tile {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.25rem;
}

.attachment-download {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-inline-start: auto;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;
  color: inherit;
  opacity: 0.7;
}

.attachment-download:hover {
  opacity: 1;
  background: var(--border);
}

.download-icon {
  width: 1.125rem;
  height: 1.125rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.attachment-failed {
  opacity: 0.6;
}

.image-overlay {
  border: none;
  background: transparent;
  padding: 0;
  max-width: 95vw;
  max-height: 95vh;
}

.image-overlay img {
  max-width: 95vw;
  max-height: 95vh;
  display: block;
}

.image-overlay::backdrop {
  background: rgb(0 0 0 / 0.7);
}

/* ===== Composer ===== */
.composer {
  border-top: 0.0625rem solid var(--border);
  background: var(--surface-alt);
  padding: 0.75rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
}

.composer-row {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
}

.composer textarea {
  flex: 1;
  resize: none;
  min-height: 2.75rem;
  max-height: 20rem; /* autosized from JS up to here, then scrolls */
  overflow-y: auto;
  padding: 0.625rem 0.75rem;
  border: 0.0625rem solid var(--border);
  border-radius: 0.5rem;
  font: inherit;
  background: var(--surface);
  color: var(--text);
}

.composer textarea:focus-visible {
  outline: 0.125rem solid var(--accent);
  outline-offset: 0.0625rem;
}

.composer-meta {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  font-size: 0.74rem;
  color: var(--muted);
}

.counter {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.counter.over {
  color: var(--danger);
  font-weight: 700;
}

/* ===== Terminal screens ===== */
.screen .actions {
  margin-top: 0.5rem;
}

a.start-new {
  color: var(--accent);
  font-weight: 600;
}

.settings-btn {
  position: fixed;
  top: calc(0.5rem + env(safe-area-inset-top));
  right: calc(0.5rem + env(safe-area-inset-right));
  z-index: 20; /* above the share overlay (10) */
  width: 2.75rem; /* touch target */
  height: 2.75rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0.0625rem solid var(--border);
  border-radius: 50%;
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
  box-shadow: var(--shadow);
}

.settings-btn svg {
  width: 1.25rem;
  height: 1.25rem;
}

.settings-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
}

.settings-dialog {
  width: min(22.5rem, calc(100vw - 2rem));
  padding: 0; /* clicks on the dialog itself = backdrop (light dismiss) */
  border: 0.0625rem solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.settings-dialog::backdrop {
  background: rgba(15, 23, 42, 0.45);
}

.settings-body {
  padding: 1.125rem 1.375rem 1.375rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.settings-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.settings-head h2 {
  font-size: 1.05rem;
  margin: 0;
}

.settings-close {
  width: 2rem;
  height: 2rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0.0625rem solid transparent;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font: inherit;
  line-height: 1;
  cursor: pointer;
}

.settings-close:hover {
  color: var(--text);
  border-color: var(--border);
  background: var(--surface-alt);
}

.settings-close svg {
  width: 1.15rem;
  height: 1.15rem;
}

.end-chat-dialog {
  width: min(22.5rem, calc(100vw - 2rem));
  padding: 0; /* clicks on the dialog itself = backdrop (light dismiss) */
  border: 0.0625rem solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.end-chat-dialog::backdrop {
  background: rgba(15, 23, 42, 0.45);
}

.end-chat-body {
  padding: 1.375rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.end-chat-body h2 {
  font-size: 1.05rem;
  margin: 0;
}

.end-chat-body p {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.625rem;
  margin-top: 0.5rem;
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-danger:hover:not(:disabled) {
  background: var(--danger);
  filter: brightness(0.9);
}

.theme-select {
  width: 100%;
  min-height: 2.75rem;
  padding: 0.5rem 0.625rem;
  font: inherit;
  font-size: 0.9rem;
  border: 0.0625rem solid var(--border);
  border-radius: 0.5rem;
  background: var(--surface-alt);
  color: var(--text);
}

/* ===== Small screens ===== */
@media (max-width: 520px) {
  #app,
  .share-overlay {
    padding: 0;
  }

  .card {
    max-width: none;
    min-height: var(--app-height, 100dvh);
    border: none;
    border-radius: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .share-card {
    justify-content: flex-start;
    padding-top: 4rem;
  }

  .chat {
    height: var(--app-height, 100dvh);
    max-width: none;
    border: none;
    border-radius: 0;
  }

  .settings-btn {
    width: 2.25rem;
    height: 2.25rem;
  }

  .settings-btn svg {
    width: 1rem;
    height: 1rem;
  }

  /* Shrink the header's controls so the safety code, status, and End chat
     button stay on one line at typical phone widths. */
  .chat-header {
    padding: 0.5rem 0.75rem;
    padding-right: 3rem; /* clear the smaller 2.25rem gear button */
    gap: 0.5rem;
  }

  .safety {
    gap: 0.375rem;
  }

  .safety-toggle {
    width: 1.375rem;
    height: 1.375rem;
    font-size: 0.7rem;
  }

  .safety-code {
    font-size: 0.95rem;
    letter-spacing: 0.1em;
  }

  .chat-header-right {
    gap: 0.375rem;
  }

  .peer-name {
    font-size: 0.75rem;
  }

  .status {
    gap: 0.25rem;
  }

  .status-pill {
    width: 1.25rem;
    height: 1.25rem;
  }

  .status-dot {
    width: 0.5rem;
    height: 0.5rem;
  }

  .status-countdown {
    font-size: 0.68rem;
  }

  .chat-header .btn {
    padding: 0.4rem 0.6rem;
    font-size: 0.8rem;
  }
}

/* --- receipts, typing, tap-to-reveal --- */
.msg {
  cursor: pointer;
}
.delivery-label,
.msg-reveal {
  font-size: 0.6875rem;
  color: var(--muted);
}
.delivery-label {
  align-self: flex-end;
}
.msg-reveal.sent {
  align-self: flex-end;
}
.msg-reveal.received {
  align-self: flex-start;
}
.msg.typing {
  cursor: default;
}
.msg.typing .dot {
  display: inline-block;
  width: 0.375rem;
  height: 0.375rem;
  border-radius: 50%;
  background: var(--muted);
  margin-right: 0.1875rem;
  animation: typing-blink 1.2s infinite;
}
.msg.typing .dot:nth-child(2) {
  animation-delay: 0.2s;
}
.msg.typing .dot:nth-child(3) {
  animation-delay: 0.4s;
}
@keyframes typing-blink {
  0%, 60%, 100% {
    opacity: 0.3;
  }
  30% {
    opacity: 1;
  }
}
@media (prefers-reduced-motion: reduce) {
  .msg.typing .dot {
    animation: none;
    opacity: 0.6;
  }
}
