/* Hallmark · scope: app · genre: editorial-utilitarian · tone: 教室佈告欄（樸素、可靠、有邊界感）
 * theme: custom (paper oklch(97% 0.008 85) · accent oklch(52% 0.11 250) 墨藍 · 系統字族配對)
 * states: default · hover · focus-visible · active · disabled · loading · error · success
 * 設計依據見 DESIGN.md，產品事實見 PRODUCT.md。
 * 註：教室網路可能不可靠，因此不引入任何 CDN 或外部字型，一律用系統字族。
 */

/* ---------------------------------------------------------------- tokens */
:root {
  /* 紙與墨。層次靠明度差而非邊框，這是「有設計」與「沒設計」的分界。 */
  --paper:        oklch(97% 0.008 85);
  --paper-2:      oklch(99.2% 0.004 85);
  --paper-3:      oklch(93.5% 0.01 85);
  --paper-sunk:   oklch(94.5% 0.009 85);   /* 訊息區的凹槽 */
  --ink:          oklch(24% 0.02 265);
  --ink-2:        oklch(44% 0.02 265);
  /* 這一階要同時滿足兩種底色：--paper（97%）與更暗的 --paper-sunk（94.5%）。
     只在 --paper 上量會得到 4.63 而誤判合格，實際上在凹槽底色上只有 4.28。
     已調深到即使落在凹槽上也 >= 4.5:1（見 DESIGN.md 的對比度表）。 */
  --ink-3:        oklch(51% 0.015 265);
  --line:         oklch(87% 0.01 85);
  --line-strong:  oklch(76% 0.015 85);
  --rule:         oklch(91% 0.008 85);     /* 大量重複處的髮絲線 */
  --accent:       oklch(52% 0.11 250);
  --accent-ink:   oklch(98% 0.01 250);
  --accent-soft:  oklch(93% 0.03 250);
  --accent-deep:  oklch(43% 0.11 250);
  /* 深墨面板上的字（入口頁左側） */
  /* 深墨面板（入口頁左欄、toast）。這組**刻意不隨主題反轉**：
     它代表的是「一塊深色的板子」，不是「相對於紙的墨色」。
     曾經這裡直接用 --ink 當背景，而 --ink 在深色模式會翻成近白，
     導致亮底亮字 —— 實測對比 1.03:1，標題幾乎看不見（見 docs/ui-audit.md P11）。 */
  --panel-ink:      oklch(24% 0.02 265);
  --panel-ink-2:    oklch(31% 0.02 265);
  --on-panel:       oklch(96% 0.006 85);
  --on-panel-dim:   oklch(82% 0.012 85);
  --on-panel-accent: oklch(80% 0.08 250);
  --on-ink:       oklch(96% 0.006 85);
  --on-ink-dim:   oklch(80% 0.012 85);
  --accent-on-ink: oklch(80% 0.08 250);
  --warn:         oklch(62% 0.15 55);
  /* --warn 當「文字」用時在凹槽底色上只有 3.23:1（不合格），
     所以另外給一個更深的文字版；--warn 本身留給圓點、邊框等非文字用途。 */
  --warn-text:    oklch(46% 0.13 48);
  --danger:       oklch(55% 0.19 25);
  --ok:           oklch(58% 0.12 155);

  --font-display: ui-serif, "Songti SC", "Source Han Serif SC", "Noto Serif CJK SC", Georgia, serif;
  --font-body:    system-ui, -apple-system, "PingFang SC", "Hiragino Sans GB",
                  "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  --font-mono:    ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;

  --space-3xs: 2px;
  --space-2xs: 4px;
  --space-xs:  6px;
  --space-sm:  8px;
  --space-md:  12px;
  --space-lg:  16px;
  --space-xl:  24px;
  --space-2xl: 32px;
  --space-3xl: 48px;

  --text-2xs: 11px;
  --text-xs:  12px;
  --text-sm:  13px;
  --text-md:  14px;
  --text-lg:  17px;
  --text-xl:  21px;
  --text-2xl: 27px;
  --text-3xl: 34px;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-pill: 999px;

  --dur-fast: 110ms;
  --dur-base: 180ms;
  --dur-slow: 320ms;
  --ease-out:  cubic-bezier(0.22, 0.61, 0.36, 1);
  --ease-in:   cubic-bezier(0.55, 0.06, 0.68, 0.19);
  --ease-in-out: cubic-bezier(0.45, 0.05, 0.55, 0.95);

  /* 雙層陰影：紙疊在紙上的感覺。比 1px 灰邊框有質感得多。 */
  --shadow-sm: 0 1px 1px oklch(24% 0.02 265 / 0.05), 0 1px 3px oklch(24% 0.02 265 / 0.06);
  --shadow-md: 0 1px 2px oklch(24% 0.02 265 / 0.06), 0 8px 20px oklch(24% 0.02 265 / 0.09);
  --shadow-lg: 0 2px 4px oklch(24% 0.02 265 / 0.07), 0 20px 50px oklch(24% 0.02 265 / 0.16);
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper:       oklch(21% 0.015 265);
    --paper-2:     oklch(25% 0.017 265);
    --paper-3:     oklch(29% 0.018 265);
    --paper-sunk:  oklch(18.5% 0.015 265);
    --ink:         oklch(94% 0.008 265);
    --ink-2:       oklch(79% 0.01 265);
    --ink-3:       oklch(67% 0.012 265);
    --line:        oklch(33% 0.015 265);
    --line-strong: oklch(45% 0.02 265);
    --rule:        oklch(29% 0.013 265);
    --accent:      oklch(72% 0.12 250);
    --accent-ink:  oklch(20% 0.02 250);
    --accent-soft: oklch(32% 0.05 250);
    --accent-deep: oklch(78% 0.11 250);
    /* 面板 token 在深色模式維持深底淺字（深色模式下比紙更暗一點，
       才不會與背景糊在一起）。 */
    --panel-ink:      oklch(19% 0.02 265);
    --panel-ink-2:    oklch(25% 0.02 265);
    --on-panel:       oklch(96% 0.006 85);
    --on-panel-dim:   oklch(82% 0.012 85);
    --on-panel-accent: oklch(82% 0.09 250);
    --on-ink:      oklch(95% 0.008 265);
    --on-ink-dim:  oklch(76% 0.012 265);
    --accent-on-ink: oklch(82% 0.09 250);
    --warn:        oklch(76% 0.14 60);
    --warn-text:   oklch(80% 0.13 62);
    --danger:      oklch(70% 0.16 25);
    --ok:          oklch(74% 0.13 155);
    --shadow-sm: 0 1px 3px oklch(0% 0 0 / 0.35);
    --shadow-md: 0 8px 20px oklch(0% 0 0 / 0.4);
    --shadow-lg: 0 20px 50px oklch(0% 0 0 / 0.55);
  }
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  /* 用 clip 而不是 hidden：hidden 會讓 position: sticky 失效 */
  overflow-x: clip;
}

