/**
 * Kundeservice Chat CSS
 * Hovedstyling for chat widget
 */

/* ========= CSS Variables ========= */
:root {
    --ks-primary: #007cba;
    --ks-primary-dark: #005a87;
    --ks-secondary: #f8f9fa;
    --ks-accent: #16a34a;
    --ks-text: #1f2937;
    --ks-text-light: #6b7280;
    --ks-border: #e5e7eb;
    --ks-background: #ffffff;
    --ks-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --ks-shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --ks-radius: 12px;
    --ks-radius-lg: 16px;
    --ks-transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* IMPORTANT: When chat is open on mobile, remove transforms on the widget to avoid iOS/WebKit fixed-position clipping inside transformed ancestors */
.ks-chat-open .kundeservice-chat-widget {
    transform: none !important;
    backface-visibility: visible !important;
    perspective: none !important;
}

/* ========= Chat Widget Container ========= */
.kundeservice-chat-widget {
    position: fixed;
    z-index: 2147483646; /* keep above site UI; container may be higher when open */
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    font-size: 14px;
    line-height: 1.5;
    color: var(--ks-text);
    color-scheme: light; /* force light controls inside widget across iOS WebKit */
    -webkit-text-size-adjust: 100%;
}

/* ========= Chat Toggle Button ========= */
.chat-toggle {
    position: relative;
    width: 60px;
    height: 60px;
    background: linear-gradient(135deg, var(--ks-primary), var(--ks-primary-dark));
    border: none;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--ks-shadow-lg);
    transition: var(--ks-transition);
    color: white;
    z-index: 2147483646; /* ensure the toggle button is tappable above everything */
    pointer-events: auto;
}

.chat-toggle:hover {
    transform: scale(1.05);
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
}

.chat-toggle svg {
    width: 24px;
    height: 24px;
}

.chat-badge {
    position: absolute;
    top: -5px;
    right: -5px;
    background: #ef4444;
    color: white;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 600;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.1); }
}

/* ========= Chat Container ========= */
.chat-container {
    position: absolute;
    width: 380px;
    height: 600px;
    background: #ffffff; /* hard-set light background to avoid iOS auto dark */
    border-radius: var(--ks-radius-lg);
    box-shadow: var(--ks-shadow-lg);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transform: translateY(20px);
    opacity: 0;
    transition: var(--ks-transition);
    pointer-events: none; /* not interactive while hidden */
}

.chat-container.open {
    transform: translateY(0);
    opacity: 1;
    display: flex !important; /* ensure flex layout when opened even if inline style was display:none */
    pointer-events: auto; /* interactive when open */
}

/* Temporarily disable animations to avoid visible jump on mobile when reparenting */
.chat-container.ks-no-anim {
    transition: none !important;
}

/* ========= Chat Header ========= */
.chat-header {
    background: linear-gradient(135deg, var(--ks-primary), var(--ks-primary-dark));
    color: white;
    padding: 16px 20px;
    border-radius: var(--ks-radius-lg) var(--ks-radius-lg) 0 0;
}

.chat-header-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.chat-header-title {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1;
}

.chat-avatar {
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(10px);
}

.chat-title-text h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    line-height: 1.2;
}

.chat-status-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    opacity: 0.9;
    margin-top: 2px;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #10b981;
    animation: pulse-dot 2s infinite;
}

.status-dot.online {
    background: #10b981;
}

.status-dot.offline {
    background: #6b7280;
}

.status-dot.connecting {
    background: #f59e0b;
    animation: pulse-dot 1s infinite;
}

.status-dot.escalated {
    background: #ef4444;
}

@keyframes pulse-dot {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.chat-header-controls {
    display: flex;
    align-items: center;
    gap: 8px;
}

.header-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: 8px;
    padding: 8px 12px;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    font-weight: 500;
    transition: var(--ks-transition);
    backdrop-filter: blur(10px);
}

.header-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: translateY(-1px);
}

.header-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.header-btn svg {
    width: 14px;
    height: 14px;
}

.chat-close {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: 8px;
    width: 32px;
    height: 32px;
    color: #ffffff; /* ensure icon is visible on desktop */
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--ks-transition);
    backdrop-filter: blur(10px);
}

/* Ensure close icon is visible and sized on desktop too */
.chat-close svg {
    display: block;
    width: 16px;
    height: 16px;
    pointer-events: none;
}

.chat-close:hover {
    background: rgba(239, 68, 68, 0.8);
    transform: scale(1.05);
}

/* ========= Chat Messages ========= */
.chat-messages {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    background: #f8f9fa; /* hard-set light */
    display: flex;
    flex-direction: column;
    gap: 16px;
    touch-action: pan-y; /* allow vertical scroll inside messages */
}

/* When the keyboard/input is fixed, keep messages visible above it */
@media (max-width: 768px) {
  /* Prevent dragging the whole chat container on mobile; allow only inner scroll */
  .chat-container { touch-action: none; }

  .ks-input-fixed .chat-messages {
      padding-bottom: calc(var(--ks-input-h, 80px) + 16px);
  }

  /* Fix the input bar to the bottom of the visible viewport while typing on mobile */
  .ks-input-fixed .chat-input-container {
      position: fixed !important;
      left: 0;
      right: 0;
      bottom: 0;
      z-index: 2147483647;
      background: #fff;
  }
}

.chat-messages::-webkit-scrollbar {
    width: 4px;
}

.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: var(--ks-border);
    border-radius: 2px;
}

.message {
    display: flex;
    flex-direction: column;
    max-width: 80%;
    animation: messageSlide 0.3s ease-out;
}

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

.bot-message {
    align-self: flex-start;
}

.user-message {
    align-self: flex-end;
}

