/* Components: chips, badges, price, status, swatches, product card, quick view, compare tray & table. */

/* ---------- chips ---------- */
.chips { display: flex; gap: var(--s2); flex-wrap: wrap; }
.chip { text-decoration: none; color: var(--ink);
  display: inline-flex; align-items: center; gap: var(--s2); min-height: var(--tap); padding: 0 var(--s4);
  border-radius: var(--r-pill); border: 1.5px solid var(--line-strong); background: #fff; font-weight: 600; font-size: var(--fs-sm);
  transition: background var(--t-fast), color var(--t-fast), border-color var(--t-fast), transform var(--t-fast) var(--ease);
}
.chip:hover { border-color: var(--ink); }
.chip:active { transform: scale(.96); }
.chip[aria-pressed="true"], .chip[aria-checked="true"] { background: var(--ink); color: #fff; border-color: var(--ink); }
.chip .x { font-size: 16px; line-height: 1; opacity: .7; }

/* ---------- badges ---------- */
.badges { display: flex; flex-wrap: wrap; gap: 6px; }
.badge {
  display: inline-flex; align-items: center; height: 24px; padding: 0 8px; border-radius: 6px;
  font-size: 13px; font-weight: 600; white-space: nowrap; font-family: var(--font-display);
}
.badge-deal { background: var(--sale); color: #fff; }
.badge-new { background: var(--blue); color: #fff; }
.badge-soon { background: var(--ink); color: #fff; }
.badge-ok { background: var(--ok-bg); color: var(--ok); }
.badge-warn { background: var(--warn-bg); color: var(--warn); }
.badge-stop { background: var(--stop-bg); color: var(--stop); }
.badge-info { background: var(--ice); color: var(--ink); }

/* ---------- price ---------- */
.price { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px var(--s2); }
.price-now { font-size: 1.25rem; font-weight: 700; letter-spacing: -.01em; }
.price-was { color: var(--ink-3); text-decoration: line-through; font-size: var(--fs-sm); }
.price-save { color: var(--deal); font-size: var(--fs-sm); font-weight: 700; flex-basis: 100%; }

/* ---------- status line ---------- */
.status { display: inline-flex; align-items: center; gap: 6px; font-size: var(--fs-sm); font-weight: 600; }
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; flex: none; }
.status.ok { color: var(--ok); }
.status.warn { color: var(--warn); }
.status.stop { color: var(--stop); }
.status.soon { color: var(--ink-2); }
.status.new { color: var(--blue); }

/* ---------- swatches ---------- */
.swatches { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.swatch {
  width: 32px; height: 32px; border-radius: 50%; border: 0; padding: 0; background: transparent;
  display: grid; place-items: center; position: relative;
}
.swatch::before {
  content: ""; width: 20px; height: 20px; border-radius: 50%; background: var(--c, #ccc);
  box-shadow: inset 0 0 0 1px rgba(23, 33, 59, .18);
}
.swatch[aria-pressed="true"]::after, .swatch[aria-checked="true"]::after {
  content: ""; position: absolute; inset: 2px; border-radius: 50%; border: 2px solid var(--ink);
}
.swatch-more { font-size: var(--fs-xs); color: var(--ink-3); font-weight: 600; padding-left: 2px; }
.swatches-lg .swatch { width: var(--tap); height: var(--tap); }
.swatches-lg .swatch::before { width: 28px; height: 28px; }

/* ---------- product grid & card (flat retail card: photo on white, name, price, one badge) ---------- */
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--s5) var(--s3); }
@media (min-width: 768px) { .grid { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--s8) var(--s5); } }
@media (min-width: 1080px) { .grid { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
@media (min-width: 1280px) { .grid { grid-template-columns: repeat(5, minmax(0, 1fr)); } }

.card { position: relative; display: flex; flex-direction: column; background: #fff; min-width: 0; }
.card-media { position: relative; aspect-ratio: 1; border-radius: var(--r-md); overflow: hidden; background: #fff; }
.card-media > a { position: absolute; inset: 0; display: block; }
.card-media img {
  position: absolute; inset: 6%; width: 88%; height: 88%; object-fit: contain;
  transition: transform var(--t-slow) var(--ease-out), opacity var(--t-med);
}
.card-media .ph { position: absolute; inset: 0; }
.card .badges { position: absolute; top: var(--s2); left: var(--s2); z-index: 2; pointer-events: none; }
.card-actions { position: absolute; top: var(--s2); right: var(--s2); left: auto; z-index: 3; display: grid; gap: 6px; }
.card-actions .icon-btn { background: #fff; border: 1px solid var(--line); }
.card-actions .icon-btn:hover { background: var(--ice); }
.card-actions .icon-btn[aria-pressed="true"] { background: var(--ink); color: #fff; border-color: var(--ink); }
.card-body { padding: var(--s3) 2px 0; display: flex; flex-direction: column; gap: 4px; flex: 1; min-width: 0; }
.card-kicker { font-size: var(--fs-sm); font-weight: 600; color: var(--ink-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-title { font-family: var(--font-display); font-size: var(--fs-md); font-weight: 400; line-height: 1.35; margin: 0; }
.card-title a { text-decoration: none; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; min-height: 2.7em; }
.card-title a::after { content: ""; position: absolute; inset: 0; z-index: 1; } /* whole card clickable */
.card .swatches, .card .card-buy { position: relative; z-index: 2; }
.card-foot { margin-top: auto; display: flex; align-items: end; justify-content: space-between; gap: var(--s2); padding-top: var(--s2); }
.card .price { font-family: var(--font-display); }
.card .price-now { font-size: 1.25rem; font-weight: 600; }
.card .price:has(.price-was) .price-now { color: var(--sale); }
.card .price-save { display: none; } /* the % badge already says it; keeps cards calm */
.card-buy { flex: none; width: 40px; height: 40px; border-radius: 50%; border: 1.5px solid var(--ink); background: #fff; color: var(--ink); display: grid; place-items: center; transition: transform var(--t-fast) var(--ease), background var(--t-fast), color var(--t-fast); }
.card-buy:hover { background: var(--ink); color: #fff; }
.card-buy:active { transform: scale(.9); }
.card-buy:disabled { border-color: #D8D8DF; color: #9A9FB0; cursor: not-allowed; background: #fff; }
.card-buy svg { width: 18px; height: 18px; }
.card .status { font-size: var(--fs-xs); }
/* desktop pointer: gentle photo zoom; quick view / compare appear on hover (always reachable by keyboard) */
@media (hover: hover) and (pointer: fine) {
  .card:hover .card-media img { transform: scale(1.04); }
  .card-actions { opacity: 0; transition: opacity var(--t-fast); }
  .card:hover .card-actions, .card:focus-within .card-actions, .card-actions:has([aria-pressed="true"]) { opacity: 1; }
}
/* touch screens: no floating icons; tap opens the product page (compare lives there) */
@media (hover: none) { .card-actions { display: none; } }

/* placeholder art when a product has no photo: category icon on a soft field */
.ph { display: grid; place-items: center; align-content: center; gap: var(--s2); background: #F4F5F8; color: #8A90A6; }
.ph svg { width: 30%; max-width: 88px; height: auto; opacity: .8; }
.ph span { font-size: var(--fs-xs); color: var(--ink-3); font-weight: 500; }

.card-skeleton { aspect-ratio: .72; border-radius: var(--r-md); }

/* ---------- quick view ---------- */
.qv { display: grid; }
.qv-media { position: relative; aspect-ratio: 1; background: radial-gradient(100% 80% at 50% 100%, var(--ice) 0%, #fff 70%); }
.qv-media img, .qv-media .ph { position: absolute; inset: 10%; width: 80%; height: 80%; object-fit: contain; }
.qv-media .ph { inset: 0; width: 100%; height: 100%; }
.qv-info { padding: var(--s6) var(--s5) var(--s8); display: grid; gap: var(--s4); align-content: start; }
.qv-info h2 { font-size: var(--fs-xl); }
.qv-info .price-now { font-size: 1.75rem; }
.qv-actions { display: grid; grid-template-columns: 1fr auto; gap: var(--s2); }
.facts { display: grid; gap: var(--s2); margin: 0; }
.facts div { display: grid; grid-template-columns: 110px 1fr; gap: var(--s3); font-size: var(--fs-sm); }
.facts dt { color: var(--ink-3); }
.facts dd { margin: 0; }
.bullets { margin: 0; padding-left: 1.2em; display: grid; gap: 6px; font-size: var(--fs-sm); color: var(--ink-2); }
@media (min-width: 768px) {
  .qv { grid-template-columns: 1.05fr 1fr; }
  .qv-media { aspect-ratio: auto; min-height: 520px; border-radius: var(--r-xl) 0 0 var(--r-xl); }
  .qv-info { padding: var(--s12) var(--s8) var(--s8); }
}

/* ---------- compare tray ---------- */
.compare-tray {
  position: fixed; z-index: var(--z-tray); left: 50%; bottom: calc(12px + var(--bottom-offset) + env(safe-area-inset-bottom));
  width: min(720px, 100% - 16px); transform: translate(-50%, 0);
  display: flex; align-items: center; gap: var(--s3); padding: var(--s2) var(--s2) var(--s2) var(--s4);
  background: var(--ink); color: #fff; border-radius: var(--r-lg); box-shadow: var(--sh-3);
  transition: transform var(--t-med) var(--ease-out), opacity var(--t-med), bottom var(--t-med) var(--ease);
}
.compare-tray[data-state="hidden"] { transform: translate(-50%, 140%); opacity: 0; pointer-events: none; }
.compare-tray .label { font-size: var(--fs-sm); font-weight: 700; white-space: nowrap; }
.compare-items { display: flex; gap: var(--s2); flex: 1; min-width: 0; overflow-x: auto; scrollbar-width: none; }
.compare-item { position: relative; display: flex; align-items: center; gap: var(--s2); background: rgba(255, 255, 255, .08); border-radius: var(--r-md); padding: 4px 4px 4px 4px; min-width: 0; flex: none; max-width: 200px; }
.compare-item .thumb-xs { width: 40px; height: 40px; border-radius: var(--r-sm); background: #fff; display: grid; place-items: center; overflow: hidden; flex: none; }
.compare-item .thumb-xs img { width: 100%; height: 100%; object-fit: contain; }
.compare-item .thumb-xs .ph svg { width: 60%; }
.compare-item .thumb-xs .ph span { display: none; }
.compare-item span.n { font-size: var(--fs-xs); line-height: 1.25; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; max-width: 110px; }
.compare-item button { width: 32px; height: 32px; border: 0; border-radius: 50%; background: transparent; color: #fff; font-size: 18px; flex: none; }
.compare-item button:hover { background: rgba(255, 255, 255, .16); }
.compare-slot { width: 48px; height: 48px; border-radius: var(--r-md); border: 1.5px dashed rgba(255, 255, 255, .3); flex: none; }
.compare-tray .btn-primary { box-shadow: none; }
@media (max-width: 767px) {
  .compare-tray { gap: var(--s2); padding-left: var(--s3); }
  .compare-tray .label, .compare-item span.n, .compare-slot { display: none; }
  .compare-items { padding: 10px 10px 4px 2px; margin-top: -8px; }
  .compare-item { padding: 2px; }
  .compare-item button { width: 28px; height: 28px; position: absolute; top: -8px; right: -8px; background: var(--blue); font-size: 14px; }
}

/* ---------- compare dialog ---------- */
.compare-dialog { width: min(1100px, 100% - 16px); }
.compare-head { display: flex; justify-content: space-between; align-items: center; gap: var(--s3); padding: var(--s6) var(--s6) var(--s3); flex-wrap: wrap; }
.compare-head h2 { font-size: var(--fs-xl); }
.compare-scroll { overflow-x: auto; padding: 0 var(--s4) var(--s6); }
.compare-table { width: 100%; min-width: 560px; border-collapse: separate; border-spacing: 0; table-layout: fixed; font-size: var(--fs-sm); }
.compare-table th, .compare-table td { padding: var(--s3); vertical-align: top; text-align: left; border-bottom: 1px solid var(--line); }
.compare-table thead th { border-bottom: 0; }
.compare-table tbody th { width: 150px; color: var(--ink-3); font-weight: 600; position: sticky; left: 0; background: #fff; z-index: 1; }
.compare-table tr.diff td { background: var(--ice-50); }
.compare-prod { display: grid; gap: var(--s2); }
.compare-prod .img { aspect-ratio: 1; border-radius: var(--r-md); background: var(--ice-50); display: grid; place-items: center; overflow: hidden; }
.compare-prod .img img { width: 80%; height: 80%; object-fit: contain; }
.compare-prod a { font-weight: 700; text-decoration: none; }
.compare-prod a:hover { text-decoration: underline; }
.switch { display: inline-flex; align-items: center; gap: var(--s2); min-height: var(--tap); font-size: var(--fs-sm); font-weight: 600; cursor: pointer; }
.switch input { width: 20px; height: 20px; accent-color: var(--blue); }

/* ---------- horizontal rail (related products) ---------- */
.rail { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(200px, 44%); gap: var(--s3); overflow-x: auto; scroll-snap-type: x mandatory; padding: 4px 4px var(--s4); margin-inline: -4px; scrollbar-width: thin; }
.rail > * { scroll-snap-align: start; }
@media (min-width: 768px) { .rail { grid-auto-columns: minmax(220px, 30%); gap: var(--s4); } }
@media (min-width: 1080px) { .rail { grid-auto-columns: calc(25% - 12px); } }
@media (pointer: coarse) {
  .swatch { width: 40px; height: 40px; }
  .swatches { gap: 0; }
}

body.has-tray { padding-bottom: var(--tray-space, 96px); }
body.has-tray .toast { bottom: calc(var(--tray-space, 96px) + var(--bottom-offset)); }

/* second photo on hover (desktop only; touch screens keep the main photo) */
.card-media img.alt { opacity: 0; }
@media (hover: hover) and (pointer: fine) {
  .card:hover .card-media img.alt { opacity: 1; }
  .card:hover .card-media:has(img.alt) img:not(.alt) { opacity: 0; }
}

/* quick view: badge inside the photo, and on larger screens the dialog always fits the viewport
   (photo stays put, only the details column scrolls; close button pinned to the corner) */
.qv-media .badges { position: absolute; top: var(--s4); left: var(--s4); z-index: 1; }
@media (min-width: 768px) {
  #quickview { overflow: hidden; }
  #quickview .modal-close { position: absolute; top: var(--s3); right: var(--s3); float: none; margin: 0; }
  .qv { height: min(640px, calc(100dvh - 48px)); }
  .qv-media { min-height: 0; height: 100%; }
  .qv-info { overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; padding: var(--s12) var(--s12) var(--s8) var(--s8); grid-auto-rows: max-content; } /* rows never squeeze to 0 inside the fixed-height column */
}
/* the buy / compare row stays visible at the bottom of the quick view while the details scroll */
.qv-actions { position: sticky; bottom: 0; z-index: 2; background: #fff; padding-block: var(--s3); margin-block: calc(-1 * var(--s3)); box-shadow: 0 -12px 16px -12px rgba(23, 33, 59, .12); }

/* ---------- product chat ("ถาม WAVY") ---------- */
.chat-fab {
  position: fixed; z-index: var(--z-tray); right: 16px;
  bottom: calc(16px + var(--bottom-offset) + env(safe-area-inset-bottom));
  display: inline-flex; align-items: center; gap: var(--s2); min-height: 48px; padding: 0 var(--s5) 0 var(--s4);
  border: 0; border-radius: var(--r-pill); background: var(--ink); color: #fff; box-shadow: var(--sh-2);
  font: 600 var(--fs-sm)/1 var(--font-display); cursor: pointer;
  transition: transform var(--t-fast) var(--ease), background var(--t-fast) var(--ease), bottom var(--t-med) var(--ease);
}
.chat-fab .ico { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; }
.chat-fab:hover { background: var(--blue); }
.chat-fab:active { transform: scale(.97); }
body.has-tray .chat-fab { bottom: calc(var(--tray-space, 96px) + var(--bottom-offset)); }
body.chat-open .chat-fab { display: none; }

.chat-panel {
  position: fixed; z-index: var(--z-toast); right: 16px; bottom: calc(16px + env(safe-area-inset-bottom));
  width: min(400px, calc(100vw - 32px)); height: min(640px, calc(100dvh - 96px));
  display: flex; flex-direction: column; background: var(--page); color: var(--ink);
  border: 1px solid var(--line); border-radius: var(--r-lg); box-shadow: var(--sh-3); overflow: hidden;
  animation: chat-in var(--t-med) var(--ease-out);
}
.chat-panel[hidden] { display: none; }
@keyframes chat-in { from { opacity: 0; transform: translateY(12px); } }
@media (max-width: 599px) {
  .chat-panel { inset: auto 0 0 0; width: 100%; height: 88dvh; border-radius: var(--r-lg) var(--r-lg) 0 0; border-bottom: 0; }
}
.chat-head { display: flex; align-items: flex-start; gap: var(--s2); padding: var(--s4) var(--s3) var(--s3) var(--s5); border-bottom: 1px solid var(--line); background: var(--ice-50); }
.chat-head > div { flex: 1; min-width: 0; }
.chat-head h2 { margin: 0; font: 600 var(--fs-lg)/1.3 var(--font-display); }
.chat-head p { margin: var(--s1) 0 0; font-size: var(--fs-xs); line-height: 1.45; color: var(--ink-3); }
.chat-head .icon-btn { flex: none; }

.chat-log { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: var(--s4); display: flex; flex-direction: column; gap: var(--s3); }
.chat-msg { max-width: 88%; padding: var(--s3) var(--s4); border-radius: var(--r-md); font-size: var(--fs-sm); line-height: 1.6; overflow-wrap: anywhere; }
.chat-msg p { margin: 0; }
.chat-msg p + p, .chat-msg p + ul, .chat-msg ul + p { margin-top: var(--s2); }
.chat-msg ul { margin: 0; padding-left: 1.2em; }
.chat-msg li + li { margin-top: 2px; }
.chat-msg.me { align-self: flex-end; background: var(--blue); color: #fff; border-bottom-right-radius: var(--s1); white-space: pre-wrap; }
.chat-msg.bot { align-self: flex-start; background: var(--cloud); border-bottom-left-radius: var(--s1); }
.chat-msg.err { background: var(--stop-bg); color: var(--stop); }
.chat-starters { display: flex; flex-wrap: wrap; gap: var(--s2); }
.chat-starters .chip { min-height: 36px; padding: 0 var(--s3); border: 1px solid var(--line-strong); border-radius: var(--r-pill); background: var(--page); color: var(--ink-2); font-size: var(--fs-sm); cursor: pointer; }
.chat-starters .chip:hover { border-color: var(--blue); color: var(--blue); }

.chat-dots { display: inline-flex; gap: 4px; padding: 4px 0; }
.chat-dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--ink-3); animation: chat-dot 1s infinite ease-in-out; }
.chat-dots i:nth-child(2) { animation-delay: .15s; }
.chat-dots i:nth-child(3) { animation-delay: .3s; }
@keyframes chat-dot { 0%, 80%, 100% { opacity: .3; } 40% { opacity: 1; } }

.chat-products { list-style: none; margin: var(--s3) 0 0; padding: 0; display: grid; gap: var(--s2); }
.chat-prod { display: flex; align-items: center; gap: var(--s2); padding: var(--s2); background: var(--page); border: 1px solid var(--line); border-radius: var(--r-sm); }
.cp-link { flex: 1; min-width: 0; display: flex; align-items: center; gap: var(--s3); color: inherit; text-decoration: none; }
.cp-link:hover .cp-name { color: var(--blue); }
.cp-img { flex: none; width: 56px; height: 56px; border-radius: var(--s2); background: var(--cloud); display: grid; place-items: center; overflow: hidden; }
.cp-img img { width: 100%; height: 100%; object-fit: contain; }
.cp-img svg { width: 28px; height: 28px; color: var(--ink-3); }
.cp-text { min-width: 0; display: grid; gap: 2px; }
.cp-name { font-size: var(--fs-xs); line-height: 1.4; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.cp-price { font: 600 var(--fs-sm)/1.2 var(--font-display); }
.cp-add { flex: none; }

.chat-form { display: flex; align-items: flex-end; gap: var(--s2); padding: var(--s3); border-top: 1px solid var(--line); }
.chat-form textarea {
  flex: 1; min-height: var(--tap); max-height: 132px; resize: none; padding: 11px var(--s4);
  border: 1px solid var(--line-strong); border-radius: 22px; font: inherit; font-size: 16px; line-height: 1.35; color: var(--ink); background: var(--page);
}
.chat-form textarea:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
.chat-send { flex: none; width: var(--tap); height: var(--tap); display: grid; place-items: center; border: 0; border-radius: 50%; background: var(--blue); color: #fff; cursor: pointer; }
.chat-send .ico { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.chat-send:disabled { opacity: .5; cursor: progress; }
@media (prefers-reduced-motion: reduce) { .chat-panel, .chat-dots i { animation: none; } .chat-fab { transition: none; } }
@media print { .chat-fab, .chat-panel { display: none !important; } }

/* ---------- bundle tag (e.g. scanner + stand) ---------- */
.bundle-tag { align-self: flex-start; display: inline-flex; align-items: center; min-height: 22px; padding: 0 8px; border-radius: var(--r-pill); background: var(--cloud); color: var(--ink-2); font-size: var(--fs-xs); font-weight: 600; }
.bundle-tag.is-kit { background: var(--blue-50); color: var(--blue-press); }
.kit-list { margin: 0; padding-left: 1.1em; }
.kit-list li + li { margin-top: 2px; }
.kit-save { display: block; margin-top: var(--s1); color: var(--ok); }
