/* ==========================================================================
   iMessage-style UI. Colours live in the variables below.
   ========================================================================== */

:root {
  --max-w: 430px;

  --bg: #ffffff;
  --text: #000000;
  --muted: #8a8a8e;
  --sep: rgba(60, 60, 67, 0.22);
  --press: #e5e5ea;
  --blue: #007aff;

  --in: #e9e9eb;
  --in-text: #000000;
  --out: #0b84fe;
  --out-text: #ffffff;
  --typing-dot: #8e8e93;

  --header-bg: rgba(247, 247, 247, 0.9);
  --banner-bg: rgba(250, 250, 250, 0.88);
  --field-border: #c8c8cc;
  --placeholder: #b8b8bd;
  --plus-bg: #e9e9eb;
  --page-bg: #d9d9de;

}

:root[data-theme="dark"] {
  --bg: #000000;
  --text: #ffffff;
  --muted: #8d8d93;
  --sep: rgba(84, 84, 88, 0.6);
  --press: #1c1c1e;
  --blue: #0a84ff;
  --in: #262628;
  --in-text: #ffffff;
  --out: #0a84ff;
  --typing-dot: #8d8d93;
  --header-bg: rgba(24, 24, 26, 0.88);
  --banner-bg: rgba(48, 48, 50, 0.88);
  --field-border: #3a3a3c;
  --placeholder: #5c5c60;
  --plus-bg: #262628;
  --page-bg: #111113;
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #000000;
    --text: #ffffff;
    --muted: #8d8d93;
    --sep: rgba(84, 84, 88, 0.6);
    --press: #1c1c1e;
    --blue: #0a84ff;
    --in: #262628;
    --in-text: #ffffff;
    --out: #0a84ff;
    --typing-dot: #8d8d93;
    --header-bg: rgba(24, 24, 26, 0.88);
    --banner-bg: rgba(48, 48, 50, 0.88);
    --field-border: #3a3a3c;
    --placeholder: #5c5c60;
    --plus-bg: #262628;
    --page-bg: #111113;
  }
}

/* ---------- Base ---------- */

*, *::before, *::after { box-sizing: border-box; }

html, body {
  position: fixed;
  inset: 0;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  margin: 0;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

svg { display: block; }

#app {
  position: relative;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  overflow: hidden;
  background: var(--bg);
}

/* Desktop: same layout in a centered phone-width column.
   (Phones always use the full width, whatever width the browser reports.) */
@media (min-width: 600px) {
  body { background: var(--page-bg); }
  #app { max-width: var(--max-w); box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.06), 0 24px 70px rgba(0, 0, 0, 0.28); }
}

/* ---------- Screens & transitions ---------- */

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  transition: transform 0.4s cubic-bezier(0.32, 0.72, 0, 1);
}

#connect { display: none; z-index: 5; }
#app[data-screen="connect"] #connect { display: flex; }
#app[data-screen="connect"] #inbox,
#app[data-screen="connect"] #thread,
#app[data-screen="loading"] .screen { visibility: hidden; }

/* pan-y: the browser handles vertical scrolling; horizontal drags go to the swipe-back gesture */
#thread { z-index: 2; transform: translateX(100%); touch-action: pan-y; }
#app[data-screen="thread"] #thread { transform: none; box-shadow: -10px 0 30px rgba(0, 0, 0, 0.12); }
#app[data-screen="thread"] #inbox { transform: translateX(-30%); }

.no-anim .screen { transition: none !important; }

/* ---------- Connect screen ---------- */

#connect {
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: calc(env(safe-area-inset-top) + 24px) 28px calc(env(safe-area-inset-bottom) + 24px);
}

.app-icon {
  width: 92px;
  height: 92px;
  border-radius: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(180deg, #5ef777 0%, #0bbd2a 100%);
  box-shadow: 0 8px 24px rgba(11, 189, 42, 0.3);
}
.app-icon svg { width: 60px; height: 60px; }

.connect-title { font-size: 28px; font-weight: 700; margin: 24px 0 8px; letter-spacing: 0.01em; }
.connect-text {
  font-size: 16px;
  line-height: 1.4;
  color: var(--muted);
  max-width: 300px;
  margin: 0 0 34px;
  white-space: pre-wrap;
}
.connect-text:empty { display: none; }
.connect-btn {
  min-width: 220px;
  padding: 15px 44px;
  border-radius: 14px;
  background: var(--blue);
  color: #fff;
  font-size: 17px;
  font-weight: 600;
}
.connect-btn:active { opacity: 0.7; }

/* ---------- Avatars ---------- */

.avatar {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  overflow: hidden;
  color: #fff;
  font-weight: 600;
  background-color: #8e8e93;
  background-image: linear-gradient(180deg, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0) 100%);
}
.avatar svg, .avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar-md { width: 48px; height: 48px; font-size: 19px; }
.avatar-lg { width: 46px; height: 46px; font-size: 19px; }
.avatar-sm { width: 38px; height: 38px; font-size: 15px; }