body {
  font-family: var(--font-body);
  font-size: var(--text-md);
  line-height: 1.65;               /* 中文需要比英文更多行距 */
  color: var(--ink);
  background: var(--paper);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3 {
  font-family: var(--font-display);
  font-style: normal;              /* 標題一律不斜體（見 DESIGN.md） */
  font-weight: 600;
  margin: 0;
  overflow-wrap: anywhere;
  min-width: 0;
  letter-spacing: 0.01em;
  line-height: 1.3;
}

button, input, textarea, select { font: inherit; color: inherit; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* 全站禁止選取（防複製的第一層），輸入框與可讀內容另外放寬 */
body { user-select: none; -webkit-user-select: none; }
input, textarea { user-select: text; -webkit-user-select: text; }

/* ------------------------------------------------------------------
   hidden 屬性的救援規則。必須放在所有元件樣式之前、且用 !important。
   為什麼需要：`hidden` 只是 UA 樣式表裡的 `display: none`，優先度極低 ——
   任何 `.modal { display: grid }` 這種 class 規則都會蓋掉它，
   於是「明明是 hidden 的元素」照樣顯示。我們用 hidden 控制
   #gate / #app / #modal / #blur-cover 等一整批元素的顯示與否，
   少了這條會變成兩個畫面疊在一起、而且空白的對話框永遠蓋住畫面。
   ------------------------------------------------------------------ */
[hidden] { display: none !important; }

/* 列印：直接不給印 */
@media print {
  .app, .gate, .modal, .blur-cover { display: none !important; }
  body::after {
    content: "聊天室內容禁止列印或截圖留存。";
    font-size: 20px;
    display: block;
    padding: 40px;
  }
}

/* ---------------------------------------------------------------- 按鈕 */
button {
  font-family: var(--font-body);
  font-size: var(--text-sm);
  font-weight: 500;
  padding: var(--space-sm) var(--space-md);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--paper-2);
  color: var(--ink-2);
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease-out),
              border-color var(--dur-fast) var(--ease-out),
              color var(--dur-fast) var(--ease-out),
              box-shadow var(--dur-fast) var(--ease-out),
              transform var(--dur-fast) var(--ease-out);
  white-space: nowrap;             /* 按鈕文字不折成兩行 */
}
button:hover:not(:disabled) {
  background: var(--paper-3);
  border-color: var(--line-strong);
  color: var(--ink);
}
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: 0.45; cursor: not-allowed; }

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  box-shadow: var(--shadow-sm);
}
button.primary:hover:not(:disabled) {
  background: var(--accent-deep);
  border-color: var(--accent-deep);
  color: var(--accent-ink);
}
button.primary[data-state="loading"] { opacity: 0.72; cursor: progress; }
button.primary[data-state="success"] { background: var(--ok); border-color: var(--ok); color: var(--paper-2); }
button.primary[data-state="error"] { background: var(--danger); border-color: var(--danger); color: var(--paper-2); }

