:root {
    --berry-dark: #8e2157;
    --berry-mid: #c94b7c;
    --paper: #fffdf8;
    --line: #c74f7b;
    --line-soft: rgba(199, 79, 123, 0.35);
    --ink: #4b2a3d;
}

/* 辰宇落雁體 ChenYuluoyan — 開源 OFL，來源：github.com/Chenyu-otf/chenyuluoyan_thin */
@font-face {
    font-family: "ChenYuluoyan";
    src:
        local("辰宇落雁體"),
        local("辰宇落雁體 Thin"),
        url("https://cdn.jsdelivr.net/gh/Chenyu-otf/chenyuluoyan_thin@main/ChenYuluoyan-2.0-Thin.ttf") format("truetype");
    font-weight: normal;
    font-style: normal;
    font-display: swap;
}

/* Y式筆書 TC — 高覆蓋行書／行楷風，OFL，來源：github.com/Steve-Yuu/YshiPen-Shuti */
@font-face {
    font-family: "YShiPenShutiTC";
    src:
        local("YShiPenShutiTC"),
        local("YshiPenShutiTC-Regular"),
        url("fonts/YshiPenShutiTC-Regular.ttf") format("truetype");
    font-weight: normal;
    font-style: normal;
    font-display: swap;
}

/* 阿里媽媽刀隸體 — 隸意風格；阿里字體平台免費可商用授權（非 SIL OFL），字檔隨專案部署以避免外部熱鏈失效 */
@font-face {
    font-family: "AlimamaDaoLiTi";
    src:
        local("阿里妈妈刀隶体"),
        local("AlimamaDaoLiTi"),
        url("fonts/AlimamaDaoLiTi/AlimamaDaoLiTi.woff2") format("woff2"),
        url("fonts/AlimamaDaoLiTi/AlimamaDaoLiTi.woff") format("woff"),
        url("fonts/AlimamaDaoLiTi/AlimamaDaoLiTi.ttf") format("truetype");
    font-weight: normal;
    font-style: normal;
    font-display: swap;
}

/* 全字庫說文解字（ebas927.ttf）— 臺灣全字庫 CNS11643，政府資料開放授權條款第 1 版；字檔隨專案部署。官方：https://www.cns11643.gov.tw/ */
@font-face {
    font-family: "全字庫說文解字";
    src:
        local("全字庫說文解字"),
        local("EBAS"),
        url("fonts/ebas927.ttf") format("truetype");
    font-weight: normal;
    font-style: normal;
    font-display: swap;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: "Kaiti TC", "STKaiti", "Songti TC", "Noto Serif TC", serif;
    color: var(--ink);
    background: linear-gradient(135deg, #ffeef2, #ffd8e5 35%, #ffeaf2);
}

.site-header {
    padding: 20px 24px;
    border-bottom: 1px solid rgba(142, 33, 87, 0.15);
    background: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(6px);
}

.site-header h1 {
    margin: 8px 0 6px;
}

.site-header p {
    margin: 0;
    color: #7c4761;
}

.back-link {
    color: var(--berry-dark);
    text-decoration: none;
    font-size: 0.95rem;
}

.layout {
    display: grid;
    grid-template-columns: 340px 1fr;
    gap: 20px;
    padding: 20px;
}

.panel {
    background: rgba(255, 255, 255, 0.86);
    border: 1px solid rgba(142, 33, 87, 0.15);
    border-radius: 14px;
    padding: 14px;
    position: sticky;
    top: 12px;
    height: fit-content;
}

label {
    display: block;
    font-size: 0.9rem;
    margin: 10px 0 6px;
    color: #6a3f55;
}

textarea,
input,
select {
    width: 100%;
    border: 1px solid rgba(142, 33, 87, 0.25);
    border-radius: 10px;
    padding: 8px 10px;
    font-size: 0.95rem;
    background: #fff;
}

.btn-chenyu {
    margin-top: 8px;
    width: 100%;
    padding: 8px 10px;
    font-size: 0.88rem;
    border-radius: 10px;
    border: 1px dashed rgba(142, 33, 87, 0.35);
    background: rgba(255, 255, 255, 0.9);
    color: var(--berry-dark);
    cursor: pointer;
    line-height: 1.35;
}

.btn-chenyu:hover {
    background: rgba(142, 33, 87, 0.06);
    border-color: rgba(142, 33, 87, 0.45);
}

.font-hint {
    margin: 6px 0 0;
    font-size: 0.78rem;
    color: #8a5b73;
    line-height: 1.4;
}

.font-hint a {
    color: var(--berry-mid);
}

textarea {
    resize: vertical;
    min-height: 160px;
}

.row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.row.row-single {
    grid-template-columns: 1fr;
}

.label-sub {
    font-weight: 400;
    color: #8a5b73;
    font-size: 0.88em;
}

.actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-top: 14px;
}

