/* ══════════════════════════════════════
   Shop Assistant Chat Drawer
   ══════════════════════════════════════ */

:root {
  --sa-green: #1CA950;
  --sa-green-dark: #158a3f;
  --sa-green-light: #e8f7ee;
  --sa-bg: #ffffff;
  --sa-surface: #f7f8fa;
  --sa-border: #e2e5ea;
  --sa-text: #1a1e26;
  --sa-text-secondary: #5f6877;
  --sa-text-muted: #8b93a1;
  --sa-user-bg: #1CA950;
  --sa-user-text: #ffffff;
  --sa-assistant-bg: #f0f2f5;
  --sa-assistant-text: #1a1e26;
  --sa-drawer-width: 420px;
  --sa-radius: 12px;
  --sa-radius-sm: 8px;
  --sa-shadow: 0 8px 32px rgba(0,0,0,0.12), 0 2px 8px rgba(0,0,0,0.08);
  --sa-font: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
}

/* ── Floating Trigger ── */
#sa-trigger-btn {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--sa-green);
  border: none;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(28,169,80,0.35);
  transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.15s ease;
  z-index: 9998;
}
#sa-trigger-btn:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 24px rgba(28,169,80,0.45);
  background: var(--sa-green-dark);
}
#sa-trigger-btn:active { transform: scale(0.96); }
#sa-trigger-btn svg { width: 26px; height: 26px; fill: white; }

#sa-trigger-btn .sa-pulse {
  position: absolute;
  top: 2px; right: 2px;
  width: 12px; height: 12px;
  background: #ff4d4f;
  border-radius: 50%;
  border: 2px solid white;
  animation: sa-pulse-ring 2s ease-out infinite;
}
@keyframes sa-pulse-ring {
  0%  { box-shadow: 0 0 0 0 rgba(255,77,79,0.5); }
  70% { box-shadow: 0 0 0 8px rgba(255,77,79,0); }
  100%{ box-shadow: 0 0 0 0 rgba(255,77,79,0); }
}

/* ── Backdrop ── */
#sa-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.25);
  opacity: 0; visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
  z-index: 9999;
}
#sa-backdrop.sa-open { opacity: 1; visibility: visible; }
#sa-backdrop.sa-fullscreen-open { opacity: 0; visibility: hidden; }

/* ── Drawer ── */
#sa-drawer {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: var(--sa-drawer-width);
  max-width: 100vw;
  background: var(--sa-bg);
  transform: translateX(100%);
  transition: transform 0.35s cubic-bezier(0.16,1,0.3,1), width 0.3s ease, left 0.3s ease;
  z-index: 10000;
  display: flex;
  flex-direction: column;
  box-shadow: var(--sa-shadow);
  font-family: var(--sa-font);
}
#sa-drawer.sa-open { transform: translateX(0); }

/* Fullscreen mode */
#sa-drawer.sa-fullscreen {
  width: 100vw;
  left: 0;
}
#sa-drawer.sa-fullscreen .sa-msg-bubble { max-width: 60%; }
#sa-drawer.sa-fullscreen .sa-messages { padding: 24px 10%; }
#sa-drawer.sa-fullscreen .sa-input-area { padding: 12px 10% 20px; }
#sa-drawer.sa-fullscreen .sa-chips { max-width: 700px; }
#sa-drawer.sa-fullscreen .sa-welcome { padding: 48px 16px 16px; }

/* Expand/collapse button */
.sa-expand-btn {
  width: 32px; height: 32px; border-radius: 8px;
  border: none; background: transparent; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: var(--sa-text-secondary); transition: background 0.15s ease;
  flex-shrink: 0;
}
.sa-expand-btn:hover { background: var(--sa-surface); }
.sa-expand-btn svg { width: 16px; height: 16px; }

