/* ============================================
   W3 Dashboard - AI Operations Chat Widget
   Sidebar panel layout (replaces floating FAB)
   ============================================ */

/* ---- Sidebar Chat Toggle Button ---- */
.sidebar-chat-toggle {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-md);
    color: var(--text-on-dark-dim);
    font-size: var(--text-sm);
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s;
    text-decoration: none;
    margin-bottom: 2px;
    position: relative;
    background: none;
    border: none;
    width: 100%;
    font-family: var(--font-sans);
    text-align: start;
}
.sidebar-chat-toggle:hover {
    background: var(--bg-sidebar-hover);
    color: var(--text-on-dark);
}
.sidebar-chat-toggle.active {
    background: rgba(139, 92, 246, 0.15);
    color: #A78BFA;
}
.sidebar-chat-toggle.active::before {
    content: '';
    position: absolute;
    inset-inline-start: 0;
    top: 6px; bottom: 6px;
    width: 3px;
    background: #A78BFA;
    border-radius: 3px;
}
.sidebar-chat-toggle svg {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
    opacity: 0.7;
}
.sidebar-chat-toggle.active svg { opacity: 1; }

.sidebar-chat-toggle .chat-status-dot {
    width: 6px;
    height: 6px;
    border-radius: var(--radius-full);
    background: var(--success);
    margin-inline-start: auto;
    flex-shrink: 0;
}

/* ---- Chat Side Panel ---- */
.chat-side-panel {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 400px;
    z-index: 800;
    background: var(--bg-surface);
    border-inline-start: 1px solid var(--border-primary);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.08);
    display: flex;
    flex-direction: column;
    transform: translateX(-100%);
    transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}
.chat-side-panel.visible {
    transform: translateX(0);
}

/* ---- Header ---- */
.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--border-secondary);
    background: var(--bg-surface-alt);
    min-height: 56px;
    flex-shrink: 0;
}
.chat-header-title {
    font-family: var(--font-sans);
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: var(--space-2);
}
.chat-header-dot {
    width: 8px;
    height: 8px;
    border-radius: var(--radius-full);
    background: var(--success);
}
.chat-header-actions {
    display: flex;
    align-items: center;
    gap: var(--space-1);
}
.chat-header-btn {
    background: none;
    border: none;
    color: var(--text-tertiary);
    cursor: pointer;
    font-size: var(--text-xs);
    padding: var(--space-1) var(--space-2);
    border-radius: var(--radius-sm);
    font-family: var(--font-sans);
    display: flex;
    align-items: center;
    gap: var(--space-1);
    transition: all 0.15s;
}
.chat-header-btn:hover {
    color: var(--text-secondary);
    background: var(--bg-surface);
}
.chat-header-btn svg {
    width: 14px;
    height: 14px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
}

