/* file: portal/app/static/css/custom-components.css */

/* ===========================================
   Bootstrap Focus Style Overrides
   =========================================== */

/*
 * Bootstrap 5.3 uses hardcoded colors for form control focus. We define these
 * as CSS custom properties for consistency across all custom components.
 *
 * All custom focus styles (Select2, CodeMirror, input groups, etc.) should use
 * these values to match native Bootstrap form controls exactly.
 */
:root {
    --input-focus-border-color: var(--focus-border);
}


.table-container {
  border: 1px solid var(--border);
  border-radius: var(--radius-box);
}

.form-control:focus,
.form-select:focus {
    border-color: var(--input-focus-border-color);
    box-shadow: 0 0 0 var(--bs-focus-ring-width) rgba(var(--bs-primary-rgb), var(--bs-focus-ring-opacity));
}

/* ===========================================
   Cloak - Hide elements until JS initializes
   =========================================== */

/* Elements with data-cloak are invisible until JS removes the attribute.
   Similar to Vue's v-cloak or Alpine's x-cloak.
   Uses opacity instead of display:none to preserve layout and minimize CLS. */
[data-cloak] {
    opacity: 0;
    pointer-events: none;
}

:root {
    --site-max-width: 1920px;
    --btn-circle-size: 2.5rem;

    /* Mirror Bootstrap's floating label SASS variables (not exposed as CSS custom properties) */
    --form-floating-label-opacity: 0.65;
    --form-floating-label-transform: scale(0.85) translateY(-0.5rem) translateX(0.15rem);
}

/* Body doesn't scroll - app-shell content-wrapper handles scrolling */
body {
    overflow: hidden;
    height: 100vh;
}

/* ===========================================
   App Shell - Sandboxed Viewport
   =========================================== */

/*
 * App shell pattern: creates a bounded "viewport" for the application.
 * - Max width capped at 1920px, centered on wider screens
 * - Fixed/absolute elements inside position relative to this container
 * - Content scrolling handled by content-wrapper, not body
 * - Provides consistent layout regardless of actual viewport size
 */
.app-shell {
    position: relative;  /* Positioning context for absolute children */
    max-width: var(--site-max-width);
    height: 100vh;
    margin: 0 auto;
}

/* Page wrapper - flex container for sidebar + content */
.page-wrapper {
    display: flex;
    height: 100%;
}

/* Main content area - scroll container */
.content-wrapper {
    flex: 1;
    min-width: 0;  /* Allow flex child to shrink below content size */
    height: 100%;  /* Explicit height needed for overflow-y to work */
    padding: 1rem;
    overflow-y: auto;
    scrollbar-gutter: stable;
}

/* ===========================================
   AJAX Navigation Loading Indicator
   =========================================== */

/* Fade content during AJAX navigation */
.ajax-loading {
    opacity: 0.5;
    pointer-events: none;
    transition: opacity 0.15s ease-out;
}

/* Optional: Add spinner overlay (uncomment if desired)
.ajax-loading::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 2rem;
    height: 2rem;
    margin: -1rem 0 0 -1rem;
    border: 3px solid var(--bs-border-color);
    border-top-color: var(--bs-primary);
    border-radius: 50%;
    animation: ajax-spinner 0.6s linear infinite;
}

@keyframes ajax-spinner {
    to { transform: rotate(360deg); }
}
*/

/* ===========================================
   Modal Utilities
   =========================================== */

/* Prevent scrollbar layout shift when modal opens */
body.modal-open {
    scrollbar-gutter: auto;
}

/* Position modal from top with viewport constraints (use with modal-dialog-scrollable) */
.modal-top-offset {
    margin-top: 15vh;
    max-height: 70vh;
}

.modal-top-offset .modal-content {
    max-height: 70vh;
}

/* form_modal.html wraps header/body/footer in a <form>, breaking Bootstrap's
   modal-dialog-scrollable flex chain. Restore it so .modal-body can scroll. */
.modal-top-offset .modal-content > form {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: 0;
    flex: 1;
}

/* class matching modal-footer */
.form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    margin-top: 2rem;
    margin-bottom: 5rem;        /* to avoid overlap with any floating elements */
    border-top: 1px solid var(--bs-border-color);
    padding-top: 1rem;
}

