/**
 * Jason Health Chat - Widget Styles
 * MWeb Design | jason-health-chat v1.2.1
 */

:root {
  --jhc-primary:      #1a6b3c;
  --jhc-primary-dark: #145530;
  --jhc-top-offset:   0px;
  --jhc-bg:           #ffffff;
  --jhc-surface:      #f7f8fa;
  --jhc-border:       #e4e7ec;
  --jhc-text:         #1a1a2e;
  --jhc-text-muted:   #6b7280;
  --jhc-user-bg:      var(--jhc-primary);
  --jhc-user-color:   #ffffff;
  --jhc-bot-bg:       #f0f4f8;
  --jhc-bot-color:    #1a1a2e;
  --jhc-radius:       18px;
  --jhc-shadow:       0 8px 32px rgba(0,0,0,.18);
  --jhc-z:            999999;
  --jhc-w:            360px;
  --jhc-h:            520px;
}

/* ── Widget shell ──────────────────────────────────────────────────── */
.jhc-widget {
  position: fixed;
  bottom: 24px;
  z-index: var(--jhc-z);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  box-sizing: border-box;
}
.jhc-widget *, .jhc-widget *::before, .jhc-widget *::after { box-sizing: inherit; }
.jhc-bottom-right { right: 24px; }
.jhc-bottom-left  { left:  24px; }

