/* =============================================================================
   Shared Component Library
   =============================================================================
   Reusable UI components with consistent dark mode support via design system
   CSS custom properties from crm-design-system.css.

   Tokens used (defined in crm-design-system.css html.dark):
     --surface-primary:   #0F1610
     --surface-secondary: #141F18
     --surface-tertiary:  #1A2A1F
     --surface-elevated:  #1E2E24
     --text-primary:      #F5F3E9
     --text-secondary:    #DDD8C6
     --text-tertiary:     #A39D88
     --border-primary:    #1E2E24
     --border-secondary:  #2C4234
   ========================================================================== */


/* =========================== CARDS =========================== */

.settings-card {
    background: var(--surface-primary);
    border-radius: 0.75rem;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
    border: 1px solid var(--border-primary);
    overflow: hidden;
}

.settings-section-header {
    padding: 1rem 1.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
    user-select: none;
}
.settings-section-header:hover {
    background: var(--surface-tertiary);
}

.settings-section-body {
    padding: 0 1.5rem 1.5rem;
    border-top: 1px solid var(--border-primary);
}


/* =========================== FORMS =========================== */

.form-label {
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 0.25rem;
}

.form-help {
    font-size: 0.75rem;
    color: var(--text-tertiary);
    margin-top: 0.25rem;
}

.form-input {
    width: 100%;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border-secondary);
    border-radius: 0.5rem;
    font-size: 0.875rem;
    background: var(--surface-secondary);
    color: var(--text-primary);
    transition: border-color 0.15s;
}
.form-input:focus {
    outline: none;
    border-color: var(--brand-cta);
    box-shadow: 0 0 0 2px rgba(201,166,86,0.15);
}
html.dark .form-input:focus {
    border-color: #3A4F41;
    box-shadow: 0 0 0 2px rgba(44,66,52,0.25);
}

.form-select {
    padding: 0.5rem 0.75rem;
    border: 1px solid #d1d5db;
    border-radius: 0.5rem;
    font-size: 0.875rem;
    background: white;
    color: #111827;
    transition: border-color 0.15s;
}
.form-select:focus {
    outline: none;
    border-color: #2C4234;
    box-shadow: 0 0 0 2px rgba(44,66,52,0.1);
}
html.dark .form-select {
    background: var(--surface-secondary);
    border-color: var(--border-secondary);
    color: var(--text-primary);
}

.form-textarea {
    min-height: 80px;
    resize: vertical;
}


/* =========================== BUTTONS =========================== */

.btn-primary {
    padding: 0.5rem 1rem;
    background: #2C4234;
    color: white;
    border-radius: 0.5rem;
    font-size: 0.875rem;
    font-weight: 500;
    border: none;
    cursor: pointer;
    transition: background 0.15s;
}
.btn-primary:hover {
    background: #1a2a20;
}
.btn-primary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-secondary {
    padding: 0.5rem 1rem;
    background: #f3f4f6;
    color: #374151;
    border-radius: 0.5rem;
    font-size: 0.875rem;
    font-weight: 500;
    border: 1px solid #d1d5db;
    cursor: pointer;
    transition: background 0.15s;
}
.btn-secondary:hover {
    background: #e5e7eb;
}
html.dark .btn-secondary {
    background: var(--surface-tertiary);
    color: var(--text-secondary);
    border-color: var(--border-secondary);
}
html.dark .btn-secondary:hover {
    background: var(--surface-elevated);
}

.btn-danger {
    padding: 0.25rem 0.75rem;
    background: #fef2f2;
    color: #dc2626;
    border-radius: 0.375rem;
    font-size: 0.75rem;
    font-weight: 500;
    border: 1px solid #fecaca;
    cursor: pointer;
}
.btn-danger:hover {
    background: #fee2e2;
}
html.dark .btn-danger {
    background: rgba(220,38,38,0.12);
    color: #FCA5A5;
    border-color: rgba(220,38,38,0.3);
}
html.dark .btn-danger:hover {
    background: rgba(220,38,38,0.2);
}


