:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #efede6;
  --text: #1d1c1a;
  --muted: #6b675f;
  --line: #dedbd2;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #d9efec;
  --danger: #b42318;
  --danger-soft: #fde7e4;
  --warn-soft: #fff1d6;
  --shadow: 0 8px 28px rgba(30, 28, 20, .14);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14130f;
    --surface: #1d1c17;
    --surface-2: #2a2822;
    --text: #efede6;
    --muted: #a09b8f;
    --line: #38352d;
    --accent: #3fb8aa;
    --accent-ink: #06231f;
    --accent-soft: #17332f;
    --danger: #ff8a7a;
    --danger-soft: #3a1f1b;
    --warn-soft: #3a2f14;
    --shadow: 0 8px 28px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- 版面：單欄卡片頁（管理頁 / 加入頁） ---------- */
.page { min-height: 100%; display: grid; place-items: center; padding: 20px 16px; }
.card {
  width: min(440px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 26px 22px;
  box-shadow: var(--shadow);
}
.card.wide { width: min(640px, 100%); }
.card h1 { font-size: 20px; margin: 0 0 4px; letter-spacing: .2px; }
.card .sub { color: var(--muted); margin: 0 0 18px; font-size: 14px; }
.stack > * + * { margin-top: 12px; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }

label.field { display: block; font-size: 13px; color: var(--muted); }
label.field > span { display: block; margin-bottom: 4px; }
input[type=text], input[type=password], input[type=search], textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input.code { font: 600 22px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: .35em; text-align: center; }

.btn {
  border: 1px solid var(--line);
  background: var(--surface-2);
  border-radius: 10px;
  padding: 9px 14px;
  font-weight: 600;
  transition: transform .06s, background .15s;
}
.btn:hover { background: var(--line); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn.block { width: 100%; }
.link { background: none; border: 0; padding: 0; color: var(--accent); text-decoration: underline; text-underline-offset: 3px; }

.notice { padding: 10px 12px; border-radius: 10px; background: var(--warn-soft); font-size: 14px; }
.notice.error { background: var(--danger-soft); color: var(--danger); }
.notice.ok { background: var(--accent-soft); }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.tag { display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 99px; background: var(--surface-2); color: var(--muted); vertical-align: 1px; }
.tag.admin { background: var(--accent); color: var(--accent-ink); }
.tag.ok { background: var(--accent-soft); color: var(--accent); }

/* ---------- 管理頁 ---------- */
.rooms { list-style: none; margin: 0; padding: 0; }
.rooms li { border: 1px solid var(--line); border-radius: 12px; padding: 12px; background: var(--bg); }
.rooms li + li { margin-top: 10px; }
.rooms .name { font-weight: 700; }
.rooms .url { font-size: 12.5px; word-break: break-all; color: var(--muted); }

/* ---------- 持久化：QR / 金鑰 ---------- */
.qr { display: grid; place-items: center; padding: 10px; background: #fff; border-radius: 12px; border: 1px solid var(--line); }
.qr svg { width: min(220px, 70vw); height: auto; display: block; }
.secret { word-break: break-all; padding: 10px 12px; border-radius: 10px; background: var(--bg); border: 1px dashed var(--line); font-size: 15px; letter-spacing: .08em; user-select: all; }
.recovery { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 12px; border-radius: 10px; background: var(--bg); border: 1px solid var(--line); font-size: 15px; text-align: center; }

/* ---------- 聊天室 ---------- */
.chat { height: 100%; height: 100dvh; display: flex; flex-direction: column; max-width: 920px; margin: 0 auto; background: var(--surface); border-inline: 1px solid var(--line); }
.top { padding: 10px 14px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 10px; padding-top: max(10px, env(safe-area-inset-top)); }
.top h2 { margin: 0; font-size: 16px; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conn { width: 9px; height: 9px; border-radius: 50%; background: #aaa; flex: none; }
.conn.on { background: #2faa5c; }
.conn.off { background: #d9534f; }

.people { display: flex; gap: 8px; padding: 8px 14px; overflow-x: auto; border-bottom: 1px solid var(--line); scrollbar-width: thin; }
.avatar {
  flex: none; display: flex; flex-direction: column; align-items: center; gap: 2px; width: 56px;
  background: none; border: 0; padding: 2px; border-radius: 10px; font-size: 11px; color: var(--muted);
}
.avatar:hover:not(.me) { background: var(--surface-2); }
.avatar .dot {
  width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center;
  font-weight: 700; font-size: 16px; color: #fff; position: relative;
}
.avatar.me .dot { outline: 2px solid var(--accent); outline-offset: 2px; }
.avatar .dot i { position: absolute; right: -2px; bottom: -2px; width: 14px; height: 14px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); font: 700 9px/14px system-ui; text-align: center; font-style: normal; border: 2px solid var(--surface); box-sizing: content-box; }
.avatar span { max-width: 56px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.log { flex: 1; overflow-y: auto; padding: 10px 14px; overscroll-behavior: contain; }
.log .hint { text-align: center; color: var(--muted); font-size: 12.5px; padding: 8px 0; }
.msg { padding: 6px 0; max-width: 100%; }
.msg .meta { font-size: 12.5px; color: var(--muted); display: flex; gap: 6px; align-items: baseline; }
.msg .who { font-weight: 700; color: var(--text); }
.msg .body { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg img { display: block; max-width: min(320px, 100%); max-height: 320px; border-radius: 10px; margin-top: 4px; cursor: zoom-in; background: var(--surface-2); }
.msg.mine .who { color: var(--accent); }
.msg.pressing { background: var(--surface-2); border-radius: 8px; }
.msg.removing { opacity: .3; transition: opacity .2s; }

.composer { border-top: 1px solid var(--line); padding: 8px 12px; padding-bottom: max(8px, env(safe-area-inset-bottom)); display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { resize: none; max-height: 140px; min-height: 42px; line-height: 1.4; }
.composer .btn { height: 42px; flex: none; }
.composer .counter { font-size: 11px; color: var(--muted); }
.banner { padding: 8px 14px; font-size: 13.5px; background: var(--warn-soft); display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.banner.error { background: var(--danger-soft); color: var(--danger); }

/* ---------- 浮層 ---------- */
.menu { position: fixed; z-index: 50; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); padding: 5px; min-width: 170px; }
.menu button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 9px 12px; border-radius: 8px; }
.menu button:hover { background: var(--surface-2); }
.menu button.danger { color: var(--danger); }
.menu .cap { padding: 6px 12px 4px; font-size: 12px; color: var(--muted); }
.overlay { position: fixed; inset: 0; z-index: 60; background: rgba(0, 0, 0, .55); display: grid; place-items: center; padding: 16px; }
.overlay img { max-width: 100%; max-height: 100%; border-radius: 8px; }
.dialog { background: var(--surface); border-radius: 16px; padding: 20px; width: min(400px, 100%); box-shadow: var(--shadow); border: 1px solid var(--line); }
.dialog h3 { margin: 0 0 8px; }

.toasts { position: fixed; left: 0; right: 0; bottom: 84px; display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none; z-index: 70; padding: 0 12px; }
.toast { background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 10px; font-size: 14px; box-shadow: var(--shadow); max-width: 100%; }

/* ---------- 檔案傳輸 ---------- */
.xfers { padding: 0 14px; }
.xfer { display: flex; flex-direction: column; gap: 4px; padding: 8px 10px; margin: 8px 0; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); font-size: 13px; }
.xfer .bar { height: 6px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.xfer .bar > div { height: 100%; width: 0; background: var(--accent); transition: width .15s; }
