/* ============================================================
   chat.css — Bli Kaon Chat Widget
   Gaya RPG Balinese — selaras dengan ai-guide.css
   Versi: v7 Bli Kaon Edition
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Cinzel:wght@400;600;700&family=IM+Fell+English:ital@0;1&family=DM+Sans:wght@400;500;600&display=swap');

:root {
  --bk-parchment:   #F2E8D0;
  --bk-parchment2:  #E8D8B0;
  --bk-ink:         #2C1810;
  --bk-ink-faded:   #5C3A28;
  --bk-gold:        #D4AF37;
  --bk-gold-dark:   #A0820A;
  --bk-crimson:     #8B1A1A;
  --bk-ocean:       #1A3A5C;
  --bk-terracotta:  #C4693A;
  --bk-shadow:      0 8px 32px rgba(44, 24, 16, 0.45);
  --navbar-height:  70px;
}

/* ============================================================
   TOGGLE BUTTON
   ============================================================ */
.chat-toggle {
  position: fixed;
  bottom: 28px;
  right: 24px;
  width: 62px;
  height: 62px;
  border-radius: 50%;
  border: 3px solid var(--bk-gold);
  background: radial-gradient(circle, var(--bk-terracotta) 40%, #8B3A1A);
  color: var(--bk-gold);
  font-size: 1.4rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 0 0 4px rgba(212,175,55,0.15), var(--bk-shadow);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  z-index: 1100;
  outline: none;
  animation: bkFabPulse 3s ease-in-out infinite;
}

.chat-toggle:hover {
  transform: scale(1.08);
  box-shadow: 0 0 0 6px rgba(212,175,55,0.3), var(--bk-shadow);
}

.chat-toggle.active {
  animation: none;
  background: radial-gradient(circle, var(--bk-gold-dark) 40%, #6B5010);
  color: var(--bk-ink);
}

@keyframes bkFabPulse {
  0%, 100% { box-shadow: 0 0 0 4px rgba(212,175,55,0.15), var(--bk-shadow); }
  50%       { box-shadow: 0 0 0 10px rgba(212,175,55,0.05), var(--bk-shadow); }
}

/* Mobile: sembunyikan FAB saat chat terbuka */
@media (max-width: 768px) {
  .chat-toggle {
    bottom: 15px;
    right: 15px;
    width: 54px;
    height: 54px;
    font-size: 1.2rem;
  }
  .chat-toggle.active {
    opacity: 0 !important;
    pointer-events: none !important;
    transform: scale(0.5) !important;
    transition: opacity 0.2s, transform 0.2s;
  }
}

/* ============================================================
   CHAT HINT BUBBLE
   ============================================================ */
.chat-hint {
  position: fixed;
  bottom: 102px;
  right: 28px;
  background: linear-gradient(135deg, #FBF4E3, #EDE0C4);
  color: var(--bk-ink);
  padding: 10px 16px;
  border-radius: 14px 14px 2px 14px;
  font-family: 'IM Fell English', serif;
  font-size: 0.85rem;
  font-style: italic;
  border: 1px solid rgba(160,130,10,0.35);
  box-shadow: 0 6px 20px rgba(44,24,16,0.2);
  z-index: 1099;
  pointer-events: none;
  max-width: 210px;
  line-height: 1.35;
  animation: bkHintFloat 3s ease-in-out infinite, bkFadeIn 0.5s ease 2s both;
}

.chat-hint::after {
  content: '';
  position: absolute;
  bottom: -8px;
  right: 14px;
  left: auto;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 8px solid #EDE0C4;
}

.chat-hint.hidden {
  display: none;
}

@keyframes bkHintFloat {
  0%, 100% { transform: translateY(0); }
  50%       { transform: translateY(-4px); }
}

@keyframes bkFadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ============================================================
   BACKDROP
   ============================================================ */
.chat-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(26, 10, 4, 0.55);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
  z-index: 1040;
  pointer-events: none;
}

.chat-backdrop.open {
  opacity: 1;
  visibility: visible;
  pointer-events: all;
}

/* ============================================================
   CONTAINER — Desktop
   ============================================================ */
.chat-container {
  font-family: 'DM Sans', sans-serif;
  position: fixed;
  /* FIX: beri gap dari bawah agar tidak kehalang toggle btn */
  bottom: 104px;
  right: 24px;
  width: 360px;
  max-width: min(360px, 92vw);
  z-index: 1050;
  pointer-events: none;
}

/* ============================================================
   POPUP — Parchment Scroll Style
   ============================================================ */
.chat-popup {
  background:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.65' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.04'/%3E%3C/svg%3E"),
    linear-gradient(160deg, #F5EFE0 0%, #EDE0C4 35%, #E5D5AA 70%, #F0E8D0 100%);
  border-radius: 12px;
  border: 2px solid var(--bk-gold);
  box-shadow:
    inset 0 0 40px rgba(212,175,55,0.08),
    0 0 0 1px rgba(160,130,10,0.3),
    var(--bk-shadow);
  display: flex;
  flex-direction: column;
  height: 520px;
  max-height: calc(100vh - var(--navbar-height) - 118px);
  overflow: hidden;
  opacity: 0;
  transform: translateY(24px) scale(0.97);
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.35s cubic-bezier(0.2,0.8,0.2,1), transform 0.35s cubic-bezier(0.2,0.8,0.2,1), visibility 0.35s;
}

/* Corner ornament */
.chat-popup::before,
.chat-popup::after {
  content: '';
  position: absolute;
  width: 32px;
  height: 32px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cpath d='M2 2 L14 2 L2 14 Z' fill='none' stroke='%23D4AF37' stroke-width='1.5' opacity='0.6'/%3E%3Cpath d='M2 8 L8 2' fill='none' stroke='%23D4AF37' stroke-width='0.8' opacity='0.4'/%3E%3C/svg%3E");
  pointer-events: none;
  z-index: 10;
}
.chat-popup::before { top: 0; left: 0; }
.chat-popup::after  { bottom: 0; right: 0; transform: rotate(180deg); }

.chat-container.open .chat-popup {
  opacity: 1;
  transform: translateY(0) scale(1);
  visibility: visible;
  pointer-events: all;
}

/* ============================================================
   HEADER — Royal Crest
   ============================================================ */
.chat-header {
  background: linear-gradient(135deg, #1A0A04 0%, #2C1810 50%, #1A3A5C 100%);
  color: #fff;
  padding: 12px 16px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  min-height: 60px;
  border-bottom: 2px solid var(--bk-gold);
  position: relative;
  overflow: hidden;
}

/* Batik diagonal pattern overlay */
.chat-header::before {
  content: '';
  position: absolute;
  inset: 0;
  background-image: repeating-linear-gradient(
    45deg,
    rgba(212,175,55,0.04) 0px,
    rgba(212,175,55,0.04) 1px,
    transparent 1px,
    transparent 10px
  );
  pointer-events: none;
}

@media (max-width: 768px) {
  .chat-header {
    padding-top: max(12px, env(safe-area-inset-top));
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
  }
}

.chat-title {
  font-family: 'Cinzel', serif;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--bk-gold);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  text-shadow: 0 0 10px rgba(212,175,55,0.5);
}

.chat-status {
  font-family: 'IM Fell English', serif;
  font-style: italic;
  font-size: 0.7rem;
  color: rgba(212,175,55,0.6);
  margin-top: 2px;
}

/* Global utility — dipakai oleh auth buttons & guest banner */
.hidden { display: none !important; }

.header-auth-btn {
  background: rgba(212,175,55,0.15);
  color: var(--bk-gold);
  border: 1px solid rgba(212,175,55,0.4);
  padding: 3px 9px;
  border-radius: 4px;
  font-size: 0.65rem;
  font-weight: 700;
  cursor: pointer;
  margin-left: 8px;
  font-family: 'DM Sans', sans-serif;
  transition: background 0.2s;
}

.header-auth-btn:hover {
  background: rgba(212,175,55,0.3);
}

.header-auth-btn.logout {
  color: rgba(212,175,55,0.7);
}

.header-user-name {
  font-size: 0.65rem;
  color: var(--bk-gold);
  margin-left: 8px;
  font-weight: 500;
  max-width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: 'DM Sans', sans-serif;
}

.chat-close {
  background: none;
  border: 1px solid rgba(212,175,55,0.3);
  border-radius: 4px;
  color: rgba(212,175,55,0.7);
  width: 30px;
  height: 30px;
  min-width: 30px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
  transition: all 0.2s;
  flex-shrink: 0;
  position: relative;
  z-index: 1;
  -webkit-tap-highlight-color: transparent;
}

.chat-close:hover,
.chat-close:active {
  background: rgba(212,175,55,0.1);
  border-color: var(--bk-gold);
  color: var(--bk-gold);
}

/* ============================================================
   MESSAGES AREA
   ============================================================ */
.chat-messages {
  flex: 1;
  min-height: 0;
  padding: 14px 12px;
  overflow-y: auto;
  overflow-x: hidden;
  background: transparent;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.chat-messages::-webkit-scrollbar { width: 4px; }
.chat-messages::-webkit-scrollbar-track { background: transparent; }
.chat-messages::-webkit-scrollbar-thumb {
  background: rgba(160,130,10,0.35);
  border-radius: 4px;
}

/* ============================================================
   MESSAGES — Parchment Bubbles
   ============================================================ */
.message {
  margin-bottom: 0.9rem;
  display: flex;
  animation: bkMsgIn 0.25s ease;
}

@keyframes bkMsgIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.message.ai   { flex-direction: row; align-items: flex-end; }
.message.user { flex-direction: row-reverse; justify-content: flex-end; align-items: flex-end; }

/* AI bubble — parchment */
.message.ai .message-bubble {
  background: linear-gradient(135deg, #FBF4E3, #EFE2C2);
  border: 1px solid rgba(160,130,10,0.35);
  color: var(--bk-ink);
  border-radius: 14px 14px 14px 4px;
  box-shadow: 0 2px 8px rgba(44,24,16,0.12), inset 0 1px 0 rgba(255,255,255,0.6);
  font-family: 'IM Fell English', serif;
}

/* Dekorasi fleuron di ai bubble */
.message.ai .message-bubble::before {
  content: '❧ ';
  font-size: 0.7rem;
  color: var(--bk-gold-dark);
  opacity: 0.6;
}

/* User bubble — dark scroll */
.message.user .message-bubble {
  background: linear-gradient(135deg, #1A3A5C, #2C1810);
  border: 1px solid rgba(212,175,55,0.4);
  color: #F2E8D0;
  border-radius: 14px 14px 4px 14px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  font-family: 'IM Fell English', serif;
}

.message-bubble {
  max-width: 80%;
  padding: 0.85rem 1.1rem;
  font-size: 0.88rem;
  line-height: 1.55;
}

.message-time {
  font-size: 0.68rem;
  color: var(--bk-ink-faded);
  opacity: 0.6;
  margin: 0 6px 4px;
  white-space: nowrap;
  font-family: 'DM Sans', sans-serif;
}

.message.user .message-time {
  color: rgba(212,175,55,0.5);
}

/* ============================================================
   TYPING INDICATOR
   ============================================================ */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: linear-gradient(135deg, #FBF4E3, #EFE2C2);
  border: 1px solid rgba(160,130,10,0.3);
  border-radius: 14px 14px 14px 4px;
  width: fit-content;
  max-width: 75%;
  font-family: 'IM Fell English', serif;
  font-style: italic;
  font-size: 0.78rem;
  color: var(--bk-ink-faded);
}

.typing-dots { display: flex; gap: 4px; align-items: center; }
.typing-dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--bk-gold-dark);
  animation: bkTypeBounce 1.4s infinite;
  opacity: 0.6;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bkTypeBounce {
  0%, 60%, 100% { transform: scale(0.8); opacity: 0.4; }
  30%            { transform: scale(1.3); opacity: 1; }
}

/* ============================================================
   SUGGESTIONS BAR
   ============================================================ */
.suggestions {
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
  padding: 8px 12px;
  overflow-x: auto;
  overflow-y: hidden;
  background: rgba(242,232,208,0.6);
  border-top: 1px solid rgba(160,130,10,0.2);
  flex-shrink: 0;
  min-height: 50px;
  align-items: center;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
  scrollbar-width: none;
}
.suggestions::-webkit-scrollbar { display: none; }

.suggestion-btn {
  background: linear-gradient(135deg, #FBF4E3, #EDE0C0);
  border: 1px solid rgba(160,130,10,0.4);
  color: var(--bk-ink);
  border-radius: 20px;
  padding: 5px 12px;
  font-family: 'IM Fell English', serif;
  font-size: 0.78rem;
  cursor: pointer;
  transition: all 0.2s ease;
  white-space: nowrap;
  flex-shrink: 0;
  min-height: 32px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.suggestion-btn::before {
  content: '⚔ ';
  font-size: 0.65rem;
  opacity: 0.5;
}

.suggestion-btn:hover,
.suggestion-btn:active {
  background: linear-gradient(135deg, var(--bk-gold), #C8A020);
  border-color: var(--bk-gold-dark);
  color: #1A0A04;
  transform: translateY(-1px);
  box-shadow: 0 3px 8px rgba(160,130,10,0.25);
}

/* ============================================================
   INPUT AREA
   ============================================================ */
.chat-input-area {
  padding: 10px 12px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: linear-gradient(180deg, rgba(242,232,208,0.6), rgba(230,215,180,0.95));
  border-top: 1.5px solid rgba(160,130,10,0.3);
  display: flex;
  gap: 8px;
  align-items: flex-end;
  flex-shrink: 0;
  min-height: 62px;
  position: relative;
  z-index: 10;
}

.chat-input {
  flex: 1;
  border: 1.5px solid rgba(160,130,10,0.35);
  border-radius: 20px;
  padding: 8px 14px;
  font-size: 16px; /* 16px wajib: cegah zoom iOS */
  font-family: 'IM Fell English', serif;
  color: var(--bk-ink);
  background: rgba(255,252,240,0.92);
  outline: none;
  resize: none;
  max-height: 90px;
  min-height: 40px;
  line-height: 1.4;
  transition: border-color 0.2s, box-shadow 0.2s;
  -webkit-appearance: none;
  appearance: none;
}

.chat-input::placeholder {
  color: rgba(92,58,40,0.45);
  font-style: italic;
}

.chat-input:focus {
  border-color: var(--bk-gold-dark);
  box-shadow: 0 0 0 3px rgba(212,175,55,0.15);
  background: rgba(255,252,240,1);
}

/* Send button — selalu visible, tidak kehalang apapun */
.chat-send {
  width: 40px;
  height: 40px;
  min-width: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, #2C1810, #1A3A5C);
  border: 2px solid var(--bk-gold);
  color: var(--bk-gold);
  font-size: 0.95rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  flex-shrink: 0;
  position: relative;
  z-index: 3;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.chat-send:hover:not(:disabled),
.chat-send:active:not(:disabled) {
  background: linear-gradient(135deg, var(--bk-gold-dark), #8B6914);
  color: #1A0A04;
  box-shadow: 0 0 12px rgba(212,175,55,0.4);
  transform: rotate(-15deg) scale(1.05);
}

.chat-send:disabled {
  opacity: 0.35;
  cursor: not-allowed;
  transform: none;
}

/* ============================================================
   GUEST BANNER
   ============================================================ */
.guest-limit-banner {
  background: linear-gradient(135deg, #FBF4E3, #EDE0C0);
  border: 1px solid rgba(160,130,10,0.4);
  color: var(--bk-ink);
  padding: 0.8rem 1rem;
  border-radius: 10px;
  margin: 0.5rem 0;
  text-align: center;
  box-shadow: 0 4px 12px rgba(44,24,16,0.15);
  animation: bkSlideDown 0.35s ease;
  font-family: 'IM Fell English', serif;
  font-size: 0.85rem;
}

.guest-limit-banner.hidden { display: none; }

@keyframes bkSlideDown {
  from { transform: translateY(-12px); opacity: 0; }
  to   { transform: translateY(0); opacity: 1; }
}

.auth-btn {
  background: linear-gradient(135deg, var(--bk-gold), #C8A020);
  color: var(--bk-ink);
  border: none;
  padding: 0.5rem 1.1rem;
  border-radius: 20px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s;
  margin-top: 0.5rem;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-family: 'DM Sans', sans-serif;
  font-size: 0.85rem;
  -webkit-tap-highlight-color: transparent;
}

.auth-btn:hover,
.auth-btn:active {
  background: linear-gradient(135deg, var(--bk-ocean), #0D1F33);
  color: var(--bk-gold);
  transform: translateY(-1px);
}

/* ============================================================
   MOBILE — Full Screen Slide-Up
   ============================================================ */
@media (max-width: 768px) {
  .chat-container {
    position: fixed !important;
    inset: 0 !important;
    width: 100% !important;
    height: 100% !important;
    pointer-events: none;
    z-index: 1050 !important;
    /* Reset desktop positioning */
    bottom: unset !important;
    right: unset !important;
  }

  /* Closed state: popup siap di bawah layar, siap slide up */
  .chat-popup {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 100% !important;
    max-height: 100dvh !important;
    height: 100dvh !important;
    border-radius: 0 !important;
    border-left: none !important;
    border-right: none !important;
    border-bottom: none !important;
    overflow: hidden !important;
    z-index: 1050 !important;
    /* Animasi: mulai dari bawah, opacity 0 */
    opacity: 0 !important;
    transform: translateY(100%) !important;
    visibility: hidden !important;
    pointer-events: none !important;
    transition:
      transform 0.38s cubic-bezier(0.22, 1, 0.36, 1),
      opacity 0.3s ease,
      visibility 0.38s !important;
  }

  /* Open state: slide naik smooth */
  .chat-container.open .chat-popup {
    opacity: 1 !important;
    transform: translateY(0) !important;
    visibility: visible !important;
    pointer-events: all !important;
  }

  /* Saat keyboard open: sembunyikan suggestions */
  .chat-container.open .chat-popup.keyboard-open .suggestions {
    display: none !important;
  }

  .chat-messages {
    flex: 1 !important;
    min-height: 0 !important;
    overflow-y: auto !important;
    padding: 12px 14px;
  }

  .suggestions { flex-shrink: 0 !important; }

  .chat-input-area {
    flex-shrink: 0 !important;
    padding: 10px 14px !important;
    padding-bottom: calc(max(35px, env(safe-area-inset-bottom)) + 10px) !important;
    min-height: 62px !important;
    position: sticky !important;
    bottom: 0 !important;
    z-index: 100;
    margin-top: auto !important;
  }

  .message-bubble {
    max-width: 85%;
    font-size: 0.9rem;
  }
}

/* ============================================================
   DESKTOP
   ============================================================ */
@media (min-width: 769px) {
  .chat-toggle  { bottom: 28px; right: 24px; }
  .chat-container { bottom: 104px; right: 24px; }
}

/* ============================================================
   ACCESSIBILITY
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  .chat-popup, .chat-toggle, .message, .chat-send, .suggestion-btn, .typing-dots span {
    transition: none !important;
    animation: none !important;
  }
}