/* ===== AI ASSISTANT WIDGET ===== */

/* --- Floating Bubble (Sidekick) ---
   Anchored at the natural bottom-right corner (24px). Bug stacks above at
   96px. Sidekick is the more prominent everyday surface, so it gets the
   corner anchor; Bug perches above as a secondary affordance.            */
.ai-bubble {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: linear-gradient(135deg, #059669 0%, #047857 100%);
    border: none;
    color: white;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12), 0 1px 3px rgba(0, 0, 0, 0.08);
    z-index: 9999;
    transition: transform 0.15s ease, box-shadow 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    line-height: 1;
}
.ai-bubble:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 18px rgba(0, 0, 0, 0.16), 0 2px 4px rgba(0, 0, 0, 0.1);
}
.ai-bubble:active {
    transform: translateY(0);
}
.ai-bubble.open {
    background: #374151;
}

/* Bubble icon swap — SVG by default, × glyph when panel is open */
.ai-bubble .ai-bubble-icon-open { display: inline-flex; align-items: center; justify-content: center; }
.ai-bubble .ai-bubble-icon-close { display: none; color: white; font-size: 22px; }
.ai-bubble.open .ai-bubble-icon-open { display: none; }
.ai-bubble.open .ai-bubble-icon-close { display: inline; }
.ai-bubble .ai-bubble-svg { width: 30px; height: 30px; display: block; }

/* Subtle entrance pulse on page load */
@keyframes ai-bubble-entrance {
    0% { transform: scale(0); opacity: 0; }
    60% { transform: scale(1.12); opacity: 1; }
    100% { transform: scale(1); opacity: 1; }
}
.ai-bubble { animation: ai-bubble-entrance 0.4s ease-out; }

/* --- Chat Panel ---
   Anchored above the Sidekick bubble (bubble top edge ≈ 80px from screen
   bottom; +12px breathing room → 92px).                                  */
.ai-panel {
    position: fixed;
    bottom: 92px;
    right: 24px;
    width: 380px;
    height: 520px;
    background: white;
    border-radius: 16px;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.18);
    display: flex;
    flex-direction: column;
    z-index: 9998;
    opacity: 0;
    transform: translateY(16px) scale(0.96);
    pointer-events: none;
    transition: opacity 0.25s ease, transform 0.25s ease;
    overflow: hidden;
}
.ai-panel.open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

/* --- Sidebar layout mode (Phase B v2) ---
   Right-docked, full-height under the global topbar. Hard-edged column
   look — square corners, neutral left border, no soft shadow — so it
   reads as part of the page chrome instead of a floating panel.
   Triggered by the tc-sidekick-mode-sidebar body class.

   !important on the positional rules is intentional: the base .ai-panel
   block sets bottom/right/width/height with stronger cascade precedence
   under some conditions (e.g. when the .open class's transform rule
   re-asserts layout context). Locking these defeats the fight.        */
body.tc-sidekick-mode-sidebar .ai-panel {
    /* inset shorthand (top right bottom left) — single declaration the
       base rule's individual bottom: 92px / right: 24px CAN'T poke
       through because the shorthand replaces all four anchors atomically. */
    inset: 48px 0 auto auto !important;
    width: 380px !important;
    /* shared/layout.css sets `body { zoom: 0.75 }` for global chrome
       density. position:fixed children inherit zoom, so vh-based heights
       render at 75% of their CSS value — leaving a ~25% gap at viewport
       bottom. The effective topbar height is 48px CSS × 0.75 = 36px
       actual, so the panel needs effective height = 100vh - 36px, i.e.
       CSS height = (100vh - 36px) / 0.75 = 133.333vh - 48px. JS in
       _applyPanelInlineDock re-reads the actual body zoom and overrides
       this on toggle + window resize so the fallback can be wrong with
       no visible consequence.                                        */
    height: calc(133.333vh - 48px) !important;
    min-height: calc(133.333vh - 48px) !important;
    max-height: none !important;
    border-radius: 0 !important;
    border-left: 1px solid #d1d5db !important;
    box-shadow: none !important;
    transform: translateX(16px);
}
body.tc-sidekick-mode-sidebar .ai-panel.open {
    transform: translateX(0) !important;
}
/* Hide the bubble when the sidebar is up — the docked panel replaces it. */
body.tc-sidekick-mode-sidebar.tc-sidekick-panel-open .ai-bubble {
    display: none !important;
}
/* Squared panel header — no rounded top corners now that the panel
   touches the global topbar's bottom edge.                            */