button.link {
  border: none;
  background: none;
  padding: var(--space-2xs) var(--space-3xs);
  color: var(--ink-3);
  text-decoration: underline;
  text-underline-offset: 3px;
  box-shadow: none;
}
button.link:hover { color: var(--accent); background: none; }

button.danger { border-color: var(--danger); color: var(--danger); }
button.danger:hover:not(:disabled) { background: var(--danger); color: var(--paper-2); }

/* ------------------------------------------------------------- 入口頁
   不再是一張小卡片飄在大片空白裡（那是模板反射）。改成左右兩欄的
   「佈告」：左邊是深墨公告板（這個聊天室是什麼、保證什麼），
   右邊是進場的兩個欄位。窄螢幕時上下堆疊。
   ---------------------------------------------------------------- */
.gate {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: var(--space-lg);
  background: var(--paper);
}
.gate-shell {
  width: min(880px, 100%);
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr);
  background: var(--paper-2);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}

.gate-intro {
  padding: var(--space-2xl);
  background: var(--panel-ink);
  color: var(--on-panel);
  display: flex;
  flex-direction: column;
  gap: var(--space-lg);
}
.gate-intro h1 {
  font-size: var(--text-3xl);
  color: var(--on-panel);
  letter-spacing: 0.02em;
}
.gate-lead {
  margin: 0;
  font-size: var(--text-md);
  color: var(--on-panel-dim);
  line-height: 1.7;
}
.gate-facts {
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0;
}
.gf-row {
  display: grid;
  grid-template-columns: 3.5em minmax(0, 1fr);
  gap: var(--space-md);
  align-items: baseline;
  padding: var(--space-sm) 0;
  border-top: 1px solid oklch(100% 0 0 / 0.12);
}
.gf-row dt {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  color: var(--on-panel-accent);
  letter-spacing: 0.08em;
}
.gf-row dd {
  margin: 0;
  font-size: var(--text-sm);
  color: var(--on-panel-dim);
  line-height: 1.6;
}

.gate-entry {
  padding: var(--space-2xl);
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  justify-content: center;
}
.gate-field {
  display: flex;
  flex-direction: column;
  gap: var(--space-2xs);
  font-size: var(--text-sm);
  color: var(--ink-2);
  font-weight: 500;
}
.gate-field input {
  padding: var(--space-sm) var(--space-md);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--paper);
  color: var(--ink);
  font-weight: 400;
  transition: border-color var(--dur-fast) var(--ease-out),
              box-shadow var(--dur-fast) var(--ease-out);
}
.gate-field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  outline: none;
}
.gate-submit { padding: var(--space-md); font-size: var(--text-md); }

.error { color: var(--danger); font-size: var(--text-sm); margin: 0; }
.fineprint { color: var(--ink-3); font-size: var(--text-xs); line-height: 1.6; margin: 0; }

