/* =========================================================
   AI APP — Sidebar + Chat Layout
   Aligned with main sidebar style
   ========================================================= */

@keyframes aiFade {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: none; }
}

@keyframes aiTypingBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: .35; }
    30% { transform: translateY(-3px); opacity: 1; }
}

@keyframes msgSlideIn {
    from { opacity: 0; transform: translateY(8px) scale(0.98); }
    to { opacity: 1; transform: none; }
}

main:has(.ai-panel.active) {
    padding: 0;
    overflow: hidden;
    align-items: stretch;
    justify-content: stretch;
}

.ai-panel.active {
    width: 100%;
    height: 100%;
}

.ai-app {
    width: 100%;
    height: 100%;
    display: flex;
    overflow: hidden;
    animation: aiFade var(--speed-slow) var(--curve);
    background: var(--bg);
}

/* =========================
   SIDEBAR — MATCHES MAIN SIDEBAR
   ========================= */

.ai-sidebar {
    width: var(--inner-sidebar);
    min-width: var(--inner-sidebar);
    height: 100%;
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border-subtle);
    background: var(--sidebar);
    flex-shrink: 0;
    padding: 18px 10px;
}

/* Sidebar header — matches .brand style */
.ai-sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 6px 10px;
    border-bottom: 1px solid var(--border-subtle);
    flex-shrink: 0;
    min-height: 44px;
}

.ai-sidebar-header h2 {
    font-family: var(--font-display);
    font-size: var(--heading-section);
    font-weight: 600;
    letter-spacing: -0.01em;
    color: var(--text);
}

.ai-sidebar-new {
    width: var(--control-h-sm);
    height: var(--control-h-sm);
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--surface-raised);
    border: 1px solid var(--border-subtle);
    color: var(--text);
    cursor: pointer;
    transition: all var(--speed) var(--ease-standard);
}

.ai-sidebar-new:hover {
    background: var(--surface-selected);
    border-color: var(--border-emphasis);
}

.ai-sidebar-new svg {
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
    width: 16px;
    height: 16px;
}

/* Search — matches .ui-field style */
.ai-sidebar-search {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-height: var(--control-h);
    padding: 0 var(--space-3);
    margin: var(--space-3) var(--space-2) var(--space-2);
    background: var(--surface-raised);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    flex-shrink: 0;
    transition: border-color var(--speed) var(--ease-standard);
}

.ai-sidebar-search:focus-within {
    border-color: var(--accent);
}

.ai-sidebar-search svg {
    stroke: var(--muted-dim);
    fill: none;
    stroke-width: 1.6;
    flex-shrink: 0;
    width: 14px;
    height: 14px;
}

.ai-sidebar-search input {
    flex: 1;
    background: none;
    border: none;
    outline: none;
    color: var(--text);
    font-size: var(--text-md);
    font-family: var(--font-body);
}

.ai-sidebar-search input::placeholder {
    color: var(--muted-dim);
}

/* List — matches .channel-list style */
.ai-sidebar-list {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-1) var(--space-2);
}

.ai-sidebar-list::-webkit-scrollbar {
    width: 4px;
}

.ai-sidebar-list::-webkit-scrollbar-thumb {
    background: var(--border-strong);
    border-radius: var(--radius-pill);
}

.ai-sidebar-empty {
    text-align: center;
    padding: var(--space-8) var(--space-3);
    color: var(--muted-dim);
    font-size: var(--text-md);
}

.ai-sidebar-item {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-height: 36px;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: background var(--speed) var(--ease-standard);
    margin-bottom: 1px;
}

.ai-sidebar-item:hover {
    background: var(--surface);
}

.ai-sidebar-item.active {
    background: var(--surface-selected);
}