body.tc-sidekick-mode-sidebar .ai-panel-header {
    border-radius: 0;
}

/* 2026-06-02 — modal grayout removed. Sidekick now stays fully bright
   and interactive while any modal is open so the teacher can chat with
   the assistant while a lesson / library / unit modal is up. The
   body.tc-modal-open class is still toggled by the MutationObserver for
   downstream listeners (e.g., sk-agent overlays that need to know when
   a modal is up — see shared/sidekick-agent.css). */

/* Page content reflow (Phase C v2, post-correction).
   Teacher pages: .main-content is position:fixed with right:0, so body
   padding is ignored. Override its right: edge to the sidebar's width
   so the content area resizes; its existing align-items:center then
   re-centers the inner card naturally.
   Admin pages: .sa-content / .adm-content / .co-content / .su-content
   use normal flow → max-width + auto margins centers them.
   We also set padding-right: 380px on body for any normal-flow elements
   that DON'T use fixed positioning (covers stray banners, footers).  */
body.tc-sidekick-mode-sidebar.tc-sidekick-panel-open {
    padding-right: 380px;
    transition: padding-right 0.25s ease;
}
body.tc-sidekick-mode-sidebar.tc-sidekick-panel-open .main-content {
    right: 380px;
    transition: right 0.25s ease;
}
body.tc-sidekick-mode-sidebar.tc-sidekick-panel-open .sa-content,
body.tc-sidekick-mode-sidebar.tc-sidekick-panel-open .adm-content,
body.tc-sidekick-mode-sidebar.tc-sidekick-panel-open .co-content,
body.tc-sidekick-mode-sidebar.tc-sidekick-panel-open .su-content {
    max-width: 1200px;
    margin-left: auto;
    margin-right: auto;
}

