/* Shopwise local chat — WhatsApp-faithful styling
 * Plain CSS, no preprocessor.
 * Colour palette adapted from WhatsApp Web defaults.
 */

:root {
  --wa-green: #075E54;
  --wa-green-dark: #054640;
  --wa-green-light: #25D366;
  --wa-bubble-user: #DCF8C6;
  --wa-bubble-bot: #FFFFFF;
  --wa-bg: #ECE5DD;
  --wa-sidebar-bg: #F0F2F5;
  --wa-text: #111B21;
  --wa-text-muted: #667781;
  --wa-text-faint: #8696A0;
  --wa-border: #D1D7DB;
  --wa-shadow: 0 1px 1px rgba(11, 20, 26, 0.08);
  --wa-shadow-lg: 0 2px 8px rgba(11, 20, 26, 0.16);
  --radius-bubble: 8px;
  --radius-card: 6px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  height: 100%;
  font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  color: var(--wa-text);
  background: var(--wa-bg);
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; }

/* ─── app shell ──────────────────────────────────────────────────────── */
.app {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  /* Bounded row track: the scroll panes (.messages, .user-list) can only own a
     scrollbar if their grid cell has a DEFINITE height. An implicit `auto` row
     sizes to content and (in engines that don't stretch it) grows past the
     viewport, so `overflow:hidden` here just clips the composer off-screen with
     no scrollbar. `minmax(0, 1fr)` pins the row to the viewport in every engine. */
  grid-template-rows: minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;   /* mobile: track the dynamic viewport so the composer isn't hidden behind the address bar */
  overflow: hidden;
}

@media (max-width: 767px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: absolute; z-index: 20; width: 85vw; max-width: 320px; height: 100%; transform: translateX(-100%); transition: transform 180ms ease; box-shadow: var(--wa-shadow-lg); }
  .sidebar.open { transform: translateX(0); }
}

/* ─── sidebar ────────────────────────────────────────────────────────── */
.sidebar {
  background: var(--wa-sidebar-bg);
  border-right: 1px solid var(--wa-border);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  background: var(--wa-green);
  color: white;
  padding: 14px 16px;
  font-weight: 600;
  letter-spacing: 0.2px;
}

.brand { display: flex; align-items: baseline; gap: 8px; font-size: 17px; }
.brand-tag {
  font-size: 11px;
  font-weight: 500;
  background: rgba(255,255,255,0.18);
  padding: 2px 6px;
  border-radius: 4px;
  letter-spacing: 0.4px;
  text-transform: uppercase;
}

.sidebar-controls { padding: 10px 12px; }

.user-list {
  list-style: none;
  margin: 0;
  padding: 0;
  flex: 1;
  overflow-y: auto;
}

.user-item {
  padding: 12px 16px;
  border-bottom: 1px solid var(--wa-border);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 12px;
  transition: background 80ms ease;
}
.user-item:hover { background: rgba(0,0,0,0.04); }
.user-item.active { background: rgba(0, 0, 0, 0.06); }

.avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: var(--wa-green-light);
  color: white;
  font-weight: 600;
  display: grid;
  place-items: center;
  font-size: 16px;
  flex-shrink: 0;
}