/* ── Toggle button ─────────────────────────────────────────────────── */
.jhc-toggle {
  width: 58px; height: 58px;
  border-radius: 50%;
  background: var(--jhc-primary);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(0,0,0,.25);
  transition: background .2s, transform .2s, opacity .2s;
  position: relative;
  margin-left: auto;
  /* iOS: prevent double-tap zoom and grey flash */
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.jhc-bottom-left .jhc-toggle { margin-left: 0; margin-right: auto; }
.jhc-toggle:hover  { background: var(--jhc-primary-dark); transform: scale(1.07); }
.jhc-toggle:focus  { outline: 3px solid var(--jhc-primary); outline-offset: 3px; }
.jhc-toggle:active { transform: scale(.96); }

.jhc-unread {
  position: absolute; top: 0; right: 0;
  width: 18px; height: 18px;
  background: #e53e3e; color: #fff;
  border-radius: 50%;
  font-size: 11px; font-weight: 700;
  display: none; align-items: center; justify-content: center;
  border: 2px solid #fff;
}

/* ── Chat window ───────────────────────────────────────────────────── */
/* Hidden by default – shown by JS setting inline style AND by class   */
.jhc-window {
  display: none;
  position: absolute;
  bottom: 70px;
  width: var(--jhc-w);
  max-width: calc(100vw - 48px); /* never overflow viewport on narrow screens */
  flex-direction: column;
  overflow: hidden; /* clip any child that escapes its bounds */
}
/* CSS class hook – !important ensures it wins over any theme rule */
.jhc-widget.jhc-is-open .jhc-window {
  display: flex !important;
  animation: jhcSlideUp .22s ease;
}
.jhc-bottom-right .jhc-window { right: 0; }
.jhc-bottom-left  .jhc-window { left:  0; }

@keyframes jhcSlideUp {
  from { opacity: 0; transform: translateY(14px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Inner panel ───────────────────────────────────────────────────── */
/* min-height:0 on flex children is CRITICAL for scroll to work */
.jhc-inner {
  background: var(--jhc-bg);
  border-radius: 16px;
  box-shadow: var(--jhc-shadow);
  border: 1px solid var(--jhc-border);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  height: var(--jhc-h);
  width: 100%;
}

/* ── Desktop fullscreen overlay ────────────────────────────────────── */
.jhc-window.jhc-fullscreen {
  position: fixed !important;
  top: 0; left: 0; right: 0; bottom: 0;
  width: 100%;
  background: rgba(0,0,0,.5);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  justify-content: center;
  align-items: center;
  z-index: var(--jhc-z);
  animation: none;
}
.jhc-window.jhc-fullscreen .jhc-inner {
  width: 700px;
  max-width: 95vw;
  height: 82vh;
  max-height: 720px;
  border-radius: 16px;
}

/* ── Header ────────────────────────────────────────────────────────── */
.jhc-header {
  background: var(--jhc-primary);
  padding: 12px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
.jhc-header-info { display: flex; align-items: center; gap: 10px; }
.jhc-avatar {
  width: 36px; height: 36px;
  background: rgba(255,255,255,.2);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 17px; flex-shrink: 0;
}
.jhc-title    { color: #fff; font-weight: 700; font-size: 14px; line-height: 1.2; }
.jhc-subtitle { color: rgba(255,255,255,.8); font-size: 11px; line-height: 1.2; margin-top: 1px; }

.jhc-expand-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  opacity: .8;
  transition: opacity .2s, background .2s;
  flex-shrink: 0;
}
.jhc-expand-btn:hover { opacity: 1; background: rgba(255,255,255,.18); }
.jhc-expand-btn:focus { outline: 2px solid rgba(255,255,255,.6); outline-offset: 2px; }

/* ── Language bar ──────────────────────────────────────────────────── */
.jhc-lang-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 13px;
  background: var(--jhc-surface);
  border-bottom: 1px solid var(--jhc-border);
  flex-shrink: 0;
}
.jhc-lang-label { font-size: 12px; color: var(--jhc-text-muted); white-space: nowrap; font-weight: 500; }
.jhc-lang-select {
  flex: 1;
  border: 1px solid var(--jhc-border);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 13px;
  background: #fff; color: var(--jhc-text);
  cursor: pointer; outline: none;
}
.jhc-lang-select:focus { border-color: var(--jhc-primary); box-shadow: 0 0 0 2px rgba(26,107,60,.15); }

/* ── Disclaimer ────────────────────────────────────────────────────── */
.jhc-disclaimer {
  background: #fffbeb;
  border-bottom: 1px solid #fde68a;
  padding: 6px 13px;
  font-size: 11px; color: #92400e; line-height: 1.4;
  flex-shrink: 0;
}

/* ── Messages ──────────────────────────────────────────────────────── */
.jhc-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain; /* stop page scroll chaining through chat */
  padding: 14px;
  display: flex; flex-direction: column; gap: 10px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}
.jhc-messages::-webkit-scrollbar       { width: 4px; }
.jhc-messages::-webkit-scrollbar-thumb { background: var(--jhc-border); border-radius: 2px; }

.jhc-message { display: flex; flex-direction: column; max-width: 85%; }
.jhc-message.jhc-user  { align-self: flex-end;   align-items: flex-end; }
.jhc-message.jhc-bot   { align-self: flex-start; align-items: flex-start; }
.jhc-message.jhc-error { align-self: stretch; max-width: 100%; }

.jhc-bubble {
  padding: 10px 14px;
  border-radius: var(--jhc-radius);
  font-size: 14px; line-height: 1.55; word-break: break-word;
}
.jhc-bubble a { color: inherit; text-decoration: underline; }

.jhc-message.jhc-user .jhc-bubble {
  background: var(--jhc-user-bg); color: var(--jhc-user-color);
  border-bottom-right-radius: 4px;
}
.jhc-message.jhc-bot .jhc-bubble {
  background: var(--jhc-bot-bg); color: var(--jhc-bot-color);
  border-bottom-left-radius: 4px;
}
.jhc-message.jhc-error .jhc-bubble {
  background: #fff5f5; color: #c53030;
  border: 1px solid #fed7d7;
  border-radius: 8px; font-size: 13px; width: 100%;
}
.jhc-timestamp {
  font-size: 10px; color: var(--jhc-text-muted);
  margin-top: 3px; padding: 0 4px;
}

/* ── Typing indicator ──────────────────────────────────────────────── */
.jhc-typing {
  display: flex; align-items: center; gap: 5px;
  padding: 12px 16px; min-height: 42px;
}
.jhc-typing span {
  width: 7px; height: 7px;
  background: var(--jhc-text-muted); border-radius: 50%;
  display: inline-block;
  animation: jhcBounce 1.2s infinite ease-in-out;
}
.jhc-typing span:nth-child(1) { animation-delay: 0s; }
.jhc-typing span:nth-child(2) { animation-delay: .18s; }
.jhc-typing span:nth-child(3) { animation-delay: .36s; }
@keyframes jhcBounce {
  0%, 60%, 100% { transform: translateY(0); }
  30%           { transform: translateY(-6px); }
}

/* ── Book bar ──────────────────────────────────────────────────────── */
.jhc-book-bar {
  padding: 10px 13px;
  border-top: 1px solid var(--jhc-border);
  background: var(--jhc-surface);
  flex-shrink: 0;
  display: none;
}
.jhc-book-bar.jhc-visible {
  display: block;
  animation: jhcFadeIn .3s ease;
}
@keyframes jhcFadeIn { from { opacity: 0; } to { opacity: 1; } }

.jhc-book-btn {
  display: block; text-align: center;
  background: var(--jhc-primary);
  color: #fff !important;
  text-decoration: none !important;
  padding: 10px 16px;
  border-radius: 8px; font-weight: 700; font-size: 14px;
  transition: background .2s, transform .1s;
}
.jhc-book-btn:hover {
  background: var(--jhc-primary-dark);
  color: #fff !important;
  text-decoration: none !important;
  transform: translateY(-1px);
}
.jhc-book-btn:active { transform: translateY(0); }
.jhc-book-btn:focus  { outline: 3px solid var(--jhc-primary); outline-offset: 2px; }

/* ── Input area ────────────────────────────────────────────────────── */
.jhc-input-area {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px 12px;
  /* iPhone X+ home-indicator safe area */
  padding-bottom: max(10px, env(safe-area-inset-bottom, 10px));
  border-top: 1px solid var(--jhc-border);
  background: var(--jhc-bg);
  flex-shrink: 0;
}
.jhc-input {
  flex: 1;
  border: 1px solid var(--jhc-border); border-radius: 10px;
  padding: 9px 12px;
  font-size: 14px; font-family: inherit; color: var(--jhc-text);
  background: var(--jhc-surface);
  resize: none; outline: none;
  transition: border-color .2s;
  max-height: 120px; line-height: 1.5;
}
.jhc-input:focus { border-color: var(--jhc-primary); background: #fff; box-shadow: 0 0 0 2px rgba(26,107,60,.12); }
.jhc-input::placeholder { color: var(--jhc-text-muted); }

.jhc-send {
  width: 40px; height: 40px;
  border-radius: 50%; background: var(--jhc-primary);
  border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: background .2s, transform .1s;
  /* iOS: prevent double-tap zoom and grey flash */
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.jhc-send:hover    { background: var(--jhc-primary-dark); transform: scale(1.08); }
.jhc-send:active   { transform: scale(.94); }
.jhc-send:focus    { outline: 3px solid var(--jhc-primary); outline-offset: 2px; }
.jhc-send:disabled { opacity: .5; cursor: not-allowed; transform: none; }

/* ── Mobile (<= 479px) ─────────────────────────────────────────────── */
@media (max-width: 479px) {
  .jhc-widget { bottom: 0; right: 0; left: 0; width: 100%; }

  .jhc-toggle {
    position: fixed; bottom: 18px; right: 18px; margin: 0;
    transition: opacity .2s, transform .2s;
  }
  .jhc-bottom-left .jhc-toggle { right: auto; left: 18px; }

  /* Hide toggle when open so it doesn't cover the send button */
  .jhc-widget.jhc-is-open .jhc-toggle {
    opacity: 0;
    pointer-events: none;
    transform: scale(0.7);
  }

  /* Chat fills viewport from top offset downward */
  .jhc-bottom-right .jhc-window,
  .jhc-bottom-left  .jhc-window {
    position: fixed !important;
    top:    var(--jhc-top-offset, 0px);
    bottom: 0; left: 0; right: 0;
    width:  100%;
    max-width: 100vw; /* hard stop – nothing escapes the screen on iOS */
    animation: none;
    background: var(--jhc-bg) !important;
    backdrop-filter: none;
  }

  .jhc-bottom-right .jhc-window .jhc-inner,
  .jhc-bottom-left  .jhc-window .jhc-inner {
    /* -webkit-fill-available = viewport minus browser chrome on iOS Safari */
    height: -webkit-fill-available;
    height: 100%;
    max-height: 100%;
    border-radius: 0; box-shadow: none; border: none;
  }

  /* Fullscreen is a no-op on mobile (already full screen) */
  .jhc-window.jhc-fullscreen { background: var(--jhc-bg) !important; backdrop-filter: none; align-items: stretch; }
  .jhc-window.jhc-fullscreen .jhc-inner { width: 100%; max-width: 100%; height: 100%; max-height: 100%; border-radius: 0; }
}

/* ── RTL (Arabic) ──────────────────────────────────────────────────── */
[dir="rtl"] .jhc-input-area,
[dir="rtl"] .jhc-lang-bar,
[dir="rtl"] .jhc-header-info { flex-direction: row-reverse; }
[dir="rtl"] .jhc-message.jhc-user { align-self: flex-start; align-items: flex-start; }
[dir="rtl"] .jhc-message.jhc-bot  { align-self: flex-end;   align-items: flex-end; }
[dir="rtl"] .jhc-message.jhc-user .jhc-bubble { border-bottom-right-radius: var(--jhc-radius); border-bottom-left-radius: 4px; }
[dir="rtl"] .jhc-message.jhc-bot  .jhc-bubble { border-bottom-left-radius:  var(--jhc-radius); border-bottom-right-radius: 4px; }
