/* ============================================================
   ผู้ช่วยขาย AI — chat tester
   Two tones only: teal = the product, amber = the moment data lands.
   ============================================================ */

/* ---------- tokens ---------- */
:root {
  color-scheme: light dark;

  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", "Leelawadee UI",
             "Noto Sans Thai", "Sukhumvit Set", "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "Cascadia Mono", SFMono-Regular, Menlo, Consolas,
               "Liberation Mono", monospace;

  --bg: #e9edf1;
  --surface: #ffffff;
  --surface-2: #f3f6f8;
  --surface-3: #eaeff3;
  --line: #d9e0e7;
  --line-soft: #e6ebf0;

  --text: #0f151a;
  --text-2: #39434e;
  --muted: #6c7885;
  --faint: #98a3af;

  --brand: #0d7d74;
  --brand-hover: #0a6a62;
  --brand-ink: #ffffff;
  --brand-soft: rgba(13, 125, 116, 0.10);
  --brand-line: rgba(13, 125, 116, 0.35);

  --capture: #a4620a;
  --capture-line: #e0982b;
  --capture-soft: rgba(224, 152, 43, 0.20);
  --capture-glow: rgba(224, 152, 43, 0.45);

  --danger: #ab3419;
  --danger-soft: rgba(171, 52, 25, 0.09);
  --danger-line: rgba(171, 52, 25, 0.30);

  --shadow-sm: 0 1px 2px rgba(15, 21, 26, 0.05);
  --shadow-md: 0 1px 2px rgba(15, 21, 26, 0.05), 0 10px 26px -14px rgba(15, 21, 26, 0.35);

  --r-lg: 16px;
  --r-md: 11px;
  --r-sm: 7px;

  --gap: 14px;
  --pad: 18px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0e11;
    --surface: #141a1e;
    --surface-2: #1a2126;
    --surface-3: #202930;
    --line: #29333a;
    --line-soft: #222b32;

    --text: #e9eef2;
    --text-2: #bcc7d0;
    --muted: #8896a3;
    --faint: #667382;

    --brand: #2ec4b2;
    --brand-hover: #45d2c1;
    --brand-ink: #04231f;
    --brand-soft: rgba(46, 196, 178, 0.12);
    --brand-line: rgba(46, 196, 178, 0.38);

    --capture: #f0ab4a;
    --capture-line: #f0ab4a;
    --capture-soft: rgba(240, 171, 74, 0.16);
    --capture-glow: rgba(240, 171, 74, 0.40);

    --danger: #ef7c5e;
    --danger-soft: rgba(239, 124, 94, 0.10);
    --danger-line: rgba(239, 124, 94, 0.30);

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 1px 2px rgba(0, 0, 0, 0.4), 0 14px 30px -18px rgba(0, 0, 0, 0.9);
  }
}

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.7;                 /* Thai vowel/tone marks need the air */
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

h1, h2, p, pre { margin: 0; }
button { font: inherit; color: inherit; }

::selection { background: var(--brand-soft); }

:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
  border-radius: 4px;
}

.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- buttons ---------- */
.btn {
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 8px 18px;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.4;
  cursor: pointer;
  background: var(--surface);
  transition: background-color .14s ease, border-color .14s ease,
              color .14s ease, opacity .14s ease;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }

.btn--ghost { border-color: var(--line); color: var(--text-2); background: var(--surface); }
.btn--ghost:hover:not(:disabled) { border-color: var(--brand-line); color: var(--brand); }

.btn--send { background: var(--brand); color: var(--brand-ink); padding-inline: 24px; }
.btn--send:hover:not(:disabled) { background: var(--brand-hover); }

.btn--mini {
  padding: 3px 11px;
  font-size: 12px;
  font-weight: 600;
  border-color: var(--line);
  color: var(--muted);
  background: transparent;
  border-radius: var(--r-sm);
}
.btn--mini:hover:not(:disabled) { color: var(--brand); border-color: var(--brand-line); }
.btn--mini.is-done { color: var(--brand); border-color: var(--brand-line); background: var(--brand-soft); }

/* ---------- top bar ---------- */
.topbar {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px clamp(14px, 3vw, 26px);
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }

.brand__mark {
  flex: none;
  width: 34px; height: 34px;
  border-radius: 10px;
  background: var(--brand);
  position: relative;
}
.brand__mark::before,
.brand__mark::after {
  content: ""; position: absolute; border-radius: 2px; background: var(--brand-ink);
}
.brand__mark::before { left: 9px; top: 11px; width: 16px; height: 2.5px; }
.brand__mark::after  { left: 9px; top: 18px; width: 10px; height: 2.5px; opacity: .55; }

