/* 主題變數。第一階段只用 pink，藍與橘的切換是第四階段。 */
[data-theme="pink"] {      /* 粉紅 + 米白 */
  --bg: #FDFBF7;  --surface: #FFFFFF;
  --bubble-me: #F5C2C7;  --bubble-ai: #FFFFFF;
  --accent: #E68A9A;
  --text: #2E2A28;  --text-dim: #8A827D;
  --border: #EDE6DE;
}

[data-theme="blue"] {      /* 嬰兒藍 + 克萊因藍 */
  --bg: #F4F8FC;  --surface: #FFFFFF;
  --bubble-me: #A8CFEA;  --bubble-ai: #FFFFFF;
  --accent: #002FA7;
  --text: #1A2230;  --text-dim: #6B7789;
  --border: #DEE7F0;
}

[data-theme="orange"] {    /* 橘色 + 米白 */
  --bg: #FDFAF5;  --surface: #FFFFFF;
  --bubble-me: #F5C396;  --bubble-ai: #FFFFFF;
  --accent: #D97534;
  --text: #2E2823;  --text-dim: #8A7F74;
  --border: #EFE5D9;
}

* { box-sizing: border-box; }

/* .app / .gate 自己設了 display，會蓋過瀏覽器對 hidden 屬性的預設樣式，
   所以 hidden 要用 !important 收回來，否則門檻會一直遮著聊天室。 */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overscroll-behavior-y: none;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 -apple-system, BlinkMacSystemFont, "PingFang TC",
        "Noto Sans TC", "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ── 進站門檻 ───────────────────────────────── */

.gate {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg);
  z-index: 10;
}

.gate-card {
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}

.gate-title {
  margin: 0;
  font-size: 28px;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: var(--accent);
}

.gate-hint {
  margin: 0 0 4px;
  font-size: 14px;
  color: var(--text-dim);
}

.gate-input,
.gate-button {
  width: 100%;
  padding: 12px 16px;
  font-size: 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
}

.gate-input {
  background: var(--surface);
  color: var(--text);
  text-align: center;
  letter-spacing: 0.1em;
}

.gate-input:focus {
  outline: none;
  border-color: var(--accent);
}

.gate-button {
  border: none;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

.gate-error {
  margin: 0;
  font-size: 13px;
  color: #C0392B;
}

/* ── 版面 ──────────────────────────────────── */

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

.header {
  flex: none;
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.header-name {
  font-size: 17px;
  font-weight: 600;
  letter-spacing: 0.05em;
}

.header-status {
  font-size: 12px;
  color: var(--text-dim);
}

.scroller {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 12px 16px;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 2px;
  max-width: 720px;
  margin: 0 auto;
}

/* ── 日期分隔線 ─────────────────────────────── */

.day-divider {
  align-self: center;
  margin: 18px 0 10px;
  padding: 3px 12px;
  font-size: 12px;
  color: var(--text-dim);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
}

/* ── 訊息 ──────────────────────────────────── */

.row {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  margin-top: 2px;
}

/* 換人說話時多一點呼吸空間 */
.row.turn-start { margin-top: 12px; }

.row.from-cenxu { justify-content: flex-start; }
.row.from-han   { justify-content: flex-end; }

.bubble {
  max-width: min(76%, 520px);
  padding: 9px 13px;
  border-radius: 18px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.from-cenxu .bubble {
  background: var(--bubble-ai);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
}

.from-han .bubble {
  background: var(--bubble-me);
  border-bottom-right-radius: 5px;
}

.meta {
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding-bottom: 2px;
  font-size: 11px;
  line-height: 1.4;
  color: var(--text-dim);
  white-space: nowrap;
}

.from-han .meta { align-items: flex-end; order: -1; }

/* 同一分鐘的連續訊息只在最後一則顯示時間 */
.meta[hidden] { display: none; }

.row.pending .bubble { opacity: 0.55; }

.row.failed .bubble {
  border: 1px solid #C0392B;
}

.row.failed .meta { color: #C0392B; }

/* ── 載入更早 ───────────────────────────────── */

.load-more {
  display: flex;
  justify-content: center;
  padding: 12px 0 4px;
}

.load-more-btn {
  padding: 6px 16px;
  font-size: 13px;
  color: var(--text-dim);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
}

.load-more-btn:disabled { opacity: 0.5; }

/* ── 輸入列 ─────────────────────────────────── */

.composer {
  flex: none;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.composer-input {
  flex: 1;
  max-height: 140px;
  padding: 9px 14px;
  font: inherit;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 18px;
  resize: none;
}

.composer-input:focus {
  outline: none;
  border-color: var(--accent);
}

.composer-send {
  flex: none;
  padding: 9px 18px;
  font: inherit;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 18px;
  cursor: pointer;
}

.composer-send:disabled {
  opacity: 0.45;
  cursor: default;
}

.empty {
  margin: 40px auto;
  font-size: 14px;
  color: var(--text-dim);
}