/* ---- Context Badge ---- */
.chat-context-bar {
    display: none;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-4);
    background: var(--accent-primary-light);
    border-bottom: 1px solid var(--border-secondary);
    font-family: var(--font-sans);
    font-size: var(--text-xs);
    color: var(--accent-primary);
    flex-shrink: 0;
}
.chat-context-bar.has-context {
    display: flex;
}
.chat-context-bar svg {
    width: 14px;
    height: 14px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    flex-shrink: 0;
}
.chat-context-text {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ---- Skill Selector Bar ---- */
.chat-skill-bar {
    display: flex;
    gap: 4px;
    padding: 8px 12px;
    overflow-x: auto;
    border-bottom: 1px solid var(--border-color, #e0e0e0);
    flex-shrink: 0;
    direction: rtl;
    scrollbar-width: none;
}
.chat-skill-bar::-webkit-scrollbar {
    display: none;
}
.chat-skill-btn {
    white-space: nowrap;
    padding: 4px 10px;
    border-radius: 12px;
    border: 1px solid var(--border-color, #ddd);
    background: transparent;
    font-size: 12px;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.15s;
    color: var(--text-secondary, #666);
    line-height: 1.4;
}
.chat-skill-btn.active {
    background: var(--accent-primary, #4f46e5);
    color: #fff;
    border-color: var(--accent-primary, #4f46e5);
}
.chat-skill-btn:hover:not(.active) {
    background: rgba(79, 70, 229, 0.08);
    border-color: var(--accent-primary, #4f46e5);
}

/* ---- Messages Area ---- */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-4);
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    min-height: 0;
}

/* ---- Message Bubbles ---- */
.chat-msg {
    max-width: 88%;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-md);
    font-family: var(--font-sans);
    font-size: var(--text-sm);
    line-height: 1.5;
    word-break: break-word;
    white-space: pre-wrap;
}
.chat-msg-user {
    align-self: flex-end;
    background: var(--accent-primary);
    color: #fff;
    border-bottom-right-radius: var(--radius-sm);
}
.chat-msg-assistant {
    align-self: flex-start;
    background: var(--bg-surface-alt);
    color: var(--text-primary);
    border-bottom-left-radius: var(--radius-sm);
}
.chat-msg-assistant table {
    border-collapse: collapse;
    width: 100%;
    margin: var(--space-2) 0;
    font-size: var(--text-xs);
}
.chat-msg-assistant th,
.chat-msg-assistant td {
    border: 1px solid var(--border-secondary);
    padding: var(--space-1) var(--space-2);
    text-align: start;
}
.chat-msg-assistant th {
    background: var(--bg-surface);
    font-weight: 600;
}
.chat-msg-assistant code {
    background: var(--bg-surface);
    padding: 1px 4px;
    border-radius: var(--radius-sm);
    font-family: var(--font-mono);
    font-size: var(--text-xs);
}
.chat-msg-assistant pre {
    background: var(--bg-surface);
    padding: var(--space-2);
    border-radius: var(--radius-sm);
    overflow-x: auto;
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    margin: var(--space-2) 0;
}

/* ---- Tool Cards ---- */
.chat-tool-card {
    align-self: flex-start;
    max-width: 92%;
    border: 1px solid var(--border-secondary);
    border-radius: var(--radius-md);
    overflow: hidden;
    font-family: var(--font-sans);
    font-size: var(--text-xs);
}
.chat-tool-header {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    background: var(--bg-surface-alt);
    cursor: pointer;
    user-select: none;
}
.chat-tool-header:hover {
    background: var(--border-secondary);
}
.chat-tool-icon {
    font-size: 14px;
    flex-shrink: 0;
}
.chat-tool-name {
    font-weight: 600;
    color: var(--text-primary);
    flex: 1;
}
.chat-tool-chevron {
    color: var(--text-tertiary);
    transition: transform 0.15s;
    font-size: 10px;
}
.chat-tool-card.expanded .chat-tool-chevron {
    transform: rotate(90deg);
}
.chat-tool-body {
    display: none;
    padding: var(--space-2) var(--space-3);
    background: var(--bg-surface);
    color: var(--text-secondary);
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    max-height: 120px;
    overflow-y: auto;
    white-space: pre-wrap;
    word-break: break-all;
    border-top: 1px solid var(--border-secondary);
}
.chat-tool-card.expanded .chat-tool-body {
    display: block;
}
.chat-tool-error {
    border-color: var(--error-border);
}
.chat-tool-error .chat-tool-header {
    background: var(--error-bg);
}

/* ---- Confirmation Card ---- */
.chat-confirm-card {
    align-self: flex-start;
    max-width: 92%;
    border: 1px solid var(--warning-border);
    border-radius: var(--radius-md);
    background: var(--warning-bg);
    padding: var(--space-3);
    font-family: var(--font-sans);
    font-size: var(--text-sm);
}
.chat-confirm-title {
    font-weight: 600;
    color: var(--warning);
    margin-bottom: var(--space-2);
    display: flex;
    align-items: center;
    gap: var(--space-2);
}
.chat-confirm-desc {
    color: var(--text-primary);
    margin-bottom: var(--space-3);
    font-size: var(--text-xs);
    line-height: 1.5;
}
.chat-confirm-actions {
    display: flex;
    gap: var(--space-2);
}
.chat-confirm-btn {
    padding: var(--space-1) var(--space-3);
    border-radius: var(--radius-sm);
    border: none;
    cursor: pointer;
    font-size: var(--text-xs);
    font-weight: 600;
    font-family: var(--font-sans);
    transition: background 0.15s;
}
.chat-confirm-approve {
    background: var(--success);
    color: #fff;
}
.chat-confirm-approve:hover {
    background: #047857;
}
.chat-confirm-reject {
    background: var(--error);
    color: #fff;
}
.chat-confirm-reject:hover {
    background: #B91C1C;
}
.chat-confirm-card.resolved {
    opacity: 0.6;
    pointer-events: none;
}
.chat-confirm-pin-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 8px 0 4px;
    direction: rtl;
}
.chat-confirm-pin-label {
    font-size: 12px;
    color: var(--text-secondary, #9CA3AF);
    white-space: nowrap;
}
.chat-confirm-pin-input {
    flex: 1;
    max-width: 120px;
    padding: 4px 8px;
    border: 1px solid var(--border-color, #374151);
    border-radius: 6px;
    background: var(--bg-primary, #111827);
    color: var(--text-primary, #F9FAFB);
    font-size: 14px;
    text-align: center;
    letter-spacing: 3px;
    outline: none;
}
.chat-confirm-pin-input:focus {
    border-color: #3B82F6;
    box-shadow: 0 0 0 2px rgba(59,130,246,0.3);
}
.chat-confirm-pin-error {
    font-size: 12px;
    color: #EF4444;
    text-align: right;
    direction: rtl;
    margin: 2px 0 4px;
}

/* ---- Typing Indicator ---- */
.chat-typing {
    align-self: flex-start;
    display: flex;
    gap: 4px;
    padding: var(--space-2) var(--space-3);
}
.chat-typing-dot {
    width: 6px;
    height: 6px;
    border-radius: var(--radius-full);
    background: var(--text-tertiary);
    animation: chat-bounce 1.2s infinite;
}
.chat-typing-dot:nth-child(2) { animation-delay: 0.15s; }
.chat-typing-dot:nth-child(3) { animation-delay: 0.3s; }
@keyframes chat-bounce {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-4px); }
}

/* ---- Input Area ---- */
.chat-input-area {
    display: flex;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-4);
    border-top: 1px solid var(--border-secondary);
    background: var(--bg-surface);
    flex-shrink: 0;
}
.chat-input {
    flex: 1;
    border: 1px solid var(--border-primary);
    border-radius: var(--radius-md);
    padding: var(--space-2) var(--space-3);
    font-family: var(--font-sans);
    font-size: var(--text-sm);
    color: var(--text-primary);
    background: var(--bg-surface);
    resize: none;
    min-height: 38px;
    max-height: 100px;
    outline: none;
    transition: border-color 0.15s;
}
.chat-input:focus {
    border-color: var(--border-focus);
}
.chat-input::placeholder {
    color: var(--text-tertiary);
}
.chat-send-btn {
    width: 38px;
    height: 38px;
    border-radius: var(--radius-md);
    background: var(--accent-primary);
    color: #fff;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s;
}
.chat-send-btn:hover {
    background: var(--accent-primary-hover);
}
.chat-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}
.chat-send-btn svg {
    width: 18px;
    height: 18px;
    fill: currentColor;
}

/* ---- Empty State ---- */
.chat-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    color: var(--text-tertiary);
    font-family: var(--font-sans);
    font-size: var(--text-sm);
    text-align: center;
    padding: var(--space-8);
    gap: var(--space-3);
}
.chat-empty-icon {
    font-size: 32px;
    opacity: 0.5;
}

/* ---- Overlay backdrop (click to close) ---- */
.chat-backdrop {
    position: fixed;
    inset: 0;
    z-index: 799;
    background: rgba(0, 0, 0, 0.15);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s;
}
.chat-backdrop.visible {
    opacity: 1;
    pointer-events: auto;
}

/* ---- Responsive ---- */
@media (max-width: 768px) {
    .chat-side-panel {
        width: 100vw;
    }
}
@media (min-width: 769px) and (max-width: 1100px) {
    .chat-side-panel {
        width: 340px;
    }
}

/* ============================================
   Chat History Panel
   ============================================ */
.chat-history-panel {
    display: none;
    flex-direction: column;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--bg-surface);
    z-index: 10;
    overflow: hidden;
}
.chat-history-panel.visible {
    display: flex;
}

/* History Header */
.chat-history-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--border-secondary);
    background: var(--bg-surface-alt);
    min-height: 56px;
    flex-shrink: 0;
}
.chat-history-title {
    font-family: var(--font-sans);
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--text-primary);
}
.chat-history-new-btn {
    background: none;
    border: 1px solid var(--border-primary);
    color: var(--accent-primary);
    cursor: pointer;
    font-size: var(--text-xs);
    font-weight: 600;
    font-family: var(--font-sans);
    padding: var(--space-1) var(--space-2);
    border-radius: var(--radius-sm);
    transition: all 0.15s;
    white-space: nowrap;
}
.chat-history-new-btn:hover {
    background: var(--accent-primary);
    color: #fff;
    border-color: var(--accent-primary);
}

