/* ============ 基础 ============ */
body {
    margin: 0;
    overflow: hidden;
    background: radial-gradient(circle at center, #0f2027 0%, #203a43 50%, #2c5364 100%);
    background-color: #050d1a;
    font-family: 'Times New Roman', serif;
}

#canvas-container {
    position: absolute;
    inset: 0;
    z-index: 1;
}

/* ============ 加载遮罩 ============ */
#loader {
    position: absolute;
    inset: 0;
    background: #050d1a;
    z-index: 100;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    transition: opacity 0.8s ease-out;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 1px solid rgba(212, 175, 55, 0.2);
    border-top: 1px solid #d4af37;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.loader-text {
    color: #d4af37;
    font-size: 14px;
    letter-spacing: 4px;
    margin-top: 20px;
    text-transform: uppercase;
    font-weight: 100;
}

/* 尊重「减少动态效果」的系统偏好 */
@media (prefers-reduced-motion: reduce) {
    .spinner { animation: none; }
    /* 按钮按压只保留颜色反馈，取消位移与缩放 */
    .upload-btn:active:not(:disabled) { transform: none; }
}

/* ============ UI 层 ============ */
#ui-layer {
    position: absolute;
    inset: 0;
    z-index: 10;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 40px;
    box-sizing: border-box;
    transition: opacity 0.5s ease;
}

h1 {
    color: #fceea7;
    font-size: clamp(28px, 6vw, 56px);
    margin: 0;
    font-weight: 400;
    letter-spacing: 6px;
    text-shadow: 0 0 50px rgba(252, 238, 167, 0.6);
    background: linear-gradient(to bottom, #fff, #eebb66);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    font-family: 'Cinzel', 'Times New Roman', serif;
    opacity: 0.9;
    text-align: center;
}

/* ============ 控制按钮 ============ */
/* 桌面：竖排工具列靠右、垂直居中，标题在顶部居中，互不遮挡 */
.controls-wrapper {
    position: absolute;
    top: 50%;
    right: 30px;
    transform: translateY(-50%);
    width: 132px;
    pointer-events: auto;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    z-index: 20;
    transition: opacity 0.5s ease;
}

.btn-group {
    display: flex;
    flex-direction: column;
    width: 100%;
    gap: 8px;
}

.upload-btn {
    background: rgba(20, 20, 20, 0.6);
    border: 1px solid rgba(212, 175, 55, 0.4);
    color: #d4af37;
    padding: 8px 10px;
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 1px;
    font-size: 10px;
    transition: all 0.4s;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(5px);
    min-width: 0;
    white-space: nowrap;
}

/* 实心金填充只在真正的鼠标 hover 时出现，键盘焦点不共用这组样式 */
@media (hover: hover) and (pointer: fine) {
    .upload-btn:hover {
        background: #d4af37;
        color: #000;
        box-shadow: 0 0 20px rgba(212, 175, 55, 0.5);
    }
}

/* 键盘焦点：空心外环，保持深色底，与 hover 明确区分 */
.upload-btn:focus-visible,
.upload-btn:has(input:focus-visible) {
    outline: 2px solid #fceea7;
    outline-offset: 3px;
}

/* 不支持 :has() 的老浏览器：用 :focus-within 兜住 label 里的隐藏 input */
@supports not selector(:has(*)) {
    .upload-btn:focus-within {
        outline: 2px solid #fceea7;
        outline-offset: 3px;
    }
}

/* 按压态：短促、下沉，禁用时不响应 */
.upload-btn:active:not(:disabled) {
    transform: translateY(1px) scale(0.985);
    background: #b8912e;
    box-shadow: none;
    transition-duration: 60ms;
}

.upload-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* 预览开关的开启态（hover 时让位给通用 hover 高亮） */
.upload-btn#webcam-toggle[aria-pressed="true"]:not(:hover) {
    background: rgba(212, 175, 55, 0.18);
    border-color: rgba(212, 175, 55, 0.7);
    color: #fceea7;
    box-shadow: 0 0 14px rgba(212, 175, 55, 0.18);
}

/* 摄像头降级时预览开关也不可用 */
body.webcam-disabled #webcam-toggle {
    opacity: 0.5;
    pointer-events: none;
}

