@import url('https://fonts.googleapis.cn/css2?family=Ma+Shan+Zheng&display=swap');

/* ===== 诗词消消乐 - 鼠尾草绿国风样式 ===== */

:root {
    /* 鼠尾草绿主色调 */
    --sage: #7D8C6E;
    --sage-light: #A3B18A;
    --sage-dark: #5A6B4E;
    --sage-lighter: #C5D1B8;
    --sage-bg: #E8EDDF;

    /* 背景色 */
    --bg-cool: #F5F3EF;
    --bg-warm: #F9F6F0;

    /* 文字色 */
    --text-ink: #2D2A24;
    --text-ink-light: #5A5548;
    --text-ink-muted: #8B8578;

    /* 赭红色 - 标题/印章 */
    --red-ochre: #8B4A4A;
    --red-light: #B85454;

    /* 金色 */
    --gold-medium: #C4A35A;
    --gold-light: #E8D5A3;
    --gold-dim: rgba(196, 163, 90, 0.3);

    /* 功能色 */
    --success-color: #C4A35A;
    --error-color: #B85454;

    /* 网格色 */
    --grid-bg: #EDEBE5;
    --cell-white: #FFFFFF;
    --cell-solid: #D8D3C8;
    --cell-empty-bg: #F0EDE6;
    --cell-empty-border: #B8B2A6;

    /* 阴影 */
    --shadow-soft: rgba(93, 107, 78, 0.1);
    --shadow-medium: rgba(93, 107, 78, 0.18);
    --shadow-strong: rgba(93, 107, 78, 0.28);

    /* 基础色 */
    --white: #FFFFFF;
    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-xl: 16px;
    --radius-xxl: 20px;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: 'Ma Shan Zheng', 'KaiTi', 'STKaiti', 'SimSun', '宋体', serif;
    background: var(--bg-cool);
    color: var(--text-ink);
    position: relative;
    touch-action: manipulation;
}

/* ===== 背景纹理 ===== */
body::before {
    content: '';
    position: fixed;
    inset: 0;
    background-image:
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='paper'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.03' numOctaves='4' stitchTiles='stitch'/%3E%3CfeColorMatrix type='matrix' values='1 0 0 0 0.01 0 1 0 0 0.008 0 0 1 0 0.005 0 0 0 0.04 0'/%3E%3C/filter%3E%3Crect width='200' height='200' filter='url(%23paper)'/%3E%3C/svg%3E");
    background-size: 200px 200px;
    pointer-events: none;
    z-index: 0;
}

/* ===== 页面切换 ===== */
.page {
    display: none;
    position: relative;
    z-index: 1;
    height: 100vh;
    height: 100dvh;
}

.page.active {
    display: flex;
}

/* ===== 首页 ===== */
#home-page {
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: var(--bg-cool);
    overflow: hidden;
    position: relative;
}

.home-bg-art {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 0;
}

.home-mountain-decor {
    position: absolute;
    right: -20px;
    bottom: 0;
    width: 70%;
    max-width: 320px;
    opacity: 0.9;
}

.home-content {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
}

.home-seal {
    width: 52px;
    height: 52px;
    border: 2.5px solid var(--red-ochre);
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--red-ochre);
    font-size: 24px;
    transform: rotate(-5deg);
    margin-bottom: 8px;
    position: relative;
}

.home-seal::after {
    content: '诗';
    font-family: 'Ma Shan Zheng', serif;
}

.home-title {
    font-size: 42px;
    color: var(--red-ochre);
    letter-spacing: 8px;
    font-weight: normal;
    text-shadow: 0 1px 3px var(--shadow-soft);
}

.home-divider {
    width: 48px;
    height: 2px;
    background: var(--sage-light);
    border-radius: 1px;
    margin: 4px 0;
}

.home-subtitle {
    font-size: 18px;
    color: var(--text-ink-muted);
    letter-spacing: 4px;
    margin-bottom: 20px;
}

.home-buttons {
    display: flex;
    flex-direction: column;
    gap: 14px;
    width: 220px;
}

.home-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 14px 24px;
    border: 1.5px solid var(--sage);
    border-radius: var(--radius-xl);
    background: rgba(255,255,255,0.6);
    color: var(--sage-dark);
    font-family: 'Ma Shan Zheng', serif;
    font-size: 20px;
    cursor: pointer;
    transition: all 0.2s ease;
    backdrop-filter: blur(4px);
}

.home-btn:active {
    transform: scale(0.96);
}

.home-btn-primary {
    background: linear-gradient(135deg, var(--sage), var(--sage-dark));
    color: #FFFFFF;
    border-color: var(--sage-dark);
    box-shadow: 0 4px 12px var(--shadow-medium);
}

.home-btn-primary:active {
    background: linear-gradient(135deg, var(--sage-dark), var(--sage));
}