.form-switch.form-switch-md {
    display: flex;
    align-items: center;
    padding-left: 3rem;
    margin-bottom: 0.5rem;
}

.form-switch.form-switch-md .form-check-input {
    height: 1.5rem;
    width: 3rem;
    border-radius: 1.5rem;
    cursor: pointer;
    margin-top: 0;
}

.form-switch.form-switch-md .form-check-label {
    margin-left: 0.5rem;
}

/* large switch styles */
.form-switch.form-switch-lg {
    display: flex;
    align-items: center;
    padding-left: 3.5rem;
    margin-bottom: 0.5rem;
}

.form-switch.form-switch-lg .form-check-input {
    height: 2rem;
    width: 3.5rem;
    border-radius: 2rem;
    cursor: pointer;
    margin-top: 0;
}

.form-switch.form-switch-lg .form-check-label {
    margin-left: 0.5rem;
}

/* extra large switch styles */
.form-switch.form-switch-xl {
    display: flex;
    align-items: center;
    padding-left: 4rem;
    margin-bottom: 0.5rem;
}

.form-switch.form-switch-xl .form-check-input {
    height: 2.5rem;
    width: 4rem;
    border-radius: 2.5rem;
    cursor: pointer;
    margin-top: 0;
}

.form-switch.form-switch-xl .form-check-label {
    margin-left: 0.5rem;
}

.form-switch .form-check-input:checked {
    background-color: var(--bs-primary);
    border-color: var(--bs-primary);
}

.form-switch .form-check-input:focus {
    border-color: var(--bs-primary);
    box-shadow: 0 0 0 var(--bs-focus-ring-width) rgba(var(--bs-primary-rgb), var(--bs-focus-ring-opacity));
}

.form-switch .form-check-input:disabled:checked {
    background-color: var(--bs-primary-border-subtle);
    border-color: var(--bs-primary-border-subtle);
}

.form-switch.form-switch-secondary .form-check-input:checked {
    background-color: var(--bs-secondary);
    border-color: var(--bs-secondary);
}

.form-switch.form-switch-secondary .form-check-input:focus {
    border-color: var(--bs-secondary);
    box-shadow: 0 0 0 var(--bs-focus-ring-width) rgba(var(--bs-secondary-rgb), var(--bs-focus-ring-opacity));
}

/* ensure circular icon buttons have equal width and height */
.btn.rounded-circle {
    width: var(--btn-circle-size);
    height: var(--btn-circle-size);
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    flex-shrink: 0;
}

.btn.rounded-circle.btn-sm {
    width: 2rem;
    height: 2rem;
}

/* Subtle border on circular icon buttons to match icon stroke weight */
.btn.rounded-circle.btn-outline-secondary {
    --bs-btn-border-color: var(--outline-border);
}

.btn.rounded-circle.btn-outline-secondary:hover,
.btn.rounded-circle.btn-outline-secondary:focus {
    --bs-btn-border-color: var(--bs-secondary);
}

/* Progress bar styles to match form controls */
.progress.rounded-pill {
    background-color: var(--bs-secondary-bg);
    border: 1px solid var(--bs-border-color);
}

.progress-bar.rounded-pill {
    transition: width 0.3s ease-in-out;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;  /* Match form-control font size */
    font-family: var(--bs-body-font-family);  /* Match Bootstrap's body font */
    font-weight: normal;  /* Regular weight like form inputs */
}

/* Ensure text is visible even at low percentages */
.progress-bar.rounded-pill span {
    min-width: 3rem;
    text-align: center;
}

/* Icon hover effect */
.icon-hover-container {
    position: relative;
    display: inline-block;
    cursor: pointer;
}

.icon-hover-container .icon-default {
    opacity: 1;
    transition: opacity 0.2s ease;
}

.icon-hover-container .icon-hover {
    position: absolute;
    top: 0;
    left: 0;
    opacity: 0;
    transition: opacity 0.2s ease;
    pointer-events: none;
}

.icon-hover-container:hover .icon-default {
    opacity: 0;
}

.icon-hover-container:hover .icon-hover {
    opacity: 1;
}

/* Floating utility container (AI Assistant button, etc.) */
.utility-container {
    transition: opacity 0.3s ease, box-shadow 0.3s ease;
    opacity: 0.85;
    padding: 0.2rem;
    background-color: var(--bs-body-bg);
    box-shadow: 0 2px 8px rgba(var(--bs-black-rgb), 0.15), 0 0 0 1px var(--bs-border-color);
}

