:root {
  --bg: #FBF6EC;
  --card: #fffef9;
  --text: #2C221E;
  --muted: #64748b;
  --border: #e2d6c4;
  --primary: #0ea5e9;
  --accent: #047857;
  --danger: #ef4444;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

/* VIP 主題：boot／core 寫入 --bg-main 等；獨立網元件多用 --bg／--card */
html[data-vip-theme] {
  --bg: var(--bg-main, #FBF6EC);
  --card: var(--card-bg, #fffef9);
  --text: var(--text-main, #2C221E);
  --muted: var(--text-muted, #64748b);
  --border: var(--border-soft, #e2d6c4);
  --primary: var(--color-brand, #0ea5e9);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Noto Sans TC", "Segoe UI", sans-serif;
}
body { padding-top: var(--safe-top); }
button, textarea, input { font: inherit; }
a { color: var(--primary); }

.tm-chat-app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.tm-chat-top {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.75rem 1rem;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}
.tm-chat-top h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 900;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tm-chat-icon-btn {
  appearance: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  text-decoration: none;
  box-sizing: border-box;
}
.tm-chat-icon-btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.tm-chat-icon-btn svg {
  width: 18px;
  height: 18px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.tm-chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.tm-chat-panel { display: none; flex: 1; flex-direction: column; min-height: 0; }
.tm-chat-panel.is-on { display: flex; }

.tm-chat-auth, .tm-chat-empty, .tm-chat-boot {
  margin: 1.25rem;
  padding: 1.5rem 1.25rem;
  text-align: center;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
}
.tm-chat-auth h2, .tm-chat-boot h2 { margin: 0 0 0.5rem; font-size: 1.15rem; }
.tm-chat-auth p, .tm-chat-empty p, .tm-chat-boot p {
  margin: 0 0 1rem;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.5;
}
.tm-chat-auth .tm-chat-btn { width: 100%; margin-bottom: 0.55rem; }
.tm-chat-temp-login {
  margin-top: 0.75rem;
  text-align: left;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.55rem 0.75rem;
  background: var(--card);
}
.tm-chat-temp-login summary {
  cursor: pointer;
  font-weight: 800;
  font-size: 0.85rem;
  color: var(--primary);
}
.tm-chat-temp-login input {
  display: block;
  width: 100%;
  margin: 0.55rem 0 0;
  padding: 0.65rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.tm-chat-temp-login .tm-chat-btn { margin-top: 0.55rem; margin-bottom: 0; }
.tm-chat-btn {
  appearance: none;
  border-radius: 999px;
  border: 2px solid var(--text);
  background: var(--text);
  color: #fff;
  font-weight: 800;
  padding: 0.7rem 1.2rem;
  cursor: pointer;
  box-shadow: 2px 2px 0 rgba(44, 34, 30, 0.25);
}
.tm-chat-btn--ghost {
  background: var(--card);
  color: var(--text);
}
.tm-chat-btn--danger {
  background: #fff;
  color: var(--danger);
  border-color: var(--danger);
}

.tm-chat-toolbar {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 1rem 0;
}
.tm-chat-toolbar .tm-chat-btn { flex: 1; font-size: 0.9rem; padding: 0.65rem 0.8rem; }

.tm-chat-rooms {
  list-style: none;
  margin: 0;
  padding: 0.75rem 1rem calc(1rem + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  overflow-y: auto;
}
.tm-chat-room {
  appearance: none;
  width: 100%;
  text-align: left;
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 14px;
  padding: 0.85rem 0.95rem;
  cursor: pointer;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.25rem 0.75rem;
}
.tm-chat-room--public {
  background: #ecfdf5;
  border-color: #a7f3d0;
}
.tm-chat-room__badge {
  display: inline-flex;
  align-items: center;
  font-size: 0.68rem;
  font-weight: 800;
  color: #047857;
  background: #d1fae5;
  border-radius: 999px;
  padding: 0.12rem 0.45rem;
  margin-left: 0.25rem;
}
.tm-chat-room__name {
  font-weight: 900;
  font-size: 0.98rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.tm-chat-room__meta {
  grid-column: 1 / -1;
  color: var(--muted);
  font-size: 0.8rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tm-chat-room__time {
  color: var(--muted);
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
}
.tm-chat-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--danger);
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.2);
  flex-shrink: 0;
}

.tm-chat-msgs {
  flex: 1;
  overflow-y: auto;
  padding: 0.85rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  background: linear-gradient(180deg, #f8fafc 0%, var(--bg) 40%);
}
.tm-chat-msg {
  max-width: min(92%, 420px);
  align-self: flex-start;
}
.tm-chat-msg.is-me { align-self: flex-end; }
.tm-chat-msg__who {
  font-size: 0.72rem;
  color: var(--muted);
  margin: 0 0 0.2rem 0.15rem;
  font-weight: 700;
}
.tm-chat-msg.is-me .tm-chat-msg__who { text-align: right; margin-right: 0.15rem; }
.tm-chat-msg__bubble {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0.65rem 0.8rem;
  line-height: 1.45;
  word-break: break-word;
  white-space: pre-wrap;
  box-shadow: 1px 1px 0 rgba(44, 34, 30, 0.08);
}
.tm-chat-msg.is-me .tm-chat-msg__bubble {
  background: #e0f2fe;
  border-color: #7dd3fc;
}
.tm-chat-msg.is-system {
  align-self: center;
  max-width: 90%;
}
.tm-chat-msg.is-system .tm-chat-msg__bubble {
  background: transparent;
  border: none;
  box-shadow: none;
  color: var(--muted);
  font-size: 0.82rem;
  text-align: center;
  padding: 0.2rem;
}
.tm-chat-msg__time {
  display: block;
  margin-top: 0.35rem;
  font-size: 0.68rem;
  color: var(--muted);
}

.tm-chat-composer {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  padding: 0.65rem 0.85rem calc(0.65rem + var(--safe-bottom));
  background: var(--card);
  border-top: 1px solid var(--border);
}
.tm-chat-composer textarea {
  flex: 1;
  min-height: 44px;
  max-height: 120px;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0.7rem 0.85rem;
  background: var(--bg);
  color: var(--text);
}
.tm-chat-composer .tm-chat-btn {
  border-radius: 14px;
  min-width: 72px;
  box-shadow: none;
}

.tm-chat-footer {
  padding: 0.5rem 1rem calc(0.75rem + var(--safe-bottom));
  text-align: center;
  font-size: 0.78rem;
  color: var(--muted);
}
.tm-chat-footer a { font-weight: 700; text-decoration: none; }

.tm-chat-toast {
  position: fixed;
  left: 50%;
  bottom: calc(1.25rem + var(--safe-bottom));
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.92);
  color: #fff;
  padding: 0.65rem 1rem;
  border-radius: 999px;
  font-size: 0.88rem;
  font-weight: 700;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.tm-chat-toast.is-on { opacity: 1; }

.tm-chat-dialog {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 90;
  padding: 1rem;
}
.tm-chat-dialog.is-on { display: flex; }
.tm-chat-dialog__card {
  width: min(100%, 360px);
  background: var(--card);
  border: 2px solid var(--text);
  border-radius: 16px;
  padding: 1.1rem;
  box-shadow: 4px 4px 0 rgba(44, 34, 30, 0.2);
}
.tm-chat-dialog__card h3 { margin: 0 0 0.5rem; }
.tm-chat-dialog__card p { margin: 0 0 0.85rem; color: var(--muted); font-size: 0.9rem; }
.tm-chat-dialog__card input {
  width: 100%;
  margin-bottom: 0.85rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.75rem;
}
.tm-chat-dialog__actions { display: flex; gap: 0.5rem; }
.tm-chat-dialog__actions .tm-chat-btn { flex: 1; }
