/**
 * Kensington Hearing Assistant — Chatbot Widget Styles
 * Refined, accessible, and responsive.
 */

/* ─── Reset & Base ─────────────────────────────── */

.kh-chatbot,
.kh-chatbot *,
.kh-chatbot *::before,
.kh-chatbot *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

.kh-chatbot {
    --kh-radius: 16px;
    --kh-radius-sm: 10px;
    --kh-shadow: 0 8px 40px rgba(0, 0, 0, 0.12), 0 2px 8px rgba(0, 0, 0, 0.08);
    --kh-shadow-toggle: 0 4px 20px rgba(0, 0, 0, 0.18), 0 2px 6px rgba(0, 0, 0, 0.1);
    --kh-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    --kh-bg: #ffffff;
    --kh-bg-muted: #f6f8f7;
    --kh-text: #1a2e2b;
    --kh-text-muted: #5c706b;
    --kh-border: #e2e8e6;
    --kh-msg-user-bg: var(--kh-primary);
    --kh-msg-user-text: #ffffff;
    --kh-msg-bot-bg: var(--kh-bg-muted);
    --kh-msg-bot-text: var(--kh-text);

    font-family: var(--kh-font);
    font-size: 15px;
    line-height: 1.55;
    color: var(--kh-text);
    position: fixed;
    z-index: 999999;
}

/* ─── Positioning ──────────────────────────────── */

.kh-chatbot.kh-position-bottom-right {
    bottom: 20px;
    right: 20px;
}

.kh-chatbot.kh-position-bottom-left {
    bottom: 20px;
    left: 20px;
}

/* ─── Toggle Button ────────────────────────────── */

.kh-chatbot-toggle {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: var(--kh-primary);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--kh-shadow-toggle);
    position: relative;
    transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1), background 0.2s ease;
    color: #fff;
}

.kh-chatbot-toggle:hover {
    transform: scale(1.08);
    background: var(--kh-primary-dark);
}

.kh-chatbot-toggle:active {
    transform: scale(0.97);
}

.kh-toggle-icon {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.2s ease, transform 0.25s ease;
}

.kh-toggle-icon--close {
    opacity: 0;
    transform: rotate(-90deg) scale(0.6);
}

.kh-chatbot.is-open .kh-toggle-icon--chat {
    opacity: 0;
    transform: rotate(90deg) scale(0.6);
}

.kh-chatbot.is-open .kh-toggle-icon--close {
    opacity: 1;
    transform: rotate(0deg) scale(1);
}

/* Pulse animation on load */
.kh-toggle-pulse {
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: var(--kh-primary);
    opacity: 0;
    animation: kh-pulse 2.5s ease-out 1s 2;
}

@keyframes kh-pulse {
    0% { transform: scale(1); opacity: 0.4; }
    100% { transform: scale(1.6); opacity: 0; }
}

/* ─── Chat Panel ───────────────────────────────── */

.kh-chatbot-panel {
    position: absolute;
    bottom: 72px;
    width: 380px;
    max-height: 580px;
    height: calc(100vh - 120px);
    background: var(--kh-bg);
    border-radius: var(--kh-radius);
    box-shadow: var(--kh-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    transform: translateY(12px) scale(0.96);
    pointer-events: none;
    transition: opacity 0.25s ease, transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    border: 1px solid var(--kh-border);
}

.kh-position-bottom-right .kh-chatbot-panel {
    right: 0;
}

.kh-position-bottom-left .kh-chatbot-panel {
    left: 0;
}

.kh-chatbot.is-open .kh-chatbot-panel {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

/* ─── Header ───────────────────────────────────── */

.kh-chatbot-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    background: var(--kh-primary);
    color: #fff;
    flex-shrink: 0;
}

.kh-chatbot-header-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.kh-chatbot-avatar {
    width: 38px;
    height: 38px;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.18);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.kh-chatbot-title {
    font-size: 15px;
    font-weight: 650;
    letter-spacing: -0.01em;
}

.kh-chatbot-subtitle {
    font-size: 12px;
    opacity: 0.8;
    font-weight: 400;
}

.kh-chatbot-close {
    background: rgba(255, 255, 255, 0.15);
    border: none;
    color: #fff;
    width: 32px;
    height: 32px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}

.kh-chatbot-close:hover {
    background: rgba(255, 255, 255, 0.28);
}

/* ─── Messages ─────────────────────────────────── */

.kh-chatbot-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    scroll-behavior: smooth;
    overscroll-behavior: contain;
}

.kh-chatbot-messages::-webkit-scrollbar {
    width: 5px;
}

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

.kh-chatbot-messages::-webkit-scrollbar-thumb {
    background: var(--kh-border);
    border-radius: 10px;
}

.kh-msg {
    max-width: 88%;
    padding: 10px 14px;
    border-radius: var(--kh-radius-sm);
    font-size: 14px;
    line-height: 1.55;
    animation: kh-msg-in 0.25s ease-out;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

@keyframes kh-msg-in {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

.kh-msg--bot {
    align-self: flex-start;
    background: var(--kh-msg-bot-bg);
    color: var(--kh-msg-bot-text);
    border-bottom-left-radius: 4px;
}

.kh-msg--user {
    align-self: flex-end;
    background: var(--kh-msg-user-bg);
    color: var(--kh-msg-user-text);
    border-bottom-right-radius: 4px;
}

/* Markdown inside bot messages */
.kh-msg--bot p {
    margin: 0 0 8px;
}

.kh-msg--bot p:last-child {
    margin-bottom: 0;
}

.kh-msg--bot strong {
    font-weight: 650;
    color: var(--kh-primary-dark, var(--kh-text));
}

.kh-msg--bot ul,
.kh-msg--bot ol {
    margin: 6px 0;
    padding-left: 18px;
}

.kh-msg--bot li {
    margin-bottom: 3px;
}

.kh-msg--bot a {
    color: var(--kh-primary);
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 2px;
    font-weight: 500;
}

.kh-msg--bot a:hover {
    text-decoration-thickness: 2px;
}

.kh-msg--user a {
    color: #fff;
    text-decoration: underline;
}

/* Typing indicator */
.kh-typing {
    display: flex;
    gap: 5px;
    padding: 12px 16px;
    align-self: flex-start;
    background: var(--kh-msg-bot-bg);
    border-radius: var(--kh-radius-sm);
    border-bottom-left-radius: 4px;
}

.kh-typing-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--kh-text-muted);
    opacity: 0.4;
    animation: kh-typing-bounce 1.2s ease-in-out infinite;
}