/* Super Admin Filter */
.chat-history-filter {
    padding: var(--space-2) var(--space-4);
    border-bottom: 1px solid var(--border-secondary);
    flex-shrink: 0;
}
.chat-history-filter-select {
    width: 100%;
    padding: var(--space-1) var(--space-2);
    border: 1px solid var(--border-primary);
    border-radius: var(--radius-sm);
    background: var(--bg-surface);
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: var(--text-xs);
    cursor: pointer;
    outline: none;
    direction: rtl;
}
.chat-history-filter-select:focus {
    border-color: var(--border-focus);
}

/* History List */
.chat-history-list {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-2) 0;
}

/* Empty State */
.chat-history-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--space-8) var(--space-4);
    color: var(--text-tertiary);
    font-family: var(--font-sans);
    font-size: var(--text-sm);
    text-align: center;
    gap: var(--space-2);
}

/* History Item */
.chat-history-item {
    display: flex;
    align-items: flex-start;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-4);
    cursor: pointer;
    transition: background 0.12s;
    border-bottom: 1px solid var(--border-secondary);
    position: relative;
}
.chat-history-item:last-child {
    border-bottom: none;
}
.chat-history-item:hover {
    background: var(--bg-surface-alt);
}
.chat-history-item.active {
    background: var(--accent-primary-light);
    border-inline-start: 3px solid var(--accent-primary);
}

