:root {
  --bg: #f6f7f9; --surface: #fff; --text: #1d2330; --muted: #667085; --border: #e3e6eb;
  --primary: #1d5fd1; --primary-ink: #fff; --ok-bg: #e8f6ee; --ok: #146c3a;
  --warn-bg: #fff6e0; --warn: #8a5a00; --bad-bg: #fdecec; --bad: #b42318;
  --ai-bg: #f1effd; --ai: #4b3fb4; --radius: 10px;
  --p1: #b42318; --p2: #c4561a; --p3: #8a6d00; --p4: #4a5568;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151c; --surface: #1a2029; --text: #e6e9ef; --muted: #9aa4b2; --border: #2b3340;
    --primary: #5b8ff0; --ok-bg: #12301f; --ok: #6fd39a; --warn-bg: #33280c; --warn: #f2c55c;
    --bad-bg: #3a1514; --bad: #f6928a; --ai-bg: #231f3d; --ai: #b3a8ff;
    --p1: #f6928a; --p2: #f5a66b; --p3: #e6c75a; --p4: #a8b3c2;
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.45rem; margin: .2rem 0 .6rem; }
h2 { font-size: 1.05rem; margin: 0 0 .6rem; }
h3 { font-size: .95rem; margin: 1rem 0 .4rem; }

