:root {
  --bg: #ffffff;
  --bar: rgba(246, 246, 248, 0.92);
  --line: #e2e2e6;
  --text: #111114;
  --muted: #8a8a92;
  --them: #e9e9eb;
  --them-text: #111114;
  --me: #0a84ff;
  --me-text: #ffffff;
  --chip-border: #c9d9f5;
  --chip-text: #0a6ee0;
  --field: #ffffff;
  --field-border: #d4d4da;
  --avatar-a: #ff9f7a;
  --avatar-b: #ff5d8f;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #000000; --bar: rgba(22, 22, 24, 0.92); --line: #2a2a2e; --text: #f2f2f4; --muted: #8e8e96;
    --them: #262628; --them-text: #f2f2f4; --me: #0a84ff; --me-text: #ffffff; --chip-border: #1f3a63; --chip-text: #5aa9ff;
    --field: #1c1c1e; --field-border: #3a3a3e; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #000000; --bar: rgba(22, 22, 24, 0.92); --line: #2a2a2e; --text: #f2f2f4; --muted: #8e8e96;
  --them: #262628; --them-text: #f2f2f4; --me: #0a84ff; --me-text: #ffffff; --chip-border: #1f3a63; --chip-text: #5aa9ff;
  --field: #1c1c1e; --field-border: #3a3a3e; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 17px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  display: flex; flex-direction: column; height: 100dvh; overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.top {
  flex: none; display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: calc(10px + env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px)) 8px max(16px, env(safe-area-inset-left, 0px));
  background: var(--bar); border-bottom: 0.5px solid var(--line);
  backdrop-filter: saturate(180%) blur(20px); -webkit-backdrop-filter: saturate(180%) blur(20px);
}
.avatar {
  width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center;
  background: linear-gradient(145deg, var(--avatar-a), var(--avatar-b)); color: #fff; font-weight: 600; font-size: 20px;
}
.who { text-align: center; }
.who h1 { margin: 0; font-size: 12px; font-weight: 500; letter-spacing: 0.01em; }
.sub { margin: 0; font-size: 11px; color: var(--muted); }

.thread {
  flex: 1; overflow-y: auto; overscroll-behavior: contain; display: flex; flex-direction: column; gap: 2px;
  padding: 12px max(12px, env(safe-area-inset-right, 0px)) 12px max(12px, env(safe-area-inset-left, 0px));
  scroll-behavior: smooth;
}
.meta { margin: 6px auto 14px; max-width: 34ch; text-align: center; font-size: 11px; line-height: 1.4; color: var(--muted); text-wrap: balance; }

.msg {
  max-width: min(78%, 34rem); padding: 8px 13px; border-radius: 19px; white-space: pre-wrap; overflow-wrap: anywhere;
  animation: pop 0.18s ease-out;
}
.msg a { color: inherit; text-decoration: underline; }
.them { align-self: flex-start; background: var(--them); color: var(--them-text); }
.me { align-self: flex-end; background: var(--me); color: var(--me-text); }
/* iMessage groups a run of bubbles from one side: tighter radius between them, a tail on the last. */
.them + .them { margin-top: 0; }
.me + .them, .them + .me { margin-top: 8px; }
.them.last { border-bottom-left-radius: 5px; }
.me.last { border-bottom-right-radius: 5px; }
.delivered { align-self: flex-end; font-size: 11px; color: var(--muted); margin: 2px 4px 4px; }

.typing { align-self: flex-start; background: var(--them); border-radius: 19px; padding: 12px 14px; display: flex; gap: 4px; margin-top: 8px; }
.typing span { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); opacity: 0.4; animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }

.composer {
  flex: none; background: var(--bar); border-top: 0.5px solid var(--line);
  backdrop-filter: saturate(180%) blur(20px); -webkit-backdrop-filter: saturate(180%) blur(20px);
  padding: 8px max(10px, env(safe-area-inset-right, 0px)) calc(8px + env(safe-area-inset-bottom, 0px)) max(10px, env(safe-area-inset-left, 0px));
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.chips:not(:empty) { margin-bottom: 8px; }
.chip {
  font: inherit; font-size: 15px; padding: 7px 13px; border-radius: 18px; cursor: pointer;
  background: transparent; color: var(--chip-text); border: 1px solid var(--chip-border);
}
.chip:hover { background: color-mix(in srgb, var(--me) 8%, transparent); }
.chip:active { transform: scale(0.97); }
.chip:disabled { opacity: 0.4; cursor: default; }

.bar { display: flex; align-items: center; gap: 8px; }
.bar input {
  flex: 1; min-width: 0; font: inherit; font-size: 17px; padding: 7px 14px; border-radius: 20px;
  border: 1px solid var(--field-border); background: var(--field); color: var(--text); outline: none;
}
.bar input:focus { border-color: var(--me); }
.bar input:disabled { opacity: 0.5; }
.bar button {
  flex: none; width: 32px; height: 32px; border-radius: 50%; border: 0; display: grid; place-items: center;
  background: var(--me); color: #fff; cursor: pointer;
}
.bar button:disabled { background: var(--field-border); cursor: default; }
.restart[hidden] { display: none; }
.restart { display: block; margin: 8px auto 0; font: inherit; font-size: 14px; color: var(--chip-text); background: none; border: 0; cursor: pointer; }

@keyframes pop { from { transform: scale(0.96); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes blink { 0%, 60%, 100% { opacity: 0.35; } 30% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .msg, .typing span { animation: none; } .thread { scroll-behavior: auto; } }
