/**
 * Chat interface styles for Agenistrator
 *
 * Global chat button and widget CSS that must be conflict-free
 * All classes use the full plugin prefix 'agenistrator-' to prevent conflicts
 * Depends on common.css for shared CSS custom properties
 */

/* Chat button styles */
.agenistrator-chat-button {
    position: fixed;
    z-index: 9998; /* One less than chat container */
    width: 60px;
    height: 60px;
}

.agenistrator-position-bottom-right {
    bottom: 20px;
    right: 20px;
}

.agenistrator-position-bottom-left {
    bottom: 20px;
    left: 20px;
}

.agenistrator-position-top-right {
    top: 40px;
    right: 20px;
}

.agenistrator-position-top-left {
    top: 40px;
    left: 20px;
}

.agenistrator-chat-btn {
    width: 60px;
    height: 60px;
    border: none;
    background: transparent;
    cursor: pointer;
    transition: all 0.3s ease;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.agenistrator-chat-btn:hover {
    transform: translateY(-2px);
}

.agenistrator-chat-btn:active {
    transform: translateY(0);
}

.agenistrator-chat-icon {
    display: block;
    width: 60px;
    height: 60px;
    background-image: url('../../assets/icon.svg');
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    transition: all 0.3s ease;
    filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.4));
}

.agenistrator-chat-btn:hover .agenistrator-chat-icon {
    filter: drop-shadow(0 6px 16px rgba(0, 0, 0, 0.6));
}

/* Chat interface styling */
.agenistrator-chat-container {
    position: fixed;
    z-index: 9999;
    width: 350px;
    max-height: 500px;
}