.btn-icon {
    width: 20px;
    height: 20px;
}

/* ===== 游戏页 ===== */
#game-page {
    flex-direction: column;
    background: var(--bg-cool);
    overflow: hidden;
}

.game-bg-art {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 0;
}

.game-mountain-decor {
    position: absolute;
    right: -10px;
    bottom: 0;
    width: 60%;
    max-width: 200px;
}

.game-top-bar {
    position: relative;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 12px;
    padding-top: max(8px, env(safe-area-inset-top));
    background: rgba(245, 243, 239, 0.9);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--sage-lighter);
}

.back-btn-text {
    font-family: 'Ma Shan Zheng', serif;
    font-size: 16px;
    color: var(--sage);
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px 8px;
}

.level-badge {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 16px;
    color: var(--text-ink-light);
}

.level-sep {
    color: var(--text-ink-muted);
}

.top-bar-tools {
    display: flex;
    gap: 6px;
}

.tool-btn {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: 1px solid var(--sage-lighter);
    background: rgba(255,255,255,0.7);
    color: var(--sage);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.15s;
}

.tool-btn:active {
    transform: scale(0.9);
    background: var(--sage-bg);
}

.tool-btn svg {
    width: 16px;
    height: 16px;
}

.debug-only {
    display: none;
}

body.debug-mode .debug-only {
    display: flex;
}

/* ===== 游戏信息栏 ===== */
.game-info-bar {
    position: relative;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    background: rgba(245, 243, 239, 0.6);
}

.timer-section {
    flex: 1;
}

.timer-display {
    font-family: system-ui, -apple-system, sans-serif;
    font-size: 22px;
    font-weight: 700;
    color: var(--text-ink);
    letter-spacing: 1px;
}

.timer-display.warning {
    color: var(--gold-medium);
}

.timer-display.danger {
    color: var(--error-color);
    animation: timerBlink 0.5s ease-in-out infinite;
}

@keyframes timerBlink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

.timer-bar {
    width: 100%;
    height: 4px;
    background: var(--sage-lighter);
    border-radius: 2px;
    margin-top: 4px;
    overflow: hidden;
}

.timer-bar-fill {
    height: 100%;
    background: var(--sage);
    border-radius: 2px;
    transition: width 1s linear, background-color 0.5s;
    width: 100%;
}

.timer-bar-fill.warning {
    background: var(--gold-medium);
}

.timer-bar-fill.danger {
    background: var(--error-color);
}

.progress-section, .error-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    min-width: 50px;
}

.progress-text {
    font-family: system-ui, -apple-system, sans-serif;
    font-size: 20px;
    font-weight: 700;
    color: var(--sage-dark);
}

.progress-label, .error-label {
    font-size: 12px;
    color: var(--text-ink-muted);
}

.error-count {
    font-family: system-ui, -apple-system, sans-serif;
    font-size: 20px;
    font-weight: 700;
    color: var(--error-color);
}

/* ===== 9×9 网格 ===== */
.grid-area {
    position: relative;
    z-index: 2;
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 8px 12px;
    min-height: 0;
}

.game-grid {
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    grid-template-rows: repeat(9, 1fr);
    gap: 2px;
    padding: 6px;
    background: var(--grid-bg);
    border-radius: var(--radius-lg);
    border: 1.5px solid var(--sage-lighter);
    box-shadow: 0 2px 12px var(--shadow-soft);
    width: min(90vw, 400px);
    height: min(90vw, 400px);
    max-width: 400px;
    max-height: 400px;
    aspect-ratio: 1;
}

.grid-cell {
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    font-size: clamp(16px, 6.2vw, 24px);
    transition: all 0.15s ease;
    position: relative;
    user-select: none;
}

/* 诗句字格 */
.grid-cell.char-cell {
    background: var(--cell-white);
    border: 1px solid rgba(197, 209, 184, 0.5);
    color: var(--text-ink);
}

/* 实心方块（诗句行内占位） */
.grid-cell.solid-cell {
    background: var(--cell-solid);
    border: none;
    color: transparent;
    pointer-events: none;
    opacity: 0.6;
}

/* 空行中的实心方块（更淡化） */
.grid-cell.solid-cell.blank-row {
    opacity: 0.25;
}

/* 空白格（缺字） */
.grid-cell.empty-cell {
    background: var(--cell-empty-bg);
    border: 1.5px dashed var(--sage-light);
    cursor: pointer;
    position: relative;
}

.grid-cell.empty-cell::after {
    content: '';
    position: absolute;
    inset: 30%;
    border-bottom: 1.5px solid var(--sage-light);
    pointer-events: none;
}

.grid-cell.empty-cell:active {
    background: var(--sage-bg);
}

