/* A quiet working tool: white cards on a warm grey ground, JoyLearn blue for actions, LINE green
   for our side of a conversation, one red for what is important. */
:root {
  --ground: #f4f3ef;
  --card: #ffffff;
  --ink: #1d2320;
  --muted: #6a716c;
  --rule: #e2e0d8;
  --rule-strong: #cfccc1;
  --accent: #1570ef;
  --accent-deep: #0d56c2;
  --accent-wash: #e9f1fe;
  --green: #06c755;
  --green-deep: #04883b;
  --green-wash: #e3f6ea;
  --red: #c4321f;
  --red-wash: #fdebe7;
  --amber: #a15c00;
  --amber-wash: #fcf0dc;
  --radius: 12px;
  --bar: 52px;
  --text: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body { background: var(--ground); color: var(--ink); font: 15px/1.55 var(--text); }
a { color: var(--accent-deep); }
h1, h2, h3, h4 { margin: 0; line-height: 1.3; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
h4 { font-size: 13px; color: var(--muted); margin-top: 6px; }
p { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; word-break: break-all; }
.nowrap { white-space: nowrap; }
.error { color: var(--red); }
.pre { white-space: pre-wrap; word-break: break-word; }
.empty, .loading { padding: 40px 20px; text-align: center; color: var(--muted); }

button, .button, select, input, textarea { font: inherit; color: inherit; }
button, .button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 7px 14px; border: 1px solid var(--rule-strong); border-radius: 8px;
  background: var(--card); cursor: pointer; text-decoration: none; white-space: nowrap;
}
button:hover, .button:hover { border-color: var(--muted); }
button:disabled { opacity: 0.55; cursor: progress; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.primary:hover { background: var(--accent-deep); border-color: var(--accent-deep); }
button.small, .button.small { padding: 4px 10px; font-size: 13px; }
button.on { background: var(--ink); border-color: var(--ink); color: #fff; }
button.link { border: 0; background: none; padding: 0; color: var(--accent-deep); text-decoration: underline; }
button.quiet { border-color: transparent; background: none; color: var(--muted); }
button.quiet:hover { background: var(--ground); border-color: transparent; color: var(--ink); }
input, select, textarea { padding: 7px 10px; border: 1px solid var(--rule-strong); border-radius: 8px; background: #fff; min-width: 0; }
textarea { width: 100%; resize: vertical; line-height: 1.6; }
:focus-visible { outline: 3px solid #9cc2f8; outline-offset: 1px; }
label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
label.check { display: inline-flex; align-items: center; gap: 6px; color: var(--ink); font-size: 14px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.stack { display: grid; gap: 12px; }
/* forms read down the page: fields a sensible width, buttons the size of their words */
.stack > label > input:not([type="checkbox"]), .stack > label > select, .stack > label > .row { max-width: 420px; }
.stack > label > .row > input { flex: 1; }
.stack > button, .stack > .row { justify-self: start; }

/* sign-in */
.signin-page { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.signin { width: min(380px, 100%); display: grid; gap: 14px; padding: 28px; background: var(--card); border: 1px solid var(--rule); border-radius: 16px; }
.signin input { padding: 10px 12px; }
.signin .primary { padding: 10px; }
.signin-logo { width: 132px; height: auto; }

/* top bar */
.topbar { display: flex; align-items: center; gap: 24px; height: var(--bar); padding: 0 18px; background: var(--card); border-bottom: 1px solid var(--rule); position: sticky; top: 0; z-index: 5; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--ink); text-decoration: none; white-space: nowrap; }
.brand-mark { height: 24px; width: auto; }
.topbar nav { display: flex; gap: 4px; }
.topbar nav a { padding: 6px 12px; border-radius: 8px; color: var(--muted); text-decoration: none; white-space: nowrap; }
.topbar nav a.active { background: var(--accent-wash); color: var(--accent-deep); font-weight: 600; }
.me { margin-left: auto; display: flex; gap: 8px; align-items: center; font-size: 14px; white-space: nowrap; }
.role { margin-left: 6px; padding: 1px 7px; border-radius: 99px; background: var(--ground); font-size: 12px; color: var(--muted); }

.page { padding: 22px; max-width: 1240px; margin: 0 auto; display: grid; gap: 16px; }
/* the inbox fills the window below the bar; its columns scroll on their own */
.page.wide { display: block; max-width: none; padding: 0; height: calc(100vh - var(--bar)); height: calc(100dvh - var(--bar)); overflow: hidden; }
.page-head { display: flex; align-items: baseline; gap: 14px; flex-wrap: wrap; }
.head-tools { margin-left: auto; align-self: center; }
.card { background: var(--card); border: 1px solid var(--rule); border-radius: var(--radius); padding: 16px; display: grid; gap: 10px; align-content: start; }

/* tabs */
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tabs button { border: 0; background: none; padding: 6px 10px; border-radius: 8px; color: var(--muted); font-size: 14px; }
.tabs button.active { background: var(--ink); color: #fff; }

/* chips and badges */
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip { display: inline-flex; align-items: center; gap: 2px; padding: 1px 8px; border-radius: 99px; background: var(--ground); font-size: 12px; color: var(--muted); border: 1px solid transparent; white-space: nowrap; }
.chip.staff { background: var(--accent-wash); color: var(--accent-deep); }
.chip.important { background: var(--red-wash); color: var(--red); font-weight: 600; }
.chip.snooze, .chip.done, .chip.assignee { background: #fff; border-color: var(--rule); }
.chip.gone { background: var(--red-wash); color: var(--red); margin-left: 6px; }
.chips.toggles .chip { cursor: pointer; padding: 3px 10px; font-size: 13px; border-color: var(--rule); background: #fff; }
.chips.toggles .chip.staff { background: var(--accent-wash); border-color: #a8c8f8; color: var(--accent-deep); font-weight: 600; }
.chip .ai-mark { font-size: 10px; color: var(--muted); }
.chip.removable button { color: var(--muted); text-decoration: none; margin-left: 2px; }
.badge { padding: 2px 10px; border-radius: 99px; font-size: 12px; font-weight: 700; background: var(--ground); white-space: nowrap; }
.badge.group-important { background: var(--red-wash); color: var(--red); }
.badge.group-normal { background: var(--ground); color: var(--ink); }
.badge.group-long_wait { background: var(--amber-wash); color: var(--amber); }
.badge.group-none { background: var(--green-wash); color: var(--green-deep); }
.badge.staff { background: var(--accent-wash); color: var(--accent-deep); }

.avatar { width: 36px; height: 36px; border-radius: 50%; flex: none; object-fit: cover; background: var(--ground); }
.avatar.initial { display: grid; place-items: center; font-weight: 700; color: var(--muted); }
.avatar.large { width: 44px; height: 44px; }
.avatar.small { width: 26px; height: 26px; font-size: 12px; }

/* the inbox: list | chat */
.inbox { display: grid; grid-template-columns: minmax(300px, 380px) 1fr; grid-template-rows: minmax(0, 1fr); height: 100%; }
.list-pane { display: flex; flex-direction: column; border-right: 1px solid var(--rule); background: var(--card); min-height: 0; }
.list-head { padding: 12px; border-bottom: 1px solid var(--rule); display: grid; gap: 8px; }
.chat-list { overflow-y: auto; flex: 1; }
.group-title { display: flex; align-items: center; gap: 6px; padding: 12px 14px 6px; font-size: 13px; position: sticky; top: 0; background: var(--card); z-index: 1; }
.group-title .count { padding: 0 7px; border-radius: 99px; background: var(--ground); font-size: 12px; }
.group-title .hint { color: var(--muted); font-weight: 400; font-size: 12px; margin-left: auto; }
.group-important .dot { color: var(--red); }
.group-normal .dot { color: #9a9a92; }
.group-pending .dot { color: var(--muted); }
.group-long_wait .dot { color: var(--amber); }
.group-none .dot { color: var(--green); }
.chat-row { display: flex; gap: 10px; padding: 8px 14px; color: inherit; text-decoration: none; border-left: 3px solid transparent; }
.chat-row:hover { background: #fafaf7; }
.chat-row.open { background: var(--accent-wash); border-left-color: var(--accent); }
.chat-row.group-important { border-left-color: var(--red); }
.chat-row-body { min-width: 0; flex: 1; display: grid; gap: 1px; }
.chat-row-top { display: flex; gap: 6px; align-items: center; }
.chat-row .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-row .wait { margin-left: auto; font-size: 12px; color: var(--muted); white-space: nowrap; }
.chat-row.group-important .wait { color: var(--red); font-weight: 600; }
.chat-row.group-long_wait .wait { color: var(--amber); }
.chat-row .summary { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-row-bottom { display: flex; gap: 4px; align-items: center; min-width: 0; }
.chat-row-bottom .chip { flex: none; }
.chat-row .preview { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; margin-left: 2px; }

.chat-pane { min-width: 0; min-height: 0; display: flex; }
.pane-empty { margin: auto; text-align: center; display: grid; gap: 8px; color: var(--muted); padding: 20px; justify-items: center; }
.tally { display: flex; gap: 10px; margin-bottom: 18px; }
.tally div { display: grid; min-width: 92px; padding: 12px 10px; border-radius: var(--radius); background: var(--card); border: 1px solid var(--rule); }
.tally strong { font-size: 26px; color: var(--ink); line-height: 1.2; }
.tally span { font-size: 13px; }
.tally .group-important strong { color: var(--red); }
.tally .group-long_wait strong { color: var(--amber); }
.tally .group-none strong { color: var(--green-deep); }
.chat { display: flex; flex-direction: column; flex: 1; min-width: 0; min-height: 0; }
.chat-head { display: flex; align-items: center; gap: 12px; padding: 12px 18px; background: var(--card); border-bottom: 1px solid var(--rule); }
.chat-head .who { min-width: 0; }
.chat-head h2 { display: flex; align-items: center; min-width: 0; }
.chat-head h2 .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head .back { display: none; font-size: 28px; line-height: 1; padding: 0 6px 4px 0; color: var(--ink); text-decoration: none; }
.head-actions { margin-left: auto; display: flex; gap: 10px; align-items: center; }
.assign { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.chat-body { display: grid; grid-template-columns: 1fr minmax(320px, 400px); grid-template-rows: minmax(0, 1fr); flex: 1; min-height: 0; }
.thread { min-height: 0; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 4px; }
.side { min-height: 0; overflow-y: auto; padding: 14px; display: grid; gap: 12px; align-content: start; border-left: 1px solid var(--rule); background: #f9f8f5; }

.day { align-self: center; margin: 10px 0 4px; padding: 2px 10px; border-radius: 99px; background: #e9e7e0; font-size: 12px; color: var(--muted); }
.event { align-self: center; font-size: 12px; color: var(--muted); }
.bubble-row { display: flex; align-items: flex-end; gap: 6px; max-width: 78%; }
.bubble-row.theirs { align-self: flex-start; }
.bubble-row.ours { align-self: flex-end; flex-direction: row-reverse; }
.bubble { padding: 8px 12px; border-radius: 16px; background: var(--card); border: 1px solid var(--rule); white-space: pre-wrap; word-break: break-word; }
.ours .bubble { background: #d6f5e1; border-color: #bfe9cf; }
/* what a bot or LINE sent on its own: outlined, so a person's answer stands out */
.ours.auto .bubble { background: #fff; border: 1px dashed #9fd9b5; color: #3b4a41; }
.bubble.media { padding: 4px; background: transparent; border: 0; }
.bubble.nofile { background: var(--ground); border-color: var(--rule); color: var(--muted); }
.ours .bubble.nofile { background: #eef8f1; }
.bubble-row .time { font-size: 11px; color: var(--muted); white-space: nowrap; }
.sticker { width: 96px; height: 96px; object-fit: contain; }
.photo { max-width: 240px; max-height: 240px; border-radius: 10px; display: block; }

.ai { border-color: #b9d3f9; }
.ai-top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.reason { font-weight: 600; }
.stale { padding: 6px 10px; border-radius: 8px; background: var(--amber-wash); color: var(--amber); font-size: 13px; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; margin: 0; font-size: 13px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; display: flex; gap: 8px; align-items: center; word-break: break-all; }
.copy-fact { padding: 0 8px; font-size: 12px; border-radius: 6px; color: var(--accent-deep); flex: none; }
.reply-label { display: flex; align-items: baseline; gap: 10px; font-size: 14px; }
.blank-hint { color: var(--amber); font-size: 13px; font-weight: 600; }
textarea.reply { font-size: 15px; background: #fff; resize: none; overflow: hidden; }
select.saved { max-width: 140px; }
.tag-input { width: 110px; padding: 3px 8px; font-size: 13px; border-radius: 99px; }
form.inline { display: inline-flex; }
button.done-button { background: var(--ink); border-color: var(--ink); color: #fff; font-weight: 600; }
button.done-button:hover { background: #000; }
.notes { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.notes li { padding: 8px 10px; border-radius: 8px; background: var(--amber-wash); display: grid; gap: 2px; }
.note-form { display: grid; gap: 6px; justify-items: start; }
ul.plain { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
details summary { cursor: pointer; color: var(--muted); font-size: 13px; }
details > :not(summary) { margin-top: 6px; }

/* customers */
.customers { display: grid; grid-template-columns: 1fr 260px; gap: 16px; align-items: start; }
.customers-main { min-width: 0; }
.filters { display: grid; gap: 8px; margin-bottom: 12px; }
.search-row input.search { flex: 1; max-width: 520px; }
.segments { display: grid; gap: 8px; }
.segment { display: flex; gap: 6px; align-items: baseline; }
.table-wrap, .table-scroll { overflow-x: auto; }
.table-wrap { background: var(--card); border: 1px solid var(--rule); border-radius: var(--radius); }
/* nothing to show yet: no empty box */
.table-wrap:has(> .loading), .table-wrap:has(> .error) { background: none; border: 0; }
table.list { width: 100%; border-collapse: collapse; font-size: 14px; }
table.list th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--rule); white-space: nowrap; }
table.list td { padding: 8px 10px; border-bottom: 1px solid #efede7; vertical-align: top; }
table.list tr.disabled td { color: var(--muted); text-decoration: line-through; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
td.wide { min-width: 160px; }
td.pre { white-space: pre-wrap; max-width: 340px; font-size: 13px; }
td.draw { min-width: 180px; }
.table-note { padding: 10px; }
.customer { display: inline-flex; align-items: center; gap: 8px; color: var(--ink); text-decoration: none; font-weight: 600; }

/* reports */
.reports { display: grid; gap: 16px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.bar { position: relative; height: 20px; background: var(--ground); border-radius: 5px; overflow: hidden; }
.bar span { position: absolute; inset: 0 auto 0 0; background: #a9c9f6; }
.bar.group-important span { background: #f0a597; }
.bar.group-normal span { background: #c9c6bc; }
.bar.group-none span { background: #9fdcb7; }
.bar em { position: relative; font-style: normal; font-size: 12px; padding-left: 6px; line-height: 20px; font-variant-numeric: tabular-nums; }
.tag-row { display: grid; grid-template-columns: 120px 1fr; gap: 10px; align-items: center; font-size: 14px; }

/* settings */
.settings { display: grid; grid-template-columns: 180px 1fr; gap: 16px; align-items: start; }
.side-tabs { display: grid; gap: 2px; }
.side-tabs a { padding: 8px 12px; border-radius: 8px; color: var(--muted); text-decoration: none; white-space: nowrap; }
.side-tabs a.active { background: var(--card); color: var(--ink); font-weight: 600; border: 1px solid var(--rule); }
.settings-body { display: grid; gap: 16px; min-width: 0; }
textarea.guide { font-size: 14px; }
.example, .saved-reply { padding: 10px; border: 1px solid var(--rule); border-radius: 10px; display: grid; gap: 4px; justify-items: start; }
form.card.row input { flex: 1; max-width: 320px; }

/* history review */
.stats { grid-template-columns: repeat(6, 1fr); text-align: center; }
.stats div { display: grid; align-content: start; }
.stats strong { font-size: 22px; }
.stats span { font-size: 12px; color: var(--muted); }
.review { grid-template-columns: 1.2fr 1fr 1fr; gap: 16px; }
.review-convo { display: flex; flex-direction: column; gap: 6px; }
.review-convo .bubble-row { max-width: 100%; }
.review-ai, .review-staff { display: grid; gap: 6px; align-content: start; }
.suggest { padding: 10px; background: var(--accent-wash); border-radius: 10px; }
.review-staff .pre { padding: 10px; background: var(--ground); border-radius: 10px; }
.pager { justify-content: center; }
.grade-controls { margin-left: auto; align-self: center; }
.verdict { padding: 8px 10px; border-radius: 10px; font-size: 13px; background: var(--ground); }
.verdict.good { background: var(--green-wash); }
.verdict.edit { background: var(--amber-wash); }
.verdict.bad { background: var(--red-wash); }

#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); padding: 10px 18px; border-radius: 10px; background: var(--ink); color: #fff; font-size: 14px; z-index: 20; box-shadow: 0 6px 20px rgb(0 0 0 / 0.15); max-width: calc(100vw - 32px); }
#toast.error { background: var(--red); }

/* laptops: an open chat takes the whole width, with a way back to the list */
@media (max-width: 1100px) {
  .inbox:has(.chat) { grid-template-columns: 1fr; }
  .inbox:has(.chat) .list-pane { display: none; }
  .chat-head .back { display: block; }
  .review { grid-template-columns: 1fr; }
  .customers, .two { grid-template-columns: 1fr; }
}

/* phones */
@media (max-width: 760px) {
  :root { --bar: 48px; }
  .topbar { gap: 4px; padding: 0 10px; }
  .brand-name, .who-am-i { display: none; }
  .topbar nav a { padding: 6px 9px; }
  .page { padding: 14px; gap: 12px; }
  .inbox { grid-template-columns: 1fr; }
  /* the list alone until a chat is picked */
  .inbox:not(:has(.chat)) .chat-pane { display: none; }

  /* the chat: a short head, then the AI card, then the conversation, all in one scroll */
  .chat-head { flex-wrap: wrap; padding: 10px 12px; gap: 8px 10px; }
  .chat-head .avatar.large { width: 36px; height: 36px; }
  .chat-head .who { flex: 1; }
  .head-actions { width: 100%; margin-left: 0; justify-content: space-between; }
  .assign-label { display: none; }
  .chat-body { display: flex; flex-direction: column; overflow-y: auto; }
  .chat-body .side { order: -1; }
  .thread, .side { flex: none; min-height: auto; overflow: visible; }
  .thread { padding: 12px; }
  .side { border-left: 0; border-bottom: 1px solid var(--rule); padding: 10px; }
  .bubble-row { max-width: 90%; }

  .tally div { min-width: 70px; }
  .settings { grid-template-columns: 1fr; }
  .side-tabs { display: flex; overflow-x: auto; gap: 4px; }
  .stats { grid-template-columns: repeat(3, 1fr); }
  .tag-row { grid-template-columns: 90px 1fr; }
  .head-tools { margin-left: 0; width: 100%; }

  /* customers as cards: name and time on top, the rest below */
  table.customers-table thead { display: none; }
  table.customers-table tr { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; padding: 10px 12px; border-bottom: 1px solid #efede7; }
  table.customers-table td { padding: 0; border: 0; }
  table.customers-table td.email, table.customers-table td.tags, table.customers-table td.draw { grid-column: 1 / -1; }
  table.customers-table td.email:empty, table.customers-table td.draw:empty { display: none; }
  table.customers-table td.count { display: none; }
  table.customers-table td.last { order: 1; }
  table.customers-table td.email, table.customers-table td.tags, table.customers-table td.draw { order: 2; }
}
