/* ============================================================
   练字网站 - 全局样式表
   包含：基础样式、田字格、响应式布局、打印样式
   ============================================================ */

/* ========== CSS 变量（主题色） ========== */
:root {
    --bg-body: #f0ece4;
    --bg-card: #ffffff;
    --bg-hover: #f5f2ed;
    --bg-input: #faf8f5;
    --text-primary: #2d2518;
    --text-secondary: #8c7b6b;
    --text-muted: #b8a99a;
    --border-color: #e0d5c7;
    --grid-border: #555;
    --grid-cross: #bbb;
    --accent: #c0392b;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
    --radius: 8px;
    --font-mono: 'Courier New', monospace;
}

/* ========== 基础重置 ========== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial,
                 "Noto Sans SC", "PingFang SC", "Microsoft YaHei", sans-serif;
    background: var(--bg-body);
    color: var(--text-primary);
    line-height: 1.6;
    min-height: 100vh;
}

/* ========== 主容器 ========== */
.main-container {
    max-width: 1100px;
    margin: 0 auto;
    padding: 16px;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ========== 头部 ========== */
.site-header {
    text-align: center;
    padding: 20px 0 12px;
    border-bottom: 2px solid var(--grid-border);
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    flex-wrap: wrap;
}

.site-title {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: 0.05em;
}

.page-info {
    font-size: 0.9rem;
    color: var(--text-secondary);
    background: var(--bg-hover);
    padding: 4px 12px;
    border-radius: 20px;
}

/* ========== 田字格主体 ========== */
.grid-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 8px 4px;
}

.grid-row {
    display: grid;
    /* 单元格间距：参考图片中格子之间有清晰间隙 */
    gap: 5px;
}

/* ========== 田字格外层包装 ========== */
.grid-cell-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* ========== 拼音（在田字格外上方）========== */
.cell-pinyin {
    height: 1.8em;
    line-height: 1.8em;
    font-size: clamp(11px, 1.6vw, 13px);
    color: #888;
    text-align: center;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
    font-weight: 500;
    letter-spacing: 0.04em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 100%;
    padding-bottom: 1px;
}

/* ========== 田字格单元格（参考标准田字格图片）========== */
.grid-cell {
    position: relative;
    width: 100%;
    aspect-ratio: 1 / 1;
    /* 外框：实线深色，与参考图一致 */
    border: 1px solid #555;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    overflow: hidden;
}

/* 田字格内部十字虚线 — 参考图中虚线比外框浅且细 */
.grid-cell::before,
.grid-cell::after {
    content: '';
    position: absolute;
    pointer-events: none;
    z-index: 0;
}

/* 竖虚线：从上到下贯穿格子正中 */
.grid-cell::before {
    top: 0;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 0;
    border-left: 0.6px dashed #bbb;
}

/* 横虚线：从左到右贯穿格子正中 */
.grid-cell::after {
    left: 0;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    height: 0;
    border-top: 0.6px dashed #bbb;
}

/* ========== 汉字样式 ========== */
.cell-char {
    position: relative;
    z-index: 1;
    font-size: clamp(26px, 4.5vw, 44px);
    line-height: 1;
    color: #1c1c1c;
    font-weight: 400;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
}

/* ========== 空状态 ========== */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-secondary);
}

.empty-icon {
    font-size: 3rem;
    margin-bottom: 16px;
}

.empty-state p {
    font-size: 1.1rem;
    margin-bottom: 8px;
}

.empty-hint {
    font-size: 0.9rem;
    color: var(--text-muted);
}

/* ========== 底部操作栏 ========== */
.site-footer {
    padding: 20px 0;
    border-top: 2px solid var(--grid-border);
    margin-top: 20px;
}

/* 分页导航 */
.pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 6px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

.btn-page {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 36px;
    height: 36px;
    padding: 0 10px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: var(--bg-card);
    color: var(--text-primary);
    font-size: 0.9rem;
    text-decoration: none;
    cursor: pointer;
    transition: all 0.2s;
    user-select: none;
}

.btn-page:hover {
    background: var(--bg-hover);
    border-color: var(--grid-border);
}

.btn-page.active {
    background: #c0392b;
    color: #fff;
    border-color: #c0392b;
    font-weight: 600;
}

.btn-page.disabled {
    opacity: 0.4;
    pointer-events: none;
}

/* 打印按钮 */
.print-actions {
    display: flex;
    justify-content: center;
    gap: 12px;
    flex-wrap: wrap;
}

.btn-print {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 12px 28px;
    border: 1.5px solid var(--grid-border);
    border-radius: 8px;
    background: var(--bg-card);
    color: var(--text-primary);
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s;
    font-weight: 500;
    user-select: none;
    box-shadow: var(--shadow-sm);
}

