/**
 * EWF-003 — Support Hub Widget UX (Phase-1.5)
 * Intercom / Freshdesk-inspired. Frontend only — no API changes.
 */
:root {
  --esh-accent: #6366f1;
  --esh-accent-2: #8b5cf6;
  --esh-accent-hover: #4f46e5;
  --esh-accent-soft: rgba(99, 102, 241, 0.14);
  --esh-bg: #ffffff;
  --esh-bg-alt: #f8fafc;
  --esh-surface: #ffffff;
  --esh-border: #e2e8f0;
  --esh-text: #0f172a;
  --esh-text-muted: #64748b;
  --esh-shadow: 0 18px 50px rgba(15, 23, 42, 0.2);
  --esh-radius: 18px;
  --esh-fab-size: 60px;
  --esh-panel-width: 400px;
  --esh-bubble-out: linear-gradient(135deg, #6366f1, #7c3aed);
  --esh-bubble-out-text: #ffffff;
  --esh-bubble-in: #f1f5f9;
  --esh-bubble-in-text: #0f172a;
  --esh-danger: #ef4444;
  --esh-success: #10b981;
  --esh-warn: #f59e0b;
  --esh-z: 99990;
  --esh-focus: 0 0 0 3px rgba(99, 102, 241, 0.35);
}

.esh-widget-root[data-theme="dark"],
[data-theme="dark"] .esh-widget-root {
  --esh-bg: #0f172a;
  --esh-bg-alt: #1e293b;
  --esh-surface: #1e293b;
  --esh-border: #334155;
  --esh-text: #f1f5f9;
  --esh-text-muted: #94a3b8;
  --esh-shadow: 0 20px 60px rgba(0, 0, 0, 0.55);
  --esh-bubble-in: #334155;
  --esh-bubble-in-text: #f1f5f9;
}

@keyframes esh-slide-up {
  from { opacity: 0; transform: translateY(16px) scale(0.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes esh-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(99, 102, 241, 0.55); }
  50% { box-shadow: 0 0 0 14px rgba(99, 102, 241, 0); }
}
@keyframes esh-dot {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}
@keyframes esh-fade {
  from { opacity: 0; } to { opacity: 1; }
}
@keyframes esh-shimmer {
  0% { background-position: -200% 0; }
  100% { background-position: 200% 0; }
}

.esh-widget-root {
  position: fixed;
  bottom: 22px;
  right: 22px;
  z-index: var(--esh-z);
  font-family: "Plus Jakarta Sans", system-ui, -apple-system, Segoe UI, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  color: var(--esh-text);
  transition: transform 0.25s ease, opacity 0.25s ease;
}
.esh-widget-root.esh-scroll-hidden {
  transform: translateY(110%);
  opacity: 0;
  pointer-events: none;
}

/* —— FAB —— */
.esh-fab {
  width: var(--esh-fab-size);
  height: var(--esh-fab-size);
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, var(--esh-accent), var(--esh-accent-2));
  color: #fff;
  box-shadow: var(--esh-shadow);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  position: relative;
}
.esh-fab:hover { transform: scale(1.07); }
.esh-fab:focus-visible { outline: none; box-shadow: var(--esh-focus), var(--esh-shadow); }
.esh-fab.has-unread { animation: esh-pulse 1.8s ease infinite; }
.esh-fab-badge {
  position: absolute;
  top: -3px;
  right: -3px;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 11px;
  background: var(--esh-danger);
  color: #fff;
  font-size: 11px;
  font-weight: 800;
  display: none;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--esh-surface);
}
.esh-fab-badge.visible { display: flex; }
.esh-fab-dot {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--esh-danger);
  border: 2px solid #fff;
  display: none;
}
.esh-fab-dot.visible { display: block; }
.esh-fab.open i.bi-headset { display: none; }
.esh-fab.open::after { content: "✕"; font-size: 22px; font-weight: 600; }