.actions.actions-5 {
    grid-template-columns: repeat(3, 1fr);
}

@media (max-width: 400px) {
    .actions.actions-5 {
        grid-template-columns: 1fr 1fr;
    }
}

button {
    border: none;
    border-radius: 10px;
    padding: 10px;
    font-size: 0.92rem;
    cursor: pointer;
    background: linear-gradient(135deg, var(--berry-mid), var(--berry-dark));
    color: #fff;
}

button:hover {
    filter: brightness(1.05);
}

.status {
    margin: 10px 0 0;
    font-size: 0.85rem;
    color: #70455d;
}

.preview-wrap {
    min-width: 0;
}

.preview {
    display: grid;
    gap: 16px;
}

/* 描紅版：格線略淡，與範字協調 */
.preview.preview--hong,
.preview.preview--trace {
    --line-soft: rgba(199, 79, 123, 0.22);
}

/* 淺粉色描紅（實心）：格線略柔，襯淺粉字 */
.preview.preview--light-pink-hong {
    --line-soft: rgba(199, 79, 123, 0.18);
}

.copy-style-hint {
    font-size: 0.85rem;
    color: #7c4761;
    line-height: 1.45;
    margin: 0.45rem 0 0;
}

.preview--a4 .page {
    max-width: 210mm;
    margin-left: auto;
    margin-right: auto;
}

.preview--a4l .page {
    max-width: 297mm;
    margin-left: auto;
    margin-right: auto;
}

.preview--letter .page {
    max-width: 8.5in;
    margin-left: auto;
    margin-right: auto;
}

.page {
    background: var(--paper);
    border-radius: 10px;
    box-shadow: 0 8px 30px rgba(142, 33, 87, 0.12);
    padding: 18px;
    border: 1px solid rgba(142, 33, 87, 0.1);
    overflow-x: auto;
}

.page-title {
    font-size: 0.85rem;
    color: #8a5b73;
    margin-bottom: 10px;
}

.grid {
    display: grid;
    gap: 0;
    border: 1px solid var(--line-soft);
    /* 右側邊框改由最後一格繪製，避免寬內容溢出時外框看起來消失 */
    border-right: none;
    width: max-content;
    max-width: none;
    margin-left: 0;
    margin-right: 0;
}

.cell {
    position: relative;
    border-right: 1px solid var(--line-soft);
    border-bottom: 1px solid var(--line-soft);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #3d2130;
    background: #fff;
    overflow: hidden;
}

.cell.col-last {
    /* 保留右側邊框 */
    border-right: 1px solid var(--line-soft);
}

.cell.row-last {
    border-bottom: none;
}

.cell-inner {
    position: relative;
    z-index: 2;
}

.cell-inner--hong,
.cell-inner--trace {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    min-width: 0;
    min-height: 0;
    font-weight: 400;
}

.cell-inner--hong svg,
.cell-inner--trace svg {
    flex: 1 1 auto;
    display: block;
    width: 100%;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
    overflow: visible;
}

.cell.tian::before,
.cell.mi::before,
.cell.mi::after {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
}

/* 田字／米字中心十字：虛線（repeating 漸層置於中央 1px 帶內） */
.cell.tian::before,
.cell.mi::before {
    background-image:
        repeating-linear-gradient(
            to bottom,
            var(--line-soft) 0 4px,
            transparent 4px 9px
        ),
        repeating-linear-gradient(
            to right,
            var(--line-soft) 0 4px,
            transparent 4px 9px
        );
    background-size: 1px 100%, 100% 1px;
    background-position: center center;
    background-repeat: no-repeat;
}

/* 米字格對角線：SVG 虛線，隨格縮放 */
.cell.mi::after {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cline x1='0' y1='0' x2='100' y2='100' stroke='%23c74f7b' stroke-opacity='0.35' stroke-width='1.2' stroke-dasharray='5 4' fill='none'/%3E%3Cline x1='100' y1='0' x2='0' y2='100' stroke='%23c74f7b' stroke-opacity='0.35' stroke-width='1.2' stroke-dasharray='5 4' fill='none'/%3E%3C/svg%3E");
    background-size: 100% 100%;
    background-position: center;
    background-repeat: no-repeat;
}

@media (max-width: 980px) {
    .layout {
        grid-template-columns: 1fr;
    }

    .panel {
        position: static;
    }
}

@media print {
    body {
        background: #fff;
    }

    .site-header,
    .panel {
        display: none;
    }

    .layout {
        display: block;
        padding: 0;
    }

    .page {
        box-shadow: none;
        border: none;
        margin: 0;
        padding: 0;
        overflow: visible;
        break-after: page;
        page-break-after: always;
    }

    .page-title {
        display: none;
    }
}