.message-content {
    background: white;
    padding: 12px 16px;
    border-radius: var(--ks-radius);
    box-shadow: var(--ks-shadow);
    font-size: 14px;
    line-height: 1.5;
    word-wrap: break-word;
}

.bot-message .message-content {
    background: white;
    border-bottom-left-radius: 4px;
}

.user-message .message-content {
    background: var(--ks-primary);
    color: white;
    border-bottom-right-radius: 4px;
}

.message-time {
    font-size: 11px;
    color: var(--ks-text-light);
    margin-top: 4px;
    text-align: right;
}

.bot-message .message-time {
    text-align: left;
}

/* ========= Chat Input ========= */
.chat-input-container {
    background: white;
    border-top: 1px solid var(--ks-border);
    padding: 16px 20px;
}

.chat-input-wrapper {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--ks-secondary);
    border-radius: var(--ks-radius);
    padding: 4px;
}

#chat-input {
    flex: 1;
    border: none;
    background: transparent;
    padding: 12px 16px;
    font-size: 16px; /* Prevent iOS zoom on focus */
    outline: none;
    color: var(--ks-text);
}

#chat-input::placeholder {
    color: var(--ks-text-light);
}

#chat-send {
    background: var(--ks-primary);
    border: none;
    border-radius: 8px;
    width: 40px;
    height: 40px;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--ks-transition);
}

#chat-send:hover:not(:disabled) {
    background: var(--ks-primary-dark);
    transform: scale(1.05);
}

#chat-send:disabled {
    background: var(--ks-border);
    cursor: not-allowed;
    opacity: 0.5;
}

#chat-send svg {
    width: 18px;
    height: 18px;
}

.chat-status {
    text-align: center;
    font-size: 12px;
    color: var(--ks-text-light);
    margin-top: 8px;
    min-height: 16px;
}

/* ========= Position specific styles ========= */
.kundeservice-position-bottom-left {
    left: calc(20px + env(safe-area-inset-left, 0px));
    right: auto;
    bottom: calc(20px + env(safe-area-inset-bottom, 0px));
}

.kundeservice-position-bottom-right {
    right: calc(20px + env(safe-area-inset-right, 0px));
    left: auto;
    bottom: calc(20px + env(safe-area-inset-bottom, 0px));
}

.kundeservice-position-top-left {
    top: calc(20px + env(safe-area-inset-top, 0px));
    bottom: auto;
    left: calc(20px + env(safe-area-inset-left, 0px));
    right: auto;
}

.kundeservice-position-top-right {
    top: calc(20px + env(safe-area-inset-top, 0px));
    bottom: auto;
    right: calc(20px + env(safe-area-inset-right, 0px));
    left: auto;
}

/* ========= Loading States ========= */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 12px 16px;
    background: white;
    border-radius: var(--ks-radius);
    box-shadow: var(--ks-shadow);
}

/* Keep the typing indicator at the bottom of the list regardless of insert order */
.typing-message {
    order: 9999;
    align-self: flex-start;
}

.typing-dot {
    width: 8px;
    height: 8px;
    background: var(--ks-text-light);
    border-radius: 50%;
    animation: typing 1.4s infinite ease-in-out;
}

.typing-dot:nth-child(1) { animation-delay: -0.32s; }
.typing-dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes typing {
    0%, 80%, 100% { transform: scale(0.8); opacity: 0.5; }
    40% { transform: scale(1); opacity: 1; }
}

/* ========= Dark Mode Support ========= */
@media (prefers-color-scheme: dark) {
    /* Force light theme inside the chat widget to ensure consistent branding on iOS dark mode */
    .kundeservice-chat-widget {
        color-scheme: light;
    }
    .kundeservice-chat-widget .chat-messages {
        background: #f8f9fa;
    }
    .kundeservice-chat-widget .message-content {
        background: #ffffff;
        color: #1f2937;
    }
    .kundeservice-chat-widget .user-message .message-content {
        background: var(--ks-primary);
        color: #ffffff;
    }
    .kundeservice-chat-widget .chat-input-wrapper {
        background: #f8f9fa;
    }
}

/* ========= Accessibility ========= */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ========= Focus States ========= */
/* Limit these strong locks to mobile/tablet only */
@media (max-width: 768px) {
  .ks-chat-open {
      overflow: hidden !important; /* prevent background scroll on mobile */
      touch-action: none;
  }

  /* Freeze the page completely while chat is open (iOS bounce/scroll fixes) */
  html.ks-chat-open,
  body.ks-chat-open {
      height: 100% !important;
      overflow: hidden !important;
      position: fixed !important;
      inset: 0 !important;
      width: 100% !important;
      overscroll-behavior: none !important;
  }
}
.header-btn:focus,
.chat-close:focus,
#chat-send:focus {
    outline: 2px solid rgba(255, 255, 255, 0.5);
    outline-offset: 2px;
}

#chat-input:focus {
    outline: none;
    box-shadow: none;
}

/* Remove focus ring completely for this specific input (per request) */
#chat-input:focus-visible {
    outline: none !important;
    box-shadow: none !important;
}

/* Stronger specificity to suppress theme/UA focus styles */
.kundeservice-chat-widget #chat-input,
.kundeservice-chat-widget #chat-input:focus,
.kundeservice-chat-widget #chat-input:focus-visible,
.kundeservice-chat-widget .chat-input-wrapper:focus-within {
    border: none !important;
    border-color: transparent !important;
    outline: none !important;
    outline-color: transparent !important;
    box-shadow: none !important;
    -webkit-appearance: none;
    appearance: none;
}

/* ========= Print Styles ========= */
@media print {
    .kundeservice-chat-widget {
        display: none !important;
    }
}