/* ---------- Inbox ---------- */

.inbox-header {
  flex: none;
  padding: env(safe-area-inset-top) max(16px, env(safe-area-inset-right)) 4px max(16px, env(safe-area-inset-left));
}
.inbox-toolbar {
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--blue);
  font-size: 17px;
}
.tb-icon { width: 25px; height: 25px; }
.inbox-title { font-size: 34px; line-height: 41px; font-weight: 700; letter-spacing: 0.01em; margin: 0 0 6px; }

.conv-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding-bottom: env(safe-area-inset-bottom);
}

.conv {
  display: flex;
  align-items: stretch;
  width: 100%;
  text-align: left;
  padding-left: env(safe-area-inset-left);
}
.conv:active { background: var(--press); }

.conv-dot {
  flex: none;
  align-self: center;
  width: 10px;
  height: 10px;
  margin: 0 6px 0 8px;
  border-radius: 50%;
  background: var(--blue);
  visibility: hidden;
}
.conv.unread .conv-dot { visibility: visible; }
.conv .avatar { align-self: center; margin-right: 12px; }

.conv-body {
  flex: 1;
  min-width: 0;
  padding: 11px max(16px, env(safe-area-inset-right)) 11px 0;
  border-bottom: 0.5px solid var(--sep);
}
.conv:last-child .conv-body { border-bottom: 0; }

.conv-top { display: flex; align-items: baseline; gap: 8px; }
.conv-name {
  flex: 1;
  min-width: 0;
  font-size: 17px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conv-time { flex: none; display: flex; align-items: center; gap: 8px; font-size: 15px; color: var(--muted); }
.conv-time svg { width: 7px; height: 12px; opacity: 0.6; }
.conv-preview {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  margin-top: 2px;
  font-size: 15px;
  line-height: 20px;
  color: var(--muted);
}

.inbox-empty {
  position: absolute;
  top: 42%;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 17px;
  color: var(--muted);
}
.inbox-empty[hidden] { display: none; }

/* ---------- Thread header ---------- */

.thread-header {
  flex: none;
  position: relative;
  z-index: 3;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: start;
  padding: calc(env(safe-area-inset-top) + 4px) max(8px, env(safe-area-inset-right)) 6px max(8px, env(safe-area-inset-left));
  background: var(--header-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--sep);
}

.back-btn {
  justify-self: start;
  height: 44px;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 0 10px 0 6px;
  color: var(--blue);
}
.back-btn svg { width: 12px; height: 21px; }
.back-btn:active { opacity: 0.5; }
.back-badge {
  display: none;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 22px;
  padding: 0 7px;
  border-radius: 11px;
  background: var(--blue);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
}
.back-badge.show { display: inline-flex; }

.thread-title {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding-top: 2px;
  max-width: 230px;
}
.thread-name {
  display: flex;
  align-items: center;
  gap: 3px;
  font-size: 12px;
  line-height: 14px;
  max-width: 100%;
}
.thread-name > span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-name svg { flex: none; width: 5px; height: 9px; color: var(--muted); }

.hdr-icon { justify-self: end; height: 44px; display: flex; align-items: center; padding-right: 8px; color: var(--blue); }
.hdr-icon svg { width: 27px; height: 18px; }

/* ---------- Messages ---------- */

.messages {
  flex: 1;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  scrollbar-width: none;
  padding: 6px max(16px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left));
}
.messages::-webkit-scrollbar,
.conv-list::-webkit-scrollbar { display: none; }
.conv-list { scrollbar-width: none; }

.sep { text-align: center; font-size: 12px; color: var(--muted); margin: 14px 0 6px; }
.sep b { font-weight: 600; }

.row { display: flex; flex-direction: column; margin-top: 2px; }
.row.g-first { margin-top: 10px; }
.row.in { align-items: flex-start; }
.row.out { align-items: flex-end; }

