:root {
  color-scheme: dark;
  --bg: #0b1220;
  --bg-soft: #111c30;
  --panel: #16233a;
  --panel-2: #1c2c47;
  --line: #2a3d5e;
  --text: #e7eefc;
  --muted: #93a6c4;
  --gold: #f5c451;
  --green: #37d39a;
  --red: #ff7a7a;
  --blue: #6fb5ff;
  --radius: 16px;
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

/* 手机竖屏是主要使用场景：禁止系统自动放大字号，点按不再等 300ms。 */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

button, input, summary, a[href] { touch-action: manipulation; }

body {
  background:
    radial-gradient(1200px 600px at 15% -10%, rgba(74, 129, 220, 0.25), transparent 60%),
    radial-gradient(900px 500px at 100% 0%, rgba(245, 196, 81, 0.12), transparent 55%),
    var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Microsoft YaHei", "PingFang SC", "Hiragino Sans GB", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 700; letter-spacing: 0.01em; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 0.95rem; color: var(--muted); }
p { margin: 0; }

.muted { color: var(--muted); }
.small { font-size: 0.82rem; }
.note {
  margin-top: 18px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--muted);
  font-size: 0.82rem;
  background: rgba(255, 255, 255, 0.02);
}

code {
  padding: 1px 5px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.08);
  font-size: 0.85em;
}

/* ---------- 登录页 ---------- */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 18px calc(24px + env(safe-area-inset-bottom));
  min-height: 100vh;
  min-height: 100dvh;
}

.login-card {
  width: 100%;
  max-width: 420px;
  padding: 28px 24px 22px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: linear-gradient(180deg, rgba(28, 44, 71, 0.92), rgba(17, 28, 48, 0.96));
  box-shadow: var(--shadow);
}

.login-brand { margin-bottom: 14px; }
.login-card h1 { font-size: 1.35rem; }
.login-sub { margin-top: 6px; font-size: 0.88rem; }

.guest-entry { margin-top: 20px; display: grid; gap: 10px; }
.guest-status {
  margin: 0;
  padding: 14px;
  border: 1px dashed var(--line);
  border-radius: 14px;
  color: var(--muted);
  text-align: center;
  font-size: 0.92rem;
}
#guest-button { min-height: 52px; font-size: 1rem; font-weight: 700; }
.token-login { margin-top: 16px; border-top: 1px solid var(--line); padding-top: 12px; }
.token-login summary { cursor: pointer; color: var(--muted); font-size: 0.88rem; }
.token-login summary::-webkit-details-marker { display: none; }
.token-login summary::before { content: '▸ '; }
.token-login[open] summary::before { content: '▾ '; }

#login-form { margin-top: 22px; display: grid; gap: 10px; }
#login-form label { font-size: 0.85rem; color: var(--muted); }

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

input[type="password"], input[type="text"], input:not([type]) {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(7, 13, 24, 0.75);
  color: var(--text);
  outline: none;
  min-height: 46px;
}

input:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(111, 181, 255, 0.18); }

