/**
 * Savian AI Chat Widget Styles
 * Responsive, accessible, follows Moodle/Bootstrap patterns
 *
 * @package    local_savian_ai
 * @copyright  2025 Savian AI
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

/* ==========================================
   FLOATING WIDGET BASE
   ========================================== */

.savian-chat-widget {
    position: fixed;
    z-index: 9999;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

/* Positioning variants */
.savian-widget-bottom-right {
    bottom: 20px;
    right: 20px;
}

.savian-widget-bottom-left {
    bottom: 20px;
    left: 20px;
}

/* ==========================================
   MINIMIZED BUBBLE
   ========================================== */

.savian-chat-bubble {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, #6C3BAA 0%, #8B5AC8 100%);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(108, 59, 170, 0.3);
    transition: all 0.3s ease;
}

.savian-chat-bubble:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 16px rgba(108, 59, 170, 0.4);
}

.savian-chat-bubble:focus {
    outline: 2px solid #6C3BAA;
    outline-offset: 2px;
}

/* Notification badge */
.savian-chat-bubble .notification-badge {
    position: absolute;
    top: -5px;
    right: -5px;
    background: #dc3545;
    color: white;
    border-radius: 10px;
    padding: 2px 6px;
    font-size: 11px;
    font-weight: bold;
}

.savian-chat-bubble .notification-badge.hidden {
    display: none;
}

/* Show/hide based on state */
.savian-chat-widget.minimized .savian-chat-window {
    display: none;
}

.savian-chat-widget.minimized .savian-chat-bubble {
    display: flex;
}

.savian-chat-widget.maximized .savian-chat-bubble {
    display: none;
}

.savian-chat-widget.maximized .savian-chat-window {
    display: flex;
}

/* ==========================================
   FULLSCREEN MODE
   ========================================== */

.savian-chat-widget.fullscreen {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    right: auto;
    bottom: auto;
    z-index: 10000;
}

.savian-chat-widget.fullscreen .savian-chat-window {
    width: 90vw;
    max-width: 1200px;
    height: 85vh;
    max-height: 800px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    animation: zoomIn 0.3s ease;
}

