:root {
  color-scheme: light;
  --bg: #f0f2f5;
  --paper: #ffffff;
  --ink: #1f2329;
  --muted: #646a73;
  --line: #e5e6eb;
  --accent: #1677ff;
  --accent-ink: #ffffff;
  --danger: #cf1322;
  --ok: #389e0d;
  --sider: #001529;
  font-family: "Segoe UI", "PingFang SC", "Noto Sans SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
}

h1, h2, p { margin: 0; }

h1 { font-size: 1.5rem; font-weight: 500; }

h2 { font-size: 1.125rem; font-weight: 500; }

a { color: var(--accent); }

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

button, .button-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 32px;
  padding: 0 15px;
  border: 1px solid var(--accent);
  border-radius: 4px;
  background: var(--accent);
  color: var(--accent-ink);
  text-decoration: none;
  cursor: pointer;
}

button.ghost, .button-link.ghost {
  background: #fff;
  color: var(--ink);
  border-color: #d9d9d9;
}

button.danger {
  background: #fff;
  color: var(--danger);
  border-color: #ffa39e;
}

input, textarea {
  width: 100%;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  background: #fff;
  padding: 6px 11px;
}

textarea { resize: none; min-height: 44px; max-height: 140px; }

label { display: grid; gap: 6px; font-size: 0.875rem; }

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

.brand { color: var(--muted); font-size: 0.8125rem; }

.me, .empty, .status, .closed-note { color: var(--muted); font-size: 0.875rem; }

.tag {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  padding: 0 8px;
  min-height: 22px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  background: #fafafa;
  color: var(--muted);
  font-size: 0.75rem;
  white-space: nowrap;
}

.center-card {
  max-width: 400px;
  margin: 12vh auto;
  padding: 32px 32px 28px;
  background: var(--paper);
  border-radius: 8px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08);
}

.center-card form, .stack-form { display: grid; gap: 12px; }

.page { min-height: 100dvh; }

.admin-top {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  padding: 16px 20px;
}

