/**
 * HES Chatbot — widget styles.
 *
 * Theming is driven entirely by the custom properties in the :root block
 * below. The accent colour is overridden at runtime by an inline style from
 * HES_Chatbot_Widget::build_inline_style(), so rebranding is a settings
 * change, not a stylesheet edit.
 *
 * All selectors are prefixed `hes-chat-` to avoid collisions with the theme
 * and with other chat plugins.
 */

:root {
    --hes-chat-accent: #0073aa;
    --hes-chat-accent-contrast: #ffffff;
    --hes-chat-surface: #ffffff;
    --hes-chat-surface-alt: #f4f6f8;
    --hes-chat-text: #1e2327;
    --hes-chat-text-muted: #646970;
    --hes-chat-border: #dcdfe3;
    --hes-chat-error-bg: #fcf0f1;
    --hes-chat-error-text: #8a2424;
    --hes-chat-radius: 12px;
    --hes-chat-shadow: 0 8px 30px rgba(0, 0, 0, 0.18);
    --hes-chat-z: 99999;
    /* Extra distance from the anchoring edges, set by the Horizontal/Vertical
       offset settings. Positive moves the widget away from the edge. */
    --hes-chat-offset-x: 0px;
    --hes-chat-offset-y: 0px;
    /* Mobile-specific equivalents (≤480px) — independent of the pair above,
       so a desktop-tuned offset doesn't get blindly reapplied on a much
       smaller screen. Default mirrors the desktop value until an admin
       explicitly sets a mobile-specific one — see class-widget.php. */
    --hes-chat-offset-x-mobile: 0px;
    --hes-chat-offset-y-mobile: 0px;
    --hes-chat-launcher-size: 56px;
}

/* ─── Container ───────────────────────────────────────────────────────────── */

#hes-chat-root {
    position: fixed;
    /* env() falls back to 0px on devices without a home indicator/notch, so
       this is safe everywhere with no separate fallback declaration. */
    bottom: calc(20px + var(--hes-chat-offset-y) + env(safe-area-inset-bottom, 0px));
    z-index: var(--hes-chat-z);
    display: flex;
    /* column-reverse keeps the launcher pinned to the bottom while the panel
       stacks above it, regardless of DOM order. */
    flex-direction: column-reverse;
    gap: 12px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    font-size: 15px;
    line-height: 1.5;
    color: var(--hes-chat-text);
}

#hes-chat-root[data-position="bottom-right"] {
    right: calc(20px + var(--hes-chat-offset-x) + env(safe-area-inset-right, 0px));
    align-items: flex-end;
}

#hes-chat-root[data-position="bottom-left"] {
    left: calc(20px + var(--hes-chat-offset-x) + env(safe-area-inset-left, 0px));
    align-items: flex-start;
}

/* ─── Launcher ────────────────────────────────────────────────────────────── */

#hes-chat-launcher {
    display: flex;
    align-items: center;
    justify-content: center;
    width: var(--hes-chat-launcher-size);
    height: var(--hes-chat-launcher-size);
    padding: 0;
    border: none;
    border-radius: 50%;
    background: var(--hes-chat-accent);
    color: var(--hes-chat-accent-contrast);
    box-shadow: var(--hes-chat-shadow);
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

/* Scales the SVG glyph with the button rather than a fixed pixel size — this
   overrides the width/height attributes assets/js/chatbot.js sets on the
   <svg>, since CSS wins over presentation attributes. Ratio matches the
   original 24px icon in a 56px button. */
#hes-chat-launcher svg {
    width: 43%;
    height: 43%;
}

#hes-chat-launcher:hover {
    transform: translateY(-2px);
}

#hes-chat-launcher:focus-visible {
    outline: 3px solid var(--hes-chat-accent);
    outline-offset: 3px;
}

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

