/* ═══════════════════════════════════════════════════
   Sekar AI Chat Widget — CV. Sekar Tama Contraction
   Fully responsive: desktop + tablet + mobile
   ═══════════════════════════════════════════════════ */

/* ── CSS Variables (overridden by JS from admin settings) ── */
:root {
  --sac-primary:        #1e3a6e;
  --sac-secondary:      #f5a623;
  --sac-text-on-p:      #ffffff;
  --sac-bg:             #ffffff;
  --sac-bot-bubble:     #f0f4ff;
  --sac-user-bubble:    #1e3a6e;
  --sac-text:           #1a1a2e;
  --sac-shadow:         0 8px 32px rgba(30,58,110,0.18);
  --sac-radius:         18px;
  --sac-font:           -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --sac-transition:     0.3s cubic-bezier(0.34, 1.56, 0.64, 1);

  /* Safe-area insets for iPhone notch/home bar */
  --sac-safe-bottom: env(safe-area-inset-bottom, 0px);
  --sac-safe-right:  env(safe-area-inset-right,  0px);
}

/* ═══════════════
   TOGGLE BUTTON
   ═══════════════ */
#sac-toggle {
  position: fixed;
  bottom: calc(28px + var(--sac-safe-bottom));
  right:  calc(28px + var(--sac-safe-right));
  width:  62px;
  height: 62px;
  border-radius: 50%;
  background: var(--sac-primary);
  border: 3px solid var(--sac-secondary);
  box-shadow: 0 4px 20px rgba(30,58,110,0.35);
  cursor: pointer;
  z-index: 99996;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.25s ease, box-shadow 0.25s ease;
  padding: 0;
  overflow: hidden;
  /* Larger tap target on touch */
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
#sac-toggle:hover {
  transform: scale(1.1);
  box-shadow: 0 6px 28px rgba(30,58,110,0.45);
}
#sac-toggle:active { transform: scale(0.95); }

#sac-toggle img.sac-toggle-avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}
#sac-toggle svg {
  width: 28px;
  height: 28px;
  fill: var(--sac-text-on-p);
}
/* Notification dot */
#sac-toggle .sac-dot {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 12px;
  height: 12px;
  background: var(--sac-secondary);
  border-radius: 50%;
  border: 2px solid white;
  animation: sac-pulse 2s infinite;
}
@keyframes sac-pulse {
  0%,100% { transform: scale(1);   opacity: 1; }
  50%      { transform: scale(1.3); opacity: 0.7; }
}

/* ══════════════
   CHAT WINDOW
   ══════════════ */
#sac-window {
  position: fixed;
  bottom: calc(104px + var(--sac-safe-bottom));
  right:  calc(28px  + var(--sac-safe-right));
  width:  370px;
  max-height: 560px;
  min-height: 0;
  background: var(--sac-bg);
  border-radius: var(--sac-radius);
  box-shadow: var(--sac-shadow);
  display: flex;
  flex-direction: column;
  z-index: 99999;
  overflow: hidden;

  /* Closed state */
  transform: scale(0.85) translateY(30px);
  opacity: 0;
  pointer-events: none;
  transition: transform var(--sac-transition), opacity 0.25s ease;
  transform-origin: bottom right;
  font-family: var(--sac-font);
}
#sac-window.sac-open {
  transform: scale(1) translateY(0);
  opacity: 1;
  pointer-events: all;
}

/* ═══════════
   HEADER
   ═══════════ */
.sac-header {
  background: var(--sac-primary);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--sac-text-on-p);
  flex-shrink: 0;
}
.sac-header-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 2px solid var(--sac-secondary);
  object-fit: cover;
  flex-shrink: 0;
}
.sac-header-info  { flex: 1; min-width: 0; }
.sac-header-name  { font-weight: 700; font-size: 15px; line-height: 1.2; }
.sac-header-status {
  font-size: 11px;
  opacity: 0.85;
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 2px;
}
.sac-header-status::before {
  content: '';
  width: 7px; height: 7px;
  background: #4ade80;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
  animation: sac-pulse 2.5s infinite;
}
.sac-close-btn {
  background: none;
  border: none;
  color: var(--sac-text-on-p);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  opacity: 0.75;
  transition: opacity 0.2s, background 0.2s;
  display: flex;
  align-items: center;
  /* Larger tap target */
  min-width: 36px;
  min-height: 36px;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}
