/* ── PhysicsMe Chat Widget ──────────────────────────────────────────────── */

#pm-chat-launcher {
    position: fixed;
    bottom: 28px;
    right: 28px;
    z-index: 99990;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, #1e3a5f 0%, #2563eb 100%);
    color: #fff;
    border: none;
    cursor: pointer;
    box-shadow: 0 4px 18px rgba(37,99,235,.45);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1px;
    transition: transform .2s, box-shadow .2s;
}
.pm-fab-icon {
    font-size: 20px;
    line-height: 1;
    display: block;
}
.pm-fab-label {
    font-size: 10px;
    font-weight: 800;
    letter-spacing: 1.5px;
    line-height: 1;
    display: block;
    font-family: 'Vazirmatn', system-ui, sans-serif;
}
#pm-chat-launcher:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 24px rgba(37,99,235,.55);
}
#pm-chat-launcher .pm-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    background: #ef4444;
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    border-radius: 50%;
    width: 18px;
    height: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    display: none;
}

/* ── Panel ───────────────────────────────────────────────────────────────── */
#pm-chat-panel {
    position: fixed;
    bottom: 96px;
    right: 24px;
    z-index: 99989;
    width: 380px;
    max-width: calc(100vw - 32px);
    height: 540px;
    max-height: calc(100vh - 120px);
    background: #0f172a;
    border-radius: 16px;
    display: flex;
    flex-direction: column;
    box-shadow: 0 8px 40px rgba(0,0,0,.5);
    transform: translateY(20px) scale(.97);
    opacity: 0;
    pointer-events: none;
    transition: transform .22s ease, opacity .22s ease;
}
#pm-chat-panel.pm-open {
    transform: translateY(0) scale(1);
    opacity: 1;
    pointer-events: all;
}

/* Header */
#pm-chat-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 16px;
    background: linear-gradient(90deg, #1e3a5f, #1e40af);
    border-radius: 16px 16px 0 0;
    flex-shrink: 0;
}
#pm-chat-header .pm-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255,255,255,.15);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
}
#pm-chat-header .pm-title {
    flex: 1;
    color: #fff;
    font-weight: 600;
    font-size: 14px;
    line-height: 1.2;
}
#pm-chat-header .pm-subtitle {
    font-size: 11px;
    color: rgba(255,255,255,.65);
    margin-top: 2px;
}
.pm-tokens-display {
    font-size: 11px;
    color: rgba(255,255,255,.75);
    background: rgba(255,255,255,.1);
    padding: 2px 8px;
    border-radius: 10px;
    white-space: nowrap;
    flex-shrink: 0;
}
.pm-tokens-display.pm-tokens-low {
    background: rgba(239,68,68,.3);
    color: #fca5a5;
}
#pm-chat-close {
    background: none;
    border: none;
    color: rgba(255,255,255,.7);
    cursor: pointer;
    font-size: 20px;
    line-height: 1;
    padding: 4px;
    border-radius: 4px;
    transition: color .15s;
}
#pm-chat-close:hover { color: #fff; }

/* Disclaimer banner */
#pm-chat-disclaimer {
    padding: 7px 14px;
    background: #1c1a10;
    border-bottom: 1px solid #3d3510;
    color: #a89030;
    font-size: 11.5px;
    text-align: center;
    flex-shrink: 0;
    line-height: 1.4;
}

/* Messages */
#pm-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
}
#pm-chat-messages::-webkit-scrollbar { width: 4px; }
#pm-chat-messages::-webkit-scrollbar-track { background: transparent; }
#pm-chat-messages::-webkit-scrollbar-thumb { background: rgba(255,255,255,.15); border-radius: 4px; }

