/* ============================================================
   AI Chat Page - Mobile-first formal UI
   Path: pages/ai.css
   ============================================================ */

:root {
    --ai-bg:           #f6f7fb;
    --ai-surface:      #ffffff;
    --ai-surface-2:    #f1f3f8;
    --ai-border:       rgba(0, 0, 0, 0.08);
    --ai-border-strong:rgba(0, 0, 0, 0.14);
    --ai-text:         #1a1d23;
    --ai-text-muted:   #5b6373;
    --ai-text-faint:   #8a93a4;
    --ai-primary:      var(--primary-color, #d32f2f);
    --ai-primary-hover:var(--button-hover, #b71c1c);
    --ai-primary-rgb:  var(--primary-color-rgb, 211, 47, 47);
    --ai-bubble-user:  linear-gradient(135deg, #d32f2f 0%, #b71c1c 100%);
    --ai-bubble-ai:    #ffffff;
    --ai-shadow-sm:    0 1px 2px rgba(0,0,0,0.06);
    --ai-shadow-md:    0 6px 20px rgba(0,0,0,0.08);
    --ai-radius:       18px;
    --ai-radius-sm:    12px;
    --ai-header-h:     0px; /* set by JS to actual sticky header height */

    /* Topic Colors */
    --topic-blue: #0070C0;
    --topic-orange: #F7A600;
    --topic-yellow: #FFC000;
    --topic-green: #00B050;
    --topic-red: #C00000;
    --topic-gray: #BFBFBF;
    --topic-purple: #7030A0;
    --topic-teal: #00B050;
}

body[data-theme="dark"] {
    --ai-bg:           #0f1115;
    --ai-surface:      #181b22;
    --ai-surface-2:    #20242d;
    --ai-border:       rgba(255, 255, 255, 0.08);
    --ai-border-strong:rgba(255, 255, 255, 0.16);
    --ai-text:         #e9ecf3;
    --ai-text-muted:   #a8b0c0;
    --ai-text-faint:   #6f7891;
    --ai-bubble-ai:    #20242d;
    --ai-shadow-md:    0 6px 22px rgba(0,0,0,0.45);
}

/* ---------- Layout ---------- */
.ai-main {
    flex: 1;
    width: 100%;
    display: flex;
    justify-content: center;
    padding: 0;
}

.ai-shell {
    width: 100%;
    max-width: 1200px;
    display: flex;
    flex-direction: row;
    /* Fill viewport below the sticky site header */
    min-height: calc(100dvh - var(--ai-header-h));
    height: calc(100dvh - var(--ai-header-h));
    background: var(--ai-bg);
    color: var(--ai-text);
    position: relative;
    overflow: hidden;
}

/* ---------- Chat Sidebar ---------- */
.ai-sidebar {
    width: 280px;
    background: var(--ai-surface);
    border-left: 1px solid var(--ai-border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    transition: transform 0.3s ease;
}

.ai-sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 1rem;
    border-bottom: 1px solid var(--ai-border);
}

.ai-new-chat-btn {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.6rem 0.8rem;
    background: var(--ai-bubble-user);
    color: #fff;
    border: none;
    border-radius: 10px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform .15s ease, box-shadow .15s ease;
    box-shadow: 0 4px 12px rgba(var(--ai-primary-rgb), 0.35);
}

.ai-new-chat-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 16px rgba(var(--ai-primary-rgb), 0.45);
}

.ai-new-chat-btn:active {
    transform: scale(0.98);
}

.ai-sidebar-toggle {
    width: 36px;
    height: 36px;
    border: 1px solid var(--ai-border);
    background: var(--ai-surface-2);
    color: var(--ai-text-muted);
    border-radius: 8px;
    display: grid;
    place-items: center;
    cursor: pointer;
    transition: background-color .18s ease, color .18s ease;
    flex-shrink: 0;
}

.ai-sidebar-toggle:hover {
    background: rgba(var(--ai-primary-rgb), 0.08);
    color: var(--ai-primary);
}

.ai-chat-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
}

.ai-chat-item {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.7rem 0.8rem;
    background: transparent;
    border: 1px solid transparent;
    border-radius: 10px;
    color: var(--ai-text);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: background-color .15s ease, border-color .15s ease;
    margin-bottom: 0.25rem;
    text-align: right;
}

.ai-chat-item:hover {
    background: var(--ai-surface-2);
}

.ai-chat-item.active {
    background: rgba(var(--ai-primary-rgb), 0.08);
    border-color: rgba(var(--ai-primary-rgb), 0.2);
    color: var(--ai-primary);
}

.ai-chat-item-title {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ai-chat-item-date {
    font-size: 0.75rem;
    color: var(--ai-text-faint);
    flex-shrink: 0;
}

.ai-chat-item-delete {
    width: 24px;
    height: 24px;
    border: none;
    background: transparent;
    color: var(--ai-text-muted);
    border-radius: 4px;
    display: grid;
    place-items: center;
    cursor: pointer;
    transition: background-color .15s ease, color .15s ease;
    flex-shrink: 0;
}

.ai-chat-item-delete:hover {
    background: rgba(var(--ai-primary-rgb), 0.1);
    color: var(--ai-primary);
}

/* ---------- Chat Area ---------- */
.ai-chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.ai-sidebar-show {
    width: 36px;
    height: 36px;
    border: 1px solid var(--ai-border);
    background: var(--ai-surface-2);
    color: var(--ai-text-muted);
    border-radius: 8px;
    display: grid;
    place-items: center;
    cursor: pointer;
    transition: background-color .18s ease, color .18s ease;
    flex-shrink: 0;
}

.ai-sidebar-show:hover {
    background: rgba(var(--ai-primary-rgb), 0.08);
    color: var(--ai-primary);
}

/* ---------- Title bar ---------- */
.ai-titlebar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    background: var(--ai-surface);
    border-bottom: 1px solid var(--ai-border);
    box-shadow: var(--ai-shadow-sm);
    z-index: 2;
}

.ai-title-wrap {
    display: flex;
    align-items: center;
    gap: 0.7rem;
    min-width: 0;
}

.ai-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    color: #fff;
    background: var(--ai-bubble-user);
    box-shadow: 0 4px 10px rgba(var(--ai-primary-rgb), 0.35);
    flex-shrink: 0;
}