.brand__text { min-width: 0; }
.brand__title {
  font-size: 16px; font-weight: 700; letter-spacing: -0.01em; line-height: 1.35;
}
.brand__sub {
  font-size: 12.5px; color: var(--muted); line-height: 1.4;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.topbar__tools { display: flex; align-items: center; gap: 12px; flex: none; }

/* compact progress — only useful when the panel is stacked out of sight */
.minibar { display: none; align-items: center; gap: 8px; }
.minibar__count {
  font-size: 12px; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--text-2);
}
.minibar__track {
  display: block; width: 64px; height: 5px; border-radius: 999px;
  background: var(--surface-3); overflow: hidden;
}
.minibar__fill {
  display: block; height: 100%; width: 0%; border-radius: 999px;
  background: var(--brand); transition: width .4s cubic-bezier(.2,.8,.25,1);
}
.minibar.is-pulse .minibar__count { animation: pulseTint 1.5s ease-out; }

/* ---------- layout ---------- */
.layout {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, 400px);
  gap: var(--gap);
  padding: var(--gap) clamp(14px, 3vw, 26px);
}

/* ---------- chat ---------- */
.chat {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.chat__log {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: var(--pad);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* message */
.msg { max-width: min(78%, 620px); display: flex; flex-direction: column; gap: 4px; }
.msg--user { align-self: flex-end; align-items: flex-end; }
.msg--bot, .msg--error, .msg--note { align-self: flex-start; }

.msg__who {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .04em;
  color: var(--faint);
  padding-inline: 4px;
}

.msg__body {
  padding: 10px 14px;
  border-radius: var(--r-lg);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.75;
  animation: msgIn .28s cubic-bezier(.2,.8,.25,1) both;
}

.msg--bot .msg__body {
  background: var(--surface-2);
  border: 1px solid var(--line-soft);
  border-bottom-left-radius: 5px;
  color: var(--text);
}
.msg--user .msg__body {
  background: var(--brand);
  color: var(--brand-ink);
  border-bottom-right-radius: 5px;
}

/* a user turn the server rolled back — shown, but visibly not delivered */
.msg--unsent .msg__body {
  opacity: .5;
  filter: grayscale(.6);
}

.msg--error { max-width: min(88%, 620px); }
.msg--error .msg__body {
  background: var(--danger-soft);
  border: 1px solid var(--danger-line);
  color: var(--danger);
  border-bottom-left-radius: 5px;
  font-size: 14px;
}
.msg--error .msg__retry {
  margin-top: 8px;
  display: inline-block;
  border: 1px solid var(--danger-line);
  background: transparent;
  color: var(--danger);
  border-radius: var(--r-sm);
  padding: 3px 12px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.msg--error .msg__retry:hover { background: var(--danger-soft); }

.msg--note {
  align-self: center;
  max-width: 100%;
  font-size: 12px;
  color: var(--faint);
  padding: 2px 12px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--line-soft);
  animation: msgIn .28s ease both;
}

/* typing indicator */
.typing__body { display: flex; gap: 5px; align-items: center; padding-block: 15px; }
.typing__dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--muted);
  animation: typingBounce 1.15s infinite ease-in-out;
}
.typing__dot:nth-child(2) { animation-delay: .16s; }
.typing__dot:nth-child(3) { animation-delay: .32s; }

/* composer */
.composer {
  flex: none;
  border-top: 1px solid var(--line);
  background: var(--surface);
  padding: 12px var(--pad) 10px;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: end;
  gap: 10px;
}

.composer__input {
  font: inherit;
  line-height: 1.7;
  resize: none;
  max-height: 168px;
  padding: 10px 14px;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  transition: border-color .14s ease, background-color .14s ease;
}
.composer__input::placeholder { color: var(--faint); }
.composer__input:focus { outline: none; border-color: var(--brand-line); background: var(--surface); }
.composer__input:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
.composer__input:disabled { opacity: .6; cursor: progress; }

.composer__hint {
  grid-column: 1 / -1;
  font-size: 11.5px;
  color: var(--faint);
  padding-inline: 4px;
}

/* ---------- extraction panel ---------- */
.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
}

.panel__head {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface);
  border-bottom: 1px solid var(--line-soft);
  padding: 14px var(--pad) 12px;
}
.panel__title { font-size: 14.5px; font-weight: 700; letter-spacing: -0.005em; }
.panel__live {
  display: flex; align-items: center; gap: 6px;
  font-size: 11.5px; color: var(--faint); line-height: 1.5;
}
.panel__live i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--brand);
  animation: heartbeat 2.6s ease-in-out infinite;
}

