:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1d2330;
  --muted: #6b7280;
  --border: #dde1e7;
  --accent: #2f6fdb;
  --accent-text: #ffffff;
  --danger: #c0392b;
  --danger-bg: #fdecea;
  --ok: #2e7d32;
  --warn: #b26a00;
  --code-bg: #f0f2f5;
  --grid: #e5e7eb;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --panel: #1c2027;
    --text: #e3e6eb;
    --muted: #9aa3af;
    --border: #2e343e;
    --accent: #6c9cf0;
    --accent-text: #0e1116;
    --danger: #ef7d71;
    --danger-bg: #3a1f1d;
    --ok: #7bc47f;
    --warn: #e0a64a;
    --code-bg: #242a33;
    --grid: #2a3039;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 20px; margin: 0 0 12px; }
h2 { font-size: 16px; margin: 20px 0 8px; }
h3 { font-size: 14px; margin: 16px 0 6px; }

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.brand { font-weight: 700; color: var(--text); }
.nav { display: flex; gap: 4px; flex-wrap: wrap; flex: 1; }
.nav a { padding: 4px 10px; border-radius: 6px; color: var(--text); }
.nav a.active { background: var(--accent); color: var(--accent-text); }
.nav a:hover { text-decoration: none; background: var(--code-bg); }
.nav a.active:hover { background: var(--accent); }
.who { display: flex; gap: 8px; align-items: center; color: var(--muted); }

main { max-width: 1200px; margin: 0 auto; padding: 20px; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; }
.nowrap { white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.pre { white-space: pre-wrap; word-break: break-word; }

.error {
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--danger);
  background: var(--danger-bg);
  border: 1px solid var(--danger);
  border-radius: 6px;
  padding: 8px 12px;
  margin: 8px 0;
}
.notice {
  color: var(--ok);
  border: 1px solid var(--ok);
  border-radius: 6px;
  padding: 8px 12px;
  margin: 8px 0;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 16px;
  margin-bottom: 16px;
  min-width: 0;
}
.card > h2:first-child { margin-top: 0; }
.badges { display: inline-flex; flex-wrap: wrap; gap: 6px; }
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.stat { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 10px 14px; }
.stat .k { color: var(--muted); font-size: 12px; }
.stat .v { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat.bad .v { color: var(--danger); }

.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; background: var(--panel); }
th, td { padding: 6px 8px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th { font-weight: 600; color: var(--muted); font-size: 12px; white-space: nowrap; }
tbody tr:hover { background: var(--code-bg); }
td.text { min-width: 240px; white-space: pre-wrap; word-break: break-word; }

dl.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; margin: 0; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; word-break: break-word; }

pre.code {
  background: var(--code-bg);
  border-radius: 6px;
  padding: 8px 12px;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 6px 0;
  font-size: 12px;
}

.badge {
  display: inline-block;
  padding: 0 6px;
  border-radius: 10px;
  font-size: 12px;
  border: 1px solid var(--border);
  color: var(--muted);
  white-space: nowrap;
}
.badge.on { color: var(--ok); border-color: var(--ok); }
.badge.off { color: var(--muted); }
.badge.warn { color: var(--warn); border-color: var(--warn); }
.badge.bad { color: var(--danger); border-color: var(--danger); }

