:root {
    color-scheme: light;
    --ink: #17202a;
    --muted: #68737d;
    --line: #dfe4e8;
    --panel: #ffffff;
    --canvas: #f4f6f7;
    --accent: #087f5b;
    --accent-strong: #066649;
    --danger: #c92a2a;
    --warning: #e67700;
    --blue: #1864ab;
    --shadow: 0 8px 24px rgba(23, 32, 42, .08);
}
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body { font-family: Inter, "Segoe UI", "Microsoft YaHei", sans-serif; color: var(--ink); background: var(--canvas); font-size: 14px; letter-spacing: 0; }
button, input, select, textarea { font: inherit; letter-spacing: 0; }
button, .button { border: 1px solid transparent; border-radius: 6px; background: var(--accent); color: #fff; padding: 9px 14px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
button:hover, .button:hover { background: var(--accent-strong); }
button:disabled { opacity: .55; cursor: not-allowed; }
.button.secondary, button.secondary { color: var(--ink); background: #fff; border-color: var(--line); }
.button.secondary:hover, button.secondary:hover { background: #f1f3f5; }
.button.danger, button.danger { background: #fff; color: var(--danger); border-color: #ffc9c9; }
.button.danger:hover, button.danger:hover { background: #fff5f5; }
.button.small, button.small { padding: 6px 9px; font-size: 12px; }
input, select, textarea { width: 100%; border: 1px solid #cfd6dc; border-radius: 5px; padding: 9px 10px; background: #fff; color: var(--ink); outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(8,127,91,.12); }
label { display: grid; gap: 6px; color: #3d4852; }
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: #eef2f3; }
.login-box { width: min(390px, 100%); background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 30px; box-shadow: var(--shadow); }
.login-box h1 { margin: 0 0 6px; font-size: 24px; }
.login-box p { margin: 0 0 24px; color: var(--muted); }
.login-box form { display: grid; gap: 16px; }
.login-remember { justify-self: start; }
.alert { padding: 10px 12px; border-radius: 5px; margin: 12px 0; border: 1px solid; }
.alert.error { background: #fff5f5; border-color: #ffc9c9; color: #a51111; }
.alert.success { background: #ebfbee; border-color: #b2f2bb; color: #216e39; }
.topbar { height: 58px; background: #182329; color: #fff; display: flex; align-items: center; padding: 0 22px; gap: 24px; position: sticky; top: 0; z-index: 30; }
.brand { font-size: 17px; font-weight: 700; white-space: nowrap; }
.topnav { display: flex; align-self: stretch; }
.topnav a { display: flex; align-items: center; color: #cbd3d7; text-decoration: none; padding: 0 16px; border-bottom: 3px solid transparent; }
.topnav a.active, .topnav a:hover { color: #fff; border-color: #38d9a9; }
.top-actions { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.top-actions form { margin: 0; }
.top-actions button { padding: 6px 10px; background: transparent; border-color: #526169; }
.page { max-width: 1780px; margin: 0 auto; padding: 22px; }
.page-head { display: flex; align-items: end; justify-content: space-between; gap: 18px; margin-bottom: 18px; }
.page-head h1 { margin: 0 0 4px; font-size: 23px; }
.page-head p { margin: 0; color: var(--muted); }
.section { background: #fff; border-top: 3px solid #2f9e7a; border-bottom: 1px solid var(--line); padding: 18px; margin-bottom: 20px; }
.section h2 { margin: 0 0 14px; font-size: 17px; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 14px; }
.section-head h2 { margin: 0; flex: 0 0 auto; }
.form-grid { display: grid; grid-template-columns: repeat(4, minmax(140px, 1fr)); gap: 12px; align-items: end; }
.form-grid .wide { grid-column: span 2; }
.site-create-grid { grid-template-columns: 160px 105px 175px 125px 125px 102px 88px; justify-content: start; }
.tenant-picker { position: relative; }
.tenant-results { position: absolute; top: calc(100% + 4px); left: 0; z-index: 40; width: 310px; max-height: 280px; overflow-y: auto; border: 1px solid #cfd6dc; border-radius: 5px; background: #fff; box-shadow: var(--shadow); }
.tenant-option { width: 100%; border: 0; border-bottom: 1px solid #edf0f2; border-radius: 0; padding: 9px 10px; justify-content: flex-start; text-align: left; background: #fff; color: var(--ink); }
.tenant-option:hover { background: #eef6f3; color: var(--ink); }
.tenant-option:last-child { border-bottom: 0; }
.tenant-empty { display: block; padding: 12px; color: var(--muted); }
.inline-check { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; color: #3d4852; }
.inline-check input[type="checkbox"] { appearance: auto; width: 16px; height: 16px; min-width: 16px; margin: 0; padding: 0; box-shadow: none; }
.site-create-check { min-height: 40px; align-self: end; }
.integration-status { display: flex; align-items: center; gap: 10px; margin: -3px 0 14px; }
.integration-form { max-width: 980px; }
.integration-check { min-height: 40px; align-self: end; }
.settings-actions { display: flex; align-items: center; gap: 8px; grid-column: span 3; flex-wrap: wrap; }
.setting-label { display: flex; align-items: center; gap: 6px; min-width: 0; }
.setting-help { position: relative; width: 18px; height: 18px; flex: 0 0 18px; display: inline-grid; place-items: center; border: 1px solid #98a4ac; border-radius: 50%; color: #5d6972; background: #fff; font-size: 12px; font-weight: 700; line-height: 1; cursor: help; outline: none; }
.setting-help:hover, .setting-help:focus { border-color: var(--accent); color: var(--accent); box-shadow: 0 0 0 3px rgba(8,127,91,.12); }
.setting-tooltip { position: absolute; left: 50%; top: calc(100% + 8px); z-index: 80; width: 320px; display: none; padding: 10px 12px; border: 1px solid #cfd6dc; border-radius: 5px; background: #17202a; color: #fff; box-shadow: var(--shadow); font-size: 12px; font-weight: 400; line-height: 1.55; text-align: left; white-space: normal; transform: translateX(-50%); pointer-events: none; }
.setting-help:hover .setting-tooltip, .setting-help:focus .setting-tooltip { display: block; }
.cleanup-controls { display: flex; align-items: end; gap: 24px; flex-wrap: wrap; }
.cleanup-form { display: flex; align-items: end; gap: 10px; }
.cleanup-form label { width: 220px; }
.cleanup-all-form { padding-left: 24px; border-left: 1px solid var(--line); }
.cleanup-all-form > div { min-width: 210px; }
.cleanup-all-form strong, .cleanup-all-form span { display: block; }
.cleanup-all-form span { margin-top: 5px; font-size: 12px; }
.cleanup-progress { max-width: 760px; margin-top: 16px; }
.cleanup-progress progress { display: block; width: 100%; height: 12px; accent-color: var(--accent); }
.cleanup-progress p { margin: 8px 0 0; color: #3d4852; }
.filter-bar { display: flex; align-items: center; justify-content: flex-end; gap: 7px; flex-wrap: nowrap; margin-left: auto; }
.filter-bar input { width: 180px; padding: 7px 9px; }
.filter-bar select { width: 100px; padding: 7px 8px; }
.site-filter input { width: 240px; }
.table-wrap { overflow: auto; border: 1px solid var(--line); }
table { width: 100%; border-collapse: collapse; min-width: 760px; background: #fff; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { background: #f5f7f8; font-size: 12px; color: #55616b; text-transform: uppercase; }
tr:last-child td { border-bottom: 0; }
.row-form { display: flex; align-items: center; gap: 6px; min-width: 320px; }
.row-form input, .row-form select { padding: 6px 7px; min-width: 80px; }
.row-form button { white-space: nowrap; }
.site-table { min-width: 1260px; }
.site-table th, .site-table td { padding: 8px 9px; }
.site-table .account-cell { width: 145px; white-space: nowrap; }
.site-row-form { display: grid; grid-template-columns: 92px 155px 72px 124px 124px 72px 56px; align-items: center; gap: 8px; min-width: 743px; white-space: nowrap; }
.site-row-form input, .site-row-form select { min-width: 0; padding: 6px 7px; }
.site-row-form button { white-space: nowrap; }
.site-table .embed-cell { width: 270px; }
.embed-inline { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.embed-cell .code-box { width: 205px; max-width: 205px; margin: 0; padding: 6px 7px; }
.copy-button { white-space: nowrap; }
.pagination { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 12px; color: var(--muted); }
.pagination > div { display: flex; gap: 8px; }
.button.disabled { opacity: .45; cursor: default; pointer-events: none; }
.empty-cell { padding: 28px; text-align: center; color: var(--muted); }
.status { display: inline-flex; align-items: center; padding: 3px 7px; border-radius: 999px; font-size: 12px; font-weight: 600; background: #edf2f4; }
.status.active { color: #087f5b; background: #d3f9d8; }
.status.paused, .status.expired { color: #a85d00; background: #fff3bf; }
.muted { color: var(--muted); }
.code-box { display: block; width: 100%; max-width: 520px; padding: 9px; border: 1px solid var(--line); background: #f8f9fa; white-space: nowrap; overflow-x: auto; font-family: Consolas, monospace; font-size: 12px; }
.embed-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 14px; }
.embed-item { border: 1px solid var(--line); border-radius: 6px; padding: 14px; min-width: 0; }
.embed-title, .embed-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.embed-title { margin-bottom: 12px; }
.embed-title strong, .embed-title span { display: block; }
.embed-title div > span { margin-top: 3px; color: var(--muted); font-size: 12px; }
.embed-item .code-box { max-width: none; margin-bottom: 12px; }
.chat-shell { height: calc(100vh - 58px); display: grid; grid-template-columns: 230px 310px minmax(360px, 1fr); background: #fff; overflow: hidden; }
.site-pane, .conversation-pane { border-right: 1px solid var(--line); min-width: 0; min-height: 0; overflow: hidden; background: #f7f9fa; display: flex; flex-direction: column; }
.pane-title { height: 54px; padding: 0 15px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--line); background: #fff; font-weight: 700; flex: 0 0 auto; }
.pane-title-main { min-width: 0; display: block; }
.conversation-title .pane-title-main { flex: 1; }
.pane-title-main strong, .pane-title-main small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pane-title-main small { margin-top: 2px; color: var(--muted); font-size: 11px; font-weight: 400; }
.mobile-back, .mobile-only { display: none; }
.site-list, .conversation-list { min-height: 0; overflow-y: auto; overscroll-behavior: contain; flex: 1 1 auto; }
.site-item, .conversation-item { width: 100%; border: 0; border-bottom: 1px solid #e8ecef; border-radius: 0; background: transparent; color: var(--ink); text-align: left; justify-content: flex-start; padding: 13px 15px; display: block; }
.site-item:hover, .conversation-item:hover { background: #eef3f3; }
.site-item.active, .conversation-item.active { background: #dff3ec; box-shadow: inset 3px 0 var(--accent); }
.conversation-load-more { width: 100%; min-height: 46px; padding: 10px 12px; border: 0; border-bottom: 1px solid #e8ecef; border-radius: 0; background: #f7f9fa; color: var(--muted); font-size: 12px; text-align: center; }
.conversation-load-more:hover { background: #eef3f3; color: var(--accent); }
.conversation-load-more.loading, .conversation-load-more.finished { display: grid; place-items: center; cursor: default; }
.conversation-load-more.loading { opacity: 1; }
.conversation-load-more.error { color: var(--danger); }
.site-name, .conversation-name { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.site-meta, .conversation-preview { color: var(--muted); font-size: 12px; margin-top: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.site-expiry { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 4px; color: #68737d; font-size: 11px; }
.site-expiry.inactive { color: var(--danger); }
.site-expiry strong { font-size: 11px; white-space: nowrap; }
.site-item.authorization-inactive { box-shadow: inset 3px 0 #e03131; }
.site-item.authorization-inactive.active { background: #fff0f0; }
.site-item.authorization-inactive .site-name, .site-item.authorization-inactive .site-meta { color: var(--danger); }
.site-row, .conversation-row { display: flex; align-items: center; gap: 8px; }
.site-row .site-name, .conversation-row .conversation-name { flex: 1; }
.unread { min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: #e03131; color: #fff; font-size: 11px; display: inline-grid; place-items: center; }
.chat-pane { min-width: 0; min-height: 0; overflow: hidden; display: grid; grid-template-rows: 64px minmax(0,1fr) auto; background: #edf1f2; }
.chat-head { background: #fff; border-bottom: 1px solid var(--line); padding: 10px 18px; display: flex; align-items: center; gap: 12px; }
.chat-head-main { min-width: 0; flex: 1; }
.chat-head-main strong, .chat-head-main span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head-main span { font-size: 12px; color: var(--muted); margin-top: 3px; }
.chat-actions, .conversation-actions { position: relative; flex: 0 0 auto; }
.conversation-actions { display: flex; align-items: center; }
.chat-more { width: 42px; height: 42px; padding: 0; border-color: var(--line); background: #fff; color: var(--ink); font-size: 25px; line-height: 1; }
.chat-more:hover { background: #f1f3f5; color: var(--ink); }
.chat-action-menu { position: absolute; top: calc(100% + 6px); right: 0; z-index: 20; width: 132px; padding: 6px; border: 1px solid var(--line); border-radius: 6px; background: #fff; box-shadow: var(--shadow); }
.chat-action-menu[hidden] { display: none; }
.chat-action-menu button { width: 100%; }
.site-conversation-action-menu { width: 154px; }
.chat-messages { min-height: 0; overflow-y: auto; padding: 22px; }
.message-load-more { width: 100%; min-height: 38px; margin: 0 0 14px; padding: 8px 10px; border: 0; border-radius: 5px; background: rgba(255,255,255,.72); color: var(--muted); font-size: 12px; text-align: center; }
.message-load-more:hover { background: #fff; color: var(--accent); }
.message-load-more.loading, .message-load-more.finished { display: grid; place-items: center; cursor: default; }
.message-load-more.loading { opacity: 1; }
.message-load-more.error { color: var(--danger); }
.empty-state { height: 100%; display: grid; place-items: center; color: var(--muted); text-align: center; padding: 30px; }
.chat-system-notice { width: fit-content; max-width: min(520px, 90%); margin: 8px auto 14px; padding: 7px 12px; border-radius: 14px; background: #fff0f0; color: #c92a2a; font-size: 12px; line-height: 1.45; text-align: center; }
.chat-empty-with-notice { align-content: center; gap: 14px; }
.chat-empty-with-notice .chat-system-notice { margin: 0; }
.loading-state { align-content: center; gap: 10px; }
.loading-state::before { content: ""; width: 22px; height: 22px; justify-self: center; border: 3px solid #d7dee2; border-top-color: var(--accent); border-radius: 50%; animation: support-loading .7s linear infinite; }
@keyframes support-loading { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .loading-state::before { animation: none; } }
.message { display: flex; margin-bottom: 15px; align-items: flex-end; gap: 7px; }
.message.mine { justify-content: flex-end; }
.chat-avatar { width: 34px; height: 34px; border: 1px solid var(--line); border-radius: 50%; background: #fff; color: #68737d; display: grid; place-items: center; flex: 0 0 34px; }
.chat-avatar.agent { border-color: var(--accent); background: var(--accent); color: #fff; }
.chat-avatar svg { width: 19px; height: 19px; }
.bubble { max-width: min(640px, 76%); background: #fff; border: 1px solid #dfe5e7; border-radius: 7px; padding: 9px 11px; box-shadow: 0 2px 5px rgba(0,0,0,.03); white-space: pre-wrap; word-break: break-word; }
.message.mine .bubble { background: #d3f9d8; border-color: #b2f2bb; }
.message-meta { color: #7c878f; font-size: 11px; margin-top: 5px; display: flex; gap: 7px; justify-content: flex-end; }
.message-delete { border: 0; background: transparent; color: #9aa3aa; padding: 3px; font-size: 11px; }
.message-delete:hover { background: transparent; color: var(--danger); }
.composer { position: relative; background: #fff; border-top: 1px solid var(--line); padding: 12px 16px; }
.composer form { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 10px; align-items: end; }
.composer textarea { min-height: 62px; max-height: 160px; resize: vertical; }
.toast { position: fixed; left: 50%; bottom: 18px; width: max-content; max-width: min(420px, calc(100vw - 20px)); background: #17202a; color: #fff; padding: 11px 14px; border-radius: 6px; box-shadow: var(--shadow); z-index: 100; text-align: center; transform: translateX(-50%); }
.toast.error { background: #a51111; }
.toast-action { cursor: pointer; }
.toast-action:focus { outline: 3px solid rgba(56,217,169,.45); outline-offset: 2px; }
@media (min-width: 921px) {
    .site-create-grid label:nth-of-type(5) { margin-left: 12px; }
    .site-create-grid .site-create-check { margin-left: 30px; }
    .site-create-grid > button { margin-left: 20px; }
}
@media (max-width: 920px) {
    .form-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
    .section-head { align-items: flex-start; flex-direction: column; }
    .filter-bar { width: 100%; justify-content: flex-start; }
    .chat-shell { grid-template-columns: 180px 260px minmax(320px,1fr); overflow-x: auto; }
}
@media (max-width: 680px) {
    html, body { overscroll-behavior: none; }
    body.chat-mobile-fixed { width: 100%; height: 100%; overflow: hidden; }
    body.chat-mobile-fixed .topbar { position: fixed; top: var(--chat-viewport-offset, 0px); right: 0; left: 0; }
    .topbar { padding: 0 12px; gap: 8px; }
    .brand { display: none; }
    .topnav { flex: 1; min-width: 0; }
    .topnav a { flex: 1; justify-content: center; padding: 0 5px; font-size: 12px; white-space: nowrap; }
    .top-actions span { display: none; }
    .page { padding: 12px; }
    .form-grid { grid-template-columns: 1fr; }
    .form-grid .wide { grid-column: auto; }
    .settings-actions { grid-column: auto; }
    .cleanup-controls, .cleanup-form { display: grid; grid-template-columns: 1fr; width: 100%; align-items: stretch; }
    .cleanup-form label, .cleanup-all-form > div { width: 100%; min-width: 0; }
    .cleanup-all-form { padding: 16px 0 0; border-top: 1px solid var(--line); border-left: 0; }
    .cleanup-form button { min-height: 44px; }
    .filter-bar { display: grid; grid-template-columns: minmax(0,1fr) auto; }
    .filter-bar input, .site-filter input { width: 100%; }
    .filter-bar select { width: auto; }
    .embed-grid { grid-template-columns: 1fr; }
    .embed-title, .embed-actions { align-items: flex-start; }
    .pagination { align-items: flex-start; }
    .chat-shell { position: relative; display: block; height: calc(100vh - 58px); height: calc(100dvh - 58px); overflow: hidden; }
    body.chat-mobile-fixed .chat-shell { position: fixed; top: calc(var(--chat-viewport-offset, 0px) + 58px); right: 0; left: 0; width: 100%; height: calc(var(--chat-viewport-height, 100dvh) - 58px); }
    .chat-shell .site-pane, .chat-shell .conversation-pane, .chat-shell .chat-pane { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; display: none; }
    .chat-shell[data-mobile-view="sites"] .site-pane { display: flex; }
    .chat-shell[data-mobile-view="conversations"] .conversation-pane { display: flex; }
    .chat-shell[data-mobile-view="chat"] .chat-pane { display: grid; }
    .pane-title { height: 58px; padding: 0 12px; }
    .conversation-title { justify-content: flex-start; gap: 12px; }
    .mobile-back { position: relative; display: inline-flex; min-width: 44px; min-height: 44px; padding: 0 8px 0 2px; border: 0; background: transparent; color: var(--accent); gap: 2px; font-weight: 600; }
    .mobile-back:hover { background: transparent; color: var(--accent-strong); }
    .mobile-back-icon { font-size: 28px; line-height: 1; }
    .mobile-back-unread { position: absolute; top: 1px; left: 18px; min-width: 18px; height: 18px; padding: 0 5px; border: 2px solid #fff; border-radius: 9px; background: #e03131; color: #fff; font-size: 10px; line-height: 14px; text-align: center; }
    .mobile-back-unread[hidden] { display: none; }
    .mobile-only { display: inline-flex; }
    .desktop-clear { display: none; }
    .site-item, .conversation-item { min-height: 66px; padding: 12px 15px; }
    .chat-pane { grid-template-rows: 58px minmax(0,1fr) auto; }
    .chat-head { padding: 7px 10px 7px 6px; gap: 6px; }
    .chat-head > .mobile-back { max-width: 112px; }
    .chat-head-main strong { font-size: 14px; }
    .chat-head-main span { font-size: 11px; }
    .chat-messages { padding: 12px 10px; }
    .chat-avatar { width: 32px; height: 32px; flex-basis: 32px; }
    .chat-avatar svg { width: 18px; height: 18px; }
    .bubble { max-width: 88%; }
    .message-delete { padding: 5px 2px; }
    .composer { padding: 9px 10px calc(9px + env(safe-area-inset-bottom)); }
    .composer form { gap: 8px; }
    .composer textarea { min-height: 44px; max-height: 110px; resize: none; padding: 10px; font-size: 16px; }
    .composer button { min-width: 58px; min-height: 44px; padding: 9px 12px; }
    .toast { bottom: calc(10px + env(safe-area-inset-bottom)); max-width: calc(100vw - 20px); }
    .setting-help .setting-tooltip { position: fixed; top: 50%; right: 16px; left: 16px; width: auto; transform: translateY(-50%); }
}