/* progress */
.progress { padding: 14px var(--pad); border-bottom: 1px solid var(--line-soft); }
.progress__row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  margin-bottom: 8px;
}
.progress__label {
  font-size: 13px; font-weight: 600; color: var(--text-2);
  font-variant-numeric: tabular-nums;
}
.progress__badge {
  font-size: 11.5px; font-weight: 700;
  padding: 2px 10px; border-radius: 999px;
  background: var(--brand); color: var(--brand-ink);
  opacity: 0; visibility: hidden; transform: scale(.9);
  transition: opacity .25s ease, transform .25s cubic-bezier(.2,1.4,.4,1), visibility .25s;
}
.progress__track {
  height: 6px; border-radius: 999px;
  background: var(--surface-3);
  overflow: hidden;
}
.progress__fill {
  display: block; height: 100%; width: 0%;
  border-radius: 999px;
  background: var(--brand);
  transition: width .5s cubic-bezier(.2,.8,.25,1);
}
.panel.is-complete .progress__badge { opacity: 1; visibility: visible; transform: scale(1); }
.panel.is-complete .progress__label { color: var(--brand); }

/* field rows */
.fields {
  list-style: none;
  margin: 0;
  padding: 6px 0;
  flex: 1 1 auto;
}

.field {
  position: relative;
  display: grid;
  grid-template-columns: 16px 1fr;
  gap: 10px;
  padding: 9px var(--pad) 9px calc(var(--pad) - 4px);
  border-radius: 0;
}

/* the rail that sweeps when a value lands — the one loud thing on the page */
.field::before {
  content: "";
  position: absolute;
  left: 0; top: 6px; bottom: 6px;
  width: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--capture-line);
  transform: scaleY(0);
  transform-origin: top center;
  opacity: 0;
}

.field__lamp {
  margin-top: 8px;
  width: 9px; height: 9px;
  border-radius: 50%;
  border: 1.5px solid var(--line);
  background: transparent;
  transition: background-color .3s ease, border-color .3s ease, box-shadow .3s ease;
}
.field.is-filled .field__lamp {
  background: var(--brand);
  border-color: var(--brand);
}

.field__body { min-width: 0; }

.field__top {
  display: flex; align-items: center; gap: 7px;
  min-height: 18px;
}
.field__label {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  line-height: 1.5;
}
.field__req {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .02em;
  padding: 0 6px;
  border-radius: 999px;
  color: var(--brand);
  background: var(--brand-soft);
  line-height: 1.6;
}

/* "ใหม่" / "อัปเดต" tag that pops for the flash duration */
.field__tag {
  font-size: 10px;
  font-weight: 700;
  padding: 0 6px;
  border-radius: 999px;
  color: var(--capture);
  background: var(--capture-soft);
  line-height: 1.6;
  margin-inline-start: auto;
  opacity: 0;
  transform: translateY(2px);
  transition: opacity .2s ease, transform .2s ease;
  pointer-events: none;
}
.field__tag.is-show { opacity: 1; transform: translateY(0); }

.field__value {
  font-size: 14.5px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.6;
  overflow-wrap: anywhere;
}
.field__value.is-empty {
  color: var(--faint);
  font-weight: 400;
  letter-spacing: .12em;
}

/* --- the capture flash --- */
.field.is-flash { animation: rowFlash var(--flash-ms, 1500ms) ease-out; }
.field.is-flash::before { animation: railSweep var(--flash-ms, 1500ms) ease-out; }
.field.is-flash .field__value { animation: valueIn .42s cubic-bezier(.2,.9,.25,1); }
.field.is-flash .field__lamp { animation: lampPop var(--flash-ms, 1500ms) ease-out; }

/* raw json */
.raw {
  flex: none;
  position: sticky;
  bottom: 0;
  background: var(--surface);
  border-top: 1px solid var(--line-soft);
}
.raw__bar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 9px var(--pad);
}
.raw__toggle {
  display: inline-flex; align-items: center; gap: 7px;
  background: none; border: 0; padding: 0;
  font-size: 12.5px; font-weight: 600; color: var(--muted);
  cursor: pointer;
}
.raw__toggle:hover { color: var(--text-2); }
.raw__chev {
  width: 0; height: 0;
  border-left: 5px solid currentColor;
  border-top: 4px solid transparent;
  border-bottom: 4px solid transparent;
  transition: transform .18s ease;
}
.raw__toggle[aria-expanded="true"] .raw__chev { transform: rotate(90deg); }