/* ---------------------------------------------------------------- 版面 */
.app {
  display: grid;
  grid-template-columns: 264px minmax(0, 1fr);
  height: 100dvh;
  background: var(--paper);
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  padding: var(--space-lg) var(--space-md);
  background: var(--paper-2);
  border-right: 1px solid var(--line);
  min-width: 0;
  overflow-y: auto;
}
.me { display: flex; align-items: center; justify-content: space-between; gap: var(--space-sm); min-width: 0; }
/* 側邊欄的「我是誰」。JS 由 renderMe() 填值 ——
   這個元素曾經在 HTML 與 CSS 都有、但 JS 從未寫入過內容（見 docs/ui-audit.md）。 */
.me-nick {
  font-family: var(--font-display);
  font-size: var(--text-lg);
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
/* 暱稱是空的時候（例如伺服器還沒回、或 JS 又漏接），
   至少不要讓側邊欄頂端看起來像壞掉的空白。 */
.me-nick:empty::before {
  content: "（讀取中…）";
  color: var(--ink-3);
  font-family: var(--font-body);
  font-size: var(--text-sm);
  font-weight: 400;
}
.side-actions { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: var(--space-sm); }
.side-actions button { padding: var(--space-sm) var(--space-xs); }

.room-list { display: flex; flex-direction: column; gap: var(--space-3xs); flex: 1; min-height: 0; }
.room-item {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: var(--space-3xs);
  width: 100%;
  text-align: left;
  padding: var(--space-sm) var(--space-md) var(--space-sm) var(--space-lg);
  border: none;
  border-radius: var(--radius-sm);
  background: none;
  white-space: normal;
}
/* 選中狀態用左側指示條，不用外框。外框會讓整排房間看起來像一堆按鈕。 */
.room-item::before {
  content: "";
  position: absolute;
  left: 4px;
  top: 50%;
  transform: translateY(-50%) scaleY(0);
  width: 3px;
  height: 60%;
  border-radius: var(--radius-pill);
  background: var(--accent);
  transition: transform var(--dur-base) var(--ease-out);
  transform-origin: center;
}
.room-item:hover { background: var(--paper-3); }
.room-item[aria-current="true"] {
  background: var(--accent-soft);
  color: var(--accent-deep);
}
.room-item[aria-current="true"]::before { transform: translateY(-50%) scaleY(1); }
.room-item .ri-name { font-weight: 600; font-size: var(--text-md); }
.room-item .ri-meta { font-size: var(--text-2xs); color: var(--ink-3); font-family: var(--font-mono); }
.room-item[aria-current="true"] .ri-meta { color: var(--accent-deep); opacity: 0.85; }

.side-foot { border-top: 1px solid var(--rule); padding-top: var(--space-md); }
/* 2×2 小方塊，取代原本一長串擠在一起的文字 */
.privacy-state {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: var(--space-xs);
}
.ps-item {
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: var(--space-xs) var(--space-sm);
  border-radius: var(--radius-sm);
  background: var(--paper-3);
  min-width: 0;
}
/* ps-key 用 --ink-2 而非 --ink-3：實測 --ink-3 在 --paper-3 底上只有 4.17:1，
   低於 WCAG AA 對一般文字的 4.5:1（11px 的小標籤不能算「大字」）。 */
.ps-key { font-size: var(--text-2xs); color: var(--ink-2); }
.ps-val {
  font-size: var(--text-xs);
  color: var(--ink);
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------------------------------------------------------------- 聊天區 */
.chat {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--paper);
}

.chat-head {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  padding: var(--space-md) var(--space-lg);
  background: var(--paper-2);
  box-shadow: 0 1px 0 var(--rule);
  min-width: 0;
  z-index: 1;
}
.chat-title { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: 1px; overflow: hidden; }
.chat-title h2 {
  font-size: var(--text-lg);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--ink);
}
/* 這一行塞了房間號、人數、即焚設定、隱私狀態，在 320px 上會折成七行，
   把標頭撐到 173px、訊息區只剩 544px。改成單行截斷，完整內容放在
   title 屬性裡（房間設定對話框也看得到）。 */
