/* =============================================================
   Proper Public Chat Widget
   Anonymous sales chat for proginter.com.
   Self-contained - uses Super Quantum 2500 tokens from style.css.
   RTL-safe via CSS logical properties.
   ============================================================= */

/* Root container - injected by JS on every page */
.proper-chat {
    position: fixed;
    inset-inline-end: var(--spacing-md, 2rem);
    inset-block-end: var(--spacing-md, 2rem);
    z-index: 10001; /* above mobile nav (9999) and cookie banner */
    font-family: var(--font-secondary, "Exo 2", system-ui, sans-serif);
    color: var(--text-primary, #ffffff);
    /* Sized to its content; the FAB defines the box when closed. */
    width: auto;
    height: auto;
    line-height: normal;
}

/* ===== FAB (closed state) ===== */
.proper-chat__fab {
    position: relative;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    border: none;
    background: linear-gradient(135deg, #00ff88, #0099ff);
    color: #0a0a0f;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 10px 28px rgba(0, 255, 136, 0.35), 0 4px 12px rgba(0, 0, 0, 0.4);
    transition: transform 0.18s ease, box-shadow 0.18s ease;
    padding: 0;
    overflow: visible;
}

.proper-chat__fab:hover {
    transform: translateY(-2px) scale(1.04);
    box-shadow: 0 14px 36px rgba(0, 255, 136, 0.5), 0 6px 16px rgba(0, 0, 0, 0.5);
}

.proper-chat__fab:focus-visible {
    outline: 3px solid var(--quantum-primary, #00ff88);
    outline-offset: 4px;
}

.proper-chat__fab:active { transform: scale(0.95); }

.proper-chat__fab svg {
    width: 26px;
    height: 26px;
    stroke-width: 2.2;
}

/* Pulse halo - first 12 seconds only, then stops being attention-grabbing.
   Drops entirely once the visitor has engaged (is-engaged class added on first open). */
.proper-chat__fab::after {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: 50%;
    box-shadow: 0 0 0 0 rgba(0, 255, 136, 0.45);
    animation: proper-chat-pulse 2.4s ease-out 0s 5;
    pointer-events: none;
}

.proper-chat__fab.is-engaged::after { animation: none; display: none; }

@keyframes proper-chat-pulse {
    0%   { box-shadow: 0 0 0 0 rgba(0, 255, 136, 0.45); }
    70%  { box-shadow: 0 0 0 18px rgba(0, 255, 136, 0); }
    100% { box-shadow: 0 0 0 0 rgba(0, 255, 136, 0); }
}

@media (prefers-reduced-motion: reduce) {
    .proper-chat__fab::after { animation: none; display: none; }
}

/* ===== Panel (open state) ===== */
.proper-chat__panel {
    position: fixed;
    inset-inline-end: var(--spacing-md, 2rem);
    inset-block-end: var(--spacing-md, 2rem);
    width: 400px;
    height: 600px;
    max-height: calc(100vh - 4rem);
    /* Reset the global `section { padding: var(--spacing-2xl) 0 }` rule
       from style.css - without this, the panel inherits 96px top + 96px
       bottom of padding, which strands the log in a tiny middle band. */
    padding: 0;
    border-radius: 18px;
    background: #14142b;
    border: 1px solid rgba(0, 255, 136, 0.32);
    box-shadow: 0 28px 72px rgba(0, 0, 0, 0.6), 0 10px 28px rgba(0, 255, 136, 0.18);
    z-index: 10002;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transform-origin: bottom right;
    animation: proper-chat-pop 0.22s ease-out;
}

[dir="rtl"] .proper-chat__panel { transform-origin: bottom left; }

@keyframes proper-chat-pop {
    from { opacity: 0; transform: translateY(8px) scale(0.96); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}

.proper-chat[data-open="false"] .proper-chat__panel { display: none; }
.proper-chat[data-open="true"] .proper-chat__fab { display: none; }

/* ===== Panel header ===== */
.proper-chat__header {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    padding: 0.55rem 0.85rem;
    border-block-end: 1px solid rgba(255, 255, 255, 0.08);
    background: linear-gradient(180deg, rgba(0, 255, 136, 0.07), rgba(0, 153, 255, 0.02));
    flex-shrink: 0;
}

.proper-chat__header-icon {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: linear-gradient(135deg, #00ff88, #0099ff);
    color: #0a0a0f;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.proper-chat__header-icon svg { width: 18px; height: 18px; stroke-width: 2.2; }

.proper-chat__header-text { flex: 1; min-width: 0; line-height: 1.1; }

.proper-chat__header-title {
    font-family: var(--font-primary, "Orbitron", sans-serif);
    font-size: 0.92rem;
    font-weight: 700;
    line-height: 1.1;
    color: #ffffff;
    letter-spacing: 0.4px;
}

.proper-chat__header-subtitle {
    font-size: 0.7rem;
    color: var(--text-muted, #8a8aa5);
    margin-block-start: 1px;
    letter-spacing: 0.2px;
    line-height: 1.1;
}

.proper-chat__close {
    background: transparent;
    border: none;
    color: var(--text-secondary, #b8b8d1);
    cursor: pointer;
    padding: 6px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.18s ease, color 0.18s ease;
}

.proper-chat__close:hover {
    background: rgba(255, 255, 255, 0.08);
    color: #ffffff;
}

.proper-chat__close:focus-visible {
    outline: 2px solid var(--quantum-primary, #00ff88);
    outline-offset: 2px;
}

.proper-chat__close svg { width: 18px; height: 18px; }

/* ===== Message log (scrollable) ===== */
.proper-chat__log {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    /* Disable browser scroll-anchoring - it fights our programmatic
       scrollToBottom by trying to "preserve" the visible viewport when
       a streaming bubble grows, which strands the visitor at the start
       of the message. */
    overflow-anchor: none;
    padding: 0.5rem 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    scrollbar-width: thin;
    scrollbar-color: rgba(0, 255, 136, 0.35) transparent;
    background:
        radial-gradient(circle at 100% 0%, rgba(0, 153, 255, 0.05), transparent 60%),
        radial-gradient(circle at 0% 100%, rgba(0, 255, 136, 0.04), transparent 60%);
}

/* When the conversation is short, push messages to the bottom of the log
   so there's no awkward empty space below the last bubble. Standard chat
   pattern - newest messages sit just above the input row. */
.proper-chat__log > *:first-child { margin-block-start: auto; }

.proper-chat__log::-webkit-scrollbar { width: 6px; }
.proper-chat__log::-webkit-scrollbar-thumb {
    background: rgba(0, 255, 136, 0.35);
    border-radius: 3px;
}

/* ===== Message bubble ===== */
.proper-chat__msg {
    max-width: 92%;
    border-radius: 14px;
    font-size: 0.92rem;
    line-height: 1.5;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    animation: proper-chat-msg-in 0.18s ease-out;
}

@keyframes proper-chat-msg-in {
    from { opacity: 0; transform: translateY(4px); }
    to   { opacity: 1; transform: translateY(0); }
}

.proper-chat__msg-body {
    padding: 0.5rem 0.8rem;
    color: #ffffff;
}

/* Markdown elements inside a bubble */
.proper-chat__msg-body > *:first-child { margin-block-start: 0; }
.proper-chat__msg-body > *:last-child { margin-block-end: 0; }

.proper-chat__para {
    margin: 0 0 0.4rem;
    line-height: 1.5;
}

.proper-chat__heading {
    font-family: var(--font-primary, "Orbitron", sans-serif);
    margin: 0.45rem 0 0.3rem;
    font-weight: 700;
    color: #ffffff;
    line-height: 1.2;
}

.proper-chat__msg-body h4.proper-chat__heading { font-size: 1rem; }
.proper-chat__msg-body h5.proper-chat__heading { font-size: 0.95rem; }
.proper-chat__msg-body h6.proper-chat__heading { font-size: 0.9rem; opacity: 0.95; }

.proper-chat__list {
    margin: 0.3rem 0 0.4rem;
    padding-inline-start: 1.15rem;
}

.proper-chat__list li { margin: 0.15rem 0; }

.proper-chat__msg-body strong { color: #ffffff; font-weight: 600; }
.proper-chat__msg-body em { color: var(--text-secondary, #b8b8d1); font-style: italic; }

.proper-chat__code {
    background: rgba(0, 0, 0, 0.4);
    border: 1px solid rgba(255, 255, 255, 0.08);
    padding: 1px 6px;
    border-radius: 5px;
    font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
    font-size: 0.82em;
    color: #00ff88;
}

.proper-chat__msg-body a {
    color: #5ad9ff;
    text-decoration: underline;
    text-underline-offset: 2px;
    text-decoration-thickness: 1px;
}

.proper-chat__msg-body a:hover { color: #8ce7ff; text-decoration-thickness: 2px; }

/* Bubble variants */
.proper-chat__msg--assistant {
    align-self: flex-start;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-start-start-radius: 4px;
}

.proper-chat__msg--user {
    align-self: flex-end;
    background: linear-gradient(135deg, rgba(0, 255, 136, 0.18), rgba(0, 153, 255, 0.18));
    border: 1px solid rgba(0, 255, 136, 0.35);
    border-end-end-radius: 4px;
}

.proper-chat__msg--error {
    align-self: stretch;
    padding: 0.5rem 0.85rem;
    background: rgba(255, 0, 102, 0.08);
    border: 1px solid rgba(255, 0, 102, 0.35);
    color: #ffb0c4;
    text-align: center;
    font-size: 0.85rem;
    max-width: none;
}

/* ===== Thinking + tool-status indicators ===== */
.proper-chat__thinking {
    align-self: flex-start;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 0.5rem 0.85rem;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: 14px;
    border-start-start-radius: 4px;
}

.proper-chat__dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--quantum-primary, #00ff88);
    opacity: 0.4;
    animation: proper-chat-bounce 1.1s ease-in-out infinite;
}
.proper-chat__dot:nth-child(2) { animation-delay: 0.15s; }
.proper-chat__dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes proper-chat-bounce {
    0%, 100% { opacity: 0.35; transform: translateY(0); }
    50%      { opacity: 1; transform: translateY(-3px); }
}

.proper-chat__tool-status {
    align-self: flex-start;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 0.4rem 0.75rem;
    background: rgba(0, 153, 255, 0.08);
    border: 1px solid rgba(0, 153, 255, 0.35);
    border-radius: 12px;
    color: #9ed8ff;
    font-size: 0.82rem;
    font-style: italic;
}

.proper-chat__tool-status::before {
    content: "";
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #5ad9ff;
    box-shadow: 0 0 6px rgba(90, 217, 255, 0.6);
    animation: proper-chat-blink 1.1s ease-in-out infinite;
}

@keyframes proper-chat-blink {
    0%, 100% { opacity: 0.35; }
    50%      { opacity: 1; }
}

/* ===== Starter prompts ===== */
.proper-chat__starters {
    display: flex;
    flex-direction: column;
    gap: 0.45rem;
    margin-block-start: 0.25rem;
}

.proper-chat__starter {
    background: rgba(0, 255, 136, 0.05);
    border: 1px solid rgba(0, 255, 136, 0.28);
    color: #ffffff;
    padding: 0.5rem 0.85rem;
    border-radius: 12px;
    cursor: pointer;
    font-size: 0.86rem;
    text-align: start;
    font-family: inherit;
    transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

.proper-chat__starter:hover {
    background: rgba(0, 255, 136, 0.13);
    border-color: rgba(0, 255, 136, 0.55);
    transform: translateY(-1px);
}

.proper-chat__starter:active { transform: scale(0.98); }
.proper-chat__starter:focus-visible {
    outline: 2px solid var(--quantum-primary, #00ff88);
    outline-offset: 2px;
}

/* ===== Input row ===== */
.proper-chat__input-row {
    display: flex;
    gap: 0.5rem;
    padding: 0.55rem 0.7rem;
    border-block-start: 1px solid rgba(255, 255, 255, 0.08);
    background: rgba(0, 0, 0, 0.22);
    align-items: flex-end;
    flex-shrink: 0;
}

.proper-chat__input {
    flex: 1;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    padding: 0.55rem 0.8rem;
    color: #ffffff;
    font-family: inherit;
    font-size: 0.92rem;
    line-height: 1.4;
    resize: none;
    max-height: 120px;
    min-height: 38px;
    transition: border-color 0.15s ease, background 0.15s ease;
}

.proper-chat__input::placeholder { color: var(--text-muted, #8a8aa5); }

.proper-chat__input:focus {
    outline: none;
    border-color: rgba(0, 255, 136, 0.55);
    background: rgba(255, 255, 255, 0.08);
}

.proper-chat__input:disabled { opacity: 0.55; cursor: not-allowed; }

.proper-chat__send {
    background: linear-gradient(135deg, #00ff88, #0099ff);
    color: #0a0a0f;
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: transform 0.12s ease, opacity 0.18s ease, box-shadow 0.18s ease;
    box-shadow: 0 4px 14px rgba(0, 255, 136, 0.3);
}

.proper-chat__send:hover:not(:disabled) {
    transform: scale(1.06);
    box-shadow: 0 6px 18px rgba(0, 255, 136, 0.45);
}

.proper-chat__send:active:not(:disabled) { transform: scale(0.95); }

.proper-chat__send:focus-visible {
    outline: 2px solid var(--quantum-primary, #00ff88);
    outline-offset: 2px;
}

.proper-chat__send:disabled { opacity: 0.4; cursor: not-allowed; box-shadow: none; }
.proper-chat__send svg { width: 18px; height: 18px; stroke-width: 2.2; }

/* Send arrow flips in RTL */
[dir="rtl"] .proper-chat__send svg { transform: scaleX(-1); }

/* ===== Responsive breakpoints ===== */

/* Tablet: shrink panel + tighter padding */
@media (max-width: 768px) {
    .proper-chat {
        inset-inline-end: 1rem;
        inset-block-end: 1rem;
    }
    .proper-chat__fab { width: 56px; height: 56px; }
    .proper-chat__fab svg { width: 24px; height: 24px; }
    .proper-chat__panel {
        width: calc(100vw - 2rem);
        max-width: 380px;
        height: 75vh;
        max-height: calc(100vh - 2rem);
    }
    .proper-chat__log { padding: 0.5rem 0.75rem; gap: 0.4rem; }
    .proper-chat__msg { font-size: 0.9rem; }
}

/* Small phone: full-screen takeover */
@media (max-width: 480px) {
    .proper-chat {
        inset-inline-end: 0.75rem;
        inset-block-end: 0.75rem;
    }
    .proper-chat__panel {
        position: fixed;
        inset: 0;
        width: 100vw;
        height: 100vh;
        height: 100dvh;
        max-width: none;
        max-height: 100dvh;
        border-radius: 0;
        border: none;
    }
    .proper-chat__header { padding: 0.6rem 0.85rem; }
    .proper-chat__header-icon { width: 32px; height: 32px; }
    .proper-chat__log { padding: 0.5rem 0.75rem; }
    .proper-chat__input-row { padding: 0.5rem 0.75rem env(safe-area-inset-bottom, 0.5rem); }
}

/* Tiny phones: keep usable */
@media (max-width: 360px) {
    .proper-chat__msg { font-size: 0.88rem; }
    .proper-chat__starter { font-size: 0.82rem; padding: 0.45rem 0.7rem; }
    .proper-chat__input { font-size: 0.9rem; }
}

/* Wide screens: panel can grow a bit */
@media (min-width: 1280px) {
    .proper-chat__panel { width: 420px; height: 640px; }
}

/* ===== RTL micro-fixes ===== */
[dir="rtl"] .proper-chat__list { padding-inline-start: 1.25rem; padding-inline-end: 0; }

/* Numbers/code stay LTR even when bubble is in an RTL document */
[dir="rtl"] .proper-chat__code { direction: ltr; display: inline-block; }