/* --- Panel Header --- */
.ai-panel-header {
    display: flex;
    align-items: center;
    padding: 14px 16px;
    background: linear-gradient(135deg, #059669 0%, #047857 100%);
    color: white;
    flex-shrink: 0;
}
.ai-panel-header-icon {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255,255,255,0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    margin-right: 10px;
    flex-shrink: 0;
}
.ai-panel-header-icon .ai-panel-header-svg { width: 22px; height: 22px; display: block; }
.ai-panel-header-text { flex: 1; }
.ai-panel-title {
    font-size: 14px;
    font-weight: 700;
    letter-spacing: -0.2px;
}
.ai-panel-status {
    font-size: 11px;
    opacity: 0.8;
    margin-top: 1px;
}
.ai-panel-close {
    background: none;
    border: none;
    color: white;
    font-size: 22px;
    cursor: pointer;
    opacity: 0.7;
    transition: opacity 0.15s;
    padding: 0 4px;
    line-height: 1;
}
.ai-panel-close:hover { opacity: 1; }
.ai-panel-prefs {
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    opacity: 0.7;
    transition: opacity 0.15s;
    padding: 0 4px;
    line-height: 1;
    display: flex;
    align-items: center;
}
.ai-panel-prefs:hover { opacity: 1; }
.ai-panel-prefs svg { display: block; }
.ai-panel-reset {
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    opacity: 0.7;
    transition: opacity 0.15s;
    padding: 0 4px;
    line-height: 1;
    display: flex;
    align-items: center;
}
.ai-panel-reset:hover { opacity: 1; }
.ai-panel-reset svg { display: block; }

/* --- Layout toggle (Phase A v2) --- */
.ai-panel-layout-wrap {
    position: relative;
    display: inline-flex;
    align-items: center;
}
.ai-panel-layout {
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    opacity: 0.7;
    transition: opacity 0.15s;
    padding: 0 4px;
    line-height: 1;
    display: flex;
    align-items: center;
}
.ai-panel-layout:hover { opacity: 1; }
.ai-panel-layout svg { display: block; }

.ai-panel-layout-menu {
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    background: #ffffff;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
    padding: 4px;
    min-width: 150px;
    z-index: 10001;     /* above the panel header itself */
}
.ai-panel-layout-menu[hidden] { display: none; }

.ai-panel-layout-item {
    width: 100%;
    background: none;
    border: none;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 7px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    color: #1f2937;
    text-align: left;
    transition: background 0.12s ease;
}
.ai-panel-layout-item:hover { background: #f3f4f6; }
.ai-panel-layout-item-icon {
    display: inline-flex;
    width: 18px;
    height: 18px;
    color: #4b5563;
    align-items: center;
    justify-content: center;
}
.ai-panel-layout-item-label { flex: 1; }
.ai-panel-layout-item-check {
    color: #059669;
    font-weight: 700;
    opacity: 0;
}
.ai-panel-layout-item.active .ai-panel-layout-item-check { opacity: 1; }
.ai-panel-layout-item.active .ai-panel-layout-item-icon { color: #059669; }

/* --- Messages Area --- */
.ai-panel-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    min-height: 0;
    background: #f8f9fa;
}
.ai-panel-messages::-webkit-scrollbar { width: 5px; }
.ai-panel-messages::-webkit-scrollbar-track { background: transparent; }
.ai-panel-messages::-webkit-scrollbar-thumb { background: #d0d0d0; border-radius: 3px; }

/* --- Message Bubbles --- */
.ai-msg {
    max-width: 85%;
    padding: 10px 14px;
    font-size: 13px;
    line-height: 1.55;
    word-wrap: break-word;
    animation: ai-msg-in 0.2s ease-out;
}
@keyframes ai-msg-in {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.ai-msg.assistant {
    align-self: flex-start;
    background: white;
    border: 1px solid #e5e7eb;
    border-radius: 14px 14px 14px 4px;
    color: #333;
}
.ai-msg.user {
    align-self: flex-end;
    background: #059669;
    color: white;
    border-radius: 14px 14px 4px 14px;
}

/* --- Streaming dots animation --- */
.ai-msg.ai-streaming::after {
    content: ' ...';
    display: inline-block;
    width: 0;
    overflow: hidden;
    vertical-align: bottom;
    color: #059669;
    font-weight: bold;
    letter-spacing: 2px;
    animation: ai-dots 1.2s steps(4, end) infinite;
}
@keyframes ai-dots {
    0% { width: 0; }
    100% { width: 1.5em; }
}

/* --- Typing Indicator --- */
.ai-msg.typing {
    align-self: flex-start;
    background: white;
    border: 1px solid #e5e7eb;
    border-radius: 14px 14px 14px 4px;
    padding: 12px 18px;
    display: flex;
    gap: 5px;
    align-items: center;
}
.ai-typing-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #9ca3af;
    animation: ai-typing-bounce 1.4s ease-in-out infinite;
}
.ai-typing-dot:nth-child(2) { animation-delay: 0.2s; }
.ai-typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes ai-typing-bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-6px); opacity: 1; }
}

/* Thinking indicator with text + dots */
.ai-thinking-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
}

.ai-thinking-text {
    font-size: 13px;
    color: #888;
    font-weight: 500;
}

.ai-thinking-dots {
    display: inline-flex;
    flex-direction: row;
    align-items: center;
    gap: 3px;
}

.ai-thinking-dots span {
    display: inline-block;
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: #059669;
    animation: ai-dot-pulse 1.4s ease-in-out infinite;
}

.ai-thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.ai-thinking-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes ai-dot-pulse {
    0%, 80%, 100% { opacity: 0.3; }
    40% { opacity: 1; }
}

