:root {
    --page-bg: #f4f4f6;
    --paper: #ffffff;
    --text: #1f1f24;
    --accent: #1559a4;
}

/* Local fonts loaded from static/ */
@font-face {
    font-family: 'Taipei Sans TC Beta';
    src: url('/static/TaipeiSansTCBeta-Regular.ttf') format('truetype');
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}
@font-face {
    font-family: 'Taipei Sans TC Beta';
    src: url('/static/TaipeiSansTCBeta-Bold.ttf') format('truetype');
    font-weight: 700;
    font-style: normal;
    font-display: swap;
}
@font-face {
    font-family: 'Taipei Sans TC Beta';
    src: url('/static/TaipeiSansTCBeta-Light.ttf') format('truetype');
    font-weight: 300;
    font-style: normal;
    font-display: swap;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--page-bg);
    color: var(--text);
    font-family: "Georgia", "Times New Roman", serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
}

.toolbar {
    width: 100%;
    background: rgba(21, 89, 164, 0.1);
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    backdrop-filter: blur(6px);
    position: sticky;
    top: 0;
    z-index: 10;
    padding: 12px 18px;
}

.toolbar-inner {
    max-width: 900px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.title {
    font-weight: 700;
    letter-spacing: 0.3px;
}

.print-btn {
    background: var(--accent);
    border: none;
    color: #ffffff;
    padding: 10px 16px;
    border-radius: 6px;
    font-size: 14px;
    cursor: pointer;
    transition: transform 120ms ease, box-shadow 120ms ease;
    box-shadow: 0 6px 14px rgba(21, 89, 164, 0.25);
}

.print-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 8px 18px rgba(21, 89, 164, 0.3);
}

.print-btn:active {
    transform: translateY(0);
    box-shadow: 0 4px 10px rgba(21, 89, 164, 0.22);
}

