/* The chat page. Chrome 80 level CSS: variables, flexbox, no nesting, no :has(). */

:root {
  --bg: #f4f2ee;
  --surface: #ffffff;
  --text: #1d1d1b;
  --muted: #6b6b66;
  --line: #dedbd4;
  --accent: #2f6f5e;
  --accent-text: #ffffff;
  --mine: #dff0e8;
  --code-bg: #f1efe9;
  --danger: #b3261e;
  --notice: #fff4d6;
  --tap: 44px;
  --radius: 14px;
  --app-h: 100%;
  --app-top: 0px;
  font-size: 17px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161816;
    --surface: #212421;
    --text: #e8e6e1;
    --muted: #a3a19b;
    --line: #3a3d39;
    --accent: #5fb39a;
    --accent-text: #0d1a15;
    --mine: #23392f;
    --code-bg: #2a2d29;
    --danger: #f2a49d;
    --notice: #3b3420;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Noto Sans CJK SC", "PingFang SC", "Microsoft YaHei", sans-serif;
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

#app {
  position: fixed;
  left: 0;
  right: 0;
  top: var(--app-top);
  height: var(--app-h);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

.offscreen {
  position: absolute;
  left: -9999px;
  width: 1px;
  height: 1px;
  opacity: 0;
}

/* ---------- buttons ---------- */

.button {
  min-height: var(--tap);
  min-width: var(--tap);
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}
.button:disabled { opacity: 0.5; cursor: default; }
.button-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.button-quiet { border-color: transparent; background: transparent; color: var(--accent); padding: 0 10px; }
.button-small { padding: 0 12px; font-size: 15px; }
.button-icon { padding: 0; width: var(--tap); font-size: 20px; flex: none; }
.button-stop { color: var(--danger); border-color: var(--danger); min-height: var(--tap); }

/* ---------- boot and login ---------- */

.boot { margin: auto; color: var(--muted); padding: 24px; text-align: center; }

.login {
  margin: auto;
  width: 100%;
  max-width: 420px;
  padding: 24px 20px;
  overflow-y: auto;
}
.login-title { font-size: 26px; margin: 0 0 12px; }
.login-text { color: var(--muted); margin: 0 0 16px; }
.login-form { display: grid; grid-gap: 12px; }
.login-input {
  width: 100%;
  font: inherit;
  font-size: 16px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  resize: none;
}
.login-error { color: var(--danger); margin: 12px 0 0; }

/* ---------- chat frame ---------- */

.chat, .panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
}

.panel {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg);
  z-index: 20;
}