.ai-title-text { display: flex; flex-direction: column; min-width: 0; }

.ai-title {
    font-size: 1.05rem;
    font-weight: 800;
    color: var(--ai-text);
    line-height: 1.2;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ai-subtitle {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.78rem;
    color: var(--ai-text-muted);
    font-weight: 500;
}

.ai-status-dot {
    width: 8px; height: 8px; border-radius: 50%;
    background: #22c55e;
    box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.18);
    animation: ai-pulse 2.2s ease-in-out infinite;
}

@keyframes ai-pulse {
    0%, 100% { box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.18); }
    50%      { box-shadow: 0 0 0 6px rgba(34, 197, 94, 0.05); }
}

.ai-iconbtn {
    width: 40px; height: 40px;
    border-radius: 12px;
    border: 1px solid var(--ai-border);
    background: var(--ai-surface-2);
    color: var(--ai-text-muted);
    display: grid;
    place-items: center;
    cursor: pointer;
    transition: background-color .18s ease, color .18s ease, transform .18s ease, border-color .18s ease;
    flex-shrink: 0;
}
.ai-iconbtn:hover { background: rgba(var(--ai-primary-rgb), 0.08); color: var(--ai-primary); border-color: rgba(var(--ai-primary-rgb), 0.25); }
.ai-iconbtn:active { transform: scale(0.95); }

.ai-lang-select {
    appearance: none;
    -webkit-appearance: none;
    background: var(--ai-surface-2);
    color: var(--ai-text);
    border: 1px solid var(--ai-border);
    border-radius: 10px;
    padding: 0.45rem 0.7rem;
    padding-left: 1.8rem;
    font-size: 0.82rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: background-color .18s ease, border-color .18s ease, color .18s ease;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%238a93a4' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: left 0.55rem center;
    background-size: 14px;
    min-width: 100px;
    flex-shrink: 0;
}

.ai-lang-select:hover {
    background-color: rgba(var(--ai-primary-rgb), 0.06);
    border-color: rgba(var(--ai-primary-rgb), 0.3);
}

body[data-theme="dark"] .ai-lang-select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%236f7891' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
}

@media (max-width: 480px) {
    .ai-lang-select {
        min-width: 80px;
        font-size: 0.75rem;
        padding: 0.35rem 0.5rem;
        padding-left: 1.6rem;
    }
}