@keyframes zoomIn {
    from {
        opacity: 0;
        transform: scale(0.95);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* Backdrop overlay */
.savian-chat-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 9999;
    animation: fadeIn 0.3s ease;
}

/* ==========================================
   MAXIMIZED CHAT WINDOW
   ========================================== */

.savian-chat-window {
    width: 380px;
    height: 600px;
    max-height: calc(100vh - 100px);
    background: white;
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: slideUp 0.3s ease;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ==========================================
   CHAT HEADER
   ========================================== */

.savian-chat-header {
    background: linear-gradient(135deg, #6C3BAA 0%, #8B5AC8 100%);
    color: white;
    padding: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
}

.savian-chat-header .chat-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    font-size: 16px;
}

.savian-chat-header .chat-actions {
    display: flex;
    gap: 8px;
}

.savian-chat-header .btn-icon {
    background: transparent;
    border: none;
    color: white;
    cursor: pointer;
    padding: 6px;
    border-radius: 4px;
    transition: background 0.2s;
    font-size: 14px;
}

.savian-chat-header .btn-icon:hover {
    background: rgba(255, 255, 255, 0.2);
}

.savian-chat-header .btn-icon:focus {
    outline: 2px solid white;
    outline-offset: 2px;
}

/* ==========================================
   CHAT BODY (Messages)
   ========================================== */

.savian-chat-body {
    flex: 1;
    overflow-y: auto;
    background: #f5f5f5;
    display: flex;
    flex-direction: column;
}

.savian-chat-messages {
    flex: 1;
    padding: 16px;
    overflow-y: auto;
}

/* Message bubbles */
.chat-message {
    margin-bottom: 16px;
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.chat-message.user {
    text-align: right;
}

.chat-message.assistant {
    text-align: left;
}

.message-content {
    display: inline-block;
    max-width: 80%;
    padding: 12px 16px;
    border-radius: 12px;
    word-wrap: break-word;
    text-align: left;
    line-height: 1.5;
}

.chat-message.user .message-content {
    background: #6C3BAA;
    color: white;
    border-bottom-right-radius: 4px;
}

.chat-message.assistant .message-content {
    background: white;
    color: #333;
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

/* Code blocks */
.message-content pre {
    background: #1e1e1e;
    color: #d4d4d4;
    padding: 12px;
    border-radius: 6px;
    overflow-x: auto;
    margin: 8px 0;
}

.message-content code {
    font-family: 'Courier New', 'Consolas', monospace;
    font-size: 13px;
}

.message-content :not(pre) > code {
    background: rgba(0, 0, 0, 0.1);
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 90%;
}

.chat-message.user .message-content :not(pre) > code {
    background: rgba(255, 255, 255, 0.2);
}

/* LaTeX math - ensure proper spacing */
.message-content .katex,
.message-content .MathJax {
    font-size: 1.1em;
}

/* Message metadata */
.message-time {
    font-size: 11px;
    color: #999;
    margin-top: 4px;
}

.chat-message.user .message-time {
    text-align: right;
}

/* Sources */
.message-sources {
    margin-top: 8px;
    font-size: 12px;
    color: #666;
    padding-top: 8px;
    border-top: 1px solid rgba(0, 0, 0, 0.1);
}

.source-badge {
    background: #e9ecef;
    padding: 2px 8px;
    border-radius: 12px;
    margin-right: 4px;
    display: inline-block;
    font-size: 11px;
}

/* Feedback buttons */
.message-feedback {
    margin-top: 8px;
    display: inline-flex;
    gap: 8px;
}

.btn-feedback {
    background: transparent;
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 4px 8px;
    cursor: pointer;
    transition: all 0.2s;
    color: #666;
    font-size: 12px;
}

.btn-feedback:hover {
    background: #f0f0f0;
    border-color: #6C3BAA;
    color: #6C3BAA;
}

.btn-feedback:focus {
    outline: 2px solid #6C3BAA;
    outline-offset: 1px;
}

.btn-feedback.active {
    background: #6C3BAA;
    border-color: #6C3BAA;
    color: white;
}

/* Typing indicator */
.typing-indicator .message-content {
    padding: 12px 20px;
    background: white;
}

.typing-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #999;
    margin: 0 2px;
    animation: typing 1.4s infinite;
}

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

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

@keyframes typing {
    0%, 60%, 100% {
        opacity: 0.3;
        transform: translateY(0);
    }
    30% {
        opacity: 1;
        transform: translateY(-8px);
    }
}

/* ==========================================
   DOCUMENT SELECTOR (Teachers)
   ========================================== */

.savian-document-selector {
    padding: 12px 16px;
    background: white;
    border-top: 1px solid #ddd;
    flex-shrink: 0;
}

.savian-document-selector label {
    font-size: 13px;
    font-weight: 600;
    color: #333;
    display: block;
    margin-bottom: 6px;
}

.savian-document-selector select {
    width: 100%;
    max-height: 80px;
    font-size: 13px;
}

/* ==========================================
   CHAT FOOTER (Input)
   ========================================== */

.savian-chat-footer {
    background: white;
    border-top: 1px solid #ddd;
    padding: 12px 16px;
    flex-shrink: 0;
}

.chat-input-wrapper {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

.chat-input {
    flex: 1;
    border: 1px solid #ddd;
    border-radius: 20px;
    padding: 10px 16px;
    font-size: 14px;
    resize: none;
    max-height: 120px;
    overflow-y: auto;
    transition: border-color 0.2s;
    font-family: inherit;
}

.chat-input:focus {
    outline: none;
    border-color: #6C3BAA;
    box-shadow: 0 0 0 3px rgba(108, 59, 170, 0.1);
}

.chat-input:disabled {
    background: #f5f5f5;
    cursor: not-allowed;
}

.btn-send {
    background: #6C3BAA;
    color: white;
    border: none;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    flex-shrink: 0;
}

.btn-send:hover:not(:disabled) {
    background: #5a2f8f;
    transform: scale(1.05);
}

.btn-send:disabled {
    background: #ccc;
    cursor: not-allowed;
}

.btn-send:focus {
    outline: 2px solid #6C3BAA;
    outline-offset: 2px;
}

.chat-status {
    font-size: 11px;
    color: #999;
    margin-top: 6px;
    min-height: 14px;
}

/* ==========================================
   SCROLLBAR STYLING
   ========================================== */

.savian-chat-messages::-webkit-scrollbar {
    width: 6px;
}

.savian-chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.savian-chat-messages::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 3px;
}

.savian-chat-messages::-webkit-scrollbar-thumb:hover {
    background: #999;
}

/* ==========================================
   FULL-PAGE CHAT INTERFACE
   ========================================== */

.savian-chat-fullpage {
    display: flex;
    height: calc(100vh - 200px);
    min-height: 500px;
    gap: 20px;
}

.chat-sidebar {
    width: 300px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
}

.sidebar-header {
    padding: 16px;
    border-bottom: 1px solid #e0e0e0;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.sidebar-header h4 {
    margin: 0;
    font-size: 16px;
}

.conversation-list {
    flex: 1;
    overflow-y: auto;
}

.conversation-item {
    padding: 12px 16px;
    border-bottom: 1px solid #e0e0e0;
    cursor: pointer;
    transition: background 0.2s;
}

.conversation-item:hover {
    background: #f5f5f5;
}

.conversation-item.active {
    background: #f0e6ff;
    border-left: 4px solid #6C3BAA;
}

.conv-title {
    font-weight: 600;
    font-size: 14px;
    margin-bottom: 4px;
}

.conv-meta {
    color: #999;
    font-size: 12px;
}

.chat-main {
    flex: 1;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chat-messages-container {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    background: #f5f5f5;
}

.chat-input-area {
    padding: 16px;
    border-top: 1px solid #e0e0e0;
    background: white;
}

/* ==========================================
   CHAT HISTORY VIEWER
   ========================================== */

.savian-chat-history-viewer .card {
    margin-bottom: 20px;
}

.savian-chat-history-viewer .stat-card {
    text-align: center;
}

#conversations-table th {
    font-weight: 600;
    background: #f5f5f5;
}

.view-conversation {
    white-space: nowrap;
}

/* ==========================================
   RESPONSIVE (Mobile)
   ========================================== */

@media (max-width: 768px) {
    .savian-chat-window {
        width: calc(100vw - 32px);
        height: calc(100vh - 100px);
        max-height: none;
        border-radius: 12px 12px 0 0;
        bottom: 0;
        right: 0;
        left: 0;
    }

    .savian-widget-bottom-right,
    .savian-widget-bottom-left {
        right: 16px;
        left: auto;
        bottom: 16px;
    }

    .message-content {
        max-width: 90%;
    }

    .savian-chat-fullpage {
        flex-direction: column;
        height: auto;
    }

    .chat-sidebar {
        width: 100%;
        max-height: 300px;
    }

    .chat-main {
        height: 500px;
    }

    /* Fullscreen on mobile takes full viewport */
    .savian-chat-widget.fullscreen .savian-chat-window {
        width: 100vw;
        height: 100vh;
        max-width: none;
        max-height: none;
        border-radius: 0;
    }
}

/* ==========================================
   ACCESSIBILITY
   ========================================== */

/* High contrast mode */
@media (prefers-contrast: high) {
    .savian-chat-bubble {
        border: 2px solid white;
    }

    .message-content {
        border: 1px solid currentColor;
    }

    .btn-feedback {
        border-width: 2px;
    }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
    .savian-chat-window,
    .chat-message,
    .typing-dot,
    .savian-chat-bubble {
        animation: none !important;
    }

    * {
        transition: none !important;
    }
}

/* Focus visible for keyboard navigation */
.chat-input:focus-visible,
.btn-send:focus-visible,
.btn-icon:focus-visible,
.btn-feedback:focus-visible {
    outline: 2px solid #6C3BAA;
    outline-offset: 2px;
}

/* ==========================================
   SIZE VARIANTS (Admin Configurable)
   ========================================== */

.savian-chat-widget.size-small .savian-chat-window {
    width: 320px;
    height: 500px;
}

.savian-chat-widget.size-medium .savian-chat-window {
    width: 380px;
    height: 600px;
}

.savian-chat-widget.size-large .savian-chat-window {
    width: 450px;
    height: 700px;
}

/* ==========================================
   DARK MODE SUPPORT (Future Enhancement)
   ========================================== */

@media (prefers-color-scheme: dark) {
    .savian-chat-window {
        background: #2d2d2d;
        color: #e0e0e0;
    }

    .savian-chat-body {
        background: #1e1e1e;
    }

    .savian-chat-messages {
        background: #1e1e1e;
    }

    .chat-message.assistant .message-content {
        background: #3a3a3a;
        color: #e0e0e0;
    }

    .chat-input {
        background: #3a3a3a;
        border-color: #555;
        color: #e0e0e0;
    }

    .savian-chat-footer {
        background: #2d2d2d;
        border-color: #555;
    }

    .savian-document-selector {
        background: #2d2d2d;
        border-color: #555;
    }

    .savian-document-selector label {
        color: #e0e0e0;
    }

    .source-badge {
        background: #3a3a3a;
        color: #e0e0e0;
    }
}

/* ==========================================
   PRINT STYLES
   ========================================== */

@media print {
    .savian-chat-widget {
        display: none !important;
    }
}