button {
  border: 0;
  border-radius: 12px;
  padding: 12px 16px;
  min-height: 44px;
  background: linear-gradient(180deg, #f7d474, var(--gold));
  color: #241a04;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.08s ease, filter 0.15s ease;
}

button:hover { filter: brightness(1.05); }
button:active { transform: translateY(1px); }
button:disabled { opacity: 0.55; cursor: not-allowed; }

button.warn { background: linear-gradient(180deg, #ffb36b, #f08a3c); color: #2a1403; }
button.ghost {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--text);
}

.error {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid rgba(255, 122, 122, 0.4);
  background: rgba(255, 122, 122, 0.12);
  color: var(--red);
  font-size: 0.88rem;
}

/* ---------- 顶栏 ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: rgba(11, 18, 32, 0.86);
  backdrop-filter: blur(12px);
}

.topbar-brand { display: flex; align-items: center; gap: 8px; min-width: 0; font-weight: 700; font-size: 0.95rem; }
.topbar-brand span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-me { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; font-size: 0.86rem; }
.me-name { max-width: 9em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.badge {
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid rgba(245, 196, 81, 0.5);
  color: var(--gold);
  font-size: 0.72rem;
  white-space: nowrap;
}

.chip {
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.04);
  font-size: 0.82rem;
  white-space: nowrap;
}

.link-button {
  min-height: 40px;
  min-width: 44px;
  padding: 6px 10px;
  background: transparent;
  color: var(--muted);
  font-weight: 500;
  font-size: 0.82rem;
}
.link-button:hover { color: var(--text); }

/* 窄屏（多数安卓/iPhone 竖屏）：品牌文字可以省略，账号区优先保留。 */
@media (max-width: 379px) {
  .topbar { padding-left: 10px; padding-right: 10px; gap: 6px; }
  .topbar-brand span { display: none; }
  .badge { display: none; }
  .me-name { max-width: 5.5em; }
  .topbar-me { gap: 6px; }
}

/* ---------- 房间列表 ---------- */
.app { max-width: 960px; margin: 0 auto; padding: 18px 16px 40px; }
.hero { margin-bottom: 16px; }
.hero p { margin-top: 6px; font-size: 0.9rem; }

.rooms {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
}

@media (min-width: 620px) {
  .rooms { grid-template-columns: 1fr 1fr; }
}

.room-card {
  display: block;
  width: 100%;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  text-align: left;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.12s ease, border-color 0.12s ease;
}

.room-card:hover { transform: translateY(-2px); border-color: rgba(245, 196, 81, 0.5); }
.room-card-cover { display: block; width: 100%; aspect-ratio: 16 / 9; object-fit: cover; background: var(--panel-2); }
/* 卡片内容用 span 拼的，必须显式 block，否则内边距不生效、文字会顶到卡片边缘被裁掉。 */
.room-card-body { display: block; padding: 12px 14px 14px; }
.room-card-top { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 4px 8px; }
.room-card-name { font-size: 1.06rem; font-weight: 700; }
.room-card-state { font-size: 0.78rem; color: var(--muted); }
.room-card-state.live { color: var(--green); }
.room-card-state.closed { color: var(--gold); }
.room-card-meta { display: block; margin-top: 4px; font-size: 0.8rem; color: var(--muted); }
.room-card-row { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 4px 8px; margin-top: 8px; font-size: 0.8rem; }
.room-card-row strong { color: var(--gold); font-weight: 600; }

/* 开奖来源状态：无数据/过期/失败必须一眼能区分，不能一律灰字。 */
.room-card-draw { display: block; margin-top: 6px; font-size: 0.78rem; color: var(--muted); }
.room-card-draw.is-ok, #draw-line.is-ok { color: var(--green); }
.room-card-draw.is-warn, #draw-line.is-warn { color: var(--gold); }
.room-card-draw.is-bad, #draw-line.is-bad { color: var(--red); }

.draw-sources { margin-top: 22px; }
.draw-sources h2 { font-size: 1rem; }
.draw-source-list { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 10px; }
.draw-source-item {
  display: block;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(22, 35, 58, 0.6);
}
.draw-source-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px 10px; }
.draw-chip {
  display: inline-block;
  padding: 2px 9px;
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 0.75rem;
  color: var(--muted);
  white-space: nowrap;
}
.draw-chip.is-ok { color: var(--green); border-color: rgba(55, 211, 154, 0.5); }
.draw-chip.is-warn { color: var(--gold); border-color: rgba(245, 196, 81, 0.5); }
.draw-chip.is-bad { color: var(--red); border-color: rgba(255, 122, 122, 0.5); }
.draw-source-detail { display: block; margin-top: 6px; line-height: 1.5; word-break: break-word; }
.draw-source-draw { display: block; margin-top: 4px; color: var(--text); }

/* ---------- 聊天页 ---------- */
/* 手机竖屏的主结构：整页锁定视口，只让消息区滚动，输入框永远贴在键盘上方。 */
#chat-view { display: flex; flex-direction: column; min-height: calc(100vh - 120px); min-height: calc(100dvh - 120px); }

body.in-chat {
  display: flex;
  flex-direction: column;
  height: var(--app-h, 100dvh);
  overflow: hidden;
  overscroll-behavior: none;
}
body.in-chat .topbar { flex: 0 0 auto; }
body.in-chat .app {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 0 12px;
  min-height: 0;
}
body.in-chat #chat-view { flex: 1 1 auto; min-height: 0; }
body.in-chat .chat-head { flex: 0 0 auto; }
/* 聊天页顶部已经显示积分，顶栏不再重复一颗，窄屏能省出位置。 */
body.in-chat .topbar #me-balance { display: none; }
body.in-chat .messages {
  flex: 1 1 auto;
  min-height: 0;
  max-height: none;
  padding: 12px 2px 8px;
}
body.in-chat .composer {
  flex: 0 0 auto;
  position: static;
  padding: 8px 0 calc(8px + env(safe-area-inset-bottom));
}

.chat-head {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.back-button {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--text);
  min-height: 40px;
  padding: 6px 12px;
  font-size: 0.85rem;
}

.chat-title { min-width: 0; }
.chat-title h2 { font-size: 1.05rem; }
#round-line { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.messages {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 14px 2px 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 280px;
  max-height: calc(100dvh - 320px);
  scroll-behavior: smooth;
}

