* {
    box-sizing: border-box;
}

:root {
    --primary: #8B4513; /* SaddleBrown - 更具书卷气 */
    --primary-hover: #5D2E0C;
    --primary-light: #FDF5E6; /* OldLace */
    --bg-main: #F4E4BC; /* Parchment - 羊皮纸色 */
    --card-bg: rgba(255, 255, 255, 0.6);
    --text-main: #2D1E12; /* 深木色文本 */
    --text-muted: #705C4E;
    --border-color: #D2B48C; /* Tan */
    --shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    
    --font-title: 'Cinzel', serif;
    --font-body: 'Cormorant Garamond', serif;
}

body {
    background-color: var(--bg-main);
    color: var(--text-main);
    font-family: var(--font-body);
    font-size: 1.1rem;
    margin: 0;
    min-height: 100vh;
    display: block;
    /* 移除 overflow-x: hidden，防止干扰 sticky 定位 */
}

/* Header & Filters */
.sticky-header {
    position: fixed; /* 改为 fixed 确保绝对跟随 */
    top: 0;
    left: 0;
    right: 0;
    width: 100%;
    z-index: 1000;
    background: #FDF5E6;
    box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1);
    padding: 0.75rem 0;
    border-bottom: 2px solid var(--border-color);
}

.header-content {
    max-width: none; /* 完全取消限制 */
    width: 100%;
    margin: 0;
    padding: 0 2rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.top-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.app-title {
    font-family: var(--font-title);
    font-size: 1.8rem;
    font-weight: 700;
    color: var(--primary);
    margin: 0;
    letter-spacing: 0.05em;
    text-shadow: 1px 1px 0px rgba(255,255,255,0.5);
}

.filter-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    background: rgba(210, 180, 140, 0.1); /* Tan with opacity */
    padding: 0.5rem;
    border-radius: 0.25rem;
    border: 1px solid var(--border-color);
}

.search-box {
    flex: 2 1 200px;
}

select {
    flex: 1 1 120px;
}

.checkbox-group {
    flex: 0 1 auto;
    background: white;
    padding: 0 1rem;
    border-radius: 0.5rem;
    border: 1px solid #e2e8f0;
    height: 34px;
    display: flex;
    align-items: center;
    gap: 1.25rem;
}

.checkbox-group label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    cursor: pointer;
    color: var(--text-main);
    white-space: nowrap;
}

.checkbox-group input[type="checkbox"] {
    width: 16px; /* 强制固定宽度，解决“很大”的问题 */
    height: 16px;
    margin: 0;
    cursor: pointer;
    accent-color: var(--primary); /* 使用主题色 */
}

input[type="text"], select {
    width: 100%;
    padding: 0.4rem 0.6rem;
    border: 1px solid #e2e8f0;
    border-radius: 0.5rem;
    background: white;
    color: var(--text-main);
    font-size: 0.85rem;
    transition: var(--transition);
    height: 34px;
}

input:focus, select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.btn-primary {
    background: var(--primary);
    font-family: var(--font-title);
    letter-spacing: 0.05em;
    color: white;
    border: none;
    padding: 0.5rem 1.25rem;
    border-radius: 0.25rem;
    font-weight: 600;
    font-size: 0.8rem;
    cursor: pointer;
    transition: var(--transition);
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

.btn-primary:hover {
    background: var(--primary-hover);
    transform: translateY(-1px);
}

/* Grid & Cards */
.main-container {
    max-width: none;
    width: 100%;
    margin: 0;
    padding: 140px 2rem 0 2rem; /* 增加顶部边距（约140px），防止内容被固定的标题栏遮挡 */
}

.spell-grid {
    display: grid;
    grid-template-columns: repeat(8, 1fr); /* 修改为一行 8 个，等分列宽 */
    gap: 1.5rem;
    padding: 2rem 1rem;
    justify-content: center;
    margin: 0 auto;
    max-width: 1800px; /* 限制最大宽度，确保卡片宽度适中且居中 */
}

.spell-card {
    background: #fff;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    padding: 1rem; /* 减小内边距以缩短高度 */
    cursor: pointer;
    transition: var(--transition);
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    height: auto; /* 高度随内容变化 */
    min-height: 140px; /* 显著缩短最小高度 */
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
    overflow: hidden;
}

/* 移除不再需要的装饰样式 */
.card-top-icon, .card-detail-eye {
    display: none;
}

.spell-card::after {
    content: "";
    position: absolute;
    bottom: 0;
    right: 0;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 0 0 15px 15px;
    border-color: transparent transparent var(--bg-main) transparent;
    filter: drop-shadow(-1px -1px 1px rgba(0,0,0,0.1));
}

.spell-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 20px rgba(139, 69, 19, 0.15);
}