/* Item Content */
.chat-history-item-content {
    flex: 1;
    min-width: 0;
}
.chat-history-item-title {
    font-family: var(--font-sans);
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    direction: rtl;
    text-align: right;
}
.chat-history-item-meta {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    margin-top: 2px;
    font-family: var(--font-sans);
    font-size: var(--text-xs);
    color: var(--text-tertiary);
    direction: rtl;
}

/* Super Admin - User Badge */
.chat-history-user-badge {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    padding: 1px 6px;
    border-radius: var(--radius-full);
    background: rgba(139, 92, 246, 0.12);
    color: #A78BFA;
    font-size: 10px;
    font-weight: 500;
    white-space: nowrap;
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Delete Button */
.chat-history-delete {
    background: none;
    border: none;
    color: var(--text-tertiary);
    cursor: pointer;
    padding: 4px;
    border-radius: var(--radius-sm);
    opacity: 0;
    transition: all 0.15s;
    flex-shrink: 0;
    font-family: var(--font-sans);
    font-size: var(--text-xs);
    align-self: center;
}
.chat-history-item:hover .chat-history-delete {
    opacity: 1;
}
.chat-history-delete:hover {
    color: var(--error);
    background: var(--error-bg);
}
.chat-history-item.confirming .chat-history-delete {
    opacity: 1;
    color: var(--error);
    background: var(--error-bg);
    font-weight: 600;
}

/* Load More Button */
.chat-history-more {
    display: block;
    width: calc(100% - var(--space-8));
    margin: var(--space-2) var(--space-4);
    padding: var(--space-2);
    border: 1px solid var(--border-primary);
    border-radius: var(--radius-sm);
    background: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-family: var(--font-sans);
    font-size: var(--text-xs);
    text-align: center;
    transition: all 0.15s;
}
.chat-history-more:hover {
    background: var(--bg-surface-alt);
    color: var(--text-primary);
}

/* History Toggle Active State */
.chat-header-btn.active {
    color: var(--accent-primary);
    background: var(--accent-primary-light);
}