.raw__body {
  margin: 0 var(--pad) 14px;
  padding: 12px 14px;
  max-height: 260px;
  overflow: auto;
  background: var(--surface-2);
  border: 1px solid var(--line-soft);
  border-radius: var(--r-md);
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.65;
  color: var(--text-2);
  white-space: pre;
  tab-size: 2;
}

/* ---------- status bar ---------- */
.statusbar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px clamp(14px, 3vw, 26px);
  border-top: 1px solid var(--line);
  background: var(--surface);
  font-size: 11.5px;
  color: var(--muted);
}
.statusbar__item { white-space: nowrap; }
.statusbar__item b {
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 11px;
  color: var(--text-2);
}
.statusbar__item--dim b { color: var(--faint); }
.statusbar__sep { width: 1px; height: 11px; background: var(--line); flex: none; }
.statusbar__spacer { flex: 1 1 auto; }

/* ---------- animations ---------- */
@keyframes msgIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: none; }
}

@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .45; }
  30%           { transform: translateY(-4px); opacity: 1; }
}

@keyframes heartbeat {
  0%, 70%, 100% { opacity: .35; }
  35%           { opacity: 1; }
}

@keyframes rowFlash {
  0%   { background-color: var(--capture-soft); }
  55%  { background-color: var(--capture-soft); }
  100% { background-color: transparent; }
}

@keyframes railSweep {
  0%   { transform: scaleY(0); opacity: 1; }
  22%  { transform: scaleY(1); opacity: 1; }
  60%  { opacity: 1; }
  100% { transform: scaleY(1); opacity: 0; }
}

@keyframes valueIn {
  from { opacity: 0; transform: translateX(-6px); }
  to   { opacity: 1; transform: none; }
}

@keyframes lampPop {
  0%   { box-shadow: 0 0 0 0 var(--capture-glow); }
  20%  { box-shadow: 0 0 0 5px var(--capture-glow); }
  100% { box-shadow: 0 0 0 8px rgba(0, 0, 0, 0); }
}

@keyframes pulseTint {
  0%, 60% { color: var(--capture); }
  100%    { color: var(--text-2); }
}

/* ---------- desktop: app fills the viewport, columns scroll on their own ---------- */
@media (min-width: 961px) {
  html, body { height: 100%; }
  body { overflow: hidden; }
}

/* ---------- narrow: stack, chat first, progress moves into the top bar ---------- */
@media (max-width: 960px) {
  .layout {
    grid-template-columns: 1fr;
    align-content: start;
  }
  .topbar { position: sticky; top: 0; z-index: 10; }
  .minibar { display: flex; }
  .brand__sub { display: none; }

  .chat { height: min(66vh, 560px); }
  .panel { max-height: none; overflow: visible; }
  .panel__head, .raw { position: static; }
  .statusbar { flex-wrap: wrap; row-gap: 2px; }
  .statusbar__spacer { display: none; }
}

@media (max-width: 460px) {
  .msg { max-width: 90%; }
  .topbar__tools { gap: 8px; }
  .minibar__track { width: 44px; }
}

/* ---------- reduced motion: keep the signal, drop the movement ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
  .field.is-flash {
    background-color: var(--capture-soft);
    transition: background-color .3s linear !important;
  }
  .field.is-flash::before { transform: scaleY(1); opacity: 1; }
}

/* ============================================================
   §11 — lead priority + sales queue
   One palette, four levels. A `--level` modifier sets --prio / --prio-soft /
   --prio-line / --prio-glow on the block; every part inside reads those, so a
   new level means one mapping rule instead of a dozen.
   Colour never carries the meaning alone: every level also prints its Thai
   label, and the dots differ in shape (solid vs ring) as well as hue.
   ============================================================ */

:root {
  /* green = most urgent here (move in within 15 days), not "all clear" */
  --prio-green: #0f7a3d;
  --prio-green-soft: rgba(15, 122, 61, 0.09);
  --prio-green-line: rgba(15, 122, 61, 0.32);
  --prio-green-glow: rgba(15, 122, 61, 0.20);

  /* pushed off the --capture amber's hue: both share the chat panel, and a
     level colour must not read as "a field just landed". Tints use a lighter
     gold than the text so the fill does not go muddy. */
  --prio-yellow: #7d6a00;
  --prio-yellow-soft: rgba(220, 174, 26, 0.16);
  --prio-yellow-line: rgba(220, 174, 26, 0.46);
  --prio-yellow-glow: rgba(220, 174, 26, 0.30);

  /* unknown outranks grey, so it gets a colour with more voice than grey */
  --prio-unknown: #5a49bd;
  --prio-unknown-soft: rgba(90, 73, 189, 0.09);
  --prio-unknown-line: rgba(90, 73, 189, 0.32);
  --prio-unknown-glow: rgba(90, 73, 189, 0.20);

  --prio-grey: #626e7b;
  --prio-grey-soft: rgba(98, 110, 123, 0.09);
  --prio-grey-line: rgba(98, 110, 123, 0.28);
  --prio-grey-glow: rgba(98, 110, 123, 0.18);
}

