:root {
  --bg: #edf1f7;
  --surface: #ffffff;
  --canvas: #f7f9fc;
  --ink: #18212f;
  --muted: #65728a;
  --line: #e1e7f0;
  --brand: #2358d8;
  --brand-dark: #1b45ac;
  --brand-soft: #e9f0ff;
  --ok: #1f9d63;
  --danger: #b42318;
  --notice-bg: #fff7e5;
  --notice-line: #f0d28b;
  --notice-ink: #6f4700;
}

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

html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 760px;
  margin: 0 auto;
  background: var(--surface);
}
@media (min-width: 800px) {
  body { padding: 24px; }
  .app { height: calc(100dvh - 48px); border-radius: 20px; overflow: hidden; box-shadow: 0 14px 44px rgba(24, 40, 72, .14); }
}

/* header */
.chat-header {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; padding-top: max(12px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-text h1 { margin: 0; font-size: 1.02rem; font-weight: 650; letter-spacing: -.005em; }
.status { margin: 0; font-size: .8rem; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); }
.avatar {
  flex: none; width: 32px; height: 32px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--brand-soft); color: var(--brand);
}
.avatar-lg { width: 42px; height: 42px; background: var(--brand); color: #fff; }
.header-actions { display: flex; align-items: center; gap: 8px; }
.timer {
  font-size: .8rem; color: var(--muted); font-variant-numeric: tabular-nums;
  padding: 5px 10px; border-radius: 999px; background: var(--canvas); border: 1px solid var(--line);
}
.btn-ghost {
  font: inherit; font-size: .875rem; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: 999px; padding: 6px 14px; cursor: pointer;
}
.btn-ghost:hover { background: var(--canvas); }
.btn-ghost:disabled { opacity: .5; cursor: default; }

/* messages */
.messages {
  flex: 1; overflow-y: auto; padding: 20px 16px;
  display: flex; flex-direction: column; gap: 10px;
  background: var(--canvas); overscroll-behavior: contain;
}
.row { display: flex; align-items: flex-end; gap: 8px; animation: rise .2s ease-out; }
.row-user { justify-content: flex-end; }
.bubble {
  max-width: min(78%, 520px); padding: 10px 14px; border-radius: 18px;
  white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.45;
}
.row-bot .bubble { background: var(--surface); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.row-user .bubble { background: var(--brand); color: #fff; border-bottom-right-radius: 6px; }
.bubble-img-wrap { margin-bottom: 6px; }
.bubble-thumb {
  max-width: 220px; max-height: 180px; border-radius: 10px; display: block;
  object-fit: cover; border: 1px solid rgba(0, 0, 0, .1); cursor: pointer;
}
.row-user .bubble-thumb { border-color: rgba(255, 255, 255, .25); }
.bubble-text { white-space: pre-wrap; overflow-wrap: anywhere; }

.row-notice { justify-content: center; }
.notice-box {
  max-width: 92%; padding: 12px 14px; border-radius: 12px; font-size: .9rem;
  background: var(--notice-bg); border: 1px solid var(--notice-line); color: var(--notice-ink);
}

.typing { display: inline-flex; gap: 5px; padding: 14px 16px; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: #9aa6bb; animation: bounce 1s infinite ease-in-out; }
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }

/* quick replies */
.actions {
  display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 16px;
  border-top: 1px solid var(--line); background: var(--surface);
  max-height: 38vh; overflow-y: auto;
}
.chip {
  font: inherit; font-size: .95rem; cursor: pointer; color: var(--brand);
  background: var(--surface); border: 1.5px solid var(--brand); border-radius: 999px; padding: 9px 16px;
  transition: background .15s, color .15s;
}
.chip:hover { background: var(--brand-soft); }
.chip.primary { background: var(--brand); color: #fff; }
.chip.primary:hover { background: var(--brand-dark); }
.chip:focus-visible, .btn-ghost:focus-visible, .send:focus-visible { outline: 3px solid rgba(35, 88, 216, .35); outline-offset: 2px; }

.error { margin: 0; padding: 6px 16px 0; font-size: .875rem; color: var(--danger); background: var(--surface); }

/* attachment preview */
.attachment-preview {
  padding: 8px 16px 0; background: var(--surface);
}
.preview-card {
  display: inline-flex; align-items: center; gap: 10px; padding: 6px 12px;
  background: var(--canvas); border: 1px solid var(--line); border-radius: 10px;
  max-width: 100%;
}
.preview-card img {
  width: 36px; height: 36px; border-radius: 6px; object-fit: cover; border: 1px solid var(--line); flex: none;
}
.preview-info { min-width: 0; display: flex; flex-direction: column; font-size: .78rem; }
.preview-name { font-weight: 600; color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 180px; }
.preview-size { color: var(--muted); }
.btn-remove {
  flex: none; width: 22px; height: 22px; border: 0; border-radius: 50%;
  background: var(--surface); color: var(--muted); font-size: 1rem; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; line-height: 1;
  border: 1px solid var(--line); margin-left: 4px;
}
.btn-remove:hover { background: var(--danger); color: #fff; border-color: var(--danger); }

/* composer */
.composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px 16px 6px; background: var(--surface); }
.attach-btn {
  flex: none; width: 44px; height: 44px; border: 1px solid var(--line); border-radius: 50%; cursor: pointer;
  background: var(--surface); color: var(--muted); display: inline-flex; align-items: center; justify-content: center;
  transition: background .15s, color .15s, border-color .15s;
}
.attach-btn:hover:not(:disabled) { background: var(--canvas); color: var(--brand); border-color: var(--brand); }
.attach-btn:disabled { opacity: .4; cursor: default; }
.attach-btn:focus-visible { outline: 3px solid rgba(35, 88, 216, .35); outline-offset: 2px; }
.field { flex: 1; min-width: 0; }
textarea {
  display: block; width: 100%; resize: none; max-height: 120px; font: inherit;
  padding: 11px 14px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); color: var(--ink);
}
textarea:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(35, 88, 216, .15); }
textarea:disabled { background: var(--canvas); color: var(--muted); }
.counter { display: block; margin-top: 3px; padding-right: 4px; text-align: right; font-size: .72rem; color: var(--muted); }
.send {
  flex: none; width: 44px; height: 44px; border: 0; border-radius: 50%; cursor: pointer;
  background: var(--brand); color: #fff; display: inline-flex; align-items: center; justify-content: center;
}
.send:hover:not(:disabled) { background: var(--brand-dark); }
.send:disabled { opacity: .4; cursor: default; }
.footnote { margin: 0; padding: 4px 16px 10px; padding-bottom: max(10px, env(safe-area-inset-bottom)); text-align: center; font-size: .75rem; color: var(--muted); background: var(--surface); }
.noscript { padding: 1rem; text-align: center; }

@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-4px); opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
  .row { animation: none; }
  .typing span { animation: none; opacity: .7; }
}