#hes-chat-panel {
    display: flex;
    flex-direction: column;
    /* 420px rather than a typical 360: replies contain multi-column product
       tables, which are unreadable narrower. */
    width: 420px;
    max-width: calc(100vw - 40px);
    height: 520px;
    /* svh (small viewport height) always assumes the most restrictive case
       — a mobile browser's toolbar fully expanded, least space available —
       guaranteeing this fits within whatever's actually visible right now.
       Plain vh doesn't make that guarantee (it's what made part of the
       panel require a page scroll to reveal on some phones), and dvh
       (dynamic viewport height) sounded like the fix but isn't reliable on
       every browser — Samsung Internet's own extra browser chrome caused
       it to size the panel larger than what was actually visible, cutting
       it off. svh trades a little space on browsers where the toolbar
       happens to be hidden for never overflowing anywhere. Browsers that
       don't understand svh simply ignore this line and keep the vh
       fallback above — additive, not a replacement. */
    max-height: calc(100vh - 120px);
    max-height: calc(100svh - 120px);
    overflow: hidden;
    background: var(--hes-chat-surface);
    border: 1px solid var(--hes-chat-border);
    border-radius: var(--hes-chat-radius);
    box-shadow: var(--hes-chat-shadow);
}

#hes-chat-panel[hidden] {
    display: none;
}

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

#hes-chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 12px 16px;
    background: var(--hes-chat-accent);
    color: var(--hes-chat-accent-contrast);
    flex: 0 0 auto;
}

#hes-chat-title {
    font-weight: 600;
    font-size: 15px;
}

#hes-chat-close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    padding: 0;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: inherit;
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
}

#hes-chat-close:hover {
    background: rgba(255, 255, 255, 0.18);
}

#hes-chat-close:focus-visible {
    outline: 2px solid var(--hes-chat-accent-contrast);
    outline-offset: 1px;
}

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

#hes-chat-messages {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: var(--hes-chat-surface-alt);
}