.card-top-icon {
    width: 12px;
    height: 12px;
    border: 1px solid var(--border-color);
    border-radius: 50%;
    margin-bottom: 0.75rem;
}

.card-detail-eye {
    position: absolute;
    top: 0.5rem;
    right: 0.75rem;
    font-size: 0.9rem;
    color: var(--text-muted);
    opacity: 0.6;
    cursor: help;
    transition: opacity 0.2s;
}

.card-detail-eye:hover {
    opacity: 1;
    color: var(--primary);
}

.spell-card.selected {
    background: #fffcf5; /* 选中的卡片带有淡淡的金黄色背景 */
    border: 2px solid var(--primary); /* 增强边框 */
    box-shadow: 0 0 15px var(--primary-light), 0 4px 12px rgba(139, 69, 19, 0.2);
    transform: scale(1.02); /* 选中时稍微放大 */
    z-index: 10;
}

/* 在选中时给名称加粗，并显示一个指示标记 */
.spell-card.selected .card-name {
    color: var(--primary);
}

.spell-card.selected::before {
    content: "✓";
    position: absolute;
    top: 0.5rem;
    left: 0.5rem;
    background: var(--primary);
    color: white;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    font-size: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 0.5rem;
}

.card-name {
    font-family: var(--font-title);
    font-size: 1.2rem;
    font-weight: 700;
    margin: 0;
    line-height: 1.2;
    color: #1a1a1a;
    width: 100%;
    padding: 0 0.5rem;
}

.card-divider {
    width: 80%;
    height: 1px;
    background: linear-gradient(to right, transparent, var(--border-color), transparent);
    margin: 0.75rem 0;
}

.card-school {
    font-size: 0.95rem;
    color: var(--text-muted);
    font-style: italic;
    margin-bottom: 0.25rem;
}

.card-meta {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.4rem;
    margin-top: 0.25rem;
    font-size: 0.85rem;
    color: var(--text-main);
}

.meta-tag {
    font-size: 0.65rem;
    padding: 0.05rem 0.3rem;
    border-radius: 0.25rem;
    border: 1px solid #e2e8f0;
    background: white;
    color: var(--text-muted);
}

.tag-c { border-color: #feb2b2; color: #c53030; background: #fff5f5; }
.tag-r { border-color: #90cdf4; color: #2b6cb0; background: #ebf8ff; }

.card-footer {
    margin-top: auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-top: 0.75rem;
    border-top: 1px solid rgba(0, 0, 0, 0.05);
}

.card-source {
    margin-top: auto;
    padding-top: 0.5rem;
    font-size: 0.65rem;
    color: var(--text-muted);
    font-style: italic;
    opacity: 0.7;
    width: 100%;
    text-align: right; /* 强制右对齐，与左侧的环位标签呼应 */
}

.card-level-tag {
    position: absolute;
    bottom: 0.5rem;
    left: 0.75rem;
    background: transparent; /* 去掉背景 */
    color: var(--text-muted); /* 使用柔和的文字颜色 */
    font-size: 0.8rem;
    font-weight: 600;
    padding: 0;
    border: none; /* 去掉边框 */
    z-index: 5;
    font-style: italic;
}

.btn-detail {
    font-size: 0.8rem;
    color: var(--primary);
    font-weight: 700;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    margin-top: 0.5rem;
    padding: 0.2rem 0.5rem;
    border: 1px solid var(--primary);
    border-radius: 0.25rem;
    transition: var(--transition);
    background: white;
}

.btn-detail:hover {
    background: var(--primary);
    color: white;
}

/* Modal */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(8px);
    z-index: 1000;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 1.5rem;
}

.modal-content {
    background: white;
    width: 100%;
    max-width: 650px;
    max-height: 85vh;
    border-radius: 1.5rem;
    overflow-y: auto;
    position: relative;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
}

.modal-close {
    position: absolute;
    top: 1.25rem;
    right: 1.25rem;
    width: 2rem;
    height: 2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: #f1f5f9;
    color: #64748b;
    cursor: pointer;
    transition: var(--transition);
    z-index: 10;
}

.modal-close:hover {
    background: #e2e8f0;
    color: #1e293b;
}

.modal-body {
    padding: 2.5rem;
}

.modal-title {
    font-family: var(--font-title);
    font-size: 2rem;
    color: var(--primary);
    margin-bottom: 0.5rem;
}

.modal-subtitle {
    font-family: var(--font-title);
    font-size: 1rem;
    color: var(--text-muted);
    margin-bottom: 1.5rem;
    border-bottom: 2px solid var(--border-color);
    padding-bottom: 0.5rem;
}

.stat-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 1.25rem;
    margin-bottom: 2rem;
    background: #f8fafc;
    padding: 1.5rem;
    border-radius: 1rem;
    border: 1px solid #f1f5f9;
}