.room-meta {
  font-size: var(--text-xs);
  color: var(--ink-3);
  font-family: var(--font-mono);
  display: block;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-tools { display: flex; gap: var(--space-xs); flex-shrink: 0; }
.chat-tools button { padding: var(--space-xs) var(--space-sm); font-size: var(--text-xs); }

.privacy-banner {
  padding: var(--space-sm) var(--space-lg);
  background: color-mix(in oklab, var(--warn) 16%, var(--paper));
  border-bottom: 1px solid color-mix(in oklab, var(--warn) 35%, transparent);
  font-size: var(--text-xs);
  color: var(--ink);
}

/* 訊息區：凹槽底色 + 內容限寬置中。
   padding-inline 用 max() 讓對話欄上限 880px 並自動置中，不必多一層 wrapper。
   為什麼要限寬：主機是 2 vCPU 的共用機器，使用者可能在 27 吋螢幕上看，
   不限寬的話訊息會貼著兩端、中間空出一大塊。 */
.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--paper-sunk);
  padding-block: var(--space-lg);
  padding-inline: max(var(--space-lg), calc((100% - 880px) / 2));
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  scrollbar-gutter: stable;
}

/* 一則訊息 */
.msg { display: flex; flex-direction: column; gap: var(--space-3xs); max-width: min(620px, 82%); }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg-other { align-self: flex-start; }

/* 同一人連續發言時收緊間距、不重複顯示暱稱（由 app.js 標記） */
.msg.is-continuation { margin-top: calc(var(--space-md) * -1 + var(--space-2xs)); }
.msg.is-continuation .msg-meta { display: none; }

.msg-meta {
  display: flex;
  align-items: baseline;
  gap: var(--space-sm);
  font-size: var(--text-2xs);
  color: var(--ink-3);
  padding-inline: var(--space-2xs);
}
.msg-nick { font-weight: 600; color: var(--ink-2); }
.msg-burn { color: var(--warn-text); font-family: var(--font-mono); }

.msg-bubble {
  position: relative;
  overflow: hidden;               /* 讓水印層不會溢出圓角 */
  padding: var(--space-sm) var(--space-md);
  border-radius: var(--radius-md);
  background: var(--paper-2);
  box-shadow: var(--shadow-sm);
  overflow-wrap: anywhere;
}
/* 對方：白紙。自己：墨藍實心。不用尾巴箭頭（過時且雜亂）。
   靠不對稱的圓角與陰影方向區分方向。 */
.msg-other .msg-bubble { border-bottom-left-radius: var(--space-2xs); }
.msg.mine .msg-bubble {
  background: var(--accent);
  color: var(--accent-ink);
  border-bottom-right-radius: var(--space-2xs);
  box-shadow: none;
}
.msg.mine .msg-bubble a { color: var(--accent-ink); }

.msg-body { margin: 0; white-space: pre-wrap; word-break: break-word; }

.msg-media { margin: var(--space-xs) 0 0; position: relative; }
.msg-media img, .msg-media video {
  display: block;
  max-width: min(320px, 100%);
  max-height: 360px;
  width: auto;
  height: auto;
  border-radius: var(--radius-sm);
  background: var(--paper-3);
  /* 透明遮罩點擊層：攔掉「長按圖片另存」這類操作 */
  -webkit-user-drag: none;
}
.msg-media { cursor: zoom-in; }
.msg-media video { cursor: default; }
.media-shield {
  position: absolute;
  inset: 0;
  border-radius: var(--radius-sm);
  background: transparent;
}
.msg-media.is-video .media-shield { pointer-events: none; }

.msg-foot {
  display: flex;
  gap: var(--space-sm);
  font-size: var(--text-2xs);
  color: var(--ink-3);
  font-family: var(--font-mono);
  padding-inline: var(--space-2xs);
}
.msg-timer { color: var(--warn-text); }

/* 系統通知：安靜的左對齊列，寬度貼合內容。
   先前是置中的膠囊，四則連續的隱私事件會變成畫面中央的一棵聖誕樹，
   把真正的對話擠到看不見。現在它讀起來像一行日誌，不像公告。
   寬度用 flex-start（不拉滿）——否則作者線會橫跨整個版面，
   右邊留一大片空白，反而像表格的一橫列。 */