/* --- Action Feedback Badges --- */
.ai-action-badge {
    align-self: center;
    background: #ecfdf5;
    color: #059669;
    font-size: 11px;
    font-weight: 600;
    padding: 4px 14px;
    border-radius: 10px;
    border: 1px solid #d1fae5;
    display: flex;
    align-items: center;
    gap: 5px;
    animation: ai-msg-in 0.2s ease-out;
}
.ai-action-badge-icon { font-size: 13px; }

/* --- File Preview Area --- */
.ai-file-preview {
    display: none;
    flex-wrap: wrap;
    gap: 6px;
    padding: 8px 12px;
    background: #f0fdf4;
    border-top: 1px solid #d1fae5;
    flex-shrink: 0;
}
.ai-file-chip {
    display: flex;
    align-items: center;
    gap: 5px;
    background: white;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    padding: 4px 8px;
    font-size: 11px;
    max-width: 160px;
}
.ai-file-chip-thumb {
    width: 24px;
    height: 24px;
    object-fit: cover;
    border-radius: 3px;
}
.ai-file-chip-icon { font-size: 16px; }
.ai-file-chip-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: #333;
}
.ai-file-chip-remove {
    background: none;
    border: none;
    color: #999;
    cursor: pointer;
    font-size: 14px;
    padding: 0 2px;
    line-height: 1;
}
.ai-file-chip-remove:hover { color: #333; }

/* --- Chat Attachments in Messages --- */
.ai-chat-attachments {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 6px;
}
.ai-chat-attachment {
    display: flex;
    align-items: center;
    gap: 4px;
    background: rgba(255,255,255,0.2);
    border-radius: 4px;
    padding: 3px 6px;
    font-size: 11px;
}
.ai-chat-attachment-img {
    width: 40px;
    height: 40px;
    object-fit: cover;
    border-radius: 4px;
}
.ai-chat-attachment-icon { font-size: 18px; }
.ai-chat-attachment-name {
    max-width: 80px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* --- Input Area --- */
.ai-panel-input-area {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    border-top: 1px solid #e5e7eb;
    background: white;
    gap: 8px;
    flex-shrink: 0;
}
.ai-panel-input-row {
    flex: 1;
    display: flex;
    align-items: center;
    border: 1px solid #e0e0e0;
    border-radius: 20px;
    padding: 2px 4px 2px 12px;
    transition: border-color 0.15s;
    background: white;
}
.ai-panel-input-row:focus-within {
    border-color: #059669;
}
.ai-panel-input {
    flex: 1;
    border: none;
    padding: 7px 4px;
    font-size: 13px;
    outline: none;
    font-family: inherit;
    background: transparent;
    min-width: 0;
}
.ai-panel-input::placeholder {
    color: #9ca3af;
}
.ai-panel-upload-btn,
.ai-panel-mic-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 16px;
    padding: 4px 5px;
    border-radius: 50%;
    color: #666;
    transition: background 0.15s, color 0.15s;
    flex-shrink: 0;
    line-height: 1;
}
.ai-panel-upload-btn:hover,
.ai-panel-mic-btn:hover {
    background: #f0f0f0;
    color: #333;
}
.ai-panel-mic-btn.recording {
    color: #ef4444;
    animation: ai-mic-pulse 1s ease-in-out infinite;
}
@keyframes ai-mic-pulse {
    0% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.5); }
    70% { box-shadow: 0 0 0 6px rgba(239, 68, 68, 0); }
    100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); }
}
.ai-panel-send {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: #059669;
    border: none;
    color: white;
    font-size: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
    flex-shrink: 0;
}
.ai-panel-send:hover { background: #047857; }

/* --- Element Highlighting --- */
/* Single slow pulse instead of 3 fast ones — the user reported the old
   3× pulse looked like a frantic flash. Now the ring expands once, then
   the solid outline holds steady for the rest of the highlight window. */
@keyframes ai-highlight-pulse {
    0%   { box-shadow: 0 0 0 0 rgba(5, 150, 105, 0.55); }
    100% { box-shadow: 0 0 0 18px rgba(5, 150, 105, 0); }
}
.ai-highlighted {
    outline: 4px solid #059669 !important;
    outline-offset: 4px;
    border-radius: 4px;
    animation: ai-highlight-pulse 1.4s ease-out 1;
    position: relative;
    z-index: 1998;
    transition: outline 0.3s ease;
}

/* Spotlight overlay for dramatic highlights */
.ai-spotlight {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0, 0, 0, 0.3);
    z-index: 1997;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.4s ease;
}
.ai-spotlight.active {
    opacity: 1;
}

