/* ============================================================
   LangSwitcher Cartoon · 卡通圆形国旗 segmented pill
   创建: 2026-06-23 Hermes — 学 beidanci 规格
   追加: 不覆盖现有 components/LanguageSwitcher.js / lang-switcher.js
   前缀: lcs-* 避免跟现有 .lang-switcher-* 冲突
   v4 2026-06-23: 控件加大 + 适配性完备(5 断点 / 4 媒体查询 / 暗色 / 高对比 / 触屏 / 减少动画 / 打印)

   视觉:
     - 白底圆角 pill (borderRadius: 9999)
     - 蓝滑块 (#3b82f6) + 弹性过渡 (cubic-bezier 1.56 overshoot)
     - 卡通 inline SVG 圆形国旗 (radialGradient, 不用 emoji)
     - 选中: 国旗原色 + 白字 + 蓝滑块
     - 未选: 国旗降饱和 (saturate filter) + 灰字

   适配性 (v4 完备):
     - 5 个尺寸档: default 58 / compact 44 / mobile 50 / tablet 58 / desktop 62 / large 66
     - 触摸目标: ≥ 44pt (Apple HIG / WCAG AAA)
     - 触屏: 取消 hover 状态, 增强 :active 反馈
     - 暗色: prefers-color-scheme
     - 高对比: prefers-contrast: more (加边框 + 粗体)
     - 减少动画: prefers-reduced-motion
     - 减少数据: prefers-reduced-data (省 SVG 重绘)
     - 打印: @media print (隐藏阴影)
     - RTL: 跟随 dir="rtl" 自动 reverse
     - 极窄屏 <360: 强制 compact
   ============================================================ */

/* =============================================================
   1. 基础变量 (CSS custom properties, 支持 JS/媒体查询覆盖)
   ============================================================= */
.lcs-pill {
    /* 尺寸变量 (媒体查询内可重写) — v4 2026-06-23: 控件加大 20% */
    --lcs-pill-h: 58px;
    --lcs-pill-pad: 5px;
    --lcs-btn-h: 48px;
    --lcs-btn-pad-x: 14px;
    --lcs-btn-font: 16px;
    --lcs-flag: 48px;

    /* 颜色变量 */
    --lcs-bg: #ffffff;
    --lcs-shadow: 0 4px 14px rgba(0, 0, 0, 0.08), 0 1px 3px rgba(0, 0, 0, 0.04);
    --lcs-shadow-hover: 0 8px 22px rgba(0, 0, 0, 0.14), 0 2px 6px rgba(0, 0, 0, 0.06);
    --lcs-knob-bg: #3b82f6;
    --lcs-knob-shadow: 0 2px 8px rgba(59, 130, 246, 0.4);
    --lcs-text: #6b7280;
    --lcs-text-hover: #1f2937;
    --lcs-text-active: #ffffff;
    --lcs-flag-ring: rgba(0, 0, 0, 0.08);
    --lcs-hover-text: #be185d;     /* 仅文字色变化, 不再设粉色背景 */
    --lcs-active-press: scale(0.97);

    /* ===== 容器本体 ===== */
    position: relative;
    display: inline-flex;
    align-items: center;
    background: var(--lcs-bg);
    border-radius: 9999px;
    padding: var(--lcs-pill-pad);
    box-shadow: var(--lcs-shadow);
    user-select: none;
    -webkit-user-select: none;
    height: var(--lcs-pill-h);
    transition: box-shadow 0.3s ease, background 0.3s ease, transform 0.32s cubic-bezier(0.34, 1.56, 0.64, 1);
    /* 极小屏滚动保护 */
    max-width: 100%;
    box-sizing: border-box;
    /* hover 浮起用 transform, 不影响滑块 left/width 定位 */
    will-change: transform;
}

/* 真鼠标设备: hover 浮起 + 阴影加深 (跟国旗 wiggle 同步) */
@media (hover: hover) and (pointer: fine) {
    .lcs-pill:hover {
        box-shadow: var(--lcs-shadow-hover);
        transform: translateY(-2px);
    }
    /* 按下时不要浮起 (回到原位) — 提供"按下去"反馈 */
    .lcs-pill:active {
        transform: translateY(0);
    }
}

/* =============================================================
   2. 尺寸档 — 5 档 (通过 :not() 排除 compact, 默认 58px)
   ============================================================= */

/* 紧凑模式 (header 用) — v4: 44px (旧 36) */
.lcs-pill.lcs-compact {
    --lcs-pill-h: 44px;
    --lcs-pill-pad: 4px;
    --lcs-btn-h: 36px;
    --lcs-btn-pad-x: 10px;
    --lcs-btn-font: 14px;
    --lcs-flag: 34px;
}