.agenistrator-chat-header {
    background-color: var(--wp-admin-theme-color, #2271b1);
    color: white;
    padding: 10px 15px;
    border-radius: 8px 8px 0 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

/* Fallback color scheme support for WordPress < 5.7 */
.admin-color-fresh .agenistrator-chat-header { background-color: var(--wp-admin-theme-color, #0073aa); }
.admin-color-light .agenistrator-chat-header { background-color: var(--wp-admin-theme-color, #04a4cc); }
.admin-color-blue .agenistrator-chat-header { background-color: var(--wp-admin-theme-color, #096484); }
.admin-color-coffee .agenistrator-chat-header { background-color: var(--wp-admin-theme-color, #46403c); }
.admin-color-ectoplasm .agenistrator-chat-header { background-color: var(--wp-admin-theme-color, #523f6d); }
.admin-color-midnight .agenistrator-chat-header { background-color: var(--wp-admin-theme-color, #363b3f); }
.admin-color-ocean .agenistrator-chat-header { background-color: var(--wp-admin-theme-color, #738e96); }
.admin-color-sunrise .agenistrator-chat-header { background-color: var(--wp-admin-theme-color, #cf4944); }
.admin-color-modern .agenistrator-chat-header { background-color: var(--wp-admin-theme-color, #3858e9); }

.agenistrator-chat-header h3 {
    margin: 0;
    font-size: 16px;
    color: inherit;
}

/* Updated chat header controls */
.agenistrator-chat-controls {
    display: flex;
    gap: 4px;
}

.agenistrator-clear-chat,
.agenistrator-close-chat,
.agenistrator-maximize-chat,
.agenistrator-restore-chat {
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    padding: var(--padding-small);
    border-radius: var(--border-radius);
    transition: background-color 0.2s;
    line-height: 1;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.agenistrator-clear-chat .dashicons,
.agenistrator-close-chat .dashicons,
.agenistrator-maximize-chat .dashicons,
.agenistrator-restore-chat .dashicons {
    font-size: 16px;
    width: 16px;
    height: 16px;
}

.agenistrator-close-chat:hover,
.agenistrator-maximize-chat:hover,
.agenistrator-restore-chat:hover,
.agenistrator-clear-chat:hover {
    background-color: rgba(255, 255, 255, 0.2);
}

/* Button visibility based on widget state */
.agenistrator-chat-container.agenistrator-state-open .agenistrator-maximize-chat {
    display: flex;
}

.agenistrator-chat-container.agenistrator-state-open .agenistrator-restore-chat {
    display: none;
}

.agenistrator-chat-container.agenistrator-state-maximized .agenistrator-maximize-chat {
    display: none;
}

.agenistrator-chat-container.agenistrator-state-maximized .agenistrator-restore-chat {
    display: flex;
}

/* State-based visibility */
.agenistrator-state-closed {
    display: none;
}

.agenistrator-state-open {
    display: block;
}

.agenistrator-state-maximized {
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100vh;
    max-width: none;
    max-height: none;
    z-index: 100000; /* Above WordPress admin bar (99999) */
}

/* Chat button specific state overrides */
.agenistrator-chat-button.agenistrator-state-closed {
    display: block; /* Override the general state-closed rule for buttons */
}

.agenistrator-chat-button.agenistrator-state-open,
.agenistrator-chat-button.agenistrator-state-maximized {
    display: none;
}

.agenistrator-chat-container.agenistrator-state-maximized .agenistrator-chat-body {
    flex: 1; /* Fill remaining space after header */
}

.agenistrator-chat-container.agenistrator-state-maximized .agenistrator-chat-messages {
    max-height: calc(100vh - 160px); /* Subtract header and input area */
}

/* Maximized state message adjustments */
.agenistrator-chat-container.agenistrator-state-maximized .agenistrator-message {
    max-width: 90%; /* Slightly wider in maximized mode */
}

/* Hide main window scrollbar when chat is maximized */
html.agenistrator-chat-maximized {
    overflow: hidden;
}

.agenistrator-chat-body {
    background: white;
    border: 1px solid #ddd;
    border-top: none;
    border-radius: 0 0 8px 8px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    height: 400px;
}

.agenistrator-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 15px;
}

.agenistrator-message {
    margin-bottom: 12px;
    max-width: 80%;
    /* Ensure proper box sizing for consistent width calculations */
    box-sizing: border-box;
    /* Prevent horizontal overflow */
    overflow-x: hidden;
}

.agenistrator-user-message {
    margin-left: auto;
    background-color: #e7f5ff;
    border-radius: 15px 15px 0 15px;
    padding: 0;
}

.agenistrator-assistant-message {
    margin-right: auto;
    background-color: #f1f1f1;
    border-radius: 15px 15px 15px 0;
    padding: 0;
}

/* Content styling within message bubbles */
.agenistrator-markdown-content img {
    max-width: 100%;
    height: auto;
    border-radius: var(--border-radius);
    box-sizing: border-box;
}

.agenistrator-markdown-content table {
    max-width: 100%;
    width: 100%;
    table-layout: fixed;
    word-wrap: break-word;
    border-collapse: collapse;
    box-sizing: border-box;
}

.agenistrator-markdown-content table td,
.agenistrator-markdown-content table th {
    word-wrap: break-word;
    word-break: break-word;
    overflow-wrap: anywhere;
    padding: 4px 8px;
    border: 1px solid #ddd;
}

.agenistrator-markdown-content pre,
.agenistrator-markdown-content code {
    max-width: 100%;
    overflow-x: auto;
    word-wrap: break-word;
    white-space: pre-wrap;
    box-sizing: border-box;
}

/* Message content container for both user and assistant messages */
.agenistrator-message-content {
    max-width: 100%;
    overflow-x: hidden;
    word-wrap: break-word;
    word-break: normal;
    overflow-wrap: break-word;
    box-sizing: border-box;
    padding: var(--padding-medium) 12px;
}

/* Markdown content within messages */
.agenistrator-markdown-content {
    /* Display inline so cursor appears at end of last line, not on new line */
    display: inline;
    /* Ensure markdown content respects container boundaries */
    max-width: 100%;
    overflow-x: hidden;
    box-sizing: border-box;
}

/* Make last element inline so cursor follows it on same line (only during streaming) */
.agenistrator-message-streaming .agenistrator-markdown-content > *:last-child {
    display: inline;
}

/* Reset margins on all markdown elements within message bubbles */
.agenistrator-markdown-content p,
.agenistrator-markdown-content ul,
.agenistrator-markdown-content ol,
.agenistrator-markdown-content h1,
.agenistrator-markdown-content h2,
.agenistrator-markdown-content h3,
.agenistrator-markdown-content h4,
.agenistrator-markdown-content h5,
.agenistrator-markdown-content h6 {
    margin: 0;
    padding: 0;
}

/* Add spacing between consecutive elements */
.agenistrator-markdown-content p + p,
.agenistrator-markdown-content ul + *,
.agenistrator-markdown-content ol + *,
.agenistrator-markdown-content h1 + *,
.agenistrator-markdown-content h2 + *,
.agenistrator-markdown-content h3 + *,
.agenistrator-markdown-content h4 + *,
.agenistrator-markdown-content h5 + *,
.agenistrator-markdown-content h6 + * {
    margin-top: 8px;
}

/* Override WordPress admin CSS interference for proper markdown rendering */
.agenistrator-message .agenistrator-markdown-content ul {
    list-style: disc !important;
    list-style-position: inside !important;
    padding-left: 0 !important;
    margin-left: 0 !important;
}

.agenistrator-message .agenistrator-markdown-content ol {
    list-style: decimal !important;
    list-style-position: inside !important;
    padding-left: 0 !important;
    margin-left: 0 !important;
}

.agenistrator-message .agenistrator-markdown-content ul li,
.agenistrator-message .agenistrator-markdown-content ol li {
    list-style: inherit !important;
    margin: 0 !important;
    padding: 0 !important;
    display: list-item !important;
}

.agenistrator-message .agenistrator-markdown-content li p {
    display: inline !important;
    margin: 0 !important;
    padding: 0 !important;
}

.agenistrator-message .agenistrator-markdown-content li p + p {
    display: block !important;
    margin-top: 8px !important;
}

.agenistrator-message .agenistrator-markdown-content ul ul {
    list-style: circle !important;
    margin-left: 20px !important;
    margin-top: 4px !important;
}

.agenistrator-message .agenistrator-markdown-content ul ul ul {
    list-style: square !important;
}

.agenistrator-message .agenistrator-markdown-content ol ol {
    list-style: lower-alpha !important;
    margin-left: 20px !important;
    margin-top: 4px !important;
}

.agenistrator-message .agenistrator-markdown-content ol ol ol {
    list-style: lower-roman !important;
}

.agenistrator-message .agenistrator-markdown-content blockquote {
    border-left: 4px solid #ddd !important;
    margin: 0 !important;
    padding: 0 0 0 16px !important;
    font-style: italic !important;
    color: #666 !important;
}

.agenistrator-message .agenistrator-markdown-content strong,
.agenistrator-message .agenistrator-markdown-content b {
    font-weight: bold !important;
}

.agenistrator-message .agenistrator-markdown-content em,
.agenistrator-message .agenistrator-markdown-content i {
    font-style: italic !important;
}

.agenistrator-message .agenistrator-markdown-content h1,
.agenistrator-message .agenistrator-markdown-content h2,
.agenistrator-message .agenistrator-markdown-content h3,
.agenistrator-message .agenistrator-markdown-content h4,
.agenistrator-message .agenistrator-markdown-content h5,
.agenistrator-message .agenistrator-markdown-content h6 {
    font-weight: bold !important;
    line-height: 1.2 !important;
}

.agenistrator-message .agenistrator-markdown-content h1 { font-size: 1.5em !important; }
.agenistrator-message .agenistrator-markdown-content h2 { font-size: 1.3em !important; }
.agenistrator-message .agenistrator-markdown-content h3 { font-size: 1.1em !important; }
.agenistrator-message .agenistrator-markdown-content h4 { font-size: 1em !important; }
.agenistrator-message .agenistrator-markdown-content h5 { font-size: 0.9em !important; }
.agenistrator-message .agenistrator-markdown-content h6 { font-size: 0.8em !important; }

.agenistrator-error-message {
    margin-right: auto;
    background-color: var(--error-bg);
    border-radius: 15px 15px 15px 0;
    padding: 10px 15px;
    color: var(--error-text);
    border-left: 3px solid #dc3545;
}

/* Status bar for rate limiting and other status messages */
.agenistrator-chat-status {
    padding: 8px 12px;
    margin: 0 15px 12px 15px;
    background-color: #fff3cd;
    border: 1px solid #ffc107;
    border-radius: var(--border-radius-large);
    color: #856404;
    font-size: 12px;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.agenistrator-chat-status.hidden {
    display: none;
}

.agenistrator-chat-status .agenistrator-spinner {
    width: 14px;
    height: 14px;
    border: 2px solid #856404;
    border-top-color: transparent;
    border-radius: 50%;
    animation: agenistrator-spin 1s linear infinite;
}

@keyframes agenistrator-spin {
    to { transform: rotate(360deg); }
}

.agenistrator-chat-prompt-form {
    padding: 10px;
    border-top: 1px solid #ddd;
    display: flex;
}

.agenistrator-chat-prompt-form textarea {
    flex: 1;
    border: 1px solid #ddd;
    border-radius: var(--border-radius);
    padding: var(--padding-medium);
    height: 60px;
    resize: none;
}

.agenistrator-chat-prompt-form button.button {
    margin: 0 0 0 5px;
    height: 60px;
    white-space: nowrap;
}

.agenistrator-chat-prompt-form button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* Chat footer with report link */
.agenistrator-chat-footer {
    text-align: center;
    border-top: 1px solid #eee;
    background: #fafafa;
}

.agenistrator-report-issue {
    font-size: 11px;
    color: #666;
    text-decoration: none;
}

.agenistrator-report-issue:hover {
    color: #0073aa;
    text-decoration: underline;
}

/* Animation for state transitions */
.agenistrator-chat-container {
    transition: opacity 0.3s ease, transform 0.3s ease;
}

.agenistrator-chat-body {
    transition: height 0.3s ease;
}

/* Streaming content styles */
.agenistrator-blinking-cursor {
    display: inline-block;
    width: 2px;
    height: 14px;
    background-color: #333;
    margin-left: 2px;
    animation: blink 1s infinite;
    vertical-align: middle;
}

@keyframes blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}

/* Stop button styles */
.agenistrator-stop-button {
    background-color: #dc3545 !important;
    border-color: #dc3545 !important;
    color: #fff !important;
    transition: all 0.2s ease-in-out;
}

.agenistrator-stop-button:hover,
.agenistrator-stop-button:focus {
    background-color: #c82333 !important;
    border-color: #bd2130 !important;
    color: #fff !important;
    box-shadow: 0 0 0 2px rgba(220, 53, 69, 0.25) !important;
}

.agenistrator-stop-button:active {
    background-color: #bd2130 !important;
    border-color: #b21f2d !important;
    transform: translateY(1px);
}

/* Tool Call and Tool Result Message Styles */
.agenistrator-tool-call-message,
.agenistrator-tool-result-message {
    margin: 6px 0;
    padding: var(--padding-medium);
    border-radius: var(--border-radius-large);
    border-left: 4px solid #0073aa;
    background-color: #f7f7f7;
    /* Override message width constraints - tools should span full width */
    max-width: 100% !important;
    margin-left: 0 !important;
    margin-right: 0 !important;
    opacity: 60%;
}

.agenistrator-tool-call-message .agenistrator-message-header,
.agenistrator-tool-result-message .agenistrator-message-header {
    display: flex;
    align-items: center;
    gap: var(--padding-medium);
    margin-bottom: var(--padding-medium);
    padding: var(--padding-small);
    font-weight: 600;
    cursor: pointer;
    user-select: none;
    transition: background-color 0.2s ease;
    border-radius: var(--border-radius);
}

.agenistrator-tool-call-message .agenistrator-message-header:hover,
.agenistrator-tool-result-message .agenistrator-message-header:hover {
    background-color: rgba(0, 115, 170, 0.1);
}

.agenistrator-tool-call-icon,
.agenistrator-tool-result-icon {
    font-size: 16px;
}

.agenistrator-tool-call-label,
.agenistrator-tool-result-label {
    color: #666;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.agenistrator-tool-name {
    color: #0073aa;
    font-family: var(--font-mono);
}

.agenistrator-tool-call-content,
.agenistrator-tool-result-content {
    margin-top: 8px;
}

/* Collapsible functionality */
.agenistrator-collapse-indicator {
    margin-left: auto;
    font-size: 12px;
    transition: transform 0.2s ease;
    color: #666;
    transform: rotate(180deg);
}

.agenistrator-collapsed-message .agenistrator-collapse-indicator {
    transform: rotate(0);
}

.agenistrator-tool-arguments {
    margin-bottom: 8px;
}

.agenistrator-tool-args-display {
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: var(--border-radius);
    padding: var(--padding-medium);
    font-family: var(--font-mono);
    font-size: var(--font-size-small);
    white-space: pre-wrap;
    overflow-x: auto;
    max-height: 300px;
    overflow-y: auto;
}

.agenistrator-tool-result-success {
    border-left-color: #28a745;
    background-color: #f8fff9;
}

.agenistrator-tool-result-error {
    border-left-color: #dc3545;
    background-color: #fff8f8;
}

.agenistrator-tool-result-data {
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: var(--border-radius);
    padding: var(--padding-medium);
    font-family: var(--font-mono);
    font-size: var(--font-size-small);
    white-space: pre-wrap;
    overflow-x: auto;
    max-height: 300px;
    overflow-y: auto;
}

/* NProgress customization for chat body container */
.agenistrator-chat-body {
    position: relative;
}

.agenistrator-chat-body #nprogress {
    pointer-events: none;
}

.agenistrator-chat-body #nprogress .bar {
    background: #fa2f2f;
    height: 3px;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
}

.agenistrator-chat-body #nprogress .peg {
    box-shadow: 0 0 10px #fa2f2f, 0 0 5px #fa2f2f;
}