@media (prefers-color-scheme: dark) {
  :root {
    --prio-green: #3fbf72;
    --prio-green-soft: rgba(63, 191, 114, 0.14);
    --prio-green-line: rgba(63, 191, 114, 0.40);
    --prio-green-glow: rgba(63, 191, 114, 0.28);

    --prio-yellow: #f5db72;
    --prio-yellow-soft: rgba(245, 219, 114, 0.13);
    --prio-yellow-line: rgba(245, 219, 114, 0.38);
    --prio-yellow-glow: rgba(245, 219, 114, 0.26);

    --prio-unknown: #a596f2;
    --prio-unknown-soft: rgba(165, 150, 242, 0.14);
    --prio-unknown-line: rgba(165, 150, 242, 0.38);
    --prio-unknown-glow: rgba(165, 150, 242, 0.26);

    --prio-grey: #8b98a6;
    --prio-grey-soft: rgba(139, 152, 166, 0.12);
    --prio-grey-line: rgba(139, 152, 166, 0.32);
    --prio-grey-glow: rgba(139, 152, 166, 0.22);
  }
}

/* The JS shows and hides the priority card, the internal group, the states and
   the notes with the `hidden` ATTRIBUTE. Any `display` rule below would beat
   the UA `[hidden] { display: none }`, so restore that once, loudly. */
[hidden] { display: none !important; }

/* ---------- .btn also dresses <a> now (คิวลูกค้า, หน้าแชท, empty state) ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}
.btn--link { text-decoration: none; }

/* ---------- selects: business picker (topbar) and queue filter ---------- */
.picker { display: inline-flex; align-items: center; min-width: 0; }

.picker__select,
.select {
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  line-height: 1.4;
  max-width: 100%;
  color: var(--text-2);
  background: var(--surface-2);
  border: 1px solid var(--line);
  padding: 6px 10px;
  cursor: pointer;
  transition: border-color .14s ease, color .14s ease;
}
.picker__select { border-radius: 999px; padding-inline: 13px; }
.select { border-radius: var(--r-sm); }

.picker__select:hover:not(:disabled),
.select:hover:not(:disabled) { border-color: var(--brand-line); color: var(--brand); }
.picker__select:disabled,
.select:disabled { opacity: .45; cursor: not-allowed; }

/* ---------- level palette: neutral first, modifiers override ---------- */
.prio, .chip, .lead, .legend__item {
  --prio: var(--muted);
  --prio-soft: var(--surface-3);
  --prio-line: var(--line);
  --prio-glow: var(--surface-3);
}

.prio--green, .chip--green, .lead--green, .legend__item--green {
  --prio: var(--prio-green);
  --prio-soft: var(--prio-green-soft);
  --prio-line: var(--prio-green-line);
  --prio-glow: var(--prio-green-glow);
}
.prio--yellow, .chip--yellow, .lead--yellow, .legend__item--yellow {
  --prio: var(--prio-yellow);
  --prio-soft: var(--prio-yellow-soft);
  --prio-line: var(--prio-yellow-line);
  --prio-glow: var(--prio-yellow-glow);
}
.prio--unknown, .chip--unknown, .lead--unknown, .legend__item--unknown {
  --prio: var(--prio-unknown);
  --prio-soft: var(--prio-unknown-soft);
  --prio-line: var(--prio-unknown-line);
  --prio-glow: var(--prio-unknown-glow);
}
.prio--grey, .chip--grey, .lead--grey, .legend__item--grey {
  --prio: var(--prio-grey);
  --prio-soft: var(--prio-grey-soft);
  --prio-line: var(--prio-grey-line);
  --prio-glow: var(--prio-grey-glow);
}
/* untriaged (car-dealer): outside the scale on purpose, so it stays neutral */
.chip--none, .lead--none {
  --prio: var(--faint);
  --prio-soft: transparent;
  --prio-line: var(--line);
  --prio-glow: var(--surface-3);
}
.chip--all {
  --prio: var(--brand);
  --prio-soft: var(--brand-soft);
  --prio-line: var(--brand-line);
}

/* ---------- priority card (chat panel) ---------- */
.prio {
  flex: none;
  display: flex;
  align-items: flex-start;
  gap: 11px;
  padding: 12px var(--pad) 12px calc(var(--pad) - 4px);
  border-inline-start: 4px solid var(--prio);
  border-bottom: 1px solid var(--line-soft);
  background: var(--prio-soft);
}

