/*
 * Pembantu Maklumat Ko-Nelayan — Chat Widget
 * Scoped dengan prefix .konel-chat-* untuk elak konflik dengan
 * Bootstrap, main.css template, AOS, Swiper, GLightbox.
 *
 * Color tokens: #0F5996 (biru utama), #FFA040 (aksen), #0A1628 (dark)
 */

:root {
  --konel-chat-primary: #0F5996;
  --konel-chat-accent: #FFA040;
  --konel-chat-dark: #0A1628;
  --konel-chat-bg: #ffffff;
  --konel-chat-text: #1a2a3a;
  --konel-chat-muted: #6c757d;
  --konel-chat-border: #e3e7eb;
  --konel-chat-user-bubble: #e7f0f8;
  --konel-chat-bot-bubble: #f5f7fa;
  --konel-chat-shadow: 0 10px 40px rgba(15, 89, 150, 0.18);
  --konel-chat-radius: 12px;
}

/* ============================================================
   FLOATING BUTTON
   ============================================================ */
.konel-chat-fab {
  position: fixed;
  bottom: 12px;
  right: 12px;
  width: 150px;
  height: 150px;
  border-radius: 50%;
  background: transparent;
  color: #fff;
  border: none;
  cursor: pointer;
  z-index: 9999;
  padding: 0;
  overflow: visible;
  filter: drop-shadow(0 8px 24px rgba(15, 89, 150, 0.35));
  transition: transform 0.25s ease, filter 0.25s ease;
}

.konel-chat-fab:hover,
.konel-chat-fab:focus {
  transform: scale(1.06);
  filter: drop-shadow(0 12px 32px rgba(255, 160, 64, 0.45));
  outline: none;
}

.konel-chat-fab:focus-visible {
  outline: 3px solid rgba(255, 160, 64, 0.6);
  outline-offset: 4px;
}

.konel-chat-fab-avatar {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  overflow: visible;
  animation: konel-chat-fab-pulse 3.5s ease-in-out infinite;
}

.konel-chat-fab-avatar img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.konel-chat-fab-bubble {
  position: absolute;
  bottom: 8px;
  right: 4px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--konel-chat-accent);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  border: 2px solid #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
}

/* Close state — bulatan gelap untuk X icon */
.konel-chat-fab[aria-expanded="true"] {
  width: 56px;
  height: 56px;
  background: var(--konel-chat-dark);
  border-radius: 50%;
  filter: drop-shadow(0 6px 16px rgba(10, 22, 40, 0.4));
}

.konel-chat-fab .konel-chat-fab-icon-close {
  display: none;
  font-size: 22px;
}

.konel-chat-fab[aria-expanded="true"] .konel-chat-fab-icon-open {
  display: none;
}

.konel-chat-fab[aria-expanded="true"] .konel-chat-fab-icon-close {
  display: inline-flex;
}

@keyframes konel-chat-fab-pulse {
  0%, 100% { transform: scale(1) translateY(0); }
  50%      { transform: scale(1.02) translateY(-2px); }
}

@media (prefers-reduced-motion: reduce) {
  .konel-chat-fab-avatar { animation: none; }
}

@media (max-width: 991.98px) {
  .konel-chat-fab {
    bottom: 8px;
    right: 8px;
    width: 115px;
    height: 115px;
  }
  .konel-chat-fab[aria-expanded="true"] {
    width: 48px;
    height: 48px;
  }
  .konel-chat-fab-bubble {
    width: 28px;
    height: 28px;
    font-size: 13px;
    bottom: 4px;
    right: 0;
  }
}

/* ============================================================
   NUDGE — bubble ajakan atas kepala FAB (sekali per sesi)
   ============================================================ */
.konel-chat-nudge {
  position: absolute;
  bottom: 146px;
  right: 92px;
  width: max-content;
  max-width: min(200px, 70vw);
  background: #ffffff;
  color: var(--konel-chat-primary);
  font-family: "Poppins", "Roboto", system-ui, -apple-system, sans-serif;
  font-size: 13px;
  font-weight: 500;
  line-height: 1.35;
  padding: 9px 24px 9px 13px;
  border-radius: 16px;
  box-shadow: 0 6px 22px rgba(15, 89, 150, 0.25);
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease;
  z-index: 1;
}

.konel-chat-nudge.is-visible {
  opacity: 1;
  pointer-events: auto;
  animation: konel-chat-nudge-bob 3.2s ease-in-out infinite;
}

/* Ekor "thought bubble" — dua titik menuju kepala avatar (bawah-kanan) */
.konel-chat-nudge::before,
.konel-chat-nudge::after {
  content: "";
  position: absolute;
  background: #ffffff;
  border-radius: 50%;
  box-shadow: 0 4px 10px rgba(15, 89, 150, 0.18);
}
.konel-chat-nudge::after {
  bottom: -8px;
  right: 30px;
  width: 13px;
  height: 13px;
}
.konel-chat-nudge::before {
  bottom: -17px;
  right: 22px;
  width: 7px;
  height: 7px;
}