.bubble {
  position: relative;
  max-width: 75%;
  padding: 7px 13px;
  border-radius: 18px;
  font-size: 17px;
  line-height: 22px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.row.in .bubble { background: var(--in); color: var(--in-text); }
.row.out .bubble { background: var(--out); color: var(--out-text); }

/* Tail on the last bubble of each group */
.row.g-last .bubble::before,
.row.g-last .bubble::after { content: ""; position: absolute; bottom: 0; height: 20px; }
.row.out.g-last .bubble::before { right: -7px; width: 20px; background: var(--out); border-bottom-left-radius: 16px 14px; }
.row.out.g-last .bubble::after { right: -26px; width: 26px; background: var(--bg); border-bottom-left-radius: 10px; }
.row.in.g-last .bubble::before { left: -7px; width: 20px; background: var(--in); border-bottom-right-radius: 16px 14px; }
.row.in.g-last .bubble::after { left: -26px; width: 26px; background: var(--bg); border-bottom-right-radius: 10px; }

/* Appear animation (only for messages that arrive while viewing) */
.row.new .bubble { animation: pop-in 0.32s cubic-bezier(0.2, 0.8, 0.3, 1.15) both; }
.row.new.in .bubble { transform-origin: 0 100%; }
.row.new.out .bubble { transform-origin: 100% 100%; }
@keyframes pop-in {
  from { opacity: 0; transform: translateY(10px) scale(0.88); }
  to { opacity: 1; transform: none; }
}

.delivered { font-size: 11px; font-weight: 500; color: var(--muted); margin: 3px 4px 0 0; }

.dbg-meta {
  font: 10px/1.3 ui-monospace, Menlo, Consolas, monospace;
  color: var(--muted);
  margin: 2px 6px 0;
  opacity: 0.85;
}

/* Typing indicator */
.typing-row { margin: 10px 0 4px 2px; }
.typing-bubble {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 36px;
  padding: 0 13px;
  border-radius: 18px;
  background: var(--in);
  animation: pop-in 0.25s ease-out both;
  transform-origin: 0 100%;
}
.typing-bubble::before,
.typing-bubble::after { content: ""; position: absolute; border-radius: 50%; background: var(--in); }
.typing-bubble::before { left: -2px; bottom: -2px; width: 12px; height: 12px; }
.typing-bubble::after { left: -7px; bottom: -7px; width: 6px; height: 6px; }
.typing-bubble span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--typing-dot);
  animation: typing 1.3s infinite ease-in-out;
}
.typing-bubble span:nth-child(2) { animation-delay: 0.18s; }
.typing-bubble span:nth-child(3) { animation-delay: 0.36s; }
@keyframes typing {
  0%, 60%, 100% { opacity: 0.35; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-2px); }
}

/* Image attachment (used for the ending) */
.row.image.g-first { margin-top: 10px; }
.row.image { margin-top: 4px; }
.att-img {
  display: block;
  width: 66%;
  max-width: 260px;
  height: auto;
  border-radius: 18px;
  background: var(--in);
  box-shadow: 0 0 0 0.5px var(--sep);
  cursor: zoom-in;
  -webkit-touch-callout: default; /* long-press → Save to Photos */
}
.row.new .att-img { animation: pop-in 0.4s cubic-bezier(0.2, 0.8, 0.3, 1.15) both; transform-origin: 0 100%; }

/* Full-screen image viewer */
.lightbox {
  display: none;
  position: absolute;
  inset: 0;
  z-index: 45;
  align-items: center;
  justify-content: center;
  padding: calc(env(safe-area-inset-top) + 50px) 0 calc(env(safe-area-inset-bottom) + 20px);
  background: #000;
  cursor: zoom-out;
}
.lightbox.show { display: flex; animation: lb-in 0.25s ease-out both; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; -webkit-touch-callout: default; }
.lightbox-done {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 12px);
  left: 16px;
  color: #0a84ff;
  font-size: 17px;
  font-weight: 600;
}
@keyframes lb-in { from { opacity: 0; } to { opacity: 1; } }

/* ---------- Composer ---------- */

.composer {
  flex: none;
  position: relative;
  z-index: 2;
  background: var(--bg);
  padding-bottom: max(8px, env(safe-area-inset-bottom));
}

.choices {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  padding: 4px max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
}
.choices:empty { display: none; }
.choices-label { font-size: 12px; color: var(--muted); margin-right: 4px; }
.choice {
  max-width: 85%;
  text-align: left;
  padding: 9px 15px;
  border-radius: 19px;
  border: 1.5px solid var(--blue);
  background: var(--bg);
  color: var(--blue);
  font-size: 16px;
  line-height: 21px;
  white-space: pre-wrap;
  animation: choice-in 0.3s cubic-bezier(0.2, 0.8, 0.3, 1.1) both;
}
.choice:active { background: var(--blue); color: #fff; }
@keyframes choice-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

.input-bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 6px max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left));
}
.plus-btn {
  flex: none;
  width: 35px;
  height: 35px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--plus-bg);
  color: var(--muted);
}
.plus-btn svg { width: 15px; height: 15px; }