.ai-sidebar-item-text {
    flex: 1;
    font-size: var(--text-md);
    font-weight: 500;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ai-sidebar-item-del {
    width: 22px;
    height: 22px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    color: var(--muted-dim);
    cursor: pointer;
    opacity: 0;
    transition: all var(--speed) var(--ease-standard);
}

.ai-sidebar-item:hover .ai-sidebar-item-del {
    opacity: 1;
}

.ai-sidebar-item-del:hover {
    background: var(--surface-hover);
    color: var(--text);
}

.ai-sidebar-item-del svg {
    stroke: currentColor;
    fill: none;
    stroke-width: 1.7;
    width: 12px;
    height: 12px;
}

/* =========================
   SIDEBAR FOOTER — matches .chat-user-bar style
   ========================= */

.ai-sidebar-footer {
    border-top: 1px solid var(--border-subtle);
    padding: var(--space-3) var(--space-2) 0;
    margin-top: var(--space-2);
    flex-shrink: 0;
}

.ai-token-row {
    display: flex;
    justify-content: space-between;
    font-family: var(--font-mono);
    font-size: 10px;
    letter-spacing: .02em;
    color: var(--muted-dim);
    padding: 2px 4px;
}

.ai-token-row span:last-child {
    color: var(--text);
    font-weight: 500;
}

.ai-upgrade-btn {
    width: 100%;
    min-height: var(--control-h-sm);
    margin-top: var(--space-3);
    padding: 0 var(--space-3);
    border-radius: var(--radius-md);
    background: var(--surface-raised);
    border: 1px solid var(--border-subtle);
    color: var(--muted);
    font-size: var(--text-sm);
    font-weight: 500;
    cursor: pointer;
    transition: all var(--speed) var(--ease-standard);
}

.ai-upgrade-btn:hover {
    background: var(--surface-selected);
    border-color: var(--border-emphasis);
    color: var(--text);
}

/* =========================
   MAIN CHAT AREA — unchanged, still matches
   ========================= */

.ai-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    height: 100%;
    overflow: hidden;
    background: var(--bg);
}

.ai-chat-header {
    min-height: 58px;
    display: flex;
    align-items: center;
    padding: 0 var(--space-8);
    border-bottom: 1px solid var(--border-subtle);
    background: var(--surface-overlay);
    flex-shrink: 0;
}

.ai-chat-header h1 {
    font-family: var(--font-display);
    font-size: 22px;
    font-weight: 600;
    letter-spacing: -0.01em;
    color: var(--text);
    cursor: pointer;
    transition: color var(--speed) var(--ease-standard);
}

.ai-chat-header h1:hover {
    color: var(--muted);
}

.ai-messages {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-6) var(--space-8) var(--space-3);
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

.ai-messages::-webkit-scrollbar {
    width: 5px;
}

.ai-messages::-webkit-scrollbar-thumb {
    background: var(--border-strong);
    border-radius: var(--radius-pill);
}

.ai-empty {
    margin: auto;
    width: 100%;
    max-width: 620px;
    text-align: center;
}

.ai-empty-greeting {
    margin-bottom: var(--space-6);
}

.ai-empty-greeting h1 {
    font-family: var(--font-display);
    font-size: var(--heading-page);
    font-weight: 600;
    letter-spacing: -0.01em;
    color: var(--text);
    margin-bottom: 4px;
}

.ai-empty-greeting p {
    font-size: var(--text-lg);
    color: var(--muted);
}

.ai-suggest-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    width: 100%;
}

.ai-suggest-card {
    min-height: 76px;
    padding: var(--space-4);
    border-radius: var(--radius-lg);
    background: var(--surface-raised);
    border: 1px solid var(--border-subtle);
    cursor: pointer;
    font-size: var(--text-md);
    text-align: left;
    color: var(--text);
    transition: all var(--speed) var(--ease-standard);
    line-height: 1.4;
}

.ai-suggest-card:hover {
    background: var(--surface-selected);
    border-color: var(--border-emphasis);
}

.ai-msg {
    display: flex;
    max-width: 760px;
    width: 100%;
    margin: 0 auto;
    animation: msgSlideIn var(--speed-slow) var(--ease-standard);
}

.ai-msg.user {
    justify-content: flex-end;
}

.ai-msg-body {
    max-width: 100%;
}