[data-bs-theme="dark"] .utility-container {
    box-shadow: 0 2px 12px rgba(var(--bs-black-rgb), 0.4), 0 0 0 1px var(--bs-border-color);
}

.utility-container:hover {
    opacity: 1;
    box-shadow: 0 4px 12px rgba(var(--bs-black-rgb), 0.2), 0 0 0 1px var(--bs-border-color);
}

[data-bs-theme="dark"] .utility-container:hover {
    box-shadow: 0 4px 16px rgba(var(--bs-black-rgb), 0.5), 0 0 0 1px var(--bs-border-color);
}

.utility-container .btn-primary .bi {
    color: var(--primary-text) !important;
}

/* Muted accordion - removes blue highlight, keeps borders */
.accordion-muted .accordion-button {
    background-color: var(--bs-accordion-btn-bg);
    color: var(--bs-accordion-btn-color);
    box-shadow: none;
}

.accordion-muted .accordion-button:not(.collapsed) {
    background-color: var(--bs-accordion-btn-bg);
    color: var(--bs-accordion-btn-color);
    box-shadow: none;
}

.accordion-muted .accordion-button:focus {
    box-shadow: none;
    border-color: var(--bs-accordion-border-color);
}

/* Accordion header with action trigger — absolutely positioned to sit between
   the title text and the collapse chevron. The trigger is a DOM sibling of the
   collapse button (child of h2, not of button), so Bootstrap's capture-phase
   delegation never matches [data-bs-toggle="collapse"] when walking up from it. */
.accordion-header:has(.snippet-select-trigger) {
    position: relative;
}

.snippet-select-trigger {
    position: absolute;
    right: 3.5rem;
    top: 50%;
    transform: translateY(-50%);
    z-index: 2;
}

/* Wide popover for detailed help content */
.popover-wide {
    --bs-popover-max-width: 400px;
}

@media (max-width: 576px) {
    .popover-wide {
        --bs-popover-max-width: 300px;
    }
}

.popover-body code {
    background-color: var(--bs-tertiary-bg);
    padding: 0.125rem 0.25rem;
    border-radius: 0.25rem;
}

/* Icon scale on hover - requires display:inline-block for transform to work */
.icon-scale-hover {
    display: inline-block;
    transition: transform 0.15s ease;
}

.icon-scale-hover:hover {
    transform: scale(1.2);
}

/* Destructive action buttons - gray outline that fills with color on hover */
.btn-archive:hover {
    background-color: var(--bs-warning);
    border-color: var(--bs-warning);
    color: var(--bs-dark);
}

.btn-delete:hover {
    background-color: var(--bs-danger);
    border-color: var(--bs-danger);
    color: var(--bs-white);
}

.btn-remove:hover {
    background-color: transparent;
    border-color: var(--bs-danger);
    color: var(--bs-danger);
}

/* Invisible button — icon-as-affordance with no chrome at any state.
   Generalizes Bootstrap's .btn-close interaction to any child icon.
   Opacity steps (0.5 → 0.75 → 1.0 → 0.25) match .btn-close exactly.
   Name follows GitHub Primer's IconButton variant="invisible". */
.btn-invisible {
    --bs-btn-color: var(--bs-body-color);
    --bs-btn-bg: transparent;
    --bs-btn-border-color: transparent;
    --bs-btn-hover-color: var(--bs-body-color);
    --bs-btn-hover-bg: transparent;
    --bs-btn-hover-border-color: transparent;
    --bs-btn-active-color: var(--bs-body-color);
    --bs-btn-active-bg: transparent;
    --bs-btn-active-border-color: transparent;
    --bs-btn-padding-x: 0.25em;
    --bs-btn-padding-y: 0.25em;
    --bs-btn-line-height: 1;
    --bs-btn-font-size: 1.25rem;
    opacity: 0.5;
}

.btn-invisible:hover {
    opacity: 0.75;
}

.btn-invisible:focus-visible {
    opacity: 1;
}

.btn-invisible:disabled,
.btn-invisible.disabled {
    opacity: 0.25;
}