.msg {
  max-width: 86%;
  padding: 9px 12px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--panel);
  font-size: 0.92rem;
  word-break: break-word;
}

.msg.me { align-self: flex-end; border-color: rgba(111, 181, 255, 0.45); background: rgba(47, 88, 148, 0.45); }
.msg.bot { align-self: flex-start; background: rgba(23, 40, 64, 0.9); }
.msg.admin { align-self: flex-start; border-color: rgba(245, 196, 81, 0.4); background: rgba(58, 46, 18, 0.55); }
.msg.direct { align-self: flex-end; border-color: rgba(55, 211, 154, 0.45); background: rgba(19, 61, 48, 0.55); }

.msg-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 4px; font-size: 0.72rem; color: var(--muted); }
.msg-head strong { color: var(--text); font-size: 0.78rem; font-weight: 600; }
.msg-body { white-space: pre-wrap; }
.msg-body a { color: var(--blue); }
.msg-hint {
  display: inline-block;
  margin-left: 6px;
  padding: 0 6px;
  border-radius: 999px;
  background: rgba(55, 211, 154, 0.16);
  color: var(--green);
  font-size: 0.68rem;
}

.msg-images { display: grid; gap: 8px; margin-top: 6px; }
.msg-images a { display: block; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); }
.msg-images img { display: block; width: 100%; height: auto; }

.msg-poster { margin-top: 6px; }
.msg-poster img { display: block; width: 100%; max-width: 360px; height: auto; border-radius: 10px; border: 1px solid var(--line); }
.msg-poster .caption { margin-top: 4px; font-size: 0.78rem; color: var(--muted); }

.composer {
  position: sticky;
  bottom: 0;
  padding: 10px 0 calc(10px + env(safe-area-inset-bottom));
  background: linear-gradient(180deg, rgba(11, 18, 32, 0), rgba(11, 18, 32, 0.96) 30%);
}

/* 快捷按钮一行横滑：竖屏手机上不再换行占掉消息区高度。 */
.quick {
  display: flex;
  flex-wrap: nowrap;
  gap: 8px;
  margin-bottom: 8px;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.quick::-webkit-scrollbar { display: none; }
.quick button {
  flex: 0 0 auto;
  min-height: 42px;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.04);
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 500;
  white-space: nowrap;
}
.quick button:hover { color: var(--text); border-color: var(--blue); }
.quick button.admin-entry { color: var(--gold); border-color: rgba(245, 196, 81, 0.55); }