.stat-item strong {
    display: block;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #94a3b8;
    margin-bottom: 0.375rem;
}

.stat-item span {
    font-size: 1rem;
    font-weight: 600;
    color: #334155;
}

.desc-text {
    line-height: 1.7;
    color: #475569;
    font-size: 1rem;
}

.desc-text p { margin-bottom: 1.25rem; }

/* Responsive */
@media (max-width: 1024px) {
    .main-container { padding-top: 220px; } /* iPad 端标题栏较宽，增加顶部边距 */
    .spell-grid { grid-template-columns: repeat(4, 1fr); } /* iPad 端一行 4 个 */
    .filter-controls { display: grid; grid-template-columns: 1fr 1fr 1fr; }
    .search-box { grid-column: span 3; }
    .checkbox-group { grid-column: span 3; justify-content: center; padding-top: 0.25rem; }
}

@media (max-width: 640px) {
    .sticky-header { padding: 0.5rem 0; }
    .header-content { padding: 0 1rem; gap: 0.5rem; }
    .main-container { padding-top: 280px; padding-left: 1rem; padding-right: 1rem; } /* 减小一点边距 */
    .spell-grid { grid-template-columns: repeat(2, 1fr); }
    .filter-controls { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; }
    .search-box { grid-column: span 2; }
    .top-row { flex-direction: column; gap: 0.75rem; align-items: flex-start; }
    .checkbox-group { grid-column: span 2; justify-content: center; padding-top: 0.25rem; }
    .logo h1 { font-size: 1.5rem; }
}