.ai-msg.user .ai-msg-body {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

.ai-msg-bubble {
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-lg);
    font-size: var(--text-body);
    line-height: 1.6;
    background: var(--surface-raised);
    border: 1px solid var(--border-subtle);
    color: var(--text);
    white-space: pre-wrap;
    word-wrap: break-word;
    max-width: 100%;
}

.ai-msg.user .ai-msg-bubble {
    background: var(--surface-selected);
    border-color: var(--border-emphasis);
}

.ai-typing-bubble {
    padding: 6px 14px !important;
    min-height: 32px;
    display: flex;
    align-items: center;
}

.ai-typing {
    display: flex;
    gap: 4px;
    padding: 0;
}

.ai-typing span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--muted);
    animation: aiTypingBounce 1.1s infinite ease-in-out;
}

.ai-typing span:nth-child(2) { animation-delay: .15s; }
.ai-typing span:nth-child(3) { animation-delay: .3s; }

.ai-error-bubble {
    padding: 10px 16px;
    border-radius: var(--radius-lg);
    background: var(--surface-raised);
    border: 1px solid var(--border-emphasis);
    color: var(--muted);
    font-size: var(--text-md);
    text-align: center;
    width: 100%;
}

.ai-error-bubble strong {
    display: block;
    color: var(--text);
    margin-bottom: 6px;
}

.ai-error-retry {
    margin-top: 6px;
    padding: 4px 14px;
    border-radius: var(--radius-sm);
    background: var(--surface-selected);
    border: 1px solid var(--border-subtle);
    color: var(--text);
    font-size: var(--text-sm);
    font-weight: 600;
    cursor: pointer;
    transition: all var(--speed) var(--ease-standard);
}

.ai-error-retry:hover {
    background: var(--accent-dim);
    border-color: var(--accent);
    color: var(--accent);
}

.ai-composer-wrap {
    flex-shrink: 0;
    padding: 0 var(--space-8) var(--space-6);
}

.ai-composer {
    display: flex;
    align-items: flex-end;
    gap: var(--space-2);
    max-width: 760px;
    margin: 0 auto;
    background: var(--surface-raised);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    padding: var(--space-1) var(--space-1) var(--space-1) var(--space-4);
    transition: border-color var(--speed) var(--ease-standard), 
                background var(--speed) var(--ease-standard),
                box-shadow var(--speed) var(--ease-standard);
}

.ai-composer:focus-within {
    border-color: var(--accent);
    background: var(--surface-selected);
    box-shadow: var(--focus-ring);
}

.ai-composer textarea {
    flex: 1;
    background: none;
    border: none;
    outline: none;
    resize: none;
    color: var(--text);
    font-family: var(--font-body);
    font-size: var(--text-body);
    line-height: 1.5;
    max-height: 160px;
    padding: 10px 0;
    min-height: 24px;
}

.ai-composer textarea::placeholder {
    color: var(--muted-dim);
}