.msg-system {
  align-self: flex-start;
  max-width: 100%;
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding-top: var(--space-sm);
  border-top: 1px solid var(--rule);
  font-size: var(--text-xs);
  color: var(--ink-3);
  background: none;
  border-radius: 0;
  text-align: left;
}
.msg-system::before {
  content: "";
  flex: 0 0 auto;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--line-strong);
}
.msg-system.is-alert { color: var(--ink); }
.msg-system.is-alert::before { background: var(--warn); }

/* 空狀態：房間裡一則訊息都沒有時，別讓使用者面對一片摸不著頭緒的空白 */
.msg-empty {
  margin: auto;
  text-align: center;
  max-width: 34ch;
  padding: var(--space-2xl) var(--space-lg);
  font-size: var(--text-sm);
  line-height: 1.8;
  color: var(--ink-3);
}

/* 首次載入骨架。慢網路下（校園網、行動網路）讓「還在載入」看得見 */
.skel-row { display: flex; flex-direction: column; gap: var(--space-xs); }
.skel-block {
  border-radius: var(--radius-md);
  background: linear-gradient(
    90deg,
    var(--paper-3) 0%,
    color-mix(in oklab, var(--paper-3) 60%, var(--paper-2)) 50%,
    var(--paper-3) 100%
  );
  background-size: 200% 100%;
  animation: skel-sweep 1400ms var(--ease-in-out) infinite;
}
.skel-meta { width: 96px; height: 10px; border-radius: var(--radius-pill); }
.skel-bubble { width: min(340px, 70%); height: 42px; }
.skel-right { align-self: flex-end; }
@keyframes skel-sweep {
  from { background-position: 200% 0; }
  to   { background-position: -200% 0; }
}
@media (prefers-reduced-motion: reduce) {
  .skel-block { animation: none; }
}

/* 訊息上的動態水印：暱稱 + uid 短碼 + 時間 */
.msg-wm {
  position: absolute;
  inset: -8% -12%;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  align-content: center;
  justify-items: center;
  gap: var(--space-lg);
  transform: rotate(-18deg);
  pointer-events: none;
  user-select: none;
  opacity: 0.16;
  font-family: var(--font-mono);
  font-size: var(--text-2xs);
  color: var(--ink);
  letter-spacing: 0.04em;
}
.msg.mine .msg-wm { color: var(--accent-ink); opacity: 0.3; }
.msg-wm span { white-space: nowrap; }

/* 全頁水印 */
.page-watermark {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  user-select: none;
  overflow: hidden;
  opacity: 0.035;
  font-family: var(--font-mono);
  font-size: var(--text-2xs);
}
.page-watermark span {
  position: absolute;
  white-space: nowrap;
  transform: rotate(-24deg);
}

/* 模糊罩 */
.blur-cover {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-md);
  text-align: center;
  padding: var(--space-lg);
  background: color-mix(in oklab, var(--paper) 88%, transparent);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
}
.blur-cover p { margin: 0; font-family: var(--font-display); font-size: var(--text-xl); }
.chat.is-blurred .messages,
.chat.is-blurred .composer { filter: blur(14px); }

/* ---------------------------------------------------------------- 輸入區 */
.composer {
  background: var(--paper-2);
  box-shadow: 0 -1px 0 var(--rule);
  padding: var(--space-md) var(--space-lg);
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}
.composer-row { display: flex; align-items: flex-end; gap: var(--space-sm); }

.file-btn {
  flex-shrink: 0;
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--paper);
  cursor: pointer;
  font-size: var(--text-lg);
  line-height: 1;
  color: var(--ink-2);
  transition: background var(--dur-fast) var(--ease-out),
              border-color var(--dur-fast) var(--ease-out);
}
.file-btn:hover { background: var(--paper-3); border-color: var(--line-strong); color: var(--ink); }

.composer textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  max-height: 160px;
  padding: var(--space-sm) var(--space-md);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--paper);
  line-height: 1.55;
  transition: border-color var(--dur-fast) var(--ease-out),
              box-shadow var(--dur-fast) var(--ease-out);
}
.composer textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  outline: none;
}