/* --- Google Drive Button --- */
.ai-panel-gdrive-btn {
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px 5px;
    border-radius: 50%;
    color: #666;
    transition: background 0.15s, color 0.15s;
    flex-shrink: 0;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}
.ai-panel-gdrive-btn:hover {
    background: #f0f0f0;
}
.ai-panel-gdrive-btn svg {
    width: 16px;
    height: 16px;
}

/* --- Google Drive Picker Panel --- */
.ai-gdrive-picker {
    display: none;
    position: absolute;
    bottom: 56px;
    left: 12px;
    right: 12px;
    background: white;
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.15);
    z-index: 10;
    max-height: 300px;
    flex-direction: column;
    overflow: hidden;
}
.ai-gdrive-picker.open {
    display: flex;
}
.ai-gdrive-picker-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    border-bottom: 1px solid #e5e7eb;
    background: #f8f9fa;
}
.ai-gdrive-picker-title {
    font-size: 13px;
    font-weight: 600;
    color: #333;
}
.ai-gdrive-picker-close {
    background: none;
    border: none;
    font-size: 18px;
    cursor: pointer;
    color: #999;
    line-height: 1;
    padding: 0 2px;
}
.ai-gdrive-picker-close:hover { color: #333; }
.ai-gdrive-picker-search {
    padding: 8px 12px;
    border-bottom: 1px solid #f0f0f0;
}
.ai-gdrive-picker-search input {
    width: 100%;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    padding: 6px 10px;
    font-size: 12px;
    outline: none;
    font-family: inherit;
}
.ai-gdrive-picker-search input:focus {
    border-color: #059669;
}
.ai-gdrive-picker-files {
    flex: 1;
    overflow-y: auto;
    max-height: 180px;
}
.ai-gdrive-picker-loading,
.ai-gdrive-picker-empty {
    padding: 20px;
    text-align: center;
    font-size: 12px;
    color: #999;
}
.ai-gdrive-file {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 14px;
    cursor: pointer;
    transition: background 0.1s;
    border-bottom: 1px solid #f5f5f5;
}
.ai-gdrive-file:hover {
    background: #f0fdf4;
}
.ai-gdrive-file.added {
    background: #ecfdf5;
    cursor: default;
}
.ai-gdrive-file-icon {
    font-size: 18px;
    flex-shrink: 0;
    width: 24px;
    text-align: center;
}
.ai-gdrive-file-info {
    flex: 1;
    min-width: 0;
}
.ai-gdrive-file-name {
    font-size: 12px;
    font-weight: 500;
    color: #333;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.ai-gdrive-file-meta {
    font-size: 10px;
    color: #999;
    margin-top: 1px;
}
.ai-gdrive-file-check {
    color: #059669;
    font-size: 14px;
    flex-shrink: 0;
}
.ai-gdrive-picker-status {
    padding: 4px 12px;
    font-size: 11px;
    color: #059669;
    min-height: 20px;
    text-align: center;
}

/* Drive file chip styling */
.ai-drive-chip {
    border-color: #d1fae5;
    background: #f0fdf4;
}
.ai-drive-attachment {
    background: rgba(5, 150, 105, 0.15);
}

/* --- New Message Indicator on Bubble --- */
.ai-bubble-badge {
    position: absolute;
    top: -2px;
    right: -2px;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: #ef4444;
    border: 2px solid white;
    display: none;
}
.ai-bubble-badge.show { display: block; }
