/* ============ 基础 ============ */
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; }
}

/* ============ 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', 'Songti SC', 'STSong', 'SimSun', 'Times New Roman', serif;
    opacity: 0.9;
    text-align: center;
}

/* 定制版副标题：两个人的名字。字距靠 text-indent 补掉末字后的空隙，才真的居中 */
.subtitle {
    margin: 14px 0 0;
    color: rgba(212, 175, 55, 0.82);
    font-family: 'Songti SC', 'STSong', 'SimSun', 'Noto Serif CJK SC', serif;
    font-size: clamp(13px, 2.4vw, 19px);
    font-weight: 400;
    letter-spacing: 0.42em;
    text-indent: 0.42em;
    text-align: center;
    text-shadow: 0 0 24px rgba(255, 217, 102, 0.35);
}

/* 底部署名：日期 / 落款 */
.signature {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 26px;
    margin: 0;
    color: rgba(212, 175, 55, 0.45);
    font-family: 'Cinzel', 'Songti SC', 'Times New Roman', serif;
    font-size: 10px;
    letter-spacing: 3px;
    text-align: center;
    pointer-events: none;
}

/* ============ 控制按钮 ============ */
.controls-wrapper {
    position: absolute;
    top: 30px;
    right: 30px;
    pointer-events: auto;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 10px;
    z-index: 20;
    transition: opacity 0.5s ease;
}

.btn-group {
    display: flex;
    gap: 10px;
}

.upload-btn {
    background: rgba(20, 20, 20, 0.6);
    border: 1px solid rgba(212, 175, 55, 0.4);
    color: #d4af37;
    padding: 10px 20px;
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 2px;
    font-size: 10px;
    transition: all 0.4s;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(5px);
    min-width: 120px;
}

.upload-btn:hover,
.upload-btn:focus-within {
    background: #d4af37;
    color: #000;
    box-shadow: 0 0 20px rgba(212, 175, 55, 0.5);
}

/* .upload-btn 自己声明了 display:flex，会盖掉 [hidden] 的 UA 样式，必须显式关掉 */
.upload-btn[hidden] {
    display: none;
}

.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); /* 镜像，符合照镜子的直觉 */
}

/* 状态条：默认只在摄像头预览下方显示一行简短提示 */
#status-info {
    position: absolute;
    bottom: 5px;
    left: 5px;
    right: 5px;
    color: rgba(212, 175, 55, 0.8);
    font-size: 10px;
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    background: rgba(0, 0, 0, 0.5);
    padding: 2px 5px;
    pointer-events: none;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ============ 快捷键状态 ============ */
/* 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 {
    opacity: 0;
    pointer-events: none;
    visibility: hidden;
    transition: opacity 0.5s ease, visibility 0s linear 0.5s;
}

/*
 * 摄像头拿不到（没设备 / 拒绝授权 / 模型加载失败）时，预览区不是直接消失——
 * 那样用户只会觉得「手势怎么没反应」。缩成一条提示说明原因，4 秒后自己淡出。
 */
#webcam-wrapper.webcam-failed {
    width: auto;
    height: auto;
    max-width: 300px;
    padding: 9px 13px;
    border-color: rgba(212, 175, 55, 0.3);
    background: rgba(6, 14, 24, 0.75);
    animation: webcam-note-out 0.6s ease 4s forwards;
}
#webcam-wrapper.webcam-failed #webcam,
#webcam-wrapper.webcam-failed #debug-info {
    display: none;
}
#webcam-wrapper.webcam-failed #status-info {
    position: static;
    background: none;
    padding: 0;
    font-size: 11px;
    line-height: 1.5;
    letter-spacing: 0.5px;
    white-space: normal;
}

@keyframes webcam-note-out {
    to { opacity: 0; visibility: hidden; }
}

/*
 * 这条必须单独写，且不能依赖上面那个带延迟的 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: 1100px) {
    /* 720–1100 之间，标题宽度已经顶到右上角的按钮了（原来只有 720 以下才挪工具栏）。
       挪到底部之后标题就不用让位，也顺带解决了手机上「MERRY CHRIST…」被裁掉的问题。 */
    .controls-wrapper {
        top: auto;
        bottom: 60px;
        right: 20px;
        left: 20px;
        align-items: center;
    }

    .btn-group { justify-content: center; }
    .hint-text { text-align: center; }
}

@media (max-width: 720px) {
    /* 1100 那条把工具栏设成了 align-items:center，子元素会缩到内容宽度，
       于是 flex:1 只能在那一小段里分配，「SAVE IMAGE」被挤成两行。这里改回拉伸。 */
    .controls-wrapper { align-items: stretch; }

    .btn-group { justify-content: stretch; }
    .upload-btn {
        flex: 1;
        white-space: nowrap;
    }

    /* 手机上 9px 的字根本看不清，手势说明交给首屏引导卡就够了 */
    .hint-text { display: none; }

    #webcam-wrapper {
        width: 140px;
        height: 105px;
        bottom: auto;
        top: 20px;
        left: 20px;
    }
}

@media (max-width: 480px) {
    /* 15 个字母 × 6px 字距在 375px 屏上会顶满整行，压一压才留得住呼吸感 */
    h1 {
        font-size: 26px;
        letter-spacing: 2px;
    }
}

/* ============ 首次引导 ============ */
#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; }
}