/* ---------- Messages ---------- */
.ai-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem 0.9rem 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
    scroll-behavior: smooth;
    overscroll-behavior: contain;
    background:
        radial-gradient(1200px 600px at 50% -10%, rgba(var(--ai-primary-rgb), 0.06), transparent 60%),
        var(--ai-bg);
}

.ai-messages::-webkit-scrollbar { width: 8px; }
.ai-messages::-webkit-scrollbar-thumb { background: var(--ai-border-strong); border-radius: 8px; }
.ai-messages::-webkit-scrollbar-track { background: transparent; }

/* Welcome / empty state */
.ai-welcome {
    margin: auto 0;
    text-align: center;
    padding: 1.5rem 0.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.6rem;
    animation: ai-fade .4s ease;
}
.ai-welcome-badge {
    width: 64px; height: 64px;
    border-radius: 20px;
    display: grid; place-items: center;
    color: #fff;
    background: var(--ai-bubble-user);
    box-shadow: 0 10px 24px rgba(var(--ai-primary-rgb), 0.28);
    margin-bottom: 0.4rem;
}
.ai-welcome-title { font-size: 1.25rem; font-weight: 800; color: var(--ai-text); margin: 0; }
.ai-welcome-sub   { font-size: 0.92rem; color: var(--ai-text-muted); margin: 0 0 0.6rem; }

.ai-suggestions {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.55rem;
    width: 100%;
    max-width: 480px;
    margin-top: 0.4rem;
}
.ai-suggestion {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    text-align: right;
    padding: 0.8rem 0.95rem;
    background: var(--ai-surface);
    border: 1px solid var(--ai-border);
    border-radius: var(--ai-radius-sm);
    color: var(--ai-text);
    font-size: 0.92rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform .15s ease, border-color .15s ease, box-shadow .15s ease, background-color .15s ease;
    box-shadow: var(--ai-shadow-sm);
}
.ai-suggestion:hover {
    border-color: rgba(var(--ai-primary-rgb), 0.45);
    box-shadow: var(--ai-shadow-md);
    transform: translateY(-1px);
}
.ai-sug-ico { font-size: 1.15rem; flex-shrink: 0; }
.ai-sug-text { flex: 1; }

/* Message rows */
.ai-msg {
    display: flex;
    gap: 0.55rem;
    max-width: 100%;
    animation: ai-fade .25s ease;
}
.ai-msg.user { justify-content: flex-start; /* RTL: user's own bubble on the right */ flex-direction: row-reverse; }
.ai-msg.ai   { justify-content: flex-start; }

.ai-msg-avatar {
    width: 32px; height: 32px;
    border-radius: 50%;
    flex-shrink: 0;
    display: grid; place-items: center;
    font-size: 0.78rem;
    font-weight: 800;
    color: #fff;
    background: var(--ai-bubble-user);
    box-shadow: 0 2px 6px rgba(var(--ai-primary-rgb), 0.30);
    align-self: flex-end;
}
.ai-msg.user .ai-msg-avatar {
    background: linear-gradient(135deg, #455a64, #263238);
    box-shadow: 0 2px 6px rgba(0,0,0,0.25);
}

.ai-bubble {
    max-width: 78%;
    padding: 0.7rem 0.95rem;
    border-radius: var(--ai-radius);
    font-size: 0.95rem;
    line-height: 1.65;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    white-space: pre-wrap;
    box-shadow: var(--ai-shadow-sm);
    position: relative;
}
.ai-msg.ai .ai-bubble {
    background: var(--ai-bubble-ai);
    color: var(--ai-text);
    border: 1px solid var(--ai-border);
    border-bottom-right-radius: 6px; /* RTL "tail" */
}
.ai-msg.user .ai-bubble {
    background: var(--ai-bubble-user);
    color: #fff;
    border-bottom-left-radius: 6px; /* RTL "tail" on the other side */
}
.ai-msg.failed .ai-bubble {
    border-color: rgba(220, 38, 38, 0.28);
    box-shadow: 0 0 0 1px rgba(220, 38, 38, 0.06), var(--ai-shadow-sm);
}
.ai-step .ai-bubble {
    background: rgba(var(--ai-primary-rgb), 0.08);
    border: 1px dashed rgba(var(--ai-primary-rgb), 0.3);
    font-size: 0.85rem;
    font-family: 'Courier New', monospace;
}
.ai-step .ai-msg-avatar {
    background: rgba(var(--ai-primary-rgb), 0.15);
    color: var(--ai-primary);
}
.ai-msg-meta {
    display: block;
    font-size: 0.7rem;
    margin-top: 0.35rem;
    opacity: 0.75;
    font-weight: 500;
}
.ai-msg.user .ai-msg-meta { color: rgba(255,255,255,0.85); }
.ai-msg.ai   .ai-msg-meta { color: var(--ai-text-faint); }

.ai-msg-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    margin-top: 0.45rem;
}