/* =========================== TOGGLE SWITCH =========================== */

.toggle-switch {
    position: relative;
    width: 2.5rem;
    height: 1.375rem;
    background: #d1d5db;
    border-radius: 9999px;
    cursor: pointer;
    transition: background 0.2s;
    flex-shrink: 0;
}
.toggle-switch.active {
    background: #2C4234;
}
.toggle-switch::after {
    content: '';
    position: absolute;
    top: 0.125rem;
    left: 0.125rem;
    width: 1.125rem;
    height: 1.125rem;
    background: white;
    border-radius: 9999px;
    transition: transform 0.2s;
}
.toggle-switch.active::after {
    transform: translateX(1.125rem);
}


/* =========================== TOASTS =========================== */

.toast {
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    padding: 0.75rem 1.25rem;
    border-radius: 0.75rem;
    font-size: 0.875rem;
    font-weight: 500;
    z-index: 100;
    opacity: 0;
    transform: translateY(1rem);
    transition: opacity 0.3s, transform 0.3s;
}
.toast.show {
    opacity: 1;
    transform: translateY(0);
}
.toast.success {
    background: #dcfce7;
    color: #166534;
    border: 1px solid #bbf7d0;
}
.toast.error {
    background: #fee2e2;
    color: #991b1b;
    border: 1px solid #fecaca;
}
html.dark .toast.success {
    background: rgba(22,101,52,0.25);
    color: #6ee7b7;
    border-color: rgba(22,101,52,0.4);
}
html.dark .toast.error {
    background: rgba(153,27,27,0.25);
    color: #fca5a5;
    border-color: rgba(153,27,27,0.4);
}


/* =========================== TIER BADGES =========================== */

