/* ───────────────────────────────────────────────
   Codex Relay — 样式
   调性：暖纸底 + 衬线标题 + 紫罗兰强调
   ─────────────────────────────────────────────── */
:root {
  --paper: #faf8f4;
  --paper-2: #ffffff;
  --ink: #1b1a17;
  --ink-2: #43413c;
  --muted: #767268;
  --faint: #a5a094;
  --line: #e6e1d6;
  --line-soft: #efeae0;
  --accent: #5b53d6;
  --accent-ink: #4539c4;
  --accent-soft: #eeecfb;
  --ok: #17845b;
  --warn: #b9700c;
  --err: #c0392b;
  --radius: 14px;
  --shadow-sm: 0 1px 2px rgba(27, 26, 23, .05), 0 1px 1px rgba(27, 26, 23, .03);
  --shadow-md: 0 8px 28px -12px rgba(27, 26, 23, .18), 0 2px 8px -4px rgba(27, 26, 23, .08);
  --serif: "Songti SC", "Noto Serif SC", "Source Han Serif SC", Georgia, "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif;
  --mono: "SF Mono", ui-monospace, "JetBrains Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

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

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.72;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* 纸纹理：极淡的双斑点，避免大面积纯色发闷 */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background-image:
    radial-gradient(circle at 18% 12%, rgba(91, 83, 214, .045), transparent 42%),
    radial-gradient(circle at 84% 78%, rgba(23, 132, 91, .035), transparent 46%);
}

button { font-family: inherit; cursor: pointer; }
input, textarea { font-family: inherit; font-size: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ───────────── 品牌 ───────────── */
.brand-mark { color: var(--accent); display: inline-flex; flex: none; }
.brand-mark.small { color: var(--accent); }

/* ───────────── 登录页 ───────────── */
.gate {
  position: relative;
  z-index: 1;
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 28px 20px 48px;
}

.gate-card {
  width: 100%;
  max-width: 420px;
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 30px 28px 22px;
  box-shadow: var(--shadow-md);
  animation: rise .45s cubic-bezier(.22, .9, .3, 1) both;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}

.brand { display: flex; gap: 14px; align-items: flex-start; margin-bottom: 26px; }
.brand h1 {
  font-family: var(--serif);
  font-size: 25px;
  font-weight: 700;
  letter-spacing: .01em;
  margin: 0 0 4px;
  line-height: 1.25;
}
.tagline { margin: 0; color: var(--muted); font-size: 13.5px; line-height: 1.5; }

.field { display: block; margin-bottom: 16px; }
.field-label {
  display: block;
  font-size: 12.5px;
  letter-spacing: .04em;
  color: var(--muted);
  margin-bottom: 7px;
  font-weight: 600;
}
.field-label em { font-style: normal; font-weight: 400; color: var(--faint); }

.field input {
  width: 100%;
  height: 46px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: #fdfcfa;
  transition: border-color .16s, box-shadow .16s, background .16s;
  outline: none;
}
.field input:focus {
  border-color: var(--accent);
  background: #fff;
  box-shadow: 0 0 0 3.5px rgba(91, 83, 214, .12);
}
.field input::placeholder { color: var(--faint); }

.btn-primary {
  width: 100%;
  height: 46px;
  border: 0;
  border-radius: 11px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: .02em;
  transition: background .16s, transform .12s, box-shadow .16s;
  box-shadow: 0 1px 2px rgba(69, 57, 196, .25), 0 6px 16px -8px rgba(69, 57, 196, .5);
}
.btn-primary:hover { background: var(--accent-ink); }
.btn-primary:active { transform: translateY(1px); }
.btn-primary:disabled { opacity: .55; cursor: not-allowed; transform: none; }
.btn-primary.small {
  width: auto;
  height: 36px;
  padding: 0 18px;
  font-size: 14px;
  border-radius: 9px;
  box-shadow: none;
}

.form-error {
  margin: 12px 0 0;
  font-size: 13.5px;
  color: var(--err);
  background: #fdf1ef;
  border: 1px solid #f6d9d4;
  border-radius: 9px;
  padding: 8px 11px;
}

.gate-teacher { margin: 10px 0 0; text-align: center; }
.link-btn {
  background: none; border: 0; padding: 4px 2px;
  font: inherit; font-size: 12.5px; color: var(--faint);
  cursor: pointer; text-decoration: underline; text-underline-offset: 3px;
}
.link-btn:hover { color: var(--accent-ink); }

.gate-foot {
  display: flex;
  align-items: center;
  gap: 7px;
  margin: 22px 0 0;
  padding-top: 15px;
  border-top: 1px solid var(--line-soft);
  font-size: 12.5px;
  color: var(--muted);
}

.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--faint);
  flex: none;
}
.dot.on { background: var(--ok); box-shadow: 0 0 0 3px rgba(23, 132, 91, .13); }
.dot.off { background: var(--warn); box-shadow: 0 0 0 3px rgba(185, 112, 12, .13); }

