:root {
  --primary: #1677ff;
  --bg: #f0f2f5;
  --panel: #fff;
  --text: #1f2329;
  --muted: #8a8f99;
  --border: #e5e6eb;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: #dfe3ea;
  -webkit-text-size-adjust: 100%;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 760px;
  margin: 0 auto;
  background: var(--bg);
}
@media (min-width: 760px) {
  .app { box-shadow: 0 0 24px rgba(0, 0, 0, .08); }
}

/* Header */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--primary);
  color: #fff;
}
.avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255, 255, 255, .2);
  color: #fff;
}
.avatar svg { width: 20px; height: 20px; }
.title-block { flex: 1; min-width: 0; }
.title-block h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.status { display: flex; align-items: center; gap: 4px; margin: 2px 0 0; font-size: 12px; opacity: .85; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: #52c41a; }
.top-btn {
  flex-shrink: 0;
  padding: 5px 11px;
  border: 1px solid rgba(255, 255, 255, .6);
  border-radius: 16px;
  background: transparent;
  color: #fff;
  font-size: 13px;
  cursor: pointer;
}
.top-btn:hover { background: rgba(255, 255, 255, .15); }
.top-btn:disabled { opacity: .5; cursor: default; }

/* Messages */
.messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 12px;
}
.row { display: flex; align-items: flex-start; gap: 8px; margin-bottom: 14px; }
.row.user { justify-content: flex-end; }
.row .avatar { width: 32px; height: 32px; background: var(--primary); }
.row .avatar svg { width: 18px; height: 18px; }
.bubble {
  max-width: calc(100% - 48px);
  padding: 10px 13px;
  border-radius: 12px;
  font-size: 15px;
  line-height: 1.65;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.row.assistant .bubble { background: var(--panel); border-top-left-radius: 4px; box-shadow: 0 1px 2px rgba(0, 0, 0, .05); }
.row.user .bubble { max-width: 80%; background: var(--primary); color: #fff; border-top-right-radius: 4px; white-space: pre-wrap; }
.row.assistant .bubble.error { background: #fff2f0; color: #cf1322; }
.error-note { margin-top: 8px; font-size: 13px; color: #cf1322; }

/* Markdown inside replies */
.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }
.bubble p, .bubble ul, .bubble ol, .bubble blockquote, .bubble pre, .bubble .table-wrap { margin: 0 0 8px; }
.bubble h4 { margin: 10px 0 6px; font-size: 15px; }
.bubble ul, .bubble ol { padding-left: 20px; }
.bubble li { margin: 2px 0; }
.bubble li > ul, .bubble li > ol { margin: 2px 0; }
.bubble a { color: var(--primary); word-break: break-all; }
.bubble code {
  padding: 1px 5px;
  border-radius: 4px;
  background: #f2f3f5;
  font-family: SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
}
.bubble pre { padding: 10px; border-radius: 6px; background: #f6f8fa; overflow-x: auto; }
.bubble pre code { padding: 0; background: none; }
.bubble blockquote { margin-left: 0; padding: 4px 10px; border-left: 3px solid #d0d7de; background: #f8f9fb; color: #57606a; }
.bubble hr { margin: 10px 0; border: 0; border-top: 1px solid var(--border); }
.bubble img {
  display: block;
  max-width: 100%;
  max-height: 320px;
  margin: 8px 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: zoom-in;
}
.bubble-images { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.row.user .bubble img { max-width: 100%; max-height: 200px; margin: 0; border: 0; }
.expired { padding: 4px 8px; border-radius: 6px; background: rgba(255, 255, 255, .2); font-size: 13px; }
.table-wrap { overflow-x: auto; }
.bubble table { border-collapse: collapse; font-size: 14px; }
.bubble th, .bubble td { padding: 5px 8px; border: 1px solid var(--border); text-align: left; }
.bubble th { background: #f7f8fa; }

/* Typing indicator */
.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: #b8bec8; animation: blink 1.2s infinite ease-in-out; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink {
  0%, 80%, 100% { opacity: .3; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

/* Quick questions */
.quick { display: flex; flex-wrap: wrap; gap: 8px; margin: -4px 0 14px 40px; }
.chip {
  padding: 6px 12px;
  border: 1px solid #b7d3ff;
  border-radius: 16px;
  background: #fff;
  color: var(--primary);
  font-size: 13px;
  text-align: left;
  cursor: pointer;
}
.chip:hover { background: #eef5ff; }

/* Composer */
.composer {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--panel);
}
.composer textarea {
  flex: 1;
  max-height: 120px;
  padding: 9px 14px;
  border: 0;
  border-radius: 18px;
  outline: none;
  resize: none;
  background: #f2f3f5;
  color: var(--text);
  font: inherit;
  font-size: 16px; /* below 16px iOS zooms the page on focus */
  line-height: 22px;
}
.send-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
}
.send-btn svg { width: 18px; height: 18px; }
.send-btn:disabled { background: #a8c8ff; cursor: default; }
.input-row { display: flex; align-items: flex-end; gap: 8px; }
.attach-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.attach-btn svg { width: 24px; height: 24px; }
.attach-btn:hover { color: var(--primary); }
.notice { color: #cf1322; font-size: 13px; }

/* Screenshots picked for the next message */
.attachments { display: flex; flex-wrap: wrap; gap: 8px; }
.thumb { position: relative; width: 64px; height: 64px; border-radius: 8px; overflow: hidden; background: #f2f3f5; }
.thumb img { display: block; width: 100%; height: 100%; object-fit: cover; }
.thumb-state {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  padding: 2px 0;
  background: rgba(0, 0, 0, .55);
  color: #fff;
  font-size: 11px;
  text-align: center;
}
.thumb.error .thumb-state { background: rgba(207, 19, 34, .85); }
.thumb-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, .55);
  color: #fff;
  font-size: 14px;
  line-height: 20px;
  cursor: pointer;
}

/* Overlay: image preview and contact card */
.overlay {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(0, 0, 0, .6);
}
.overlay[hidden] { display: none; }
.overlay-body { display: flex; max-width: 100%; max-height: 100%; }
.overlay-body .preview { max-width: 100%; max-height: calc(100vh - 40px); border-radius: 6px; background: #fff; cursor: zoom-out; }
.card { width: 300px; max-width: 100%; padding: 20px; border-radius: 12px; background: #fff; text-align: center; }
.card h2 { margin: 0 0 6px; font-size: 17px; }
.card p { margin: 0 0 12px; color: var(--muted); font-size: 13px; line-height: 1.6; }
.card img { width: 100%; max-height: 60vh; border-radius: 6px; object-fit: contain; }
.card .close {
  width: 100%;
  margin-top: 12px;
  padding: 9px;
  border: 0;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  font-size: 14px;
  cursor: pointer;
}