.user-meta { flex: 1; min-width: 0; }
.user-name { font-weight: 500; color: var(--wa-text); }
.user-sub { font-size: 12px; color: var(--wa-text-muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.sidebar-footer {
  padding: 8px 16px;
  border-top: 1px solid var(--wa-border);
  font-size: 12px;
  color: var(--wa-text-muted);
}

.status-row { display: flex; align-items: center; gap: 8px; }
.status-dot {
  width: 10px; height: 10px; border-radius: 50%;
  display: inline-block;
}
.status-ok { background: #2BB673; box-shadow: 0 0 6px rgba(43, 182, 115, 0.4); }
.status-down { background: #E03E3E; }
.status-unknown { background: #C0C7CC; }

/* ─── chat surface ───────────────────────────────────────────────────── */
.chat {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;          /* allow flex child .messages to shrink + own its scrollbar */
  overflow: hidden;       /* confine scrolling to .messages, not the whole chat column */
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='40' height='40' viewBox='0 0 40 40'><circle cx='20' cy='20' r='0.7' fill='%23000' opacity='0.04'/></svg>");
  background-color: var(--wa-bg);
}

.chat-header {
  background: var(--wa-green-dark);
  color: white;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: var(--wa-shadow);
  z-index: 2;
}

.chat-header-title { flex: 1; min-width: 0; }
.header-alias { font-weight: 600; font-size: 16px; }
.header-sub { font-size: 12px; opacity: 0.7; margin-top: 2px; }

.chat-header-actions { display: flex; align-items: center; gap: 8px; }

.icon-btn {
  background: transparent;
  color: white;
  border: none;
  font-size: 18px;
  padding: 6px 10px;
  border-radius: 4px;
  cursor: pointer;
}
.icon-btn:hover { background: rgba(255,255,255,0.1); }

@media (min-width: 768px) {
  #btn-toggle-sidebar { display: none; }
}

.toggle { display: flex; align-items: center; gap: 6px; cursor: pointer; font-size: 13px; user-select: none; }
.toggle input { accent-color: var(--wa-green-light); margin: 0; }
.toggle-label { color: white; }

/* ─── messages ───────────────────────────────────────────────────────── */
.messages {
  flex: 1 1 0;            /* basis 0 + min-height:0 = the flex item can shrink AND scroll inside */
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;   /* momentum scrolling on iOS */
  overscroll-behavior: contain;        /* don't chain scroll to the page behind the pane */
  padding: 16px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scroll-behavior: smooth;
}

.empty-hint {
  color: var(--wa-text-muted);
  text-align: center;
  margin: 40px 12px;
  font-size: 14px;
  font-style: italic;
}

.bubble-row { display: flex; }
.bubble-row.user { justify-content: flex-end; }
.bubble-row.bot { justify-content: flex-start; }

.bubble {
  max-width: min(70%, 560px);
  padding: 8px 10px 6px;
  border-radius: var(--radius-bubble);
  box-shadow: var(--wa-shadow);
  position: relative;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.bubble.user { background: var(--wa-bubble-user); }
.bubble.bot { background: var(--wa-bubble-bot); }
.bubble.error { background: #FDECEA; color: #B71C1C; }

/* WhatsApp's grey footer chrome (the lifted affiliate disclosure). */
.bubble-footer {
  font-size: 12px;
  color: var(--wa-text-faint);
  margin-top: 6px;
}

.bubble-meta {
  font-size: 11px;
  color: var(--wa-text-faint);
  margin-top: 4px;
  display: flex;
  justify-content: flex-end;
  gap: 6px;
}

.bubble-text { line-height: 1.4; }
.bubble-text strong { font-weight: 600; }
.bubble-text em { font-style: italic; }
.bubble-text del { text-decoration: line-through; opacity: 0.7; }
.bubble-text code {
  background: rgba(0,0,0,0.06);
  padding: 1px 5px;
  border-radius: 3px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.93em;
}
.bubble-text pre {
  background: rgba(0,0,0,0.06);
  padding: 8px 10px;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.93em;
  margin: 4px 0;
  white-space: pre-wrap;
}
.bubble-text a { color: #075E54; text-decoration: underline; }
.bubble-text a:hover { color: #054640; }

.debug-collapser {
  margin-top: 6px;
  font-size: 12px;
  color: var(--wa-text-muted);
}
.debug-collapser summary { cursor: pointer; user-select: none; outline: none; }
.debug-collapser summary:hover { color: var(--wa-text); }
.debug-block {
  margin-top: 4px;
  padding: 6px 8px;
  background: #F5F6F8;
  border: 1px solid var(--wa-border);
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11.5px;
  line-height: 1.5;
  white-space: pre-wrap;
  color: #324049;
}
body:not(.debug-on) .debug-collapser { display: none; }

/* typing indicator */
.typing {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.typing-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--wa-text-faint);
  animation: typing-bounce 1.2s infinite ease-in-out;
}
.typing-dot:nth-child(2) { animation-delay: 0.15s; }
.typing-dot:nth-child(3) { animation-delay: 0.3s; }
@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* ─── composer ───────────────────────────────────────────────────────── */
.composer {
  background: #F0F2F5;
  padding: 10px 12px;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  border-top: 1px solid var(--wa-border);
}

.composer-input {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  padding: 10px 14px;
  border-radius: 22px;
  background: white;
  font-size: 15px;
  font-family: inherit;
  max-height: 140px;
  line-height: 1.4;
  box-shadow: var(--wa-shadow);
}
.composer-input:disabled { background: #F5F6F8; color: var(--wa-text-faint); }

.btn {
  border: none;
  border-radius: 22px;
  padding: 8px 16px;
  font-size: 14px;
  cursor: pointer;
  font-weight: 500;
}
.btn-primary { background: var(--wa-green); color: white; }
.btn-primary:hover { background: var(--wa-green-dark); }
.btn-ghost { background: transparent; color: var(--wa-text-muted); }
.btn-ghost:hover { background: rgba(0,0,0,0.04); color: var(--wa-text); }
.btn-send {
  background: var(--wa-green);
  color: white;
  width: 44px; height: 44px;
  border-radius: 50%;
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 18px;
  flex-shrink: 0;
}
.btn-send:disabled { background: #C0C7CC; cursor: not-allowed; }
.btn-send:hover:not(:disabled) { background: var(--wa-green-dark); }
.send-icon { transform: translateX(1px); }

/* ─── modal ──────────────────────────────────────────────────────────── */
.modal {
  position: fixed; inset: 0;
  background: rgba(11,20,26,0.5);
  display: grid;
  place-items: center;
  z-index: 100;
}
.modal[hidden] { display: none; }
.modal-card {
  background: white;
  border-radius: 8px;
  padding: 20px 22px;
  width: min(420px, 92vw);
  box-shadow: var(--wa-shadow-lg);
}
.modal-card h2 { margin: 0 0 8px; font-size: 18px; }
.modal-body { color: var(--wa-text-muted); font-size: 13px; line-height: 1.5; margin: 0 0 14px; }
.modal-body code { background: #F5F6F8; padding: 1px 5px; border-radius: 3px; font-size: 12px; }
.modal-input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--wa-border);
  border-radius: 6px;
  font-size: 15px;
  font-family: inherit;
  outline: none;
}
.modal-input:focus { border-color: var(--wa-green); box-shadow: 0 0 0 2px rgba(7, 94, 84, 0.15); }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}

/* Affiliate CTA buttons under a bot bubble */
.cta-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}
.cta-button {
  display: inline-block;
  padding: 6px 12px;
  border-radius: 16px;
  background: var(--wa-green-light);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
}
.cta-button:hover { background: var(--wa-green); }

/* "Why this pick" deep-dive link — quiet secondary action under the CTAs */
.why-pick-link {
  display: inline-block;
  margin-top: 6px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--wa-green-light);
  text-decoration: none;
}
.why-pick-link:hover { text-decoration: underline; }

/* Explorative follow-up chips ("Why this over the others?") — tap to ask */
.quick-replies {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}
.quick-reply {
  padding: 6px 12px;
  border-radius: 16px;
  border: 1px solid var(--wa-green);
  background: transparent;
  color: var(--wa-green);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease;
}
.quick-reply:hover { background: var(--wa-green); color: #fff; }