/* ── Header ── */
.sa-header {
  display: flex; align-items: center; gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--sa-border);
  flex-shrink: 0; background: white;
}
.sa-header-icon {
  width: 36px; height: 36px; border-radius: 10px;
  background: var(--sa-green-light);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.sa-header-icon svg { width: 20px; height: 20px; fill: var(--sa-green); }
.sa-header-text { flex: 1; min-width: 0; }
.sa-header-title { font-size: 15px; font-weight: 600; color: var(--sa-text); }
.sa-header-subtitle { font-size: 12px; color: var(--sa-text-muted); margin-top: 1px; }

.sa-close-btn {
  width: 32px; height: 32px; border-radius: 8px;
  border: none; background: transparent; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: var(--sa-text-secondary); transition: background 0.15s ease;
  flex-shrink: 0;
}
.sa-close-btn:hover { background: var(--sa-surface); }
.sa-close-btn svg { width: 18px; height: 18px; }

/* ── Messages ── */
.sa-messages {
  flex: 1; overflow-y: auto; padding: 20px;
  display: flex; flex-direction: column; gap: 16px;
  scroll-behavior: smooth;
}

/* Welcome */
.sa-welcome {
  display: flex; flex-direction: column; align-items: center;
  text-align: center; padding: 32px 16px 16px; gap: 16px;
}
.sa-welcome-icon {
  width: 56px; height: 56px; border-radius: 16px;
  background: var(--sa-green-light);
  display: flex; align-items: center; justify-content: center;
}
.sa-welcome-icon svg { width: 28px; height: 28px; fill: var(--sa-green); }
.sa-welcome h3 { font-size: 17px; font-weight: 600; color: var(--sa-text); margin: 0; }
.sa-welcome p { font-size: 13px; color: var(--sa-text-secondary); margin: 0; line-height: 1.5; max-width: 300px; }

/* Chips */
.sa-chips {
  display: flex; flex-wrap: wrap; gap: 8px;
  justify-content: center; padding: 0 8px; max-width: 380px;
}
.sa-chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px; border-radius: 20px;
  border: 1px solid var(--sa-border); background: white;
  color: var(--sa-text); font-size: 12.5px;
  font-family: var(--sa-font); cursor: pointer;
  transition: all 0.15s ease; line-height: 1.3; text-align: left;
}
.sa-chip:hover {
  border-color: var(--sa-green);
  background: var(--sa-green-light);
  color: var(--sa-green-dark);
}

/* Bubbles */
.sa-msg { display: flex; gap: 10px; max-width: 100%; animation: sa-fade-in 0.25s ease; }
@keyframes sa-fade-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}
.sa-msg-user { flex-direction: row-reverse; }
.sa-msg-avatar {
  width: 28px; height: 28px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 600; margin-top: 2px;
}
.sa-msg-user .sa-msg-avatar { background: var(--sa-green); color: white; }
.sa-msg-assistant .sa-msg-avatar { background: var(--sa-green-light); color: var(--sa-green); }
.sa-msg-avatar svg { width: 14px; height: 14px; }

.sa-msg-bubble {
  padding: 10px 14px; border-radius: var(--sa-radius);
  font-size: 13.5px; line-height: 1.55; max-width: 85%; word-wrap: break-word;
}
.sa-msg-user .sa-msg-bubble {
  background: var(--sa-user-bg); color: var(--sa-user-text);
  border-bottom-right-radius: 4px;
}
.sa-msg-assistant .sa-msg-bubble {
  background: var(--sa-assistant-bg); color: var(--sa-assistant-text);
  border-bottom-left-radius: 4px;
}
.sa-msg-bubble p { margin: 0 0 8px; }
.sa-msg-bubble p:last-child { margin-bottom: 0; }
.sa-msg-bubble strong { font-weight: 600; }
.sa-msg-bubble ol, .sa-msg-bubble ul { margin: 4px 0; padding-left: 20px; }
.sa-msg-bubble li { margin: 2px 0; }