/* 极窄屏 (<360): 强制 compact, 避免横向溢出 */
@media (max-width: 360px) {
    .lcs-pill:not(.lcs-no-force-compact) {
        --lcs-pill-h: 44px;
        --lcs-pill-pad: 4px;
        --lcs-btn-h: 36px;
        --lcs-btn-pad-x: 10px;
        --lcs-btn-font: 14px;
        --lcs-flag: 34px;
    }
}

/* 移动 (< 480): 50px 触摸目标 (旧 44) */
@media (min-width: 361px) and (max-width: 480px) {
    .lcs-pill {
        --lcs-pill-h: 50px;
        --lcs-pill-pad: 4px;
        --lcs-btn-h: 42px;
        --lcs-btn-pad-x: 12px;
        --lcs-btn-font: 15px;
        --lcs-flag: 40px;
    }
}

/* 平板 (481-1024): 58px (旧 48) */
@media (min-width: 481px) and (max-width: 1024px) {
    .lcs-pill {
        --lcs-pill-h: 58px;
        --lcs-pill-pad: 5px;
        --lcs-btn-h: 48px;
        --lcs-btn-pad-x: 14px;
        --lcs-btn-font: 16px;
        --lcs-flag: 48px;
    }
}

/* 桌面 (1025-1279): 62px (旧 50) */
@media (min-width: 1025px) and (max-width: 1279px) {
    .lcs-pill {
        --lcs-pill-h: 62px;
        --lcs-pill-pad: 5px;
        --lcs-btn-h: 52px;
        --lcs-btn-pad-x: 16px;
        --lcs-btn-font: 17px;
        --lcs-flag: 52px;
    }
}

/* 大屏 (> 1280): 66px, 国旗更显眼 (旧 52) */
@media (min-width: 1280px) {
    .lcs-pill {
        --lcs-pill-h: 66px;
        --lcs-pill-pad: 5px;
        --lcs-btn-h: 56px;
        --lcs-btn-pad-x: 18px;
        --lcs-btn-font: 18px;
        --lcs-flag: 56px;
    }
}

/* =============================================================
   3. 滑块 — JS 算 left/width, CSS 只管过渡
   ============================================================= */
.lcs-knob {
    position: absolute;
    top: var(--lcs-pill-pad);
    left: 0;
    width: 0;
    height: calc(100% - var(--lcs-pill-pad) * 2);
    background: var(--lcs-knob-bg);
    border-radius: 9999px;
    box-shadow: var(--lcs-knob-shadow);
    z-index: 0;
    pointer-events: none;
    transition:
        left 0.32s cubic-bezier(0.34, 1.56, 0.64, 1),
        width 0.32s cubic-bezier(0.34, 1.56, 0.64, 1),
        opacity 0.2s ease;
    opacity: 0;
    will-change: left, width;
}

.lcs-knob.lcs-ready {
    opacity: 1;
}

/* =============================================================
   4. 按钮 — 滑块之上, 默认灰字, 选中白字
   ============================================================= */
.lcs-btn {
    position: relative;
    z-index: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0;
    min-width: 0;
    height: var(--lcs-btn-h);
    padding: 0 var(--lcs-btn-pad-x);
    background: transparent;
    border: none;
    cursor: pointer;
    font-family: inherit;
    font-size: var(--lcs-btn-font);
    font-weight: 600;
    color: var(--lcs-text);
    white-space: nowrap;
    letter-spacing: 0.02em;
    transition: color 0.2s ease, background 0.2s ease, transform 0.15s ease;
    -webkit-tap-highlight-color: transparent;
    /* 触摸目标兜底 (min 44pt for a11y) */
    min-height: 44px;
}

/* 非 compact: 按钮高度可能被父容器压, 强制保持触摸目标 */
.lcs-pill:not(.lcs-compact) .lcs-btn {
    min-height: 44px;
    align-items: center;
}

.lcs-pill.lcs-compact .lcs-btn {
    min-height: 32px;
    padding: 0 6px;
    font-size: 12px;
}

.lcs-btn:focus,
.lcs-btn:focus-visible {
    outline: none;
    box-shadow: none;
}

/* 键盘焦点 — 蓝色 outline (a11y 必备) */
.lcs-btn:focus-visible {
    outline: 2px solid var(--lcs-knob-bg);
    outline-offset: 2px;
    border-radius: 9999px;
}