.konel-chat-nudge-text {
  display: block;
  transition: opacity 0.18s ease;
}

.konel-chat-nudge-close {
  position: absolute;
  top: 3px;
  right: 5px;
  width: 18px;
  height: 18px;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--konel-chat-muted);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  border-radius: 50%;
  transition: color 0.2s ease;
}
.konel-chat-nudge-close:hover,
.konel-chat-nudge-close:focus {
  color: var(--konel-chat-primary);
}

@keyframes konel-chat-nudge-bob {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-3px); }
}

@media (max-width: 991.98px) {
  .konel-chat-nudge {
    bottom: 112px;
    right: 60px;
    width: max-content;
    max-width: min(160px, 62vw);
    font-size: 12px;
  }
  .konel-chat-nudge::after { right: 24px; }
  .konel-chat-nudge::before { right: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  .konel-chat-nudge.is-visible { animation: none; }
}

/* ============================================================
   PANEL
   ============================================================ */
.konel-chat-panel {
  position: fixed;
  bottom: 24px;
  right: 20px;
  width: 380px;
  /* Atas: jangan tindih navbar (~100px). Bawah: clear FAB (~24px). */
  max-height: calc(100vh - 140px);
  background: var(--konel-chat-bg);
  border-radius: var(--konel-chat-radius);
  box-shadow: var(--konel-chat-shadow);
  z-index: 9998;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-family: "Poppins", "Roboto", system-ui, -apple-system, sans-serif;
  color: var(--konel-chat-text);
  opacity: 0;
  transform: translateY(10px) scale(0.98);
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.konel-chat-panel[data-open="true"] {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

/* Tablet — masih floating, lebih sempit */
@media (max-width: 767.98px) {
  .konel-chat-panel {
    width: calc(100vw - 32px);
    right: 16px;
    bottom: 16px;
    max-height: calc(100vh - 130px);
  }
}

/* Mobile kecil — full-bottom modal, kekal show navbar atas */
@media (max-width: 575.98px) {
  .konel-chat-panel {
    inset: auto 0 0 0;
    width: 100vw;
    height: 80vh;
    max-height: calc(100vh - 90px);
    border-radius: 16px 16px 0 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .konel-chat-panel,
  .konel-chat-fab {
    transition: none;
  }
}

/* ============================================================
   HEADER
   ============================================================ */
.konel-chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  background: linear-gradient(135deg, var(--konel-chat-primary) 0%, #1a6fb0 100%);
  color: #fff;
}

.konel-chat-header-info {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.konel-chat-header-text {
  min-width: 0;
}

.konel-chat-header-title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  line-height: 1.2;
  font-family: "Poppins", sans-serif;
  letter-spacing: 0.2px;
}

.konel-chat-header-subtitle {
  margin: 2px 0 0;
  font-size: 0.72rem;
  opacity: 0.9;
  font-weight: 400;
}

/* ============================================================
   AVATAR (PERSONA — NELIA)
   Tiada circle background — biar character free-floating dengan
   transparent PNG. Drop-shadow di image, bukan container.
   ============================================================ */
.konel-chat-avatar {
  object-fit: contain;
  flex-shrink: 0;
  display: block;
  background: transparent;
}

/* Crossfade transition untuk avatar rotation (setiap 10s) */
.konel-chat-avatar,
.konel-chat-fab-avatar img {
  transition: opacity 0.4s ease;
}

.konel-chat-avatar-fade,
.konel-chat-fab-avatar img.konel-chat-avatar-fade {
  opacity: 0;
}

@media (prefers-reduced-motion: reduce) {
  .konel-chat-avatar,
  .konel-chat-fab-avatar img,
  .konel-chat-avatar-fade,
  .konel-chat-fab-avatar img.konel-chat-avatar-fade {
    transition: none;
    opacity: 1;
  }
}

.konel-chat-avatar-header {
  width: 84px;
  height: 84px;
  filter: drop-shadow(0 3px 8px rgba(0, 0, 0, 0.25));
  animation: konel-chat-breathe 4.5s ease-in-out infinite;
  margin-top: -14px;
  margin-bottom: -14px;
}

.konel-chat-avatar-msg {
  width: 38px;
  height: 38px;
  align-self: flex-end;
  margin-bottom: -2px;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.15));
}

@keyframes konel-chat-breathe {
  0%, 100% { transform: scale(1) rotate(0deg); }
  50%      { transform: scale(1.04) rotate(-1.5deg); }
}

/* Subtle bounce bila panel buka */
.konel-chat-panel[data-open="true"] .konel-chat-avatar-header {
  animation: konel-chat-greet 0.6s ease-out, konel-chat-breathe 4.5s ease-in-out 0.6s infinite;
}

@keyframes konel-chat-greet {
  0%   { transform: scale(0.6) rotate(-15deg); opacity: 0.6; }
  60%  { transform: scale(1.1) rotate(5deg); opacity: 1; }
  100% { transform: scale(1) rotate(0); opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .konel-chat-avatar-header,
  .konel-chat-panel[data-open="true"] .konel-chat-avatar-header {
    animation: none !important;
  }
}

.konel-chat-close {
  background: transparent;
  border: none;
  color: #fff;
  font-size: 1.25rem;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  line-height: 1;
}

.konel-chat-close:hover,
.konel-chat-close:focus {
  background: rgba(255, 255, 255, 0.15);
  outline: 2px solid rgba(255, 255, 255, 0.4);
}

/* ============================================================
   MESSAGES
   ============================================================ */
.konel-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: #fafbfc;
}

/* Row wrapper untuk bot message + avatar */
.konel-chat-msg-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 92%;
}

.konel-chat-msg-row-bot {
  align-self: flex-start;
}

.konel-chat-msg {
  padding: 10px 14px;
  border-radius: var(--konel-chat-radius);
  font-size: 0.9rem;
  line-height: 1.5;
  word-wrap: break-word;
  flex: 1;
  min-width: 0;
}

.konel-chat-msg-bot {
  background: var(--konel-chat-bot-bubble);
  border-bottom-left-radius: 4px;
}

.konel-chat-msg-user {
  background: var(--konel-chat-user-bubble);
  align-self: flex-end;
  border-bottom-right-radius: 4px;
  color: var(--konel-chat-dark);
  max-width: 85%;
  padding: 10px 14px;
  border-radius: var(--konel-chat-radius);
  font-size: 0.9rem;
  line-height: 1.5;
  word-wrap: break-word;
}

.konel-chat-msg a {
  color: var(--konel-chat-primary);
  text-decoration: underline;
  font-weight: 500;
}

.konel-chat-msg a:hover {
  color: var(--konel-chat-accent);
}

.konel-chat-msg p {
  margin: 0 0 10px;
}

.konel-chat-msg p:last-child {
  margin-bottom: 0;
}

.konel-chat-msg strong {
  color: var(--konel-chat-primary);
  font-weight: 600;
}

.konel-chat-msg ul {
  margin: 6px 0 10px;
  padding-left: 18px;
}

.konel-chat-msg li {
  margin-bottom: 4px;
  line-height: 1.45;
}

.konel-chat-msg li:last-child {
  margin-bottom: 0;
}

/* Tajuk seksyen dalam jawapan (### markdown) — kemas & mudah scan */
.konel-chat-msg-h {
  display: block;
  font-weight: 600;
  color: var(--konel-chat-primary);
  font-size: 1.02em;
  margin: 14px 0 5px;
}

.konel-chat-msg-h:first-child {
  margin-top: 0;
}

/* Senarai bernombor (langkah/turutan) */
.konel-chat-msg ol {
  margin: 6px 0 10px;
  padding-left: 20px;
}

/* Markdown tables — kemas dan tersusun dalam chat bubble */
.konel-chat-msg-table {
  width: 100%;
  border-collapse: collapse;
  margin: 8px 0 10px;
  font-size: 0.82rem;
  background: #fff;
  border: 1px solid var(--konel-chat-border);
  border-radius: 6px;
  overflow: hidden;
}

.konel-chat-msg-table th,
.konel-chat-msg-table td {
  padding: 7px 10px;
  text-align: left;
  border-bottom: 1px solid var(--konel-chat-border);
  vertical-align: top;
  line-height: 1.4;
}

.konel-chat-msg-table th {
  background: var(--konel-chat-primary);
  color: #fff;
  font-weight: 600;
  font-size: 0.8rem;
  white-space: nowrap;
}

.konel-chat-msg-table tbody tr:last-child td {
  border-bottom: none;
}

.konel-chat-msg-table tbody tr:nth-child(even) {
  background: rgba(15, 89, 150, 0.04);
}

.konel-chat-msg-table strong {
  color: var(--konel-chat-primary);
}

/* Sembunyikan <p> kosong yang dihasilkan oleh table di dalam <p> wrapper */
.konel-chat-msg p:empty {
  display: none;
}

.konel-chat-typing {
  display: inline-flex;
  gap: 4px;
  padding: 12px 14px;
  background: var(--konel-chat-bot-bubble);
  border-radius: var(--konel-chat-radius);
}

.konel-chat-typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--konel-chat-muted);
  animation: konel-chat-bounce 1.2s infinite ease-in-out;
}