.topbar { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; padding: .6rem 1rem; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; }
.brand { font-weight: 700; color: var(--text); }
.nav { display: flex; gap: .2rem; flex-wrap: wrap; flex: 1; }
.nav a { padding: .35rem .65rem; border-radius: 8px; color: var(--muted); }
.nav a.on, .nav a:hover { background: var(--bg); color: var(--text); text-decoration: none; }
.logout { display: flex; align-items: center; gap: .5rem; margin: 0; }
.who { color: var(--muted); font-size: .9rem; max-width: 14rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.container { max-width: 1180px; margin: 0 auto; padding: 1.2rem 1rem 3rem; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.1rem; margin-bottom: 1rem; }
.card.narrow { max-width: 430px; margin: 3rem auto; }
.card.highlight { border-color: var(--primary); }
.card.ai { background: var(--ai-bg); border-color: transparent; }
.card.empty { text-align: center; color: var(--muted); }
.pagehead { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; flex-wrap: wrap; margin-bottom: .6rem; }
.meta { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; color: var(--muted); font-size: .9rem; }

.stack { display: flex; flex-direction: column; gap: .8rem; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.row.spread { justify-content: space-between; }
.grid2, .grid3 { display: grid; gap: .8rem; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.cols { display: grid; grid-template-columns: minmax(0, 1fr) 330px; gap: 1rem; align-items: start; }
@media (max-width: 860px) { .cols, .grid2, .grid3 { grid-template-columns: 1fr; } }

label { display: flex; flex-direction: column; gap: .3rem; font-weight: 500; font-size: .92rem; }
label.radio, label.check { flex-direction: row; align-items: center; gap: .5rem; font-weight: 400; }
label.danger { color: var(--bad); }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: .6rem .9rem; display: flex; flex-direction: column; gap: .35rem; }
legend { font-weight: 500; font-size: .92rem; padding: 0 .3rem; }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: .5rem .65rem; width: 100%; }
input[type=checkbox], input[type=radio] { width: auto; }
.row input, .row select { width: auto; flex: 1 1 10rem; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: transparent; }
textarea { resize: vertical; }
.otp { font-size: 1.6rem; letter-spacing: .4rem; text-align: center; font-family: ui-monospace, monospace; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: .3rem; padding: .5rem .95rem; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap; }
.btn:hover { text-decoration: none; border-color: var(--muted); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.ghost { background: transparent; }
.btn.small { padding: .25rem .6rem; font-size: .85rem; }
.btn.link { border: 0; background: none; color: var(--primary); padding: 0; }
.inline-form { margin: .6rem 0 0; }

.alert { border-radius: var(--radius); padding: .7rem .9rem; margin-bottom: 1rem; }
.alert ul { margin: .3rem 0; padding-left: 1.2rem; }
.alert.ok { background: var(--ok-bg); color: var(--ok); }
.alert.warn { background: var(--warn-bg); color: var(--warn); }
.alert.err { background: var(--bad-bg); color: var(--bad); }
.alert a { color: inherit; text-decoration: underline; }

.muted { color: var(--muted); }
.small { font-size: .85rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: .9em; }
.pre { white-space: pre-wrap; overflow-wrap: anywhere; }
.bad { color: var(--bad); }
.num { text-align: right; }

.pill { display: inline-block; padding: .05rem .45rem; border-radius: 6px; font-size: .78rem; font-weight: 600; border: 1px solid currentColor; }
.pP1 { color: var(--p1); } .pP2 { color: var(--p2); } .pP3 { color: var(--p3); } .pP4 { color: var(--p4); }
.st { display: inline-block; padding: .05rem .5rem; border-radius: 999px; font-size: .8rem; background: var(--bg); border: 1px solid var(--border); }
.st-new { background: #e7efff; color: #1d4fb0; border-color: transparent; }
.st-waiting_user { background: var(--warn-bg); color: var(--warn); border-color: transparent; }
.st-resolved, .st-closed { background: var(--ok-bg); color: var(--ok); border-color: transparent; }
.tag { display: inline-block; padding: 0 .4rem; border-radius: 6px; font-size: .75rem; background: var(--bg); color: var(--muted); border: 1px solid var(--border); }
.tag.ai { background: var(--ai-bg); color: var(--ai); border-color: transparent; }
.tag.warn { background: var(--warn-bg); color: var(--warn); border-color: transparent; }

.table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; margin-bottom: 1rem; }
.table th, .table td { text-align: left; padding: .55rem .7rem; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th { font-size: .8rem; color: var(--muted); font-weight: 600; background: var(--bg); }
.table tr:last-child td { border-bottom: 0; }
.table.compact { border: 0; }
@media (max-width: 700px) { .table { display: block; overflow-x: auto; } }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .7rem; margin-bottom: 1rem; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .8rem 1rem; color: var(--text); display: flex; flex-direction: column; }
a.stat:hover { text-decoration: none; border-color: var(--muted); }
.stat b { font-size: 1.6rem; line-height: 1.2; }
.stat span { color: var(--muted); font-size: .85rem; }
.stat.bad { background: var(--bad-bg); } .stat.bad b { color: var(--bad); }
.stat.warn { background: var(--warn-bg); } .stat.warn b { color: var(--warn); }

.queue { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .5rem; }
.qitem { display: flex; gap: .8rem; background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--border); border-radius: 0 var(--radius) var(--radius) 0; padding: .7rem .9rem; }
.qitem.risk { border-left-color: var(--warn); }
.qitem.breached { border-left-color: var(--bad); }
.qitem.paused { opacity: .7; }
.qscore { min-width: 2.6rem; text-align: center; font-weight: 700; font-size: 1.1rem; color: var(--muted); }
.qbody { flex: 1; min-width: 0; }
.qtitle { color: var(--text); font-weight: 600; }
.qmeta { color: var(--muted); font-size: .85rem; }
.qreasons { font-size: .85rem; margin-top: .15rem; }
.breached .qreasons { color: var(--bad); }

.sla { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .7rem .9rem; margin-bottom: 1rem; }
.sla.risk { background: var(--warn-bg); color: var(--warn); border-color: transparent; }
.sla.breached { background: var(--bad-bg); color: var(--bad); border-color: transparent; }
.bar { height: 6px; background: var(--border); border-radius: 3px; margin-top: .45rem; overflow: hidden; }
.bar span { display: block; height: 100%; background: currentColor; opacity: .7; }

.timeline { list-style: none; margin: 0 0 1rem; padding: 0; }
.ev { padding: .6rem 0; border-bottom: 1px solid var(--border); }
.ev:last-child { border-bottom: 0; }
.ev.internal { background: var(--warn-bg); margin: 0 -.6rem; padding: .6rem; border-radius: 8px; border-bottom: 0; margin-bottom: .3rem; }
.evhead { display: flex; gap: .5rem; align-items: baseline; flex-wrap: wrap; font-size: .9rem; }
.evbody { white-space: pre-wrap; overflow-wrap: anywhere; margin-top: .15rem; }

.searchbar, .filters { display: flex; gap: .5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.searchbar input { flex: 1 1 16rem; width: auto; }
.filters input, .filters select { width: auto; flex: 1 1 10rem; }
.list { margin: 0; padding-left: 1.1rem; }
.list li { margin: .3rem 0; }
.suggest { background: var(--ok-bg); color: var(--ok); border-radius: var(--radius); padding: .6rem .9rem; }
.suggest ul { margin: .3rem 0 0; padding-left: 1.1rem; }
.suggest a { color: inherit; text-decoration: underline; }
.action form { margin-top: .5rem; }

.center { text-align: center; }
.chatbox { padding: 0; overflow: hidden; }
.webchat { height: min(70vh, 640px); min-height: 380px; display: flex; flex-direction: column; justify-content: center; background: var(--surface); }
.webchat > div { flex: 1; }
.chat { display: flex; flex-direction: column; gap: .7rem; }
.chatlog { display: flex; flex-direction: column; gap: .7rem; max-height: 62vh; min-height: 240px; overflow-y: auto; padding: .2rem; }
.msg { display: flex; flex-direction: column; max-width: 85%; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg .bubble { white-space: pre-wrap; overflow-wrap: anywhere; padding: .6rem .85rem; border-radius: 12px; background: var(--bg); border: 1px solid var(--border); }
.msg.user .bubble { background: var(--primary); color: var(--primary-ink); border-color: transparent; }
.msg.agent .bubble { background: var(--ai-bg); border-color: transparent; }
.msg.error .bubble { background: var(--bad-bg); color: var(--bad); border-color: transparent; }
.msg .bubble code { font-family: ui-monospace, monospace; font-size: .9em; }
.msg .bubble .chat-table { white-space: normal; overflow-x: auto; margin: .3rem 0; }
.msg .bubble .chat-table table { margin: 0; background: var(--surface); border-radius: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .4rem; }
.chips form { margin: 0; }
.chatform { display: flex; gap: .5rem; align-items: flex-end; }
.chatform textarea { flex: 1; }
.typing { color: var(--muted); font-size: .9rem; }
.htmx-indicator { display: none; }
.htmx-request.htmx-indicator, .htmx-request .htmx-indicator { display: block; }
