/**
 * Language switcher — user-dropdown-integrated side panel (Phase 3 redesign)
 *
 * Anchored to .lang-switcher-wrapper (position: relative) inside the user
 * dropdown menu. The trigger row uses the existing .dropdown-item pattern
 * and the side panel opens to the LEFT of the trigger.
 *
 * Mobile fallback (≤480px): the panel becomes viewport-anchored to avoid
 * overflow on narrow screens; uses the canonical 480px breakpoint per
 * CLAUDE.md rule 14.
 */

/* ============================================================
   Trigger — self-contained, container-agnostic
   Works both inside .user-dropdown-menu and standalone in a header.
   ============================================================ */

.lang-switcher-wrapper {
    position: relative;
}

.lang-switcher-trigger {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary, #64748b);
    background: transparent;
    border: 1px solid transparent;
    border-radius: 8px;
    cursor: pointer;
    text-align: left;
    transition: all var(--anim-duration-fast, 0.15s);
}

.lang-switcher-trigger:hover {
    color: var(--brand-primary, #6366f1);
    background: var(--brand-primary-soft, rgba(99, 102, 241, 0.08));
}

.lang-switcher-trigger:focus-visible {
    outline: 2px solid var(--brand-primary, #6366f1);
    outline-offset: 1px;
}

/* When used inside the user dropdown menu, match the full-width row layout
   and stack label + current locale vertically (more space-efficient than
   horizontal layout in a narrow dropdown). */
.user-dropdown-menu .lang-switcher-trigger {
    display: flex;
    width: 100%;
    padding: 10px 14px;
    gap: 10px;
    border-radius: 8px;
}

.user-dropdown-menu .lang-switcher-trigger .lang-switcher-text {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-width: 0; /* allow ellipsis on children */
    align-items: flex-start;
}

.user-dropdown-menu .lang-switcher-trigger .lang-switcher-label {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-primary, #1e293b);
    line-height: 1.3;
    white-space: nowrap; /* prevent CJK character-level wrapping */
}

.user-dropdown-menu .lang-switcher-trigger .lang-switcher-current {
    margin: 2px 0 0 0; /* small gap below label */
    font-size: 11px;
    font-weight: 400;
    color: var(--text-tertiary, #94a3b8);
    line-height: 1.3;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.user-dropdown-menu .lang-switcher-trigger:hover .lang-switcher-label {
    color: var(--brand-primary, #6366f1);
}

.lang-switcher-trigger .lang-switcher-icon {
    flex-shrink: 0;
    color: var(--text-tertiary, #94a3b8);
    transition: color var(--anim-duration-fast, 0.15s);
}

.lang-switcher-trigger:hover .lang-switcher-icon,
.lang-switcher-trigger[aria-expanded="true"] .lang-switcher-icon {
    color: var(--brand-primary, #6366f1);
}

.lang-switcher-trigger .lang-switcher-label {
    flex: 0 1 auto;
    margin-right: auto;
}

.lang-switcher-trigger .lang-switcher-current {
    flex-shrink: 0;
    margin-left: 8px;
    font-size: 12px;
    color: var(--text-tertiary, #94a3b8);
    font-weight: 400;
    max-width: 96px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.lang-switcher-trigger .lang-switcher-chevron {
    flex-shrink: 0;
    margin-left: 4px;
    color: var(--text-tertiary, #94a3b8);
    transition: transform var(--anim-duration-fast, 0.15s),
                color var(--anim-duration-fast, 0.15s);
}

.lang-switcher-trigger[aria-expanded="true"] .lang-switcher-chevron {
    color: var(--brand-primary, #6366f1);
    transform: rotate(180deg);
}

/* ============================================================
   Side panel — opens to the LEFT of the trigger row
   ============================================================ */

.lang-switcher-panel {
    /* Default (standalone in a header): panel opens BELOW the trigger */
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    min-width: 240px;
    max-width: calc(100vw - 32px);
    background: var(--bg-white, #ffffff);
    border: 1px solid var(--border-light, #e2e8f0);
    border-radius: 12px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.12);
    padding: 8px;
    z-index: 1100; /* above .user-dropdown-menu (1000) */

    /* Default hidden state — controlled by aria-hidden */
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transform: translateX(4px);
    transition: opacity var(--anim-duration-normal, 0.2s) cubic-bezier(0.16, 1, 0.3, 1),
                transform var(--anim-duration-normal, 0.2s) cubic-bezier(0.16, 1, 0.3, 1),
                visibility 0s linear var(--anim-duration-normal, 0.2s);
}

.lang-switcher-panel[aria-hidden="false"] {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transform: translateX(0);
    transition: opacity var(--anim-duration-normal, 0.2s) cubic-bezier(0.16, 1, 0.3, 1),
                transform var(--anim-duration-normal, 0.2s) cubic-bezier(0.16, 1, 0.3, 1),
                visibility 0s linear 0s;
}

/* When the panel lives inside the user dropdown, open to the LEFT instead
   so we don't extend below the dropdown menu. */
.user-dropdown-menu .lang-switcher-panel {
    top: 0;
    right: calc(100% + 8px);
}

/* ============================================================
   Individual locale item — name + subtitle + checkmark
   ============================================================ */

.lang-switcher-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 10px 12px;
    background: transparent;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    text-align: left;
    transition: background var(--anim-duration-fast, 0.15s),
                color var(--anim-duration-fast, 0.15s);
}

.lang-switcher-item + .lang-switcher-item {
    margin-top: 2px;
}

.lang-switcher-item:hover {
    background: var(--brand-primary-soft, rgba(99, 102, 241, 0.08));
}

.lang-switcher-item:focus-visible {
    outline: 2px solid var(--brand-primary, #6366f1);
    outline-offset: 1px;
}

.lang-switcher-item[aria-checked="true"] {
    background: var(--ai-soft, rgba(167, 139, 250, 0.06));
}

.lang-switcher-item-text {
    flex: 1 1 auto;
    min-width: 0; /* allows ellipsis on overflow */
}

.lang-switcher-item-name {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-primary, #1e293b);
    line-height: 1.3;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.lang-switcher-item-subtitle {
    margin-top: 2px;
    font-size: 11px;
    color: var(--text-tertiary, #94a3b8);
    line-height: 1.3;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.lang-switcher-item[aria-checked="true"] .lang-switcher-item-name {
    color: var(--ai-lavender, #A78BFA);
}

.lang-switcher-item-check {
    flex-shrink: 0;
    color: var(--ai-lavender, #A78BFA);
    opacity: 0;
    transform: scale(0.6);
    transition: opacity var(--anim-duration-fast, 0.15s),
                transform var(--anim-duration-fast, 0.15s) cubic-bezier(0.16, 1, 0.3, 1);
}

.lang-switcher-item[aria-checked="true"] .lang-switcher-item-check {
    opacity: 1;
    transform: scale(1);
}

/* ============================================================
   Mobile fallback (≤480px) — panel anchors to viewport edge to
   prevent overflow when the parent dropdown is right-aligned and
   there isn't 240px of horizontal room.
   ============================================================ */

@media (max-width: 480px) {
    .lang-switcher-panel {
        /* Pin to the right edge of the viewport with a small inset; the
           transition still slides from the parent dropdown. */
        right: auto;
        left: 50%;
        top: calc(100% + 8px);
        transform: translateX(-50%) translateY(-4px);
        min-width: min(280px, calc(100vw - 32px));
    }

    .lang-switcher-panel[aria-hidden="false"] {
        transform: translateX(-50%) translateY(0);
    }
}

/* ============================================================
   Respect prefers-reduced-motion
   ============================================================ */

@media (prefers-reduced-motion: reduce) {
    .lang-switcher-panel,
    .lang-switcher-item,
    .lang-switcher-item-check,
    .lang-switcher-trigger .lang-switcher-chevron {
        transition: none;
    }
}
