/**
 * TallyBook AI Dev Assistant & Screen Annotator (Development Mode Only)
 * High-performance Floating Trigger, Screen Markup Layer & Slide-Over Drawer
 */

/* 1. Floating Draggable Dev AI Trigger */
#tbAiDevTrigger {
    position: fixed;
    bottom: 85px;
    right: 18px;
    z-index: 99998;
    display: flex;
    align-items: center;
    gap: 8px;
    background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
    color: #ffffff;
    border: 1.5px solid #38bdf8;
    box-shadow: 0 4px 20px rgba(2, 132, 199, 0.35), 0 0 0 1px rgba(255, 255, 255, 0.1);
    border-radius: 28px;
    padding: 7px 13px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    font-size: 12px;
    font-weight: 600;
    cursor: grab;
    user-select: none;
    transition: box-shadow 0.2s, transform 0.15s;
}

#tbAiDevTrigger:active {
    cursor: grabbing;
}

#tbAiDevTrigger:hover {
    box-shadow: 0 6px 25px rgba(2, 132, 199, 0.5), 0 0 0 2px rgba(56, 189, 248, 0.3);
    transform: translateY(-1px);
}

#tbAiDevTrigger .tb-ai-dev-icon-wrap {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: linear-gradient(135deg, #0284c7, #38bdf8);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #ffffff;
    font-size: 11px;
    box-shadow: 0 2px 6px rgba(2, 132, 199, 0.4);
    position: relative;
}

#tbAiDevTrigger .tb-ai-dev-pulse {
    position: absolute;
    top: -2px;
    right: -2px;
    width: 8px;
    height: 8px;
    background: #10b981;
    border: 1.5px solid #0f172a;
    border-radius: 50%;
    animation: tbAiPulse 2s infinite;
}

@keyframes tbAiPulse {
    0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7); }
    70% { transform: scale(1.1); box-shadow: 0 0 0 6px rgba(16, 185, 129, 0); }
    100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(16, 185, 129, 0); }
}

#tbAiDevTrigger .tb-ai-dev-title {
    color: #f8fafc;
    letter-spacing: 0.2px;
}

#tbAiDevTrigger .tb-ai-dev-tag {
    background: #0369a1;
    color: #e0f2fe;
    font-size: 9.5px;
    font-weight: 700;
    padding: 1px 6px;
    border-radius: 8px;
    text-transform: uppercase;
    letter-spacing: 0.4px;
}

/* Quick Action Menu from Floating Button */
#tbAiDevActionMenu {
    position: fixed;
    z-index: 99999;
    display: none;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.18), 0 2px 8px rgba(0, 0, 0, 0.08);
    padding: 6px;
    min-width: 220px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    animation: tbAiFadeUp 0.18s ease-out;
}

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

.tb-ai-menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 12px;
    font-weight: 600;
    color: #1e293b;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}

.tb-ai-menu-item:hover {
    background: #f1f5f9;
    color: #0284c7;
}

.tb-ai-menu-item i {
    font-size: 14px;
    width: 18px;
    text-align: center;
}

.tb-ai-menu-divider {
    height: 1px;
    background: #e2e8f0;
    margin: 4px 0;
}

/* 2. Interactive Screen Annotator Layer */
#tbAiAnnotatorOverlay {
    position: fixed;
    inset: 0;
    z-index: 999999;
    display: none;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    background: rgba(15, 23, 42, 0.45);
    user-select: none;
    overflow: hidden;
}

/* Quick Close Button for Annotator (Always visible & tap-friendly) */
.tb-ai-annot-quick-close {
    position: fixed;
    top: 10px;
    right: 12px;
    z-index: 1000005;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: rgba(15, 23, 42, 0.88);
    border: 1.5px solid rgba(255, 255, 255, 0.3);
    color: #ffffff;
    font-size: 20px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.45);
    transition: background 0.15s, transform 0.15s;
}
.tb-ai-annot-quick-close:hover {
    background: #ef4444;
    transform: scale(1.06);
}