.btn-print:hover {
    background: var(--bg-hover);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn-print:active {
    transform: translateY(0);
}

.btn-print-direct {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

.btn-print-direct:hover {
    background: #a93226;
}

/* ========== 打印预览弹窗 ========== */
.print-preview-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    z-index: 1000;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

.print-preview-overlay.active {
    display: flex;
}

.print-preview-modal {
    background: #fff;
    border-radius: 16px;
    width: 100%;
    max-width: 850px;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
    animation: modalIn 0.25s ease-out;
}

@keyframes modalIn {
    from {
        opacity: 0;
        transform: scale(0.95) translateY(10px);
    }
    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

.preview-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid #e5e5e5;
}

.preview-header h3 {
    margin: 0;
    font-size: 1.1rem;
    color: #333;
}

.btn-close {
    width: 32px;
    height: 32px;
    border: none;
    background: #f0f0f0;
    border-radius: 50%;
    font-size: 1.1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #666;
    transition: all 0.2s;
}

.btn-close:hover {
    background: #e0e0e0;
    color: #333;
}

.preview-content {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    background: #fafaf8;
}

.preview-content .grid-main {
    padding: 0;
}

.preview-footer {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    padding: 16px 20px;
    border-top: 1px solid #e5e5e5;
}

.btn-cancel {
    padding: 10px 20px;
    border: 1px solid #d0d0d0;
    border-radius: 8px;
    background: #fff;
    color: #666;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-cancel:hover {
    background: #f0f0f0;
}

/* ========== 手机端响应式适配 ========== */

/* 平板及以下 */
@media (max-width: 768px) {
    .main-container {
        padding: 10px;
    }
    
    .site-header {
        padding: 14px 0 10px;
        margin-bottom: 14px;
    }
    
    .site-title {
        font-size: 1.25rem;
    }
    
    .grid-main {
        gap: 4px;
    }
    
    .grid-row {
        gap: 3px;
    }
    
    .grid-cell {
        border-width: 0.8px;
    }
    
    .grid-cell::before,
    .grid-cell::after {
        border-left-width: 0.5px;
        border-top-width: 0.5px;
    }
    
    .btn-print {
        padding: 10px 20px;
        font-size: 0.9rem;
    }
}

/* 手机端 */
@media (max-width: 480px) {
    .main-container {
        padding: 6px;
    }
    
    .site-header {
        padding: 10px 0 8px;
        margin-bottom: 10px;
    }
    
    .site-title {
        font-size: 1.1rem;
    }
    
    .page-info {
        font-size: 0.8rem;
        padding: 2px 10px;
    }

    .grid-main {
        gap: 2px;
    }
    
    .grid-row {
        gap: 2px;
    }
    
    .grid-cell::before,
    .grid-cell::after {
        border-width: 0.6px;
    }
    
    .btn-print {
        padding: 10px 16px;
        font-size: 0.85rem;
        border-radius: 6px;
    }
    
    .print-actions {
        gap: 8px;
    }
    
    .site-footer {
        padding: 14px 0;
        margin-top: 14px;
    }
    
    .pagination {
        gap: 4px;
    }
    
    .btn-page {
        min-width: 30px;
        height: 30px;
        font-size: 0.8rem;
        padding: 0 8px;
    }
}

/* 小屏手机 */
@media (max-width: 360px) {
    .main-container {
        padding: 4px;
    }
    
    .site-title {
        font-size: 1rem;
    }
    
    .btn-print {
        padding: 8px 12px;
        font-size: 0.8rem;
    }
    
    .print-actions {
        flex-direction: column;
        align-items: stretch;
    }
    
    .btn-print {
        width: 100%;
        justify-content: center;
    }
}

/* ========== 打印样式 ========== */

/* 屏幕隐藏（打印时显示） */
.screen-hidden {
    display: none;
}

@media screen {
    .screen-hidden {
        display: none !important;
    }
}

@media print {
    /* 打印时：隐藏当前页展示区，显示全量页面 */
    .grid-main {
        display: none !important;
    }
    
    .screen-hidden {
        display: block !important;
    }

    /* 打印时自动分页 — 由浏览器根据纸张大小自然换页 */
    #all-pages-print .print-page-section {
        margin-bottom: 12px;
    }
    /* 每行田字格不跨页断裂 */
    #all-pages-print .grid-row {
        break-inside: avoid;
        page-break-inside: avoid;
    }

    /* 隐藏非打印元素 */
    .no-print,
    .site-header,
    .site-footer,
    .print-preview-overlay,
    .btn-print,
    .pagination,
    .print-actions {
        display: none !important;
    }

    /* 重置页面样式 */
    @page {
        size: A4;
        margin: 15mm 12mm;
    }

    html, body {
        background: #fff !important;
        color: #000 !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .main-container {
        max-width: 100%;
        padding: 0;
    }

    #all-pages-print .grid-row {
        gap: 2px;
    }

    #all-pages-print .grid-cell {
        border-color: #555 !important;
        border-width: 0.8px !important;
        background: #fff !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .grid-cell::before {
        border-left-color: #ccc !important;
        border-left-width: 0.4px !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .grid-cell::after {
        border-top-color: #ccc !important;
        border-top-width: 0.4px !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .cell-char {
        color: #000 !important;
        font-weight: 400 !important;
    }

    .cell-pinyin {
        color: #666 !important;
    }

    /* 分页控制 */
    .grid-cell-wrapper {
        break-inside: avoid;
        page-break-inside: avoid;
    }
    
    .grid-row {
        break-inside: avoid;
        page-break-inside: avoid;
    }
    
    .grid-cell {
        break-inside: avoid;
        page-break-inside: avoid;
    }
}

/* ========== 主题切换（可选扩展） ========== */
@media (prefers-color-scheme: dark) {
    /* 默认不做深色适配，田字格在白底上效果更好 */
}
