:root {
  --bg: #fbf6ef;
  --surface: #ffffff;
  --chat-bg: #f4ebdf;
  --ink: #2b211b;
  --muted: #6d5f55;
  --line: #e4d8ca;
  --brand: #9c4221;
  --brand-ink: #ffffff;
  --mine: #f8d9c4;
  --theirs: #ffffff;
  --read: #1c5fa8;
  --danger: #b3261e;
  --font-scale: 1;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1d1815;
    --surface: #2a231f;
    --chat-bg: #17130f;
    --ink: #f4ece4;
    --muted: #b8a99c;
    --line: #40362f;
    --brand: #ad5029;
    --mine: #5c3320;
    --theirs: #2e2722;
    --read: #7db8f5;
    --danger: #f2877f;
    color-scheme: dark;
  }

  :root:not([data-theme="light"]) .bubble .sender { filter: brightness(1.8); }
}

* { box-sizing: border-box; }

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

body {
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: calc(19px * var(--font-scale));
  line-height: 1.4;
  overscroll-behavior-y: none;
}

#app {
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

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

button { cursor: pointer; }

:focus-visible { outline: 3px solid var(--read); outline-offset: 2px; }

.topbar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 64px;
  padding: max(8px, env(safe-area-inset-top)) 12px 8px;
  background: var(--brand);
  color: var(--brand-ink);
}

.topbar h1 {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 1.15em;
  font-weight: 650;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.icon-btn {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 52px;
  height: 52px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: inherit;
  text-decoration: none;
}

.icon-btn:active { background: rgb(127 127 127 / 0.25); }

.icon-btn svg { width: 28px; height: 28px; fill: currentColor; }

.scroll { flex: 1; min-height: 0; overflow-y: auto; }

.avatar {
  flex: none;
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  color: #fff;
  font-weight: 700;
  font-size: 22px;
}

.avatar svg { width: 30px; height: 30px; fill: currentColor; }

.chat-row {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 84px;
  padding: 12px 16px;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: none;
  text-align: left;
}

.chat-row:active { background: var(--line); }

.chat-row .body { flex: 1; min-width: 0; }

.chat-row .line { display: flex; align-items: baseline; gap: 8px; }

.chat-row .name {
  flex: 1;
  min-width: 0;
  font-weight: 650;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.chat-row .time { flex: none; color: var(--muted); font-size: 0.8em; }

.chat-row .preview {
  flex: 1;
  min-width: 0;
  color: var(--muted);
  font-size: 0.92em;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.badge {
  flex: none;
  min-width: 28px;
  height: 28px;
  padding: 0 8px;
  border-radius: 14px;
  background: var(--brand);
  color: var(--brand-ink);
  font-size: 0.85em;
  font-weight: 700;
  line-height: 28px;
  text-align: center;
}

.banner {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 12px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
}

.messages {
  background: var(--chat-bg);
  padding: 10px 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.messages > :first-child { margin-top: auto; }

.day {
  align-self: center;
  margin: 10px 0 6px;
  padding: 3px 12px;
  border-radius: 10px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.8em;
}

.bubble {
  position: relative;
  max-width: 85%;
  padding: 7px 11px 5px;
  border-radius: 12px;
  background: var(--theirs);
  align-self: flex-start;
  overflow-wrap: anywhere;
  box-shadow: 0 1px 1px rgb(0 0 0 / 0.1);
}

.bubble.mine { background: var(--mine); align-self: flex-end; }

.bubble.failed { outline: 2px solid var(--danger); }

.bubble .sender { display: block; font-size: 0.82em; font-weight: 700; }

.bubble .text { white-space: pre-wrap; }

.bubble img {
  display: block;
  width: 100%;
  max-width: 340px;
  max-height: 420px;
  min-height: 120px;
  object-fit: cover;
  border-radius: 8px;
  background: var(--line);
}

.bubble .photo-btn { display: block; padding: 0; border: 0; background: none; width: 100%; }

.bubble audio { display: block; width: min(270px, 62vw); height: 48px; }

.meta {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 4px;
  margin-top: 1px;
  color: var(--muted);
  font-size: 0.72em;
}

.meta svg { width: 18px; height: 18px; fill: currentColor; }

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

.retry {
  align-self: flex-end;
  border: 0;
  background: none;
  color: var(--danger);
  font-size: 0.85em;
  font-weight: 650;
  padding: 2px 4px 8px;
  text-decoration: underline;
}

.composer {
  flex: none;
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 8px 8px max(8px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.composer textarea {
  flex: 1;
  min-width: 0;
  min-height: 52px;
  max-height: 160px;
  padding: 12px 16px;
  border: 1px solid var(--line);
  border-radius: 26px;
  background: var(--bg);
  resize: none;
}

.composer .primary-round { background: var(--brand); color: var(--brand-ink); }

.composer .side { color: var(--brand); }

.recording {
  flex: 1;
  min-height: 52px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 650;
}

.recording .dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--danger);
  animation: pulse 1.2s infinite;
}

@keyframes pulse { 50% { opacity: 0.25; } }

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

.page { padding: 20px 16px 40px; display: flex; flex-direction: column; gap: 14px; }

.page h2 { margin: 18px 0 0; font-size: 1.05em; }

.page p { margin: 0; }

.muted { color: var(--muted); font-size: 0.92em; }

.hero { align-items: center; text-align: center; padding-top: 40px; }

.hero img { width: 96px; height: 96px; border-radius: 22px; }

.hero h1 { margin: 0; font-size: 1.5em; }

.field { display: flex; flex-direction: column; gap: 6px; width: 100%; text-align: left; font-weight: 600; }

.field input {
  width: 100%;
  min-height: 56px;
  padding: 10px 14px;
  border: 1px solid var(--muted);
  border-radius: 12px;
  background: var(--surface);
  font-weight: 400;
}

.field input.code { font-size: 1.3em; letter-spacing: 0.12em; text-align: center; text-transform: uppercase; }

.btn {
  width: 100%;
  min-height: 56px;
  padding: 10px 18px;
  border: 1px solid var(--brand);
  border-radius: 14px;
  background: var(--surface);
  color: var(--brand);
  font-weight: 650;
  text-decoration: none;
  text-align: center;
}

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

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

.btn:disabled { opacity: 0.55; }

.error { color: var(--danger); font-weight: 600; }

.card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
}

.card-head { display: flex; align-items: center; gap: 12px; }

.card-head .name { font-weight: 650; }

.code-display {
  padding: 12px;
  border-radius: 10px;
  background: var(--bg);
  font-size: 1.3em;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-align: center;
  user-select: all;
}

.size-row { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }

.size-row .btn { padding: 8px 2px; font-size: 0.85em; }

.size-row .btn[aria-pressed="true"] { background: var(--brand); color: var(--brand-ink); }

.steps { margin: 0; padding-left: 1.3em; display: flex; flex-direction: column; gap: 8px; }

.viewer {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 0;
  padding: 0;
  background: #000;
  width: 100%;
}

.viewer img { max-width: 100%; max-height: 100%; }

.toast {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: 90px;
  z-index: 20;
  max-width: 480px;
  margin: 0 auto;
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--ink);
  color: var(--bg);
  text-align: center;
}
