:root {
  --paper: #eef1f4;
  --surface: #ffffff;
  --ink: #1b2430;
  --muted: #677385;
  --line: #d5dce3;
  --danger: #b4432f;
  --focus: #2f7fb8;
  --font: 'PingFang SC', 'HarmonyOS Sans SC', 'Noto Sans SC', 'Microsoft YaHei', system-ui, -apple-system, sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #161a20;
    --surface: #1f252d;
    --ink: #e6eaf0;
    --muted: #95a0ae;
    --line: #2e3640;
    --danger: #e0735c;
    --focus: #6fb0e0;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.6 var(--font);
  -webkit-font-smoothing: antialiased;
  padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
}
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---------- 视图切换 ---------- */
.screen, .chat, .home { display: none; }
body[data-view='loading'] .screen,
body[data-view='screen'] .screen,
body[data-view='home'] .home { display: flex; }
body[data-view='chat'] .chat { display: flex; }

/* ---------- 按钮 ---------- */
.btn {
  appearance: none;
  border: 1px solid var(--ink);
  background: var(--ink);
  color: var(--paper);
  font: inherit;
  font-size: 15px;
  padding: 8px 18px;
  border-radius: 999px;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { opacity: 0.88; }
.btn.small { font-size: 13px; padding: 4px 12px; }
.btn.ghost { background: transparent; color: var(--ink); border-color: var(--line); }
.btn.ghost:hover { border-color: var(--ink); opacity: 1; }
.btn.danger { background: transparent; color: var(--danger); border-color: currentColor; }
.btn[aria-pressed='true'] { border-color: var(--ink); }

/* ---------- 状态页 ---------- */
.screen {
  min-height: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 32px 24px;
}
.screen h1 { font-size: 28px; font-weight: 600; margin: 24px 0 8px; letter-spacing: 0.02em; }
.screen p { color: var(--muted); margin: 0 0 24px; max-width: 26em; }
.screen-actions { display: flex; gap: 12px; }
.screen-dots { display: flex; }
.screen-dots i { width: 44px; height: 44px; border-radius: 50%; margin-left: -12px; border: 3px solid var(--paper); }
.screen-dots i:nth-child(1) { background: #d9643a; margin-left: 0; }
.screen-dots i:nth-child(2) { background: #2f7fb8; }
.screen-dots i:nth-child(3) { background: #3f9b6b; }
body[data-view='loading'] .screen-dots i { animation: bob 1.2s ease-in-out infinite; }
body[data-view='loading'] .screen-dots i:nth-child(2) { animation-delay: 0.15s; }
body[data-view='loading'] .screen-dots i:nth-child(3) { animation-delay: 0.3s; }
@keyframes bob { 50% { transform: translateY(-8px); } }
@media (prefers-reduced-motion: reduce) { .screen-dots i { animation: none !important; } }

/* ---------- 聊天 ---------- */
.chat { height: 100%; flex-direction: column; max-width: 860px; margin: 0 auto; }

.bar {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: 'room controls' 'members members';
  gap: 12px 16px;
  padding: 16px 20px 14px;
  border-bottom: 1px solid var(--line);
}
.room { grid-area: room; display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.room-label { color: var(--muted); font-size: 13px; }
.room-code {
  font-size: 30px;
  font-weight: 600;
  letter-spacing: 0.12em;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}
.chip {
  font-size: 12px;
  padding: 1px 8px;
  border-radius: 999px;
  border: 1px solid var(--ink);
  align-self: center;
}
.room .btn { align-self: center; }
.controls { grid-area: controls; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.members { grid-area: members; display: flex; align-items: center; min-height: 36px; }
.members .dot { margin-left: -8px; border: 3px solid var(--paper); }
.members .dot:first-child { margin-left: 0; }
.members .dot.self { outline: 2px solid var(--ink); outline-offset: 1px; z-index: 1; }
.members .count { margin-left: 12px; color: var(--muted); font-size: 13px; }

.dot {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  font-size: 16px;
  line-height: 1;
  font-family: 'Apple Color Emoji', 'Segoe UI Emoji', 'Noto Color Emoji', 'Twemoji Mozilla', var(--font);
  user-select: none;
  position: relative;
}
.dot.lg { width: 36px; height: 36px; font-size: 19px; }
.members .dot.pending { opacity: 0.45; outline: 2px dashed var(--muted); outline-offset: 1px; }

.banner {
  margin: 12px 20px 0;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px dashed var(--muted);
  color: var(--muted);
  font-size: 14px;
}

.log { flex: 1; overflow-y: auto; padding: 16px 20px 8px; overscroll-behavior: contain; }
.sys { text-align: center; color: var(--muted); font-size: 13px; margin: 10px 0; }

.msg { display: flex; gap: 10px; margin-top: 14px; align-items: flex-start; }
.msg .body { max-width: min(78%, 34em); display: flex; flex-direction: column; align-items: flex-start; }
.msg .name { font-size: 12px; margin-bottom: 3px; }
/* 名字颜色向正文色靠一点，浅色头像的名字在浅背景上也看得清 */
.msg .name { color: color-mix(in oklch, var(--who) 72%, var(--ink)); font-weight: 600; }
.msg .text {
  margin: 0;
  padding: 8px 12px;
  border-radius: 4px 14px 14px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.cont { margin-top: 4px; }
.msg.cont .dot { visibility: hidden; height: 0; }
.msg.cont .name { display: none; }

.msg.mine { flex-direction: row-reverse; }
.msg.mine .body { align-items: flex-end; }
.msg.mine .text { background: var(--who); border-color: var(--who); color: var(--who-ink, #fff); border-radius: 14px 4px 14px 14px; }

.composer { display: flex; gap: 10px; align-items: flex-end; padding: 12px 20px 4px; border-top: 1px solid var(--line); }
.composer textarea {
  flex: 1;
  resize: none;
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 8px 14px;
  max-height: 160px;
  line-height: 1.5;
}
.composer textarea:focus { border-color: var(--ink); outline: none; }
.hint { margin: 4px 20px 12px; color: var(--muted); font-size: 12px; min-height: 1.2em; }

@media (max-width: 600px) {
  .bar { grid-template-columns: 1fr; grid-template-areas: 'room' 'members' 'controls'; padding: 12px 14px; }
  .controls { justify-content: flex-start; }
  .room-code { font-size: 26px; }
  .log { padding: 12px 14px 8px; }
  .composer { padding: 10px 14px 4px; }
  .hint { margin: 4px 14px 10px; }
  .msg .body { max-width: 84%; }
}

/* ---------- 管理后台 ---------- */
.admin { max-width: 960px; margin: 0 auto; padding: 32px 20px 60px; }
.admin h1 { font-size: 24px; font-weight: 600; margin: 0; }
.admin-head { display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap; margin-bottom: 28px; }
.admin-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.admin input[type='password'] {
  font: inherit; color: inherit; background: var(--surface);
  border: 1px solid var(--line); border-radius: 999px; padding: 8px 16px; min-width: 260px;
}
.site-state { display: flex; align-items: center; gap: 16px; padding: 18px 20px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); margin-bottom: 12px; }
.site-state + section, .site-state + .site-state + section { margin-top: 28px; }
.site-state .lamp { width: 14px; height: 14px; border-radius: 50%; background: var(--muted); flex: none; }
.site-state.open .lamp { background: #3f9b6b; }
.site-state strong { font-size: 18px; font-weight: 600; }
.site-state p { margin: 0; color: var(--muted); font-size: 14px; }
.site-state .grow { flex: 1; }
.rooms { width: 100%; border-collapse: collapse; font-size: 14px; }
.rooms th { text-align: left; font-weight: 500; color: var(--muted); padding: 8px 10px; border-bottom: 1px solid var(--line); }
.rooms td { padding: 12px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.rooms .code { font-size: 18px; font-weight: 600; letter-spacing: 0.1em; }
.rooms .who { display: flex; flex-direction: column; gap: 4px; }
.rooms .who span { display: block; color: var(--muted); font-size: 12px; }
.rooms .actions { white-space: nowrap; text-align: right; }
.rooms .actions .btn + .btn { margin-left: 8px; }
.empty { color: var(--muted); padding: 24px 0; }
.err { color: var(--danger); font-size: 14px; }

.btn:disabled { opacity: 0.5; cursor: default; }

/* ---------- 首页 ---------- */
.home {
  min-height: 100%;
  flex-direction: column;
  justify-content: center;
  max-width: 480px;
  margin: 0 auto;
  padding: 40px 24px;
}
.home h1 { font-size: 30px; font-weight: 600; margin: 0 0 10px; letter-spacing: 0.02em; }
.home-lead { color: var(--muted); margin: 0 0 28px; }
.home-code { display: flex; gap: 12px; align-items: center; }
.home-code input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 40px;
  font-weight: 600;
  letter-spacing: 0.14em;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px 18px;
  text-transform: lowercase;
}
.home-code input:focus { border-color: var(--ink); outline: none; }
.home-code input::placeholder { color: var(--line); }
body.join-only #home-shuffle { display: none; }
.home-go { margin-top: 16px; font-size: 17px; padding: 12px 18px; width: 100%; }
.home .err { min-height: 1.4em; margin: 10px 0 0; }
.home-me { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; margin-top: 18px; }
.home-note { color: var(--muted); font-size: 13px; margin: 28px 0 0; padding-top: 16px; border-top: 1px solid var(--line); }
@media (max-width: 600px) { .home-code input { font-size: 32px; } }

/* ---------- 消息附加 ---------- */
.msg .text a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
/* 气泡内右下角的时间：浮在最后一行右侧，放不下时自动换到下一行 */
.msg .text .stamp {
  float: right;
  margin: 0.55em 0 -0.3em 0.9em;
  font-size: 11px;
  line-height: 1;
  opacity: 0.6;
  font-variant-numeric: tabular-nums;
  user-select: none;
}
.media-info .stamp { flex: none; font-variant-numeric: tabular-nums; }

/* ---------- 媒体 ---------- */
.media {
  position: relative;
  width: min(320px, 100%);
  max-height: 360px;
  border-radius: 12px;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--line);
  display: flex;
  flex-direction: column;
}
.media.audio { aspect-ratio: auto !important; max-height: none; }
.media img, .media video { display: block; width: 100%; height: 100%; min-height: 0; flex: 1; object-fit: cover; background: #000; }
.media.image img:not(.thumb) { cursor: zoom-in; object-fit: contain; }
.media img.thumb { filter: blur(1px) brightness(0.85); }
.media audio { width: calc(100% - 16px); margin: 10px 8px 0; }
.media-icon { font-size: 28px; text-align: center; padding: 14px 0 0; color: var(--muted); }
.media-load {
  position: absolute; left: 50%; top: 42%; transform: translate(-50%, -50%);
  appearance: none; border: 0; border-radius: 999px; padding: 8px 18px; font: inherit; font-size: 14px;
  background: rgba(0, 0, 0, 0.62); color: #fff; cursor: pointer;
}
.media.audio .media-load { position: static; transform: none; margin: 10px auto 0; background: var(--ink); color: var(--paper); }
.media .bar { position: absolute; left: 12px; right: 12px; top: 42%; height: 6px; border-radius: 3px; background: rgba(0, 0, 0, 0.45); overflow: hidden; }
.media.audio .bar { position: static; margin: 12px 12px 0; }
.media .bar i { display: block; height: 100%; background: #fff; transition: width 0.2s; }
.media.audio .bar i { background: var(--ink); }
.media-info {
  display: flex; gap: 8px; align-items: baseline; padding: 6px 10px; font-size: 12px; color: var(--muted);
  background: var(--surface); flex: none;
}
.media-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; }
.media-save { color: var(--ink); }
.media-state.err { color: var(--danger); white-space: normal; }
.media-note { padding: 18px 12px 6px; margin: 0; font-size: 13px; color: var(--muted); }

.chat.dropping { outline: 2px dashed var(--ink); outline-offset: -8px; }
.composer .attach { padding: 6px 12px; font-size: 18px; line-height: 1.2; }

.viewer { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.88); display: grid; place-items: center; z-index: 10; cursor: zoom-out; }
.viewer img { max-width: 96vw; max-height: 94vh; object-fit: contain; }

/* ---------- 管理后台附加 ---------- */
.admin section + section { margin-top: 36px; }
.admin h2 { font-size: 18px; font-weight: 600; margin: 0 0 12px; }
.settings { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 14px; }
.settings label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.settings input {
  font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; padding: 6px 10px; width: 100%;
}
.link-btn { appearance: none; border: 0; background: none; padding: 0; font: inherit; font-size: 12px; color: var(--danger); cursor: pointer; }
.ok { color: #3f9b6b; font-size: 14px; }