.ai-composer-right {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

.ai-model-selector {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 5px 10px 5px 8px;
    border-radius: var(--radius-md);
    cursor: pointer;
    font-size: var(--text-sm);
    color: var(--text);
    background: var(--surface-selected);
    border: 1px solid var(--border-subtle);
    transition: all var(--speed) var(--ease-standard);
    user-select: none;
    white-space: nowrap;
}

.ai-model-selector:hover {
    background: var(--surface-hover);
    border-color: var(--border-emphasis);
}

.ai-model-selector.active {
    border-color: var(--accent);
    background: var(--surface-hover);
}

.ai-model-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

.ai-model-icon svg {
    width: 16px;
    height: 16px;
    stroke: var(--text);
    fill: none;
    opacity: 0.7;
}

.ai-model-name {
    font-size: var(--text-sm);
    color: var(--text);
    font-weight: 500;
}

.ai-model-arrow {
    font-size: 8px;
    color: var(--muted-dim);
    transition: transform var(--speed) var(--ease-standard);
}

.ai-model-selector.active .ai-model-arrow {
    transform: rotate(180deg);
}

.ai-send {
    width: var(--control-h);
    height: var(--control-h);
    flex-shrink: 0;
    border-radius: var(--radius-md);
    background: var(--surface-selected);
    border: 1px solid var(--border-emphasis);
    color: var(--text);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all var(--speed) var(--ease-standard);
}

.ai-send:hover:not(:disabled) {
    background: var(--accent-dim);
    border-color: var(--accent);
    color: var(--accent);
}

.ai-send:disabled {
    opacity: .3;
    cursor: default;
}

.ai-send svg {
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
    width: 18px;
    height: 18px;
}

.ai-model-dropdown {
    position: fixed;
    z-index: 300;
    background: var(--surface-overlay);
    border: 1px solid var(--border-emphasis);
    border-radius: var(--radius-lg);
    padding: var(--space-2);
    box-shadow: var(--shadow-overlay);
    min-width: 280px;
    max-height: 320px;
    overflow-y: auto;
}

.ai-model-dropdown::-webkit-scrollbar {
    width: 4px;
}

.ai-model-dropdown::-webkit-scrollbar-thumb {
    background: var(--border-strong);
    border-radius: var(--radius-pill);
}

.ai-model-group-label {
    font-family: var(--font-mono);
    font-size: 9px;
    letter-spacing: .08em;
    text-transform: uppercase;
    color: var(--muted-dim);
    padding: 8px 10px 4px;
}

.ai-model-option {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--speed) var(--ease-standard);
}

.ai-model-option:hover {
    background: var(--surface);
}

.ai-model-option.active {
    background: var(--surface-selected);
}

.ai-model-option-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    flex-shrink: 0;
}

.ai-model-option-icon svg {
    width: 18px;
    height: 18px;
    stroke: var(--text);
    fill: none;
    opacity: 0.6;
}

.ai-model-option.active .ai-model-option-icon svg {
    stroke: var(--accent);
    opacity: 1;
}

.ai-model-option-info {
    flex: 1;
    min-width: 0;
}

.ai-model-option-name {
    display: block;
    font-size: var(--text-md);
    font-weight: 500;
    color: var(--text);
    line-height: 1.3;
}

.ai-model-option-desc {
    display: block;
    font-size: 11px;
    color: var(--muted-dim);
    line-height: 1.2;
}

.ai-model-option-tags {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

.ai-model-option-tag {
    font-size: 9px;
    font-weight: 600;
    padding: 1px 7px;
    border-radius: 4px;
    text-transform: uppercase;
    letter-spacing: .04em;
}

.ai-model-option-tag.free {
    background: var(--accent-dim);
    color: var(--accent);
}

.ai-model-option-tag.fast {
    background: var(--info-dim);
    color: var(--info);
}

.ai-model-option-check {
    color: var(--accent);
    font-weight: 600;
    font-size: 14px;
    margin-left: 2px;
}

/* =========================
   MOBILE
   ========================= */

@media(max-width: 768px) {
    .ai-sidebar {
        width: 0;
        min-width: 0;
        overflow: hidden;
        border-right: none;
        padding: 0;
    }

    .ai-chat-header {
        padding: 0 var(--space-4);
        min-height: 50px;
    }

    .ai-chat-header h1 {
        font-size: 20px;
    }

    .ai-messages {
        padding: var(--space-4) var(--space-4) var(--space-2);
        gap: var(--space-1);
    }

    .ai-msg-bubble {
        font-size: var(--text-md);
        padding: 6px 12px;
    }

    .ai-composer-wrap {
        padding: 0 var(--space-4) var(--space-4);
    }

    .ai-suggest-grid {
        grid-template-columns: 1fr;
    }

    .ai-empty-greeting h1 {
        font-size: 26px;
    }

    .ai-model-dropdown {
        right: 16px !important;
        left: 16px !important;
        width: auto !important;
        min-width: 0 !important;
    }

    .ai-model-option {
        padding: 6px 8px;
    }

    .ai-model-option-name {
        font-size: var(--text-sm);
    }
}