:root {
  --paper: #ffffff;
  --ink: #000000;
  --text: #242424;
  --muted: #6f6f6f;
  --line: #e7e7e7;
  --wash: #f6f6f6;
  --orange: #f26b1d;
  --orange-deep: #c94f0c;
  --orange-wash: #fff2e9;
  --green: #1d8a4e;
  --amber: #b7791f;
  --red: #c23a2b;
  --font: "IBM Plex Sans Thai", "Noto Sans Thai", "Leelawadee UI", Tahoma, sans-serif;
  color-scheme: light;
  box-sizing: border-box;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
*, *::before, *::after { box-sizing: inherit; }
html, body { height: 100%; margin: 0; }
body { background: var(--paper); color: var(--text); font: 400 16px/1.6 var(--font); -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--orange); outline-offset: 2px; }
[hidden] { display: none !important; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.muted { color: var(--muted); }
.link { background: none; border: 0; padding: 0; color: var(--muted); text-decoration: underline; text-underline-offset: 3px; }

/* ตัวอักษรแบรนด์ */
.wordmark { margin: 0; font-weight: 700; font-size: 20px; color: var(--ink); display: flex; align-items: center; gap: 10px; letter-spacing: -0.01em; }
.wordmark .k { display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 7px; background: var(--orange); color: #fff; font: 900 19px/1 Arial, sans-serif; }
.wordmark.small { font-size: 17px; }
.wordmark.small .k { width: 26px; height: 26px; font-size: 16px; }

/* ล็อกอิน */
.login { min-height: 100%; display: grid; place-items: center; padding: 32px 22px; }
.login-inner { width: min(420px, 100%); }
.login h1 { font-size: clamp(28px, 7vw, 38px); line-height: 1.25; font-weight: 700; color: var(--ink); margin: 40px 0 12px; letter-spacing: -0.015em; }
.login .muted { margin: 0 0 32px; }
.btn-google { width: 100%; display: flex; align-items: center; justify-content: center; gap: 12px; padding: 14px 18px; border: 1.5px solid var(--ink); background: #fff; border-radius: 10px; font-weight: 600; }
.btn-google:hover { background: var(--wash); }
.btn-google svg { width: 22px; height: 22px; }
.login-error { margin-top: 18px; padding: 12px 14px; border-left: 3px solid var(--red); background: #fdf1ef; color: var(--red); }

/* โครงแอป */
.app { height: 100%; display: grid; grid-template-columns: 320px 1fr; }
.side { border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 10px; }
.btn-primary { background: var(--ink); color: #fff; border: 0; border-radius: 8px; padding: 8px 14px; font-weight: 600; }
.btn-primary:hover { background: var(--orange-deep); }
.side-tools { padding: 0 16px 10px; display: grid; gap: 8px; }
.side-tools input { width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; background: var(--wash); }
.seg { display: flex; gap: 4px; }
.seg button { flex: 1; border: 0; background: none; padding: 6px 0; border-bottom: 2px solid transparent; color: var(--muted); font-size: 14px; }
.seg button.on { color: var(--ink); border-bottom-color: var(--orange); font-weight: 600; }
.case-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.case-list li button { width: 100%; text-align: left; border: 0; background: none; padding: 11px 16px 11px 13px; border-left: 3px solid transparent; display: grid; gap: 1px; }
.case-list li button:hover { background: var(--wash); }
.case-list li button[aria-current="true"] { background: var(--orange-wash); border-left-color: var(--orange); }
.case-list .row1 { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; color: var(--muted); }
.case-list .name { font-weight: 600; color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.case-list .row3 { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.case-list .none { padding: 24px 16px; color: var(--muted); font-size: 14px; }
.dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; background: var(--muted); }
.st-open .dot, .dot.st-open { background: var(--orange); }
.st-quoted .dot, .dot.st-quoted { background: var(--amber); }
.st-sent .dot, .dot.st-sent { background: #2f6fca; }
.st-won .dot, .dot.st-won { background: var(--green); }
.st-lost .dot, .dot.st-lost, .st-cancelled .dot, .dot.st-cancelled { background: #b5b5b5; }
.side-foot { border-top: 1px solid var(--line); padding: 10px 16px; display: flex; justify-content: space-between; font-size: 14px; color: var(--muted); }

/* แชท */
.chat { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.empty { margin: auto; max-width: 420px; padding: 24px; }
.empty-title { font-size: 22px; font-weight: 700; color: var(--ink); margin: 0 0 8px; }
.chat-view { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.case-head { display: flex; align-items: flex-start; gap: 12px; padding: 12px 20px; border-bottom: 1px solid var(--line); }
.back { display: none; border: 0; background: none; font-size: 30px; line-height: 1; padding: 0 6px 0 0; color: var(--ink); }
.case-title { flex: 1; min-width: 0; }
.case-code { margin: 0; font-size: 13px; color: var(--muted); }
.case-title h2 { margin: 0; font-size: 19px; line-height: 1.35; color: var(--ink); font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meta-row { display: flex; align-items: center; gap: 10px; margin-top: 6px; min-width: 0; }
.customer { font-size: 14px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status-pill { flex: none; border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 3px 11px; font-size: 13px; white-space: nowrap; }
.status-pill:hover { border-color: var(--ink); }

.thread { flex: 1; overflow-y: auto; padding: 20px 20px 8px; display: flex; flex-direction: column; gap: 14px; }
.msg { max-width: min(620px, 88%); white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--orange-wash); border-radius: 14px 14px 4px 14px; padding: 10px 14px; }
.msg.assistant { align-self: flex-start; display: grid; grid-template-columns: 26px 1fr; gap: 10px; }
.msg.assistant .k { width: 26px; height: 26px; border-radius: 7px; background: var(--orange); color: #fff; font: 900 15px/26px Arial, sans-serif; text-align: center; }
.msg.note { align-self: center; font-size: 13px; color: var(--muted); }
.msg .imgs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.msg .imgs button { border: 0; padding: 0; background: none; }
.msg .imgs img { width: 112px; height: 112px; object-fit: cover; border-radius: 8px; display: block; background: var(--wash); }
.msg .time { display: block; font-size: 12px; color: var(--muted); margin-top: 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 2px 0 0 36px; }
.chips button { border: 1.5px solid var(--orange); background: #fff; color: var(--orange-deep); border-radius: 999px; padding: 6px 14px; font-weight: 500; }
.chips button:hover { background: var(--orange-wash); }
.typing { align-self: flex-start; margin-left: 36px; color: var(--muted); font-size: 14px; }
.typing::after { content: "…"; animation: blink 1.2s steps(3) infinite; }
@keyframes blink { 50% { opacity: .2; } }
.retry { align-self: flex-start; margin-left: 36px; color: var(--red); font-size: 14px; display: flex; gap: 10px; align-items: center; }
.retry button { border: 1px solid var(--red); background: #fff; color: var(--red); border-radius: 6px; padding: 3px 10px; }

/* ใบราคา — จุดเด่นของหน้า: ขอบบนฉีกแบบใบเสร็จ */
.ticket { align-self: flex-start; margin-left: 36px; width: min(380px, calc(100% - 36px)); background: #fff; border: 1.5px solid var(--ink); border-top: 0; border-radius: 0 0 12px 12px; position: relative; padding: 18px 18px 14px; }
.ticket::before { content: ""; position: absolute; left: -1.5px; right: -1.5px; top: -9px; height: 10px; background:
  radial-gradient(circle at 7px 0, transparent 6px, var(--ink) 6px, var(--ink) 7.5px, transparent 7.5px) 0 0 / 14px 10px repeat-x; }
.ticket .t-head { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); }
.ticket .t-total { font-size: 34px; font-weight: 700; color: var(--ink); letter-spacing: -0.02em; line-height: 1.15; margin: 6px 0 2px; font-variant-numeric: tabular-nums; }
.ticket .t-total small { font-size: 16px; font-weight: 500; margin-left: 4px; }
.ticket .t-unit { font-variant-numeric: tabular-nums; }
.ticket .t-range { font-size: 13px; color: var(--muted); }
.ticket .conf { display: inline-block; margin-top: 10px; font-size: 13px; font-weight: 600; padding: 2px 10px; border-radius: 999px; }
.conf.high { background: #e8f5ee; color: var(--green); }
.conf.confirm { background: #fdf3e2; color: var(--amber); }
.conf.owner { background: #fdecea; color: var(--red); }
.ticket .t-msg { border-top: 1.5px dashed var(--line); margin-top: 14px; padding-top: 12px; }
.ticket .t-msg p { margin: 0 0 8px; white-space: pre-wrap; font-size: 15px; }
.ticket .copy { border: 0; background: var(--ink); color: #fff; border-radius: 6px; padding: 5px 12px; font-size: 14px; }
.ticket .review { margin-top: 12px; padding: 8px 10px; background: var(--wash); border-radius: 8px; font-size: 14px; }

/* ช่องพิมพ์ */
.composer { border-top: 1px solid var(--line); padding: 10px 16px 12px; background: #fff; }
.composer-row { display: flex; align-items: flex-end; gap: 8px; }
.composer textarea { flex: 1; resize: none; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; max-height: 160px; line-height: 1.5; background: var(--wash); }
.composer textarea:focus { background: #fff; border-color: var(--ink); outline: none; }
.attach, .send { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 12px; flex: none; }
.attach { color: var(--ink); cursor: pointer; }
.attach:hover { background: var(--wash); }
.attach svg, .send svg { width: 22px; height: 22px; }
.send { border: 0; background: var(--orange); color: #fff; }
.send:disabled { background: #f3b48f; cursor: default; }
.previews { display: flex; gap: 8px; flex-wrap: wrap; }
.previews:not(:empty) { margin-bottom: 8px; }
.previews figure { margin: 0; position: relative; }
.previews img { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; display: block; }
.previews button { position: absolute; top: -6px; right: -6px; width: 22px; height: 22px; border-radius: 50%; border: 0; background: var(--ink); color: #fff; font-size: 13px; line-height: 22px; padding: 0; }
.readonly { text-align: center; color: var(--muted); font-size: 14px; padding: 12px; border-top: 1px solid var(--line); margin: 0; }

/* กล่องโต้ตอบ */
.dlg { border: 0; border-radius: 14px; padding: 0; width: min(440px, calc(100% - 24px)); box-shadow: 0 18px 60px rgba(0,0,0,.18); }
.dlg::backdrop { background: rgba(0,0,0,.35); }
.dlg form { padding: 20px; display: grid; gap: 12px; }
.dlg h3 { margin: 0; font-size: 19px; color: var(--ink); }
.dlg label { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.dlg input, .dlg select, .dlg textarea { border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; color: var(--text); background: #fff; }
.dlg .opts { display: grid; gap: 6px; }
.dlg .opt { text-align: left; border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 10px 12px; }
.dlg .opt:hover, .dlg .opt.on { border-color: var(--ink); }
.dlg .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.dlg .ghost { border: 0; background: none; color: var(--muted); padding: 8px 12px; }
.dlg .danger { border: 0; background: none; color: var(--red); padding: 8px 0; margin-right: auto; }
.dlg .found { max-height: 180px; overflow: auto; display: grid; gap: 4px; }
.dlg .hint { font-size: 13px; color: var(--muted); margin: 0; }
.dlg img.full { width: 100%; height: auto; border-radius: 8px; }

.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px; max-width: calc(100% - 32px); }

/* มือถือ: รายการเคสกับแชทสลับกันเต็มจอ */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .side { border-right: 0; }
  .app.in-case .side { display: none; }
  .app:not(.in-case) .chat { display: none; }
  .back { display: block; }
  .case-head { padding: 10px 14px; }
  .thread { padding: 16px 14px 8px; }
  .composer { padding: 8px 10px 10px; }
  .msg { max-width: 92%; }
}
@media (prefers-reduced-motion: reduce) { .typing::after { animation: none; } }