/* Results toggle */
.sa-results-toggle {
  display: inline-flex; align-items: center; gap: 6px;
  margin-top: 8px; padding: 5px 10px; border-radius: 6px;
  border: 1px solid var(--sa-border); background: white;
  color: var(--sa-text-secondary); font-size: 11.5px;
  font-family: var(--sa-font); cursor: pointer; transition: all 0.15s ease;
}
.sa-results-toggle:hover { border-color: var(--sa-green); color: var(--sa-green-dark); }
.sa-results-toggle svg { width: 12px; height: 12px; transition: transform 0.2s; }
.sa-results-toggle.sa-expanded svg { transform: rotate(90deg); }

.sa-results-table-wrap {
  max-height: 0; overflow: hidden; transition: max-height 0.3s ease;
}
.sa-results-table-wrap.sa-expanded { max-height: 400px; overflow-y: auto; overflow-x: auto; margin-top: 8px; }

.sa-results-table { width: 100%; border-collapse: collapse; font-size: 11.5px; }
.sa-results-table th {
  background: var(--sa-surface); padding: 6px 10px; text-align: left;
  font-weight: 600; color: var(--sa-text-secondary);
  border-bottom: 1px solid var(--sa-border); white-space: nowrap;
  position: sticky; top: 0;
}
.sa-results-table td {
  padding: 5px 10px; border-bottom: 1px solid #f0f1f3;
  color: var(--sa-text); white-space: nowrap;
}
.sa-results-table tr:last-child td { border-bottom: none; }

/* SQL block */
.sa-sql-block { margin-top: 6px; max-height: 0; overflow: hidden; transition: max-height 0.3s ease; }
.sa-sql-block.sa-expanded { max-height: 300px; overflow-y: auto; }
.sa-sql-block pre {
  background: #1e1e2e; color: #cdd6f4;
  padding: 10px 12px; border-radius: 6px; font-size: 11px; line-height: 1.5;
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  white-space: pre-wrap; word-break: break-all; margin: 0;
}

/* Typing */
.sa-typing { display: flex; align-items: center; gap: 4px; padding: 12px 14px; }
.sa-typing-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--sa-text-muted);
  animation: sa-typing-bounce 1.4s ease-in-out infinite;
}
.sa-typing-dot:nth-child(2) { animation-delay: 0.16s; }
.sa-typing-dot:nth-child(3) { animation-delay: 0.32s; }
@keyframes sa-typing-bounce {
  0%,60%,100% { transform: translateY(0); opacity: 0.4; }
  30%          { transform: translateY(-5px); opacity: 1; }
}

/* ── Input ── */
.sa-input-area {
  padding: 12px 16px 16px; border-top: 1px solid var(--sa-border);
  background: white; flex-shrink: 0;
}
.sa-input-row { display: flex; gap: 8px; align-items: flex-end; }
.sa-input-wrapper { flex: 1; position: relative; }
#sa-input {
  width: 100%; padding: 10px 14px;
  border: 1.5px solid var(--sa-border); border-radius: 22px;
  font-size: 13.5px; font-family: var(--sa-font); color: var(--sa-text);
  background: var(--sa-surface); outline: none; resize: none;
  min-height: 42px; max-height: 120px; line-height: 1.4;
  transition: border-color 0.15s ease; box-sizing: border-box;
}
#sa-input:focus { border-color: var(--sa-green); background: white; }
#sa-input::placeholder { color: var(--sa-text-muted); }

#sa-send-btn {
  width: 42px; height: 42px; border-radius: 50%; border: none;
  background: var(--sa-green); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; transition: background 0.15s ease, opacity 0.15s ease;
}
#sa-send-btn:hover { background: var(--sa-green-dark); }
#sa-send-btn:disabled { opacity: 0.4; cursor: not-allowed; }
#sa-send-btn svg { width: 18px; height: 18px; fill: white; }

.sa-duration { font-size: 10.5px; color: var(--sa-text-muted); margin-top: 4px; }

@media (max-width: 480px) {
  #sa-drawer { width: 100vw; }
  #sa-drawer.sa-fullscreen .sa-messages { padding: 16px; }
  #sa-drawer.sa-fullscreen .sa-input-area { padding: 12px 16px 16px; }
  .sa-chips { padding: 0; }
}