.pm-msg {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 13.5px;
    line-height: 1.55;
    word-break: break-word;
    white-space: pre-wrap;
    animation: pm-msg-in .18s ease;
}
@keyframes pm-msg-in {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}
.pm-msg-user {
    background: #2563eb;
    color: #fff;
    align-self: flex-end;
    border-bottom-right-radius: 4px;
}
.pm-msg-assistant {
    background: #1e293b;
    color: #e2e8f0;
    align-self: flex-start;
    border-bottom-left-radius: 4px;
}
/* MathJax output inside chat bubbles */
.pm-msg mjx-container {
    direction: ltr !important;
    white-space: normal !important;
    overflow-x: auto;
    max-width: 100%;
    margin: 4px 0;
}
.pm-msg-assistant.pm-typing::after {
    content: '▋';
    animation: pm-blink .7s steps(1) infinite;
}
@keyframes pm-blink { 50% { opacity: 0; } }

.pm-source-badge {
    font-size: 11px;
    padding: 3px 8px;
    border-radius: 10px;
    align-self: flex-start;
    margin-top: -6px;
    opacity: .75;
}
.pm-source-site     { color: #4ade80; }
.pm-source-external { color: #60a5fa; }
.pm-source-general  { color: #94a3b8; }

.pm-msg-system {
    align-self: center;
    background: transparent;
    color: #64748b;
    font-size: 12px;
    text-align: center;
    padding: 4px 8px;
}

/* Input area */
#pm-chat-input-area {
    display: flex;
    gap: 8px;
    padding: 12px 14px;
    background: #1e293b;
    border-radius: 0 0 16px 16px;
    flex-shrink: 0;
    border-top: 1px solid rgba(255,255,255,.07);
}
#pm-chat-input {
    flex: 1;
    background: #0f172a;
    border: 1px solid rgba(255,255,255,.12);
    border-radius: 10px;
    color: #e2e8f0;
    padding: 9px 12px;
    font-size: 13.5px;
    resize: none;
    outline: none;
    line-height: 1.4;
    max-height: 120px;
    font-family: inherit;
    transition: border-color .15s;
}
#pm-chat-input:focus {
    border-color: #2563eb;
}
#pm-chat-input::placeholder { color: #475569; }
#pm-chat-send {
    background: #2563eb;
    border: none;
    border-radius: 10px;
    color: #fff;
    cursor: pointer;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background .15s, transform .1s;
    align-self: flex-end;
}
#pm-chat-send:hover:not(:disabled) { background: #1d4ed8; }
#pm-chat-send:active:not(:disabled) { transform: scale(.93); }
#pm-chat-send:disabled { background: #334155; cursor: not-allowed; }
#pm-chat-send svg { width: 18px; height: 18px; fill: currentColor; }
#pm-chat-stop {
    background: #dc2626;
    border: none;
    border-radius: 10px;
    color: #fff;
    cursor: pointer;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 16px;
    transition: background .15s, transform .1s;
    align-self: flex-end;
}
#pm-chat-stop:hover { background: #b91c1c; }
#pm-chat-stop:active { transform: scale(.93); }

/* RTL support */
body.rtl #pm-chat-launcher,
html[dir=rtl] #pm-chat-launcher {
    right: auto;
    left: 28px;
}
body.rtl #pm-chat-panel,
html[dir=rtl] #pm-chat-panel {
    right: auto;
    left: 24px;
}
body.rtl .pm-msg-user,
html[dir=rtl] .pm-msg-user {
    align-self: flex-start;
}
body.rtl .pm-msg-assistant,
html[dir=rtl] .pm-msg-assistant {
    align-self: flex-end;
}

/* ── Translation block ───────────────────────────────────────────────────── */
.pm-translation-sep {
    font-size: 11px;
    color: #94a3b8;
    margin-top: 10px;
    padding-top: 8px;
    border-top: 1px dashed rgba(148,163,184,.3);
    direction: rtl;
    text-align: right;
}
.pm-translation-fa {
    font-size: 13.5px;
    line-height: 1.8;
    color: #cbd5e1;
    margin-top: 4px;
    white-space: pre-wrap;
    word-break: break-word;
    font-family: 'Vazirmatn', Tahoma, sans-serif;
}