/* ───────────── 主界面 ───────────── */
.app {
  position: relative;
  z-index: 1;
  height: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.topbar {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px max(18px, env(safe-area-inset-left)) 12px max(18px, env(safe-area-inset-right));
  border-bottom: 1px solid var(--line);
  background: rgba(250, 248, 244, .86);
  backdrop-filter: saturate(180%) blur(12px);
  position: sticky;
  top: 0;
  z-index: 5;
}
.topbar-left { display: flex; align-items: center; gap: 11px; min-width: 0; }
.topbar-title { display: flex; flex-direction: column; min-width: 0; }
.topbar-title strong {
  font-family: var(--serif);
  font-size: 15.5px;
  font-weight: 700;
  letter-spacing: .01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.status { display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; color: var(--muted); }
.status .dot { width: 6px; height: 6px; }

.topbar-right { display: flex; gap: 6px; flex: none; }
.btn-ghost {
  height: 32px;
  padding: 0 11px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  transition: background .15s, color .15s, border-color .15s;
}
.btn-ghost:hover { background: #fff; border-color: var(--line); color: var(--ink); }

.offline-bar {
  flex: none;
  padding: 8px 18px;
  font-size: 13px;
  color: #7a4c00;
  background: #fdf4e3;
  border-bottom: 1px solid #f3e3c4;
  text-align: center;
}

/* ───────────── 消息流 ───────────── */
.stream {
  flex: 1 1 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 26px max(18px, env(safe-area-inset-left)) 8px max(18px, env(safe-area-inset-right));
  scroll-behavior: smooth;
}
.stream-inner { max-width: 760px; margin: 0 auto; }

.empty { max-width: 620px; margin: 6vh auto 0; text-align: center; }
.empty h2 {
  font-family: var(--serif);
  font-size: 27px;
  margin: 0 0 8px;
  letter-spacing: .01em;
}
.empty p { color: var(--muted); margin: 0 auto 26px; font-size: 14.5px; max-width: 420px; }
.samples { display: flex; flex-wrap: wrap; gap: 9px; justify-content: center; }
.sample {
  border: 1px solid var(--line);
  background: var(--paper-2);
  color: var(--ink-2);
  font-size: 13.5px;
  padding: 8px 14px;
  border-radius: 999px;
  transition: border-color .15s, color .15s, transform .12s, box-shadow .15s;
  box-shadow: var(--shadow-sm);
}
.sample:hover {
  border-color: #cfc8f5;
  color: var(--accent-ink);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px -6px rgba(69, 57, 196, .35);
}

.msg { margin-bottom: 22px; animation: msgIn .3s ease both; }
@keyframes msgIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.msg-user { display: flex; flex-direction: column; align-items: flex-end; }
.msg-user .bubble {
  max-width: 86%;
  background: var(--accent-soft);
  border: 1px solid #ded9f9;
  color: #2c2585;
  padding: 11px 15px;
  border-radius: 15px 15px 4px 15px;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 15px;
}
.msg-meta {
  font-size: 11.5px;
  color: var(--faint);
  margin-top: 6px;
  display: flex;
  gap: 8px;
  align-items: center;
}

.msg-ai .card {
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 4px 15px 15px 15px;
  padding: 16px 19px 12px;
  box-shadow: var(--shadow-sm);
  position: relative;
}
.msg-ai .card::before {
  content: "";
  position: absolute;
  left: -1px; top: 14px; bottom: 14px;
  width: 2.5px;
  border-radius: 2px;
  background: linear-gradient(180deg, var(--accent), #9d8cf0);
  opacity: .8;
}
.ai-head {
  display: flex; align-items: center; gap: 7px;
  font-size: 11.5px; color: var(--faint);
  margin-bottom: 9px; letter-spacing: .03em;
}
.ai-head .badge {
  font-weight: 600; color: var(--accent-ink);
  background: var(--accent-soft);
  border-radius: 5px; padding: 1px 6px; font-size: 10.5px;
}

.answer-body { font-size: 15.4px; line-height: 1.78; color: var(--ink); overflow-wrap: break-word; }
.answer-body > *:first-child { margin-top: 0; }
.answer-body > *:last-child { margin-bottom: 0; }
.answer-body p { margin: 0 0 .78em; }
.answer-body h1, .answer-body h2, .answer-body h3, .answer-body h4 {
  font-family: var(--serif);
  line-height: 1.4;
  margin: 1.25em 0 .5em;
}
.answer-body h1 { font-size: 21px; }
.answer-body h2 { font-size: 18.5px; }
.answer-body h3 { font-size: 16.5px; }
.answer-body ul, .answer-body ol { margin: 0 0 .85em; padding-left: 1.45em; }
.answer-body li { margin-bottom: .3em; }
.answer-body li::marker { color: var(--faint); }
.answer-body strong { font-weight: 700; color: #0f0e0c; }
.answer-body a { color: var(--accent-ink); text-underline-offset: 2px; }
.answer-body blockquote {
  margin: 0 0 .9em; padding: 2px 0 2px 14px;
  border-left: 2.5px solid var(--line);
  color: var(--ink-2);
}
.answer-body code {
  font-family: var(--mono);
  font-size: .875em;
  background: #f3f1ea;
  border: 1px solid var(--line-soft);
  border-radius: 5px;
  padding: 1.5px 5px;
}
.answer-body pre {
  background: #f7f5ef;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 13px 15px;
  overflow-x: auto;
  margin: 0 0 .95em;
  line-height: 1.6;
}
.answer-body pre code { background: none; border: 0; padding: 0; font-size: 13px; }
.answer-body table { border-collapse: collapse; width: 100%; margin: 0 0 1em; font-size: 14.5px; }
.answer-body th, .answer-body td { border: 1px solid var(--line); padding: 7px 11px; text-align: left; }
.answer-body th { background: #f8f6f0; font-weight: 600; }
.answer-body hr { border: 0; border-top: 1px solid var(--line); margin: 1.3em 0; }
.answer-body .katex-display { margin: .8em 0; overflow-x: auto; overflow-y: hidden; padding: 2px 0; }

/* 独立公式块：手机上公式比屏幕宽时可以横向滑动，不会撑破气泡 */
.answer-body .math-block {
  margin: 1.05em 0;
  overflow-x: auto;
  overflow-y: hidden;
  text-align: center;
  padding: 1px 0;
  -webkit-overflow-scrolling: touch;
}
.answer-body .math-block .katex-display { margin: 0; }
.answer-body .math-block.inline {
  display: inline-block;
  margin: 0 .12em;
  max-width: 100%;
  overflow-x: auto;
  vertical-align: baseline;
}
.answer-body .math-raw {
  display: block;
  text-align: left;
  font-family: var(--mono);
  font-size: 13px;
  white-space: pre-wrap;
  word-break: break-word;
  background: #f7f5ef;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--ink-2);
}

.ai-foot {
  display: flex; align-items: center; gap: 12px;
  margin-top: 10px; padding-top: 9px;
  border-top: 1px dashed var(--line-soft);
  font-size: 11.5px; color: var(--faint);
}
.copy-btn {
  border: 0; background: none; padding: 2px 0;
  color: var(--faint); font-size: 11.5px;
  transition: color .15s;
}
.copy-btn:hover { color: var(--accent-ink); }

/* 思考中 */
.thinking { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; padding: 3px 0 5px; }
.pulse { display: inline-flex; gap: 4px; }
.pulse i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent); opacity: .35;
  animation: blink 1.25s infinite ease-in-out;
}
.pulse i:nth-child(2) { animation-delay: .16s; }
.pulse i:nth-child(3) { animation-delay: .32s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; transform: scale(.9); } 40% { opacity: 1; transform: scale(1); } }

.msg-err .card { border-color: #f0cfc9; background: #fdf5f3; }
.msg-err .answer-body { color: #8e2f22; font-size: 14px; }

/* ───────────── 输入区 ───────────── */
.composer-wrap {
  flex: none;
  padding: 10px max(18px, env(safe-area-inset-left)) max(14px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-right));
  background: linear-gradient(180deg, rgba(250, 248, 244, 0), var(--paper) 28%);
}
.composer {
  max-width: 760px;
  margin: 0 auto;
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 11px 13px 9px;
  box-shadow: var(--shadow-md);
  transition: border-color .16s, box-shadow .16s;
}
.composer:focus-within {
  border-color: #c9c2f2;
  box-shadow: 0 8px 30px -14px rgba(69, 57, 196, .4), 0 0 0 3.5px rgba(91, 83, 214, .07);
}
.composer textarea {
  width: 100%;
  border: 0;
  outline: none;
  resize: none;
  background: transparent;
  font-size: 15.5px;
  line-height: 1.65;
  max-height: 190px;
  padding: 2px 2px 6px;
}
.composer textarea::placeholder { color: var(--faint); }
.composer-bar { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.hint { font-size: 11.5px; color: var(--faint); }
.composer-actions { display: flex; align-items: center; gap: 10px; }
.counter { font-size: 11.5px; color: var(--faint); font-variant-numeric: tabular-nums; }
.quota { font-size: 11.5px; color: var(--accent-ink); font-variant-numeric: tabular-nums; }
.quota.empty { color: var(--err); }


/* ───────────── 附件 ───────────── */
.attach { display: flex; gap: 4px; }
.btn-attach {
  display: inline-flex; align-items: center; gap: 5px;
  height: 30px; padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fdfcfa;
  color: var(--ink-2);
  font-size: 12.5px;
  transition: border-color .15s, color .15s, background .15s;
}
.btn-attach:hover { border-color: #cfc8f5; color: var(--accent-ink); background: #fff; }
.btn-attach svg { opacity: .75; }

.thumbs {
  display: flex; gap: 8px; flex-wrap: wrap;
  padding: 2px 2px 10px;
}
.thumb {
  position: relative;
  width: 62px; height: 62px;
  border-radius: 9px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: #f4f2ec;
  flex: none;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb-x {
  position: absolute; top: 2px; right: 2px;
  width: 18px; height: 18px;
  border: 0; border-radius: 50%;
  background: rgba(20, 18, 15, .62);
  color: #fff; font-size: 12px; line-height: 18px;
  padding: 0; text-align: center;
  backdrop-filter: blur(2px);
}
.thumb-x:hover { background: rgba(20, 18, 15, .85); }

/* 消息里的图片 */
.msg-user .shots {
  display: flex; gap: 7px; flex-wrap: wrap;
  justify-content: flex-end;
  margin-bottom: 7px;
}
.msg-user .shots img {
  width: 108px; height: 108px;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid #ded9f9;
  cursor: zoom-in;
  background: #fff;
  transition: transform .14s, box-shadow .14s;
}
.msg-user .shots img:hover { transform: scale(1.02); box-shadow: 0 6px 16px -8px rgba(27, 26, 23, .4); }

.foot-note {
  max-width: 760px;
  margin: 8px auto 0;
  text-align: center;
  font-size: 11px;
  color: var(--faint);
  line-height: 1.5;
}

/* 图片灯箱 */
.lightbox {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(16, 15, 13, .86);
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  animation: fadeIn .16s ease both;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.lightbox img {
  max-width: 100%; max-height: 100%;
  border-radius: 8px;
  box-shadow: 0 24px 60px -20px rgba(0, 0, 0, .6);
}
.lb-close {
  position: absolute; top: max(16px, env(safe-area-inset-top)); right: 18px;
  width: 38px; height: 38px;
  border: 0; border-radius: 50%;
  background: rgba(255, 255, 255, .14);
  color: #fff; font-size: 22px; line-height: 1;
}
.lb-close:hover { background: rgba(255, 255, 255, .24); }

/* ───────────── 响应式 ───────────── */
@media (max-width: 640px) {
  body { font-size: 15.5px; }
  .gate-card { padding: 26px 20px 18px; border-radius: 16px; }
  .brand h1 { font-size: 22px; }
  .stream { padding-top: 18px; }
  .msg-user .bubble { max-width: 92%; }
  .msg-ai .card { padding: 14px 15px 10px; }
  .hint { display: none; }
  .topbar { padding-top: 10px; padding-bottom: 10px; }
  .topbar-title strong { font-size: 14.5px; }
  .empty { margin-top: 3vh; }
  .empty h2 { font-size: 23px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}