/* ── Charts ── */
.sa-chart-block { margin-top: 10px; border: 1px solid var(--sa-border); border-radius: var(--sa-radius-sm); padding: 10px; background: white; }
.sa-chart-toolbar { display:flex; align-items:center; justify-content:space-between; gap:8px; margin-bottom:8px; }
.sa-chart-types { display:flex; gap:4px; flex-wrap:wrap; }
.sa-chart-type { font-family:var(--sa-font); font-size:11px; padding:4px 10px; border-radius:14px; border:1px solid var(--sa-border); background:white; color:var(--sa-text-secondary); cursor:pointer; transition:all .15s ease; }
.sa-chart-type:hover { border-color:var(--sa-green); color:var(--sa-green-dark); }
.sa-chart-type.sa-active { background:var(--sa-green); border-color:var(--sa-green); color:white; }
.sa-chart-actions { display:flex; gap:4px; }
.sa-chart-dl { width:28px; height:28px; border-radius:6px; border:1px solid var(--sa-border); background:white; color:var(--sa-text-secondary); cursor:pointer; display:flex; align-items:center; justify-content:center; }
.sa-chart-dl:hover { border-color:var(--sa-green); color:var(--sa-green-dark); }
.sa-chart-dl svg { width:15px; height:15px; }
.sa-chart-canvas-wrap { position:relative; height:240px; width:100%; }
#sa-drawer.sa-fullscreen .sa-chart-canvas-wrap { height:340px; }
.sa-chart-hint { font-size:10.5px; color:var(--sa-text-muted); margin-top:8px; }
.sa-ts-explore { margin-top:8px; display:inline-flex; align-items:center; gap:6px; padding:6px 12px; border-radius:8px; border:1px solid var(--sa-green); background:var(--sa-green-light); color:var(--sa-green-dark); font-family:var(--sa-font); font-size:12px; font-weight:500; cursor:pointer; transition:all .15s ease; }
.sa-ts-explore:hover { background:var(--sa-green); color:white; }
.sa-ts-explore svg { width:14px; height:14px; }
.sa-stats { display:flex; flex-wrap:wrap; gap:8px; margin-top:10px; }
.sa-stat { flex:1 1 120px; border:1px solid var(--sa-border); border-radius:var(--sa-radius-sm); padding:12px 14px; background:var(--sa-green-light); }
.sa-stat-val { font-size:20px; font-weight:700; color:var(--sa-green-dark); line-height:1.1; }
.sa-stat-lbl { font-size:11px; color:var(--sa-text-secondary); margin-top:4px; }


/* ── PO Order Menu ── */
.sa-po-menu { border: 1px solid #e2e8f0; border-radius: 10px; overflow: hidden; margin: 8px 0; }
.sa-po-header { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; background: #f0faf3; border-bottom: 1px solid #e2e8f0; }
.sa-po-title { font-weight: 700; color: #16843f; }
.sa-po-totals { display: flex; gap: 14px; font-size: 12px; color: #4a5568; }
.sa-po-totals span { font-weight: 600; }
.sa-po-row { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-bottom: 1px solid #f1f5f9; }
.sa-po-row:last-child { border-bottom: none; }
.sa-po-main { flex: 1; min-width: 0; }
.sa-po-name { font-weight: 600; color: #2d3748; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sa-po-sub { font-size: 12px; color: #718096; }
.sa-po-qty { text-align: center; min-width: 54px; }
.sa-po-qty-num { font-weight: 700; font-size: 18px; color: #1CA950; }
.sa-po-qty-lbl { font-size: 10px; color: #a0aec0; text-transform: uppercase; }
.sa-po-cost { text-align: right; min-width: 80px; }
.sa-po-line { font-weight: 600; color: #2d3748; }
.sa-po-unit { font-size: 11px; color: #a0aec0; }
.sa-po-dl { background: none; border: none; cursor: pointer; color: #16843f; padding: 0; display: inline-flex; }
.sa-po-dl svg { width: 15px; height: 15px; }