/* ==========================================================================
   GLOBAL SETUP & LAYOUT ENGINE (RESPONSIVE & FLUID)
   ========================================================================== */
@page {
    margin: 10mm 10mm 16mm 10mm;
    background-color: transparent;
    @bottom-right {
        content: counter(page);
        vertical-align: top;
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        font-size: 8pt;
        color: #a0aec0;
    }
}

* { box-sizing: border-box; }

body {
    margin: 0;
    padding: 0;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    color: #2b2e35;
    background-color: #fcfdfe;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
    font-size: 10pt;
    line-height: 1.4;
}

/* Fluid Web Container - No Maximum Width Restrictions */
.book-container {
    margin: 0 auto;
    width: 100%;
}

@media screen {
    .book-container { padding: 20px; }
}

@media print {
    .book-container { padding: 0; }

    body,
    .book-container,
    .book-page.is-landscape {
        background-color: transparent !important;
        border: none !important;
        outline: none !important;
        box-shadow: none !important;
    }
}

ul {
    padding: 0 0 0 12px;
    margin: 0;
}

/* Page Break System */
.book-page {
    page-break-before: always;
    page-break-after: avoid;
    page-break-inside: auto;
    position: relative;
    clear: both;
}

.book-page:first-child { page-break-before: avoid; }

.force-break { page-break-before: always !important; }

/* Standalone chapter-page nav strip (back-to-index + prev/next) — only
   generated on per-chapter pages by reference/build.js, never on the
   combined index.html. Hidden when printing a single chapter, same as the
   rest of the site's chrome-vs-content split. */
.chapter-nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: 1px solid #e2e5ea;
    font-size: 9pt;
}
.chapter-nav a {
    color: #185fa5;
    text-decoration: none;
}
.chapter-nav a:hover { text-decoration: underline; }
.chapter-nav .chapter-nav-prev { text-align: left; }
.chapter-nav .chapter-nav-index { text-align: center; }
.chapter-nav .chapter-nav-next { text-align: right; }
.chapter-nav .chapter-nav-disabled { color: #a0aec0; }

@media print {
    .chapter-nav { display: none; }
}

/* Link back to the full (all-chapters, printable) compendium — shown only
   on the lightweight TOC-only index page, above the TOC itself. */
.full-compendium-link {
    display: block;
    text-align: center;
    margin-bottom: 16px;
    padding: 10px;
    background: #f7f2ea;
    border: 1px solid #ddd0b8;
    border-radius: 8px;
    color: #185fa5;
    text-decoration: none;
    font-size: 9.5pt;
}
.full-compendium-link:hover { text-decoration: underline; }

/* Table of Contents Formatting */
.toc-wrapper { padding: 5px; }

.toc-title {
    text-align: center;
    font-size: clamp(14pt, 3vw, 16pt);
    text-transform: uppercase;
    font-weight: bold;
    letter-spacing: 1px;
    color: #1a202c;
    margin-bottom: 4px;
}

.toc-subtitle {
    text-align: center;
    font-size: clamp(8.5pt, 2vw, 9.5pt);
    color: #718096;
    font-style: italic;
    margin-bottom: 16px;
}

.toc-list {
    display: flex;
    flex-direction: column;
    gap: 14px;
    border-top: 2px solid #2d3748;
    padding-top: 14px;
}

.toc-section {
    margin-bottom: 2px;
    page-break-inside: avoid;
}

.toc-item-main {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    text-decoration: none;
    font-weight: bold;
    font-size: 11pt;
    margin-bottom: 4px;
}

.toc-sub-stack {
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding-left: 16px;
    margin-top: 2px;
}

.toc-item-sub {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    text-decoration: none;
    color: #4a5568;
    font-size: 9pt;
    font-weight: normal;
}

.toc-item-main:hover .toc-name-text,
.toc-item-sub:hover .toc-name-text { text-decoration: underline; }

.toc-leader {
    flex-grow: 1;
    border-bottom: 1px dashed #cbd5e1;
    margin: 0 6px 3px 6px;
}

/* Collapsible sub-section lists — index.html only (loadIndexPage() adds
   .toc-collapsible to .toc-wrapper after inserting the TOC fragment).
   reference.html (loadCombinedPage()) never adds that class, so the
   chevron button stays hidden and every sub-list stays fully expanded
   there, same as before this feature existed — a printed page can't be
   interacted with, so nothing should be collapsed on it. */
.toc-chevron-btn {
    display: none;
}
.toc-collapsible .toc-chevron-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    width: 18px;
    height: 18px;
    margin-right: 4px;
    padding: 0;
    border: none;
    background: none;
    color: #718096;
    cursor: pointer;
    transition: transform 0.15s ease;
}
.toc-collapsible .toc-chevron-btn.open {
    transform: rotate(90deg);
}
.toc-collapsible .toc-section {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
}
.toc-collapsible .toc-item-main {
    flex: 1 1 auto;
    min-width: 0;
}
/* Direct child only — the Appendix's "Wall Setup & Dealing Process" entry
   has its own nested .toc-sub-stack one level deeper, with no chevron of
   its own; it should just inherit its parent's visibility, not be
   independently collapsible. */