.ai-msg-action {
    border: 1px solid rgba(var(--ai-primary-rgb), 0.18);
    background: rgba(var(--ai-primary-rgb), 0.06);
    color: inherit;
    font-size: 0.73rem;
    font-weight: 700;
    border-radius: 999px;
    padding: 0.22rem 0.6rem;
    cursor: pointer;
    transition: background-color .18s ease, border-color .18s ease, transform .18s ease;
}

.ai-msg-action:hover {
    background: rgba(var(--ai-primary-rgb), 0.12);
    border-color: rgba(var(--ai-primary-rgb), 0.3);
}

.ai-msg-action:active {
    transform: translateY(1px);
}

.ai-msg-icon-action {
    width: 28px;
    height: 28px;
    padding: 0;
    display: inline-grid;
    place-items: center;
    border-radius: 999px;
}

.ai-msg.user .ai-msg-action {
    background: rgba(255,255,255,0.12);
    border-color: rgba(255,255,255,0.22);
    color: #fff;
}

.ai-followup {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    position: relative;
    margin-top: 0.85rem;
    padding: 0.85rem 1rem;
    border: 1px solid var(--ai-border);
    border-radius: 14px;
    background: rgba(var(--ai-primary-rgb), 0.035);
    width: 100%;
}

.ai-followup-close {
    appearance: none;
    position: absolute;
    top: 0.45rem;
    left: 0.55rem;
    border: none;
    background: transparent;
    color: var(--ai-text-muted);
    width: 24px;
    height: 24px;
    border-radius: 6px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 1rem;
    line-height: 1;
}

.ai-followup-close:hover {
    color: var(--ai-text);
    background: rgba(var(--ai-primary-rgb), 0.08);
}

.ai-followup-title {
    font-size: 0.83rem;
    font-weight: 700;
    color: var(--ai-text-muted);
    padding-left: 1.6rem;
}

.ai-followup-choices {
    display: flex;
    flex-wrap: wrap;
    gap: 0.45rem;
}

.ai-followup-chip {
    border: 1px solid rgba(var(--ai-primary-rgb), 0.2);
    background: rgba(var(--ai-primary-rgb), 0.08);
    color: var(--ai-primary);
    border-radius: 999px;
    padding: 0.38rem 0.72rem;
    font-size: 0.78rem;
    font-weight: 700;
    cursor: pointer;
    transition: background-color .18s ease, border-color .18s ease, transform .18s ease;
}

.ai-followup-chip:hover {
    background: rgba(var(--ai-primary-rgb), 0.14);
    border-color: rgba(var(--ai-primary-rgb), 0.34);
}

.ai-followup-chip:active {
    transform: translateY(1px);
}

.ai-stream-state {
    font-size: 0.8rem;
    color: var(--ai-text-muted);
    margin-bottom: 0.35rem;
    font-weight: 600;
}

.ai-stream-hint {
    font-size: 0.78rem;
    color: #2563eb;
    background: rgba(37, 99, 235, 0.08);
    border: 1px solid rgba(37, 99, 235, 0.16);
    border-radius: 10px;
    padding: 0.35rem 0.55rem;
    margin-bottom: 0.45rem;
}

.ai-codeblock {
    margin: 0.45rem 0;
    border: 1px solid var(--ai-border);
    border-radius: 14px;
    overflow: hidden;
    background: var(--ai-surface-2);
}

.ai-codebar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.45rem 0.7rem;
    background: rgba(var(--ai-primary-rgb), 0.08);
    border-bottom: 1px solid var(--ai-border);
}

