:root {
  --app-height: 100dvh;
  --app-offset-top: 0px;
  --page: #e9eaec;
  --chat: #f1f1f1;
  --panel: #ffffff;
  --ink: #171717;
  --muted: #9a9a9a;
  --line: #dedede;
  --blue: #4f678a;
  --green: #07c160;
  --red: #e94b4b;
  --shadow: 0 18px 52px rgba(0, 0, 0, .12);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  background: var(--page);
  font-family: "Microsoft YaHei", "PingFang SC", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, textarea, a { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }

.app-shell {
  width: min(100%, 980px);
  height: 100vh;
  height: 100dvh;
  margin: 0 0 0 auto;
  display: grid;
  grid-template-rows: 78px minmax(0, 1fr) auto;
  overflow: hidden;
  background: var(--chat);
  border-radius: 0;
  box-shadow: var(--shadow);
}

.chat-header {
  position: relative;
  display: grid;
  grid-template-columns: 64px 1fr 64px;
  align-items: center;
  background: rgba(247, 247, 247, .96);
  border-bottom: 1px solid #d8d8d8;
  backdrop-filter: blur(16px);
  z-index: 2;
}
.header-title { text-align: center; }
.header-title h1 { margin: 0; font-size: 24px; line-height: 1.25; font-weight: 650; letter-spacing: .01em; }
.header-title p { margin: 3px 0 0; color: #777; font-size: 13px; }
.icon-button { width: 48px; height: 48px; padding: 0; color: #111; background: transparent; border: 0; }
.header-spacer { width: 48px; height: 48px; }
.back-button { margin-left: 6px; font-family: Arial, sans-serif; font-size: 42px; font-weight: 300; line-height: 1; }
.menu-button { justify-self: end; margin-right: 8px; font-size: 22px; letter-spacing: 4px; }

.chat-viewport { min-height: 0; padding: 38px clamp(22px, 5vw, 66px) 22px; overflow-y: auto; scroll-behavior: smooth; }
.welcome-area { max-width: 820px; margin: 0 auto; }
.message { max-width: 820px; margin: 0 auto 24px; display: flex; align-items: flex-start; gap: 16px; }
.message.user { justify-content: flex-end; }
.avatar { width: 56px; height: 56px; flex: 0 0 56px; object-fit: cover; border-radius: 8px; box-shadow: 0 0 0 1px rgba(0,0,0,.04); }
.message-body { min-width: 0; max-width: min(72%, 620px); }
.bubble {
  position: relative;
  padding: 15px 18px;
  color: #151515;
  background: white;
  border-radius: 7px;
  font-size: 18px;
  line-height: 1.7;
  overflow-wrap: anywhere;
  box-shadow: 0 1px 1px rgba(0,0,0,.025);
}
.assistant .bubble::before {
  content: "";
  position: absolute;
  top: 17px;
  left: -9px;
  width: 0;
  height: 0;
  border-top: 8px solid transparent;
  border-bottom: 8px solid transparent;
  border-right: 10px solid white;
}
.user .message-body { max-width: min(72%, 620px); }
.user .bubble { background: #95ec69; }
.user .bubble::after {
  content: "";
  position: absolute;
  top: 17px;
  right: -9px;
  width: 0;
  height: 0;
  border-top: 8px solid transparent;
  border-bottom: 8px solid transparent;
  border-left: 10px solid #95ec69;
}
.message-images { margin: 0 0 7px; display: grid; grid-template-columns: repeat(3, minmax(0, 150px)); gap: 7px; justify-content: end; }
.message-images img { width: 100%; max-height: 190px; display: block; object-fit: cover; border: 1px solid #d7d7d7; border-radius: 8px; background: white; }
.bubble a { color: #356da5; text-decoration: none; }
.error-bubble { color: #9a2727; background: #fff1f1; }
.assistant .error-bubble::before { border-right-color: #fff1f1; }
.typing-message { margin-bottom: 18px; }
.typing-message .bubble {
  padding: 8px 2px;
  color: #a6a6a6;
  background: transparent;
  box-shadow: none;
  font-size: 14px;
  line-height: 1.5;
}
.typing-message .bubble::before { display: none; }

.quick-questions {
  margin: 18px 0 34px 72px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}
.quick-questions button {
  min-height: 54px;
  padding: 11px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  color: #2c5e8b;
  text-align: left;
  background: rgba(255,255,255,.9);
  border: 1px solid #d9e0e6;
  border-radius: 9px;
}
.quick-questions button:hover { background: #fff; border-color: #b6c8d8; }
.quick-questions span { width: 24px; height: 24px; flex: 0 0 24px; display: grid; place-items: center; color: white; background: #6687a7; border-radius: 50%; font-size: 12px; }
.chat-list { padding-bottom: 8px; }

.verdict-controls { display: none; margin-top: 8px; align-items: center; gap: 7px; color: #777; font-size: 12px; }
body.review-mode .verdict-controls { display: flex; }
.verdict-controls button { padding: 6px 10px; color: #555; background: #fff; border: 1px solid #ddd; border-radius: 7px; }
.verdict-controls button.active[data-verdict="pass"] { color: #087c43; border-color: #73bd98; background: #ecf8f2; }
.verdict-controls button.active[data-verdict="fail"] { color: #a22e2e; border-color: #df9e9e; background: #fff0f0; }
.verdict-controls input { width: min(270px, 45vw); padding: 7px 9px; border: 1px solid #ddd; border-radius: 7px; }

.composer-shell { background: rgba(249,249,249,.98); border-top: 1px solid #ddd; }
.attachment-tray { padding: 10px 20px 0; display: flex; gap: 10px; overflow-x: auto; }
.attachment-tray[hidden] { display: none; }
.attachment-tray figure { position: relative; width: 92px; margin: 0; flex: 0 0 92px; }
.attachment-tray img { width: 92px; height: 76px; display: block; object-fit: cover; border: 1px solid #d5d5d5; border-radius: 8px; background: white; }
.attachment-tray figcaption { margin-top: 3px; overflow: hidden; color: #777; font-size: 10px; text-overflow: ellipsis; white-space: nowrap; }
.attachment-tray button { position: absolute; top: 4px; right: 4px; padding: 3px 6px; color: #555; background: rgba(255,255,255,.92); border: 1px solid #d8d8d8; border-radius: 5px; font-size: 10px; }
.composer {
  min-height: 86px;
  padding: 14px 20px max(14px, env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  gap: 12px;
  background: transparent;
}
.input-wrap { min-width: 0; flex: 1; padding: 12px 14px; background: white; border-radius: 8px; }
.composer textarea { width: 100%; max-height: 110px; padding: 0; display: block; resize: none; outline: 0; border: 0; background: transparent; font-size: 17px; line-height: 1.45; }
.composer textarea::placeholder { color: #b6b6b6; }
.icon-tool { width: 42px; height: 42px; flex: 0 0 42px; padding: 8px; display: grid; place-items: center; color: #3d3d3d; background: transparent; border: 0; border-radius: 50%; }
.icon-tool svg { width: 27px; height: 27px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.icon-tool:hover { background: #ececec; }
.icon-tool:disabled { color: #aaa; cursor: default; }
.send-button { display: none; min-width: 62px; height: 42px; padding: 0 14px; color: white; background: var(--green); border: 0; border-radius: 6px; font-weight: 600; }
.composer.has-content .send-button { display: block; }
.send-button:disabled { opacity: .58; cursor: default; }

.drawer-mask { position: fixed; inset: 0; background: rgba(0,0,0,.28); z-index: 10; }
.login-mask { position: fixed; inset: 0; padding: 20px; display: grid; place-items: center; background: rgba(0,0,0,.42); z-index: 30; }
.login-mask[hidden] { display: none; }
.login-card { width: min(390px, 100%); padding: 24px; background: white; border-radius: 14px; box-shadow: 0 20px 60px rgba(0,0,0,.28); }
.login-card h2 { margin: 0 0 8px; font-size: 21px; }
.login-card p { margin: 0 0 18px; color: #777; font-size: 13px; line-height: 1.6; }
.login-card label { display: grid; gap: 7px; color: #555; font-size: 13px; }
.login-card input { width: 100%; min-height: 44px; padding: 9px 11px; border: 1px solid #cfd6dc; border-radius: 8px; font-size: 16px; }
.login-actions { margin-top: 18px; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.login-actions button { min-height: 42px; border-radius: 8px; }
.admin-drawer {
  position: fixed;
  top: 0;
  right: 0;
  width: min(430px, 92vw);
  height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom);
  overflow-y: auto;
  background: #f5f6f7;
  box-shadow: -16px 0 46px rgba(0,0,0,.16);
  transform: translateX(105%);
  transition: transform .22s ease;
  z-index: 11;
}
.admin-drawer[hidden] { display: none; }
.admin-drawer.open { transform: translateX(0); }
.drawer-header { padding: 24px; display: flex; align-items: flex-start; justify-content: space-between; color: white; background: #263d50; }
.drawer-header p { margin: 0 0 5px; color: #bfd0dc; font-size: 12px; }
.drawer-header h2 { margin: 0; font-size: 21px; }
.drawer-header-actions { display: flex; align-items: center; gap: 8px; }
.drawer-logout { min-height: 34px; padding: 0 10px; color: white; background: rgba(255,255,255,.12); border: 0; border-radius: 7px; }
.drawer-close { width: 38px; height: 38px; padding: 0; color: white; background: rgba(255,255,255,.12); border: 0; border-radius: 50%; font-size: 27px; }
.admin-section { margin: 12px; padding: 17px; background: white; border-radius: 12px; box-shadow: 0 2px 9px rgba(0,0,0,.04); }
.section-heading { margin-bottom: 13px; display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.section-heading h3 { margin: 0; font-size: 15px; }
.status { padding: 5px 9px; border-radius: 99px; font-size: 11px; }
.status.pending { color: #6a6a6a; background: #eee; }
.status.ok { color: #0a7040; background: #e3f6ec; }
.status.error { color: #a22d2d; background: #ffe8e8; }
.config-list, .debug-list { margin: 0; }
.config-list div, .debug-list div { padding: 8px 0; display: grid; grid-template-columns: 82px 1fr; gap: 10px; border-top: 1px solid #eee; }
.config-list dt, .debug-list dt { color: #888; font-size: 12px; }
.config-list dd, .debug-list dd { margin: 0; text-align: right; overflow-wrap: anywhere; font-family: Consolas, monospace; font-size: 11px; }
.summary-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.summary-grid div { padding: 12px 5px; text-align: center; background: #f5f7f8; border-radius: 9px; }
.summary-grid strong { display: block; font-size: 21px; }
.summary-grid span { color: #888; font-size: 11px; }
.admin-note { margin: 13px 0; color: #777; font-size: 12px; line-height: 1.65; }
.date-range { margin: 13px 0 8px; display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.date-range label { display: grid; gap: 5px; color: #666; font-size: 11px; }
.date-range input { width: 100%; min-height: 39px; padding: 7px 8px; border: 1px solid #d5dde2; border-radius: 7px; background: white; font-size: 13px; }
.range-preview { min-height: 19px; margin: 7px 0 12px; color: #64727c; font-size: 11px; line-height: 1.55; }
.admin-actions { display: grid; gap: 8px; }
.admin-actions button, .wide-button { min-height: 42px; padding: 10px 12px; display: grid; place-items: center; text-decoration: none; border-radius: 8px; cursor: pointer; }
.admin-primary { color: white; background: #2f6f99; border: 0; }
.admin-secondary, .wide-button { color: #2f5f7c; background: #edf3f7; border: 1px solid #d4e1e9; }
.admin-danger { color: #a42d2d; background: #fff1f1; border: 1px solid #efc4c4; }
.target-delete { margin-top: 14px; padding-top: 14px; display: grid; gap: 9px; border-top: 1px solid #e5e5e5; }
.target-delete label { display: grid; gap: 5px; color: #666; font-size: 12px; }
.target-delete input { min-height: 38px; padding: 8px 10px; border: 1px solid #d5d9dd; border-radius: 7px; }
.records-table-wrap { width: 100%; overflow-x: auto; border: 1px solid #e2e5e7; border-radius: 9px; }
.records-table { width: 920px; border-collapse: collapse; table-layout: fixed; background: white; font-size: 11px; }
.records-table th, .records-table td { padding: 9px; vertical-align: top; text-align: left; border-right: 1px solid #eceeef; border-bottom: 1px solid #eceeef; overflow-wrap: anywhere; line-height: 1.55; }
.records-table th { position: sticky; top: 0; color: #56636c; background: #f5f7f8; z-index: 1; }
.records-table th:nth-child(1) { width: 128px; }
.records-table th:nth-child(2) { width: 190px; }
.records-table th:nth-child(3) { width: 310px; }
.records-table th:nth-child(4) { width: 292px; }
.record-answer { margin-top: 6px; padding-top: 6px; color: #4d5961; border-top: 1px dashed #d9dde0; }
.record-ids { color: #74818a; font-family: Consolas, monospace; font-size: 10px; }
.diagnostic-lines { display: grid; gap: 2px; color: #52616b; }
.diagnostic-empty { color: #aaa; }
.text-button { padding: 0; color: #39759c; background: transparent; border: 0; font-size: 12px; }
.wide-button { width: 100%; cursor: pointer; }
.switch-row { margin-top: 14px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.switch-row span { display: grid; gap: 3px; }
.switch-row b { font-size: 13px; }
.switch-row small { color: #888; font-size: 11px; }
.switch-row input { width: 19px; height: 19px; accent-color: #2f6f99; }
.toast { position: fixed; left: 50%; bottom: 110px; padding: 10px 16px; color: white; background: rgba(25,25,25,.9); border-radius: 8px; opacity: 0; pointer-events: none; transform: translate(-50%, 10px); transition: .18s; z-index: 20; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: #a83232; }

.knowledge-config-form { display: grid; gap: 10px; margin-top: 12px; }
.knowledge-config-form label { display: grid; gap: 5px; font-size: 12px; color: #666; }
.knowledge-config-form input { width: 100%; box-sizing: border-box; border: 1px solid #ddd; border-radius: 8px; padding: 9px 10px; font: inherit; color: #222; background: #fff; }
.knowledge-config-history { display: grid; gap: 9px; margin-top: 12px; }
.knowledge-config-item { border: 1px solid #e3e3e3; border-radius: 10px; padding: 10px; background: #fafafa; }
.knowledge-config-item.active { border-color: #61a16f; background: #f3fbf5; }
.knowledge-config-item > div { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.knowledge-config-item b { font-size: 12px; color: #37844a; }
.knowledge-config-item span { font: 12px/1.4 ui-monospace, SFMono-Regular, Consolas, monospace; word-break: break-all; }
.knowledge-config-item p, .knowledge-config-item small { display: block; margin: 6px 0 0; color: #777; font-size: 11px; line-height: 1.5; }
.knowledge-config-item button { margin-top: 9px; width: 100%; }

@media (max-width: 680px) {
  html, body { width: 100%; min-height: 100%; overscroll-behavior: none; background: var(--chat); }
  .app-shell {
    position: fixed;
    top: var(--app-offset-top, 0px);
    right: 0;
    bottom: auto;
    left: 0;
    width: 100%;
    height: var(--app-height, 100dvh);
    margin: 0;
    border-radius: 0;
    box-shadow: none;
    grid-template-rows: calc(68px + env(safe-area-inset-top)) minmax(0, 1fr) auto;
  }
  .chat-header { padding-top: env(safe-area-inset-top); }
  .chat-header { grid-template-columns: 52px 1fr 52px; }
  .header-title h1 { font-size: 20px; }
  .header-title p { font-size: 12px; }
  .back-button { margin-left: 2px; font-size: 38px; }
  .menu-button { margin-right: 2px; letter-spacing: 2px; }
  .date-range { grid-template-columns: 1fr; }
  .chat-viewport { padding: 30px 18px 14px; }
  .message { gap: 12px; margin-bottom: 21px; }
  .avatar { width: 46px; height: 46px; flex-basis: 46px; border-radius: 7px; }
  .message-body, .user .message-body { max-width: calc(100% - 72px); }
  .bubble { padding: 12px 14px; font-size: 16px; line-height: 1.65; }
  .typing-message .bubble { padding: 7px 1px; font-size: 13px; }
  .quick-questions { margin: 14px 0 28px 58px; grid-template-columns: 1fr; gap: 8px; }
  .quick-questions button { min-height: 48px; padding: 9px 12px; font-size: 14px; }
  .composer-shell { position: relative; z-index: 3; }
  .composer { min-height: 70px; padding: 10px 10px max(10px, env(safe-area-inset-bottom)); gap: 8px; }
  .attachment-tray { padding: 8px 10px 0; }
  .input-wrap { padding: 10px 11px; }
  .composer textarea { font-size: 16px; }
  .icon-tool { width: 40px; height: 40px; flex-basis: 40px; padding: 7px; }
  .icon-tool svg { width: 27px; height: 27px; }
  .send-button { min-width: 56px; height: 38px; padding: 0 10px; }
  .verdict-controls { flex-wrap: wrap; }
  .verdict-controls input { width: 100%; }
}

@media (max-width: 380px) {
  .icon-tool { width: 36px; height: 36px; flex-basis: 36px; padding: 6px; }
  .quick-questions { margin-left: 0; }
}