.konel-chat-typing span:nth-child(2) { animation-delay: 0.15s; }
.konel-chat-typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes konel-chat-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-6px); opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .konel-chat-typing span { animation: none; opacity: 0.6; }
}

/* ============================================================
   CONSENT BANNER
   ============================================================ */
.konel-chat-consent {
  padding: 12px 16px;
  background: #fff8ec;
  border-top: 1px solid var(--konel-chat-border);
  border-bottom: 1px solid var(--konel-chat-border);
  font-size: 0.8rem;
  color: var(--konel-chat-text);
}

.konel-chat-consent p {
  margin: 0 0 8px;
  line-height: 1.4;
}

.konel-chat-consent-actions {
  display: flex;
  gap: 8px;
}

.konel-chat-consent-actions button {
  flex: 1;
  padding: 6px 10px;
  border-radius: 6px;
  border: 1px solid var(--konel-chat-border);
  background: #fff;
  cursor: pointer;
  font-size: 0.8rem;
}

.konel-chat-consent-actions .konel-chat-consent-accept {
  background: var(--konel-chat-primary);
  color: #fff;
  border-color: var(--konel-chat-primary);
}

.konel-chat-consent-actions .konel-chat-consent-accept:hover {
  background: var(--konel-chat-accent);
  border-color: var(--konel-chat-accent);
}