/* 真鼠标: hover 未选态 — 国旗 wiggle 晃动动画 (无粉背景) */
@media (hover: hover) and (pointer: fine) {
    .lcs-btn:hover:not(.lcs-active) {
        color: var(--lcs-hover-text);
        /* 不再设背景 — 改用国旗晃动动画 */
    }
    .lcs-btn:hover:not(.lcs-active) .lcs-flag {
        animation: lcs-wiggle 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) both;
    }
    /* 已激活的不动 */
    .lcs-btn.lcs-active:hover .lcs-flag {
        animation: none;
    }
}

/* wiggle 关键帧 — 左右 6° 摇 3 下, 跟规格文档里 wave 类动效一致 */
@keyframes lcs-wiggle {
    0%   { transform: rotate(0deg)   scale(1); }
    15%  { transform: rotate(-7deg)  scale(1.06); }
    30%  { transform: rotate(6deg)   scale(1.08); }
    45%  { transform: rotate(-5deg)  scale(1.06); }
    60%  { transform: rotate(4deg)   scale(1.05); }
    75%  { transform: rotate(-2deg)  scale(1.03); }
    100% { transform: rotate(0deg)   scale(1.05); }
}

/* v5: 切换 fly-in 关键帧 */
@keyframes lcs-flag-out {
    0%   { opacity: 1; transform: translateY(0) scale(1); }
    100% { opacity: 0; transform: translateY(-12px) scale(0.7); }
}
@keyframes lcs-flag-in {
    0%   { opacity: 0; transform: translateY(12px) scale(0.7); }
    60%  { opacity: 1; transform: translateY(-2px) scale(1.08); }
    100% { opacity: 1; transform: translateY(0) scale(1); }
}

/* v5: 旧国旗 fade-out (跟滑块 start 同步) */
.lcs-flag--out {
    animation: lcs-flag-out 0.18s cubic-bezier(0.55, 0, 0.7, 0.3) forwards;
    pointer-events: none;
}