.composer-foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-md);
  font-size: var(--text-xs);
  color: var(--ink-3);
  min-height: 18px;
}
.composer-status { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.composer-status[data-state="error"] { color: var(--danger); }
.composer-status[data-state="success"] { color: var(--ok); }
.composer-status[data-state="loading"] { color: var(--accent); }
/* 字數只在接近上限時才出現，平時不佔版面 */
.char-count { font-family: var(--font-mono); flex-shrink: 0; margin-left: auto; }
.char-count[data-over="true"] { color: var(--danger); font-weight: 600; }

.attach-preview {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-sm);
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--paper);
  font-size: var(--text-xs);
  min-width: 0;
}
.attach-preview img, .attach-preview video { max-height: 64px; max-width: 96px; border-radius: var(--radius-sm); }
.attach-preview .ap-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.progress-track { flex: 1; height: 6px; border-radius: var(--radius-pill); background: var(--paper-3); overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--accent); transition: width var(--dur-base) var(--ease-out); }

/* ---------------------------------------------------------------- 對話框 */
.modal {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: var(--space-lg);
  background: oklch(20% 0.02 265 / 0.55);
}
.modal-card {
  width: min(470px, 100%);
  max-height: 86dvh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  background: var(--paper-2);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
}
.modal-card h3 {
  font-size: var(--text-xl);
  /* 標題區用髮絲線與內容分開，讓對話框有結構而不是一團間距 */
  padding: var(--space-lg) var(--space-xl);
  border-bottom: 1px solid var(--rule);
}
/* 對話框內容區：JS 會塞進 .field-stack 或多個 .field。
   沒有這條的話欄位之間完全沒有間距（先前確實是如此）。 */
.modal-card > :not(h3):not(.modal-foot) { padding: 0 var(--space-xl); }
.modal-card > .field-stack,
.modal-card > .member-list { padding: var(--space-lg) var(--space-xl); }
.modal-card > p { padding: var(--space-md) var(--space-xl) 0; }
.modal-card > :first-child:not(h3) { padding-top: var(--space-lg); }
.modal-card > :last-child:not(.modal-foot) { padding-bottom: var(--space-lg); }
.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-sm);
  padding: var(--space-lg) var(--space-xl);
  border-top: 1px solid var(--rule);
}
.field-stack { display: flex; flex-direction: column; gap: var(--space-md); }
.field { display: flex; flex-direction: column; gap: var(--space-2xs); font-size: var(--text-sm); color: var(--ink-2); }
.field input, .field select {
  padding: var(--space-sm) var(--space-md);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--paper);
  color: var(--ink);
}
.field input:focus, .field select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  outline: none;
}
.field .hint { font-size: var(--text-xs); color: var(--ink-3); line-height: 1.5; }
.code-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-md);
  padding: var(--space-md);
  border: 1px dashed var(--accent);
  border-radius: var(--radius-md);
  background: var(--accent-soft);
}
.code-box code {
  font-family: var(--font-mono);
  font-size: var(--text-xl);
  font-weight: 600;
  letter-spacing: 0.18em;
  color: var(--accent-deep);
  user-select: text;
  -webkit-user-select: text;
}
.member-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-sm);
  padding: var(--space-sm) 0;
  border-bottom: 1px solid var(--rule);
  font-size: var(--text-sm);
}
.member-row:last-child { border-bottom: none; }
.member-row .mr-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.checkline {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  font-size: var(--text-sm);
  color: var(--ink-2);
  cursor: pointer;
}
.checkline input { flex-shrink: 0; }

/* 圖片放大檢視 */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  background: oklch(12% 0.01 265 / 0.92);
  padding: var(--space-lg);
}
.lightbox figure { position: relative; margin: 0; max-width: 100%; max-height: 100%; }
.lightbox img, .lightbox video { max-width: min(92vw, 1000px); max-height: 82dvh; border-radius: var(--radius-md); }
.lightbox .lb-wm {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  transform: rotate(-18deg);
  pointer-events: none;
  user-select: none;
  opacity: 0.3;
  color: oklch(100% 0 0);
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  letter-spacing: 0.08em;
}
.lightbox button { position: absolute; top: var(--space-lg); right: var(--space-lg); }

