:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #1f2328;
  --muted: #6b7280;
  --border: #e3e5e8;
  --accent: #2563eb;
  --human: #fff4e5;
  --human-border: #f59e0b;
  --danger: #b42318;
}

* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--accent); text-decoration: none; }

.topbar { display: flex; justify-content: space-between; align-items: center; padding: 12px 20px; background: var(--card); border-bottom: 1px solid var(--border); }
.brand { font-weight: 600; color: var(--text); }
.logout { color: var(--muted); }
.topnav { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.pill { padding: 4px 10px; border-radius: 999px; background: #eef0f3; color: var(--text); font-size: 14px; white-space: nowrap; }
.human-pill.on { background: var(--human); color: #92400e; box-shadow: inset 0 0 0 1px var(--human-border); }
[hidden] { display: none !important; }
.container { max-width: 1100px; margin: 0 auto; padding: 20px 16px 40px; }

.card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 16px; }
.muted { color: var(--muted); }
.nowrap { white-space: nowrap; }
.empty { text-align: center; color: var(--muted); padding: 40px 0; }
.alert { background: #fdecea; color: var(--danger); border-radius: 8px; padding: 10px 12px; margin-bottom: 12px; }

button { background: var(--accent); color: #fff; border: 0; border-radius: 8px; padding: 8px 14px; font: inherit; cursor: pointer; }
button.secondary { background: #eef0f3; color: var(--text); }
input, select, textarea { font: inherit; border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; background: #fff; }

.login { max-width: 360px; margin: 60px auto; display: grid; gap: 12px; }
.login h1 { margin: 0 0 4px; font-size: 22px; }
.login label { display: grid; gap: 4px; }

.stats { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }
.stat { --c: var(--accent); display: grid; min-width: 120px; padding: 10px 14px; background: var(--card); border: 1px solid var(--border); border-left: 4px solid var(--c); border-radius: 10px; color: var(--text); }
.stat b { font-size: 22px; }
.stat span { color: var(--muted); font-size: 13px; }
.stat.human { --c: var(--human-border); }
.stat.closed { --c: #9ca3af; }
.stat.active { outline: 2px solid var(--c); }

.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.filters input[type=search] { flex: 1; min-width: 180px; }
.filters .check { display: flex; gap: 6px; align-items: center; cursor: pointer; user-select: none; }
.filters .reset { color: var(--muted); }
.small { font-size: 13px; margin-top: 8px; }
.tickets th a { color: inherit; display: inline-flex; gap: 4px; align-items: center; white-space: nowrap; }
.tickets th .arrow { font-size: 11px; opacity: .35; }
.tickets th.sorted { color: var(--text); }
.tickets th.sorted .arrow { opacity: 1; color: var(--accent); }
.tickets .center { text-align: center; }

.table-wrap { overflow-x: auto; background: var(--card); border: 1px solid var(--border); border-radius: 10px; }
table.tickets { width: 100%; border-collapse: collapse; }
.tickets th, .tickets td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
.tickets th { font-size: 13px; color: var(--muted); font-weight: 500; }
.tickets tbody tr { cursor: pointer; }
.tickets tbody tr:hover { background: #fafbfc; }
.tickets tr.needs-human { background: var(--human); }
.tickets td.preview { max-width: 380px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.badge { --c: #6b7280; display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; color: var(--c); background: color-mix(in srgb, var(--c) 14%, white); white-space: nowrap; }
.status { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; white-space: nowrap; }
.status-new { background: #e0ecff; color: #1d4ed8; }
.status-open { background: #fef3c7; color: #92400e; }
.status-closed { background: #eef0f3; color: var(--muted); }

.back { display: inline-block; margin-bottom: 10px; }
.ticket-head { display: flex; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 14px; }
.ticket-head h1 { margin: 0 0 6px; font-size: 20px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.ticket-head p { margin: 0; }
.flag-line { margin-top: 6px !important; color: #92400e; }
.status-buttons { display: flex; gap: 8px; align-items: flex-start; flex-wrap: wrap; }

.chat { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }
.msg { max-width: 75%; padding: 10px 12px; border-radius: 12px; background: var(--card); border: 1px solid var(--border); }
.msg .meta { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.msg .text { white-space: pre-wrap; word-wrap: break-word; }
.msg-customer { align-self: flex-start; }
.msg-bot { align-self: flex-end; background: #f0f4ff; }
.msg-operator { align-self: flex-end; background: #e8f7ee; border-color: #bfe5cc; }
.delivery-error { margin-top: 6px; font-size: 13px; color: var(--danger); }

.reply { display: grid; gap: 10px; }
.reply textarea { width: 100%; resize: vertical; }
.reply button { justify-self: end; }

@media (max-width: 640px) {
  .msg { max-width: 92%; }
  .tickets th:nth-child(6), .tickets td:nth-child(6) { display: none; }
}