.prio__dot {
  flex: none;
  margin-top: 4px;
  width: 13px; height: 13px;
  border-radius: 50%;
  background: var(--prio);
}
/* ring instead of a disc: the level reads even without colour vision */
.prio--unknown .prio__dot { background: transparent; box-shadow: inset 0 0 0 3px var(--prio); }

.prio__body { min-width: 0; flex: 1 1 auto; }

.prio__top { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 8px; }

.prio__level {
  font-size: 13px;
  font-weight: 700;
  line-height: 1.5;
  color: var(--prio);
}

.prio__days {
  font-size: 11px;
  font-weight: 700;
  line-height: 1.6;
  padding: 0 8px;
  border-radius: 999px;
  color: var(--prio);
  background: var(--prio-soft);
  border: 1px solid var(--prio-line);
  white-space: nowrap;
}

.prio__action {
  font-size: 12.5px;
  line-height: 1.6;
  color: var(--text-2);
  overflow-wrap: anywhere;
}

/* level changed — same beat as a field row landing, tinted by the new level */
.prio.is-flash { animation: prioFlash var(--flash-ms, 1500ms) ease-out; }
.prio.is-flash .prio__dot { animation: prioDotPop var(--flash-ms, 1500ms) ease-out; }
.prio.is-flash .prio__level { animation: valueIn .42s cubic-bezier(.2,.9,.25,1); }

@keyframes prioFlash {
  0%   { background-color: var(--prio-glow); }
  55%  { background-color: var(--prio-glow); }
  100% { background-color: var(--prio-soft); }
}

/* scale, not box-shadow: the ring dots keep their shape while it plays */
@keyframes prioDotPop {
  0%   { transform: scale(1); }
  22%  { transform: scale(1.4); }
  100% { transform: scale(1); }
}

/* ---------- internal fields group (hidden:true) ---------- */
.internal {
  flex: none;
  border-top: 1px solid var(--line-soft);
}

.internal__summary {
  display: flex;
  align-items: center;
  gap: 7px;
  list-style: none;
  cursor: pointer;
  padding: 9px var(--pad);
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}
.internal__summary::-webkit-details-marker { display: none; }
.internal__summary:hover { color: var(--text-2); }
.internal__summary::before {
  content: "";
  flex: none;
  width: 0; height: 0;
  border-left: 5px solid currentColor;
  border-top: 4px solid transparent;
  border-bottom: 4px solid transparent;
  transition: transform .18s ease;
}
.internal[open] .internal__summary::before { transform: rotate(90deg); }

.fields--internal {
  flex: none;
  padding: 0 0 8px;
  background: var(--surface-2);
  border-top: 1px solid var(--line-soft);
}
.fields--internal .field { padding-block: 7px; }
.fields--internal .field__label { color: var(--faint); }
.fields--internal .field__value { font-family: var(--font-mono); font-size: 12.5px; }

/* ============================================================
   leads.html — สายลูกค้าเรียงตามความเร่งด่วน
   ============================================================ */

.leads {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding: var(--gap) clamp(14px, 3vw, 26px);
}

/* ---------- filter bar ---------- */
.leadbar {
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px var(--pad);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
}

.leadbar__row { display: flex; align-items: center; flex-wrap: wrap; gap: 10px 16px; }
.leadbar__field { display: inline-flex; align-items: center; gap: 8px; min-width: 0; }
.leadbar__label { font-size: 12px; font-weight: 600; color: var(--muted); white-space: nowrap; }
.leadbar__tools { display: flex; align-items: center; gap: 12px; margin-inline-start: auto; }

.leads__note { font-size: 12px; color: var(--muted); }

/* ---------- count chips (they are also the level filter) ---------- */
.chips { display: flex; align-items: center; flex-wrap: wrap; gap: 7px; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-2);
  font-size: 12.5px;
  font-weight: 600;
  line-height: 1.5;
  cursor: pointer;
  transition: background-color .14s ease, border-color .14s ease, color .14s ease;
}
.chip::before {
  content: "";
  flex: none;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--prio);
}
.chip--all::before { display: none; }              /* "ทั้งหมด" is not a bucket */
.chip--unknown::before,
.chip--none::before { background: transparent; box-shadow: inset 0 0 0 2px var(--prio); }

.chip b { font-weight: 700; font-variant-numeric: tabular-nums; color: var(--muted); }
.chip:hover { border-color: var(--prio-line); color: var(--prio); }
.chip:hover b { color: inherit; }
.chip.is-active {
  border-color: var(--prio-line);
  background: var(--prio-soft);
  color: var(--prio);
}
.chip--none.is-active { background: var(--surface-3); }
.chip.is-active b { color: inherit; }

