:root {
  --bg: #f5f6f8; --panel: #fff; --text: #1c2230; --muted: #6b7385; --line: #e3e6ec;
  --brand: #128c7e; --brand-soft: #e3f4f1; --warn: #b7791f; --warn-soft: #fdf3e1;
  --danger: #c53030; --danger-soft: #fdecec; --info: #2b6cb0; --info-soft: #e8f1fb;
  --bubble-in: #fff; --bubble-out: #dcf8c6; --bubble-ai: #e8f1fb;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b; --panel: #1b1f27; --text: #e6e9ef; --muted: #98a0b3; --line: #2b313d;
    --brand: #25d0b8; --brand-soft: #173a36; --warn: #f0b45a; --warn-soft: #3a2f1b;
    --danger: #f56565; --danger-soft: #3b1f22; --info: #63b3ed; --info-soft: #1b2b3d;
    --bubble-in: #232833; --bubble-out: #1f3b2c; --bubble-ai: #1d2b3e;
  }
}
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
.hidden { display: none !important; }
h1, h2, h3 { margin: 0 0 12px; line-height: 1.2; }
h2 { font-size: 20px; } h3 { font-size: 15px; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1em; }
button { font: inherit; cursor: pointer; border: 1px solid var(--brand); background: var(--brand); color: #fff; padding: 7px 14px; border-radius: 6px; }
button.secondary { background: transparent; color: var(--brand); }
button.danger { background: var(--danger); border-color: var(--danger); }
button.link { background: none; border: none; color: var(--muted); padding: 0; text-decoration: underline; }
button:disabled { opacity: .5; cursor: default; }
input, select, textarea { font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 7px 9px; width: 100%; }
textarea { resize: vertical; }
label { display: block; margin-bottom: 10px; color: var(--muted); font-size: 13px; }
label > input, label > select, label > textarea { margin-top: 4px; color: var(--text); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 16px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }

.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; }
.login-card button { width: 100%; margin-top: 6px; }

.shell { display: grid; grid-template-columns: 210px 1fr; min-height: 100vh; }
.nav { background: var(--panel); border-right: 1px solid var(--line); padding: 16px 10px; display: flex; flex-direction: column; }
.brand { font-weight: 700; font-size: 16px; padding: 0 8px 16px; color: var(--brand); }
.nav nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav a { color: var(--text); text-decoration: none; padding: 7px 10px; border-radius: 6px; display: flex; justify-content: space-between; }
.nav a.active, .nav a:hover { background: var(--brand-soft); color: var(--brand); }
.me { padding: 10px 8px 0; border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
.view { padding: 20px; min-width: 0; }

.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.stat .value { font-size: 26px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }

table { width: 100%; border-collapse: collapse; background: var(--panel); }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 12px; background: var(--line); color: var(--text); white-space: nowrap; }
.badge.ai { background: var(--info-soft); color: var(--info); }
.badge.human { background: var(--warn-soft); color: var(--warn); }
.badge.paused, .badge.failed, .badge.cancelled { background: var(--danger-soft); color: var(--danger); }
.badge.open, .badge.new, .badge.approved, .badge.published, .badge.completed { background: var(--brand-soft); color: var(--brand); }
.badge.pending_agent { background: var(--warn-soft); color: var(--warn); }

.inbox { display: grid; grid-template-columns: 320px 1fr 280px; gap: 12px; height: calc(100vh - 40px); }
.inbox > * { min-height: 0; }
.list { overflow-y: auto; padding: 0; }
.list .filters { padding: 10px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); display: grid; gap: 6px; }
.conv { padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.conv:hover, .conv.active { background: var(--brand-soft); }
.conv .top { display: flex; justify-content: space-between; gap: 6px; }
.conv .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .preview { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; }
.unread { background: var(--brand); color: #fff; border-radius: 99px; padding: 0 7px; font-size: 12px; }

.chat { display: flex; flex-direction: column; padding: 0; }
.chat-head { padding: 10px 14px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; align-items: center; }
.msgs { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 6px; }
.bubble { max-width: 72%; padding: 7px 10px; border-radius: 10px; background: var(--bubble-in); border: 1px solid var(--line); white-space: pre-wrap; word-wrap: break-word; }
.bubble.out { align-self: flex-end; background: var(--bubble-out); }
.bubble.ai { background: var(--bubble-ai); }
.bubble .meta { font-size: 11px; color: var(--muted); margin-top: 3px; }
.composer { border-top: 1px solid var(--line); padding: 10px; display: flex; gap: 8px; }
.composer textarea { min-height: 42px; max-height: 140px; }
.side { overflow-y: auto; }
.side dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; font-size: 13px; }
.side dt { color: var(--muted); }
.side dd { margin: 0; word-break: break-word; }
.side section { margin-bottom: 16px; }
.note { font-size: 13px; padding: 6px 8px; background: var(--warn-soft); border-radius: 6px; margin-bottom: 4px; }

.split { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.testchat .msgs { min-height: 320px; max-height: 55vh; border: 1px solid var(--line); border-radius: 8px; margin-bottom: 8px; }
pre.small { font-size: 12px; background: var(--bg); padding: 8px; border-radius: 6px; overflow-x: auto; max-height: 220px; }

.toast { position: fixed; bottom: 16px; right: 16px; background: var(--text); color: var(--panel); padding: 10px 14px; border-radius: 8px; max-width: 360px; z-index: 10; }

@media (max-width: 1100px) { .inbox { grid-template-columns: 280px 1fr; } .inbox .side { display: none; } }
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .nav { flex-direction: row; flex-wrap: wrap; border-right: none; border-bottom: 1px solid var(--line); padding: 8px 16px; gap: 8px; }
  .nav nav { flex-direction: row; overflow-x: auto; }
  .brand { padding: 6px 0; }
  .me { border-top: none; padding: 0; flex-direction: row; align-items: center; }
  .view { padding: 16px; }
  .inbox { grid-template-columns: 1fr; height: auto; }
  .inbox .list { max-height: 40vh; }
  .inbox .chat { height: 70vh; }
  .split { grid-template-columns: 1fr; }
  .bubble { max-width: 90%; }
}

/* media in chat bubbles */
.media-img { display: block; max-width: 260px; max-height: 260px; border-radius: 8px; margin-bottom: 4px; background: var(--line); min-height: 60px; }
.media-img.sticker { max-width: 120px; background: none; }
.media-video { display: block; max-width: 280px; border-radius: 8px; margin-bottom: 4px; }
.bubble audio { display: block; max-width: 260px; margin-bottom: 4px; }
.media-doc { display: table; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px; color: var(--text); text-decoration: none; margin-bottom: 4px; background: var(--panel); }
.quote { font-size: 12px; color: var(--muted); border-left: 3px solid var(--brand); padding-left: 6px; margin-bottom: 4px; }
.reactions { margin-top: 2px; }
.reaction { font-size: 14px; margin-right: 2px; }
.ticks .read { color: var(--info); }
.bubble .react { font-size: 12px; text-decoration: none; opacity: .5; }
.bubble .react:hover { opacity: 1; }
.composer #attachBtn { padding: 7px 10px; min-width: 44px; flex-shrink: 0; font-size: 16px; }

/* AI review */
.rate button { text-decoration: none; opacity: .45; font-size: 13px; margin-left: 2px; }
.rate button:hover, .rate button.chosen { opacity: 1; }
.cell-text { max-width: 280px; white-space: pre-wrap; word-break: break-word; font-size: 13px; }
.ctx-out h4 { margin: 12px 0 6px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.ctx-out ul { margin: 0; padding-left: 18px; }