.input-field {
  position: relative;
  flex: 1;
  min-width: 0;
  min-height: 36px;
  padding: 7px 42px 7px 14px;
  border: 1px solid var(--field-border);
  border-radius: 18px;
  font-size: 17px;
  line-height: 20px;
  color: var(--placeholder);
}
.input-text {
  display: block;
  max-height: 100px;
  overflow-y: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.input-field.ready { color: var(--text); }

.mic { position: absolute; right: 12px; bottom: 7px; width: 13px; height: 20px; color: var(--placeholder); }
.input-field.ready .mic { display: none; }

.send-btn {
  position: absolute;
  right: 3px;
  bottom: 3px;
  width: 29px;
  height: 29px;
  border-radius: 50%;
  display: none;
  align-items: center;
  justify-content: center;
  background: var(--blue);
  color: #fff;
}
.send-btn svg { width: 15px; height: 15px; }
.send-btn::before { content: ""; position: absolute; inset: -10px; } /* bigger tap target */
.input-field.ready .send-btn { display: flex; animation: send-pulse 2s ease-in-out infinite; }
.send-btn:active { opacity: 0.6; }
@keyframes send-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(10, 132, 255, 0.45); }
  50% { box-shadow: 0 0 0 7px rgba(10, 132, 255, 0); }
}

/* ---------- Notification banner ---------- */

.banner {
  position: absolute;
  z-index: 20;
  top: calc(env(safe-area-inset-top) + 8px);
  left: 8px;
  right: 8px;
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 12px 14px;
  border-radius: 22px;
  background: var(--banner-bg);
  -webkit-backdrop-filter: saturate(180%) blur(24px);
  backdrop-filter: saturate(180%) blur(24px);
  box-shadow: 0 10px 34px rgba(0, 0, 0, 0.22);
  cursor: pointer;
  transform: translateY(calc(-100% - 80px));
  transition: transform 0.45s cubic-bezier(0.32, 0.72, 0, 1);
}
.banner.show { transform: none; }
.banner-avatar { position: relative; flex: none; }
.banner-app {
  position: absolute;
  right: -4px;
  bottom: -4px;
  width: 18px;
  height: 18px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(180deg, #5ef777, #0bbd2a);
  box-shadow: 0 0 0 1.5px var(--banner-bg);
}
.banner-app svg { width: 13px; height: 13px; }
.banner-body { flex: 1; min-width: 0; }
.banner-top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.banner-name { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.banner-time { flex: none; font-size: 13px; color: var(--muted); }
.banner-text {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-size: 15px;
  line-height: 20px;
}

/* ---------- Ended state ---------- */

.game-ended .input-field { opacity: 0.6; }

/* ---------- Error screen ---------- */

.error-screen {
  display: none;
  position: absolute;
  inset: 0;
  z-index: 50;
  padding: calc(env(safe-area-inset-top) + 40px) 28px 28px;
  background: var(--bg);
  font-size: 15px;
  line-height: 1.5;
  -webkit-user-select: text;
  user-select: text;
}
.error-screen.show { display: block; }
.error-screen h2 { margin: 0 0 12px; font-size: 22px; }
.error-screen code { font: 13px ui-monospace, Menlo, monospace; background: var(--press); padding: 1px 5px; border-radius: 4px; }

/* ---------- Debug panel (?debug=1) ---------- */

.dbg-btn {
  position: absolute;
  z-index: 30;
  left: 6px;
  bottom: calc(max(8px, env(safe-area-inset-bottom)) + 52px);
  padding: 4px 8px;
  border-radius: 10px;
  background: rgba(255, 59, 48, 0.9);
  color: #fff;
  font: 600 11px/1.2 ui-monospace, Menlo, monospace;
  opacity: 0.85;
}
.dbg-panel {
  display: none;
  position: absolute;
  z-index: 40;
  inset: 0;
  overflow-y: auto;
  padding: calc(env(safe-area-inset-top) + 12px) 14px calc(env(safe-area-inset-bottom) + 16px);
  background: rgba(0, 0, 0, 0.92);
  color: #e5e5e5;
  font: 12px/1.45 ui-monospace, Menlo, Consolas, monospace;
  -webkit-user-select: text;
  user-select: text;
}
.dbg-panel.show { display: block; }
.dbg-panel h3 { margin: 14px 0 4px; font-size: 12px; color: #ffd27a; text-transform: uppercase; letter-spacing: 0.06em; }
.dbg-panel .dbg-warn { color: #ff9f0a; }
.dbg-panel .dbg-ok { color: #30d158; }
.dbg-panel pre { margin: 0; white-space: pre-wrap; word-break: break-word; font: inherit; }
.dbg-actions { display: flex; gap: 8px; position: sticky; top: 0; }
.dbg-actions button { padding: 6px 12px; border-radius: 8px; background: #2c2c2e; color: #fff; font: inherit; }
.dbg-actions button.danger { background: #a1261e; }