/* 拖拽目标高亮 */
.grid-cell.empty-cell.drop-target {
    border-color: var(--sage);
    background: var(--sage-bg);
    box-shadow: 0 0 0 2px var(--sage-light);
    animation: targetPulse 1s ease-in-out infinite;
}

@keyframes targetPulse {
    0%, 100% { box-shadow: 0 0 0 2px var(--sage-light); }
    50% { box-shadow: 0 0 0 4px var(--sage-light); }
}

/* 拖拽悬浮高亮 */
.grid-cell.empty-cell.drop-hover {
    border-color: var(--sage-dark);
    background: rgba(125, 140, 110, 0.2);
    box-shadow: 0 0 0 3px var(--sage);
    transform: scale(1.05);
}

/* 已填入的字 */
.grid-cell.filled-cell {
    background: var(--cell-white);
    border: 1.5px solid var(--sage-light);
    color: var(--sage-dark);
}

/* 已消除行 */
.grid-cell.eliminating {
    animation: eliminateCell 0.6s ease forwards;
}

@keyframes eliminateCell {
    0% { transform: scale(1); opacity: 1; }
    30% { transform: scale(1.08); background: rgba(196, 163, 90, 0.2); box-shadow: 0 0 8px var(--gold-dim); }
    100% { transform: scale(0.8) translateY(-20px); opacity: 0; }
}

.grid-cell.eliminated {
    visibility: hidden;
    pointer-events: none;
}

/* 错误抖动 */
.grid-cell.error-shake {
    animation: shakeError 0.4s ease;
    border-color: var(--error-color) !important;
    background: rgba(184, 84, 84, 0.1) !important;
}

@keyframes shakeError {
    0%, 100% { transform: translateX(0); }
    20% { transform: translateX(-4px); }
    40% { transform: translateX(4px); }
    60% { transform: translateX(-3px); }
    80% { transform: translateX(3px); }
}

/* 飞入动画 */
.grid-cell.fly-in {
    animation: flyInCell 0.3s ease;
}

@keyframes flyInCell {
    0% { transform: scale(0.3) translateY(30px); opacity: 0; }
    60% { transform: scale(1.1); opacity: 1; }
    100% { transform: scale(1); opacity: 1; }
}

/* ===== 候选字区域 ===== */
.candidate-area {
    position: relative;
    z-index: 2;
    padding: 8px 16px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    margin-bottom: -5px;
    background: rgba(245, 243, 239, 0.9);
    backdrop-filter: blur(8px);
    border-top: 1px solid var(--sage-lighter);
}

.candidate-container {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
    min-height: 48px;
}

.candidate-btn {
    width: 48px;
    height: 48px;
    min-width: 44px;
    min-height: 44px;
    border-radius: var(--radius-md);
    border: none;
    background: linear-gradient(145deg, var(--sage), var(--sage-dark));
    color: #FFFFFF;
    font-family: 'Ma Shan Zheng', serif;
    font-size: 22px;
    cursor: grab;
    transition: all 0.15s ease;
    box-shadow: 0 2px 6px var(--shadow-medium);
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
}

.candidate-btn:active {
    transform: scale(0.92);
}

.candidate-btn.dragging {
    opacity: 0.4;
    transform: scale(0.85);
}

/* ===== 拖拽浮层 ===== */
.drag-ghost {
    display: none;
    position: fixed;
    z-index: 200;
    width: 52px;
    height: 52px;
    border-radius: var(--radius-md);
    background: linear-gradient(145deg, var(--sage), var(--sage-dark));
    color: #FFFFFF;
    font-family: 'Ma Shan Zheng', serif;
    font-size: 24px;
    align-items: center;
    justify-content: center;
    box-shadow: 0 6px 20px var(--shadow-strong), 0 0 0 3px rgba(255,255,255,0.5);
    pointer-events: none;
    transform: scale(1.15);
    opacity: 0.95;
}

/* ===== 弹窗通用 ===== */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 100;
    background: rgba(45, 42, 36, 0.5);
    backdrop-filter: blur(4px);
    align-items: center;
    justify-content: center;
}

.modal-overlay.active {
    display: flex;
}

.modal-content {
    position: relative;
    background: var(--bg-warm);
    border-radius: var(--radius-xxl);
    padding: 28px 24px;
    width: min(88vw, 340px);
    box-shadow: 0 8px 32px var(--shadow-strong);
    text-align: center;
}

.modal-seal {
    width: 48px;
    height: 48px;
    border: 2.5px solid var(--red-ochre);
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--red-ochre);
    font-size: 22px;
    margin: 0 auto 12px;
    transform: rotate(-5deg);
}

.seal-fail {
    border-color: var(--error-color);
    color: var(--error-color);
}

.modal-title {
    font-size: 26px;
    color: var(--text-ink);
    font-weight: normal;
    margin-bottom: 16px;
}