/* 提示條 */
.toast-stack {
  position: fixed;
  z-index: 60;
  bottom: var(--space-lg);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  max-width: min(420px, calc(100vw - 32px));
}
.toast {
  padding: var(--space-sm) var(--space-md);
  border-radius: var(--radius-md);
  /* 用面板 token 而不是 --ink：--ink 在深色模式會翻成近白，
     提示條會變成淺色塊（語意上「深色浮出物」不該跟著主題反轉）。 */
  background: var(--panel-ink);
  color: var(--on-panel);
  font-size: var(--text-sm);
  box-shadow: var(--shadow-md);
  animation: toast-in var(--dur-base) var(--ease-out);
}
.toast.is-error { background: var(--danger); color: var(--paper-2); }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ---------------------------------------------------------------- 響應式 */
.drawer-toggle { display: none; }

@media (max-width: 768px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed;
    z-index: 30;
    inset: 0 auto 0 0;
    width: min(300px, 84vw);
    transform: translateX(-102%);
    transition: transform var(--dur-base) var(--ease-out);
    box-shadow: var(--shadow-lg);
  }
  .app.drawer-open .sidebar { transform: translateX(0); }
  .app.drawer-open::after {
    content: "";
    position: fixed;
    inset: 0;
    z-index: 20;
    background: oklch(20% 0.02 265 / 0.45);
  }
  .drawer-toggle {
    display: inline-grid;
    place-items: center;
    /* 必須高於 .sidebar 的 z-index:30，否則抽屜打開後這顆按鈕會被側邊欄蓋住，
       使用者就只能改點抽屜外的空白處才能關閉（實測過的 bug）。 */
    position: relative;
    z-index: 31;
  }
  /* 抽屜打開時 ☰ 會疊在側邊欄上方，把側邊欄內容往下推，免得壓到暱稱。 */
  .app.drawer-open .sidebar { padding-top: calc(var(--space-md) + 40px); }
  .chat-head { padding: var(--space-sm) var(--space-md); }
  .messages {
    padding-inline: var(--space-md);
    padding-block: var(--space-md);
  }
  .composer { padding: var(--space-sm) var(--space-md); }
  .msg { max-width: 94%; }
  /* 入口頁改上下堆疊：公告在上，表單在下 */
  .gate-shell { grid-template-columns: minmax(0, 1fr); }
  .gate-intro { padding: var(--space-xl); gap: var(--space-md); }
  .gate-intro h1 { font-size: var(--text-2xl); }
  .gate-entry { padding: var(--space-xl); }
  .gate { padding: var(--space-md); }
}

@media (max-width: 414px) {
  /* P14：原本是 nowrap + ellipsis，在 414/375/320 會把「有密碼／人數／
     我是房主／閱後即焚 N 秒」整段截掉。桌機可以靠 hover 的 title 補救，
     但手機才是這個產品的主要使用場景（PRODUCT.md），觸控沒有 hover。

     只是放寬成兩行還不夠：量到 320px 時三顆工具按鈕佔 162px，
     標題只剩 71px，37 字的 meta 要四行才放得下。
     所以窄螢幕改成**換行排版** —— 標題獨占第一行（可用寬度 ~237px，
     meta 兩行內可完整顯示），工具列移到第二行。
     標頭因此長到約 110px，但資訊不再消失；這比「標頭矮卻看不見房間設定」好。 */
  .chat-head { flex-wrap: wrap; }
  /* ☰ 與標題留在第一行（抽屜鈕 order -2 排最前），工具列換到第二行。 */
  .chat-head .drawer-toggle { order: -2; }
  .chat-title { flex: 1 1 0; order: -1; min-width: 0; }
  .chat-tools { flex: 1 1 100%; margin-left: 0; justify-content: flex-end; }
  .room-meta {
    white-space: normal;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    line-height: 1.4;
  }
  .msg-media img, .msg-media video { max-width: 100%; max-height: 300px; }
  .chat-tools button { padding: var(--space-xs) var(--space-sm); font-size: var(--text-xs); }
  .modal-card h3 { padding: var(--space-md) var(--space-lg); }
  .modal-card > :not(h3):not(.modal-foot) { padding: 0 var(--space-lg); }
  .modal-card > .field-stack,
  .modal-card > .member-list { padding: var(--space-md) var(--space-lg); }
  .modal-foot { padding: var(--space-md) var(--space-lg); }
  .gate-facts { font-size: var(--text-sm); }
  .gf-row { grid-template-columns: 3.2em minmax(0, 1fr); gap: var(--space-sm); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 1ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 1ms !important;
  }
}