.ai-code-lang {
    font-size: 0.72rem;
    color: var(--ai-text-muted);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.ai-copy-btn {
    border: 0;
    background: transparent;
    color: var(--ai-primary);
    font-size: 0.76rem;
    font-weight: 700;
    cursor: pointer;
}

.ai-copy-btn:hover {
    text-decoration: underline;
}

.ai-codeblock pre {
    margin: 0;
    padding: 0.8rem 0.9rem;
    overflow-x: auto;
    direction: ltr;
    text-align: left;
}

.ai-codeblock code {
    font-family: Consolas, 'Courier New', monospace;
    font-size: 0.84rem;
    white-space: pre;
}

/* ---------- AI Message Content Styling ---------- */
.ai-msg.ai .ai-bubble h1,
.ai-msg.ai .ai-bubble h2,
.ai-msg.ai .ai-bubble h3,
.ai-msg.ai .ai-bubble h4 {
    margin: 1rem 0 0.5rem;
    font-weight: 650;
    line-height: 1.3;
}

.ai-msg.ai .ai-bubble h1 {
    font-size: 1.5rem;
    letter-spacing: -0.03em;
    color: var(--topic-blue);
}

.ai-msg.ai .ai-bubble h2 {
    font-size: 1.25rem;
    letter-spacing: -0.02em;
    color: var(--topic-yellow);
}

.ai-msg.ai .ai-bubble h3 {
    font-size: 1.08rem;
    color: var(--topic-green);
}

.ai-msg.ai .ai-bubble h4 {
    color: var(--topic-purple);
}

.ai-msg.ai .ai-bubble p,
.ai-msg.ai .ai-bubble ul,
.ai-msg.ai .ai-bubble ol {
    margin-bottom: 1rem;
}

.ai-msg.ai .ai-bubble p {
    font-size: 0.95rem;
    line-height: 1.7;
}

.ai-msg.ai .ai-bubble ul,
.ai-msg.ai .ai-bubble ol {
    padding-left: 1.4rem;
}

.ai-msg.ai .ai-bubble li + li {
    margin-top: 0.35rem;
}

.ai-msg.ai .ai-bubble > :first-child {
    margin-top: 0;
}

.ai-msg.ai .ai-bubble strong {
    color: var(--topic-orange);
    font-weight: 800;
}

.ai-msg.ai .ai-bubble em {
    font-style: italic;
}

/* ---------- RTL-specific styling ---------- */
.ai-msg.ai .ai-bubble[dir="rtl"] {
    text-align: right;
}

.ai-msg.ai .ai-bubble[dir="rtl"] ul,
.ai-msg.ai .ai-bubble[dir="rtl"] ol {
    padding-left: 0;
    padding-right: 1.4rem;
}

.ai-msg.ai .ai-bubble[dir="rtl"] blockquote {
    border-left: none;
    border-right: 3px solid rgba(var(--ai-primary-rgb), 0.4);
    padding: 0.2rem 1rem 0.2rem 0;
    background: var(--ai-surface-2);
    border-radius: 0.6rem 0 0 0.6rem;
}

.ai-scroll-bottom {
    position: absolute;
    left: 1rem;
    bottom: 6.25rem;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 1px solid var(--ai-border);
    background: var(--ai-surface);
    color: var(--ai-primary);
    box-shadow: var(--ai-shadow-md);
    display: grid;
    place-items: center;
    cursor: pointer;
    opacity: 0;
    pointer-events: none;
    transform: translateY(8px);
    transition: opacity .18s ease, transform .18s ease, background-color .18s ease;
    z-index: 3;
}

.ai-scroll-bottom.visible {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0);
}

.ai-scroll-bottom:hover {
    background: rgba(var(--ai-primary-rgb), 0.08);
}

/* Typing indicator */
.ai-typing { display: inline-flex; align-items: center; gap: 4px; padding: 4px 0; }
.ai-typing span {
    width: 7px; height: 7px; border-radius: 50%;
    background: var(--ai-text-faint);
    animation: ai-bounce 1.2s infinite ease-in-out;
}
.ai-typing span:nth-child(2) { animation-delay: .15s; }
.ai-typing span:nth-child(3) { animation-delay: .30s; }

@keyframes ai-bounce {
    0%, 80%, 100% { transform: scale(0.7); opacity: 0.5; }
    40%           { transform: scale(1);   opacity: 1;   }
}

