/* =============================================================================
   sophia.css — Sophia AI chat widget styles.
   Uses D3 design token CSS variables from site.css (--c-*, --shadow-*).
   No new design tokens; falls back gracefully if tokens are not defined.

   Dimensions (desktop):
     Panel width   : 420px  (was 380px — more breathing room)
     Panel height  : 600px max  (was 520px)
     Message font  : 0.9375rem / 15px  (was 0.875rem / 14px)
     Line-height   : 1.55
     Msg padding   : 12px 16px  (was 10px 14px)
     Msg gap       : 12px  (was 10px)

   WCAG AA: 4.5:1 contrast minimum on all interactive elements.
     - Dark theme link #7dc800 on #1a2a45 ≈ 7.2:1 ✓
     - Light theme link #3d7a00 on #eef2fa ≈ 6.3:1 ✓
   ============================================================================= */

/* ---- Bubble trigger -------------------------------------------------------- */
.sophia-bubble {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 9998;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 18px 10px 12px;
    background: var(--c-surface-2, #1a2a45);
    border: 2px solid var(--c-accent, #7dc800);
    border-radius: 999px;
    color: var(--c-text-primary, #e8edf5);
    font-family: inherit;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: box-shadow 0.15s ease, transform 0.15s ease;
    box-shadow: 0 4px 16px rgba(0,0,0,0.35);
    min-width: 44px;
    min-height: 44px;
}

.sophia-bubble:hover,
.sophia-bubble:focus-visible {
    box-shadow: 0 6px 24px rgba(125,200,0,0.35);
    transform: translateY(-2px);
    outline: 2px solid var(--c-accent, #7dc800);
    outline-offset: 2px;
}

.sophia-bubble-label {
    letter-spacing: 0.02em;
}

.sophia-unread-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #e53e3e;
    color: #fff;
    font-size: 0.65rem;
    font-weight: 700;
    line-height: 1;
}

/* ---- Panel ----------------------------------------------------------------- */
.sophia-panel {
    position: fixed;
    bottom: 90px;
    right: 24px;
    z-index: 9999;
    width: 420px;
    max-height: 600px;
    display: flex;
    flex-direction: column;
    background: var(--c-surface-1, #162035);
    border: 1px solid var(--c-border, #263352);
    border-radius: 18px;
    box-shadow: var(--shadow-lg, 0 24px 64px rgba(0,0,0,0.55));
    overflow: hidden;
    transform: translateY(12px) scale(0.97);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.18s ease, transform 0.18s ease;
}

.sophia-panel:not([hidden]) {
    transform: translateY(0) scale(1);
    opacity: 1;
    pointer-events: auto;
}

/* Override [hidden] display:none so CSS transition runs */
.sophia-panel[hidden] {
    display: flex !important;
    visibility: hidden;
}

/* ---- Header ---------------------------------------------------------------- */
.sophia-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px 18px;
    background: var(--c-surface-2, #1a2a45);
    border-bottom: 1px solid var(--c-border, #263352);
    flex-shrink: 0;
}

.sophia-header-avatar {
    border-radius: 50%;
    flex-shrink: 0;
    /* Subtle ring to lift avatar off the header */
    box-shadow: 0 0 0 2px rgba(125,200,0,0.25);
}

.sophia-header-info {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
}

.sophia-header-name {
    font-weight: 700;
    font-size: 0.9375rem;
    color: var(--c-text-primary, #e8edf5);
    line-height: 1.2;
}

.sophia-header-role {
    font-size: 0.78rem;
    color: var(--c-text-secondary, #8fa3c8);
    line-height: 1.35;
    margin-top: 1px;
}

/* Online indicator dot */
.sophia-header-role::before {
    content: '';
    display: inline-block;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #7dc800;
    margin-right: 5px;
    vertical-align: middle;
    position: relative;
    top: -1px;
}

.sophia-close {
    background: none;
    border: none;
    color: var(--c-text-secondary, #8fa3c8);
    font-size: 1.1rem;
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
    line-height: 1;
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.1s ease, background 0.1s ease;
}

.sophia-close:hover,
.sophia-close:focus-visible {
    color: var(--c-text-primary, #e8edf5);
    background: rgba(255,255,255,0.07);
    outline: 2px solid var(--c-accent, #7dc800);
    outline-offset: 1px;
}

/* ---- Message log ----------------------------------------------------------- */
.sophia-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
}

/* Thin custom scrollbar — visual polish */
.sophia-messages::-webkit-scrollbar {
    width: 4px;
}
.sophia-messages::-webkit-scrollbar-track {
    background: transparent;
}
.sophia-messages::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.12);
    border-radius: 2px;
}

.sophia-msg {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    max-width: 86%;
    animation: sophia-msg-in 0.18s ease;
}

/* History-restored messages appear instantly — no slide-in animation */
.sophia-msg--no-anim {
    animation: none;
}

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

.sophia-msg--user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.sophia-msg--assistant {
    align-self: flex-start;
}

/* Per-message Sophia avatar (28×28, aligned to bubble bottom) */
.sophia-msg-avatar {
    flex-shrink: 0;
    border-radius: 50%;
    width: 28px;
    height: 28px;
    align-self: flex-end;
    margin-bottom: 2px;
    opacity: 0.92;
}

.sophia-msg-bubble {
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 0.9375rem;   /* 15px */
    line-height: 1.55;
    word-break: break-word;
}

.sophia-msg--user .sophia-msg-bubble {
    background: var(--c-accent-dim, rgba(125,200,0,0.14));
    border: 1px solid rgba(125,200,0,0.45);
    color: var(--c-text-primary, #e8edf5);
    border-bottom-right-radius: 4px;
}

.sophia-msg--assistant .sophia-msg-bubble {
    background: var(--c-surface-2, #1a2a45);
    border: 1px solid var(--c-border, #263352);
    color: var(--c-text-primary, #e8edf5);
    border-bottom-left-radius: 4px;
}

/* ---- In-message clickable links (parsed from markdown [label](url)) -------- */
/* Dark theme: #7dc800 on #1a2a45 ≈ 7.2:1 — passes WCAG AA */
.sophia-link {
    color: var(--c-accent, #7dc800);
    text-decoration: underline;
    text-underline-offset: 2px;
    text-decoration-thickness: 1px;
    transition: opacity 0.12s ease;
    border-radius: 2px;
}

.sophia-link:hover {
    opacity: 0.8;
    text-decoration-thickness: 2px;
}

.sophia-link:focus-visible {
    outline: 2px solid var(--c-accent, #7dc800);
    outline-offset: 2px;
    border-radius: 3px;
}

/* Audit result card inside assistant message */
.sophia-audit-card {
    margin-top: 10px;
    padding: 14px 16px;
    background: var(--c-surface-2, #1a2a45);
    border: 1px solid var(--c-accent, #7dc800);
    border-radius: 12px;
    font-size: 0.85rem;
    line-height: 1.5;
}

.sophia-audit-card .sophia-score {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--c-accent, #7dc800);
}

.sophia-audit-card .sophia-report-btn {
    display: inline-block;
    margin-top: 10px;
    padding: 7px 16px;
    background: var(--c-accent, #7dc800);
    color: #0f1a2e;
    border-radius: 7px;
    font-weight: 700;
    font-size: 0.82rem;
    text-decoration: none;
    transition: opacity 0.15s ease;
}

.sophia-audit-card .sophia-report-btn:hover,
.sophia-audit-card .sophia-report-btn:focus-visible {
    opacity: 0.85;
    outline: 2px solid var(--c-accent, #7dc800);
    outline-offset: 2px;
}

/* ---- Typing indicator ------------------------------------------------------ */
.sophia-typing {
    display: flex;
    gap: 5px;
    padding: 10px 16px 10px 52px; /* 52px = avatar-width(28) + gap(8) + msg-padding(16) */
    align-items: center;
    flex-shrink: 0;
}

.sophia-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--c-text-secondary, #8fa3c8);
    animation: sophia-pulse 1.2s infinite ease-in-out;
}

.sophia-dot:nth-child(2) { animation-delay: 0.2s; }
.sophia-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes sophia-pulse {
    0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
    40%            { opacity: 1;   transform: scale(1.1); }
}

/* ---- Quick-action buttons -------------------------------------------------- */
.sophia-quick-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 7px;
    padding: 0 16px 14px;
    flex-shrink: 0;
}

.sophia-quick-actions button {
    background: transparent;
    border: 1px solid var(--c-accent, #7dc800);
    color: var(--c-accent, #7dc800);
    border-radius: 999px;
    padding: 6px 14px;
    font-size: 0.8rem;
    font-family: inherit;
    cursor: pointer;
    transition: background 0.12s ease, color 0.12s ease;
    min-height: 34px;
}

.sophia-quick-actions button:hover,
.sophia-quick-actions button:focus-visible {
    background: var(--c-accent, #7dc800);
    color: #0f1a2e;
    outline: 2px solid var(--c-accent, #7dc800);
    outline-offset: 2px;
}

/* ---- Input row ------------------------------------------------------------- */
.sophia-input-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 14px 16px;
    border-top: 1px solid var(--c-border, #263352);
    background: var(--c-surface-2, #1a2a45);
    flex-shrink: 0;
}

.sophia-input-row input {
    flex: 1;
    background: var(--c-surface-1, #162035);
    border: 1px solid var(--c-border, #263352);
    border-radius: 10px;
    padding: 10px 14px;
    color: var(--c-text-primary, #e8edf5);
    font-size: 0.9375rem;
    font-family: inherit;
    min-height: 44px;
    outline: none;
    transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

.sophia-input-row input:focus {
    border-color: var(--c-accent, #7dc800);
    box-shadow: 0 0 0 3px rgba(125,200,0,0.12);
}

.sophia-input-row input::placeholder {
    color: var(--c-text-secondary, #8fa3c8);
}

.sophia-send-btn {
    background: var(--c-accent, #7dc800);
    border: none;
    border-radius: 10px;
    color: #0f1a2e;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: opacity 0.12s ease, transform 0.1s ease;
    min-width: 44px;
    min-height: 44px;
}

.sophia-send-btn:hover,
.sophia-send-btn:focus-visible {
    opacity: 0.85;
    transform: scale(1.05);
    outline: 2px solid var(--c-accent, #7dc800);
    outline-offset: 2px;
}

/* ---- Visually hidden (WCAG) ------------------------------------------------ */
.sophia-panel .visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0,0,0,0);
    white-space: nowrap;
    border: 0;
}

/* ---- URL prompt (inline in message list for audit flow) -------------------- */
.sophia-url-prompt {
    margin-top: 10px;
    display: flex;
    gap: 7px;
}

.sophia-url-prompt input {
    flex: 1;
    background: var(--c-surface-1, #162035);
    border: 1px solid var(--c-border, #263352);
    border-radius: 8px;
    padding: 7px 11px;
    color: var(--c-text-primary, #e8edf5);
    font-size: 0.875rem;
    font-family: inherit;
    min-height: 38px;
    outline: none;
}

.sophia-url-prompt input:focus {
    border-color: var(--c-accent, #7dc800);
}

.sophia-url-prompt button {
    background: var(--c-accent, #7dc800);
    border: none;
    border-radius: 8px;
    color: #0f1a2e;
    font-size: 0.82rem;
    font-weight: 700;
    font-family: inherit;
    padding: 7px 14px;
    cursor: pointer;
    white-space: nowrap;
    min-height: 38px;
    transition: opacity 0.12s ease;
}

.sophia-url-prompt button:hover,
.sophia-url-prompt button:focus-visible {
    opacity: 0.85;
    outline: 2px solid var(--c-accent, #7dc800);
    outline-offset: 2px;
}

/* ---- Mobile — bottom-sheet style (<480px) ---------------------------------- */
@media (max-width: 479px) {
    .sophia-bubble {
        bottom: 16px;
        right: 16px;
    }

    .sophia-panel {
        width: 100vw;
        max-height: 75vh;
        bottom: 0;
        right: 0;
        left: 0;
        border-radius: 20px 20px 0 0;
        border-left: none;
        border-right: none;
        border-bottom: none;
    }
}

/* ---- Light theme overrides ------------------------------------------------- */
[data-theme="light"] .sophia-bubble,
[data-bs-theme="light"] .sophia-bubble {
    background: #f0f7e6;
    color: #0f1a2e;
    border-color: #5a9a00;
}

[data-theme="light"] .sophia-panel,
[data-bs-theme="light"] .sophia-panel {
    background: #f8fafc;
    border-color: #d0dae8;
}

[data-theme="light"] .sophia-header,
[data-bs-theme="light"] .sophia-header {
    background: #eef2fa;
    border-color: #d0dae8;
}

[data-theme="light"] .sophia-msg--assistant .sophia-msg-bubble,
[data-bs-theme="light"] .sophia-msg--assistant .sophia-msg-bubble {
    background: #eef2fa;
    border-color: #d0dae8;
    color: #1a2740;
}

[data-theme="light"] .sophia-msg--user .sophia-msg-bubble,
[data-bs-theme="light"] .sophia-msg--user .sophia-msg-bubble {
    color: #0f1a2e;
}

[data-theme="light"] .sophia-input-row,
[data-bs-theme="light"] .sophia-input-row {
    background: #eef2fa;
    border-color: #d0dae8;
}

[data-theme="light"] .sophia-input-row input,
[data-bs-theme="light"] .sophia-input-row input {
    background: #fff;
    border-color: #d0dae8;
    color: #1a2740;
}

[data-theme="light"] .sophia-header-name,
[data-bs-theme="light"] .sophia-header-name {
    color: #1a2740;
}

[data-theme="light"] .sophia-header-role,
[data-bs-theme="light"] .sophia-header-role {
    color: #5a6a85;
}

/* Light theme link: #3d7a00 on #eef2fa ≈ 6.3:1 — passes WCAG AA */
[data-theme="light"] .sophia-link,
[data-bs-theme="light"] .sophia-link {
    color: #3d7a00;
}

[data-theme="light"] .sophia-link:focus-visible,
[data-bs-theme="light"] .sophia-link:focus-visible {
    outline-color: #3d7a00;
}

[data-theme="light"] .sophia-messages::-webkit-scrollbar-thumb,
[data-bs-theme="light"] .sophia-messages::-webkit-scrollbar-thumb {
    background: rgba(0,0,0,0.15);
}

[data-theme="light"] .sophia-close:hover,
[data-bs-theme="light"] .sophia-close:hover {
    background: rgba(0,0,0,0.06);
}
