:root {
  --bg: #ffffff;
  --fg: #111111;
  --muted: #8a8a8a;
  --soft: #f2f2f2;
  --line: #e8e8e8;
  --me: #111111;
  --me-fg: #ffffff;
  --shade: rgba(0, 0, 0, .22);
  --ease: cubic-bezier(.22, .8, .24, 1);
  color-scheme: light dark;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Hiragino Sans GB",
    "Helvetica Neue", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e0e0e;
    --fg: #ececec;
    --muted: #7a7a7a;
    --soft: #1b1b1b;
    --line: #242424;
    --me: #ececec;
    --me-fg: #0e0e0e;
    --shade: rgba(0, 0, 0, .55);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  transition: background-color .3s, color .3s;
}

body { overflow: hidden; }

svg {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

button { font: inherit; color: inherit; }

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

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 14px) 20px 14px;
  border-bottom: 1px solid var(--line);
}

.status {
  display: flex;
  align-items: center;
  gap: 7px;
  margin: 0;
  font-size: 12px;
  color: var(--muted);
}

.status-text { transition: opacity .16s; }

.dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--fg);
  transition: background-color .3s, box-shadow .3s;
}

.status[data-state="connecting"] .dot { animation: pulse 1.4s ease-in-out infinite; }
.status[data-state="offline"] .dot,
.status[data-state="unreachable"] .dot { background: transparent; box-shadow: inset 0 0 0 1px var(--muted); }

.pill {
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font: 12px ui-monospace, "SF Mono", SFMono-Regular, Menlo, monospace;
  cursor: pointer;
  transition: color .2s, border-color .2s;
}

.pill:hover { color: var(--fg); border-color: var(--muted); }

/* ---------- thread ---------- */

.thread {
  flex: 1;
  overflow-y: auto;
  padding: 12px 20px 8px;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--line) transparent;
}

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  height: 100%;
  text-align: center;
  color: var(--muted);
  animation: fade .6s var(--ease) both;
}

.empty-title { margin: 0; font-size: 20px; font-weight: 600; color: var(--fg); }
.empty-sub { margin: 0; font-size: 13px; line-height: 1.8; }

.thread.has .empty { display: none; }

.messages {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  min-height: 100%;
  margin: 0;
  padding: 0;
  list-style: none;
}

.thread:not(.has) .messages { display: none; }

.divider {
  align-self: center;
  margin: 22px 0 4px;
  font-size: 11px;
  color: var(--muted);
  animation: fade .4s var(--ease) both;
}

.msg {
  display: flex;
  flex-direction: column;
  max-width: 78%;
  margin-top: 14px;
  animation: rise .34s var(--ease) both;
}

.msg.cont { margin-top: 4px; }
.msg.guest { align-self: flex-end; align-items: flex-end; }
.msg.host { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 9px 14px;
  border-radius: 18px;
  font-size: 15px;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  transition: opacity .3s var(--ease);
}

.guest .bubble { background: var(--me); color: var(--me-fg); }
.host .bubble { background: var(--soft); }

.msg.pending .bubble:not(.file) { opacity: .55; }
.msg.failed .bubble { opacity: .35; }

.note {
  margin-top: 5px;
  font-size: 11px;
  color: var(--muted);
  animation: fade .3s var(--ease) both;
}