.sac-close-btn:hover { opacity: 1; background: rgba(255,255,255,0.15); }
.sac-close-btn svg   { width: 18px; height: 18px; fill: currentColor; }

/* ══════════════════
   MESSAGES AREA
   ══════════════════ */
.sac-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #f7f9ff;
  scroll-behavior: smooth;
  /* Momentum scroll on iOS */
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.sac-messages::-webkit-scrollbar { width: 4px; }
.sac-messages::-webkit-scrollbar-track { background: transparent; }
.sac-messages::-webkit-scrollbar-thumb { background: #d0d9f0; border-radius: 4px; }

/* ═════════
   BUBBLES
   ═════════ */
.sac-bubble-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  animation: sac-fadein 0.3s ease;
}
@keyframes sac-fadein {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
.sac-bubble-row.sac-user { flex-direction: row-reverse; }

.sac-bubble-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  flex-shrink: 0;
  object-fit: cover;
  border: 1.5px solid var(--sac-secondary);
}
.sac-bubble {
  max-width: 80%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 13.5px;
  line-height: 1.55;
  word-break: break-word;
  word-wrap: break-word;
  overflow-wrap: break-word;
}
.sac-bubble-row:not(.sac-user) .sac-bubble {
  background: var(--sac-bot-bubble);
  color: var(--sac-text);
  border-bottom-left-radius: 4px;
}
.sac-bubble-row.sac-user .sac-bubble {
  background: var(--sac-user-bubble);
  color: #fff;
  border-bottom-right-radius: 4px;
}

/* Typing indicator */
.sac-typing .sac-bubble {
  display: flex;
  gap: 5px;
  align-items: center;
  padding: 12px 16px;
}
.sac-dot-blink {
  width: 7px; height: 7px;
  background: #94a3b8;
  border-radius: 50%;
  animation: sac-blink 1.2s infinite;
}
.sac-dot-blink:nth-child(2) { animation-delay: 0.2s; }
.sac-dot-blink:nth-child(3) { animation-delay: 0.4s; }
@keyframes sac-blink {
  0%,80%,100% { transform: scale(1);   opacity: 0.5; }
  40%          { transform: scale(1.3); opacity: 1; }
}

/* ══════════════
   INPUT AREA
   ══════════════ */
.sac-input-area {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 14px;
  background: white;
  border-top: 1px solid #e8edf8;
  flex-shrink: 0;
}
#sac-input {
  flex: 1;
  border: 1.5px solid #dde4f5;
  border-radius: 22px;
  padding: 9px 14px;
  /* Prevent iOS auto-zoom (min 16px) */
  font-size: 16px;
  font-family: var(--sac-font);
  resize: none;
  outline: none;
  min-height: 40px;
  max-height: 110px;
  overflow-y: auto;
  color: var(--sac-text);
  line-height: 1.4;
  transition: border-color 0.2s;
  -webkit-appearance: none;
  background: white;
  box-sizing: border-box;
}
#sac-input:focus { border-color: var(--sac-primary); }
#sac-input::placeholder { color: #aab2cc; }

#sac-send {
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  aspect-ratio: 1 / 1;
  border-radius: 50%;
  background: var(--sac-secondary);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  flex-grow: 0;
  transition: filter 0.2s, transform 0.2s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  padding: 0;
  box-sizing: border-box;
}
#sac-send:hover  { filter: brightness(0.88); transform: scale(1.06); }
#sac-send:active { transform: scale(0.93); }
#sac-send svg {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  display: block;
}
#sac-send:disabled { opacity: 0.45; cursor: default; transform: none; }

/* Powered by */
.sac-powered {
  text-align: center;
  font-size: 10px;
  color: #b0b8d0;
  padding: 5px 0 7px;
  background: white;
  flex-shrink: 0;
}

/* ══════════════════════════════
   TABLET  (481px – 768px)
   ══════════════════════════════ */
@media (max-width: 768px) and (min-width: 481px) {
  #sac-window {
    width: 340px;
    max-height: 520px;
  }
}

