/* Support chat widget ─ floating bubble + slide-up panel.
   Scoped under #support-chat-root so it never leaks into page styles. */

#support-chat-root {
  --sc-primary: #0f172a;
  --sc-primary-hover: #1e293b;
  --sc-accent: #6366f1;
  --sc-border: #e2e8f0;
  --sc-muted: #64748b;
  --sc-bg: #ffffff;
  --sc-bg-alt: #f8fafc;
  --sc-radius: 12px;
  --sc-shadow: 0 10px 30px -10px rgba(15, 23, 42, .25), 0 4px 6px -4px rgba(15, 23, 42, .1);
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  color: #0f172a;
}

/* The HTML `hidden` attribute uses UA `display: none`, but several
   widget elements below explicitly set `display: flex`, which has
   higher specificity than the UA rule and prevents the attribute
   from hiding them. These rules restore the expected behaviour so
   `element.hidden = true` actually hides the panel, intro, and
   conversation. */
#support-chat-root [hidden],
#support-chat-panel[hidden],
#support-chat-intro[hidden],
#support-chat-conversation[hidden] { display: none !important; }

#support-chat-bubble {
  position: fixed;
  right: 22px; bottom: 22px;
  width: 56px; height: 56px;
  border-radius: 50%;
  background: var(--sc-primary);
  color: #ffffff;
  border: 0;
  box-shadow: var(--sc-shadow);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  z-index: 9998;
  transition: transform .15s ease, background-color .15s ease;
}
#support-chat-bubble:hover { background: var(--sc-primary-hover); transform: translateY(-1px); }
#support-chat-bubble:focus-visible {
  outline: 3px solid var(--sc-accent); outline-offset: 2px;
}
#support-chat-bubble .sc-unread {
  position: absolute; top: -4px; right: -4px;
  min-width: 20px; height: 20px; padding: 0 5px;
  background: #ef4444; color: #ffffff;
  border-radius: 999px;
  font-size: 11px; font-weight: 700; line-height: 20px;
  text-align: center;
  box-shadow: 0 0 0 2px #ffffff;
}

#support-chat-panel {
  position: fixed;
  right: 22px; bottom: 90px;
  width: min(380px, calc(100vw - 32px));
  max-height: min(620px, calc(100vh - 120px));
  background: var(--sc-bg);
  border-radius: var(--sc-radius);
  box-shadow: var(--sc-shadow);
  border: 1px solid var(--sc-border);
  display: flex; flex-direction: column;
  overflow: hidden;
  z-index: 9999;
}

.sc-header {
  background: var(--sc-primary);
  color: #ffffff;
  padding: 14px 16px;
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 10px;
}
.sc-header h2 {
  margin: 0;
  font-size: 15px; font-weight: 600; letter-spacing: .01em;
}
.sc-subtitle {
  margin: 2px 0 0; font-size: 12px; color: rgba(255, 255, 255, .75);
}
#support-chat-close {
  background: transparent; color: #ffffff;
  border: 0; padding: 4px; border-radius: 4px; cursor: pointer;
  display: grid; place-items: center;
}
#support-chat-close:hover { background: rgba(255, 255, 255, .12); }

.sc-body {
  flex: 1;
  display: flex; flex-direction: column;
  min-height: 0;
  background: var(--sc-bg-alt);
}

.sc-intro {
  padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
  background: var(--sc-bg);
  border-bottom: 1px solid var(--sc-border);
}
.sc-intro-copy {
  margin: 0 0 4px; font-size: 13px; color: var(--sc-muted);
  line-height: 1.45;
}
.sc-field {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 12px; color: var(--sc-muted); font-weight: 500;
}
.sc-field input, .sc-field textarea {
  font: inherit; font-size: 14px;
  color: #0f172a;
  padding: 8px 10px;
  border: 1px solid var(--sc-border);
  border-radius: 6px;
  background: #ffffff;
  resize: vertical;
}
.sc-field input:focus, .sc-field textarea:focus {
  outline: 2px solid var(--sc-accent); outline-offset: 0;
  border-color: var(--sc-accent);
}
.sc-primary {
  padding: 10px 16px;
  border: 0; border-radius: 6px;
  background: var(--sc-primary); color: #ffffff;
  font: inherit; font-size: 14px; font-weight: 600;
  cursor: pointer;
  transition: background-color .15s ease;
}
.sc-primary:hover { background: var(--sc-primary-hover); }
.sc-primary:disabled { opacity: .6; cursor: wait; }

.sc-conversation {
  flex: 1;
  display: flex; flex-direction: column;
  min-height: 0;
}
.sc-messages {
  flex: 1;
  overflow-y: auto;
  margin: 0; padding: 14px 14px 8px;
  list-style: none;
  display: flex; flex-direction: column; gap: 8px;
  scrollbar-width: thin;
}
.sc-messages li {
  max-width: 82%;
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 14px; line-height: 1.4;
  word-wrap: break-word; overflow-wrap: anywhere;
}
.sc-messages li.sc-from-visitor,
.sc-messages li.sc-from-user {
  background: var(--sc-primary); color: #ffffff;
  align-self: flex-end;
  border-bottom-right-radius: 2px;
}
.sc-messages li.sc-from-staff {
  background: #ffffff; color: #0f172a;
  border: 1px solid var(--sc-border);
  align-self: flex-start;
  border-bottom-left-radius: 2px;
}
.sc-messages li.sc-from-system {
  align-self: center;
  background: transparent; color: var(--sc-muted);
  font-size: 12px; font-style: italic;
  padding: 4px 0;
}
.sc-messages time {
  display: block;
  font-size: 11px; opacity: .65; margin-top: 4px;
}

.sc-reply {
  display: flex; gap: 8px;
  padding: 10px 12px;
  background: var(--sc-bg);
  border-top: 1px solid var(--sc-border);
}
.sc-reply textarea {
  flex: 1;
  font: inherit; font-size: 14px;
  padding: 8px 10px;
  border: 1px solid var(--sc-border);
  border-radius: 6px;
  resize: none;
  background: var(--sc-bg-alt);
}
.sc-reply textarea:focus {
  outline: 2px solid var(--sc-accent); outline-offset: 0;
  border-color: var(--sc-accent);
}
.sc-reply button {
  flex-shrink: 0;
  width: 40px; height: 40px;
  display: grid; place-items: center;
  border: 0; border-radius: 6px;
  background: var(--sc-primary); color: #ffffff;
  cursor: pointer;
}
.sc-reply button:hover { background: var(--sc-primary-hover); }
.sc-reply button:disabled { opacity: .5; cursor: wait; }

.sc-error {
  padding: 8px 12px;
  background: #fef2f2; color: #991b1b;
  border-top: 1px solid #fecaca;
  font-size: 13px;
}

/* Hide widget entirely when an explicit opt-out is in play
   (e.g. certificate PDF capture pages, admin inbox itself). */
body.support-chat-hidden #support-chat-root { display: none !important; }

@media (max-width: 480px) {
  #support-chat-panel {
    right: 8px; left: 8px; bottom: 80px;
    width: auto;
    max-height: calc(100vh - 100px);
  }
  #support-chat-bubble { right: 14px; bottom: 14px; }
}

@media print { #support-chat-root { display: none !important; } }