.input-row { display: flex; gap: 8px; }
.input-row input { flex: 1; min-width: 0; }
.send-button { min-width: 76px; }

.hint { margin-top: 6px; font-size: 0.75rem; color: var(--muted); }

/* ---------- 管理员控制台（手机从底部弹出的面板） ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 45;
  background: rgba(3, 7, 14, 0.62);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

.admin-panel {
  position: fixed;
  z-index: 46;
  left: 0;
  right: 0;
  bottom: 0;
  display: none;
  max-height: min(86dvh, 760px);
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 0 16px calc(20px + env(safe-area-inset-bottom));
  border: 1px solid rgba(245, 196, 81, 0.4);
  border-bottom: 0;
  border-radius: 20px 20px 0 0;
  background: linear-gradient(180deg, #241b09, #140f05);
  box-shadow: 0 -14px 44px rgba(0, 0, 0, 0.55);
  animation: sheet-in 0.16s ease-out;
}
.admin-panel.is-open { display: block; }

@keyframes sheet-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* 标题栏吸顶：面板往下滚时“关闭”一直点得到。 */
.admin-head {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 0 0 6px;
  padding: 14px 0 8px;
  background: #241b09;
  border-bottom: 1px solid rgba(245, 196, 81, 0.22);
}
.admin-head h2 { font-size: 1.05rem; color: var(--gold); }

.admin-group { margin-top: 14px; }
.admin-group h3 { margin-bottom: 8px; }
/* 竖屏手机：一列到底，每个输入框占满整行，手指点得准。 */
.admin-row { display: grid; grid-template-columns: 1fr; gap: 8px; align-items: stretch; }
.admin-row button { grid-column: auto; min-height: 46px; }
.admin-buttons { display: flex; flex-wrap: wrap; gap: 8px; }
.admin-buttons button { min-height: 44px; padding: 10px 14px; font-size: 0.86rem; }

@media (min-width: 720px) {
  .admin-panel {
    left: 50%;
    right: auto;
    bottom: 20px;
    width: min(100vw - 40px, 680px);
    transform: translateX(-50%);
    border-bottom: 1px solid rgba(245, 196, 81, 0.4);
    border-radius: 18px;
    padding: 14px 18px 18px;
  }
  .admin-row { grid-template-columns: repeat(3, 1fr) auto; }
  .admin-row button { grid-column: auto; }
}

/* ---------- 提示条 ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 50;
  max-width: min(92vw, 520px);
  padding: 10px 16px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(22, 35, 58, 0.96);
  box-shadow: var(--shadow);
  font-size: 0.88rem;
  text-align: center;
}
.toast.error { border-color: rgba(255, 122, 122, 0.5); color: var(--red); }
.toast.ok { border-color: rgba(55, 211, 154, 0.5); color: var(--green); }
/* 聊天页把提示条抬到输入框上方，避免挡住发送按钮。 */
body.in-chat .toast { bottom: calc(112px + env(safe-area-inset-bottom)); }

/* ---------- 手机竖屏细节 ---------- */
.admin-close { color: var(--gold); }

@media (max-width: 480px) {
  .app { padding: 14px 12px 32px; }
  .msg { max-width: 92%; font-size: 0.95rem; }
  .msg-head { font-size: 0.74rem; }
  .msg-poster img { max-width: 100%; }
  .hint { font-size: 0.78rem; line-height: 1.5; }
  .room-card-body { padding: 10px 12px 12px; }
  .hero h1 { font-size: 1.22rem; }
  .hero p { font-size: 0.86rem; }
  .note { font-size: 0.78rem; }
  .chat-head { gap: 8px; }
  .back-button { padding: 6px 10px; }
}

.empty {
  padding: 26px 16px;
  text-align: center;
  color: var(--muted);
  border: 1px dashed var(--line);
  border-radius: 14px;
  font-size: 0.9rem;
}

[hidden] { display: none !important; }
