/**
 * Clinic Queue Date Picker Styles
 *
 * Scoped entirely to .clinic-queue-datepicker (project BEM prefix convention).
 * Uses design tokens from assets/css/shared/base.css (:root variables).
 *
 * Layout notes:
 *  - .clinic-queue-datepicker  position:fixed popup, appended to <body>
 *  - .cq-dp-backdrop           mobile overlay behind centered popup
 *  - All CSS is nested inside the root selector per project convention
 */

/* ══════════════════════════════════════════════════════════════════
   POPUP ROOT
   ══════════════════════════════════════════════════════════════════ */

.clinic-queue-datepicker {
    position: fixed;
    /*
     * Must be higher than .bcm-overlay (z-index: 99999) so the picker
     * renders on top of the booking-calendar expanded modal.
     */
    z-index: 100002;
    width: 296px;
    background: var(--color-white, #ffffff);
    border: 1px solid var(--color-gray-200, #e2e6e9);
    border-radius: var(--radius-md, 16px);
    box-shadow: var(--shadow-xl, 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04));
    font-family: var(--font-primary, "Assistant", sans-serif);
    direction: rtl;
    overflow: hidden;

    /* Initial state: invisible (transition from invisible → visible) */
    opacity: 0;
    transform: translateY(6px) scale(0.97);
    pointer-events: none;
    transition:
        opacity 160ms ease,
        transform 160ms ease;

    &.cq-dp--visible {
        opacity: 1;
        transform: translateY(0) scale(1);
        pointer-events: all;
    }

    /* ── Header (month/year + navigation arrows) ── */

    .cq-dp__header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 14px 16px 10px;
        border-bottom: 1px solid var(--color-gray-100, #f3f4f6);
        gap: 8px;
    }

    .cq-dp__month-year {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 5px;
        font-size: 15px;
        font-weight: 700;
        color: var(--color-text-title, #050e42);
        user-select: none;
        white-space: nowrap;
        flex: 1;
        min-width: 0;
    }

    .cq-dp__month,
    .cq-dp__year {
        border: none;
        background: transparent;
        padding: 4px 8px;
        border-radius: var(--radius-xs, 8px);
        font: inherit;
        color: inherit;
        cursor: pointer;
        transition:
            background var(--transition-base, 200ms ease),
            color var(--transition-base, 200ms ease);

        &:hover {
            background: var(--color-gray-100, #f3f4f6);
            color: var(--color-primary, #d82466);
        }

        &:focus-visible {
            outline: 2px solid var(--color-primary, #d82466);
            outline-offset: 2px;
        }
    }

    .cq-dp__year {
        color: var(--color-text-subtitle, #3d4784);
        font-weight: 600;
    }

    .cq-dp__view-label {
        font-size: 15px;
        font-weight: 700;
        color: var(--color-text-title, #050e42);
    }

    /* Nav buttons: ‹ prev (right side in RTL) and › next (left side in RTL) */
    .cq-dp__nav {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 32px;
        height: 32px;
        padding: 0;
        border: none;
        border-radius: var(--radius-xs, 8px);
        background: transparent;
        color: var(--color-secondary, #2d3461);
        cursor: pointer;
        flex-shrink: 0;
        transition: background var(--transition-base, 200ms ease), color var(--transition-base, 200ms ease);

        svg {
            display: block;
            flex-shrink: 0;
        }

        &:hover:not([disabled]) {
            background: var(--color-gray-100, #f3f4f6);
            color: var(--color-primary, #d82466);
        }

        &:active:not([disabled]) {
            background: var(--color-primary-light, #fce9f0);
            transform: scale(0.93);
        }

        &:focus-visible {
            outline: 2px solid var(--color-primary, #d82466);
            outline-offset: 2px;
        }

        &[disabled] {
            opacity: 0.3;
            cursor: not-allowed;
            pointer-events: none;
        }
    }

    /* ── Month / year selection grids ── */

    .cq-dp__months-grid,
    .cq-dp__years-grid {
        display: grid;
        gap: 6px;
        padding: 12px 16px 16px;
    }

    .cq-dp__months-grid {
        grid-template-columns: repeat(3, 1fr);
    }

    .cq-dp__years-grid {
        grid-template-columns: repeat(4, 1fr);
    }

    .cq-dp__month-cell,
    .cq-dp__year-cell {
        display: flex;
        align-items: center;
        justify-content: center;
        min-height: 40px;
        padding: 8px 6px;
        border: none;
        border-radius: var(--radius-xs, 8px);
        background: transparent;
        font-family: var(--font-primary, "Assistant", sans-serif);
        font-size: 13px;
        font-weight: 600;
        color: var(--color-text-title, #050e42);
        cursor: pointer;
        transition:
            background var(--transition-base, 200ms ease),
            color var(--transition-base, 200ms ease);

        &:hover:not([disabled]) {
            background: var(--color-primary-light, #fce9f0);
            color: var(--color-primary, #d82466);
        }

        &:focus-visible {
            outline: 2px solid var(--color-primary, #d82466);
            outline-offset: 1px;
        }

        &.cq-dp__month-cell--selected,
        &.cq-dp__year-cell--selected {
            background: var(--color-primary, #d82466);
            color: var(--color-white, #ffffff);
        }

        &.cq-dp__month-cell--disabled,
        &.cq-dp__year-cell--disabled {
            color: var(--color-disabled-text, #98a6b3);
            cursor: not-allowed;
            opacity: 0.45;
            pointer-events: none;
        }
    }

    .cq-dp__year-cell {
        font-size: 14px;
        font-weight: 700;
    }

    /* ── 7-column grid (day names + day buttons) ── */

    .cq-dp__grid {
        display: grid;
        grid-template-columns: repeat(7, 1fr);
        gap: 2px;
        padding: 8px 12px 14px;
    }

    /* Day-name header cells */
    .cq-dp__weekday {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 26px;
        font-size: 11px;
        font-weight: 600;
        color: var(--color-gray-400, #9ca3af);
        user-select: none;
        letter-spacing: 0;
    }

    /* ── Day buttons ── */

    .cq-dp__day {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        /* Use aspect-ratio so cells are always square regardless of grid column width */
        width: 100%;
        aspect-ratio: 1;
        padding: 0;
        border: none;
        border-radius: var(--radius-xs, 8px);
        background: transparent;
        font-family: var(--font-primary, "Assistant", sans-serif);
        font-size: 13px;
        font-weight: 500;
        color: var(--color-text-title, #050e42);
        cursor: pointer;
        transition:
            background var(--transition-base, 200ms ease),
            color var(--transition-base, 200ms ease);
        outline: none;

        &.cq-dp__day--empty {
            pointer-events: none;
            background: transparent;
        }

        &:hover:not([disabled]):not(.cq-dp__day--empty) {
            background: var(--color-primary-light, #fce9f0);
            color: var(--color-primary, #d82466);
        }

        &:focus-visible {
            outline: 2px solid var(--color-primary, #d82466);
            outline-offset: 1px;
        }

        /* Today indicator: bold text + small dot below the number */
        &.cq-dp__day--today:not(.cq-dp__day--selected) {
            font-weight: 700;
            color: var(--color-primary, #d82466);

            &::after {
                content: '';
                position: absolute;
                bottom: 3px;
                left: 50%;
                transform: translateX(-50%);
                width: 4px;
                height: 4px;
                background: var(--color-primary, #d82466);
                border-radius: 50%;
            }
        }

        /* Selected day */
        &.cq-dp__day--selected {
            background: var(--color-primary, #d82466);
            color: var(--color-white, #ffffff);
            font-weight: 700;

            &:hover:not([disabled]) {
                background: var(--color-primary-dark, #b01d52);
                color: var(--color-white, #ffffff);
            }

            /* Suppress today dot when selected */
            &::after {
                content: none;
            }
        }

        /* Disabled (outside min/max range) */
        &.cq-dp__day--disabled {
            color: var(--color-disabled-text, #98a6b3);
            cursor: not-allowed;
            opacity: 0.45;
            pointer-events: none;
        }

        /* ── Range mode styles ── */

        /* Start date (RTL: rightmost = start) */
        &.cq-dp__day--start {
            background: var(--color-primary, #d82466);
            color: var(--color-white, #ffffff) !important;
            font-weight: 700;
            border-radius: 0 8px 8px 0;

            &:hover:not([disabled]) {
                background: var(--color-primary-dark, #b01d52);
                color: var(--color-white, #ffffff);
            }
        }

        /* End date (RTL: leftmost = end) */
        &.cq-dp__day--end {
            background: var(--color-primary, #d82466);
            color: var(--color-white, #ffffff) !important;
            font-weight: 700;
            border-radius: 8px 0 0 8px;

            &:hover:not([disabled]) {
                background: var(--color-primary-dark, #b01d52);
                color: var(--color-white, #ffffff);
            }
        }

        /* Start and End are the same day */
        &.cq-dp__day--start.cq-dp__day--end {
            border-radius: 8px;
        }

        /* In-range days (between start and end) */
        &.cq-dp__day--in-range {
            background: rgba(216, 36, 102, 0.15);
            color: var(--color-text-title, #050e42);
            border-radius: 0;

            &:hover:not([disabled]) {
                background: rgba(216, 36, 102, 0.25);
            }
        }
    }

    /* ── Footer (range mode only) ── */

    .cq-dp__footer {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        gap: 10px;
        padding: 10px 16px 14px;
        border-top: 1px solid var(--color-gray-100, #f3f4f6);
    }

    .cq-dp__btn {
        padding: 9px 18px;
        border: none;
        border-radius: var(--radius-xs, 8px);
        font-size: 14px;
        font-weight: 600;
        font-family: var(--font-primary, "Assistant", sans-serif);
        cursor: pointer;
        transition: all var(--transition-base, 200ms ease);
        line-height: 1;

        &:focus-visible {
            outline: 2px solid var(--color-primary, #d82466);
            outline-offset: 2px;
        }

        &:active {
            transform: scale(0.97);
        }
    }

    .cq-dp__btn--cancel {
        background: transparent;
        color: var(--color-gray-600, #5a6976);

        &:hover {
            background: var(--color-gray-100, #f3f4f6);
            color: var(--color-text-title, #050e42);
        }
    }

    .cq-dp__btn--apply {
        background: var(--color-primary, #d82466);
        color: var(--color-white, #ffffff);

        &:hover {
            background: var(--color-primary-dark, #b01d52);
        }

        &:disabled {
            opacity: 0.5;
            cursor: not-allowed;
            pointer-events: none;
        }
    }

    /* ── Warning message ── */

    .cq-dp__warning {
        position: absolute;
        bottom: 8px;
        left: 50%;
        transform: translateX(-50%);
        background: #fff6e5;
        border: 1px solid #ffd591;
        color: #8a5a00;
        padding: 8px 12px;
        border-radius: 6px;
        font-size: 13px;
        font-weight: 500;
        font-family: var(--font-primary, "Assistant", sans-serif);
        z-index: 10;
        animation: cq-dp-warning-fade 2s ease-in-out;
    }

    /* ── Mobile centered popup (touch-friendly, not bottom sheet) ── */

    &.cq-dp--mobile {
        width: min(296px, calc(100vw - 32px));
        max-width: calc(100vw - 32px);
        max-height: calc(100dvh - 32px);
        overflow-y: auto;

        .cq-dp__day {
            font-size: 15px;
            min-height: 44px; /* WCAG 2.5.5 touch target */
        }

        .cq-dp__weekday {
            font-size: 12px;
            height: 32px;
        }
    }
}

/* ══════════════════════════════════════════════════════════════════
   MOBILE BACKDROP
   ══════════════════════════════════════════════════════════════════ */

.cq-dp-backdrop {
    position: fixed;
    inset: 0;
    z-index: 100001; /* below popup (100002), above modal (99999) */
    background: rgba(0, 0, 0, 0.35);
    opacity: 0;
    transition: opacity 180ms ease;
    pointer-events: none;

    &.cq-dp-backdrop--visible {
        opacity: 1;
        pointer-events: all;
    }
}

/* ══════════════════════════════════════════════════════════════════
   TRIGGER FIELD – open state
   ══════════════════════════════════════════════════════════════════ */

/*
 * When the picker is open, keep the .bcm-native-shell in its focus state
 * so the user can see which field is active.
 */
.cq-datepicker-open .bcm-native-shell {
    border-color: #3d4784 !important;
    box-shadow: 0 0 0 3px rgba(61, 71, 132, 0.16) !important;
}

/* ══════════════════════════════════════════════════════════════════
   REDUCED MOTION
   ══════════════════════════════════════════════════════════════════ */

/* Warning fade animation */
@keyframes cq-dp-warning-fade {
    0% {
        opacity: 0;
        transform: translateX(-50%) translateY(4px);
    }
    10% {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
    90% {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
    100% {
        opacity: 0;
        transform: translateX(-50%) translateY(-4px);
    }
}

@media (prefers-reduced-motion: reduce) {
    .clinic-queue-datepicker {
        transition: opacity 0ms;
    }

    .cq-dp-backdrop {
        transition: opacity 0ms;
    }

    @keyframes cq-dp-warning-fade {
        0% {
            opacity: 0;
        }
        10% {
            opacity: 1;
        }
        90% {
            opacity: 1;
        }
        100% {
            opacity: 0;
        }
    }
}
