:root {
  --line-green: #06C755;
  --line-green-dark: #04a647;
  --sent-bubble: #84E063;
  --recv-bubble: #ffffff;
  --chat-bg: #8AABD3;
  --time-color: #ffffffcc;
  --header-bg: #ffffff;
  --header-fg: #1f1f1f;
  --shadow: 0 1px 1px rgba(0,0,0,0.08);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: 'Noto Sans Thai', -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: #1f1f1f;
  color: #1f1f1f;
  overscroll-behavior-y: none;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.phone {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  background: var(--chat-bg);
  position: relative;
  box-shadow: 0 0 24px rgba(0,0,0,0.25);
}

/* ───── header ───── */
.chat-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--header-bg);
  color: var(--header-fg);
  border-bottom: 1px solid #e5e5e5;
  position: sticky;
  top: 0;
  z-index: 5;
}
.chat-header .back {
  background: none;
  border: 0;
  font-size: 28px;
  line-height: 1;
  color: var(--line-green);
  padding: 0 4px;
  cursor: pointer;
}
.chat-header .group-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  background: #f2f2f2;
  flex: 0 0 36px;
}
.chat-header .title { flex: 1; min-width: 0; }
.chat-header .name {
  font-weight: 700;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-header .members {
  font-size: 12px;
  color: #777;
}
.chat-header .header-icons {
  display: flex;
  gap: 6px;
  align-items: center;
  color: #555;
}
.chat-header .icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 0;
  background: none;
  color: #555;
  border-radius: 50%;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease, transform 80ms ease;
  -webkit-tap-highlight-color: transparent;
}
.chat-header .icon-btn:hover { background: #f0f0f0; color: #1f1f1f; }
.chat-header .icon-btn:active { transform: scale(0.92); }
.chat-header .icon-btn svg {
  width: 22px;
  height: 22px;
  display: block;
}
.chat-header .icon-btn .ico-play { display: none; }
.chat-header .icon-btn[data-state="paused"] {
  color: var(--line-green);
}
.chat-header .icon-btn[data-state="paused"] .ico-pause { display: none; }
.chat-header .icon-btn[data-state="paused"] .ico-play  { display: block; }

/* ───── chat scroll area ───── */
.chat {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 14px 12px 24px;
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* day separator */
.daysep {
  align-self: center;
  background: rgba(0,0,0,0.18);
  color: #fff;
  font-size: 12px;
  padding: 3px 12px;
  border-radius: 12px;
  margin: 6px 0 10px;
}

/* ───── message rows ───── */
.row {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  max-width: 100%;
  animation: pop-in 220ms cubic-bezier(0.18, 0.89, 0.32, 1.28) both;
}
.row.sent { justify-content: flex-end; }
.row.recv { justify-content: flex-start; }

@keyframes pop-in {
  from { opacity: 0; transform: translateY(8px) scale(0.96); }
  to   { opacity: 1; transform: translateY(0)  scale(1); }
}

/* avatar */
.avatar {
  flex: 0 0 36px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  user-select: none;
  box-shadow: var(--shadow);
}
.avatar.spacer { background: transparent; box-shadow: none; }

/* avatar palette (kept in CSS so CSP can forbid inline styles) */
.avatar.avatar-c0  { background: #F87171; }
.avatar.avatar-c1  { background: #FB923C; }
.avatar.avatar-c2  { background: #FBBF24; }
.avatar.avatar-c3  { background: #A3E635; }
.avatar.avatar-c4  { background: #34D399; }
.avatar.avatar-c5  { background: #22D3EE; }
.avatar.avatar-c6  { background: #60A5FA; }
.avatar.avatar-c7  { background: #A78BFA; }
.avatar.avatar-c8  { background: #F472B6; }
.avatar.avatar-c9  { background: #FB7185; }
.avatar.avatar-c10 { background: #10B981; }
.avatar.avatar-c11 { background: #0EA5E9; }
.avatar.avatar-c12 { background: #8B5CF6; }
.avatar.avatar-c13 { background: #EC4899; }
.avatar.avatar-c14 { background: #F59E0B; }

/* bubble cluster (name + bubble + time) */
.cluster {
  display: flex;
  flex-direction: column;
  max-width: 78%;
  min-width: 0;
}
.row.sent .cluster { align-items: flex-end; }
.row.recv .cluster { align-items: flex-start; }

.sender-name {
  font-size: 12px;
  color: #fff;
  margin: 0 8px 2px;
  text-shadow: 0 1px 1px rgba(0,0,0,0.15);
}

.bubble-line {
  display: flex;
  align-items: flex-end;
  gap: 4px;
}
.row.sent .bubble-line { flex-direction: row-reverse; }

.bubble {
  padding: 8px 12px;
  border-radius: 18px;
  font-size: 15px;
  line-height: 1.45;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  box-shadow: var(--shadow);
  max-width: 100%;
}
.bubble.sent {
  background: var(--sent-bubble);
  color: #1b1b1b;
  border-bottom-right-radius: 6px;
}
.bubble.recv {
  background: var(--recv-bubble);
  color: #1b1b1b;
  border-bottom-left-radius: 6px;
}

.time {
  font-size: 10px;
  color: var(--time-color);
  white-space: nowrap;
  text-shadow: 0 1px 1px rgba(0,0,0,0.15);
}

/* typing indicator */
.typing .bubble {
  padding: 12px 14px;
  display: inline-flex;
  gap: 4px;
  align-items: center;
  height: 32px;
}
.typing .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #b6b6b6;
  animation: blink 1.2s infinite ease-in-out;
}
.typing .dot:nth-child(2) { animation-delay: 0.18s; }
.typing .dot:nth-child(3) { animation-delay: 0.36s; }
@keyframes blink {
  0%, 80%, 100% { opacity: 0.35; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

/* ───── start overlay ───── */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  transition: opacity 280ms ease;
}
.overlay.hidden {
  opacity: 0;
  pointer-events: none;
}
.overlay-card {
  background: #fff;
  padding: 28px 32px;
  border-radius: 18px;
  text-align: center;
  max-width: 320px;
  width: 80%;
  box-shadow: 0 12px 40px rgba(0,0,0,0.4);
}
.overlay-emoji { font-size: 44px; }
.overlay-logo {
  width: 88px;
  height: 88px;
  border-radius: 22px;
  object-fit: cover;
  display: block;
  margin: 0 auto 4px;
  box-shadow: 0 6px 16px rgba(0,0,0,0.15);
}
.overlay-title { font-size: 20px; font-weight: 700; margin-top: 6px; }
.overlay-sub { font-size: 14px; color: #777; margin: 6px 0 18px; }
.start-btn {
  background: var(--line-green);
  color: #fff;
  border: 0;
  border-radius: 999px;
  padding: 10px 32px;
  font-size: 15px;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  transition: background 150ms ease, transform 80ms ease;
}
.start-btn:hover { background: var(--line-green-dark); }
.start-btn:active { transform: scale(0.97); }

/* ───── small screens ───── */
@media (max-width: 380px) {
  .bubble { font-size: 14px; }
  .avatar { flex-basis: 32px; width: 32px; height: 32px; font-size: 13px; }
  .cluster { max-width: 80%; }
}