/* —— Panel —— */
.esh-panel {
  position: absolute;
  bottom: calc(var(--esh-fab-size) + 14px);
  right: 0;
  width: var(--esh-panel-width);
  max-width: calc(100vw - 28px);
  max-height: min(680px, calc(100vh - 100px));
  background: var(--esh-surface);
  border: 1px solid var(--esh-border);
  border-radius: var(--esh-radius);
  box-shadow: var(--esh-shadow);
  display: none;
  flex-direction: column;
  overflow: hidden;
  animation: esh-slide-up 0.28s cubic-bezier(0.22, 1, 0.36, 1);
  opacity: 1;
  isolation: isolate;
}
.esh-panel.open { display: flex; }

.esh-panel-header {
  padding: 14px 16px;
  background: linear-gradient(135deg, var(--esh-accent), var(--esh-accent-2));
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-shrink: 0;
}
.esh-panel-title {
  margin: 0;
  font-size: 16px;
  font-weight: 800;
  display: flex;
  align-items: center;
  gap: 8px;
}
.esh-header-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  opacity: 0.92;
}
.esh-online-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #86efac;
  box-shadow: 0 0 0 3px rgba(134, 239, 172, 0.25);
}
.esh-panel-actions { display: flex; gap: 4px; }
.esh-icon-btn {
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 10px;
  background: rgba(255,255,255,0.16);
  color: #fff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.esh-icon-btn:hover { background: rgba(255,255,255,0.28); }
.esh-icon-btn:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }

.esh-conn-banner {
  display: none;
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 600;
  text-align: center;
  background: #fef3c7;
  color: #92400e;
  border-bottom: 1px solid #fde68a;
}
.esh-conn-banner.visible { display: block; }
.esh-conn-banner.offline { background: #fee2e2; color: #991b1b; border-color: #fecaca; }

.esh-tabs-wrap {
  display: flex;
  align-items: stretch;
  flex-shrink: 0;
  border-bottom: 1px solid var(--esh-border);
  background: var(--esh-bg-alt);
  position: relative;
}
.esh-tabs-nav {
  flex: 0 0 28px;
  width: 28px;
  border: none;
  background: var(--esh-bg-alt);
  color: var(--esh-text-muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
  padding: 0;
}
.esh-tabs-nav:hover { color: var(--esh-accent); background: rgba(99,102,241,0.08); }
.esh-tabs-nav:disabled {
  opacity: 0.35;
  cursor: default;
  pointer-events: none;
}
.esh-tabs-wrap:not(.has-overflow) .esh-tabs-nav { display: none; }
.esh-tabs-wrap.has-overflow::before,
.esh-tabs-wrap.has-overflow::after {
  content: '';
  position: absolute;
  top: 0;
  bottom: 1px;
  width: 18px;
  pointer-events: none;
  z-index: 1;
}
.esh-tabs-wrap.has-overflow::before {
  left: 28px;
  background: linear-gradient(90deg, var(--esh-bg-alt), transparent);
}
.esh-tabs-wrap.has-overflow::after {
  right: 28px;
  background: linear-gradient(270deg, var(--esh-bg-alt), transparent);
}
.esh-tabs {
  display: flex;
  gap: 2px;
  padding: 6px 4px 0;
  overflow-x: auto;
  flex: 1;
  min-width: 0;
  scrollbar-width: thin;
  scrollbar-color: rgba(99,102,241,0.35) transparent;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}
.esh-tabs::-webkit-scrollbar { height: 3px; }
.esh-tabs::-webkit-scrollbar-thumb {
  background: rgba(99,102,241,0.35);
  border-radius: 999px;
}
.esh-tab {
  flex: 0 0 auto;
  border: none;
  background: transparent;
  color: var(--esh-text-muted);
  padding: 8px 10px;
  font-size: 11px;
  font-weight: 700;
  border-radius: 10px 10px 0 0;
  cursor: pointer;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.esh-tab span { max-width: 72px; overflow: hidden; text-overflow: ellipsis; }
.esh-tab:hover { color: var(--esh-text); background: rgba(99,102,241,0.06); }
.esh-tab.active {
  color: var(--esh-accent);
  background: var(--esh-surface);
  box-shadow: 0 -1px 0 var(--esh-surface);
}
.esh-tab:focus-visible { outline: none; box-shadow: var(--esh-focus); }

.esh-body {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--esh-surface);
}
.esh-pane {
  display: none;
  flex: 1;
  min-height: 0;
  flex-direction: column;
  overflow: hidden;
}
.esh-pane.active { display: flex; }

/* —— Chat —— */
.esh-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scroll-behavior: smooth;
  background:
    radial-gradient(circle at 10% 10%, rgba(99,102,241,0.05), transparent 40%),
    var(--esh-bg-alt);
}
.esh-day-sep {
  align-self: center;
  margin: 10px 0;
  padding: 4px 12px;
  border-radius: 999px;
  background: var(--esh-surface);
  border: 1px solid var(--esh-border);
  color: var(--esh-text-muted);
  font-size: 11px;
  font-weight: 700;
}
.esh-msg-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  margin: 2px 0;
  max-width: 92%;
  animation: esh-fade 0.2s ease;
}
.esh-msg-row.outgoing { align-self: flex-end; flex-direction: row-reverse; }
.esh-msg-row.incoming { align-self: flex-start; }
.esh-msg-row.grouped .esh-avatar { visibility: hidden; }
.esh-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, #c7d2fe, #a5b4fc);
  color: #312e81;
  font-size: 11px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.esh-msg-row.outgoing .esh-avatar {
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  color: #fff;
}
.esh-msg {
  padding: 10px 12px;
  border-radius: 16px;
  font-size: 13.5px;
  word-break: break-word;
  position: relative;
  box-shadow: 0 1px 2px rgba(15,23,42,0.05);
}
.esh-msg-row.incoming .esh-msg {
  background: var(--esh-bubble-in);
  color: var(--esh-bubble-in-text);
  border-bottom-left-radius: 5px;
}
.esh-msg-row.outgoing .esh-msg {
  background: var(--esh-bubble-out);
  color: var(--esh-bubble-out-text);
  border-bottom-right-radius: 5px;
}
.esh-msg.sending { opacity: 0.65; }
.esh-msg-meta {
  margin-top: 4px;
  font-size: 10px;
  opacity: 0.75;
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: flex-end;
}
.esh-msg-status i { font-size: 12px; }
.esh-msg-attach {
  margin-top: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  background: rgba(255,255,255,0.15);
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  font-weight: 600;
}
.esh-msg-row.incoming .esh-msg-attach { background: rgba(15,23,42,0.06); }
.esh-msg-attach img {
  max-width: 180px;
  max-height: 140px;
  border-radius: 8px;
  display: block;
}