/* 小屏用短标签，完整名称由 aria-label 提供给读屏 */
.label-short { display: none; }

/* 视觉隐藏但读屏可访问：一次性结果播报 */
.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;
}

.hint-text {
    color: rgba(212, 175, 55, 0.5);
    font-size: 9px;
    letter-spacing: 1px;
    text-transform: uppercase;
    text-align: right;
    line-height: 1.6;
}

input[type='file'] {
    /* 不能用 display:none，否则键盘用户无法聚焦到 label 里的 input */
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
    pointer-events: none;
}

/* ============ 摄像头预览 ============ */
#webcam-wrapper {
    position: absolute;
    bottom: 30px;
    left: 30px;
    width: 280px;
    height: 210px;
    border: 1px solid rgba(212, 175, 55, 0.5);
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.9);
    border-radius: 4px;
    overflow: hidden;
    pointer-events: none;
    z-index: 50;
    background: #000;
    transition: opacity 0.5s ease;
}

#webcam {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transform: scaleX(-1); /* 镜像，符合照镜子的直觉 */
}

/* 状态条：底部居中。层级 canvas1 < ui10 < controls20 < webcam50 < status60 */
#status-info {
    position: fixed;
    z-index: 60;
    left: 50%;
    bottom: 24px;
    transform: translateX(-50%);
    max-width: min(560px, calc(100vw - 40px));
    box-sizing: border-box;
    padding: 8px 14px;
    color: #fceea7;
    background: rgba(5, 13, 26, 0.82);
    border: 1px solid rgba(212, 175, 55, 0.35);
    border-radius: 4px;
    backdrop-filter: blur(8px);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.45);
    font-size: 12px;
    line-height: 1.5;
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    pointer-events: none;
}

#status-info::before {
    content: '';
    display: inline-block;
    width: 6px;
    height: 6px;
    margin-right: 8px;
    border-radius: 50%;
    background: #d4af37;
    vertical-align: middle;
}

/* 摄像头不可用后不再允许任何方式把它显示出来（降级后按 C/点预览也不行） */
body.webcam-disabled #webcam-wrapper {
    display: none !important;
}

/* ============ 快捷键状态 ============ */
/* H：隐藏所有 UI，用于截图 / 录屏 */
/*
 * 只写 opacity:0 是不够的：.controls-wrapper 自己声明了 pointer-events:auto，
 * 会盖过从祖先继承来的 none，于是「隐藏」后的按钮依然会吃掉点击 ——
 * 想拖拽旋转却打开了文件选择框。visibility:hidden 既能真正取消命中测试，
 * 也能让按钮退出 Tab 顺序；延迟 0.5s 生效是为了不打断淡出动画。
 */
.ui-hidden-all #ui-layer,
.ui-hidden-all #webcam-wrapper,
.ui-hidden-all #status-info {
    opacity: 0;
    pointer-events: none;
    visibility: hidden;
    transition: opacity 0.5s ease, visibility 0s linear 0.5s;
}

/*
 * 这条必须单独写，且不能依赖上面那个带延迟的 visibility：
 * 一是 .controls-wrapper 自己的 pointer-events:auto 会盖过祖先的 none，
 * 二是 visibility 挂在 transition 上，延迟期间（以及标签页被浏览器冻结时）
 * 按钮依然可点。pointer-events 不参与过渡，写在这里是立即生效的。
 */
.ui-hidden-all .controls-wrapper {
    pointer-events: none;
}

/* D：手势原始数值默认隐藏，调阈值时才打开 */
#debug-info {
    position: absolute;
    top: 5px;
    left: 5px;
    display: none;
    color: rgba(212, 175, 55, 0.9);
    font-size: 10px;
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    background: rgba(0, 0, 0, 0.6);
    padding: 2px 5px;
    pointer-events: none;
}

.debug-visible #debug-info {
    display: block;
}