/* ══════════════════════════════
   MOBILE  (≤ 480px)
   Full-screen chat sheet style
   ══════════════════════════════ */
@media (max-width: 480px) {

  /* Toggle: smaller, lower-right, above home bar */
  #sac-toggle {
    width:  54px;
    height: 54px;
    right:  calc(16px + var(--sac-safe-right));
    bottom: calc(20px + var(--sac-safe-bottom));
  }

  /* Window: full-width sheet that slides up */
  #sac-window {
    /* Full width with small gutters */
    left:   0;
    right:  0;
    bottom: 0;
    width:  100%;
    /* Max ~85% of screen height so page peeking from top */
    max-height: 88vh;
    max-height: 88dvh;     /* dynamic viewport height (Chrome 108+) */
    min-height: 320px;

    /* Flat bottom, rounded top */
    border-radius: var(--sac-radius) var(--sac-radius) 0 0;

    /* Override transform origin for bottom-center slide-up */
    transform-origin: bottom center;
    transform: translateY(100%);
    transition: transform 0.35s cubic-bezier(0.25, 0.8, 0.25, 1),
                opacity   0.2s ease;

    /* Account for iPhone home bar */
    padding-bottom: var(--sac-safe-bottom);
  }
  #sac-window.sac-open {
    transform: translateY(0);
    opacity: 1;
  }

  /* Handle bar above header (visual cue for bottom sheet) */
  #sac-window::before {
    content: '';
    display: block;
    width: 36px;
    height: 4px;
    background: rgba(255,255,255,0.4);
    border-radius: 2px;
    margin: 8px auto 0;
    flex-shrink: 0;
  }

  /* Bigger header on mobile */
  .sac-header {
    padding: 12px 16px 14px;
  }
  .sac-header-avatar {
    width: 40px;
    height: 40px;
  }
  .sac-header-name { font-size: 16px; }

  /* Messages area: fill remaining space */
  .sac-messages {
    flex: 1;
    padding: 14px 12px;
  }

  /* Bubbles: wider on mobile */
  .sac-bubble { max-width: 88%; font-size: 14px; }
  .sac-bubble-avatar { width: 28px; height: 28px; }

  /* Input area: slightly taller tap targets */
  .sac-input-area {
    padding: 10px 12px;
    /* Stays above keyboard — handled by the browser focus scroll */
  }
  #sac-input {
    font-size: 16px; /* MUST stay 16px — prevents iOS zoom-in */
    padding: 10px 14px;
    min-height: 44px;
  }
  #sac-send {
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    aspect-ratio: 1 / 1;
    border-radius: 50%;
    flex-shrink: 0;
    flex-grow: 0;
  }

  /* Close button: bigger tap target */
  .sac-close-btn {
    min-width: 40px;
    min-height: 40px;
  }
}

/* ════════════════════════════════════
   VERY SMALL PHONES  (≤ 360px)
   e.g. iPhone SE 1st gen
   ════════════════════════════════════ */
@media (max-width: 360px) {
  #sac-window {
    max-height: 92vh;
    max-height: 92dvh;
  }
  .sac-bubble { font-size: 13.5px; }
  .sac-header-name { font-size: 14px; }
}

/* ════════════════════════════════════
   LANDSCAPE MOBILE
   Shorter height, don't cover whole screen
   ════════════════════════════════════ */
@media (max-height: 500px) and (max-width: 900px) {
  #sac-window {
    max-height: 96vh;
    max-height: 96dvh;
  }
  .sac-header {
    padding: 8px 14px;
  }
  .sac-header-avatar { width: 34px; height: 34px; }
  .sac-messages { padding: 10px 12px; gap: 8px; }
  .sac-bubble { padding: 8px 12px; font-size: 13px; }
  .sac-input-area { padding: 8px 12px; }
}

/* ════════════════════════════════════
   REDUCED MOTION
   ════════════════════════════════════ */
@media (prefers-reduced-motion: reduce) {
  #sac-window,
  #sac-toggle,
  .sac-bubble-row { transition: none !important; animation: none !important; }
  .sac-dot-blink  { animation: none !important; opacity: 0.5; }
}