/* ---------- auto refresh toggle ---------- */
.toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  color: var(--muted);
  cursor: pointer;
}
.toggle input { width: 15px; height: 15px; accent-color: var(--brand); cursor: pointer; }
.toggle span { white-space: nowrap; }
.toggle:hover { color: var(--text-2); }

/* ---------- legend ---------- */
.legend { border-top: 1px solid var(--line-soft); padding-top: 9px; }

.legend__sum {
  display: flex;
  align-items: center;
  gap: 7px;
  list-style: none;
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--muted);
}
.legend__sum::-webkit-details-marker { display: none; }
.legend__sum:hover { color: var(--text-2); }
.legend__sum::before {
  content: "";
  flex: none;
  width: 0; height: 0;
  border-left: 5px solid currentColor;
  border-top: 4px solid transparent;
  border-bottom: 4px solid transparent;
  transition: transform .18s ease;
}
.legend[open] .legend__sum::before { transform: rotate(90deg); }

.legend__list {
  list-style: none;
  margin: 9px 0 2px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.legend__item {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px 9px;
  font-size: 12px;
  line-height: 1.6;
}
.legend__dot {
  flex: none;
  width: 9px; height: 9px;
  border-radius: 50%;
  background: var(--prio);
}
.legend__item--unknown .legend__dot { background: transparent; box-shadow: inset 0 0 0 2.5px var(--prio); }
.legend__label { font-weight: 700; color: var(--prio); }
.legend__hint {
  font-size: 11px;
  color: var(--muted);
  background: var(--surface-2);
  border: 1px solid var(--line-soft);
  border-radius: 999px;
  padding: 0 8px;
}
.legend__action { color: var(--faint); }

/* ---------- the queue itself ---------- */
.leads__list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 7px;
}
/* nothing to scroll — hand the space to whichever state block is showing */
.leads__list:empty { flex: none; }

.lead {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  overflow: hidden;
  transition: border-color .14s ease;
}
/* green is the loudest row on the page: solid rail + a tinted body */
.lead--green { background: var(--prio-soft); }
.lead--yellow { background: var(--prio-soft); }
.lead:hover { border-color: var(--prio-line); }
.lead[open] { border-color: var(--prio-line); box-shadow: var(--shadow-sm); }

.lead__head {
  display: grid;
  grid-template-columns: 4px minmax(0, 1fr) auto;
  align-items: stretch;
  list-style: none;
  cursor: pointer;
}
.lead__head::-webkit-details-marker { display: none; }

.lead__bar { background: var(--prio); }

.lead__main {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 9px 12px;
}

.lead__line {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 3px 10px;
  min-width: 0;
}
.lead__line--meta { gap: 2px 14px; }

.lead__level {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 700;
  color: var(--prio);
  white-space: nowrap;
}
.lead__dot {
  flex: none;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--prio);
}
.lead--unknown .lead__dot,
.lead--none .lead__dot { background: transparent; box-shadow: inset 0 0 0 2px var(--prio); }

.lead__days {
  font-size: 11px;
  font-weight: 700;
  line-height: 1.6;
  padding: 0 8px;
  border-radius: 999px;
  color: var(--prio);
  background: var(--prio-soft);
  border: 1px solid var(--prio-line);
  white-space: nowrap;
}
.lead__days--none {
  font-weight: 600;
  color: var(--faint);
  background: var(--surface-2);
  border-color: var(--line);
}

.lead__name { font-size: 14px; font-weight: 700; color: var(--text); overflow-wrap: anywhere; }
.lead__name.is-empty { font-weight: 400; color: var(--faint); }

.lead__phone { font-family: var(--font-mono); font-size: 12.5px; color: var(--text-2); }
.lead__phone.is-empty { font-family: var(--font-ui); font-size: 12px; color: var(--faint); }

.lead__uc {
  font-size: 11px;
  color: var(--muted);
  background: var(--surface-2);
  border: 1px solid var(--line-soft);
  border-radius: 999px;
  padding: 0 8px;
}

.lead__status { font-size: 11px; color: var(--faint); }
.lead__status--done { font-weight: 700; color: var(--brand); }

.lead__time {
  margin-inline-start: auto;
  font-size: 11.5px;
  color: var(--faint);
  white-space: nowrap;
}

.lead__meta { display: inline-flex; align-items: baseline; gap: 5px; min-width: 0; font-size: 12px; color: var(--text-2); }
.lead__meta b { font-weight: 600; color: var(--faint); white-space: nowrap; }
.lead__meta.is-empty { color: var(--faint); }

