:root {
    --lg-blue: #003399;
    --lg-green: #00C853;
    --lg-bg: #F8FAFC;
    --page-speed: 0.8s;
}

body {
    font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
    -webkit-font-smoothing: antialiased;
}

body.theme-night {
    background-color: #0b1220;
    color: #e2e8f0;
}

/* Apple Mesh Background */
.bg-mesh {
    background-image: 
        radial-gradient(at 0% 0%, hsla(210, 100%, 93%, 0.5) 0, transparent 50%),
        radial-gradient(at 100% 0%, hsla(145, 70%, 90%, 0.5) 0, transparent 50%),
        radial-gradient(at 50% 100%, hsla(220, 80%, 92%, 0.5) 0, transparent 50%);
}

body.theme-night .bg-mesh {
    background-image: 
        radial-gradient(at 0% 0%, rgba(12, 39, 78, 0.6) 0, transparent 50%),
        radial-gradient(at 100% 0%, rgba(25, 80, 66, 0.55) 0, transparent 50%),
        radial-gradient(at 50% 100%, rgba(38, 47, 92, 0.55) 0, transparent 50%);
    opacity: 0.55;
}

/* 3D Core */
.perspective-container {
    perspective: 3000px;
}

.preserve-3d {
    transform-style: preserve-3d;
}

.book-body {
    transform-style: preserve-3d;
}

.page {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    transform-origin: left center;
    transition: transform var(--page-speed) cubic-bezier(0.645, 0.045, 0.355, 1);
    backface-visibility: hidden;
    user-select: none;
    pointer-events: none;
}

/* 激活态允许交互 */
.page.z-active, .page-front:not(.flipped) {
    pointer-events: auto;
}

.page-content {
    width: 100%;
    height: 100%;
    background: white;
    border-radius: 0 1.5rem 1.5rem 0;
    overflow: hidden;
    box-shadow: 20px 0 50px rgba(0,0,0,0.1);
}

/* 滚动条美化 */
.inner-scroll {
    overflow-y: auto;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.inner-scroll::-webkit-scrollbar {
    display: none;
}

/* BROKE 变量高亮 */
.variable-tag {
    display: inline-block;
    background: rgba(0, 200, 83, 0.08);
    color: var(--lg-green);
    border-bottom: 2px solid var(--lg-green);
    padding: 0 6px;
    margin: 0 2px;
    border-radius: 4px;
    cursor: text;
    font-weight: 700;
    min-width: 40px;
    transition: all 0.2s;
    outline: none;
}

.variable-tag:focus {
    background: var(--lg-green);
    color: white;
    box-shadow: 0 4px 12px rgba(0, 200, 83, 0.3);
}

/* 翻页逻辑 */
.flipped {
    transform: rotateY(-180deg);
}

.z-flipped {
    z-index: 0 !important;
}

.z-active {
    z-index: 50 !important;
}

/* 卡片特效 */
.template-card {
    background: white;
    border: 1px solid #f1f5f9;
    transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.template-card:hover {
    transform: translateY(-8px) scale(1.02);
    box-shadow: 0 30px 60px -12px rgba(0, 51, 153, 0.15);
    border-color: var(--lg-blue);
}

/* iOS 适配 */
@supports (-webkit-touch-callout: none) {
    .perspective-container {
        perspective: none; /* 某些低端 iOS 3D 渲染会闪烁 */
    }
}

/* 移动端覆盖 */
@media (max-width: 768px) {
    .page-content { padding: 1.5rem !important; }
    .page-front h1 { font-size: 3rem !important; }
    .stats { display: none; }
}

/* 浮动导航条 */
.floating-nav {
    transform: translate3d(-50%, 0, 0) scale(0.9);
    transform-origin: center;
    will-change: transform;
    isolation: isolate;
    backface-visibility: hidden;
    transition: transform 0.35s ease, box-shadow 0.35s ease;
    -webkit-backdrop-filter: blur(24px);
    backdrop-filter: blur(24px);
}

@media (min-width: 768px) {
    .floating-nav {
        transform: translate3d(-50%, 0, 0) scale(1);
    }
}

/* Prevent flickering on button hover */
#open-book-btn {
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}

/* Optimized Card Interactions */
.part-card,
.chapter-card,
.template-card {
    transition: transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94), box-shadow 0.3s ease, border-color 0.3s ease;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    transform: translateZ(0);
    will-change: transform, box-shadow;
}

/* Override previous specific transition to avoid conflict */
.template-card {
    transition: transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94), box-shadow 0.3s ease, border-color 0.3s ease !important;
}

/* Performance Optimization: Visibility Culling */
.page {
    visibility: hidden; /* Default hidden */
}

.page.z-active,
.page.z-visible {
    visibility: visible;
}

/* Optimize Scrolling Content */
.inner-scroll {
    transform: translateZ(0); /* Hardware accelerate scrolling layer */
    will-change: scroll-position;
    -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
}

/* Reduce Paint Cost */
.page-content {
    /* Slightly reduce shadow spread to lower rendering cost */
    box-shadow: 10px 0 30px rgba(0,0,0,0.08);
    transform: translateZ(0);
}

/* 夜间模式细节 */
body.theme-night .page-content {
    box-shadow: 10px 0 40px rgba(0,0,0,0.35);
}

body.theme-night .floating-nav {
    background: rgba(11, 18, 32, 0.9);
    border-color: rgba(255,255,255,0.06);
}

body.theme-night .floating-nav .nav-btn,
body.theme-night .floating-nav .control-btn {
    color: #e2e8f0;
}

body.theme-night .floating-nav i {
    color: #e2e8f0 !important;
}