@media (max-width: 480px) {
    #pm-chat-panel {
        bottom: 0;
        right: 0;
        left: 0;
        width: 100%;
        max-width: 100%;
        border-radius: 16px 16px 0 0;
        /* dvh = dynamic viewport height — shrinks when keyboard appears or browser chrome shows */
        height: 65dvh;
        max-height: calc(100dvh - 56px); /* never cover the site header */
    }
    /* pad input area above iOS home indicator */
    #pm-chat-input-area {
        padding-bottom: max(12px, env(safe-area-inset-bottom));
    }
    /* tighter disclaimer on mobile */
    #pm-chat-disclaimer {
        font-size: 11px;
        padding: 5px 12px;
    }
    /* tighter header on mobile */
    #pm-chat-header {
        padding: 10px 14px;
    }
    #pm-chat-launcher {
        bottom: max(18px, env(safe-area-inset-bottom));
        right: 18px;
    }
    /* RTL override — same specificity as RTL rules but appears later → wins on mobile */
    html[dir=rtl] #pm-chat-panel,
    body.rtl #pm-chat-panel {
        left: 0;
        right: 0;
    }
    html[dir=rtl] #pm-chat-launcher,
    body.rtl #pm-chat-launcher {
        left: auto;
        right: 18px;
    }
}

/* ── User strip (logged-in users) ─────────────────────────────────────────── */
#pm-user-strip {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 5px 14px;
    background: #f0f4f8;
    border-bottom: 1px solid #dde3ea;
    font-size: 12px;
    color: #4a5568;
    flex-wrap: wrap;
}

#pm-user-name {
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 130px;
}

/* Claude token usage display */
.pm-claude-usage {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
    color: #555;
    flex: 1;
    justify-content: flex-end;
    min-width: 0;
}

.pm-claude-label {
    white-space: nowrap;
    font-family: monospace;
    font-size: 11px;
}

.pm-claude-bar-wrap {
    display: inline-block;
    width: 60px;
    height: 5px;
    background: #d1d9e0;
    border-radius: 3px;
    overflow: hidden;
    flex-shrink: 0;
}

.pm-claude-bar-fill {
    display: block;
    height: 100%;
    background: #3b82f6;
    border-radius: 3px;
    transition: width 0.4s ease;
}

.pm-claude-bar-fill.pm-claude-warn {
    background: #f59e0b;
}

.pm-claude-usage.pm-claude-near-limit .pm-claude-label {
    color: #b45309;
    font-weight: 600;
}

/* ── Rating buttons ──────────────────────────────────────────────────────── */
.pm-rating-wrap {
    display: flex;
    align-items: center;
    gap: 6px;
    align-self: flex-start;
    margin-top: 4px;
    padding: 3px 8px;
    background: rgba(255,255,255,.06);
    border-radius: 20px;
    border: 1px solid rgba(255,255,255,.08);
    transition: opacity .2s;
}
.pm-rating-wrap:hover { background: rgba(255,255,255,.1); }
.pm-rating-wrap.pm-rating-sent { opacity: .5; pointer-events: none; }
.pm-rate-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 15px;
    padding: 2px 4px;
    border-radius: 4px;
    line-height: 1;
    transition: transform .1s;
}
.pm-rate-btn:hover  { transform: scale(1.25); }
.pm-rate-chosen     { opacity: 1 !important; }
.pm-rating-label {
    font-size: 11px;
    color: #64748b;
    margin: 0 2px;
}
.pm-rating-fb {
    font-size: 11px;
    color: #4ade80;
    align-self: center;
    animation: pm-fb-in .2s ease;
}
@keyframes pm-fb-in { from { opacity: 0; } to { opacity: 1; } }
html[dir=rtl] .pm-rating-wrap { align-self: flex-end; }

/* ── Per-response token note ─────────────────────────────────────────────── */
.pm-token-note {
    align-self: flex-start;
    font-size: 10.5px;
    color: #475569;
    margin-top: -8px;
    padding: 1px 6px;
    opacity: .75;
    font-family: monospace;
    letter-spacing: .2px;
}
body.rtl .pm-token-note,
html[dir=rtl] .pm-token-note { align-self: flex-end; }