.toc-collapsible .toc-section > .toc-sub-stack {
    flex: 1 1 100%;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.18s ease;
    margin-top: 0;
    /* Base .toc-sub-stack padding-left (16px) indents subs under the
       chapter name's own start — but the chevron button (18px + 4px
       margin-right) pushes that name over by 22px here, so add the same
       amount to keep the two aligned. */
    padding-left: 38px;
}
.toc-collapsible .toc-section > .toc-sub-stack.open {
    max-height: 600px;
    margin-top: 2px;
}

.toc-page-num {
    font-family: "Courier New", Courier, monospace;
    font-weight: bold;
}

/* Generic Master Component Blocks (Powered by CSS Variables) */
.header {
    text-align: center;
    color: #ffffff;
    padding: 10px;
    margin-bottom: 12px;
    border-radius: 4px;
    page-break-inside: avoid;
    page-break-after: avoid;
    background-color: var(--theme-header-bg, #333333);
}

.header h1 {
    margin: 0;
    font-size: clamp(12pt, 2.5vw, 13pt);
    letter-spacing: 1px;
    text-transform: uppercase;
    font-weight: 700;
}

.legend {
    padding: 8px 12px;
    margin-bottom: 14px;
    border-radius: 4px;
    page-break-inside: avoid;
    background-color: var(--theme-legend-bg, #ffffff);
    border: 1px solid var(--theme-legend-border, #dcdfe6);
}

/* Two legends side by side (combined HKOS chapter: Scoring Limit + Variant Key) */
.legend-pair {
    display: flex;
    gap: 14px;
    align-items: stretch;
    flex-wrap: wrap;
    page-break-inside: avoid;
}
.legend-pair > .legend {
    flex: 1 1 260px;
}

.legend-title {
    font-weight: bold;
    margin-bottom: 6px;
    font-size: 9.5pt;
    text-transform: uppercase;
    color: var(--theme-legend-title, #1a202c);
}

#cc-legend-scoring-process .legend-grid {
    display: grid;
    grid-auto-flow: column;           /* Tells items to fill vertically first */
    grid-template-rows: repeat(3, auto); /* Desktop: 3 rows yields 3 columns for 9 items */
    gap: 10px 16px;
}

#cc-legend-scoring-process .legend-cell {
    font-size: 9.5pt;
}

/* Responsive Shrink: Switch to 2 columns */
@media screen and (max-width: 750px) {
    #cc-legend-scoring-process .legend-grid {
        /* 5 rows forces the 9 items to split across exactly 2 columns */
        grid-template-rows: repeat(5, auto);
    }
}

.legend-grid {
    display: flex;
    flex-direction: column;
    gap: 4px;
    width: 100%;
}

.legend-row {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: space-between;
}

.legend-cell {
    flex: 1 1 30%;
    font-size: 9.5pt;
    min-width: 120px;
}

.section-box {
    margin-bottom: 16px;
    background-color: transparent;
    page-break-inside: auto;
}

.section-header {
    color: #ffffff;
    font-weight: bold;
    font-size: 11pt;
    padding: 6px 12px;
    text-transform: uppercase;
    border-radius: 4px;
    margin-bottom: 6px;
    page-break-inside: avoid;
    page-break-after: avoid;
    background-color: var(--theme-section-bg, #555555);
}

/* Semantic Tile Palette Colors */
.crak { color: #b83b5e; font-weight: bold; }
.bam { color: #2ea44f; font-weight: bold; }
.dot { color: #2271b1; font-weight: bold; }
.wind { color: #7952b3; font-weight: bold; }
.flower { color: #e67e22; font-weight: bold; }

/* Hong Kong Style tier colors — used on the combined chapter to mark which
   variant introduced each hand (base hands keep .col-name's neutral grey).
   Compound selector overrides .col-name's hardcoded color. */
.col-name.tier-new6 { color: #0f766e; }   /* teal — New 6 additions */
.col-name.tier-new18 { color: #b45309; }  /* amber/bronze — New 18 additions */

/* ==========================================================================
   GLOBAL HAND GRID COMPONENT ENGINE
   ========================================================================== */
.hand-table {
    width: 100%;
    display: flex;
    flex-direction: column;
    border-bottom: 1px solid #f1f5f9;
}

.hand-row {
    padding: 8px 6px;
    page-break-inside: avoid;
    border-top: 1px solid #f1f5f9;
}

.hand-row:nth-child(even) { background-color: var(--theme-row-even-bg, #fafbfc); }

.hand-meta {
    margin-bottom: 3px;
    display: flex;
    align-items: center;
}

.col-num {
    display: inline-block;
    width: 40px;
    font-weight: bold;
    font-size: 10pt;
    text-align: center;
    border-radius: 3px;
    margin-right: 8px;
    flex-shrink: 0;
    white-space: nowrap;
    padding: 2px 0;
    background-color: transparent;
    color: var(--theme-num-color, #2b2e35);
}

.col-name {
    display: inline-block;
    font-weight: bold;
    color: #2b2e35;
    font-size: 10.5pt;
    vertical-align: middle;
}

.col-pattern {
    margin-bottom: 3px;
    font-family: "Courier New", Courier, monospace;
    font-weight: bold;
    font-size: 11.5pt;
    letter-spacing: 0.5px;
    padding: 2px 6px;
    border-radius: 3px;
    display: inline-block;
    background-color: transparent;
}

.col-pattern, .col-desc { margin-left: 48px !important; }

.col-desc {
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 9.5pt;
    line-height: 1.35;
    display: block;
    color: var(--theme-desc-color, #2b2e35);
}

.exc {
    color: #718096;
    font-size: 7.5pt;
    font-style: italic;
    display: block;
    margin-top: 2px;
}

.pattern-group { white-space: nowrap; }

/* Responsive Grid Adjustments */
@media screen and (min-width: 750px), print and (min-width: 7in) {
    body { font-size: 10.5pt; }

    .hand-row {
        display: grid;
        grid-template-columns: 50px minmax(280px, 2.25fr) 1.75fr;
        grid-template-rows: max-content 1fr;
        align-items: start;
        gap: 4px 16px;
        padding: 10px 8px;
    }

    .hand-meta { display: contents; }
    .col-num { grid-column: 1; grid-row: 1 / span 2; margin-right: 0; font-size: 10.5pt; align-self: start; padding-top: 2px; }
    .col-name { grid-column: 2; grid-row: 1; font-size: 11pt; align-self: start; padding-top: 2px; }
    .col-pattern { grid-column: 2; grid-row: 2; margin-left: 0 !important; margin-bottom: 0; align-self: start; display: block; }
    .col-desc { grid-column: 3; grid-row: 1 / span 2; margin-left: 0 !important; font-size: 10.5pt; align-self: start; }
}

@media screen and (min-width: 1100px) {
    .hand-row { grid-template-columns: 50px minmax(200px, 1.5fr) minmax(240px, 1.5fr) 2fr; grid-template-rows: auto; gap: 16px; }
    .col-num { grid-row: 1; }
    .col-name { grid-column: 2; }
    .col-pattern { grid-column: 3; grid-row: 1; padding-top: 2px; }
    .col-desc { grid-column: 4; grid-row: 1; }
}

/* ==========================================================================
   UNIVERSAL COMPENDIUM TABLE ENGINE
   ========================================================================== */
.c-table-container {
    padding: 6px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.c-table-card {
    border: 1px solid var(--theme-legend-border);
    border-radius: 4px;
    background-color: #ffffff;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    width: 100%;
    page-break-inside: avoid;
}

.c-table-row {
    display: flex;
    width: 100%;
    border-bottom: 1px solid var(--theme-legend-border);
    page-break-inside: avoid;
}

.c-table-row:last-child {
    border-bottom: none;
}

.c-table-header {
    background-color: var(--theme-section-bg);
    color: #ffffff;
}

.c-table-th {
    padding: 6px 8px; /* Changed from 8px 10px */
    font-weight: bold;
    font-size: 8.5pt;
    text-transform: uppercase;
    align-self: center;
    min-width: 0;
}

.c-table-td {
    padding: 6px 8px; /* Changed from 8px 10px */
    font-size: 9.5pt;
    color: var(--theme-desc-color);
    align-self: center;
    line-height: 1.25;  /* Slightly tighter line spacing */
    min-width: 0;
}

/* Table Modifiers */
.c-row-alt { background-color: var(--theme-row-even-bg); }
.c-border-thick { border-bottom: 2px solid var(--theme-section-bg); }
.c-row-total { background-color: var(--theme-total-bg, #eeeeee); }
.c-row-total .c-table-td { font-weight: bold; color: var(--theme-legend-title); }

.c-net-col {
    border-left: 2px solid var(--theme-section-bg);
    background-color: var(--theme-net-bg);
}
.c-table-header .c-net-col {
    border-left: 2px solid #ffffff;
    background-color: transparent;
}

.c-diagonal { color: #a0aec0; font-weight: bold; }
.c-text-positive { color: #2ea44f; font-size: 10.5pt; }
.c-text-negative { color: #b83b5e; font-size: 10pt; }

/* Rules Block Integrations */
.c-rules-card {
    padding: 6px;
    background-color: var(--theme-legend-bg);
}

.c-legend-title {
    font-weight: bold;
    font-size: 9.5pt;
    text-transform: uppercase;
    color: var(--theme-legend-title);
    letter-spacing: 0.3px;
    margin-bottom: 6px;
}

.c-rules-text {
    font-size: 9.5pt;
    line-height: 1.5;
    color: var(--theme-desc-color);
}

.c-rules-text ul {
    margin: 4px 0 0 0;
    padding-left: 20px;
}

.c-rules-text li { margin-bottom: 4px; }

/* Table Matrix Stacking Components */
.c-table-stack {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.c-matrix-block { page-break-inside: avoid; }
.c-matrix-title {
    font-weight: bold;
    font-size: 9.5pt;
    text-transform: uppercase;
    color: var(--theme-legend-title);
    margin-bottom: 6px;
    letter-spacing: 0.3px;
}

.c-matrix-meta {
    font-size: 8.5pt;
    font-weight: normal;
    text-transform: none;
    color: var(--theme-desc-color);
    margin-left: 6px;
    white-space: nowrap;
}

/* Width & Text Alignment Utilities */
.u-text-left { text-align: left; }
.u-text-center { text-align: center; }
.u-text-right { text-align: right; }
.u-text-bold { font-weight: bold; }
.u-text-mono { font-family: "Courier New", Courier, monospace; font-weight: bold; white-space: nowrap; }
.u-text-highlight { font-weight: bold; color: var(--theme-legend-title); }
.u-text-nowrap { white-space: nowrap; }

/* Flex Column Width Utilities */
.u-col-0-5 { flex: 0.5 1 0%; }
.u-col-1 { flex: 1 1 0%; }
.u-col-1-2 { flex: 1.2 1 0%; }
.u-col-1-25 { flex: 1.25 1 0%; }
.u-col-1-3 { flex: 1.3 1 0%; }
.u-col-1-5 { flex: 1.5 1 0%; }
.u-col-2 { flex: 2 1 0%; }
.u-col-3 { flex: 3 1 0%; }

/* Limit custom width max for single grids */
.c-mult-container-limit { max-width: 400px; margin: 0 auto; }

/* ==========================================================================
   INTERACTIVE MCR CALCULATOR COMPONENT
   ========================================================================== */
.mcr-calc-wrapper {
    width: 100%;
    padding: 20px 8px;
    border-top: 1px solid #f1f5f9;
    border-bottom: 1px solid #f1f5f9;
    background-color: var(--theme-row-even-bg, #fafbfc);
}

.mcr-calc-card {
    display: block;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    max-width: 450px;
    width: 100%;
    margin: 0 auto;
    padding: 20px;
    border: 1px solid var(--theme-legend-border, #e2e8f0);
    border-radius: 8px;
    background-color: #ffffff;
    text-align: center;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
    page-break-inside: avoid;
}

.mcr-calc-text {
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 9.5pt;
    color: var(--theme-desc-color, #4a5568);
    line-height: 1.4;
    margin-top: 0;
    margin-bottom: 16px;
}

.mcr-calc-qr-wrap { margin-bottom: 16px; }

.mcr-calc-qr-img {
    width: 150px;
    height: 150px;
    border: 1px solid var(--theme-row-even-bg, #edf2f7);
    padding: 4px;
    border-radius: 4px;
    background: #ffffff;
}

.mcr-calc-btn {
    display: inline-block;
    color: #ffffff;
    background-color: var(--theme-section-bg, #1e3a5f);
    padding: 8px 16px;
    font-size: 9.5pt;
    font-weight: bold;
    text-decoration: none;
    border-radius: 4px;
}

.mcr-calc-link-wrap { margin-top: 10px; }
.mcr-calc-link { color: #718096; font-size: 8pt; text-decoration: underline; }

/* ==========================================================================
   APPENDIX WALL SETUP & DEALING PROCESS STYLES
   ========================================================================== */

.section-content-text {
    margin-left: 15px;
    line-height: 1.5;
    color: var(--theme-desc-color);
}

.section-content-text p {
    margin-top: 15px;
    margin-bottom: 0;
}

.section-content-text p:first-of-type {
    margin-top: 0;
}

.dice-grid {
    display: grid;
    grid-template-columns: 75px 1fr;
    gap: 4px 0;
    margin: 8px 0 15px 0;
    page-break-inside: avoid;
}

.dice-grid.triple-system {
    grid-template-columns: 120px 1fr;
}

.dice-flex-container {
    display: flex;
    gap: 40px;
    margin: 15px 0;
    page-break-inside: avoid;
}

.dice-flex-col {
    flex: 1;
}

.dice-flex-col p {
    margin: 0 0 8px 0;
}

/* ==========================================================================
   THEME VARIABLE DICTIONARIES
   ========================================================================== */

/* FORMAT 1: CHINESE CLASSICAL RULES */
.cc-theme {
    --theme-header-bg: #4a3728;
    --theme-section-bg: #4a3728;
    --theme-legend-bg: #faf8f5;
    --theme-legend-border: #e8e5de;
    --theme-legend-title: #4a3728;
    --theme-num-color: #4a3728;
    --theme-row-even-bg: #faf8f5;
    --theme-desc-color: #2b2e35;
    --theme-net-bg: rgba(250, 248, 245, 0.5);
    --theme-total-bg: #eeeeee;
}

/* FORMAT 2: HONG KONG OLD STYLE RULES */
.hkos-theme {
    --theme-header-bg: #1b4332;
    --theme-section-bg: #1b4332;
    --theme-legend-bg: #f9fbfb;
    --theme-legend-border: #e2e9e5;
    --theme-legend-title: #1b4332;
    --theme-num-color: #4a5568;
    --theme-row-even-bg: #f4f7f5;
    --theme-desc-color: #4a5568;
    --theme-net-bg: rgba(244, 247, 245, 0.5);
    --theme-total-bg: #eeeeee;
}

/* FORMAT 3: MAHJONG COMPETITION RULES (MCR) */
.mcr-theme {
    --theme-header-bg: #1e3a5f;
    --theme-section-bg: #1e3a5f;
    --theme-legend-bg: #f6f8fb;
    --theme-legend-border: #e2e8f0;
    --theme-legend-title: #1e3a5f;
    --theme-num-color: #4a5568;
    --theme-row-even-bg: #f8fafc;
    --theme-desc-color: #4a5568;
    --theme-net-bg: rgba(248, 250, 252, 0.5);
    --theme-total-bg: #eeeeee;
}

/* FORMAT 4: ZUNG JUNG RULES */
.zj-theme {
    --theme-header-bg: #4a1525;
    --theme-section-bg: #5c1d31;
    --theme-legend-bg: #fdfafb;
    --theme-legend-border: #f0e2e4;
    --theme-legend-title: #4a1525;
    --theme-num-color: #5c1d31;
    --theme-row-even-bg: #fdfafb;
    --theme-desc-color: #4a5568;
    --theme-net-bg: rgba(253, 250, 251, 0.5);
    --theme-total-bg: #eeeeee;
}

/* FORMAT 5: SINGAPOREAN RULES */
.sg-theme {
    --theme-header-bg: #0f4c5c;
    --theme-section-bg: #145f73;
    --theme-legend-bg: #f6fafb;
    --theme-legend-border: #e0ecef;
    --theme-legend-title: #0f4c5c;
    --theme-num-color: #145f73;
    --theme-row-even-bg: #f6fafb;
    --theme-desc-color: #2b2e35;
    --theme-net-bg: rgba(246, 250, 251, 0.5);
    --theme-total-bg: #eeeeee;
}

/* FORMAT 6: APPENDIX THEME DICTIONARY */
.appendix-theme {
    --theme-header-bg: #333333;
    --theme-section-bg: #555555;
    --theme-legend-bg: #f9f9f9;
    --theme-legend-border: #dddddd;
    --theme-legend-title: #000000;
    --theme-num-color: #000000;
    --theme-row-even-bg: #f9f9f9;
    --theme-desc-color: #222222;
    --theme-net-bg: rgba(249, 249, 249, 0.5);
    --theme-total-bg: #eeeeee;
}

/* FORMAT 7: CHINESE SIMPLIFIED RULES */
.cs-theme {
    --theme-header-bg: #5a2a6b;
    --theme-section-bg: #6d3382;
    --theme-legend-bg: #faf7fc;
    --theme-legend-border: #ece2f0;
    --theme-legend-title: #5a2a6b;
    --theme-num-color: #6d3382;
    --theme-row-even-bg: #faf7fc;
    --theme-desc-color: #2b2e35;
    --theme-net-bg: rgba(250, 247, 252, 0.5);
    --theme-total-bg: #eeeeee;
}

/* FORMAT 8: BABCOCK'S STANDARD RULES */
.bsr-theme {
    --theme-header-bg: #004D40;
    --theme-section-bg: #00695C;
    --theme-legend-bg: #F4F9F8;
    --theme-legend-border: #DCE6E5;
    --theme-legend-title: #004D40;
    --theme-num-color: #004D40;
    --theme-row-even-bg: #F4F9F8;
    --theme-desc-color: #2b2e35;
    --theme-net-bg: rgba(244, 249, 248, 0.5);
    --theme-total-bg: #eeeeee;
}
