:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e3e6eb;
  --text: #1b1f24;
  --text-2: #5b6470;
  --muted: #8a929d;
  --accent: #1f7a5a;
  --accent-ink: #ffffff;
  --accent-soft: #e3f3ec;
  --bubble-in: #ffffff;
  --bubble-out: #dcf5e7;
  --bubble-agent: #e4ecfb;
  --bubble-note: #fff6d9;
  --chat-bg: #eef0ea;
  --bar: #2f6fb3;
  --good: #1e7b45; --good-bg: #e2f4e9;
  --warn: #8a5a00; --warn-bg: #fff1d1;
  --bad: #b3261e; --bad-bg: #fde5e3;
  --info: #22558f; --info-bg: #e3edf9;
  --neutral: #4d5560; --neutral-bg: #eceef1;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418; --surface: #1a1e24; --surface-2: #20252c; --border: #2d333c;
    --text: #e8eaed; --text-2: #b0b7c0; --muted: #808893;
    --accent: #3fb68b; --accent-ink: #08130e; --accent-soft: #1b3229;
    --bubble-in: #232931; --bubble-out: #1d3a2d; --bubble-agent: #1f2e45; --bubble-note: #3a3220;
    --chat-bg: #15191e; --bar: #6aa5e6;
    --good: #6fd39a; --good-bg: #183424; --warn: #f0c060; --warn-bg: #3a2e12;
    --bad: #ff8a80; --bad-bg: #3d1c1a; --info: #8ab8f0; --info-bg: #1a2a40;
    --neutral: #c0c6ce; --neutral-bg: #2a3038;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 0 0 8px; }