/* ============ 小屏适配 ============ */
@media (max-width: 720px) {
    .controls-wrapper {
        top: auto;
        bottom: 20px;
        right: 20px;
        left: 20px;
        transform: none; /* 取消桌面的垂直居中，否则按钮会往上偏 */
        width: auto; /* 取消桌面固定列宽，恢复满宽横排 */
        align-items: stretch;
        gap: 8px;
    }

    .btn-group {
        flex-direction: row; /* 小屏回到横排，按钮在底部不碰标题 */
        width: auto;
        justify-content: stretch;
        flex-wrap: wrap; /* 窄屏下宁可换行，也不要把按钮挤出屏幕 */
        gap: 8px;
    }

    .upload-btn {
        flex: 1 1 auto;
        min-width: 0;
        padding: 9px 10px;
        letter-spacing: 1px;
    }

    .label-full { display: none; }
    .label-short { display: inline; }
    .hint-text { text-align: center; }

    /* 小屏底部被控件占满，状态条固定到顶部；预览展开时再让位到其下方 */
    #status-info {
        top: 86px;
        bottom: auto;
        left: 50%;
        transform: translateX(-50%);
        font-size: 11px;
    }

    body.webcam-shown #status-info {
        top: 135px;
    }

    /* 小屏默认不显示预览，由「预览」按钮 / C 键显式开启 */
    #webcam-wrapper {
        display: none;
        width: 140px;
        height: 105px;
        bottom: auto;
        top: 20px;
        left: 20px;
    }

    body.webcam-shown #webcam-wrapper {
        display: block;
        top: 20px;
        left: 20px;
        width: 140px;
        height: 105px;
    }
}

/* 中段：右下是恒定空白区（预览左下、控件右上、标题上中），状态条靠右避开预览 */
@media (min-width: 721px) and (max-width: 1100px) {
    /* 中等宽度下标题与右侧竖排工具列容易相碰，收紧字号与字距留出间隙 */
    h1 {
        font-size: clamp(28px, 4.5vw, 44px);
        letter-spacing: 4px;
    }

    .controls-wrapper {
        width: 112px;
    }

    #status-info {
        left: auto;
        right: 24px;
        transform: none;
        max-width: min(420px, calc(100vw - 360px));
    }
}

/* ============ 首次引导 ============ */
#onboarding {
    position: absolute;
    inset: 0;
    z-index: 80;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    box-sizing: border-box;
    background: rgba(5, 13, 26, 0.72);
    backdrop-filter: blur(6px);
    animation: fade-in 0.5s ease both;
}

#onboarding[hidden] {
    display: none;
}

.onboarding-card {
    max-width: 440px;
    width: 100%;
    padding: 32px;
    box-sizing: border-box;
    border: 1px solid rgba(212, 175, 55, 0.35);
    border-radius: 6px;
    background: rgba(10, 16, 28, 0.9);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
    text-align: center;
}

.onboarding-card h2 {
    margin: 0 0 24px;
    color: #fceea7;
    font-family: 'Cinzel', 'Times New Roman', serif;
    font-size: 20px;
    font-weight: 400;
    letter-spacing: 3px;
}

.gesture-list {
    list-style: none;
    margin: 0 0 22px;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.gesture-list li {
    display: grid;
    grid-template-columns: 40px 1fr;
    grid-template-rows: auto auto;
    align-items: center;
    gap: 2px 14px;
    text-align: left;
}

.gesture-emoji {
    grid-row: 1 / 3;
    font-size: 30px;
    line-height: 1;
    text-align: center;
}

.gesture-list strong {
    color: #d4af37;
    font-size: 13px;
    letter-spacing: 1px;
    font-weight: 500;
}

.gesture-list li > span:last-child {
    color: rgba(255, 255, 255, 0.6);
    font-size: 12px;
    line-height: 1.5;
}

.onboarding-note {
    margin: 0 0 24px;
    color: rgba(255, 255, 255, 0.45);
    font-size: 11px;
    line-height: 1.7;
}

.onboarding-note kbd {
    display: inline-block;
    padding: 1px 5px;
    border: 1px solid rgba(212, 175, 55, 0.4);
    border-radius: 3px;
    color: #d4af37;
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: 10px;
}

.onboarding-start {
    margin: 0 auto;
    min-width: 160px;
}

@keyframes fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
    #onboarding { animation: none; }
}