.esh-typing {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  color: var(--esh-text-muted);
  font-size: 12px;
}
.esh-typing.visible { display: flex; }
.esh-typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--esh-text-muted);
  display: inline-block;
  margin-right: 3px;
  animation: esh-dot 1.2s infinite;
}
.esh-typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.esh-typing-dots span:nth-child(3) { animation-delay: 0.3s; }

.esh-jump-latest {
  position: absolute;
  bottom: 118px;
  right: 28px;
  z-index: 2;
  border: 1px solid var(--esh-border);
  background: var(--esh-surface);
  color: var(--esh-text);
  border-radius: 999px;
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 700;
  box-shadow: var(--esh-shadow);
  cursor: pointer;
  display: none;
}
.esh-jump-latest.visible { display: inline-flex; align-items: center; gap: 6px; }

/* —— Composer —— */
.esh-chat-compose {
  border-top: 1px solid var(--esh-border);
  padding: 10px 12px 12px;
  background: var(--esh-surface);
  flex-shrink: 0;
  position: relative;
}
.esh-drop-overlay {
  display: none;
  position: absolute;
  inset: 0;
  background: rgba(99,102,241,0.12);
  border: 2px dashed var(--esh-accent);
  border-radius: 12px;
  z-index: 5;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  color: var(--esh-accent);
}
.esh-drop-overlay.visible { display: flex; }
.esh-attach-preview {
  display: none;
  gap: 8px;
  align-items: center;
  padding: 8px;
  margin-bottom: 8px;
  border-radius: 12px;
  background: var(--esh-bg-alt);
  border: 1px solid var(--esh-border);
}
.esh-attach-preview.visible { display: flex; }
.esh-attach-preview img {
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: 8px;
}
.esh-attach-preview .meta { flex: 1; min-width: 0; font-size: 12px; }
.esh-attach-preview .meta strong { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.esh-progress {
  height: 4px;
  background: var(--esh-border);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 6px;
}
.esh-progress > span {
  display: block;
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--esh-accent), var(--esh-accent-2));
  transition: width 0.15s ease;
}
.esh-compose-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
.esh-compose-input {
  flex: 1;
  resize: none;
  min-height: 42px;
  max-height: 120px;
  border: 1px solid var(--esh-border);
  border-radius: 14px;
  padding: 10px 12px;
  font: inherit;
  background: var(--esh-bg-alt);
  color: var(--esh-text);
}
.esh-compose-input:focus {
  outline: none;
  border-color: var(--esh-accent);
  box-shadow: var(--esh-focus);
  background: var(--esh-surface);
}
.esh-send-btn {
  width: 42px;
  height: 42px;
  border: none;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--esh-accent), var(--esh-accent-2));
  color: #fff;
  cursor: pointer;
  flex-shrink: 0;
}
.esh-send-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.esh-send-btn:focus-visible { outline: none; box-shadow: var(--esh-focus); }
.esh-compose-tools {
  display: flex;
  gap: 4px;
  margin-top: 8px;
  align-items: center;
  flex-wrap: wrap;
}
.esh-tool-btn {
  border: none;
  background: transparent;
  color: var(--esh-text-muted);
  font-size: 12px;
  font-weight: 700;
  padding: 6px 8px;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.esh-tool-btn:hover { background: var(--esh-accent-soft); color: var(--esh-accent); }
.esh-tool-btn:focus-visible { outline: none; box-shadow: var(--esh-focus); }
.esh-hint {
  margin-left: auto;
  font-size: 10px;
  color: var(--esh-text-muted);
}
.esh-emoji-pop {
  display: none;
  position: absolute;
  bottom: 100%;
  left: 8px;
  width: 260px;
  max-height: 180px;
  overflow: auto;
  background: var(--esh-surface);
  border: 1px solid var(--esh-border);
  border-radius: 14px;
  box-shadow: var(--esh-shadow);
  padding: 8px;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
  z-index: 6;
}
.esh-emoji-pop.open { display: grid; }
.esh-emoji-pop button {
  border: none;
  background: transparent;
  font-size: 18px;
  padding: 4px;
  border-radius: 8px;
  cursor: pointer;
}
.esh-emoji-pop button:hover { background: var(--esh-accent-soft); }

/* —— Forms / lists —— */
.esh-form-pane, .esh-kb-wrap, .esh-tickets-wrap, .esh-announce-wrap {
  padding: 14px;
  overflow-y: auto;
  flex: 1;
}
.esh-form-group { margin-bottom: 12px; }
.esh-form-group label {
  display: block;
  font-size: 12px;
  font-weight: 700;
  margin-bottom: 6px;
  color: var(--esh-text-muted);
}
.esh-form-group input,
.esh-form-group textarea {
  width: 100%;
  border: 1px solid var(--esh-border);
  border-radius: 12px;
  padding: 10px 12px;
  font: inherit;
  background: var(--esh-bg-alt);
  color: var(--esh-text);
}
.esh-form-group input:focus,
.esh-form-group textarea:focus {
  outline: none;
  border-color: var(--esh-accent);
  box-shadow: var(--esh-focus);
}
.esh-btn-primary {
  width: 100%;
  border: none;
  border-radius: 12px;
  padding: 12px;
  font-weight: 800;
  color: #fff;
  background: linear-gradient(135deg, var(--esh-accent), var(--esh-accent-2));
  cursor: pointer;
}
.esh-btn-primary:hover { filter: brightness(1.05); }
.esh-btn-link {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--esh-accent-soft);
  color: var(--esh-accent);
  font-weight: 800;
  text-decoration: none;
}
.esh-search {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}
.esh-search input {
  flex: 1;
  border: 1px solid var(--esh-border);
  border-radius: 12px;
  padding: 10px 12px;
  background: var(--esh-bg-alt);
  color: var(--esh-text);
  font: inherit;
}
.esh-kb-item, .esh-ticket-card {
  padding: 12px;
  border: 1px solid var(--esh-border);
  border-radius: 14px;
  margin-bottom: 10px;
  background: var(--esh-surface);
  cursor: pointer;
  transition: border-color 0.15s, transform 0.15s;
}
.esh-kb-item:hover, .esh-ticket-card:hover {
  border-color: var(--esh-accent);
  transform: translateY(-1px);
}
.esh-kb-item-meta, .esh-list-item-meta {
  font-size: 11px;
  color: var(--esh-text-muted);
  margin-top: 4px;
}
.esh-kb-body { font-size: 13px; color: var(--esh-text-muted); margin-top: 8px; }
.esh-status-badge {
  display: inline-block;
  margin-left: 8px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
  background: var(--esh-accent-soft);
  color: var(--esh-accent);
}
.esh-chip-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.esh-chip {
  border: 1px solid var(--esh-border);
  background: var(--esh-bg-alt);
  color: var(--esh-text-muted);
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}
.esh-chip.active {
  background: var(--esh-accent-soft);
  color: var(--esh-accent);
  border-color: transparent;
}

