/**
 * TOOLTIPS & POPOVERS - PharmData Design System v2.0
 * Dicas contextuais e informações flutuantes
 *
 * Inclui: Tooltip simples, Popover com conteúdo, posicionamento automático
 */

/* ===================
   CSS CUSTOM PROPERTIES
   =================== */

:root {
    /* Tooltip */
    --tooltip-bg: var(--emerald-abyss, #0B2D2A);
    --tooltip-text: white;
    --tooltip-padding: 0.5rem 0.75rem;
    --tooltip-radius: 0.375rem;
    --tooltip-font-size: 0.75rem;
    --tooltip-max-width: 20rem;
    --tooltip-arrow-size: 6px;
    --tooltip-offset: 8px;
    --tooltip-z-index: 1200;

    /* Popover */
    --popover-bg: var(--cloud);
    --popover-border: rgba(183, 228, 213, 0.2);
    --popover-shadow: 0 10px 40px rgba(0, 0, 0, 0.12);
    --popover-padding: 1rem;
    --popover-radius: 0.5rem;
    --popover-max-width: 24rem;

    /* Animation */
    --tooltip-transition: opacity 0.15s ease, transform 0.15s ease;
}

/* Dark mode */
:root.dark-mode {
    --tooltip-bg: var(--graphite-depth, #E5E8EB);
    --tooltip-text: var(--emerald-abyss, #0B2D2A);
    --popover-bg: var(--arctic-mist, #1a2332);
    --popover-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}

/* ===================
   TOOLTIP BASE
   =================== */

.tooltip {
    position: relative;
    display: inline-block;
}

.tooltip-content {
    position: absolute;
    z-index: var(--tooltip-z-index);
    padding: var(--tooltip-padding);
    background: var(--tooltip-bg);
    color: var(--tooltip-text);
    font-size: var(--tooltip-font-size);
    font-weight: 500;
    line-height: 1.4;
    text-align: center;
    white-space: nowrap;
    border-radius: var(--tooltip-radius);
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
    transition: var(--tooltip-transition);
}

/* Multiline tooltip */
.tooltip-content.multiline {
    white-space: normal;
    text-align: left;
    max-width: var(--tooltip-max-width);
}

/* Arrow */
.tooltip-content::after {
    content: '';
    position: absolute;
    border: var(--tooltip-arrow-size) solid transparent;
}

/* Show on hover/focus */
.tooltip:hover .tooltip-content,
.tooltip:focus-within .tooltip-content,
.tooltip.active .tooltip-content {
    opacity: 1;
    visibility: visible;
}

/* ===================
   TOOLTIP POSITIONS
   =================== */

/* Top (default) */
.tooltip-content,
.tooltip-top .tooltip-content {
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%) translateY(-4px);
    margin-bottom: var(--tooltip-offset);
}

.tooltip-content::after,
.tooltip-top .tooltip-content::after {
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    border-top-color: var(--tooltip-bg);
}

.tooltip:hover .tooltip-content,
.tooltip-top:hover .tooltip-content {
    transform: translateX(-50%) translateY(0);
}

/* Bottom */
.tooltip-bottom .tooltip-content {
    top: 100%;
    bottom: auto;
    left: 50%;
    transform: translateX(-50%) translateY(4px);
    margin-top: var(--tooltip-offset);
    margin-bottom: 0;
}

.tooltip-bottom .tooltip-content::after {
    bottom: 100%;
    top: auto;
    left: 50%;
    transform: translateX(-50%);
    border-top-color: transparent;
    border-bottom-color: var(--tooltip-bg);
}

.tooltip-bottom:hover .tooltip-content {
    transform: translateX(-50%) translateY(0);
}

/* Left */
.tooltip-left .tooltip-content {
    right: 100%;
    left: auto;
    top: 50%;
    bottom: auto;
    transform: translateY(-50%) translateX(-4px);
    margin-right: var(--tooltip-offset);
    margin-bottom: 0;
}

.tooltip-left .tooltip-content::after {
    left: 100%;
    right: auto;
    top: 50%;
    bottom: auto;
    transform: translateY(-50%);
    border-top-color: transparent;
    border-left-color: var(--tooltip-bg);
}

.tooltip-left:hover .tooltip-content {
    transform: translateY(-50%) translateX(0);
}

/* Right */
.tooltip-right .tooltip-content {
    left: 100%;
    right: auto;
    top: 50%;
    bottom: auto;
    transform: translateY(-50%) translateX(4px);
    margin-left: var(--tooltip-offset);
    margin-bottom: 0;
}

.tooltip-right .tooltip-content::after {
    right: 100%;
    left: auto;
    top: 50%;
    bottom: auto;
    transform: translateY(-50%);
    border-top-color: transparent;
    border-right-color: var(--tooltip-bg);
}

.tooltip-right:hover .tooltip-content {
    transform: translateY(-50%) translateX(0);
}

/* ===================
   TOOLTIP VARIANTS
   =================== */

/* Light tooltip */
.tooltip-light .tooltip-content {
    background: var(--cloud);
    color: var(--graphite-depth, #1F2937);
    border: 1px solid var(--popover-border);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.tooltip-light .tooltip-content::after {
    border-top-color: var(--cloud);
}

.tooltip-light.tooltip-bottom .tooltip-content::after {
    border-bottom-color: var(--cloud);
    border-top-color: transparent;
}

/* Info tooltip */
.tooltip-info .tooltip-content {
    background: var(--info, #3B82F6);
}

.tooltip-info .tooltip-content::after {
    border-top-color: var(--info, #3B82F6);
}

/* Success tooltip */
.tooltip-success .tooltip-content {
    background: var(--success, #10B981);
}

.tooltip-success .tooltip-content::after {
    border-top-color: var(--success, #10B981);
}

/* Warning tooltip */
.tooltip-warning .tooltip-content {
    background: var(--warning-dark, #B45309);
}

.tooltip-warning .tooltip-content::after {
    border-top-color: var(--warning-dark, #B45309);
}

/* Error tooltip */
.tooltip-error .tooltip-content {
    background: var(--error, #EF4444);
}

.tooltip-error .tooltip-content::after {
    border-top-color: var(--error, #EF4444);
}

/* ===================
   DATA ATTRIBUTE TOOLTIPS
   Tooltip via atributo [data-tooltip]
   =================== */

[data-tooltip] {
    position: relative;
}

[data-tooltip]::before {
    content: attr(data-tooltip);
    position: absolute;
    z-index: var(--tooltip-z-index);
    padding: var(--tooltip-padding);
    background: var(--tooltip-bg);
    color: var(--tooltip-text);
    font-size: var(--tooltip-font-size);
    font-weight: 500;
    line-height: 1.4;
    text-align: center;
    white-space: nowrap;
    border-radius: var(--tooltip-radius);
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
    transition: var(--tooltip-transition);
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%) translateY(-4px);
    margin-bottom: var(--tooltip-offset);
}

[data-tooltip]::after {
    content: '';
    position: absolute;
    z-index: var(--tooltip-z-index);
    border: var(--tooltip-arrow-size) solid transparent;
    border-top-color: var(--tooltip-bg);
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
    transition: var(--tooltip-transition);
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    margin-bottom: calc(var(--tooltip-offset) - var(--tooltip-arrow-size));
}

[data-tooltip]:hover::before,
[data-tooltip]:hover::after,
[data-tooltip]:focus::before,
[data-tooltip]:focus::after {
    opacity: 1;
    visibility: visible;
}

[data-tooltip]:hover::before {
    transform: translateX(-50%) translateY(0);
}

/* Position variants */
[data-tooltip-position="bottom"]::before {
    bottom: auto;
    top: 100%;
    margin-bottom: 0;
    margin-top: var(--tooltip-offset);
    transform: translateX(-50%) translateY(4px);
}

[data-tooltip-position="bottom"]::after {
    bottom: auto;
    top: 100%;
    border-top-color: transparent;
    border-bottom-color: var(--tooltip-bg);
    margin-bottom: 0;
    margin-top: calc(var(--tooltip-offset) - var(--tooltip-arrow-size));
}

[data-tooltip-position="bottom"]:hover::before {
    transform: translateX(-50%) translateY(0);
}

/* ===================
   POPOVER
   Tooltip com conteúdo rico
   =================== */

.popover {
    position: relative;
    display: inline-block;
}

.popover-trigger {
    cursor: pointer;
}

.popover-content {
    position: absolute;
    z-index: var(--tooltip-z-index);
    min-width: 12rem;
    max-width: var(--popover-max-width);
    padding: var(--popover-padding);
    background: var(--popover-bg);
    border: 1px solid var(--popover-border);
    border-radius: var(--popover-radius);
    box-shadow: var(--popover-shadow);
    opacity: 0;
    visibility: hidden;
    transform: translateY(4px);
    transition: var(--tooltip-transition);
    pointer-events: none;
}

.popover.active .popover-content,
.popover:focus-within .popover-content {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
    pointer-events: auto;
}

/* Popover header */
.popover-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding-bottom: 0.75rem;
    margin-bottom: 0.75rem;
    border-bottom: 1px solid var(--popover-border);
}

.popover-title {
    font-size: 0.9375rem;
    font-weight: 600;
    color: var(--graphite-depth, #1F2937);
    margin: 0;
}

.popover-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.5rem;
    height: 1.5rem;
    background: transparent;
    border: none;
    border-radius: 0.25rem;
    color: var(--soft-steel, #64748B);
    cursor: pointer;
    transition: all 0.15s ease;
}

.popover-close:hover {
    background: rgba(0, 0, 0, 0.05);
    color: var(--graphite-depth, #1F2937);
}

/* Popover body */
.popover-body {
    font-size: 0.875rem;
    color: var(--soft-steel, #64748B);
    line-height: 1.5;
}

.popover-body p {
    margin: 0 0 0.5rem;
}

.popover-body p:last-child {
    margin-bottom: 0;
}

/* Popover footer */
.popover-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.5rem;
    padding-top: 0.75rem;
    margin-top: 0.75rem;
    border-top: 1px solid var(--popover-border);
}

/* ===================
   POPOVER POSITIONS
   =================== */

/* Top */
.popover-top .popover-content {
    bottom: 100%;
    left: 50%;
    top: auto;
    transform: translateX(-50%) translateY(-8px);
    margin-bottom: var(--tooltip-offset);
}

.popover-top.active .popover-content {
    transform: translateX(-50%) translateY(0);
}

/* Bottom (default) */
.popover-content,
.popover-bottom .popover-content {
    top: 100%;
    left: 50%;
    transform: translateX(-50%) translateY(8px);
    margin-top: var(--tooltip-offset);
}

.popover.active .popover-content,
.popover-bottom.active .popover-content {
    transform: translateX(-50%) translateY(0);
}

/* Left */
.popover-left .popover-content {
    right: 100%;
    left: auto;
    top: 50%;
    bottom: auto;
    transform: translateY(-50%) translateX(-8px);
    margin-right: var(--tooltip-offset);
    margin-top: 0;
}

.popover-left.active .popover-content {
    transform: translateY(-50%) translateX(0);
}

/* Right */
.popover-right .popover-content {
    left: 100%;
    right: auto;
    top: 50%;
    bottom: auto;
    transform: translateY(-50%) translateX(8px);
    margin-left: var(--tooltip-offset);
    margin-top: 0;
}

.popover-right.active .popover-content {
    transform: translateY(-50%) translateX(0);
}

/* Alignment variants */
.popover-start .popover-content {
    left: 0;
    transform: translateY(8px);
}

.popover-start.active .popover-content {
    transform: translateY(0);
}

.popover-end .popover-content {
    left: auto;
    right: 0;
    transform: translateY(8px);
}

.popover-end.active .popover-content {
    transform: translateY(0);
}

/* ===================
   POPOVER ON HOVER
   =================== */

.popover-hover:hover .popover-content {
    opacity: 1;
    visibility: visible;
    transform: translateX(-50%) translateY(0);
    pointer-events: auto;
}

/* Delay para evitar fechamento acidental */
.popover-hover .popover-content {
    transition-delay: 0.1s;
}

.popover-hover:hover .popover-content {
    transition-delay: 0s;
}

/* ===================
   HELP TOOLTIP
   Ícone de ajuda com tooltip
   =================== */

.help-tooltip {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1rem;
    height: 1rem;
    background: rgba(100, 116, 139, 0.15);
    color: var(--soft-steel, #64748B);
    border-radius: 50%;
    font-size: 0.625rem;
    font-weight: 700;
    cursor: help;
    margin-left: 0.25rem;
    vertical-align: middle;
}

.help-tooltip::before {
    content: '?';
}

/* ===================
   ACCESSIBILITY
   =================== */

/* Focus management */
.tooltip-content:focus,
.popover-content:focus {
    outline: none;
}

/* Screen reader only */
.tooltip-sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Keyboard accessible tooltip */
.tooltip[tabindex]:focus .tooltip-content {
    opacity: 1;
    visibility: visible;
}

/* High contrast */
@media (prefers-contrast: high) {
    .tooltip-content,
    .popover-content {
        border: 2px solid currentColor;
    }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
    .tooltip-content,
    .popover-content,
    [data-tooltip]::before,
    [data-tooltip]::after {
        transition: none;
    }
}

/* ===================
   RESPONSIVE
   =================== */

@media (max-width: 640px) {
    .tooltip-content {
        max-width: calc(100vw - 2rem);
    }

    .popover-content {
        max-width: calc(100vw - 2rem);
        min-width: 0;
        width: calc(100vw - 2rem);
    }

    /* Force bottom position on mobile */
    .popover-content {
        position: fixed;
        bottom: 1rem;
        top: auto;
        left: 1rem !important;
        right: 1rem !important;
        transform: translateY(100%);
    }

    .popover.active .popover-content {
        transform: translateY(0);
    }
}
