:root {
  color-scheme: light;
  --paper: #f6f3ec;
  --surface: #fffdf8;
  --ink: #20231f;
  --muted: #757a72;
  --line: #dedbd2;
  --accent: #315d4a;
  --accent-soft: #e2eee7;
  --user: #315d4a;
  --danger: #9d3d35;
  --shadow: 0 14px 40px rgb(43 47 42 / 12%);
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; background: var(--paper); color: var(--ink); }

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

button, textarea { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

.login-screen { position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; padding: 22px; background: var(--paper); }
.login-screen[hidden] { display: none; }
.login-card { width: min(100%, 390px); padding: 28px; border: 1px solid var(--line); border-radius: 24px; background: var(--surface); box-shadow: var(--shadow); }
.login-card .agent-mark { margin-bottom: 18px; }
.login-card h1 { margin: 0 0 8px; font: 600 25px/1.3 Georgia, "Songti SC", serif; }
.login-card p { margin: 0 0 18px; color: var(--muted); line-height: 1.6; }
.login-card label { display: grid; gap: 6px; margin: 13px 0; color: var(--muted); font-size: 13px; }
.login-card input { width: 100%; min-height: 46px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: white; font: inherit; }
.login-card button { width: 100%; min-height: 46px; margin-top: 8px; border: 0; border-radius: 12px; background: var(--accent); color: white; font-weight: 700; }

.app-shell {
  width: min(100%, 760px);
  height: 100dvh;
  min-height: 520px;
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.topbar {
  min-height: 64px;
  padding: calc(8px + env(safe-area-inset-top)) 14px 8px;
  display: grid;
  grid-template-columns: 48px 1fr 48px;
  align-items: center;
  border-bottom: 1px solid var(--line);
  background: rgb(255 253 248 / 92%);
  backdrop-filter: blur(18px);
  z-index: 4;
}

.identity { text-align: center; }
.identity h1 { margin: 0; font-family: Georgia, "Songti SC", serif; font-size: 18px; font-weight: 600; letter-spacing: .08em; }
.identity p { margin: 1px 0 0; display: flex; justify-content: center; align-items: center; gap: 5px; color: var(--muted); font-size: 11px; }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: #c29641; }
.status-dot.ready { background: #4b8668; }

.icon-button, .composer-button {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ink);
  display: grid;
  place-items: center;
  cursor: pointer;
}
.icon-button:hover, .composer-button:hover { background: #efebe2; }
.icon-button:focus-visible, .composer-button:focus-visible, .send-button:focus-visible, textarea:focus-visible { outline: 3px solid #8aab9b; outline-offset: 2px; }

.message-stream {
  overflow-y: auto;
  padding: 22px 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}

.welcome-message {
  margin: auto 0;
  padding: 28px 22px;
  display: flex;
  gap: 14px;
  align-items: flex-start;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: linear-gradient(145deg, #fffefb, #f0eee6);
}
.agent-mark {
  flex: 0 0 42px;
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 14px;
  background: var(--accent);
  color: white;
  font-family: Georgia, "Songti SC", serif;
  font-size: 20px;
}
.welcome-message h2 { margin: 1px 0 7px; font-family: Georgia, "Songti SC", serif; font-size: clamp(20px, 6vw, 27px); font-weight: 500; line-height: 1.28; }
.welcome-message p { margin: 0; color: var(--muted); line-height: 1.65; font-size: 14px; }

.message { max-width: 86%; padding: 11px 14px; border-radius: 18px; line-height: 1.55; white-space: pre-wrap; overflow-wrap: anywhere; }
.message.user { align-self: flex-end; background: var(--user); color: white; border-bottom-right-radius: 5px; }
.message.assistant { align-self: flex-start; background: #eeece5; border-bottom-left-radius: 5px; }
.message.note { align-self: center; max-width: 94%; padding: 7px 12px; background: transparent; color: var(--muted); font-size: 12px; text-align: center; }

.confirmation-card { align-self: stretch; padding: 14px; border: 1px solid #c9b36c; border-radius: 16px; background: #fff9e8; }
.confirmation-card h3 { margin: 0 0 7px; font-size: 15px; }
.confirmation-card pre { margin: 0 0 12px; white-space: pre-wrap; overflow-wrap: anywhere; font: inherit; font-size: 13px; line-height: 1.5; }
.confirmation-actions { display: flex; gap: 8px; }
.confirmation-actions button { min-height: 42px; flex: 1; border: 1px solid var(--accent); border-radius: 12px; background: white; color: var(--accent); font-weight: 650; }
.confirmation-actions button.confirm { background: var(--accent); color: white; }
.confirmation-actions button:disabled { opacity: .55; }

.attachment-tray {
  padding: 8px 12px;
  gap: 7px;
  overflow-x: auto;
  display: flex;
  border-top: 1px solid var(--line);
  background: #faf8f2;
}
.attachment-tray[hidden] { display: none; }
.attachment-chip { flex: 0 0 auto; max-width: 280px; padding: 7px 9px; display: grid; grid-template-columns: minmax(0, 1fr) auto auto; align-items: center; gap: 5px 7px; border: 1px solid var(--line); border-radius: 12px; background: white; }
.attachment-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; }
.attachment-chip small { grid-column: 1 / -1; color: var(--muted); font-size: 11px; }
.attachment-chip small.failed { color: var(--danger); }
.attachment-chip audio { grid-column: 1 / -1; width: 230px; height: 34px; }
.attachment-chip button { width: 28px; height: 28px; border: 0; border-radius: 50%; background: #eeeae1; cursor: pointer; }
.transcript-editor { grid-column: 1 / -1; display: grid; gap: 7px; width: 250px; }
.transcript-editor small { white-space: normal; line-height: 1.5; }
.transcript-editor label { display: grid; gap: 4px; color: var(--muted); font-size: 11px; }
.transcript-editor textarea { min-height: 72px; padding: 8px; border: 1px solid var(--line); border-radius: 9px; background: white; }
.transcript-editor button { width: auto; min-height: 34px; padding: 0 11px; border-radius: 9px; background: var(--accent); color: white; justify-self: start; }

.recording-panel {
  min-height: 52px;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  border-top: 1px solid var(--line);
  background: #fff7f4;
  color: var(--danger);
  font-size: 13px;
}
.recording-panel[hidden] { display: none; }
.recording-panel button { min-height: 36px; padding: 0 10px; border: 1px solid #d9c8c3; border-radius: 18px; background: white; color: var(--ink); }
.recording-panel button:last-child { margin-left: auto; border-color: var(--danger); color: var(--danger); }
.recording-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--danger); animation: pulse 1.2s ease-in-out infinite; }

.composer {
  padding: 9px 10px calc(9px + env(safe-area-inset-bottom));
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 44px auto;
  gap: 7px;
  align-items: end;
  border-top: 1px solid var(--line);
  background: rgb(255 253 248 / 96%);
  backdrop-filter: blur(18px);
}
.add-button { background: #efebe2; font-size: 25px; font-weight: 300; }
.mic-button { background: var(--accent-soft); color: var(--accent); font-size: 15px; }
.mic-button.recording { background: #f3d9d5; color: var(--danger); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(.9); opacity: .65; } }

.message-input-wrap { min-height: 44px; padding: 10px 13px; display: flex; align-items: center; border: 1px solid var(--line); border-radius: 22px; background: white; }
textarea { width: 100%; max-height: 128px; resize: none; border: 0; outline: 0; background: transparent; color: var(--ink); line-height: 1.4; }
textarea::placeholder { color: #9b9e98; }
.send-button { min-width: 54px; min-height: 44px; padding: 0 13px; border: 0; border-radius: 22px; background: var(--accent); color: white; font-weight: 650; cursor: pointer; }
.send-button:disabled { opacity: .45; cursor: default; }

.drawer-backdrop { position: fixed; inset: 0; background: rgb(23 27 23 / 28%); z-index: 8; }
.drawer {
  position: fixed;
  inset: 0 auto 0 0;
  width: min(86vw, 350px);
  padding: calc(14px + env(safe-area-inset-top)) 16px 18px;
  background: var(--surface);
  box-shadow: var(--shadow);
  transform: translateX(-105%);
  transition: transform .2s ease;
  z-index: 9;
}
.drawer.open { transform: translateX(0); }
.drawer-head { min-height: 48px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--line); }
.drawer-head button { min-width: 44px; min-height: 44px; border: 0; background: transparent; color: var(--accent); cursor: pointer; }
.drawer-empty { color: var(--muted); line-height: 1.6; }
.settings-list { margin: 0; }
.settings-list div { padding: 13px 0; display: flex; justify-content: space-between; gap: 16px; border-bottom: 1px solid var(--line); }
.settings-list dt { color: var(--muted); }
.settings-list dd { margin: 0; text-align: right; }
.routing-settings { padding-top: 18px; }
.routing-settings h2 { margin: 0 0 8px; font-size: 15px; }
.routing-settings p { margin: 0 0 14px; color: var(--muted); font-size: 13px; line-height: 1.5; }
.routing-settings label { display: grid; gap: 6px; margin: 12px 0; color: var(--muted); font-size: 13px; }
.routing-settings select, .routing-settings button { width: 100%; border-radius: 12px; font: inherit; }
.routing-settings select { border: 1px solid var(--line); background: var(--surface); color: var(--text); padding: 10px; }
.routing-settings button { border: 0; background: var(--accent); color: white; padding: 11px; font-weight: 700; }

.toast { position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); max-width: min(88vw, 460px); padding: 9px 13px; border-radius: 12px; background: #272b27; color: white; font-size: 12px; opacity: 0; pointer-events: none; transform: translate(-50%, 8px); transition: .18s; z-index: 12; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

@media (min-width: 761px) {
  body { padding: 24px; }
  .app-shell { height: calc(100dvh - 48px); border: 1px solid var(--line); border-radius: 24px; overflow: hidden; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