.lead__chev {
  align-self: center;
  margin-inline: 4px 12px;
  flex: none;
  width: 0; height: 0;
  border-left: 5px solid var(--faint);
  border-top: 4px solid transparent;
  border-bottom: 4px solid transparent;
  transition: transform .18s ease;
}
.lead[open] .lead__chev { transform: rotate(90deg); }

/* ---------- expanded row ---------- */
.lead__detail {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px 14px 14px;
  background: var(--surface);
  border-top: 1px solid var(--line-soft);
  border-inline-start: 4px solid var(--prio-line);   /* the rail carries on */
}

.lead__action { font-size: 13px; font-weight: 600; color: var(--prio); }
.lead__src { font-size: 12px; color: var(--muted); }

.lead__detailbar { display: flex; align-items: center; flex-wrap: wrap; gap: 8px 12px; }
.lead__detailbar > .btn { margin-inline-start: auto; }
.lead__id { font-family: var(--font-mono); font-size: 11.5px; color: var(--faint); }

.lead__tel {
  font-size: 12.5px;
  font-weight: 600;
  text-decoration: none;
  color: var(--brand);
  background: var(--brand-soft);
  border: 1px solid var(--brand-line);
  border-radius: var(--r-sm);
  padding: 2px 11px;
  transition: background-color .14s ease, color .14s ease;
}
.lead__tel:hover { background: var(--brand); color: var(--brand-ink); }

.lead__kv {
  margin: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 0 18px;
}
.lead__kvrow {
  display: grid;
  grid-template-columns: minmax(88px, 40%) minmax(0, 1fr);
  gap: 10px;
  padding: 5px 0;
  border-bottom: 1px solid var(--line-soft);
}
.lead__kv dt {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11.5px;
  color: var(--muted);
}
.lead__kv dd { margin: 0; font-size: 13px; color: var(--text); overflow-wrap: anywhere; }
.lead__kv dd.is-empty { color: var(--faint); }

.lead__int {
  font-size: 9.5px;
  font-weight: 700;
  line-height: 1.7;
  padding: 0 6px;
  border-radius: 999px;
  color: var(--muted);
  background: var(--surface-3);
  white-space: nowrap;
}

.lead__json {
  margin: 0;
  padding: 10px 12px;
  max-height: 320px;
  /* flex item: without this, min-height:auto grows to the full JSON and the
     max-height never bites */
  min-height: 0;
  overflow: auto;
  background: var(--surface-2);
  border: 1px solid var(--line-soft);
  border-radius: var(--r-md);
  font-family: var(--font-mono);
  font-size: 11.5px;
  line-height: 1.65;
  color: var(--text-2);
  white-space: pre;
  tab-size: 2;
}

/* ---------- loading / empty / error ---------- */
.state {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 30px 18px;
  text-align: center;
}
/* an empty queue means the state block owns the page, not a 30px strip */
.leads__list:empty ~ .state { flex: 1 1 auto; }

.state__title { font-size: 14px; font-weight: 700; color: var(--text-2); }
.state__text { font-size: 12.5px; color: var(--muted); max-width: 46ch; }
.state__actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 5px; }

.state--loading .state__title { color: var(--muted); animation: heartbeat 2.6s ease-in-out infinite; }

/* dashed outline so an empty queue reads as "nothing yet", not as a broken page */
.state--empty { border: 1px dashed var(--line); border-radius: var(--r-md); }

.state--error {
  background: var(--danger-soft);
  border: 1px solid var(--danger-line);
  border-radius: var(--r-md);
}
.state--error .state__title { color: var(--danger); }
.state--error .state__text { color: var(--danger); }

/* ---------- narrow: the page scrolls, not the queue ---------- */
@media (max-width: 960px) {
  .leads { min-height: auto; }
  .leads__list { flex: none; overflow: visible; }
  .leads__list:empty ~ .state { flex: none; }
  .leadbar__tools { margin-inline-start: 0; }
}

@media (max-width: 560px) {
  .lead__kv { grid-template-columns: minmax(0, 1fr); }
  .lead__time { margin-inline-start: 0; }
  .chips { gap: 6px; }
}

@media (max-width: 460px) {
  .picker__select { max-width: 116px; }
  .brand__title { font-size: 15px; }
}

/* ---------- reduced motion: keep the level change readable, drop the motion ---------- */
@media (prefers-reduced-motion: reduce) {
  .prio.is-flash {
    background-color: var(--prio-glow);
    transition: background-color .3s linear !important;
  }
}