.hes-chat-msg {
    max-width: 85%;
    padding: 9px 13px;
    border-radius: var(--hes-chat-radius);
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

/* Rendered Markdown replies get more room — tables need it. */
.hes-chat-msg.hes-chat-md {
    max-width: 95%;
    /* Structure comes from real elements now, so preserving source
       whitespace would double every gap. */
    white-space: normal;
}

.hes-chat-msg--user {
    align-self: flex-end;
    background: var(--hes-chat-accent);
    color: var(--hes-chat-accent-contrast);
    border-bottom-right-radius: 4px;
}

.hes-chat-msg--bot {
    align-self: flex-start;
    background: var(--hes-chat-surface);
    border: 1px solid var(--hes-chat-border);
    border-bottom-left-radius: 4px;
}

.hes-chat-msg--error {
    align-self: flex-start;
    background: var(--hes-chat-error-bg);
    color: var(--hes-chat-error-text);
    border: 1px solid currentColor;
}

/* ─── Rendered Markdown ───────────────────────────────────────────────────── */

/*
 * Styles for the DOM produced by markdown.js. Every selector is scoped under
 * .hes-chat-md so the theme's own headings and tables are untouched — and so
 * a theme's global rules are less likely to reach in here.
 */

.hes-chat-md > *:first-child { margin-top: 0; }
.hes-chat-md > *:last-child  { margin-bottom: 0; }

.hes-chat-md p {
    margin: 0 0 0.7em;
}

.hes-chat-md h1,
.hes-chat-md h2,
.hes-chat-md h3,
.hes-chat-md h4,
.hes-chat-md h5,
.hes-chat-md h6 {
    margin: 0.9em 0 0.45em;
    font-weight: 600;
    line-height: 1.3;
    color: inherit;
}

/* Replies routinely open with an h1; full-size headings would shout inside a
   chat bubble, so the scale is compressed. */
.hes-chat-md h1 { font-size: 1.2em; }
.hes-chat-md h2 { font-size: 1.12em; }
.hes-chat-md h3 { font-size: 1.05em; }
.hes-chat-md h4,
.hes-chat-md h5,
.hes-chat-md h6 { font-size: 1em; }

.hes-chat-md ul,
.hes-chat-md ol {
    margin: 0 0 0.7em;
    padding-left: 1.35em;
}

.hes-chat-md li {
    margin: 0.2em 0;
}

.hes-chat-md ul ul,
.hes-chat-md ul ol,
.hes-chat-md ol ul,
.hes-chat-md ol ol {
    margin: 0.2em 0 0;
}

.hes-chat-md a {
    color: var(--hes-chat-accent);
    text-decoration: underline;
}

.hes-chat-md a:focus-visible {
    outline: 2px solid var(--hes-chat-accent);
    outline-offset: 2px;
}

.hes-chat-md code {
    padding: 0.12em 0.35em;
    border-radius: 4px;
    background: var(--hes-chat-surface-alt);
    border: 1px solid var(--hes-chat-border);
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.88em;
}

.hes-chat-md pre {
    margin: 0 0 0.7em;
    padding: 10px 12px;
    overflow-x: auto;
    background: var(--hes-chat-surface-alt);
    border: 1px solid var(--hes-chat-border);
    border-radius: 8px;
}

.hes-chat-md pre code {
    padding: 0;
    border: none;
    background: none;
    white-space: pre;
}

.hes-chat-md blockquote {
    margin: 0 0 0.7em;
    padding: 0.15em 0 0.15em 0.8em;
    border-left: 3px solid var(--hes-chat-border);
    color: var(--hes-chat-text-muted);
}

.hes-chat-md hr {
    margin: 0.9em 0;
    border: none;
    border-top: 1px solid var(--hes-chat-border);
}

.hes-chat-md strong { font-weight: 600; }
.hes-chat-md em     { font-style: italic; }
.hes-chat-md del    { text-decoration: line-through; opacity: 0.75; }

/*
 * Tables scroll inside their own container rather than widening the bubble.
 * Without this a seven-column product table forces the whole panel to
 * overflow horizontally.
 */
.hes-chat-table-wrap {
    margin: 0 0 0.7em;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border: 1px solid var(--hes-chat-border);
    border-radius: 8px;
}

.hes-chat-md table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.92em;
    background: var(--hes-chat-surface);
}

.hes-chat-md th,
.hes-chat-md td {
    padding: 6px 10px;
    text-align: left;
    vertical-align: top;
    border-bottom: 1px solid var(--hes-chat-border);
    /* Keep columns readable rather than letting one wrap to a single letter. */
    min-width: 5.5em;
}

.hes-chat-md thead th {
    background: var(--hes-chat-surface-alt);
    font-weight: 600;
    white-space: nowrap;
}

.hes-chat-md tbody tr:last-child th,
.hes-chat-md tbody tr:last-child td {
    border-bottom: none;
}

/* ─── Feedback (thumbs up/down) ───────────────────────────────────────────── */

.hes-chat-feedback {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 6px;
}

.hes-chat-feedback-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 26px;
    height: 26px;
    padding: 0;
    border: 1px solid transparent;
    border-radius: 6px;
    background: transparent;
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
}

.hes-chat-feedback-btn:hover {
    border-color: var(--hes-chat-border);
    background: var(--hes-chat-surface-alt);
}

.hes-chat-feedback-btn:focus-visible {
    outline: 2px solid var(--hes-chat-accent);
    outline-offset: 1px;
}

.hes-chat-feedback-thanks {
    font-size: 0.85em;
    color: var(--hes-chat-text-muted);
}

.hes-chat-feedback-comment {
    width: 100%;
    margin: 4px 0;
    padding: 6px 8px;
    border: 1px solid var(--hes-chat-border);
    border-radius: 8px;
    background: var(--hes-chat-surface);
    color: var(--hes-chat-text);
    font: inherit;
    font-size: 0.85em;
    resize: vertical;
}

.hes-chat-feedback-form {
    display: flex;
    flex-direction: column;
    gap: 4px;
    width: 100%;
    margin-top: 2px;
}