.flash { margin: 12px 16px; padding: 10px 12px; background: #fff; border: 1px solid var(--line); border-radius: 4px; }

.flash.ok { color: var(--ok); background: #f6ffed; border-color: #b7eb8f; }

.flash.err { color: var(--danger); background: #fff2f0; border-color: #ffccc7; }

.room-grid {
  list-style: none;
  padding: 16px;
  margin: 0;
  display: grid;
  gap: 12px;
}

.room-grid a {
  display: grid;
  gap: 8px;
  padding: 20px;
  background: var(--paper);
  border: 1px solid var(--line);
  color: inherit;
  text-decoration: none;
}

.chat-app {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background: #fff;
}

.side {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--paper);
}

.side-head { display: flex; align-items: baseline; gap: 8px; flex: none; }

.side-head strong { font-size: 15px; }

.room-nav {
  display: flex;
  gap: 6px;
  min-width: 0;
  overflow-x: auto;
}

.room-nav a {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 32px;
  padding: 0 12px;
  border-radius: 16px;
  background: #f5f6f8;
  color: var(--ink);
  text-decoration: none;
  white-space: nowrap;
}

.room-nav a.active { background: #e6f4ff; color: var(--accent); font-weight: 600; }

.room-nav em { color: var(--muted); font-size: 12px; font-style: normal; }

.unread-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  margin-left: auto;
  padding: 0 5px;
  border-radius: 9px;
  background: #ff4d4f;
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  font-style: normal;
  line-height: 18px;
}

.conversation {
  position: relative;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: #f5f6f8;
}

.conversation-body {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
}

.thread {
  position: relative;
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.chat-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 0 20px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}

.chat-bar h2 { font-size: 16px; font-weight: 600; }

.room-back { display: none; }

.chat-app .status { margin-left: auto; padding: 0; }

.chat-app .status::before {
  content: "";
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 6px;
  border-radius: 50%;
  background: #d9d9d9;
  vertical-align: 0;
}

.chat-app .status.live::before { background: var(--ok); }

.messages { flex: 1; min-height: 0; overflow: auto; padding: 16px 0; }

.chat-app .messages { display: flex; flex-direction: column; }

.chat-app .message-log { margin-top: auto; }

.unread-mark {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 6px 16px;
  color: var(--accent);
  font-size: 12px;
}

.unread-mark::before,
.unread-mark::after {
  content: "";
  flex: 1;
  height: 1px;
  background: #91caff;
}

.jump {
  position: absolute;
  z-index: 2;
  left: 50%;
  bottom: 84px;
  transform: translateX(-50%);
  border-color: #91caff;
  background: #fff;
  color: var(--accent);
  box-shadow: 0 2px 8px rgba(22, 119, 255, 0.16);
}

.msg { display: flex; align-items: flex-end; gap: 8px; margin: 10px 16px; }

.msg.mine { justify-content: flex-end; }

.avatar {
  display: grid;
  place-items: center;
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #e6f4ff;
  color: var(--accent);
  font-size: 14px;
  font-weight: 600;
}

.bubble {
  max-width: min(70ch, 78%);
  padding: 8px 12px;
  background: #fff;
  border: 0;
  border-radius: 12px 12px 12px 4px;
  box-shadow: 0 1px 2px rgba(31, 35, 41, 0.06);
}

.msg.mine .bubble {
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 12px 12px 4px 12px;
}

.meta {
  display: flex;
  gap: 8px;
  align-items: baseline;
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 4px;
}

.msg.mine .meta { color: rgba(255, 255, 255, 0.85); }

.pin {
  margin-left: auto;
  min-height: 22px;
  padding: 0 6px;
  border-color: transparent;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
}

.pin.on { color: var(--accent); }

.msg.mine .pin { color: rgba(255, 255, 255, 0.85); }

.msg.mine .pin.on { color: #fff; }

.msg.flash .bubble { box-shadow: 0 0 0 2px var(--accent); }

.pin-dock {
  flex: none;
  display: flex;
  min-height: 0;
  border-left: 1px solid var(--line);
  background: #fff;
}

.pin-dock #pins {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: 40px;
  margin: 0;
  padding: 16px 0;
  border: 0;
  border-radius: 0;
  background: #fff;
  color: var(--muted);
}

.pin-dock #pins span { writing-mode: vertical-rl; letter-spacing: 0.12em; }

.pin-dock #pins.on { color: var(--accent); }

.pin-dock #pins .pin-total {
  min-width: 18px;
  height: 18px;
  margin: 0;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  line-height: 18px;
}

.pin-sheet {
  display: none;
  flex-direction: column;
  width: min(280px, 46vw);
  min-height: 0;
  border-right: 1px solid var(--line);
}

.pin-dock.open .pin-sheet { display: flex; }

.pin-sheet .pin-title {
  flex: none;
  margin: 0;
  padding: 16px 16px 8px;
  font-weight: 600;
}

.pin-panel {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-height: 0;
  overflow: auto;
  padding: 4px 8px 12px;
}

.pin-panel .empty { margin: 8px; }

.pin-item {
  display: grid;
  justify-content: stretch;
  justify-items: start;
  gap: 2px;
  width: 100%;
  height: auto;
  min-height: 0;
  padding: 8px;
  border-color: transparent;
  background: transparent;
  color: var(--ink);
  font-size: 13px;
  text-align: left;
  white-space: normal;
}

.pin-item span {
  max-width: 100%;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pin-item:hover { background: #f5f6f8; }

.text { white-space: pre-wrap; word-break: break-word; }

.bubble img { display: block; max-width: 100%; max-height: 240px; border-radius: 6px; cursor: zoom-in; }

.bubble a { word-break: break-all; }

.msg.mine .bubble a { color: var(--accent-ink); }

.file-card,
.msg.mine .bubble a.file-card {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 220px;
  max-width: 100%;
  margin-top: 2px;
  padding: 8px 10px;
  border-radius: 8px;
  background: #f7f8fa;
  color: var(--ink);
  text-decoration: none;
}

.file-badge {
  display: grid;
  place-items: center;
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: #f0f0f0;
  color: #595959;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.file-badge.archive { background: #fff7e6; color: #d46b08; }

.file-badge.document { background: #f6ffed; color: #389e0d; }

.file-badge.image { background: #e6f4ff; color: var(--accent); }

.file-badge.package { background: #f9f0ff; color: #531dab; }

.file-info { display: grid; gap: 2px; min-width: 0; }

.file-info strong {
  overflow: hidden;
  font-size: 0.875rem;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-info span { color: var(--muted); font-size: 0.75rem; }

.composer {
  display: grid;
  gap: 8px;
  margin: 12px 16px 16px;
  padding: 16px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--paper);
  box-shadow: 0 1px 2px rgba(31, 35, 41, 0.04);
}

.chat-app .composer textarea {
  border: 0;
  padding: 10px 4px;
  line-height: 24px;
  min-height: 44px;
  max-height: 152px;
  overflow-y: auto;
  background: transparent;
}

.composer.dropping { outline: 2px dashed var(--accent); outline-offset: -6px; }

.composer-row { display: flex; gap: 8px; align-items: flex-end; }

.composer-actions { display: flex; flex: none; gap: 8px; }

.composer textarea { flex: 1; min-width: 0; }

.composer-hint { color: var(--muted); font-size: 12px; }

.pending-file {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fafafa;
}

.upload-progress { flex: 1 0 100%; display: grid; gap: 4px; }

.upload-bar { height: 4px; overflow: hidden; border-radius: 2px; background: #f0f0f0; }

.upload-bar > span { display: block; height: 100%; width: 0; background: var(--accent); }

.upload-label { color: var(--muted); font-size: 12px; }

.composer.uploading textarea,
.composer.uploading .file-btn { pointer-events: none; opacity: 0.65; }

.pending-file img {
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: 6px;
}

.pending-file .ghost { margin-left: auto; }

.file-btn, .composer button { white-space: nowrap; min-height: 44px; }

.file-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  background: var(--paper);
  cursor: pointer;
}

.file-btn input {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

.closed-note, .status { padding: 8px 16px; }

#older { margin: 8px 16px; justify-self: start; }

dialog#viewer { padding: 0; border: 0; background: transparent; }

dialog#viewer img { max-width: 92vw; max-height: 92dvh; }

dialog#viewer::backdrop { background: rgba(22, 32, 48, 0.72); }

@media (min-width: 720px) {
  .room-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (min-width: 880px) {
  .chat-app:has(> .side) {
    grid-template-columns: 260px minmax(0, 1fr);
    grid-template-rows: 1fr;
  }

  .chat-app > .side {
    flex-direction: column;
    align-items: stretch;
    height: 100%;
    padding: 20px 12px;
    border-bottom: 0;
    border-right: 1px solid var(--line);
  }

  .side-head {
    display: grid;
    gap: 4px;
    padding: 0 8px 16px;
    border-bottom: 1px solid var(--line);
  }

  .room-nav {
    display: grid;
    align-content: start;
    gap: 4px;
    margin-top: 12px;
    overflow: auto;
  }

  .room-nav a { min-height: 40px; border-radius: 8px; }
}

@media (max-width: 879px) {
  body:has(.chat-app) { height: 100dvh; overflow: hidden; }

  .chat-app:has(> .side) { grid-template-rows: minmax(0, 1fr); }

  .chat-app > .side { display: none; }

  .room-back {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    flex: none;
    min-height: 44px;
    margin-left: -4px;
    color: var(--accent);
    font-size: 16px;
    text-decoration: none;
  }

  .room-back::before { content: "‹"; font-size: 24px; line-height: 1; }

  .chat-bar {
    gap: 8px;
    min-height: calc(52px + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) 0 max(8px, env(safe-area-inset-left));
  }

  .chat-bar h2 {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .pin-bar {
    display: inline-flex;
    flex: none;
    flex-direction: row;
    align-items: center;
    gap: 6px;
    width: auto;
    min-height: 32px;
    margin-left: auto;
    padding: 0 12px;
    border: 1px solid var(--line);
    border-radius: 16px;
    background: #fff;
    color: var(--muted);
  }

  .pin-bar span { letter-spacing: 0; }

  .pin-bar.on { color: var(--accent); }

  .chat-bar .pin-bar + .status { margin-left: 8px; font-size: 0; }

  .chat-bar .pin-bar + .status::before { margin-right: 0; }

  .pin-dock {
    position: absolute;
    inset: 0;
    z-index: 6;
    display: none;
    border: 0;
    background: rgba(31, 35, 41, 0.35);
  }

  .pin-dock.open { display: flex; }

  .pin-dock.open .pin-sheet {
    width: min(320px, 86vw);
    height: 100%;
    margin-left: auto;
    border-right: 0;
    background: #fff;
  }

  .messages { padding: 12px 0; }

  .msg { margin: 8px 12px; }

  .bubble { max-width: calc(100vw - 92px); }

  .file-card,
  .msg.mine .bubble a.file-card { min-width: 0; }

  .chat-app .composer {
    margin: 0;
    border-width: 1px 0 0;
    border-radius: 0;
    padding: 16px 12px calc(16px + env(safe-area-inset-bottom));
    box-shadow: none;
  }

  .chat-app .composer textarea { font-size: 16px; }

  .pin { min-height: 32px; padding: 0 8px; }

  .page { padding-bottom: env(safe-area-inset-bottom); }

  .admin-top { padding-top: calc(16px + env(safe-area-inset-top)); }

  .room-grid a {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    min-height: 72px;
    border-radius: 12px;
  }

  .room-grid a strong { font-size: 16px; }

  .room-grid .unread-count { margin-left: 0; }

  .room-grid .tag { grid-column: 1; }
}