.esh-empty, .esh-coming-soon {
  text-align: center;
  padding: 40px 20px;
  color: var(--esh-text-muted);
}
.esh-empty i, .esh-coming-soon i {
  font-size: 36px;
  display: block;
  margin-bottom: 10px;
  opacity: 0.45;
}
.esh-coming-soon h4 { margin: 0 0 6px; color: var(--esh-text); }

.esh-alert {
  margin: 10px 12px 0;
  padding: 10px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
}
.esh-alert-success { background: #ecfdf5; color: #065f46; }
.esh-alert-error { background: #fef2f2; color: #991b1b; }
.esh-widget-root[data-theme="dark"] .esh-alert-success { background: #064e3b; color: #a7f3d0; }
.esh-widget-root[data-theme="dark"] .esh-alert-error { background: #7f1d1d; color: #fecaca; }

.esh-skeleton {
  border-radius: 12px;
  background: linear-gradient(90deg, var(--esh-border), var(--esh-bg-alt), var(--esh-border));
  background-size: 200% 100%;
  animation: esh-shimmer 1.2s infinite;
  height: 54px;
  margin-bottom: 8px;
}

.esh-spinner {
  width: 22px;
  height: 22px;
  border: 2px solid var(--esh-border);
  border-top-color: var(--esh-accent);
  border-radius: 50%;
  display: inline-block;
  animation: esh-spin 0.7s linear infinite;
}
@keyframes esh-spin { to { transform: rotate(360deg); } }

/* —— Responsive —— */
@media (max-width: 480px) {
  .esh-widget-root {
    bottom: 14px;
    right: 14px;
    left: auto;
    display: block;
  }
  .esh-panel {
    position: fixed;
    left: 8px;
    right: 8px;
    bottom: 78px;
    width: auto;
    max-width: none;
    max-height: calc(100dvh - 96px);
    border-radius: 16px;
    background: var(--esh-surface);
    opacity: 1;
    backdrop-filter: none;
  }
  .esh-fab {
    position: relative;
    margin: 0;
    float: none;
  }
  .esh-body,
  .esh-chat-messages,
  .esh-chat-compose,
  .esh-tabs {
    background: var(--esh-surface);
  }
  .esh-chat-messages {
    background:
      radial-gradient(circle at 10% 10%, rgba(99,102,241,0.05), transparent 40%),
      var(--esh-bg-alt);
  }
  .esh-compose-tools .esh-hint { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .esh-fab, .esh-panel, .esh-msg-row, .esh-fab.has-unread { animation: none !important; transition: none !important; }
}

@media (prefers-contrast: more) {
  .esh-fab, .esh-tab.active, .esh-btn-primary { outline: 2px solid currentColor; }
}