.topbar {
  flex: none;
  display: flex;
  align-items: center;
  padding: 4px 6px;
  padding-top: calc(4px + env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.topbar > * + * { margin-left: 4px; }
.topbar-title {
  flex: 1;
  min-width: 0;
  margin: 0 6px;
  font-size: 18px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.notice {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: var(--notice);
  font-size: 15px;
}
.notice > * + * { margin-left: 8px; }
/* Looking at someone else's conversation: impossible to mistake for one's own. */
.notice-family { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.family-text { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.notice-actions { flex: none; display: flex; }
.notice-actions > * + * { margin-left: 6px; }

.list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 8px 10px 12px;
}

.older, .empty { text-align: center; color: var(--muted); font-size: 15px; padding: 12px; }

.conv-start {
  display: flex;
  align-items: center;
  color: var(--muted);
  font-size: 14px;
  margin: 8px 0 12px;
}
.conv-start::before { margin-right: 10px; }
.conv-start::after { margin-left: 10px; }
.conv-start::before, .conv-start::after {
  content: "";
  flex: 1;
  border-top: 1px solid var(--line);
}

.jump {
  position: absolute;
  right: 14px;
  bottom: 84px;
  z-index: 5;
  min-height: var(--tap);
  padding: 0 16px;
  border-radius: 22px;
  border: none;
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
  font-size: 15px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

/* ---------- messages ---------- */

.messages { display: flex; flex-direction: column; }
.messages > * + * { margin-top: 10px; }

.msg { display: flex; flex-direction: column; max-width: 100%; min-width: 0; }
.msg-me { align-items: flex-end; }
.msg-bot { align-items: stretch; }

.bubble {
  max-width: 100%;
  min-width: 0;
  padding: 8px 12px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--line);
}
.msg-me .bubble { max-width: 88%; background: var(--mine); border-color: transparent; }
.msg-failed .bubble { border-color: var(--danger); }
.msg-pending .bubble { opacity: 0.75; }

.meta {
  display: flex;
  align-items: center;
  padding: 2px 6px 0;
  font-size: 12px;
  color: var(--muted);
  user-select: none;
  -webkit-user-select: none;
}
.meta > * + * { margin-left: 8px; }
.sender { font-weight: 600; color: var(--text); }
.msg-failed .pending-status { color: var(--danger); }

/* Message text: selectable, wraps, never widens the page. */
.body {
  user-select: text;
  -webkit-user-select: text;
  overflow-wrap: anywhere;
  word-break: break-word;
  min-width: 0;
}
.body-plain { white-space: pre-wrap; }
.body > :first-child { margin-top: 0; }
.body > :last-child { margin-bottom: 0; }
.body p, .body ul, .body ol, .body blockquote, .body .table-wrap, .body .code-block, .body details { margin: 0.5em 0; }
.body ul, .body ol { padding-left: 1.4em; }
.body h1, .body h2, .body h3, .body h4, .body h5, .body h6 { font-size: 1.05em; margin: 0.7em 0 0.3em; }
.body h1 { font-size: 1.2em; }
.body h2 { font-size: 1.12em; }
.body blockquote { margin-left: 0; padding-left: 10px; border-left: 3px solid var(--line); color: var(--muted); }
.body hr { border: none; border-top: 1px solid var(--line); }
.body a { color: var(--accent); }
.body a:not([href]) { color: inherit; }
.body img { max-width: 100%; height: auto; }

.body code {
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Noto Sans Mono CJK SC", monospace;
  font-size: 0.88em;
  background: var(--code-bg);
  border-radius: 4px;
  padding: 0.1em 0.3em;
}

/* Code: a copy button in its own row above the code, so it never covers a line. */
.code-block {
  display: flex;
  flex-direction: column;
  background: var(--code-bg);
  border-radius: 8px;
  min-width: 0;
}
.body pre {
  margin: 0;
  padding: 10px 12px;
  background: var(--code-bg);
  border-radius: 8px;
  overflow-x: auto;
  max-width: 100%;
  -webkit-overflow-scrolling: touch;
}
.body pre code { background: none; padding: 0; white-space: pre; overflow-wrap: normal; word-break: normal; }
.copy-button {
  align-self: flex-end;
  min-height: var(--tap);
  min-width: var(--tap);
  padding: 0 10px;
  border: none;
  background: transparent;
  color: var(--accent);
  font: inherit;
  font-size: 14px;
  user-select: none;
  -webkit-user-select: none;
}
.code-block pre { padding-top: 0; }

/* Formulas: MathML from KaTeX (js/math.js). The vendored font has an OpenType MATH table, which
   Chromium's MathML layout needs for radicals, big operators and stretchy brackets. A displayed
   formula is centred and, when wider than the bubble, scrolls sideways in its own box (inline
   math inside it, so the overflow starts at the left edge and stays reachable). */
@font-face {
  font-family: "ZCB Math";
  src: url("/static/vendor/noto-sans-math.woff2") format("woff2");
  font-display: swap;
}
.body math {
  font-family: "ZCB Math", math;
  font-size: 1.08em;
  color: inherit;
}
.body .math-display {
  display: block;
  max-width: 100%;
  margin: 0.5em 0;
  padding: 0.25em 0;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  text-align: center;
  white-space: nowrap;
}
.body .math-display > math { display: inline math; }
.body .math-source { overflow-wrap: anywhere; white-space: pre-wrap; text-align: left; }
.body span.math-display.math-source { display: inline; }
.body annotation, .body annotation-xml { display: none; }

.table-wrap {
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.body table { border-collapse: collapse; font-size: 0.95em; }
.body th, .body td {
  border: 1px solid var(--line);
  padding: 4px 8px;
  vertical-align: top;
  overflow-wrap: normal;
  word-break: normal;
  /* A narrow phone would squeeze every column to a character or two; keep columns readable
     and let the table scroll sideways in its box instead. */
  min-width: 6.5em;
}
.body th { background: var(--code-bg); }
.align-left { text-align: left; }
.align-center { text-align: center; }
.align-right { text-align: right; }

.body details.spoiler {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
}
.body details.spoiler > summary {
  min-height: var(--tap);
  display: flex;
  align-items: center;
  cursor: pointer;
  color: var(--muted);
  list-style: none;
}
.body details.spoiler > summary::-webkit-details-marker { display: none; }
.body details.spoiler > summary::before { content: "▸"; margin-right: 8px; }
.body details.spoiler[open] > summary::before { content: "▾"; }
.spoiler-body { padding-bottom: 8px; }

.upload-image { display: inline-block; max-width: 100%; }
.upload-image img { display: block; max-width: 100%; max-height: 60vh; border-radius: 8px; }

.file-chip {
  display: inline-flex;
  align-items: center;
  max-width: 100%;
  min-height: var(--tap);
  padding: 0 12px;
  margin: 2px 0;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: var(--surface);
  color: var(--text) !important;
  text-decoration: none;
}
.file-chip > * + * { margin-left: 6px; }
.file-chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- busy, composer, attachments ---------- */

.busy {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 12px;
  color: var(--muted);
  font-size: 15px;
  border-top: 1px solid var(--line);
  background: var(--bg);
}
.busy > * + * { margin-left: 8px; }
.dots span { animation: blink 1.4s infinite both; }
.dots span:nth-child(2) { animation-delay: 0.2s; }
.dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.2; } 40% { opacity: 1; } }

.composer {
  flex: none;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding: 6px 6px;
  padding-bottom: calc(6px + env(safe-area-inset-bottom));
}
.composer-row { display: flex; align-items: flex-end; }
.composer-row > * + * { margin-left: 6px; }
.text {
  flex: 1;
  min-width: 0;
  min-height: var(--tap);
  max-height: 40vh;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 17px;
  line-height: 1.45;
  resize: none;
  overflow-y: auto;
}
.button-send { flex: none; border-radius: 22px; }

.attachments { display: grid; grid-gap: 4px; grid-template-columns: minmax(0, 1fr); padding: 0 0 6px; }
.attachment {
  display: flex;
  align-items: center;
  padding-left: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 15px;
}
.attachment > * + * { margin-left: 8px; }
.attachment-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attachment-bar { width: 80px; flex: none; }
.attachment-done .attachment-bar { display: none; }
.attachment-done .attachment-name::after { content: " ✓"; color: var(--accent); }
.attachment-failed { border-color: var(--danger); }
.attachment-failed .attachment-name::after { content: " 没传上去"; color: var(--danger); }
.attachment-remove { border: none; background: transparent; font-size: 18px; }

/* ---------- history panel ---------- */

.panel-body { flex: 1; overflow-y: auto; padding: 8px 10px 24px; }
.history-list { list-style: none; margin: 0; padding: 0; display: grid; grid-gap: 8px; grid-template-columns: minmax(0, 1fr); }
.history-list li { min-width: 0; }
.history-item {
  width: 100%;
  min-height: var(--tap);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  text-align: left;
}
.history-head { font-weight: 600; }
.history-preview {
  color: var(--muted);
  font-size: 15px;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.button-logout { margin-top: 24px; width: 100%; color: var(--muted); }

/* ---------- confirm sheet, image viewer, toast ---------- */

.sheet {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: 40;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0.4);
}
.sheet-box {
  width: 100%;
  max-width: 480px;
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  padding: 18px 16px;
  padding-bottom: calc(18px + env(safe-area-inset-bottom));
}
.sheet-text { margin: 0 0 16px; }
.sheet-actions { display: flex; }
.sheet-actions > * + * { margin-left: 10px; }
.sheet-actions .button { flex: 1; }

.viewer {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: 50;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
}
.viewer-image { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer-close { position: absolute; top: calc(10px + env(safe-area-inset-top)); right: 10px; }

.toast {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 96px;
  z-index: 60;
  padding: 10px 14px;
  border-radius: 10px;
  background: rgba(30, 30, 30, 0.92);
  color: #fff;
  font-size: 15px;
  text-align: center;
}