button, .button {
  font: inherit;
  padding: 5px 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.danger { color: var(--danger); border-color: var(--danger); }
button.link { border: none; background: none; color: var(--accent); padding: 0; }
button:disabled { opacity: .6; cursor: default; }
button.selected { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
a.button { display: inline-block; }
a.button:hover { text-decoration: none; }

input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
  max-width: 100%;
}
textarea { width: 100%; min-height: 80px; resize: vertical; }
textarea.json { min-height: 360px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; }
input[type=checkbox] { width: 16px; height: 16px; vertical-align: middle; }

.field { display: flex; flex-direction: column; gap: 2px; margin-bottom: 10px; }
.field-label { color: var(--muted); font-size: 12px; }
.field.inline { flex-direction: row; align-items: center; gap: 8px; }
.hint { color: var(--muted); font-size: 12px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 0 16px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.toolbar .spacer { flex: 1; }
.quick-adjust { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 10px; }
.bar-edit { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.bar-edit input[type=number] { width: 72px; }
meter { width: 120px; vertical-align: middle; }
ul.plain { list-style: none; margin: 0; padding: 0; }
.persona-image {
  display: block;
  max-width: 240px;
  max-height: 320px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--code-bg);
}

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin: 12px 0 16px; flex-wrap: wrap; }
.tabs a { padding: 6px 14px; color: var(--text); border-bottom: 2px solid transparent; }
.tabs a.active { border-bottom-color: var(--accent); color: var(--accent); }
.tabs a:hover { text-decoration: none; }

.messages { list-style: none; margin: 0; padding: 0; }
.messages li { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; margin-bottom: 8px; }
.messages li.bot { border-left: 3px solid var(--accent); }
.messages .meta { color: var(--muted); font-size: 12px; display: flex; gap: 8px; flex-wrap: wrap; }
.messages .body { white-space: pre-wrap; word-break: break-word; margin-top: 4px; }

/* The classic chat page (#/chat), kept for debugging: views/chatsclassic.js
   wraps it in .chat-classic, and every rule of the page hangs on it. */
.chat-classic .chat-classic-title { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.chat-classic .chat-classic-title h1 { margin: 0; }
.chat-classic .chat-classic-title a.button { font-size: 12px; padding: 3px 10px; }
.chat-classic .chat { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 12px; }
.chat-classic .chat-log {
  list-style: none;
  margin: 0;
  padding: 0 4px;
  height: 60vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.chat-classic .bubble { max-width: 75%; padding: 6px 10px; border-radius: 10px; border: 1px solid var(--border); }
.chat-classic .bubble.bot { align-self: flex-start; background: var(--code-bg); border-bottom-left-radius: 2px; }
.chat-classic .bubble.user { align-self: flex-end; background: var(--panel); border-color: var(--accent); border-bottom-right-radius: 2px; }
.chat-classic .bubble .meta { color: var(--muted); font-size: 12px; display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.chat-classic .bubble.user .meta { justify-content: flex-end; }
.chat-classic .bubble .body { white-space: pre-wrap; word-break: break-word; }
.chat-classic .chat-typing { color: var(--muted); font-size: 12px; padding: 4px; }
.chat-classic .chat-form { position: relative; display: flex; gap: 8px; align-items: flex-end; margin-top: 8px; }
.chat-classic .cmd-menu {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 100%;
  z-index: 20;
  max-height: min(288px, 40vh);
  overflow-y: auto;
  margin-bottom: 4px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .15);
}
.chat-classic .cmd-item { display: flex; flex-wrap: wrap; align-items: baseline; column-gap: 8px; padding: 6px 10px; cursor: pointer; }
.chat-classic .cmd-item[aria-selected="true"] { background: var(--code-bg); }
.chat-classic .cmd-args, .chat-classic .cmd-desc { color: var(--muted); }
.chat-classic .cmd-desc { margin-left: auto; font-size: 12px; }
.chat-classic .chat-form .chat-input { flex: 1; min-height: 60px; }
.chat-classic .chat-suggestions { display: flex; flex-wrap: wrap; gap: 6px; max-width: 100%; }
.chat-classic .chat-suggestions button { white-space: normal; text-align: left; border-color: var(--accent); color: var(--accent); }
.chat-classic .chat-options { display: inline-flex; align-items: center; gap: 6px; margin-top: 6px; color: var(--muted); font-size: 12px; }
.chat-classic .chat-actions { display: flex; gap: 4px; margin-top: 4px; }
.chat-classic .bubble.user .chat-actions { justify-content: flex-end; }
.chat-classic .chat-action { padding: 0; border: 0; background: none; color: var(--muted); font-size: 12px; cursor: pointer; }
.chat-classic .chat-action:hover { text-decoration: underline; }
.chat-classic .chat-action.danger { color: var(--danger); }
.chat-classic .chat-action[hidden] { display: none; }
.chat-classic .chat-action:disabled { opacity: .45; cursor: default; text-decoration: none; }
.chat-classic .chat-ctx {
  position: fixed;
  z-index: 20;
  min-width: 120px;
  padding: 4px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
}
.chat-classic .chat-ctx[hidden] { display: none; }
.chat-classic .chat-ctx-item {
  display: block;
  width: 100%;
  padding: 6px 8px;
  border: 0;
  border-radius: 4px;
  background: none;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}
.chat-classic .chat-ctx-item:hover { background: var(--code-bg); }
.chat-classic .chat-ctx-item.danger { color: var(--danger); }

.chat-classic .chat-page { display: grid; grid-template-columns: 280px minmax(0, 1fr); gap: 16px; align-items: start; }
.chat-classic .chat-side {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  max-height: calc(100vh - 120px);
  overflow-y: auto;
}
.chat-classic .chat-group { border-top: 1px solid var(--border); padding: 10px 0; }
.chat-classic .chat-group:first-child { border-top: none; padding-top: 0; }
.chat-classic .chat-group-head { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.chat-classic .chat-group-head .spacer { flex: 1; }
.chat-classic .chat-group p { margin: 2px 0; }
.chat-classic .chat-items { list-style: none; margin: 6px 0 8px; padding: 0; }
.chat-classic .chat-items a { display: flex; align-items: center; gap: 6px; padding: 4px 8px; border-radius: 6px; color: var(--text); }
.chat-classic .chat-items a:hover { background: var(--code-bg); text-decoration: none; }
.chat-classic .chat-items a.active { background: var(--accent); color: var(--accent-text); }
.chat-classic .chat-items a.active .muted, .chat-classic .chat-items a.active .badge { color: var(--accent-text); border-color: var(--accent-text); }
.chat-classic .chat-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-classic .chat-time { font-size: 12px; white-space: nowrap; }
.chat-classic .chat-main { min-width: 0; }
.chat-classic .chat-head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; margin-bottom: 4px; }
.chat-classic .chat-head h2 { margin: 0; }
.chat-classic .chat-head .spacer { flex: 1; }
.chat-classic .chat-empty { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 24px 16px; }
.chat-classic .chat-empty p { margin: 4px 0; }

/* The Telegram-style chat page (#/im) fills the window like Telegram Desktop:
   app.js gives the body the class chat-app there, and every rule of the page
   hangs on it. */
body.chat-app main { max-width: none; margin: 0; padding: 0; }
.chat-app .tg {
  position: relative;
  display: grid;
  grid-template-columns: 72px 320px minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.chat-app .avatar {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--avatar-size, 42px);
  height: var(--avatar-size, 42px);
  border-radius: 50%;
  object-fit: cover;
  background: var(--code-bg);
  color: #ffffff;
  font-size: calc(var(--avatar-size, 42px) * .42);
  font-weight: 600;
  line-height: 1;
  user-select: none;
}
.chat-app .avatar-c0 { background: #e17076; }
.chat-app .avatar-c1 { background: #f0a35e; }
.chat-app .avatar-c2 { background: #a695e7; }
.chat-app .avatar-c3 { background: #6fb560; }
.chat-app .avatar-c4 { background: #4fb3b5; }
.chat-app .avatar-c5 { background: #5a9fd6; }
.chat-app .avatar-c6 { background: #e0729f; }
.chat-app .avatar-c7 { background: #c99a3b; }

.chat-app .tg-rail {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-y: auto;
  background: var(--bg);
  border-right: 1px solid var(--border);
}
.chat-app .tg-menu-button {
  border: none;
  border-radius: 0;
  background: none;
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
  padding: 14px 0;
}
.chat-app .tg-menu-button:hover { color: var(--text); }
.chat-app .tg-folders { display: flex; flex-direction: column; }
.chat-app .tg-folder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 4px;
  color: var(--muted);
  font-size: 11px;
  text-align: center;
  transition: background-color 120ms ease-out;
}
.chat-app .tg-folder:hover { background: var(--panel); text-decoration: none; }
.chat-app .tg-folder.active { background: var(--panel); color: var(--accent); }
.chat-app .tg-folder-pic { position: relative; display: inline-flex; }
.chat-app .tg-folder-icon {
  width: 28px;
  height: 28px;
  margin: 4px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linejoin: round;
  stroke-linecap: round;
}
.chat-app .tg-folder-name { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-app .tg-folder .tg-folder-unread {
  position: absolute;
  top: -6px;
  right: -12px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  font-size: 11px;
  line-height: 18px;
  box-shadow: 0 0 0 2px var(--bg);
}
.chat-app .tg-folder-warn {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--warn);
  box-shadow: 0 0 0 2px var(--bg);
}

.chat-app .tg-menu {
  position: absolute;
  top: 48px;
  left: 8px;
  z-index: 20;
  min-width: 200px;
  padding: 6px 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .25);
  transform-origin: left top;
  animation: tg-menu-in 150ms cubic-bezier(.2,.8,.2,1);
}
.chat-app .tg-menu nav { display: flex; flex-direction: column; }
.chat-app .tg-menu nav a,
.chat-app .tg-menu-item {
  display: block;
  width: 100%;
  padding: 8px 16px;
  border: none;
  border-radius: 0;
  background: none;
  color: var(--text);
  text-align: left;
}
.chat-app .tg-menu nav a:hover,
.chat-app .tg-menu-item:hover { background: var(--code-bg); text-decoration: none; }
.chat-app .tg-menu-item { border-top: 1px solid var(--border); }
.chat-app .tg-menu-user {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 16px;
  border-top: 1px solid var(--border);
}

.chat-app .tg-list {
  position: relative;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
}
.chat-app .tg-search { padding: 10px 12px; }
.chat-app .tg-search input {
  width: 100%;
  padding: 6px 14px;
  border-color: transparent;
  border-radius: 18px;
  background: var(--code-bg);
}
.chat-app .tg-search input:focus { border-color: var(--accent); outline: none; }
.chat-app .tg-list .notice,
.chat-app .tg-list .error { margin: 4px 12px; }
.chat-app .tg-botbar { padding: 0 12px 8px; border-bottom: 1px solid var(--border); }
.chat-app .tg-botbar-head { display: flex; align-items: center; gap: 8px; }
.chat-app .tg-botbar-head .spacer { flex: 1; }
.chat-app .tg-botbar p { margin: 2px 0; }
.chat-app .tg-chats {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  list-style: none;
  margin: 0;
  padding: 4px 6px 80px;
}
.chat-app .tg-chat {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 8px;
  border-radius: 10px;
  color: var(--text);
  transition: background-color 120ms ease-out;
}
.chat-app .tg-chat:hover { background: var(--code-bg); text-decoration: none; }
.chat-app .tg-chat.active { background: var(--accent); color: var(--accent-text); }
.chat-app .tg-chat-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.chat-app .tg-chat-line { display: flex; align-items: center; gap: 6px; min-width: 0; }
.chat-app .tg-chat-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-app .tg-chat-title,
.chat-app .tg-chat-preview { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
.chat-app .tg-chat-title { font-size: 12px; }
.chat-app .tg-chat-time { margin-left: auto; color: var(--muted); font-size: 12px; white-space: nowrap; }
.chat-app .tg-you { color: var(--accent); }
.chat-app .tg-chat.active .tg-chat-title,
.chat-app .tg-chat.active .tg-chat-preview,
.chat-app .tg-chat.active .tg-chat-time,
.chat-app .tg-chat.active .tg-you { color: var(--accent-text); }
.chat-app .tg-unread {
  flex: none;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 12px;
  font-weight: 600;
  line-height: 20px;
  text-align: center;
}
.chat-app .tg-unread.bump { animation: tg-bump 200ms ease-out backwards; }
.chat-app .tg-chat.active .tg-unread { background: var(--accent-text); color: var(--accent); }
.chat-app .tg-list-empty { padding: 24px 12px; text-align: center; }
.chat-app .tg-fab {
  position: absolute;
  right: 16px;
  bottom: 16px;
  width: 54px;
  height: 54px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 22px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .3);
}

/* The conversation pane: its colors, then the top bar, the messages and the
   message box. --tg-side is the side margin that keeps the box in a column of
   at most 760px; the messages sit 8px further in, room for the tails. */
.chat-app .tg-pane {
  --tg-wall: #e6ebf0;
  --tg-in: #ffffff;
  --tg-out: var(--accent);
  --tg-out-meta: rgba(255, 255, 255, .75);
  --tg-pill: rgba(40, 55, 75, .65);
  --tg-key: rgba(255, 255, 255, .6);
  --tg-key-hover: rgba(255, 255, 255, .85);
  --tg-key-text: var(--accent);
  --tg-side: max(16px, calc((100% - 760px) / 2));
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: var(--tg-wall);
}
@media (prefers-color-scheme: dark) {
  .chat-app .tg-pane {
    --tg-wall: var(--bg);
    --tg-in: #222a35;
    --tg-out: #2b5278;
    --tg-pill: rgba(255, 255, 255, .1);
    --tg-key: rgba(255, 255, 255, .1);
    --tg-key-hover: rgba(255, 255, 255, .18);
    --tg-key-text: #ffffff;
  }
}
.chat-app .tg-empty { flex: 1; display: flex; align-items: center; justify-content: center; }
.chat-app .tg-pill {
  padding: 4px 12px;
  border-radius: 14px;
  background: var(--tg-pill);
  color: #ffffff;
  font-size: 13px;
}
.chat-app .tg-pane > .error,
.chat-app .tg-pane > p { margin: 12px 16px; }
.chat-app .tg-pane-msg { padding: 0 16px; }
.chat-app .tg-pane-msg:empty { display: none; }

.chat-app .tg-bar {
  position: relative;
  z-index: 2;
  flex: none;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 8px 8px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.chat-app .tg-bar-back,
.chat-app .tg-bar-more {
  flex: none;
  width: 40px;
  height: 40px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: none;
  color: var(--muted);
  font-size: 20px;
  line-height: 1;
}
.chat-app .tg-bar-back { display: none; align-items: center; justify-content: center; }
.chat-app .tg-bar-back:hover,
.chat-app .tg-bar-more:hover { background: var(--code-bg); color: var(--text); }
.chat-app .tg-bar-name { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.35; }
.chat-app .tg-bar-name strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-app .tg-bar-status { min-height: 1.35em; color: var(--accent); font-size: 13px; }
.chat-app .tg-typing { opacity: 0; transition: opacity 150ms ease-out; }
.chat-app .tg-typing.on { opacity: 1; }
.chat-app .tg-typing-dot {
  display: inline-block;
  width: 3px;
  height: 3px;
  margin-left: 2px;
  border-radius: 50%;
  background: currentColor;
  animation: tg-typing-dot 1.2s ease-out infinite;
}
.chat-app .tg-typing-dot:first-child { margin-left: 3px; }
.chat-app .tg-typing-dot:nth-child(2) { animation-delay: 200ms; }
.chat-app .tg-typing-dot:nth-child(3) { animation-delay: 400ms; }
.chat-app .tg-typing:not(.on) .tg-typing-dot { animation-play-state: paused; }
.chat-app .tg-bar-actions { position: relative; flex: none; }
.chat-app .tg-bar-menu {
  position: absolute;
  top: 44px;
  right: 0;
  z-index: 20;
  min-width: 180px;
  padding: 6px 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .25);
  transform-origin: right top;
  animation: tg-menu-in 150ms cubic-bezier(.2,.8,.2,1);
}
.chat-app .tg-bar-item {
  display: block;
  width: 100%;
  padding: 8px 16px;
  border: none;
  border-radius: 0;
  background: none;
  color: var(--text);
  text-align: left;
}
.chat-app .tg-bar-item:hover { background: var(--code-bg); text-decoration: none; }
.chat-app .tg-bar-item.danger { color: var(--danger); }
.chat-app .tg-bar-item[hidden] { display: none; }
.chat-app .tg-bar-item:disabled { opacity: .45; cursor: default; background: none; }
.chat-app .tg-ctx-menu {
  position: fixed;
  z-index: 30;
  min-width: 160px;
  padding: 6px 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .25);
  transform-origin: left top;
  animation: tg-menu-in 150ms cubic-bezier(.2,.8,.2,1);
}
.chat-app .tg-ctx-menu[hidden] { display: none; }
.chat-app .tg-msg-menu .tg-bar-item:focus-visible { background: var(--code-bg); outline: none; }
.chat-app .tg-warn { margin: 4px 0; color: var(--warn); font-size: 13px; }
/* A message's actions are in its menu, opened by a right-click, a long press
   or the menu key, so the bubble keeps its size. A finger's long press opens
   the menu rather than selecting text, which 复制 copies instead. */
.chat-app .tg-bubble:focus { outline: none; }
.chat-app .tg-bubble:focus-visible { box-shadow: 0 0 0 2px var(--accent); }
.chat-app .tg-bubble.tg-selected { filter: brightness(.9); }
.chat-app .tg-toast {
  position: absolute;
  left: 50%;
  bottom: 72px;
  z-index: 5;
  padding: 6px 14px;
  border-radius: 14px;
  background: var(--tg-pill);
  color: #ffffff;
  font-size: 13px;
  transform: translateX(-50%);
  pointer-events: none;
  animation: tg-fade-in 150ms ease-out;
}
.chat-app .tg-copy-buffer {
  position: fixed;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  font-size: 16px;
  opacity: 0;
  pointer-events: none;
}
@media (pointer: coarse) {
  .chat-app .tg-bubble { -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
  /* A finger picks a command from a row at least 44px high. */
  .chat-app .cmd-item,
  .chat-classic .cmd-item { min-height: 44px; align-items: center; align-content: center; }
}

.chat-app .tg-conv { position: relative; flex: 1; min-height: 0; display: flex; flex-direction: column; animation: tg-fade-in 150ms ease-out; }
.chat-app .tg-log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  list-style: none;
  margin: 0;
  padding: 8px calc(var(--tg-side) + 8px) 12px;
}
.chat-app .tg-log > :first-child { margin-top: auto; }
.chat-app .tg-note,
.chat-app .tg-day span {
  border-radius: 12px;
  background: var(--tg-pill);
  color: #ffffff;
  font-size: 13px;
}
.chat-app .tg-note { align-self: center; max-width: 460px; margin: 8px 0; padding: 6px 12px; text-align: center; }
.chat-app .tg-day { align-self: center; margin: 12px 0 4px; }
.chat-app .tg-day span { display: inline-block; padding: 2px 10px; }
.chat-app .tg-msg { display: flex; max-width: min(75%, 560px); margin-top: 8px; }
.chat-app .tg-msg.cont { margin-top: 2px; }
.chat-app .tg-msg.in { align-self: flex-start; }
.chat-app .tg-msg.out { align-self: flex-end; }
.chat-app .tg-msg.is-new { animation: tg-msg-in 200ms cubic-bezier(.2,.8,.2,1) backwards; }
.chat-app .tg-msg.in.is-new { transform-origin: left bottom; }
.chat-app .tg-msg.out.is-new { transform-origin: right bottom; }
.chat-app .tg-bubble {
  position: relative;
  display: flow-root;
  min-width: 0;
  padding: 6px 10px 7px 12px;
  border-radius: 14px;
  background: var(--tg-in);
  color: var(--text);
  box-shadow: 0 1px 1px rgba(0, 0, 0, .12);
  overflow-wrap: anywhere;
}
.chat-app .tg-msg.out .tg-bubble { background: var(--tg-out); color: #ffffff; }
.chat-app .tg-msg.in.cont .tg-bubble { border-top-left-radius: 6px; }
.chat-app .tg-msg.in:not(.tail) .tg-bubble { border-bottom-left-radius: 6px; }
.chat-app .tg-msg.in.tail .tg-bubble { border-bottom-left-radius: 0; }
.chat-app .tg-msg.out.cont .tg-bubble { border-top-right-radius: 6px; }
.chat-app .tg-msg.out:not(.tail) .tg-bubble { border-bottom-right-radius: 6px; }
.chat-app .tg-msg.out.tail .tg-bubble { border-bottom-right-radius: 0; }
.chat-app .tg-msg.tail .tg-bubble::after {
  content: "";
  position: absolute;
  bottom: 0;
  width: 9px;
  height: 14px;
}
.chat-app .tg-msg.in.tail .tg-bubble::after {
  left: -9px;
  background: radial-gradient(100% 100% at 0 0, transparent 98%, var(--tg-in) 100%);
}
.chat-app .tg-msg.out.tail .tg-bubble::after {
  right: -9px;
  background: radial-gradient(100% 100% at 100% 0, transparent 98%, var(--tg-out) 100%);
}
.chat-app .tg-text { white-space: pre-wrap; }
.chat-app .tg-time {
  float: right;
  margin: 9px -2px -4px 12px;
  color: var(--muted);
  font-size: 11px;
  line-height: 1;
  white-space: nowrap;
  user-select: none;
}
.chat-app .tg-msg.out .tg-time,
.chat-app .tg-msg.out .badge { color: var(--tg-out-meta); border-color: var(--tg-out-meta); }
.chat-app .tg-keys {
  align-self: flex-start;
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: min(75%, 560px);
  margin-top: 4px;
}
.chat-app .tg-keys button {
  width: 100%;
  padding: 8px 12px;
  border: none;
  border-radius: 10px;
  background: var(--tg-key);
  color: var(--tg-key-text);
  white-space: normal;
  text-align: center;
  transition: background-color 150ms ease-out, transform 150ms ease-out;
  animation: tg-fade-in 180ms ease-out backwards;
}
.chat-app .tg-keys button:nth-child(2) { animation-delay: 40ms; }
.chat-app .tg-keys button:nth-child(3) { animation-delay: 80ms; }
.chat-app .tg-keys button:nth-child(4) { animation-delay: 120ms; }
.chat-app .tg-keys button:hover { background: var(--tg-key-hover); }
.chat-app .tg-keys button:active { transform: scale(.97); }
.chat-app .tg-status { padding: 0 var(--tg-side); }
.chat-app .tg-status:empty { display: none; }
.chat-app .tg-status .hint { margin: 4px 0; }
.chat-app .tg-compose {
  flex: none;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 6px var(--tg-side) 12px;
}
.chat-app .tg-input {
  flex: 1;
  width: auto;
  min-width: 0;
  min-height: 0;
  padding: 9px 16px;
  border-radius: 20px;
  line-height: 20px;
  resize: none;
  overflow-y: hidden;
}
.chat-app .tg-input:focus { border-color: var(--accent); outline: none; }
.chat-app .tg-send {
  flex: none;
  width: 40px;
  height: 40px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--tg-out);
  color: #ffffff;
  font-size: 18px;
  line-height: 1;
  transition: opacity 150ms ease-out, transform 150ms ease-out;
}
.chat-app .tg-send:active:not(:disabled) { transform: scale(.92); }
/* The command menu (views/commandmenu.js) rises from the box, over the
   newest messages, about eight rows high before it scrolls. */
.chat-app .tg-compose { position: relative; }
.chat-app .cmd-menu {
  position: absolute;
  left: var(--tg-side);
  right: var(--tg-side);
  bottom: 100%;
  z-index: 20;
  max-height: min(320px, 40vh);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 6px 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .25);
  transform-origin: left bottom;
  animation: tg-menu-in 150ms cubic-bezier(.2,.8,.2,1);
}
.chat-app .cmd-item {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  column-gap: 8px;
  padding: 8px 16px;
  cursor: pointer;
}
.chat-app .cmd-item[aria-selected="true"] { background: var(--code-bg); }
.chat-app .cmd-name { font-weight: 600; white-space: nowrap; }
.chat-app .cmd-args { color: var(--muted); white-space: nowrap; }
.chat-app .cmd-desc { margin-left: auto; color: var(--muted); font-size: 13px; }

/* The page's motion: what arrives eases in, from below for a message, from
   its corner for a menu, from the side it comes from for a page on a narrow
   screen. With reduced motion asked for, nothing moves (see the end). */
@keyframes tg-fade-in {
  from { opacity: 0; }
}
@keyframes tg-msg-in {
  from { opacity: 0; transform: translateY(8px) scale(.98); }
}
@keyframes tg-menu-in {
  from { opacity: 0; transform: scale(.95); }
}
@keyframes tg-bump {
  0% { transform: scale(.6); }
  60% { transform: scale(1.12); }
  100% { transform: scale(1); }
}
@keyframes tg-typing-dot {
  0%, 60%, 100% { opacity: .3; transform: none; }
  30% { opacity: 1; transform: translateY(-2px); }
}
@keyframes tg-slide-left {
  from { opacity: 0; transform: translateX(40px); }
}
@keyframes tg-slide-right {
  from { opacity: 0; transform: translateX(-40px); }
}

.hint.warn { color: var(--warn); }
button.link.danger { color: var(--danger); }

dialog.dialog { width: min(420px, 92vw); margin: auto; color: var(--text); }
dialog.dialog.wide { width: min(560px, 92vw); }
dialog.dialog select, dialog.dialog input { width: 100%; }
dialog.dialog input[type=checkbox] { width: 16px; flex: none; }
dialog::backdrop { background: rgba(0, 0, 0, .4); }
dialog ul.names { max-height: 30vh; overflow-y: auto; margin: 4px 0 8px; padding-left: 20px; }
fieldset.skills { border: 1px solid var(--border); border-radius: 6px; padding: 8px 12px; margin: 0 0 10px; max-height: 40vh; overflow-y: auto; }
fieldset.skills legend { color: var(--muted); font-size: 12px; padding: 0 4px; }
fieldset.intimacy { border: none; margin: 0; padding: 0; min-width: 0; }
fieldset.intimacy:disabled { opacity: .6; }
.skill { display: flex; gap: 8px; align-items: flex-start; margin-bottom: 6px; }
.skill input[type=checkbox] { margin-top: 2px; }
.skill > span { display: flex; flex-direction: column; }

.login { max-width: 360px; margin: 10vh auto; }
.login input { width: 100%; }

.chart { margin: 0; }
.chart-svg { width: 100%; height: auto; display: block; }
.chart-svg .grid { stroke: var(--grid); stroke-width: 1; }
.chart-svg .axis { fill: var(--muted); font-size: 11px; }
.chart-svg .hit { fill: transparent; }
.chart-svg .bar:hover .hit { fill: var(--code-bg); }
.legend { list-style: none; display: flex; flex-wrap: wrap; gap: 4px 14px; padding: 0; margin: 6px 0 0; font-size: 12px; }
.legend li { display: flex; align-items: center; gap: 4px; }

details > summary { cursor: pointer; color: var(--accent); white-space: nowrap; }

@media (max-width: 699px) {
  main { padding: 12px; }
  .topbar { padding: 8px 12px; }
  .cols { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .cards { grid-template-columns: 1fr; }
  dl.kv { grid-template-columns: 1fr; }
  dl.kv dd { margin-bottom: 6px; }
  .chat-classic .chat-page { grid-template-columns: 1fr; }
  .chat-classic .chat-side { max-height: none; }
  .chat-app .tg { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr); }
  .chat-app .tg.open { grid-template-rows: minmax(0, 1fr); }
  .chat-app .tg-rail {
    flex-direction: row;
    align-items: center;
    overflow-x: auto;
    overflow-y: hidden;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .chat-app .tg-menu-button { padding: 8px 14px; }
  .chat-app .tg-folders { flex-direction: row; }
  .chat-app .tg-folder {
    flex-direction: row;
    padding: 10px 12px;
    border-bottom: 2px solid transparent;
    font-size: 14px;
    white-space: nowrap;
  }
  .chat-app .tg-folder.active { background: none; border-bottom-color: var(--accent); }
  .chat-app .tg-folder-icon,
  .chat-app .tg-folder .avatar { display: none; }
  .chat-app .tg-folder-pic { order: 1; }
  .chat-app .tg-folder .tg-folder-unread { position: static; box-shadow: none; }
  .chat-app .tg-folder-warn { position: static; display: inline-block; margin: auto 0 auto 4px; box-shadow: none; }
  .chat-app .tg-folder-name { max-width: 10em; }
  .chat-app .tg-menu { top: 40px; }
  .chat-app .tg-list { border-right: none; }
  .chat-app .tg:not(.open) .tg-pane { display: none; }
  .chat-app .tg.open .tg-rail,
  .chat-app .tg.open .tg-list { display: none; }
  .chat-app .tg.tg-enter-chat .tg-pane { animation: tg-slide-left 200ms cubic-bezier(.2,.8,.2,1); }
  .chat-app .tg.tg-enter-list .tg-rail,
  .chat-app .tg.tg-enter-list .tg-list { animation: tg-slide-right 200ms cubic-bezier(.2,.8,.2,1); }
  .chat-app .tg-pane { --tg-side: 8px; }
  .chat-app .tg-bar { gap: 8px; padding-left: 4px; }
  .chat-app .tg-bar-back { display: inline-flex; }
  .chat-app .tg-msg { max-width: 85%; }
  .chat-app .tg-keys { width: 85%; }
  input, select, textarea, textarea.json { font-size: 16px; }
}

/* With reduced motion asked for, the Telegram-style page does not move:
   正在输入's dots stand still, shown. */
@media (prefers-reduced-motion: reduce) {
  .chat-app *,
  .chat-app *::before,
  .chat-app *::after { animation: none !important; transition: none !important; }
  .chat-app .tg-typing-dot { opacity: 1; transform: none; }
}