.modal-body {
    margin-bottom: 20px;
}

.modal-actions {
    display: flex;
    gap: 12px;
    justify-content: center;
}

.modal-btn {
    padding: 10px 24px;
    border-radius: var(--radius-xl);
    border: 1.5px solid var(--sage-lighter);
    background: rgba(255,255,255,0.6);
    color: var(--sage-dark);
    font-family: 'Ma Shan Zheng', serif;
    font-size: 18px;
    cursor: pointer;
    transition: all 0.15s;
}

.modal-btn:active {
    transform: scale(0.96);
}

.modal-btn-primary {
    background: linear-gradient(135deg, var(--sage), var(--sage-dark));
    color: #FFFFFF;
    border-color: var(--sage-dark);
    box-shadow: 0 2px 8px var(--shadow-medium);
}

/* 星级 */
.stars-container {
    display: flex;
    justify-content: center;
    gap: 8px;
    margin-bottom: 16px;
}

.star {
    font-size: 32px;
    color: var(--cell-solid);
    transition: all 0.3s ease;
}

.star.active {
    color: var(--gold-medium);
    animation: starPop 0.4s ease;
}

@keyframes starPop {
    0% { transform: scale(0); }
    60% { transform: scale(1.3); }
    100% { transform: scale(1); }
}

/* 结果统计 */
.result-stats {
    display: flex;
    justify-content: center;
    gap: 20px;
}

.stat-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
}

.stat-label {
    font-size: 13px;
    color: var(--text-ink-muted);
}

.stat-value {
    font-family: system-ui, -apple-system, sans-serif;
    font-size: 20px;
    font-weight: 700;
    color: var(--text-ink);
}

/* 失败弹窗 */
.fail-text {
    font-size: 18px;
    color: var(--text-ink);
    margin-bottom: 8px;
}

.fail-sub {
    font-size: 14px;
    color: var(--text-ink-muted);
}

/* 说明弹窗 */
.instructions-section-title {
    font-size: 16px;
    color: var(--sage-dark);
    margin: 12px 0 6px;
    text-align: left;
    padding-left: 8px;
    border-left: 3px solid var(--sage);
}

#instructions-modal p {
    font-size: 14px;
    color: var(--text-ink-light);
    text-align: left;
    line-height: 1.7;
    margin-bottom: 4px;
}

/* ===== 设置弹窗 ===== */
.setting-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 8px;
    border-bottom: 1px solid var(--sage-lighter);
}

.setting-item:last-child {
    border-bottom: none;
}

.setting-label {
    font-size: 18px;
    color: var(--text-ink);
}

/* 开关组件 */
.toggle-switch {
    position: relative;
    display: inline-block;
    width: 50px;
    height: 28px;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    cursor: pointer;
    inset: 0;
    background: var(--cell-solid);
    border-radius: 28px;
    transition: all 0.3s ease;
}

.toggle-slider::before {
    content: '';
    position: absolute;
    width: 22px;
    height: 22px;
    left: 3px;
    bottom: 3px;
    background: white;
    border-radius: 50%;
    transition: all 0.3s ease;
    box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}

.toggle-switch input:checked + .toggle-slider {
    background: var(--sage);
}

.toggle-switch input:checked + .toggle-slider::before {
    transform: translateX(22px);
}

/* ===== 粒子画布 ===== */
.particle-canvas {
    position: fixed;
    inset: 0;
    z-index: 99;
    pointer-events: none;
}

/* ===== 响应式 ===== */
@media (min-width: 768px) {
    .game-grid {
        width: min(420px, 55vh);
        height: min(420px, 55vh);
    }
    
    #home-page .home-title {
        font-size: 52px;
    }
}

@media (max-width: 375px) {
    .game-grid {
        gap: 1.5px;
        padding: 4px;
    }
    
    .grid-cell {
        font-size: clamp(14px, 4vw, 20px);
    }
    
    .candidate-btn {
        width: 42px;
        height: 42px;
        font-size: 19px;
    }
    
    .drag-ghost {
        width: 46px;
        height: 46px;
        font-size: 20px;
    }
    
    .home-title {
        font-size: 36px !important;
    }
}

/* 横屏适配 */
@media (max-height: 500px) and (orientation: landscape) {
    .game-info-bar {
        padding: 4px 16px;
    }
    
    .grid-area {
        padding: 4px 12px;
    }
    
    .candidate-area {
        padding: 6px 16px;
        padding-bottom: 6px;
    }
    
    .game-grid {
        width: min(280px, 50vh);
        height: min(280px, 50vh);
    }
}

/* 刘海屏适配 */
@supports (padding: env(safe-area-inset-bottom)) {
    .candidate-area {
        padding-bottom: max(10px, env(safe-area-inset-bottom));
    }
}