.tier-badge {
    display: inline-flex;
    padding: 0.25rem 0.75rem;
    font-size: 0.875rem;
    font-weight: 600;
    border-radius: 0.5rem;
}
.tier-TRIAL       { background: #f3f4f6; color: #6b7280; }
.tier-STARTER     { background: #dbeafe; color: #1e40af; }
.tier-PROFESSIONAL { background: #dcfce7; color: #166534; }
.tier-ENTERPRISE  { background: #fef3c7; color: #92400e; }

html.dark .tier-TRIAL       { background: rgba(255,255,255,0.06); color: #A3B19B; }
html.dark .tier-STARTER     { background: rgba(30,64,175,0.2); color: #93c5fd; }
html.dark .tier-PROFESSIONAL { background: rgba(22,101,52,0.25); color: #6ee7b7; }
html.dark .tier-ENTERPRISE  { background: rgba(146,64,14,0.2); color: #FBD35E; }


/* =========================== ROLE BADGES =========================== */

.role-badge {
    display: inline-flex;
    padding: 0.125rem 0.5rem;
    font-size: 0.75rem;
    font-weight: 500;
    border-radius: 9999px;
}
.role-ORG_ADMIN { background: #dcfce7; color: #166534; }
.role-PARTNER   { background: #dbeafe; color: #1e40af; }
.role-ANALYST   { background: #fef3c7; color: #92400e; }
.role-VIEWER    { background: #f3f4f6; color: #4b5563; }

html.dark .role-ORG_ADMIN { background: rgba(22,101,52,0.3); color: #6ee7b7; }
html.dark .role-PARTNER   { background: rgba(30,64,175,0.25); color: #93c5fd; }
html.dark .role-ANALYST   { background: rgba(146,64,14,0.25); color: #FBD35E; }
html.dark .role-VIEWER    { background: rgba(75,85,99,0.3); color: #C1CEBE; }


/* =========================== USAGE BARS =========================== */

.usage-bar {
    height: 0.5rem;
    border-radius: 9999px;
    background: #e5e7eb;
    overflow: hidden;
}
.usage-bar-fill {
    height: 100%;
    border-radius: 9999px;
    background: #2C4234;
    transition: width 0.3s;
}
.usage-bar-fill.warning { background: #f59e0b; }
.usage-bar-fill.danger  { background: #dc2626; }

html.dark .usage-bar { background: rgba(255,255,255,0.08); }


/* =========================== SKELETON LOADING =========================== */

.skeleton {
    background: linear-gradient(90deg, #e5e7eb 25%, #f3f4f6 50%, #e5e7eb 75%);
    background-size: 200% 100%;
    animation: shimmer 1.5s infinite;
    border-radius: 0.25rem;
}
@keyframes shimmer {
    0%   { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}
html.dark .skeleton {
    background: linear-gradient(90deg, rgba(20,31,24,0.8) 25%, rgba(44,66,52,0.4) 50%, rgba(20,31,24,0.8) 75%);
    background-size: 200% 100%;
}


/* =========================== WEIGHT SUM BADGE =========================== */

.weight-sum-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.25rem 0.75rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 600;
    transition: background 0.2s, color 0.2s;
}
.weight-sum-badge.valid {
    background: #dcfce7;
    color: #166534;
}
.weight-sum-badge.invalid {
    background: #fee2e2;
    color: #991b1b;
}
html.dark .weight-sum-badge.valid {
    background: rgba(22,101,52,0.25);
    color: #6ee7b7;
}
html.dark .weight-sum-badge.invalid {
    background: rgba(153,27,27,0.25);
    color: #fca5a5;
}


/* =========================== CHIP SELECTORS =========================== */

.stage-chip,
.sector-chip {
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.stage-chip.selected,
.sector-chip.selected {
    background: #2C4234;
    border-color: #2C4234;
    color: white;
}
html.dark .stage-chip,
html.dark .sector-chip {
    border-color: var(--border-secondary);
    color: var(--text-secondary);
}
html.dark .stage-chip.selected,
html.dark .sector-chip.selected {
    background: #2C4234;
    border-color: #2C4234;
    color: white;
}


/* =========================== TAXONOMY ROWS =========================== */

.taxonomy-sector-row {
    border: 1px solid #e5e7eb;
    border-radius: 0.75rem;
    padding: 1rem;
    background: #f9fafb;
}
html.dark .taxonomy-sector-row {
    border-color: var(--border-primary);
    background: var(--surface-secondary);
}


/* =========================== CHEVRON ANIMATION =========================== */

.chevron-icon {
    transition: transform 0.2s;
}
.chevron-icon.open {
    transform: rotate(180deg);
}


/* =========================== MODALS (dark mode) =========================== */

.modal-panel {
    background: white;
    border-radius: 0.75rem;
    box-shadow: 0 25px 50px -12px rgba(0,0,0,0.25);
}
html.dark .modal-panel {
    background: var(--surface-secondary);
    box-shadow: 0 25px 50px -12px rgba(0,0,0,0.5);
}

.modal-divider {
    border-color: #e5e7eb;
}
html.dark .modal-divider {
    border-color: var(--border-primary);
}


/* =========================== TOOLTIPS =========================== */

.tooltip-container {
    position: relative;
    display: inline-flex;
    align-items: center;
}

.tooltip-container .tooltip-text {
    visibility: hidden;
    width: 250px;
    background-color: #000000;
    color: #ffffff;
    text-align: left;
    border-radius: 8px;
    padding: 12px 16px;
    position: absolute;
    z-index: 10050;
    bottom: calc(100% + 8px);
    left: 50%;
    margin-left: -125px;
    opacity: 0;
    transition: opacity 0.3s;
    font-size: 0.875rem;
    font-weight: 500;
    line-height: 1.55;
    letter-spacing: 0.01em;
    -webkit-font-smoothing: antialiased;
    border: 1px solid rgba(255, 255, 255, 0.22);
    box-shadow:
        0 0 0 2px rgba(0, 0, 0, 0.65),
        0 16px 40px rgba(0, 0, 0, 0.65),
        0 2px 8px rgba(0, 0, 0, 0.45);
    pointer-events: none;
}

.tooltip-container .tooltip-text::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: #000000 transparent transparent transparent;
}

.tooltip-container:hover .tooltip-text {
    visibility: visible;
    opacity: 1;
}

/* Page title: open downward so popovers are not clipped by the scroll viewport top */
.dealtitan-page-title .tooltip-container .tooltip-text:not([data-pos]) {
    bottom: auto;
    top: calc(100% + 8px);
}
.dealtitan-page-title .tooltip-container .tooltip-text:not([data-pos])::after {
    top: auto;
    bottom: 100%;
    border-color: transparent transparent #000000 transparent;
}

/* Position: bottom */
.tooltip-container .tooltip-text[data-pos="bottom"] {
    bottom: auto;
    top: calc(100% + 8px);
}
.tooltip-container .tooltip-text[data-pos="bottom"]::after {
    top: auto;
    bottom: 100%;
    border-color: transparent transparent #000000 transparent;
}

/* Position: left */
.tooltip-container .tooltip-text[data-pos="left"] {
    bottom: auto;
    top: 50%;
    transform: translateY(-50%);
    left: auto;
    right: calc(100% + 8px);
    margin-left: 0;
}
.tooltip-container .tooltip-text[data-pos="left"]::after {
    top: 50%;
    left: 100%;
    margin-top: -5px;
    margin-left: 0;
    border-color: transparent transparent transparent #000000;
}

/* Position: right */
.tooltip-container .tooltip-text[data-pos="right"] {
    bottom: auto;
    top: 50%;
    transform: translateY(-50%);
    left: calc(100% + 8px);
    margin-left: 0;
}
.tooltip-container .tooltip-text[data-pos="right"]::after {
    top: 50%;
    right: 100%;
    left: auto;
    margin-top: -5px;
    margin-left: 0;
    border-color: transparent #000000 transparent transparent;
}

/* Width variants */
.tooltip-text--wide { width: 300px !important; margin-left: -150px !important; }
.tooltip-text--narrow { width: 180px !important; margin-left: -90px !important; }

/* Dark mode: light panel + dark text for strong contrast on dark UI */
html.dark .tooltip-container .tooltip-text {
    background-color: #faf8f5;
    color: #0a100c;
    border: 1px solid rgba(10, 16, 12, 0.35);
    box-shadow:
        0 0 0 2px rgba(0, 0, 0, 0.35),
        0 16px 40px rgba(0, 0, 0, 0.55),
        0 2px 8px rgba(0, 0, 0, 0.35);
}
html.dark .tooltip-container .tooltip-text::after {
    border-top-color: #faf8f5;
}
html.dark .tooltip-container .tooltip-text[data-pos="bottom"]::after {
    border-top-color: transparent;
    border-bottom-color: #faf8f5;
}
html.dark .dealtitan-page-title .tooltip-container .tooltip-text:not([data-pos])::after {
    border-top-color: transparent;
    border-bottom-color: #faf8f5;
}
html.dark .tooltip-container .tooltip-text[data-pos="left"]::after {
    border-color: transparent transparent transparent #faf8f5;
}
html.dark .tooltip-container .tooltip-text[data-pos="right"]::after {
    border-color: transparent #faf8f5 transparent transparent;
}

/* Mobile: show on tap */
@media (hover: none) {
    .tooltip-container {
        cursor: pointer;
    }
    .tooltip-container:focus-within .tooltip-text {
        visibility: visible;
        opacity: 1;
    }
}

/* Hide tooltips in print */
@media print {
    .tooltip-text {
        display: none !important;
    }
    .tooltip-icon {
        display: none !important;
    }
}
