:root {
    color-scheme: light;
    --bg: #f3f5f7;
    --surface: #ffffff;
    --surface-soft: #f8fafb;
    --line: #dce2e8;
    --line-strong: #c8d0d9;
    --text: #18212b;
    --muted: #667383;
    --accent: #2563eb;
    --accent-soft: #eaf0ff;
    --success: #16815d;
    --success-soft: #e8f7f1;
    --warning: #a15c08;
    --warning-soft: #fff4df;
    --danger: #c23b42;
    --danger-soft: #fdecee;
    --violet: #6d46c7;
    --violet-soft: #f1ecff;
    --sidebar: #151b22;
    --sidebar-muted: #9ca8b5;
    --shadow: 0 10px 28px rgba(24, 33, 43, .08);
    --radius: 6px;
    font-family: Inter, "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
    font-size: 15px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { min-height: 100%; background: var(--bg); }
body { margin: 0; min-height: 100vh; color: var(--text); background: var(--bg); letter-spacing: 0; }
button, input, textarea, select { font: inherit; letter-spacing: 0; }
button, a { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }
a { color: inherit; text-decoration: none; }
svg { width: 18px; height: 18px; stroke-width: 1.9; flex: 0 0 auto; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 224px minmax(0, 1fr); }
.sidebar { position: sticky; top: 0; height: 100vh; padding: 18px 14px; color: #eef3f7; background: var(--sidebar); display: flex; flex-direction: column; }
.brand { height: 48px; padding: 0 8px; display: flex; align-items: center; gap: 11px; }
.brand-mark { width: 34px; height: 34px; border-radius: 6px; display: inline-grid; place-items: center; color: #fff; background: var(--accent); }
.brand-mark svg { width: 20px; height: 20px; }
.brand strong, .brand small { display: block; }
.brand strong { font-size: .98rem; }
.brand small { margin-top: 2px; color: var(--sidebar-muted); font-size: .73rem; }
.side-nav { margin-top: 24px; display: grid; gap: 5px; }
.nav-link { height: 42px; padding: 0 12px; border-radius: 5px; display: flex; align-items: center; gap: 11px; color: var(--sidebar-muted); }
.nav-link:hover { color: #fff; background: rgba(255,255,255,.06); }
.nav-link.active { color: #fff; background: #27313d; box-shadow: inset 3px 0 0 var(--accent); }
.sidebar-foot { margin-top: auto; min-height: 52px; padding: 12px 8px 0; border-top: 1px solid rgba(255,255,255,.09); display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.account-line { min-width: 0; display: flex; align-items: center; gap: 8px; color: var(--sidebar-muted); }
.account-line span { overflow: hidden; text-overflow: ellipsis; }
.sidebar-foot .icon-button { color: var(--sidebar-muted); }
.main-area { min-width: 0; }
.mobile-header, .bottom-nav { display: none; }
.page { width: 100%; max-width: 1440px; margin: 0 auto; padding: 26px 30px 48px; }
.page-heading { min-height: 50px; margin-bottom: 18px; display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.page-heading h1 { margin: 1px 0 0; font-size: 1.55rem; line-height: 1.2; }
.eyebrow { margin: 0; color: var(--muted); font-size: .76rem; font-weight: 700; text-transform: uppercase; }
.heading-actions { display: flex; align-items: center; gap: 8px; }
.sync-state { height: 36px; padding: 0 10px; border: 1px solid var(--line); border-radius: 5px; display: inline-flex; align-items: center; gap: 7px; color: var(--muted); background: var(--surface); font-size: .84rem; }
.sync-state.loading svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.button, .icon-button { border: 1px solid transparent; border-radius: 5px; display: inline-flex; align-items: center; justify-content: center; gap: 7px; font-weight: 650; transition: background .15s, border-color .15s, color .15s, opacity .15s; }
.button { min-height: 38px; padding: 0 14px; }
.button.primary { color: #fff; background: var(--accent); }
.button.primary:hover { background: #1d4ed8; }
.button.secondary { border-color: var(--line); color: var(--text); background: var(--surface); }
.button.secondary:hover, .icon-button:hover { border-color: var(--line-strong); background: var(--surface-soft); }
.button.danger { color: var(--danger); border-color: #f1c7cb; background: var(--danger-soft); }
.button:disabled, .icon-button:disabled { cursor: not-allowed; opacity: .5; }
.icon-button { width: 36px; height: 36px; padding: 0; color: var(--muted); background: transparent; }

.metric-strip { min-height: 72px; margin-bottom: 16px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); }
.metric-strip > div { min-width: 0; padding: 14px 18px; border-right: 1px solid var(--line); display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.metric-strip > div:last-child { border-right: 0; }
.metric-strip span { color: var(--muted); font-size: .84rem; }
.metric-strip strong { font-size: 1.5rem; line-height: 1; }
.compact-metrics { max-width: 720px; grid-template-columns: repeat(3, minmax(0, 1fr)); }
.danger-text { color: var(--danger); }

.message-toolbar { position: sticky; top: 0; z-index: 10; margin: 0 -1px 14px; padding: 10px 1px; background: rgba(243,245,247,.95); backdrop-filter: blur(8px); display: flex; align-items: center; gap: 12px; }
.status-tabs { min-width: 0; height: 40px; padding: 3px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); display: flex; align-items: center; overflow-x: auto; scrollbar-width: none; }
.status-tabs::-webkit-scrollbar { display: none; }
.status-tabs button { flex: 0 0 auto; height: 32px; padding: 0 10px; border: 0; border-radius: 4px; color: var(--muted); background: transparent; display: inline-flex; align-items: center; gap: 6px; font-weight: 620; white-space: nowrap; }
.status-tabs button b { min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; color: var(--muted); background: #edf1f4; display: inline-grid; place-items: center; font-size: .7rem; }
.status-tabs button.active { color: #fff; background: var(--accent); }
.status-tabs button.active b { color: var(--accent); background: #fff; }
.search-control { position: relative; flex: 1 1 260px; min-width: 210px; max-width: 360px; height: 40px; }
.search-control > svg { position: absolute; left: 11px; top: 11px; color: var(--muted); }
.search-control input { width: 100%; height: 100%; padding: 0 38px 0 38px; border: 1px solid var(--line); border-radius: 6px; outline: 0; color: var(--text); background: var(--surface); }
.search-control input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37,99,235,.12); }
.clear-search { position: absolute; right: 4px; top: 4px; width: 32px; height: 32px; border: 0; color: var(--muted); background: transparent; display: none; place-items: center; }
.search-control.has-value .clear-search { display: grid; }

.message-list { min-height: 320px; border-top: 1px solid var(--line-strong); background: var(--surface); }
.message-row { border-bottom: 1px solid var(--line); display: grid; grid-template-columns: 158px minmax(220px, 1.2fr) minmax(260px, 2fr) 186px; align-items: stretch; }
.message-row:hover { background: #fbfcfd; }
.message-meta, .message-person, .message-body, .message-actions { min-width: 0; padding: 14px; }
.message-meta, .message-person, .message-body { border-right: 1px solid var(--line); }
.message-id-line { display: flex; align-items: center; gap: 7px; }
.message-id { color: var(--muted); font-size: .76rem; font-variant-numeric: tabular-nums; }
.message-time { margin-top: 8px; color: var(--muted); font-size: .78rem; line-height: 1.45; }
.message-person strong, .message-person span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message-person strong { font-size: .94rem; }
.message-person span { margin-top: 5px; color: var(--muted); font-size: .8rem; }
.message-title { margin-bottom: 6px; color: var(--accent); font-size: 14px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message-content { line-height: 1.55; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }
.reply-preview { margin-top: 8px; padding-left: 9px; border-left: 2px solid var(--accent); color: #425166; font-size: .84rem; line-height: 1.45; }
.message-actions { display: flex; align-items: center; justify-content: flex-end; gap: 6px; flex-wrap: wrap; }
.message-actions .button { min-height: 34px; padding: 0 10px; font-size: .82rem; }
.message-shot { width: 34px; height: 34px; border: 1px solid var(--line); border-radius: 4px; padding: 0; display: grid; place-items: center; color: var(--muted); background: var(--surface); }
.status-badge { height: 23px; padding: 0 7px; border-radius: 4px; display: inline-flex; align-items: center; font-size: .72rem; font-weight: 750; white-space: nowrap; }
.status-new { color: var(--danger); background: var(--danger-soft); }
.status-read { color: #426172; background: #edf3f6; }
.status-reply_queued { color: var(--warning); background: var(--warning-soft); }
.status-claimed { color: var(--violet); background: var(--violet-soft); }
.status-completed { color: var(--success); background: var(--success-soft); }
.status-failed { color: var(--danger); background: var(--danger-soft); }
.auto-badge { color: var(--violet); background: var(--violet-soft); }
.list-footer { padding: 16px; text-align: center; }
.empty-state, .loading-state { min-height: 300px; padding: 48px 20px; color: var(--muted); display: grid; place-items: center; text-align: center; }
.empty-state > div, .loading-state > div { display: grid; justify-items: center; gap: 10px; }
.empty-state svg, .loading-state svg { width: 30px; height: 30px; }
.skeleton-row { height: 86px; border-bottom: 1px solid var(--line); background: linear-gradient(90deg, #f6f8f9 25%, #eef2f5 37%, #f6f8f9 63%); background-size: 400% 100%; animation: shimmer 1.4s ease infinite; }
@keyframes shimmer { 0% { background-position: 100% 0; } 100% { background-position: 0 0; } }

.data-list { border-top: 1px solid var(--line-strong); background: var(--surface); }
.data-row { min-height: 72px; padding: 13px 14px; border-bottom: 1px solid var(--line); display: grid; grid-template-columns: minmax(180px, 1.2fr) minmax(160px, 1fr) minmax(150px, .8fr) auto; align-items: center; gap: 16px; }
.data-row:hover { background: #fbfcfd; }
.data-main { min-width: 0; }
.data-main strong, .data-main span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.data-main span, .data-cell span { margin-top: 3px; color: var(--muted); font-size: .79rem; }
.data-cell { min-width: 0; }
.data-actions { display: flex; align-items: center; justify-content: flex-end; gap: 6px; }
.online-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; background: var(--muted); }
.online-dot.online { background: var(--success); box-shadow: 0 0 0 3px var(--success-soft); }

.sheet-dialog, .image-dialog { padding: 0; border: 0; border-radius: 7px; color: var(--text); background: var(--surface); box-shadow: var(--shadow); }
.sheet-dialog::backdrop, .image-dialog::backdrop { background: rgba(12,18,25,.52); }
.sheet-dialog { width: min(560px, calc(100vw - 24px)); }
.wide-dialog { width: min(680px, calc(100vw - 24px)); }
.dialog-shell { max-height: min(760px, calc(100vh - 32px)); padding: 20px; display: flex; flex-direction: column; gap: 16px; overflow: auto; }
.dialog-head { min-height: 38px; display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.dialog-head h2 { margin: 0; font-size: 1.14rem; }
.reply-context { padding: 11px 12px; border-left: 3px solid var(--line-strong); color: var(--muted); background: var(--surface-soft); line-height: 1.5; }
.field { display: grid; gap: 7px; }
.field[hidden] { display: none; }
.field > span { color: #405064; font-size: .82rem; font-weight: 680; }
.field input, .field textarea, .field select { width: 100%; border: 1px solid var(--line); border-radius: 5px; outline: 0; color: var(--text); background: var(--surface); }
.field input, .field select { height: 40px; padding: 0 11px; }
.field textarea { padding: 10px 11px; resize: vertical; line-height: 1.5; }
.field input:focus, .field textarea:focus, .field select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37,99,235,.11); }
.grow-field textarea { min-height: 150px; }
.dialog-actions { display: flex; align-items: center; justify-content: flex-end; gap: 8px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.span-two { grid-column: span 2; }
.toggle-field { min-height: 40px; display: flex; align-items: center; gap: 9px; }
.toggle-field input { width: 18px; height: 18px; accent-color: var(--accent); }
.token-box { padding: 12px; border: 1px solid var(--line); border-radius: 5px; color: #2b3745; background: var(--surface-soft); font-family: Consolas, monospace; font-size: .78rem; line-height: 1.55; overflow-wrap: anywhere; user-select: all; }
.image-dialog { width: min(1100px, calc(100vw - 24px)); max-height: calc(100vh - 24px); background: #0f141a; }
.image-dialog img { display: block; width: 100%; max-height: calc(100vh - 24px); object-fit: contain; }
.close-lightbox { position: absolute; right: 10px; top: 10px; color: #fff; background: rgba(0,0,0,.48); }

.settings-page { max-width: 980px; margin-left: 0; }
.settings-form { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.setting-row { min-height: 72px; padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.setting-row h2 { margin: 0; font-size: 1rem; }
.setting-status { margin-top: 5px; color: var(--muted); display: block; font-size: .8rem; }
.switch { display: inline-flex; }
.switch input { position: absolute; opacity: 0; }
.switch span { position: relative; width: 44px; height: 24px; border-radius: 12px; background: #b9c1ca; transition: .2s; }
.switch span::after { content: ''; position: absolute; left: 3px; top: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.2); }
.switch input:checked + span { background: var(--success); }
.switch input:checked + span::after { transform: translateX(20px); }
.settings-body { padding: 18px; display: grid; gap: 16px; }
.numeric-field { max-width: 260px; }
.settings-actions { padding: 14px 18px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; }

.toast-region { position: fixed; z-index: 100; right: 18px; bottom: 18px; display: grid; gap: 8px; pointer-events: none; }
.toast { min-width: 260px; max-width: min(420px, calc(100vw - 30px)); padding: 11px 13px; border: 1px solid var(--line); border-left: 4px solid var(--success); border-radius: 5px; background: var(--surface); box-shadow: var(--shadow); }
.toast.error { border-left-color: var(--danger); }

.login-page { min-height: 100vh; background: #eef2f5; display: grid; place-items: center; padding: 20px; }
.login-shell { width: min(400px, 100%); }
.login-brand { margin-bottom: 14px; display: flex; align-items: center; gap: 11px; }
.login-brand strong, .login-brand span { display: block; }
.login-brand span { margin-top: 2px; color: var(--muted); font-size: .76rem; }
.login-form { padding: 24px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface); box-shadow: 0 14px 36px rgba(24,33,43,.08); display: grid; gap: 16px; }
.login-form h1 { margin: 0 0 2px; font-size: 1.3rem; }
.login-form label { display: grid; gap: 7px; }
.login-form label span { color: #405064; font-size: .82rem; font-weight: 680; }
.login-form input { width: 100%; height: 42px; padding: 0 11px; border: 1px solid var(--line); border-radius: 5px; outline: 0; }
.login-form input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37,99,235,.11); }
.login-submit { margin-top: 2px; height: 42px; }
.form-error { padding: 10px 11px; border-left: 3px solid var(--danger); color: #8f2d33; background: var(--danger-soft); font-size: .86rem; }

@media (max-width: 1120px) {
    .message-row { grid-template-columns: 135px minmax(170px, .9fr) minmax(240px, 1.5fr) 150px; }
    .message-actions .button span { display: none; }
    .message-actions .button { width: 34px; padding: 0; }
}

@media (max-width: 820px) {
    body { padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
    .app-shell { display: block; }
    .sidebar { display: none; }
    .mobile-header { position: sticky; top: 0; z-index: 30; height: 54px; padding: 0 14px; border-bottom: 1px solid var(--line); background: rgba(255,255,255,.96); backdrop-filter: blur(8px); display: flex; align-items: center; justify-content: space-between; }
    .mobile-brand { display: flex; align-items: center; gap: 9px; }
    .mobile-brand .brand-mark { width: 30px; height: 30px; }
    .bottom-nav { position: fixed; z-index: 40; left: 0; right: 0; bottom: 0; height: calc(58px + env(safe-area-inset-bottom)); padding: 3px 8px env(safe-area-inset-bottom); border-top: 1px solid var(--line); background: rgba(255,255,255,.98); display: grid; grid-template-columns: repeat(4, 1fr); }
    .bottom-nav a { min-width: 0; border-radius: 4px; color: var(--muted); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: .68rem; }
    .bottom-nav a.active { color: var(--accent); background: var(--accent-soft); }
    .bottom-nav svg { width: 19px; height: 19px; }
    .page { padding: 16px 12px 30px; }
    .page-heading { min-height: 42px; margin-bottom: 12px; }
    .page-heading h1 { font-size: 1.25rem; }
    .metric-strip { min-height: 58px; margin-left: -12px; margin-right: -12px; border-left: 0; border-right: 0; border-radius: 0; }
    .metric-strip > div { padding: 10px 12px; display: grid; gap: 4px; }
    .metric-strip span { font-size: .7rem; white-space: nowrap; }
    .metric-strip strong { font-size: 1.15rem; }
    .message-toolbar { top: 54px; margin: 0 -12px 10px; padding: 8px 12px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }
    .status-tabs { width: 100%; }
    .search-control { width: 100%; max-width: none; min-width: 0; }
    .message-list { margin: 0 -12px; border-top-color: var(--line); }
    .message-row { position: relative; padding: 12px; grid-template-columns: minmax(0,1fr) auto; grid-template-areas: 'person meta' 'body body' 'actions actions'; gap: 8px 10px; }
    .message-meta, .message-person, .message-body, .message-actions { padding: 0; border: 0; }
    .message-meta { grid-area: meta; text-align: right; }
    .message-person { grid-area: person; }
    .message-body { grid-area: body; }
    .message-actions { grid-area: actions; justify-content: flex-start; padding-top: 3px; }
    .message-time { margin-top: 5px; font-size: .7rem; }
    .message-content { -webkit-line-clamp: 4; }
    .message-actions .button { width: auto; padding: 0 10px; }
    .message-actions .button span { display: inline; }
    .data-list { margin: 0 -12px; }
    .data-row { padding: 13px 12px; grid-template-columns: minmax(0,1fr) auto; gap: 8px 12px; }
    .data-row .data-cell { grid-column: 1; }
    .data-actions { grid-column: 2; grid-row: 1 / span 2; }
    .compact-metrics { margin-left: -12px; margin-right: -12px; }
    .dialog-shell { max-height: calc(100vh - 14px); padding: 16px; }
    .sheet-dialog { width: calc(100vw - 12px); margin-bottom: 6px; }
    .sheet-dialog[open] { margin-top: auto; }
    .form-grid { grid-template-columns: 1fr; }
    .span-two { grid-column: auto; }
    .settings-form { margin: 0 -12px; border-left: 0; border-right: 0; border-radius: 0; }
    .toast-region { right: 8px; left: 8px; bottom: calc(66px + env(safe-area-inset-bottom)); }
    .toast { min-width: 0; max-width: none; width: 100%; }
}

@media (max-width: 430px) {
    .heading-actions .sync-state { display: none; }
    .metric-strip > div:nth-child(3) { display: none; }
    .metric-strip { grid-template-columns: repeat(3, minmax(0,1fr)); }
    .compact-metrics > div:nth-child(3) { display: grid; }
    .compact-metrics { grid-template-columns: repeat(3, minmax(0,1fr)); }
}
