/* ═══════════════════════════════════════════════════════════════════════
   Preview Page — WYSIWYG worksheet preview with side controls
   ═══════════════════════════════════════════════════════════════════════ */

/* ── Chinese Font Stack ─────────────────────────────────────────────── */
/* Default is Song Ti. Linux fallback uses Noto Serif CJK (not Noto Sans which is heiti). */
.pv-chinese {
    font-family: var(--pv-font, "STKaiti", "Kaiti SC", "Kaiti TC", "KaiTi", "楷体",
                 "AR PL UKai CN", "Noto Serif CJK SC", serif);
}

/* ── Base ────────────────────────────────────────────────────────────── */
.pv-body {
    margin: 0;
    background: #f1f3f5;
    overflow-x: hidden;
}

/* ── Toolbar ─────────────────────────────────────────────────────────── */
.pv-toolbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 52px;
    background: rgba(255,255,255,0.92);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(0,0,0,0.08);
    display: flex;
    align-items: center;
    padding: 0 1.25rem;
    gap: 0.75rem;
    z-index: 100;
}

.pv-toolbar-back {
    font-size: 0.82rem;
    color: var(--hxc-green, #0284c7);
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.3rem 0.5rem;
    border-radius: 6px;
    transition: background .15s;
    flex-shrink: 0;
}
.pv-toolbar-back:hover {
    background: #f0f9ff;
    color: var(--hxc-green, #0284c7);
}

.pv-toolbar-title {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--hxc-text, #1a1a1a);
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.pv-toolbar-btn {
    font-size: 0.8rem;
    font-weight: 500;
    padding: 0.4rem 1rem;
    border: 1px solid var(--hxc-green, #0284c7);
    border-radius: 8px;
    background: var(--hxc-green, #0284c7);
    color: #fff;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    flex-shrink: 0;
    transition: all .15s;
    box-shadow: 0 1px 3px rgba(22,101,52,0.2);
}
.pv-toolbar-btn:hover {
    background: var(--hxc-green-mid, #0369a1);
    border-color: var(--hxc-green-mid, #0369a1);
    box-shadow: 0 2px 6px rgba(22,101,52,0.3);
    transform: translateY(-1px);
}
.pv-toolbar-btn:active {
    transform: translateY(0);
    box-shadow: 0 1px 2px rgba(22,101,52,0.2);
}

.pv-toolbar-btn-save {
    background: #fff;
    color: var(--hxc-green, #0284c7);
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.pv-toolbar-btn-save:hover {
    background: #f0f9ff;
    color: var(--hxc-green, #0284c7);
    box-shadow: 0 2px 6px rgba(0,0,0,0.1);
}

/* ── Sidebar ─────────────────────────────────────────────────────────── */
.pv-sidebar {
    position: fixed;
    top: 52px;
    left: 0;
    bottom: 0;
    width: 240px;
    background: #fff;
    border-right: 1px solid rgba(0,0,0,0.06);
    padding: 1.25rem 1rem;
    overflow-y: auto;
    z-index: 90;
    box-shadow: 1px 0 8px rgba(0,0,0,0.03);
}

.pv-sidebar-title {
    font-size: 0.68rem;
    font-weight: 700;
    color: var(--hxc-muted, #6b7280);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    margin-bottom: 0.9rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--hxc-border, #e5e7eb);
}

/* ── Sidebar Controls ────────────────────────────────────────────────── */
.pv-ctrl-group {
    margin-bottom: 1rem;
}

.pv-ctrl-label {
    display: flex;
    align-items: baseline;
    gap: 0.3rem;
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--hxc-text, #1a1a1a);
    margin-bottom: 0.35rem;
}

.pv-ctrl-val {
    font-weight: 700;
    color: var(--hxc-green, #0284c7);
    min-width: 1.4em;
    text-align: center;
}

.pv-ctrl-range {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 6px;
    background: var(--hxc-border, #e5e7eb);
    border-radius: 3px;
    outline: none;
    margin-top: 0.2rem;
    cursor: pointer;
}
.pv-ctrl-range::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px;
    height: 18px;
    background: var(--hxc-green, #0284c7);
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 1px 4px rgba(22,101,52,0.3);
    transition: transform .12s;
}
.pv-ctrl-range::-webkit-slider-thumb:hover {
    transform: scale(1.15);
}
.pv-ctrl-range::-moz-range-thumb {
    width: 18px;
    height: 18px;
    background: var(--hxc-green, #0284c7);
    border-radius: 50%;
    border: none;
    cursor: pointer;
    box-shadow: 0 1px 4px rgba(22,101,52,0.3);
}

/* Toggle switch (CSS-only) */
.pv-toggle {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    font-size: 0.82rem;
    color: var(--hxc-text, #1a1a1a);
    user-select: none;
}
.pv-toggle input[type="checkbox"] {
    -webkit-appearance: none;
    appearance: none;
    width: 36px;
    height: 20px;
    background: #d1d5db;
    border-radius: 10px;
    position: relative;
    cursor: pointer;
    transition: background .2s;
    flex-shrink: 0;
}
.pv-toggle input[type="checkbox"]::after {
    content: '';
    position: absolute;
    top: 2px;
    left: 2px;
    width: 16px;
    height: 16px;
    background: #fff;
    border-radius: 50%;
    transition: transform .2s;
    box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}
.pv-toggle input[type="checkbox"]:checked {
    background: var(--hxc-green, #0284c7);
}
.pv-toggle input[type="checkbox"]:checked::after {
    transform: translateX(16px);
}
.pv-toggle input[type="checkbox"]:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.pv-ctrl-hint {
    font-size: 0.68rem;
    color: var(--hxc-muted, #6b7280);
    margin-top: 0.15rem;
}

.pv-ctrl-select {
    width: 100%;
    padding: 0.25rem 0.4rem;
    font-size: 0.82rem;
    border: 1px solid var(--hxc-border, #e5e7eb);
    border-radius: 6px;
    outline: none;
    background: #fff;
    cursor: pointer;
    margin-top: 0.2rem;
}
.pv-ctrl-select:focus {
    border-color: var(--hxc-green, #0284c7);
}

.pv-ctrl-input {
    width: 4.5rem;
    padding: 0.25rem 0.4rem;
    font-size: 0.82rem;
    border: 1px solid var(--hxc-border, #e5e7eb);
    border-radius: 6px;
    outline: none;
    transition: border-color .15s;
}
.pv-ctrl-input:focus {
    border-color: var(--hxc-green, #0284c7);
}

.pv-ctrl-input-sm {
    width: 2.8rem;
    padding: 0.15rem 0.25rem;
    font-size: 0.82rem;
    border: 1px solid var(--hxc-border, #e5e7eb);
    border-radius: 6px;
    text-align: center;
    outline: none;
    transition: border-color .15s;
}
.pv-ctrl-input-sm:focus {
    border-color: var(--hxc-green, #0284c7);
}

.pv-ctrl-sub {
    margin-left: 0.5rem;
    margin-top: 0.4rem;
    padding-left: 0.75rem;
    border-left: 2px solid var(--hxc-green-light, #dcfce7);
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.pv-ctrl-inline {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.82rem;
    color: var(--hxc-text, #1a1a1a);
}

.pv-ctrl-sep {
    height: 1px;
    background: var(--hxc-border, #e5e7eb);
    margin: 0.25rem 0 0.75rem;
}

/* ── Content Area ────────────────────────────────────────────────────── */
.pv-content {
    margin-left: 240px;
    margin-top: 52px;
    padding: 2rem;
    background: #e9ecef;
    min-height: calc(100vh - 52px);
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* ── Paper Pages ─────────────────────────────────────────────────────── */
.pv-page {
    width: 8.5in;
    min-height: 11in;
    background: #fff;
    padding: 0.25in 0.5in 0.5in;  /* top reduced to 0.25in so header is near page top */
    margin-bottom: 2rem;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08), 0 4px 16px rgba(0,0,0,0.06);
    border-radius: 2px;
    position: relative;
    box-sizing: border-box;
}

/* ── Per-page student header: 姓名 (left) + worksheet title (center) ─── */
/* Height is controlled entirely by font size + padding — no explicit height */
.pv-sheet-header {
    display: flex;
    align-items: center;
    padding: 0.01in 0 0.02in;
    margin-bottom: 0.02in;
    border-bottom: 1px solid #bbb;
    line-height: 1;
    overflow: hidden;
}

.pv-sheet-header-name {
    display: flex;
    align-items: center;
    font-size: 7.5pt;
    color: #444;
    white-space: nowrap;
    min-width: 2in;
    gap: 0.04in;
    line-height: 1;
}

.pv-name-line {
    display: inline-block;
    width: 1.2in;
    border-bottom: 1px solid #666;
    height: 0.08in;
    vertical-align: middle;
}

.pv-sheet-header-title {
    flex: 1;
    text-align: center;
    font-size: 8.5pt;
    color: #000;
    font-weight: 700;
    letter-spacing: 0.03em;
    padding: 0 0.1in;
    line-height: 1;
}

.pv-sheet-header-spacer {
    min-width: 2in;
    display: flex;
    align-items: center;
    justify-content: flex-end;
}

/* Worksheet title at top of first page */
.pv-page-title {
    text-align: center;
    font-size: 14pt;
    font-weight: 700;
    color: #222;
    margin: 0 0 0.25in;
    padding-bottom: 0.12in;
    border-bottom: 1.5px solid #e0e0e0;
    letter-spacing: 0.02em;
}

/* Header with logo: flex row */
.pv-page-header {
    display: flex;
    align-items: center;
    margin: 0 0 0.25in;
    padding-bottom: 0.12in;
    border-bottom: 1.5px solid #e0e0e0;
}
.pv-page-header .pv-page-title {
    flex: 1;
    margin: 0;
    padding: 0;
    border: none;
}
.pv-header-logo {
    height: 0.15in;
    width: auto;
    max-width: 1in;
    object-fit: contain;
    flex-shrink: 0;
}

.pv-page-footer {
    position: absolute;
    bottom: 0.12in;
    left: 0.5in;
    right: 0.5in;
    display: flex;
    justify-content: space-between;
    font-size: 6.5pt;
    color: #bbb;
}

/* ── Mi-zi-ge Grid ───────────────────────────────────────────────────── */
.mi-zi-ge {
    position: relative;
    border: 1px solid #d44;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    /* Border color must print; exact needed for the red border */
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
}

/* Collapse double borders between adjacent cells */
.charcard-row .mi-zi-ge + .mi-zi-ge,
.write-grid-row .mi-zi-ge + .mi-zi-ge,
.write-trace-row .mi-zi-ge + .mi-zi-ge {
    border-left: none;
}

/* Grid line elements — CSS borders on HTML divs.
   CSS border + print-color-adjust: exact (via .pv-page *) prints WYSIWYG.
   No SVG engine involved — that was causing black-line overrides in print. */
.mzg-v, .mzg-h, .mzg-d1, .mzg-d2 {
    position: absolute;
    pointer-events: none;
}
/* Vertical center line */
.mzg-v {
    top: 0;
    bottom: 0;
    left: 50%;
    width: 0;
    border-left: 1.5px dashed rgb(238,162,162);
}
/* Horizontal center line */
.mzg-h {
    left: 0;
    right: 0;
    top: 50%;
    height: 0;
    border-top: 1.5px dashed rgb(238,162,162);
}
/* Diagonal: top-left → bottom-right */
.mzg-d1 {
    top: 0;
    left: 0;
    width: 141.42%;
    height: 1px;
    box-sizing: border-box;
    border-top: 1px dashed rgb(247,201,201);
    transform-origin: 0 0;
    transform: rotate(45deg);
}
/* Diagonal: top-right → bottom-left */
.mzg-d2 {
    top: 0;
    left: 100%;
    width: 141.42%;
    height: 1px;
    box-sizing: border-box;
    border-top: 1px dashed rgb(247,201,201);
    transform-origin: 0 0;
    transform: rotate(135deg);
}

.mi-zi-ge-char {
    position: relative;
    z-index: 1;
    line-height: 1;
}

/* ── Char Card Layout ────────────────────────────────────────────────── */
.charcard-row {
    display: flex;
    gap: 0;
    margin-bottom: 0.1in;
}

.charcard-cell {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.charcard-anno {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.03in;
    margin-bottom: 0.02in;
    width: 100%;
    overflow: hidden;
}

.charcard-anno .pinyin {
    color: #333;
    font-weight: 400;
    white-space: nowrap;
    font-family: 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
    flex-shrink: 0;
}

.charcard-anno img {
    flex-shrink: 1;
    object-fit: contain;
}

.charcard-anno .decomp {
    overflow: hidden;
    text-overflow: ellipsis;
    flex-shrink: 1;
}

/* ── Write Layout ────────────────────────────────────────────────────── */
.write-section {
    margin-bottom: 0.15in;
    page-break-inside: avoid;
}

.write-anno-row {
    display: flex;
    gap: 0;
    margin-bottom: 2px;
}

/* Grouped annotation: single line, no wrap, shrink images to fit grid width */
.write-grouped-anno {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: 0.08in;
}

.write-grouped-anno img {
    flex-shrink: 1;
    min-width: 0;
    width: auto;
    object-fit: contain;
}

.write-grouped-anno .pinyin,
.write-grouped-anno .decomp {
    flex-shrink: 0;
    white-space: nowrap;
}

.write-anno-cell {
    display: flex;
    flex-direction: row;
    align-items: center;
    flex-shrink: 0;
    overflow: hidden;
    gap: 0.03in;
}

.write-anno-line {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 0.1in;
    white-space: nowrap;
}

.write-anno-line .pinyin {
    color: #333;
    font-weight: 400;
    white-space: nowrap;
    font-family: 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
}

.write-grid-row {
    display: flex;
    gap: 0;
}

.write-notes-row {
    border-bottom: 1px dashed #ccc;
    height: 0.25in;
    margin-top: 0.05in;
}

/* Write: Tracing mode */
.write-trace-section {
    page-break-inside: avoid;
    margin-bottom: 0;
}

.write-trace-header {
    display: flex;
    align-items: center;
    gap: 0.12in;
    margin-bottom: 0.06in;
}

.write-trace-header .pinyin {
    color: #333;
    font-weight: 400;
    font-family: 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
}

.decomp {
    font-size: 9pt;
    color: #222;
    white-space: nowrap;
}

.write-trace-row {
    display: flex;
    gap: 0;
    flex-wrap: wrap;
}

/* ── Flash Card Layout ───────────────────────────────────────────────── */
.flash-table {
    width: 100%;
    border-collapse: collapse;
}

.flash-table td {
    border: 1.5px solid #ccc;
    padding: 0.1in 0.25in;
    vertical-align: middle;
}

.flash-table .flash-char {
    width: 35%;
    text-align: center;
    font-weight: 400;
    color: #111;
}

.flash-table .flash-pinyin {
    width: 30%;
    text-align: center;
    color: #333;
    font-weight: 400;
    font-family: 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
}

.flash-table .flash-def {
    width: 35%;
    text-align: left;
    color: #444;
    font-size: 0.75em;
    line-height: 1.3;
    font-style: italic;
}

/* Fold line between character and pinyin columns */
.flash-table .flash-pinyin {
    border-left: 1.5px dashed #bbb;
}

.flash-table .flash-def {
    border-left: 1.5px dashed #bbb;
}

/* ── Read Layout ─────────────────────────────────────────────────────── */
.read-paragraph {
    margin-bottom: 0.25in;
    line-height: 2.4;
}

.read-char-wrap {
    display: inline-block;
    text-align: center;
    vertical-align: bottom;
}

.read-char-wrap .pinyin {
    display: block;
    font-size: 9pt;
    color: #333;
    line-height: 1.2;
    font-family: 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
    font-weight: 500;
}

.read-char-wrap .char {
    display: block;
    line-height: 1.4;
    color: #000;
}

.read-punct {
    margin-left: -0.15em;
}

/* ── Loading State ───────────────────────────────────────────────────── */
.pv-loading {
    text-align: center;
    padding: 4rem 2rem;
    color: var(--hxc-muted, #6b7280);
    font-size: 0.9rem;
}

.pv-spinner {
    display: inline-block;
    width: 28px;
    height: 28px;
    border: 3px solid var(--hxc-border, #e5e7eb);
    border-top-color: var(--hxc-green, #0284c7);
    border-radius: 50%;
    animation: pv-spin 0.7s linear infinite;
    margin-bottom: 0.75rem;
}

@keyframes pv-spin {
    to { transform: rotate(360deg); }
}

/* ═══════════════════════════════════════════════════════════════════════
   Print Styles
   ═══════════════════════════════════════════════════════════════════════ */
@media print {
    /* Hide everything that isn't preview page content */
    .pv-toolbar,
    .pv-sidebar,
    .no-print,
    .hx-topbar,
    .ws-list-panel,
    .ws-ws-header,
    .ws-text-area,
    .ws-ops-bar,
    .ws-preview-header,
    .ws-preview-controls,
    #email-verification-banner,
    .modal,
    .modal-backdrop {
        display: none !important;
    }

    /* Hide the site footer */
    body > footer,
    footer {
        display: none !important;
    }

    body {
        background: none !important;
        min-height: auto !important;
        display: block !important;
    }

    .pv-body {
        background: none;
    }

    .pv-content {
        margin-left: 0;
        margin-top: 0;
        padding: 0;
        background: none;
    }

    /* Remove layout constraints so preview fills the page */
    #content,
    .hx-page-content,
    .ws-workspace-layout,
    .ws-workspace-panel,
    #ws-preview-section,
    .pv-content-inline {
        display: block !important;
        width: auto !important;
        max-width: none !important;
        max-height: none !important;
        height: auto !important;
        margin: 0 !important;
        padding: 0 !important;
        border: none !important;
        overflow: visible !important;
        box-shadow: none !important;
        flex: none !important;
    }

    @page {
        size: letter;
        margin: 0;
    }

    .pv-page {
        width: 8.5in;
        min-height: auto;
        box-shadow: none;
        border-radius: 0;
        margin: 0;
        padding: 0.25in 0.5in 0.5in;
        page-break-after: always;
    }

    .pv-page:last-child {
        page-break-after: auto;
    }

    /* Prevent grid rows from wrapping in print */
    .write-grid-row,
    .write-trace-row,
    .charcard-row {
        flex-wrap: nowrap !important;
    }

    /* Force all content inside pages to preserve colors */
    .pv-page * {
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
        color-adjust: exact !important;
    }

    /* Grid lines are background-image on .mi-zi-ge; print-color-adjust: exact is on the element itself */
    .mi-zi-ge { border-color: #d44 !important; }
}
