:root {
  font: 16px/1.55 system-ui, "Microsoft YaHei", sans-serif;
  color-scheme: light;
  --paper: #f7f4ef;
  --surface: #fffdf9;
  --ink: #302720;
  --muted: #6d6055;
  --accent: #6f5038;
  --line: #e8ddd0;
  --soft: #f3ebe0;
  --gold: #f0ce9a;
  background: var(--paper);
  color: var(--ink);
}

:root[data-support-theme="dark"] {
  color-scheme: dark;
  --paper: #251f1c;
  --surface: #312824;
  --ink: #fff6ed;
  --muted: #dac8b8;
  --line: #5b493c;
  --soft: #44362d;
  --accent: #8e6244;
}

* { box-sizing: border-box; }
body { margin: 0; }
button, input, textarea { font: inherit; }
button {
  min-height: 44px;
  padding: .55rem .9rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
}
button:disabled { opacity: .55; cursor: wait; }
input, textarea {
  width: 100%;
  min-width: 0;
  padding: .65rem .8rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--ink);
}
:is(button, input, textarea, a):focus-visible { outline: 3px solid #bb8a5b; outline-offset: 2px; }
[hidden] { display: none !important; }
h1, h2, p { margin: 0; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1.12rem; }
.primary { background: var(--accent); color: white; border-color: var(--accent); font-weight: 650; }
.shell {
  max-width: 1050px;
  margin: 26px auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: 0 12px 40px #4f372417;
  overflow: hidden;
}
.site-topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  min-height: 66px;
  gap: 10px;
  padding: 8px clamp(16px, 3vw, 48px);
  background: linear-gradient(110deg, #73563b, #533b2d);
  color: #fff6eb;
}
.site-topbar a { text-decoration: none; color: inherit; }
.home-link {
  justify-self: start;
  border: 1px solid #ffffff4a;
  border-radius: 9px;
  padding: .48rem .8rem;
  color: var(--gold) !important;
}
.site-brand { font-size: 1.2rem; font-weight: 750; white-space: nowrap; }
.site-tools { display: flex; justify-content: flex-end; align-items: center; gap: 12px; }
#site-date { font-size: .84rem; white-space: nowrap; }
#theme-toggle { border-color: transparent; background: transparent; color: inherit; font-size: 1.35rem; padding: 0 9px; }
.panel-header {
  display: flex;
  align-items: center;
  gap: 12px;
  background: #654730;
  color: #fff5e9;
  padding: 18px 24px;
}
.logo { display: grid; place-items: center; color: var(--gold); font-size: 1.6rem; }
.visitor .panel-header { padding-block: 13px; }
.visitor-heading {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  min-height: 64px;
}
.visitor-heading h1 { text-align: center; }
#visitor-back { justify-self: start; display: grid; place-items: center; min-width: 44px; min-height: 44px; color: inherit; font-size: 1.8rem; line-height: 1; text-decoration: none; }
.support-banner { display: block; width: min(100%, 680px); height: auto; margin-inline: auto; aspect-ratio: 3 / 1; object-fit: cover; }
.welcome-card {
  display: grid;
  gap: 16px;
  margin: 24px;
  border-radius: 16px;
  padding: 24px;
  border: 1px solid var(--line);
  background: var(--soft);
}
.welcome-card p { margin-top: .4rem; color: var(--muted); }
#start { justify-self: center; width: min(100%, 370px); min-height: 48px; }
.chat-intro { margin: 20px 24px 8px; padding: 14px 18px; background: var(--soft); border: 1px solid var(--line); border-radius: 14px; }
.chat-intro p + p { margin-top: .3rem; font-size: .87rem; color: var(--muted); }
.visitor-page.chat-active .visitor {
  display: flex;
  flex-direction: column;
  min-height: min(700px, calc(100dvh - 114px));
}
.visitor-page.chat-active #messages { flex: 1; min-height: 92px; }
.visitor-page.chat-active #compose { margin-top: auto; background: var(--paper); }
#status { padding: 9px 24px; color: var(--muted); font-size: .88rem; background: var(--paper); overflow-wrap: anywhere; }
#status:empty { display: none; }
#messages {
  padding: 16px 24px;
  min-height: 120px;
  max-height: 54dvh;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 13px;
}
.bubble { align-self: flex-start; background: var(--soft); padding: 10px 14px; border-radius: 15px 15px 15px 5px; max-width: 88%; white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble.mine { align-self: flex-end; border-radius: 15px 15px 5px 15px; color: white; background: var(--accent); }
.bubble small { display: block; font-size: .75rem; opacity: .82; margin-bottom: 4px; }
.failed-message {
  display: flex;
  align-items: flex-end;
  align-self: flex-end;
  gap: 8px;
  max-width: 100%;
}
.failed-message .bubble { min-width: 0; }
.failed-message-label { flex: none; color: #a52f26; font-size: .78rem; padding-bottom: 3px; }
:root[data-support-theme="dark"] .failed-message-label { color: #ff9d94; }
#compose { padding: 16px 24px; border-top: 1px solid var(--line); background: var(--surface); }
label { display: block; margin-bottom: 7px; font-weight: 600; font-size: .9rem; }
.compose-row { display: flex; gap: 10px; align-items: flex-end; }
.compose-row button { flex: 0 0 auto; }
textarea { resize: vertical; min-height: 56px; max-height: 150px; }
#compose small { display: block; color: var(--muted); margin-top: 7px; font-size: .78rem; }
.agent-brand small { color: var(--gold); font-size: .76rem; letter-spacing: .08em; }
.agent-actions { display: flex; margin-left: auto; align-items: center; gap: 6px; }
.agent-actions button { background: #ffffff12; color: #fff7ec; border-color: #ffffff30; font-size: .88rem; }
#login { max-width: 410px; margin: auto; padding: 24px; }
#login button { width: 100%; margin-top: 12px; }
#push-controls { display: flex; align-items: center; gap: 12px; padding: 12px 24px; border-bottom: 1px solid var(--line); }
#push-controls button { flex: 0 0 auto; }
#push-status { color: var(--muted); font-size: .84rem; }
.agent-actions #push-controls { display: block; padding: 0; border: 0; }
.agent-actions #toggle-push { font-size: .78rem; padding: .3rem .5rem; }
.agent-actions #push-status {
  position: absolute;
  width: 1px;
  height: 1px;
  clip-path: inset(50%);
  white-space: nowrap;
  overflow: hidden;
}
#attachment-preview { display: flex; align-items: center; gap: 8px; margin: 0 0 8px; }
#attachment-image { max-width: 96px; max-height: 58px; object-fit: contain; border-radius: 6px; }
#attach-image { flex: 0 0 auto; }
.attach-image-plus { display: none; }
.message-image {
  display: block;
  width: min(100%, 16rem);
  height: auto;
  max-height: 22rem;
  object-fit: contain;
  margin-block-start: 5px;
  border-radius: 8px;
}
.message-image:not([src]) { min-height: 3rem; }
.bubble:has(.message-image) { width: min(88%, 18rem); }
.reply-to-message { display: block; min-height: 36px; margin-top: 6px; padding: .2rem .55rem; font-size: .78rem; }
.reply-to-message[aria-pressed="true"] { background: var(--accent); color: white; }
#workspace { display: grid; grid-template-columns: minmax(270px, 36%) minmax(0, 1fr); }
.inbox { padding: 16px; border-right: 1px solid var(--line); min-width: 0; }
.topics { display: flex; flex-wrap: wrap; gap: 6px; padding: 10px 0; }
.topics button { font-size: .84rem; padding-inline: .68rem; }
.topics button[aria-pressed="true"] { color: white; background: var(--accent); border-color: var(--accent); }
#refresh { margin-left: auto; }
#conversations { display: grid; gap: 7px; max-height: 68dvh; overflow: auto; }
.inbox-empty { padding: 15px 6px; color: var(--muted); }
.conversation-item { text-align: left; width: 100%; min-width: 0; padding: 11px 12px; }
.conversation-item.selected { background: var(--soft); border-color: #aa7e59; }
.conversation-item-header { display: flex; justify-content: space-between; align-items: baseline; gap: .75rem; }
.conversation-item-header strong { font-size: .96rem; }
.conversation-item-header time { flex: 0 0 auto; color: var(--muted); font-size: .75rem; }
.conversation-preview { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .85rem; color: var(--muted); margin-top: .22rem; }
.unread-label { margin-left: .45rem; color: var(--accent); font-size: .74rem; }
.conversation-heading { display: flex; align-items: center; gap: 10px; padding: 14px 24px; border-bottom: 1px solid var(--line); }
#back { display: none; }
dialog { width: min(24rem, calc(100% - 32px)); padding: 24px; border: 1px solid var(--line); border-radius: 17px; background: var(--surface); color: var(--ink); }
dialog::backdrop { background: #1d140f7d; }
dialog form { display: flex; justify-content: flex-end; margin-top: 12px; }
.settings-option { display: flex; align-items: center; gap: 9px; margin-top: 14px; }
.settings-option input { width: auto; }

@media (min-width: 621px) and (max-width: 1100px) {
  .shell { margin-inline: 20px; }
}
@media (max-width: 620px) {
  .shell { margin: 0; border: 0; border-radius: 0; min-height: calc(100dvh - 58px); box-shadow: none; }
  .agent-page .shell { min-height: 100dvh; }
  .site-topbar { min-height: 58px; gap: 5px; padding-inline: 12px; }
  .site-brand { font-size: 1rem; }
  .site-tools { gap: 3px; }
  #site-date { font-size: .68rem; }
  #theme-toggle { padding-inline: 4px; }
  .home-link { padding: .42rem .58rem; font-size: .85rem; }
  .panel-header { padding: 13px 16px; }
  .agent-actions { gap: 3px; }
  .agent-actions button { padding-inline: .45rem; font-size: .82rem; white-space: nowrap; }
  .agent-page .panel-header { gap: 7px; }
  .agent-page .agent-brand { min-width: 0; }
  .support-banner { aspect-ratio: 2 / 1; object-position: 48% center; }
  .welcome-card { margin: 15px; padding: 18px; gap: 14px; }
  .chat-intro { margin: 14px 16px 8px; }
  .visitor-page.chat-active .visitor { min-height: calc(100dvh - 58px); }
  #status, #push-controls { padding-inline: 16px; }
  #workspace { display: block; }
  .inbox { border: 0; padding: 12px 16px; }
  #conversations { max-height: none; }
  #workspace article { display: none; }
  #workspace.chatting aside { display: none; }
  #workspace.chatting article { display: block; }
  #back { display: block; }
  .conversation-heading, #messages, #compose { padding-inline: 16px; }
  #compose { padding-bottom: max(16px, env(safe-area-inset-bottom)); }
  .agent-page.agent-chat-open { overflow: hidden; }
  .agent-chat-open .shell {
    position: fixed;
    inset: var(--agent-chat-top, 0px) 0 auto;
    display: flex;
    flex-direction: column;
    height: var(--agent-chat-height, 100dvh);
    min-height: 0;
  }
  .agent-chat-open .shell > :not(#workspace) { flex: 0 0 auto; }
  .agent-chat-open #workspace.chatting,
  .agent-chat-open #workspace.chatting article { display: flex; flex: 1; flex-direction: column; min-height: 0; }
  .agent-chat-open .conversation-heading,
  .agent-chat-open #compose { flex: 0 0 auto; }
  .agent-chat-open #messages {
    flex: 1;
    min-height: 0;
    max-height: none;
    overscroll-behavior-y: contain;
  }
  .agent-chat-open .bubble { flex: 0 0 auto; }
  .agent-chat-open #compose {
    padding: 12px 10px max(14px, env(safe-area-inset-bottom)) 6px;
  }
  .agent-chat-open #compose label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
  }
  .agent-chat-open .compose-row { align-items: center; gap: 7px; }
  .agent-chat-open #attach-image {
    display: grid;
    place-items: center;
    width: 44px;
    height: 46px;
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--accent);
  }
  .agent-chat-open .attach-image-label { display: none; }
  .agent-chat-open .attach-image-plus { display: block; font-size: 2rem; font-weight: 600; line-height: 1; }
  .agent-chat-open #message {
    height: 48px;
    min-height: 48px;
    padding: 12px;
    border-radius: 12px;
    resize: none;
    font-size: 16px;
    line-height: 22px;
  }
  .agent-chat-open .compose-row .primary {
    min-width: 64px;
    min-height: 46px;
    padding: 0 .7rem;
    border-radius: 12px;
  }
  .agent-chat-open.agent-chat-typing #status { padding-block: 2px; font-size: .75rem; }
  .agent-chat-open.agent-chat-typing #compose { padding-block: 6px; }
  .agent-chat-open.agent-chat-typing .panel-header,
  .agent-chat-open.agent-chat-typing .conversation-heading { padding-block: 4px; }
}
@media (max-width: 350px) {
  #site-date { display: none; }
  .agent-page .panel-header .logo { display: none; }
  .agent-page .agent-brand h1 { font-size: 1rem; }
}