#tbAiAnnotatorCanvasWrap {
    position: absolute;
    inset: 0;
    overflow: hidden;
    cursor: crosshair;
}

#tbAiAnnotatorCanvas {
    position: absolute;
    top: 0;
    left: 0;
}

/* Annotator Scrollable Toolbar Wrapper */
#tbAiAnnotatorToolbarWrap {
    position: relative;
    z-index: 1000000;
    width: 100%;
    max-width: 100vw;
    display: flex;
    justify-content: center;
    padding: 10px 10px 6px;
    box-sizing: border-box;
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    scrollbar-color: rgba(255, 255, 255, 0.35) transparent;
}
#tbAiAnnotatorToolbarWrap::-webkit-scrollbar {
    height: 4px;
}
#tbAiAnnotatorToolbarWrap::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.35);
    border-radius: 4px;
}

/* Annotator Floating Toolbar */
#tbAiAnnotatorToolbar {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: rgba(15, 23, 42, 0.94);
    backdrop-filter: blur(12px);
    border: 1px solid rgba(255, 255, 255, 0.18);
    border-radius: 40px;
    padding: 6px 14px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    color: #ffffff;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    white-space: nowrap;
    flex-shrink: 0;
}

.tb-ai-tool-btn {
    background: none;
    border: none;
    color: #94a3b8;
    padding: 6px 10px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: all 0.15s;
    flex-shrink: 0;
}

.tb-ai-tool-btn:hover {
    color: #ffffff;
    background: rgba(255, 255, 255, 0.1);
}

.tb-ai-tool-btn.active {
    color: #ffffff;
    background: #0284c7;
}

.tb-ai-tool-btn i {
    font-size: 13px;
}

.tb-ai-tool-divider {
    width: 1px;
    height: 20px;
    background: rgba(255, 255, 255, 0.2);
    margin: 0 4px;
    flex-shrink: 0;
}

.tb-ai-color-dot {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    cursor: pointer;
    border: 2px solid transparent;
    transition: transform 0.15s, border-color 0.15s;
    flex-shrink: 0;
}

.tb-ai-color-dot.active {
    border-color: #ffffff;
    transform: scale(1.2);
}

.tb-ai-annot-cancel-btn {
    background: rgba(239, 68, 68, 0.2);
    color: #fca5a5;
    border: 1px solid rgba(239, 68, 68, 0.4);
    border-radius: 20px;
    padding: 5px 12px;
    font-size: 11.5px;
    font-weight: 600;
    cursor: pointer;
    flex-shrink: 0;
}

.tb-ai-annot-cancel-btn:hover {
    background: #ef4444;
    color: #ffffff;
}

.tb-ai-annot-done-btn {
    background: linear-gradient(135deg, #10b981, #059669);
    color: #ffffff;
    border: none;
    border-radius: 20px;
    padding: 6px 14px;
    font-size: 12px;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    box-shadow: 0 2px 8px rgba(16, 185, 129, 0.4);
    transition: transform 0.15s;
    flex-shrink: 0;
}

.tb-ai-annot-done-btn:hover {
    transform: scale(1.03);
}

/* 3. Slide-Over AI Dev Right Drawer */
#tbAiDevDrawerBackdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.5);
    backdrop-filter: blur(2px);
    z-index: 999990;
    display: none;
    opacity: 0;
    transition: opacity 0.25s ease-out;
}

#tbAiDevDrawerBackdrop.show {
    opacity: 1;
}

#tbAiDevDrawer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 600px;
    max-width: 100vw;
    background: #ffffff;
    z-index: 999995;
    box-shadow: -8px 0 30px rgba(0, 0, 0, 0.22);
    transform: translateX(100%);
    transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1), width 0.25s;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

#tbAiDevDrawer.open {
    transform: translateX(0);
}

#tbAiDevDrawer.expanded {
    width: 85vw;
}

