:root {
	--brand: #FECB00; --on-brand: #000; --ink: #17120e; --ink-2: #4d443d; --soft: #7b7068; --line: #ebe5de; --bg: #f7f4f0; --card: #fff;
	--good: #1f8a4c; --bad: #b42318; --wa: #25D366; --radius: 14px;
	--safe-b: env(safe-area-inset-bottom, 0px);
	color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif; color: var(--ink); background: var(--bg); -webkit-font-smoothing: antialiased; }
a { color: inherit; }
button, input, textarea, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--ink); outline-offset: 2px; }

/* Layout */
.shell { display: grid; grid-template-columns: 220px 340px 1fr; height: 100vh; height: 100dvh; }
.nav { background: var(--ink); color: #fff; display: flex; flex-direction: column; padding: 16px 12px; gap: 4px; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; font-weight: 800; }
.brand i { width: 30px; height: 30px; border-radius: 9px; background: var(--brand); color: #000; display: grid; place-items: center; font-style: normal; }
.brand small { display: block; font-weight: 500; color: #b8aea4; font-size: 12px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 10px; text-decoration: none; color: #d9d1c9; font-weight: 600; }
.nav a.on, .nav a:hover { background: rgba(255,255,255,.1); color: #fff; }
.nav .count { margin-left: auto; background: var(--brand); color: #000; border-radius: 10px; font-size: 12px; padding: 0 7px; min-width: 22px; text-align: center; }
.nav .grow { flex: 1; }
.nav .plan { font-size: 12.5px; color: #cfc6bd; padding: 10px 12px; border-top: 1px solid rgba(255,255,255,.1); }
.list { min-width: 0; border-right: 1px solid var(--line); background: var(--card); display: flex; flex-direction: column; min-height: 0; }
.main { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.page { flex: 1; overflow-y: auto; padding: 24px; }
.page-narrow { max-width: 820px; }
.tabs { display: none; }

.head { display: flex; align-items: center; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--line); background: var(--card); min-height: 62px; }
.head h1, .head h2 { margin: 0; font-size: 18px; line-height: 1.2; }
.head .sub { font-size: 12.5px; color: var(--soft); }
.head .sp { flex: 1; min-width: 0; }
.head .back { display: none; }

/* Inbox list */
.filters { display: flex; gap: 6px; padding: 10px 12px; border-bottom: 1px solid var(--line); overflow-x: auto; }
.chip { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 6px 12px; font-size: 13px; font-weight: 600; cursor: pointer; white-space: nowrap; }
.chip.on { background: var(--ink); color: #fff; border-color: var(--ink); }
.search { margin: 10px 12px 0; }
.search input { width: 100%; height: 40px; border: 1px solid var(--line); border-radius: 10px; padding: 0 12px; background: var(--bg); }
.items { overflow-y: auto; flex: 1; }
.item { display: flex; gap: 12px; padding: 12px 14px; border-bottom: 1px solid #f2ede7; text-decoration: none; position: relative; }
.item:hover { background: #fbf8f4; }
.item.on { background: #fff7d1; }
.av { width: 40px; height: 40px; border-radius: 50%; background: #efe7dd; display: grid; place-items: center; font-weight: 700; flex: none; color: var(--ink-2); }
.item .t { flex: 1; min-width: 0; }
.item .row { display: flex; gap: 8px; align-items: baseline; }
.item b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item time { margin-left: auto; font-size: 12px; color: var(--soft); flex: none; }
.item p { margin: 2px 0 0; color: var(--ink-2); font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item.unread b, .item.unread p { color: #000; font-weight: 700; }
.item.unread::before { content: ""; position: absolute; left: 4px; top: 50%; width: 6px; height: 6px; margin-top: -3px; border-radius: 50%; background: #e5383b; }
.pill { display: inline-block; font-size: 11px; font-weight: 700; border-radius: 999px; padding: 1px 8px; background: #f1ece6; color: var(--ink-2); vertical-align: 1px; }
.pill.human { background: #fff1b8; color: #6b5200; }
.pill.ai { background: #e6f0ff; color: #1d4ed8; }
.pill.miss { background: #fde8e8; color: var(--bad); }
.empty { text-align: center; color: var(--soft); padding: 48px 20px; }
.empty b { display: block; color: var(--ink); font-size: 16px; margin-bottom: 4px; }

/* Thread */
.thread { flex: 1; overflow-y: auto; padding: 18px 16px; display: flex; flex-direction: column; gap: 10px; background: var(--bg); }
.m { max-width: 78%; display: flex; flex-direction: column; }
.m .meta { font-size: 11.5px; color: var(--soft); margin: 0 6px 3px; display: flex; gap: 6px; align-items: center; }
.m .b { background: #fff; border: 1px solid var(--line); border-radius: 16px; padding: 9px 13px; white-space: pre-wrap; overflow-wrap: anywhere; }
.m.user { align-self: flex-end; align-items: flex-end; }
.m.user .b { background: var(--ink); color: #fff; border-color: var(--ink); border-bottom-right-radius: 6px; }
.m.bot .b { border-bottom-left-radius: 6px; }
.m.agent .b { border-color: var(--brand); box-shadow: inset 3px 0 0 var(--brand); }
.m .teach { font-size: 12px; font-weight: 700; color: #1d4ed8; background: none; border: 0; padding: 0; cursor: pointer; }
.banner { background: #fff4c2; border-bottom: 1px solid #f0d77a; padding: 8px 16px; font-size: 13.5px; display: flex; gap: 10px; align-items: center; }
.banner .sp { flex: 1; }
.composer { display: flex; gap: 8px; align-items: flex-end; padding: 10px 12px calc(10px + var(--safe-b)); background: #fff; border-top: 1px solid var(--line); }
.composer textarea { flex: 1; resize: none; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; max-height: 140px; min-height: 44px; background: var(--bg); }
.visitor { display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 16px; background: #fff; border-bottom: 1px solid var(--line); }
.visitor a.act { display: inline-flex; align-items: center; gap: 6px; text-decoration: none; font-weight: 600; font-size: 13px; border: 1px solid var(--line); border-radius: 999px; padding: 5px 12px; }
.visitor a.wa { background: #e7f9ee; border-color: #bdeccd; color: #0b6b33; }

/* Buttons & forms */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: 44px; padding: 0 18px; border-radius: 12px; border: 0; background: var(--brand); color: var(--on-brand); font-weight: 700; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:disabled { opacity: .6; cursor: wait; }
.btn.dark { background: var(--ink); color: #fff; }
.btn.ghost { background: #fff; border: 1px solid var(--line); color: var(--ink); }
.btn.sm { height: 36px; padding: 0 12px; font-size: 13.5px; border-radius: 10px; }
.btn.danger { background: #fff; color: var(--bad); border: 1px solid #f1c4c0; }
.icon { width: 44px; padding: 0; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 16px; }
.card h2 { font-size: 16px; margin: 0 0 12px; }
.card h3 { font-size: 14px; margin: 16px 0 6px; }
.muted { color: var(--soft); }
.f { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.f label { font-weight: 600; font-size: 13.5px; }
.f small { color: var(--soft); font-size: 12.5px; }
.f input, .f textarea, .f select { border: 1.5px solid #ddd5cc; border-radius: 10px; padding: 10px 12px; background: #fff; width: 100%; }
.f input:focus, .f textarea:focus, .f select:focus { outline: none; border-color: var(--ink); }
.check { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 12px; }
.check input { width: 18px; height: 18px; margin-top: 2px; accent-color: var(--ink); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.err { color: var(--bad); font-weight: 600; font-size: 13.5px; }
.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; background: var(--ink); color: #f8f3ec; padding: 14px; border-radius: 10px; overflow-x: auto; white-space: pre-wrap; word-break: break-all; }

/* Stats */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px; }
.stat { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.stat span { font-size: 12px; color: var(--soft); font-weight: 700; text-transform: uppercase; letter-spacing: .03em; }
.stat strong { display: block; font-size: 28px; line-height: 1.2; }
.rows .r { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid #f2ede7; }
.rows .r:last-child { border-bottom: 0; }
.rows .r .t { flex: 1; min-width: 0; }
.rows .r .t b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Auth */
.auth { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 24px; background: var(--ink); }
.auth .box { width: min(420px, 100%); background: #fff; border-radius: 20px; padding: 28px; }
.auth h1 { margin: 6px 0 4px; font-size: 24px; }
.auth .brand { color: var(--ink); padding: 0; }

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(23,18,14,.5); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal .box { background: #fff; border-radius: 18px; width: min(560px, 100%); max-height: 90vh; overflow-y: auto; padding: 20px; }
#toast { position: fixed; left: 50%; bottom: calc(84px + var(--safe-b)); transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 12px; font-weight: 600; z-index: 60; box-shadow: 0 10px 30px rgba(0,0,0,.25); }

/* Mobile */
@media (max-width: 900px) {
	.shell { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; }
	.nav { display: none; }
	.list, .main { grid-row: 1; grid-column: 1; }
	.shell.has-conv .list { display: none; }
	.shell:not(.has-conv).inbox .main { display: none; }
	.shell:not(.inbox) .list { display: none; }
	.head .back { display: inline-flex; }
	.list, .main { min-height: 0; }
	.tabs { display: flex; grid-row: 2; grid-column: 1; background: #fff; border-top: 1px solid var(--line); padding-bottom: var(--safe-b); }
	.shell.has-conv .tabs { display: none; }
	.tabs a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 0 6px; font-size: 11.5px; font-weight: 700; text-decoration: none; color: var(--soft); position: relative; }
	.tabs a.on { color: var(--ink); }
	.tabs a svg { width: 22px; height: 22px; }
	.tabs .count { position: absolute; top: 3px; left: 54%; background: #e5383b; color: #fff; border-radius: 9px; font-size: 10.5px; padding: 0 5px; }
	.page { padding: 16px; }
	.stats { grid-template-columns: 1fr 1fr; }
	.grid2 { grid-template-columns: 1fr; }
	.m { max-width: 88%; }
}

/* Knowledge: upload documents */
.drop { position: relative; display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center; border: 2px dashed #d9cfc3; border-radius: 14px; padding: 22px 16px; cursor: pointer; transition: border-color .2s, background .2s; }
.drop:hover, .drop.on { border-color: var(--ink); background: #fffbea; }
.drop input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.drop:focus-within { outline: 3px solid var(--ink); outline-offset: 2px; }
.drop-i { color: var(--ink); }
.drop b { font-size: 15px; }
.drop span, .drop small { color: var(--soft); font-size: 13px; }
#upstate { margin-top: 10px; }
.modal .box.wide { width: min(760px, 100%); }
.imp-list { display: flex; flex-direction: column; gap: 8px; }
.imp { display: flex; gap: 10px; align-items: flex-start; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.imp.off { opacity: .45; }
.imp-b { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.imp-t, .imp textarea, .imp input[data-w], .imp select { border: 1.5px solid #ddd5cc; border-radius: 8px; padding: 7px 10px; width: 100%; background: #fff; font: inherit; }
.imp-t { font-weight: 600; }
.imp summary { cursor: pointer; color: var(--soft); font-size: 13.5px; }
.imp details[open] summary { margin-bottom: 6px; }
.imp details textarea { margin-bottom: 6px; }
.imp-m { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.imp-m select { width: auto; padding: 5px 8px; font-size: 13px; }
.imp-m .muted { font-size: 12.5px; }