@keyframes ai-fade {
    from { opacity: 0; transform: translateY(4px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ---------- Composer ---------- */
.ai-composer {
    position: sticky;
    bottom: 0;
    background: linear-gradient(180deg, rgba(255,255,255,0) 0%, var(--ai-surface) 24%);
    padding: 0.65rem 0.75rem calc(0.65rem + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--ai-border);
    z-index: 2;
}

body[data-theme="dark"] .ai-composer {
    background: linear-gradient(180deg, rgba(15,17,21,0) 0%, var(--ai-surface) 24%);
}

.ai-composer-inner {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.5rem;
    background: var(--ai-surface);
    border: 1px solid var(--ai-border-strong);
    border-radius: 22px;
    padding: 0.45rem 0.55rem 0.45rem 0.55rem;
    box-shadow: var(--ai-shadow-sm);
    transition: border-color .18s ease, box-shadow .18s ease;
}
.ai-composer-inner:focus-within {
    border-color: rgba(var(--ai-primary-rgb), 0.55);
    box-shadow: 0 0 0 3px rgba(var(--ai-primary-rgb), 0.15);
}

/* Attachment Preview */
.ai-attachment-preview {
    width: 100%;
    display: none;
    align-items: center;
    gap: 0.5rem;
    padding: 0 0.2rem;
}

.ai-attachment-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.4rem 0.6rem;
    background: var(--ai-surface-2);
    border: 1px solid var(--ai-border);
    border-radius: 10px;
    font-size: 0.85rem;
    color: var(--ai-text);
}

.ai-attachment-thumb {
    width: 32px;
    height: 32px;
    border-radius: 6px;
    object-fit: cover;
    flex-shrink: 0;
}

.ai-attachment-icon {
    flex-shrink: 0;
    color: var(--ai-text-muted);
}

.ai-attachment-name {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 500;
}

.ai-attachment-remove {
    width: 20px;
    height: 20px;
    border: 0;
    background: transparent;
    color: var(--ai-text-muted);
    border-radius: 4px;
    cursor: pointer;
    display: grid;
    place-items: center;
    flex-shrink: 0;
    transition: background-color .15s ease, color .15s ease;
}
.ai-attachment-remove:hover {
    background: rgba(var(--ai-primary-rgb), 0.1);
    color: var(--ai-primary);
}

/* Input Wrapper */
.ai-input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 0.4rem;
    width: 100%;
}

.ai-attachment-btn {
    width: 40px;
    height: 40px;
    border: 0;
    background: transparent;
    color: var(--ai-text-muted);
    border-radius: 50%;
    display: grid;
    place-items: center;
    cursor: pointer;
    transition: background-color .15s ease, color .15s ease;
    flex-shrink: 0;
}
.ai-attachment-btn:hover {
    background: var(--ai-surface-2);
    color: var(--ai-text);
}

.ai-input {
    flex: 1;
    border: 0;
    outline: 0;
    resize: none;
    background: transparent;
    color: var(--ai-text);
    font-family: inherit;
    font-size: 1rem;
    line-height: 1.5;
    padding: 0.45rem 0.55rem;
    max-height: 160px;
    min-height: 24px;
}
.ai-input::placeholder { color: var(--ai-text-faint); }

.ai-composer-actions {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.ai-counter {
    font-size: 0.7rem;
    color: var(--ai-text-faint);
    user-select: none;
    min-width: 60px;
    text-align: center;
}

.ai-send {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 0;
    background: var(--ai-bubble-user);
    color: #fff;
    display: grid;
    place-items: center;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(var(--ai-primary-rgb), 0.35);
    transition: transform .15s ease, opacity .15s ease, box-shadow .15s ease, background-color .15s ease;
    flex-shrink: 0;
}
.ai-send:hover:not(:disabled) { transform: translateY(-1px); }
.ai-send:active:not(:disabled) { transform: scale(0.95); }
.ai-send:disabled {
    background: var(--ai-text-faint);
    box-shadow: none;
    cursor: not-allowed;
    opacity: 0.6;
}
/* Flip the send icon for RTL so the arrow points to the start (left) */
.ai-send svg { transform: scaleX(-1); }

.ai-disclaimer {
    text-align: center;
    margin: 0.5rem 0 0.1rem;
    font-size: 0.7rem;
    color: var(--ai-text-faint);
}

/* ---------- Mobile-first refinements ---------- */
@media (max-width: 768px) {
    .ai-bubble    { max-width: 86%; font-size: 0.93rem; }
    .ai-titlebar  { padding: 0.6rem 0.8rem; }
    .ai-avatar    { width: 36px; height: 36px; }
    .ai-iconbtn   { width: 36px; height: 36px; border-radius: 10px; }
    .ai-title     { font-size: 1rem; }
    .ai-welcome-title { font-size: 1.1rem; }
    .ai-messages  { padding: 0.85rem 0.7rem 1rem; }
    .ai-counter   { display: none; }
    .ai-scroll-bottom { left: 0.8rem; bottom: 6rem; width: 40px; height: 40px; }

    .ai-shell {
        max-width: 100%;
        width: 100%;
        flex-direction: column;
    }

    .ai-chat-area {
        width: 100%;
        height: 100%;
    }
    
    /* Mobile sidebar - hidden by default */
    .ai-sidebar {
        position: absolute;
        top: 0;
        right: 0;
        width: min(86vw, 320px);
        max-width: 320px;
        height: 100%;
        z-index: 10;
        transform: translateX(100%);
        box-shadow: -4px 0 20px rgba(0,0,0,0.2);
    }
    
    .ai-sidebar.open {
        transform: translateX(0);
    }
    
    .ai-sidebar-show {
        display: grid !important;
    }
}

@media (min-width: 769px) {
    .ai-sidebar-show {
        display: none !important;
    }
}

/* Tablet+ */
@media (min-width: 720px) {
    .ai-suggestions { grid-template-columns: 1fr 1fr; }
    .ai-shell { border-left: 1px solid var(--ai-border); border-right: 1px solid var(--ai-border); }
}

/* ---------- Debug Steps (individual messages) ---------- */
.ai-debug-step-msg {
    display: flex;
    align-items: flex-start;
    gap: 0.7rem;
    margin: 0.6rem 0;
    animation: ai-fade .25s ease;
    max-width: 95%;
}
.ai-debug-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: rgba(var(--ai-primary-rgb), 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    color: var(--ai-primary);
    font-size: 0.65rem;
    font-weight: 700;
}
.ai-debug-step-num::before {
    content: '#';
}
.ai-debug-bubble {
    background: var(--ai-surface);
    border: 1px solid var(--ai-border);
    border-radius: 12px;
    padding: 0.55rem 0.8rem;
    flex: 1;
    min-width: 0;
    font-size: 0.75rem;
    line-height: 1.55;
    overflow: hidden;
}
.ai-debug-step-label {
    font-size: 0.65rem;
    font-weight: 700;
    color: var(--ai-primary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 0.35rem;
    border-bottom: 1px solid var(--ai-border);
    padding-bottom: 0.25rem;
}
.ai-debug-bubble pre {
    margin: 0;
    white-space: pre-wrap;
    word-break: break-word;
    font-family: Consolas, 'Courier New', monospace;
    font-size: 0.72rem;
    color: var(--ai-text);
    direction: ltr;
    text-align: left;
    max-height: 240px;
    overflow-y: auto;
}

/* Hide the body's decorative pattern from ::after under chat area for clarity */
body.ai-page::after { opacity: 0.35; }

/* When chat page is active, prevent body scroll on mobile so only messages scroll */
body.ai-page { overflow: hidden; }

/* Login Prompt */
.ai-login-prompt {
    background: rgba(211, 47, 47, 0.1);
    border: 1px solid rgba(211, 47, 47, 0.3);
    border-radius: var(--ai-radius-sm);
    padding: 1.5rem;
    margin: 1rem 0;
    animation: ai-fade .3s ease;
}

.ai-login-prompt-content {
    text-align: center;
}

.ai-login-prompt-content h3 {
    color: #d32f2f;
    font-size: 1.05rem;
    font-weight: 700;
    margin: 0 0 0.5rem;
}

.ai-login-prompt-content p {
    color: var(--ai-text);
    font-size: 0.9rem;
    margin: 0 0 1rem;
}

.ai-login-btn {
    background: var(--ai-bubble-user);
    color: #fff;
    border: none;
    padding: 0.6rem 1.2rem;
    border-radius: 10px;
    font-weight: 600;
    cursor: pointer;
    transition: transform .15s ease, box-shadow .15s ease;
    box-shadow: 0 4px 12px rgba(var(--ai-primary-rgb), 0.35);
}

.ai-login-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 16px rgba(var(--ai-primary-rgb), 0.45);
}