.hes-chat-feedback-reason {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.85em;
    cursor: pointer;
}

.hes-chat-feedback-reason input[type="checkbox"] {
    margin: 0;
    accent-color: var(--hes-chat-accent);
}

.hes-chat-feedback-actions {
    display: flex;
    gap: 8px;
}

.hes-chat-feedback-send {
    padding: 4px 10px;
    border: none;
    border-radius: 999px;
    background: var(--hes-chat-accent);
    color: var(--hes-chat-accent-contrast);
    font-size: 0.8em;
    cursor: pointer;
}

.hes-chat-feedback-cancel {
    padding: 4px 10px;
    border: 1px solid var(--hes-chat-border);
    border-radius: 999px;
    background: transparent;
    color: var(--hes-chat-text-muted);
    font-size: 0.8em;
    cursor: pointer;
}

.hes-chat-feedback-cancel:hover {
    background: var(--hes-chat-surface-alt);
}

/* ─── Typing indicator ────────────────────────────────────────────────────── */

.hes-chat-typing {
    display: flex;
    gap: 4px;
    align-items: center;
    padding: 13px;
}

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

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

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

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

/* ─── Composer ────────────────────────────────────────────────────────────── */

#hes-chat-form {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px;
    border-top: 1px solid var(--hes-chat-border);
    background: var(--hes-chat-surface);
    flex: 0 0 auto;
}

#hes-chat-input {
    flex: 1 1 auto;
    min-width: 0;
    margin: 0;
    padding: 9px 12px;
    border: 1px solid var(--hes-chat-border);
    border-radius: 999px;
    background: var(--hes-chat-surface);
    color: var(--hes-chat-text);
    font: inherit;
}

#hes-chat-input:focus {
    outline: none;
    border-color: var(--hes-chat-accent);
    box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.06);
}

#hes-chat-input:disabled {
    background: var(--hes-chat-surface-alt);
    cursor: not-allowed;
}

#hes-chat-send {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    width: 38px;
    height: 38px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: var(--hes-chat-accent);
    color: var(--hes-chat-accent-contrast);
    cursor: pointer;
}

#hes-chat-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

#hes-chat-send:focus-visible {
    outline: 2px solid var(--hes-chat-accent);
    outline-offset: 2px;
}

/* ─── Disclaimer ──────────────────────────────────────────────────────────── */

#hes-chat-disclaimer {
    flex: 0 0 auto;
    margin: 0;
    padding: 6px 12px 10px;
    background: var(--hes-chat-surface);
    color: var(--hes-chat-text-muted);
    font-size: 0.75em;
    line-height: 1.4;
    text-align: center;
}

/* ─── Small screens ───────────────────────────────────────────────────────── */

@media (max-width: 480px) {
    /* Independent mobile offset — see --hes-chat-offset-x-mobile/-y-mobile
       above. Replaces the desktop offset at this breakpoint rather than
       stacking with it. */
    #hes-chat-root {
        bottom: calc(12px + var(--hes-chat-offset-y-mobile) + env(safe-area-inset-bottom, 0px));
    }

    #hes-chat-root[data-position="bottom-right"] {
        right: calc(12px + var(--hes-chat-offset-x-mobile) + env(safe-area-inset-right, 0px));
    }
    #hes-chat-root[data-position="bottom-left"] {
        left: calc(12px + var(--hes-chat-offset-x-mobile) + env(safe-area-inset-left, 0px));
    }

    #hes-chat-panel {
        width: calc(100vw - 24px);
        height: min(72vh, 560px);
        height: min(72svh, 560px);
    }

    /* Reclaim horizontal space on a phone — tables need every pixel. */
    .hes-chat-msg.hes-chat-md {
        max-width: 100%;
    }
}

/* ─── Reduced motion ──────────────────────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
    #hes-chat-launcher,
    .hes-chat-typing span {
        transition: none;
        animation: none;
    }
}