/* Drawer Header */
.tb-ai-drawer-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    background: #0f172a;
    color: #ffffff;
    border-bottom: 1px solid #1e293b;
    gap: 10px;
}

.tb-ai-drawer-header-left {
    display: flex;
    align-items: center;
    gap: 9px;
    min-width: 0;
}

.tb-ai-drawer-title {
    font-size: 13.5px;
    font-weight: 700;
    color: #f8fafc;
    white-space: nowrap;
}

.tb-ai-tab-select {
    background: #1e293b;
    color: #38bdf8;
    border: 1px solid rgba(56, 189, 248, 0.35);
    border-radius: 6px;
    padding: 4px 8px;
    font-size: 11.5px;
    font-weight: 600;
    outline: none;
    max-width: 240px;
    cursor: pointer;
    font-family: inherit;
    transition: border-color 0.15s;
}
.tb-ai-tab-select:focus {
    border-color: #38bdf8;
}
.tb-ai-tab-select option {
    background: #0f172a;
    color: #f8fafc;
    padding: 4px;
}

.tb-ai-drawer-capture-btn {
    background: linear-gradient(135deg, #ef4444, #dc2626) !important;
    color: #ffffff !important;
    border: none !important;
    border-radius: 6px !important;
    padding: 3px 10px !important;
    font-size: 11px !important;
    font-weight: 700 !important;
    display: inline-flex !important;
    align-items: center !important;
    gap: 5px !important;
    cursor: pointer !important;
    width: auto !important;
    height: 28px !important;
    box-shadow: 0 2px 6px rgba(239, 68, 68, 0.35);
    transition: transform 0.15s, background 0.15s !important;
    white-space: nowrap;
}
.tb-ai-drawer-capture-btn:hover {
    transform: scale(1.03);
    background: #b91c1c !important;
    color: #ffffff !important;
}

.tb-ai-drawer-header-actions {
    display: flex;
    align-items: center;
    gap: 6px;
}

.tb-ai-drawer-header-actions button {
    background: none;
    border: none;
    color: #94a3b8;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}

.tb-ai-drawer-header-actions button:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #ffffff;
}

/* Drawer Body (Iframe) */
.tb-ai-drawer-body {
    flex: 1;
    position: relative;
    background: #f8fafc;
}

.tb-ai-drawer-body iframe {
    width: 100%;
    height: 100%;
    border: none;
    display: block;
}

.tb-ai-drawer-loading {
    position: absolute;
    inset: 0;
    background: #ffffff;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    color: #64748b;
    font-size: 13px;
    font-weight: 600;
    z-index: 10;
}

/* Mobile Responsiveness for Annotator & Slide-over Drawer */
@media (max-width: 600px) {
    #tbAiAnnotatorToolbarWrap {
        justify-content: flex-start;
        padding: 8px 50px 8px 8px; /* Space on right for quick close button */
    }
    #tbAiAnnotatorToolbar {
        padding: 5px 10px;
        gap: 5px;
        border-radius: 25px;
    }
    .tb-ai-tool-btn {
        padding: 5px 8px;
        font-size: 11px;
        gap: 4px;
    }
    .tb-ai-tool-btn span {
        font-size: 10px;
    }
    .tb-ai-annot-cancel-btn,
    .tb-ai-annot-done-btn {
        padding: 5px 10px;
        font-size: 11px;
    }

    /* Slide-over Drawer Fullscreen on Mobile */
    #tbAiDevDrawer {
        width: 100vw !important;
        max-width: 100vw !important;
    }
    .tb-ai-drawer-header {
        padding: 8px 10px;
        gap: 6px;
    }
    .tb-ai-drawer-title {
        display: none;
    }
    .tb-ai-tab-select {
        max-width: 150px;
        font-size: 11px;
        padding: 3px 5px;
    }
    .tb-ai-drawer-capture-btn span {
        display: none;
    }
    .tb-ai-drawer-capture-btn {
        padding: 4px 8px !important;
    }
    #tbAiDrawerToggleExpand {
        display: none;
    }
}