/* v5: 新国旗 fade-in (跟滑块中段同步) */
.lcs-flag--in {
    animation: lcs-flag-in 0.24s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.lcs-btn.lcs-active {
    color: var(--lcs-text-active);
}

/* 触屏 active 反馈 (无 hover 设备) — 不加粉背景, 轻微下沉即可 */
@media (hover: none) {
    .lcs-btn:active:not(.lcs-active) {
        transform: scale(0.97);
    }
    .lcs-btn:active.lcs-active {
        transform: scale(0.97);
    }
}

/* 鼠标按下: 所有设备轻微下沉 + 国旗额外 scale 反馈 */
.lcs-btn:active {
    transform: var(--lcs-active-press);
}
.lcs-btn:active .lcs-flag {
    transform: scale(0.82);
    transition-duration: 0.08s;
}

/* =============================================================
   5. 国旗 — 圆形 inline SVG, 满按钮高度
   ============================================================= */
.lcs-flag {
    width: var(--lcs-flag);
    height: var(--lcs-flag);
    flex-shrink: 0;
    overflow: hidden;
    border-radius: 50%;
    box-shadow: 0 0 0 1px var(--lcs-flag-ring);
    transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.2s ease, filter 0.2s ease;
    display: inline-block;
    /* iOS Safari 渲染 SVG 圆形 clip 需要 */
    -webkit-mask-image: -webkit-radial-gradient(circle, white 100%, black 100%);
}

/* 未选: 降饱和保色彩 (saturate 0.6 + 提亮, 比 v2 的 0.45 更接近参考图) */
.lcs-btn:not(.lcs-active) .lcs-flag {
    filter: saturate(0.6) brightness(1.0);
}

.lcs-btn.lcs-active .lcs-flag {
    box-shadow: none;
    filter: none;
}

/* =============================================================
   6. 文字 label
   ============================================================= */
.lcs-label {
    transition: letter-spacing 0.2s ease;
}

.lcs-pill.lcs-compact .lcs-label {
    display: none;
}

/* 极窄屏: 隐藏 label, 减少宽度 */
@media (max-width: 360px) {
    .lcs-pill:not(.lcs-no-force-compact) .lcs-label {
        display: none;
    }
}

/* =============================================================
   7. 暗色模式 — prefers-color-scheme: dark
   ============================================================= */
@media (prefers-color-scheme: dark) {
    .lcs-pill {
        --lcs-bg: #1f2937;
        --lcs-shadow: 0 4px 14px rgba(0, 0, 0, 0.4), 0 1px 3px rgba(0, 0, 0, 0.25);
        --lcs-shadow-hover: 0 8px 22px rgba(0, 0, 0, 0.5), 0 2px 6px rgba(0, 0, 0, 0.3);
        --lcs-knob-shadow: 0 2px 8px rgba(59, 130, 246, 0.55);
        --lcs-text: #9ca3af;
        --lcs-text-hover: #e5e7eb;
        --lcs-flag-ring: rgba(255, 255, 255, 0.12);
        --lcs-hover-text: #f9a8d4;
    }
}

/* =============================================================
   8. 高对比模式 — prefers-contrast: more
   ============================================================= */
@media (prefers-contrast: more) {
    .lcs-pill {
        border: 2px solid currentColor;
    }
    .lcs-btn {
        font-weight: 700;
    }
    .lcs-btn.lcs-active {
        text-decoration: underline;
    }
    .lcs-flag {
        box-shadow: 0 0 0 2px currentColor;
    }
}

/* =============================================================
   9. 减少动画 — prefers-reduced-motion
   ============================================================= */
@media (prefers-reduced-motion: reduce) {
    .lcs-pill,
    .lcs-knob,
    .lcs-btn,
    .lcs-flag,
    .lcs-label {
        transition-duration: 0.1s !important;
        animation-duration: 0.1s !important;
    }
    .lcs-btn:hover .lcs-flag,
    .lcs-btn:active .lcs-flag {
        transform: none !important;
        animation: none !important;
    }
    /* v5: fly-in/out 关键帧也关 */
    .lcs-flag--out,
    .lcs-flag--in {
        animation: none !important;
        opacity: 1 !important;
        transform: none !important;
    }
}

/* =============================================================
   10. 减少数据 — prefers-reduced-data (省 SVG 重绘)
   ============================================================= */
@media (prefers-reduced-data: reduce) {
    .lcs-pill {
        box-shadow: none;
    }
    .lcs-knob {
        box-shadow: none;
    }
    .lcs-flag {
        box-shadow: none;
    }
}

/* =============================================================
   11. 打印模式 — 简化输出
   ============================================================= */
@media print {
    .lcs-pill {
        box-shadow: none;
        border: 1px solid #000;
    }
    .lcs-knob {
        display: none;
    }
    .lcs-btn {
        color: #000 !important;
        background: transparent !important;
    }
    .lcs-btn.lcs-active {
        font-weight: 700;
    }
    .lcs-btn:not(.lcs-active) {
        display: none;
    }
    .lcs-flag {
        filter: none !important;
    }
}

/* =============================================================
   12. 强制色模式 — forced-colors (Windows 高对比)
   ============================================================= */
@media (forced-colors: active) {
    .lcs-pill {
        border: 1px solid CanvasText;
    }
    .lcs-knob {
        background: Highlight;
    }
    .lcs-btn.lcs-active {
        color: HighlightText;
    }
}

/* =============================================================
   13. RTL 适配 (自动, 用 [dir="rtl"] 验证)
   ============================================================= */
.lcs-pill[dir="rtl"] .lcs-knob,
[dir="rtl"] .lcs-pill .lcs-knob {
    /* 滑块定位用 left (JS 算), RTL 下视觉仍正确, 不需要 transform */
}

/* =============================================================
   14. .lcs-pill-outer — 拖动 + X 关闭的契约容器 (data-fab opt-in)
   创建: 2026-06-25 Hermes — 对齐 lang-switcher-outer + fab-close-btn
   行为:
     - 默认 position: relative (inline 在 nav, 不破坏现有布局)
     - 首次拖动时加 .lcs-pill-detached → position: fixed + z-index 抬高
     - X 按钮 (.fab-close-btn .fab-close-lcs-header) 由 floating-fabs.js 注入
   ============================================================= */
.lcs-pill-outer {
    position: relative;
    display: inline-flex;
    align-items: center;
    cursor: grab;              /* 学 lang-switcher-outer, 抓手指示 */
    touch-action: none;        /* iOS Safari 防拖动时 scroll 抢手势 */
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}
.lcs-pill-outer:active,
.lcs-pill-outer.dragging {
    cursor: grabbing;
}

/* 脱钩: 拖动一次后切到 fixed, 自由摆放 */
.lcs-pill-outer.lcs-pill-detached {
    position: fixed;
    z-index: 2147483646;       /* 跟 lang-switcher-outer 同档, 仅低 1 */
    margin: 0;
}

/* 外层里的 X 按钮: 桌面 hover 弹出, 触屏永久显示
   (基础样式复用 floating-fabs.css 里的 .fab-close-btn) */
.lcs-pill-outer:hover .fab-close-lcs-header {
    opacity: 1;
    transform: scale(1);
    animation: fab-close-bounceIn 0.5s ease-out;
}

/* 触屏/窄屏: X 按钮永远显示, 跟 .fab-close-btn 的 @media (max-width:768px) 配合 */
@media (max-width: 768px) {
    .lcs-pill-outer .fab-close-lcs-header {
        opacity: 1 !important;
        transform: scale(1) !important;
    }
}