/* ===========================================
   Fieldset & Legend Styles
   =========================================== */

/* Make legends subtle section markers, not competing headers */
fieldset.border > legend {
    font-weight: 600;
    color: var(--bs-secondary-color);
}

/* ===========================================
   Form Validation Utilities
   =========================================== */

/* Use for error elements that should match form validation colors
   (icons, tab highlights, etc. within form context) */
.text-form-invalid {
    color: var(--bs-form-invalid-color);
}

/* Fix Bootstrap's validation icon - SVG uses hardcoded colors that need to match
   --bs-form-invalid-color: light mode #dc3545, dark mode #ea868f */
[data-bs-theme="light"] .form-control.is-invalid,
[data-bs-theme="light"] .was-validated .form-control:invalid {
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' width='12' height='12' fill='none' stroke='%23dc3545'%3e%3ccircle cx='6' cy='6' r='4.5'/%3e%3cpath stroke-linejoin='round' d='M5.8 3.6h.4L6 6.5z'/%3e%3ccircle cx='6' cy='8.2' r='.6' fill='%23dc3545' stroke='none'/%3e%3c/svg%3e");
}

[data-bs-theme="dark"] .form-control.is-invalid,
[data-bs-theme="dark"] .was-validated .form-control:invalid {
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' width='12' height='12' fill='none' stroke='%23ea868f'%3e%3ccircle cx='6' cy='6' r='4.5'/%3e%3cpath stroke-linejoin='round' d='M5.8 3.6h.4L6 6.5z'/%3e%3ccircle cx='6' cy='8.2' r='.6' fill='%23ea868f' stroke='none'/%3e%3c/svg%3e");
}

[data-bs-theme="light"] .form-select.is-invalid:not([multiple]):not([size]),
[data-bs-theme="light"] .form-select.is-invalid:not([multiple])[size="1"],
[data-bs-theme="light"] .was-validated .form-select:invalid:not([multiple]):not([size]),
[data-bs-theme="light"] .was-validated .form-select:invalid:not([multiple])[size="1"] {
    --bs-form-select-bg-icon: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' width='12' height='12' fill='none' stroke='%23dc3545'%3e%3ccircle cx='6' cy='6' r='4.5'/%3e%3cpath stroke-linejoin='round' d='M5.8 3.6h.4L6 6.5z'/%3e%3ccircle cx='6' cy='8.2' r='.6' fill='%23dc3545' stroke='none'/%3e%3c/svg%3e");
}

[data-bs-theme="dark"] .form-select.is-invalid:not([multiple]):not([size]),
[data-bs-theme="dark"] .form-select.is-invalid:not([multiple])[size="1"],
[data-bs-theme="dark"] .was-validated .form-select:invalid:not([multiple]):not([size]),
[data-bs-theme="dark"] .was-validated .form-select:invalid:not([multiple])[size="1"] {
    --bs-form-select-bg-icon: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' width='12' height='12' fill='none' stroke='%23ea868f'%3e%3ccircle cx='6' cy='6' r='4.5'/%3e%3cpath stroke-linejoin='round' d='M5.8 3.6h.4L6 6.5z'/%3e%3ccircle cx='6' cy='8.2' r='.6' fill='%23ea868f' stroke='none'/%3e%3c/svg%3e");
}

/* ===========================================
   Table Utilities
   =========================================== */

/* Fixed-layout table where column widths are set by <th> percentages.
   Use .table-col-truncate on <td> cells to truncate content with ellipsis. */
.table-fixed {
    table-layout: fixed;
}