.kh-typing-dot:nth-child(2) { animation-delay: 0.15s; }
.kh-typing-dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes kh-typing-bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-5px); opacity: 0.9; }
}

/* ─── Quick Questions ──────────────────────────── */

.kh-chatbot-quick {
    padding: 0 16px 10px;
    flex-shrink: 0;
}

.kh-quick-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--kh-text-muted);
    margin-bottom: 8px;
    font-weight: 600;
}

.kh-quick-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.kh-quick-btn {
    background: var(--kh-primary-light);
    color: var(--kh-primary-dark, var(--kh-primary));
    border: 1px solid var(--kh-primary-medium, var(--kh-primary-light));
    border-radius: 20px;
    padding: 6px 14px;
    font-size: 12.5px;
    font-family: var(--kh-font);
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, transform 0.1s;
    line-height: 1.4;
    font-weight: 500;
}

.kh-quick-btn:hover {
    background: var(--kh-primary-medium, var(--kh-primary-light));
    border-color: var(--kh-primary);
    transform: translateY(-1px);
}

.kh-quick-btn:active {
    transform: translateY(0);
}

/* ─── Input Area ───────────────────────────────── */

.kh-chatbot-input-wrap {
    border-top: 1px solid var(--kh-border);
    background: var(--kh-bg);
    flex-shrink: 0;
}

.kh-chatbot-input-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 12px 14px 8px;
}

.kh-chatbot-input {
    flex: 1;
    resize: none;
    border: 1.5px solid var(--kh-border);
    border-radius: var(--kh-radius-sm);
    padding: 9px 12px;
    font-size: 14px;
    font-family: var(--kh-font);
    line-height: 1.45;
    color: var(--kh-text);
    background: var(--kh-bg);
    outline: none;
    transition: border-color 0.15s;
    max-height: 90px;
    min-height: 38px;
}

.kh-chatbot-input::placeholder {
    color: var(--kh-text-muted);
    opacity: 0.7;
}

.kh-chatbot-input:focus {
    border-color: var(--kh-primary);
}

.kh-chatbot-send {
    width: 38px;
    height: 38px;
    border-radius: var(--kh-radius-sm);
    background: var(--kh-primary);
    color: #fff;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s, opacity 0.15s, transform 0.1s;
}

.kh-chatbot-send:disabled {
    opacity: 0.4;
    cursor: default;
}

.kh-chatbot-send:not(:disabled):hover {
    background: var(--kh-primary-dark);
}

.kh-chatbot-send:not(:disabled):active {
    transform: scale(0.94);
}

/* Footer — Booking CTA */
.kh-chatbot-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 6px 14px 12px;
}

.kh-chatbot-book-btn,
.kh-chatbot-phone-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: 12px;
    font-weight: 600;
    font-family: var(--kh-font);
    text-decoration: none;
    padding: 5px 12px;
    border-radius: 6px;
    transition: background 0.15s, color 0.15s;
    line-height: 1;
}

.kh-chatbot-book-btn {
    background: var(--kh-primary);
    color: #fff;
}

.kh-chatbot-book-btn:hover {
    background: var(--kh-primary-dark);
    color: #fff;
}

.kh-chatbot-phone-btn {
    color: var(--kh-primary);
    background: var(--kh-primary-light);
}

.kh-chatbot-phone-btn:hover {
    background: var(--kh-primary-medium, var(--kh-primary-light));
}

/* ─── Mobile Responsive ────────────────────────── */

@media (max-width: 480px) {
    .kh-chatbot-panel {
        width: calc(100vw - 20px);
        max-height: calc(100vh - 100px);
        height: calc(100vh - 100px);
        bottom: 68px;
        right: -10px !important;
        left: auto !important;
        border-radius: var(--kh-radius) var(--kh-radius) 0 0;
    }

    .kh-position-bottom-left .kh-chatbot-panel {
        left: -10px !important;
        right: auto !important;
    }

    .kh-chatbot-toggle {
        width: 54px;
        height: 54px;
    }
}

/* ─── Accessibility: Focus Styles ──────────────── */

.kh-chatbot-toggle:focus-visible,
.kh-chatbot-close:focus-visible,
.kh-chatbot-send:focus-visible,
.kh-quick-btn:focus-visible,
.kh-chatbot-input:focus-visible {
    outline: 2px solid var(--kh-primary);
    outline-offset: 2px;
}

/* ─── Reduced Motion ───────────────────────────── */

@media (prefers-reduced-motion: reduce) {
    .kh-chatbot-panel,
    .kh-msg,
    .kh-toggle-icon,
    .kh-chatbot-toggle {
        transition-duration: 0.01ms !important;
        animation-duration: 0.01ms !important;
    }
    .kh-toggle-pulse {
        display: none;
    }
}