/* Print styles */
@media print {
    @page {
        size: A4;
        margin: 5mm; /* 恢复统一 5mm 边距，确保 3 列显示正常 */
    }
    
    html, body {
        height: auto !important;
        overflow: visible !important;
    }

    .sticky-header, .modal-overlay, .btn-detail, .main-container { display: none !important; }
    body { background: white; margin: 0; padding: 0; }
    
    #print-area {
        display: block !important;
        width: 100% !important;
        height: auto !important;
        min-height: 0 !important;
        margin: 0 !important;
        padding: 0 !important;
        overflow: visible !important;
    }
    
    .print-grid {
        display: block !important; /* 改为 block 布局，内部使用 inline-block 元素 */
        width: 100% !important;
        text-align: center; /* 居中卡片 */
        padding-top: 5mm;
        padding-bottom: 0;
    }

    .spell-card-print {
        display: inline-flex !important;
        vertical-align: top;
        width: 60mm; /* 稍微减小宽度 (从 62mm 到 60mm) 以确保在 Mac Safari 上不会挤出页面 */
        min-height: 88mm;
        height: auto;
        margin: 2.5mm 1mm; /* 稍微减小水平间距 */
        border: 1.2mm solid #7d6752;
        border-radius: 3mm;
        box-sizing: border-box;
        padding: 0;
        flex-direction: column;
        background: #fff;
        position: relative;
        
        /* 极致的分页控制 */
        break-inside: avoid !important;
        -webkit-column-break-inside: avoid !important;
        page-break-inside: avoid !important;
        page-break-after: auto !important;
        page-break-before: auto !important;
        
        font-family: "Cormorant Garamond", serif;
        overflow: visible !important;
        
        /* 解决 Mac Safari 可能的渲染问题 */
        -webkit-transform: translateZ(0); 
    }

    /* 移除固定高度倍数定义 */
    .spell-card-print.double-height,
    .spell-card-print.triple-height {
        height: auto;
    }

    /* 超长法术的紧凑排版 */
    .spell-card-print.double-height .card-desc-print,
    .spell-card-print.triple-height .card-desc-print {
        line-height: 1.15;
        padding: 1mm 2mm;
    }

    .spell-card-print.double-height .card-desc-content p,
    .spell-card-print.triple-height .card-desc-content p,
    .spell-card-print.double-height .card-desc-content li,
    .spell-card-print.triple-height .card-desc-content li {
        margin-bottom: 0.4mm !important;
    }

    .card-level-badge {
        position: absolute;
        top: 2mm;
        right: 2mm;
        width: 6mm;
        height: 6mm;
        background: #7d6752;
        color: white;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 8pt;
        font-weight: bold;
        z-index: 10;
    }

    .card-header-print {
        border-bottom: 1px solid #7d6752;
        padding: 2.5mm 8mm 1.5mm 2.5mm; /* 为右侧徽章留出空间 */
        margin: 0;
        text-align: center; /* 修改为居中对齐 */
        background: #fdfaf7;
        border-radius: 2mm 2mm 0 0; /* 添加上方内圆角 */
    }

    .card-title-print {
        font-size: 10.5pt;
        font-weight: bold;
        text-transform: uppercase;
        margin: 0;
        line-height: 1.1;
    }

    .card-subtitle-print {
        font-size: 7pt;
        font-style: italic;
        color: #555;
    }

    .card-stats-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        border-bottom: 1px solid #7d6752;
    }

    .stat-box {
        padding: 0.8mm 0.5mm; /* 压缩格子高度 */
        border-right: 1px solid #7d6752;
        border-bottom: 1px solid #7d6752;
        text-align: center;
        display: flex;
        flex-direction: column;
        justify-content: center;
        min-height: 8mm;
    }
    .stat-box:nth-child(2n) { border-right: none; }
    .stat-box:nth-child(3), .stat-box:nth-child(4) { border-bottom: none; }

    .stat-label {
        font-size: 6.2pt;
        font-weight: bold;
        text-transform: uppercase;
        color: #7d6752;
        margin-bottom: 0.2mm;
    }

    .stat-value {
        font-size: 7.5pt;
        color: #000;
        line-height: 1.1;
    }

    .card-desc-print {
        flex-grow: 1;
        font-size: 8.5pt; /* 稍微恢复一点字号 */
        line-height: 1.3; /* 增加行高提高可读性 */
        text-align: justify;
        display: block; /* 改为 block 避免 flex 在打印时的分页问题 */
        padding: 3mm 4mm; /* 增加内边距 */
        overflow: visible !important;
    }

    .card-desc-content {
        margin: 0;
    }
    
    .card-desc-content p {
        margin: 0 0 1.5mm 0; /* 恢复段落间距 */
    }

    .card-footer-print {
        margin-top: auto; /* 关键：确保即使内容少，页脚也在底部 */
        padding: 1.2mm 3mm;
        border-top: 1px solid #7d6752;
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 6.5pt;
        font-weight: bold;
        text-transform: uppercase;
        color: #7d6752;
        background: #fdfaf7;
        border-radius: 0 0 2mm 2mm; /* 添加下方内圆角 */
    }

    .global-print-footer {
        position: fixed; /* 固定定位 */
        bottom: 0; /* 贴紧 @page 设定的 5mm 边距边缘 */
        left: 0;
        right: 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-top: 2mm;
        border-top: 0.5px solid var(--border-color);
        font-size: 7pt;
        color: var(--text-muted);
        font-family: var(--font-body);
        font-style: italic;
        background: white;
        z-index: 9999;
    }

    .print-watermark {
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%) rotate(-45deg);
        font-size: 80pt;
        font-weight: bold;
        color: rgba(125, 103, 82, 0.1); /* 使用棕色调，透明度设为 0.1 */
        white-space: nowrap;
        z-index: -1;
        pointer-events: none;
        user-select: none;
        text-transform: uppercase;
        font-family: var(--font-title);
    }

    .print-footer { display: none; }
}