.page {
    width: 210mm;
    min-height: 297mm;
    margin: 24px auto 32px auto;
    padding: 20mm 18mm;
    background: var(--paper);
    box-shadow: 0 8px 28px rgba(0, 0, 0, 0.15);
    border: 1px solid rgba(0, 0, 0, 0.04);
.draw-game-title { font-weight: 700; margin: 8px 0 6px 0; font-size: 13px; }
    .draw-table { width: 100%; border-collapse: collapse; margin-bottom: 8px; table-layout: fixed; font-size: 12px; }
.draw-table th, .draw-table td { border: 1px solid #ccc; text-align: center; vertical-align: middle; }
.draw-table thead th { background: #fafafa; font-weight: 700; }
.draw-table td.draw-number { width: 1%; white-space: nowrap; font-weight: 700; }
.draw-table td.draw-number.special { background: #fff3f0; font-weight: 700; color: #b33; }
.draw-table th.special-header { background: #fff0f0; }
    position: relative;
    overflow: hidden;
.draw-table th:first-child, .draw-table td:first-child { width: 10%; }
.draw-table th:nth-child(2), .draw-table td:nth-child(2) { width: 10%; }
}

/* Screen-only auto-scaling: scale the page visually to fit viewport
   without changing printed output. The transform is applied only
   for screen (JS sets scale) and print rules below force 1:1. */
@media screen {
    .page-wrap {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        align-items: flex-start;
        padding: 24px;
        gap: 24px;
    }

    .page {
        transform-origin: top center;
        transition: transform 140ms ease;
        /* transform is set by JS to a value <= 1; default none (1) */
        flex: 0 0 auto; /* Prevent shrinking */
    }
}

/* Page Manager Styles */
.page-manager-section {
    margin-bottom: 12px;
}

.full-width-btn {
    width: 100%;
    padding: 8px 12px;
    background-color: #fff;
    border: 1px solid #ccc;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    color: var(--text);
    margin-bottom: 12px;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.full-width-btn:hover {
    background-color: #f7f9fb;
    border-color: #bbb;
}

.page-list-container {
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: 250px;
    overflow-y: auto;
    padding-right: 4px;
}

.page-thumbnail-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px;
    border: 1px solid transparent;
    border-radius: 6px;
    cursor: pointer;
    transition: background-color 0.2s;
    position: relative;
}

.page-thumbnail-item:hover {
    background-color: rgba(0,0,0,0.03);
}

.page-thumbnail-item.active {
    background-color: rgba(21, 89, 164, 0.08);
    border-color: rgba(21, 89, 164, 0.2);
}

.page-thumbnail-item .page-controls {
    display: none;
    position: absolute;
    right: 4px;
    top: 50%;
    transform: translateY(-50%);
    gap: 4px;
}

.page-thumbnail-item:hover .page-controls {
    display: flex;
}

.page-control-btn {
    width: 24px;
    height: 24px;
    border: none;
    background: #fff;
    border-radius: 4px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    color: #555;
}

.page-control-btn:hover {
    background: #f0f0f0;
    color: #000;
}

.page-control-btn.delete:hover {
    background: #fff0f0;
    color: #d00;
}

.page.selected-page {
    outline: 4px solid rgba(21, 89, 164, 0.6);
    outline-offset: 4px;
}

.page-preview-box {
    width: 30px;
    height: 42px; /* Approx A4 ratio */
    background-color: #fff;
    border: 1px solid #ddd;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    position: relative;
    flex-shrink: 0;
}

.page-info {
    font-size: 13px;
    font-weight: 500;
    color: var(--text);
}

.separator {
    height: 1px;
    background-color: rgba(0,0,0,0.08);
    margin: 16px 0;
}

/* Editor layout: page on the left, module panel on the right */
.editor-area {
    display: flex;
    gap: 18px;
    align-items: flex-start;
    justify-content: center;
    padding: 18px;
}

.sidebar-left,
.sidebar-right {
    width: 240px;
    background: rgba(255,255,255,0.98);
    border: 1px solid rgba(0,0,0,0.06);
    border-radius: 8px;
    padding: 12px;
    box-shadow: 0 6px 18px rgba(0,0,0,0.06);
    font-family: inherit;
    color: var(--text);
    flex-shrink: 0; /* Prevent shrinking */
    position: sticky;
    top: 80px; /* Stick to top when scrolling */
    max-height: calc(100vh - 100px);
    overflow-y: auto;
}

.module-inspector {
    margin-top: 12px;
    background: transparent;
    border: none;
    padding: 0;
    box-shadow: none;
    font-family: inherit;
    color: var(--text);
    border-top: 1px solid rgba(0,0,0,0.06);
    padding-top: 10px;
}

.module-inspector h3 { margin: 8px 0 6px 0; font-size: 13px; }
.module-inspector .inspector-row { display:flex; align-items:center; gap:8px; margin-bottom:8px; }
.module-inspector input { width: 80px; }
.module-inspector .inspector-actions { display:flex; gap:8px; }

/* ensure hidden attribute hides inspector in all browsers */
[hidden] { display: none !important; }

.sidebar-left h3,
.sidebar-right h3 {
    margin: 0 0 8px 0;
    font-size: 15px;
}

.module-item {
    background: linear-gradient(180deg, #fff, #f7f9fb);
    border: 1px dashed rgba(0,0,0,0.08);
    padding: 12px;
    margin: 8px 0;
    border-radius: 6px;
    cursor: grab;
    user-select: none;
}

.module-item:active { cursor: grabbing; }

.page-content {
    position: absolute;
    inset: 0;
}

.placed-module {
    position: absolute;
    background: linear-gradient(180deg,#fff,#fbfdff);
    border: 1px solid rgba(0,0,0,0.08);
    box-shadow: 0 6px 14px rgba(0,0,0,0.06);
    border-radius: 4px;
    padding: 8px;
    overflow: hidden;
}

.placed-module { touch-action: none; }

.placed-module .handle {
    position: absolute;
    width: 12px;
    height: 12px;
    background: rgba(0,0,0,0.12);
    border: 1px solid rgba(255,255,255,0.6);
    border-radius: 2px;
    box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}
.placed-module .handle.se { right: -6px; bottom: -6px; cursor: se-resize; }

.placed-module.dragging { opacity: 0.92; transform: translateZ(0); }

/* Selected state */
.placed-module.selected {
    outline: 3px solid rgba(42,141,246,0.95);
    outline-offset: -3px;
    z-index: 50;
}

/* Small floating controls shown when module is selected */
.module-controls {
    position: absolute;
    display: flex;
    gap: 6px;
    background: rgba(255,255,255,0.98);
    padding: 6px;
    border-radius: 6px;
    box-shadow: 0 6px 18px rgba(0,0,0,0.12);
    border: 1px solid rgba(0,0,0,0.06);
    z-index: 120;
}

.module-controls button {
    border: none;
    background: #2a8df6;
    color: #fff;
    padding: 6px 8px;
    border-radius: 4px;
    font-size: 12px;
    cursor: pointer;
}

.module-controls button.danger { background: #d33; }

.preview {
    position: absolute;
    pointer-events: none;
    opacity: 0.92;
    z-index: 9999;
    border: 2px dashed rgba(21,89,164,0.7);
    background: rgba(21,89,164,0.04);
}

.grid-overlay {
    position: absolute;
    inset: 0;
    pointer-events: none;
    opacity: 0;
}

/* Improve rendering when transform is active */
.page {
    will-change: transform;
    transform-origin: top center;
}

article {
    max-width: 100%;
    line-height: 1.6;
    font-size: 15px;
}

h1 {
    margin-top: 0;
    margin-bottom: 14px;
    font-size: 24px;
    letter-spacing: 0.2px;
}

p {
    margin: 0 0 12px 0;
}

@page {
    size: A4;
    margin: 0mm;
}

@media print {
    html,
    body {
        width: 210mm;
        height: 297mm;
        margin: 0;
        background: var(--paper);
        overflow: hidden;
    }

    .page {
        margin: 0;
        padding: 20mm 18mm;
        box-shadow: none;
        border: none;
        width: 210mm;
        height: 297mm;
        page-break-inside: avoid;
        break-inside: avoid;
        /* Ensure print uses 1:1 scale */
        transform: none !important;
        position: absolute;
        left: 0;
        top: 0;
        box-sizing: border-box;
        overflow: hidden;
    }

    .no-print {
        display: none !important;
    }

    /* Fix vertical alignment in print */
    .draw-table th, .draw-table td {
        line-height: 0.8 !important;
        padding-top: 0.5em !important;
        padding-bottom: 0.3em !important;
    }

    /* Remove outer table border when printing to avoid double-border effect
       (inner cell borders remain visible) */
    .draw-table {
        border: none !important;
        border-collapse: collapse !important;
    }
}

/* Draw Module Column Layout */
.draw-row {
    display: grid;
    width: 100%;
    break-inside: avoid;
    border-bottom: 1px solid #eee;
    align-items: center;
}
.draw-header {
    font-weight: bold;
    background: #f9f9f9;
    border-bottom: 2px solid #333;
}
.draw-cell {
    text-align: center;
    padding: 2px 0;
    border-right: 1px solid #f0f0f0;
    overflow: hidden;
    white-space: nowrap;
}
.draw-cell:last-child {
    border-right: none;
}
.draw-cell.special {
    color: #d00;
    font-weight: bold;
}

/* Explicit column containers for draw module when distributing tables by height */
.draw-columns {
    display: flex;
    gap: 10px;
    width: 100%;
    height: 100%;
}
.draw-column {
    flex: 1 1 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
    height: 100%;
    overflow: hidden;
}
.draw-column table {
    margin-bottom: 6px;
}


/* Draw Module Column Layout */
.draw-row {
    display: grid;
    width: 100%;
    break-inside: avoid;
    border-bottom: 1px solid #eee;
    align-items: center;
}
.draw-header {
    font-weight: bold;
    background: #f9f9f9;
    border-bottom: 2px solid #333;
}
.draw-cell {
    text-align: center;
    padding: 2px 0;
    border-right: 1px solid #f0f0f0;
    overflow: hidden;
    white-space: nowrap;
}
.draw-cell:last-child {
    border-right: none;
}
.draw-cell.special {
    color: #d00;
    font-weight: bold;
}