.failed .note {
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* file bubble */

.bubble.file {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 220px;
  max-width: 100%;
  padding: 12px 16px 12px 12px;
  overflow: hidden;
  white-space: normal;
}

.file-icon {
  display: grid;
  place-items: center;
  flex: none;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: color-mix(in srgb, currentColor 12%, transparent);
}

.file-meta { display: flex; flex-direction: column; gap: 2px; min-width: 0; }

.file-name {
  overflow: hidden;
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.file-size { font-size: 12px; opacity: .6; }

.progress {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 2px;
  background: color-mix(in srgb, currentColor 14%, transparent);
  transition: opacity .5s var(--ease) .2s;
}

.progress i {
  display: block;
  height: 100%;
  background: currentColor;
  transform: scaleX(0);
  transform-origin: left;
  transition: transform .25s linear;
}

.msg:not(.pending) .progress { opacity: 0; }

/* ---------- composer ---------- */

.composer { padding: 10px 14px calc(env(safe-area-inset-bottom) + 14px); }

#form {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: var(--bg);
  transition: border-color .25s var(--ease);
}

#form:focus-within { border-color: var(--muted); }

textarea {
  flex: 1;
  min-width: 0;
  max-height: 160px;
  padding: 6px 4px;
  border: 0;
  outline: 0;
  resize: none;
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 16px;
  line-height: 22px;
}

textarea::placeholder { color: var(--muted); }

.icon-btn, .send {
  display: grid;
  place-items: center;
  flex: none;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  transition: background-color .2s, opacity .25s, transform .25s var(--ease);
}

.icon-btn { background: transparent; }
.icon-btn:hover { background: var(--soft); }
.icon-btn:disabled { opacity: .3; cursor: default; background: transparent; }

.send { background: var(--fg); color: var(--bg); }
.send:disabled { opacity: .15; cursor: default; }

.icon-btn:not(:disabled):active,
.send:not(:disabled):active { transform: scale(.9); }

/* ---------- drop overlay ---------- */

.drop {
  position: fixed;
  inset: 0;
  z-index: 20;
  padding: 16px;
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s var(--ease);
}

.drop-inner {
  display: grid;
  place-items: center;
  height: 100%;
  border: 1.5px dashed var(--line);
  border-radius: 24px;
  color: var(--muted);
  font-size: 15px;
  transition: border-color .5s var(--ease) .05s, color .5s var(--ease) .05s;
}

.drop.on { opacity: 1; }
.drop.on .drop-inner { border-color: var(--fg); color: var(--fg); }

/* ---------- toast ---------- */

.toast {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 16px);
  left: 50%;
  z-index: 30;
  max-width: calc(100% - 32px);
  padding: 9px 16px;
  border-radius: 999px;
  background: var(--fg);
  color: var(--bg);
  font-size: 13px;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -8px);
  transition: opacity .25s var(--ease), transform .25s var(--ease);
}

.toast.on { opacity: 1; transform: translate(-50%, 0); }

/* ---------- session sheet ---------- */

dialog.sheet {
  width: min(420px, calc(100% - 32px));
  margin: auto;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--bg);
  color: var(--fg);
  box-shadow: 0 24px 60px rgba(0, 0, 0, .12);
}

dialog.sheet[open] { animation: pop .3s var(--ease); }
dialog.sheet.closing { animation: pop-out .2s var(--ease) forwards; }
dialog.sheet::backdrop { background: var(--shade); animation: fade .3s var(--ease); }
dialog.sheet.closing::backdrop { animation: fade-out .2s var(--ease) forwards; }

.sheet-body { display: flex; flex-direction: column; gap: 20px; padding: 20px 22px 22px; }

.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-right: -8px; }
.sheet-head h2 { margin: 0; font-size: 16px; font-weight: 600; }

.sid {
  margin-left: 4px;
  color: var(--muted);
  font: 13px ui-monospace, "SF Mono", SFMono-Regular, Menlo, monospace;
}

.field { display: flex; flex-direction: column; gap: 8px; }
.label { font-size: 12px; color: var(--muted); }
.hint { margin: -2px 0 2px; font-size: 12px; line-height: 1.6; color: var(--muted); }

.field input {
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  outline: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 16px;
  transition: border-color .25s var(--ease);
}

.field input:focus { border-color: var(--muted); }
.field input::placeholder { color: var(--muted); }

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

.link-row code {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--soft);
  font: 12px ui-monospace, "SF Mono", SFMono-Regular, Menlo, monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--muted);
}

.btn {
  flex: none;
  padding: 9px 16px;
  border: 1px solid var(--fg);
  border-radius: 999px;
  background: var(--fg);
  color: var(--bg);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: opacity .2s, transform .25s var(--ease), background-color .2s, color .2s;
}

.btn:disabled { opacity: .2; cursor: default; }
.btn:not(:disabled):active { transform: scale(.96); }

.btn.ghost {
  align-self: flex-start;
  border-color: var(--line);
  background: transparent;
  color: var(--muted);
}

.btn.ghost:hover { color: var(--fg); border-color: var(--muted); }
.btn.ghost.armed { border-color: var(--fg); color: var(--fg); }

/* ---------- keyframes ---------- */

@keyframes rise { from { opacity: 0; transform: translateY(8px); } }
@keyframes fade { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }
@keyframes pulse { 50% { opacity: .25; } }
@keyframes pop { from { opacity: 0; transform: translateY(10px) scale(.98); } }
@keyframes pop-out { to { opacity: 0; transform: translateY(6px) scale(.98); } }