/* ---- layout ---- */
.layout { display: grid; grid-template-columns: 220px 1fr; height: 100vh; }
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 16px 10px; gap: 2px; overflow-y: auto;
}
.brand { font-weight: 700; font-size: 16px; padding: 4px 10px 16px; }
.brand small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }
.nav a {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px; border-radius: 8px; color: var(--text-2); text-decoration: none;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav .badge { background: var(--bad); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 7px; font-weight: 600; }
.sidebar .spacer { flex: 1; }
.userbox { border-top: 1px solid var(--border); padding: 12px 10px 0; font-size: 13px; color: var(--text-2); }
.main { overflow-y: auto; min-width: 0; }
.page { padding: 24px; max-width: 1200px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.menu-btn { display: none; }

/* ---- controls ---- */
.btn {
  border: 1px solid var(--border); background: var(--surface); padding: 7px 12px; border-radius: 8px;
  cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.small { padding: 4px 9px; font-size: 13px; }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .5; cursor: default; }
input[type=text], input[type=password], input[type=number], input[type=search], select, textarea {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 7px 10px; width: 100%;
}
textarea { resize: vertical; min-height: 70px; }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 12px; font-weight: 600; color: var(--text-2); margin-bottom: 4px; }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar > input, .toolbar > select { width: auto; min-width: 160px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.cards { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); margin-bottom: 16px; }
.stat .label { font-size: 12px; color: var(--text-2); }
.stat .value { font-size: 24px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.stat .sub { font-size: 12px; color: var(--muted); }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; color: var(--text-2); font-weight: 600; background: var(--surface-2); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.empty { padding: 32px; text-align: center; color: var(--muted); }

.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.pill.good { color: var(--good); background: var(--good-bg); }
.pill.warn { color: var(--warn); background: var(--warn-bg); }
.pill.bad { color: var(--bad); background: var(--bad-bg); }
.pill.info { color: var(--info); background: var(--info-bg); }
.pill.neutral { color: var(--neutral); background: var(--neutral-bg); }

.banner { padding: 10px 14px; border-radius: 8px; margin-bottom: 12px; font-size: 13px; }
.banner.warn { background: var(--warn-bg); color: var(--warn); }
.banner.info { background: var(--info-bg); color: var(--info); }
.banner.bad { background: var(--bad-bg); color: var(--bad); }

/* ---- auth ---- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth .card { width: 100%; max-width: 380px; }
.auth h1 { margin-bottom: 4px; }
.auth p { color: var(--text-2); margin: 0 0 16px; }

/* ---- inbox ---- */
.inbox { display: grid; grid-template-columns: 320px 1fr 300px; height: 100vh; }
.inbox-list { border-right: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
.inbox-list .head { padding: 12px; border-bottom: 1px solid var(--border); }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 8px; }
.tabs button { border: 0; background: var(--surface-2); border-radius: 999px; padding: 3px 10px; font-size: 12px; cursor: pointer; color: var(--text-2); }
.tabs button.active { background: var(--accent); color: var(--accent-ink); }
.conv-items { overflow-y: auto; flex: 1; }
.conv { padding: 10px 12px; border-bottom: 1px solid var(--border); cursor: pointer; display: block; color: inherit; text-decoration: none; }
#back { display: none; }
.conv:hover { background: var(--surface-2); }
.conv.active { background: var(--accent-soft); }
.conv .top { display: flex; justify-content: space-between; gap: 8px; }
.conv .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .time { font-size: 11px; color: var(--muted); white-space: nowrap; }
.conv .preview { font-size: 13px; color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; }
.conv .meta { display: flex; gap: 4px; margin-top: 4px; flex-wrap: wrap; }
.unread-dot { background: var(--accent); color: var(--accent-ink); border-radius: 999px; font-size: 11px; padding: 0 6px; font-weight: 700; }

.chat { display: flex; flex-direction: column; min-height: 0; background: var(--chat-bg); }
.chat-head { background: var(--surface); border-bottom: 1px solid var(--border); padding: 10px 16px; display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.chat-head .who { font-weight: 600; }
.chat-head .who small { display: block; font-weight: 400; color: var(--muted); }
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 72%; padding: 7px 10px 5px; border-radius: 10px; box-shadow: var(--shadow); white-space: pre-wrap; word-wrap: break-word; }
.msg.in { align-self: flex-start; background: var(--bubble-in); }
.msg.out { align-self: flex-end; background: var(--bubble-out); }
.msg.agent { background: var(--bubble-agent); }
.msg.note { align-self: center; background: var(--bubble-note); font-size: 13px; max-width: 85%; box-shadow: none; }
.msg .by { font-size: 11px; font-weight: 600; color: var(--text-2); margin-bottom: 2px; }
.msg .ts { font-size: 10px; color: var(--muted); text-align: right; margin-top: 2px; }
.composer { background: var(--surface); border-top: 1px solid var(--border); padding: 10px; }
.composer textarea { min-height: 44px; max-height: 160px; }
.composer .row { justify-content: space-between; margin-top: 6px; }
.composer.internal textarea { background: var(--bubble-note); }

.side { border-left: 1px solid var(--border); background: var(--surface); overflow-y: auto; padding: 16px; }
.side section { margin-bottom: 18px; }
.order-mini { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; margin-bottom: 8px; font-size: 13px; }
.order-mini .row { justify-content: space-between; }

/* ---- chart ---- */
.chart { width: 100%; height: auto; display: block; }
td select { min-width: 130px; }
.chart .bar { fill: var(--bar); }
.chart .bar:hover { opacity: .8; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart text { fill: var(--muted); font-size: 11px; }
.chart-tip { position: fixed; pointer-events: none; background: var(--text); color: var(--surface); padding: 5px 8px; border-radius: 6px; font-size: 12px; display: none; z-index: 50; }
.inline-bar { height: 6px; background: var(--neutral-bg); border-radius: 3px; overflow: hidden; margin-top: 4px; min-width: 60px; }
.inline-bar > div { height: 100%; background: var(--bar); border-radius: 3px; }

/* ---- simulator ---- */
.phone { max-width: 400px; height: 640px; border: 1px solid var(--border); border-radius: 24px; overflow: hidden; display: flex; flex-direction: column; background: var(--chat-bg); box-shadow: var(--shadow); }
.phone .bar { background: var(--accent); color: var(--accent-ink); padding: 12px 16px; font-weight: 600; }
.phone .bar small { display: block; font-weight: 400; opacity: .85; }
.phone .messages .msg { max-width: 82%; }
.phone .messages .msg.in { align-self: flex-end; background: var(--bubble-out); }
.phone .messages .msg.out { align-self: flex-start; background: var(--bubble-in); }
.typing { font-size: 12px; color: var(--muted); padding: 0 16px 6px; min-height: 18px; }
.sim-layout { display: grid; grid-template-columns: 400px 1fr; gap: 24px; align-items: start; }

/* ---- modal & toast ---- */
.modal-bg { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); display: grid; place-items: center; padding: 16px; z-index: 40; }
.modal { background: var(--surface); border-radius: 12px; padding: 20px; width: 100%; max-width: 560px; max-height: 90vh; overflow-y: auto; }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--surface); padding: 10px 16px; border-radius: 8px; display: none; z-index: 60; max-width: 90vw; }
#toast.show { display: block; }
#toast.error { background: var(--bad); color: #fff; }
code { background: var(--surface-2); border: 1px solid var(--border); padding: 1px 5px; border-radius: 4px; font-size: 12px; word-break: break-all; }

/* ---- responsive ---- */
@media (max-width: 1100px) {
  .inbox { grid-template-columns: 280px 1fr; }
  .inbox .side { display: none; }
  .inbox.show-side .side { display: block; position: fixed; right: 0; top: 0; bottom: 0; width: min(340px, 100vw); z-index: 30; box-shadow: var(--shadow); }
}
@media (max-width: 800px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 240px; z-index: 35; transform: translateX(-100%); transition: transform .2s; }
  .layout.nav-open .sidebar { transform: none; box-shadow: var(--shadow); }
  .menu-btn { display: inline-block; position: fixed; top: 10px; left: 10px; z-index: 36; }
  .page { padding: 56px 16px 16px; }
  .inbox { grid-template-columns: 1fr; height: 100vh; padding-top: 48px; }
  .inbox .inbox-list { display: flex; }
  .inbox .chat { display: none; }
  .inbox.has-chat .inbox-list { display: none; }
  .inbox.has-chat .chat { display: flex; }
  #back { display: inline-block; }
  .grid2, .sim-layout { grid-template-columns: 1fr; }
  .msg { max-width: 88%; }
}