/* ============================================================
   INPUT
   ============================================================ */
.konel-chat-input-wrap {
  border-top: 1px solid var(--konel-chat-border);
  padding: 12px 16px;
  display: flex;
  gap: 8px;
  align-items: flex-end;
  background: #fff;
}

.konel-chat-textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--konel-chat-border);
  border-radius: 8px;
  padding: 8px 12px;
  font-family: inherit;
  font-size: 0.9rem;
  line-height: 1.4;
  max-height: 100px;
  min-height: 40px;
  color: var(--konel-chat-text);
}

.konel-chat-textarea:focus {
  outline: none;
  border-color: var(--konel-chat-primary);
  box-shadow: 0 0 0 3px rgba(15, 89, 150, 0.15);
}

.konel-chat-send {
  background: var(--konel-chat-primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  width: 40px;
  height: 40px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  flex-shrink: 0;
}

.konel-chat-send:hover:not(:disabled),
.konel-chat-send:focus:not(:disabled) {
  background: var(--konel-chat-accent);
  outline: 2px solid rgba(255, 160, 64, 0.4);
}

.konel-chat-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ============================================================
   FOOTER DISCLAIMER
   ============================================================ */
.konel-chat-footer {
  padding: 8px 16px 10px;
  font-size: 0.7rem;
  color: var(--konel-chat-muted);
  text-align: center;
  background: #fafbfc;
  border-top: 1px solid var(--konel-chat-border);
  line-height: 1.4;
}

.konel-chat-footer a {
  color: var(--konel-chat-primary);
}

/* ============================================================
   ERROR STATE
   ============================================================ */
.konel-chat-error {
  background: #ffe9ec;
  color: #8b1538;
  border: 1px solid #f5c6cb;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 0.85rem;
  align-self: stretch;
}

/* ============================================================
   IKON CUACA GIF
   ============================================================ */
.konel-chat-weather-icon {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
  padding: 0;
  border-radius: 10px;
  background: var(--konel-chat-bot-bubble);
}

.konel-chat-weather-gif {
  width: 72px;
  height: 72px;
  object-fit: contain;
  mix-blend-mode: multiply;
}

.konel-chat-weather-icon-label {
  font-size: 0.8rem;
  color: var(--konel-chat-muted);
  font-weight: 500;
}

/* Jadual cuaca per-waktu (Pagi/Petang/Malam) */
.konel-chat-weather-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 12px;
  font-size: 0.82rem;
  border-radius: 10px;
  overflow: hidden;
}

.konel-chat-weather-table th {
  background: var(--konel-chat-primary, #0F5996);
  color: #fff;
  padding: 8px 10px;
  text-align: left;
  font-weight: 600;
  font-size: 0.78rem;
}

.konel-chat-weather-table td {
  padding: 8px 10px;
  border-bottom: 1px solid rgba(15, 89, 150, 0.1);
  vertical-align: middle;
}

.konel-chat-weather-table tbody tr:nth-child(even) {
  background: rgba(15, 89, 150, 0.03);
}

.konel-chat-weather-table tbody tr:nth-child(odd) {
  background: #fff;
}

.konel-chat-weather-table .konel-chat-weather-gif {
  width: 48px;
  height: 48px;
  mix-blend-mode: multiply;
}

/* ============================================================
   ACCESSIBILITY
   ============================================================ */
.konel-chat-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