.table-col-truncate {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Inner flex wrapper for truncatable cells with sibling elements (icons, links).
   Keeps the td as a normal table cell; the wrapper handles the layout. */
.truncate-cell {
    display: flex;
    align-items: baseline;
}

.truncate-cell > .preview-link {
    flex: 0 0 auto;
}

/* Truncatable text span. Works as a flex child inside .truncate-cell,
   or standalone inside a .table-col-truncate cell (block display ensures
   scrollWidth-based overflow detection works in both contexts). */
.truncate-text {
    display: block;
    flex: 0 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ===========================================
   Flex Utilities
   =========================================== */

/* Allow flex child to shrink below content size (overrides min-width: auto default) */
.min-w-0 {
    min-width: 0;
}

/* ===========================================
   Input Group Utilities
   =========================================== */

/* Focus-within for input groups - highlights entire container when child input is focused */
.input-group-focus-within:focus-within {
    border-color: var(--input-focus-border-color) !important;
    box-shadow: 0 0 0 var(--bs-focus-ring-width) rgba(var(--bs-primary-rgb), var(--bs-focus-ring-opacity));
}

/* Remove default focus styling from inputs inside focus-within groups */
.input-group-focus-within .form-control:focus {
    box-shadow: none;
    border-color: transparent;
}

/* Fix floating label transform when inside input-group or with form-control-plaintext */
.input-group .form-floating > .form-control:not(:placeholder-shown) ~ label,
.input-group .form-floating > .form-control-plaintext ~ label,
.form-floating > .form-control-plaintext ~ label {
    opacity: var(--form-floating-label-opacity);
    transform: var(--form-floating-label-transform);
}

/* ===========================================
   Section Navigation Bar
   =========================================== */

/* Navbar with h4-sized brand for section headers */
.navbar-section {
  padding-top: 0.75rem;
  padding-bottom: 0.75rem;
}

.navbar-section .navbar-brand {
  font-size: 1.5rem;  /* Match h4 */
  font-weight: 500;   /* Match h4 weight */
  padding: 0;
  margin: 0;
}

.navbar-section .nav-link {
  padding: 0;
}

.navbar-section .nav-link.active {
  font-weight: 400;
  border-bottom: 2px solid var(--bs-primary);
}

/* ===========================================
   Readonly Input Group
   =========================================== */

/*
 * Readonly input group with floating label - for displaying non-editable values
 * with optional action buttons (e.g., copy).
 *
 * Use template for convenience:
 *   {% include "display_field_template.html" with field_id="my-field" label="Label" value=val show_copy=True %}
 *
 * Or use classes directly:
 *   .input-group-readonly  - Dashed border container
 *   .copy-btn              - Copy button (with data-copy-target="#field-id")
 *   .copy-icon             - Icon inside copy button (toggles to check on success)
 */
.input-group-readonly {
    border: 1px dashed var(--bs-border-color);
}

/* Focus state - solid border with highlight (uses Bootstrap's focus ring variables) */
.input-group-readonly:focus-within {
    border-style: solid;
    border-color: var(--input-focus-border-color);
    box-shadow: 0 0 0 var(--bs-focus-ring-width) rgba(var(--bs-primary-rgb), var(--bs-focus-ring-opacity));
}

/* Remove default focus styling from inputs inside readonly groups */
.input-group-readonly .form-control:focus {
    box-shadow: none;
    border-color: transparent;
}

/* ===========================================
   Title Field (Inline Editable Title)
   =========================================== */

/* Container and shared text properties */
.title-field {
    position: relative;
    flex: 1;
    font-size: 1.25rem;
    font-weight: 500;
}

/* Extra space in edit mode - applied to header row, outside flex alignment */
.d-flex.justify-content-between:has(.title-field-input) {
    padding-bottom: 1.25rem;
}

/* Error messages - absolutely positioned to not affect flex centering */
.title-field .invalid-feedback {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    font-size: 0.875rem;  /* Fixed size, not scaled with title font */
}

/* Input field - override .form-control to look like plain text until interacted */
.title-field-input {
    font-size: inherit;  /* Override .form-control's 1rem to inherit from .title-field */
    font-weight: inherit;
    background-color: var(--input);  /* Override .form-control's white background */
    border-color: var(--border);
    height: auto;
    transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

.title-field-input:hover {
    border-color: var(--bs-border-color);
}

.title-field-input:focus {
    border-color: var(--input-focus-border-color);
    box-shadow: 0 0 0 var(--bs-focus-ring-width) rgba(var(--bs-primary-rgb), var(--bs-focus-ring-opacity));
}

.title-field-input.is-invalid {
    /* Keep validation icon size same as standard fields */
    background-size: 1.125rem 1.125rem;
}

/* Readonly mode - match .form-control dimensions */
.title-field-readonly {
    display: inline-block;
    line-height: 1.5;  /* Match .form-control */
    /* Padding = .form-control padding + border width */
    padding: calc(0.375rem + var(--bs-border-width, 1px)) calc(0.75rem + var(--bs-border-width, 1px));
}
