:root {
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --secondary: #475569;
    --secondary-hover: #334155;
    --success: #16a34a;
    --success-hover: #15803d;
    --danger: #dc2626;
    --danger-hover: #b91c1c;
    --bg-color: #f1f5f9;
    --sidebar-bg: #ffffff;
    --text-color: #1e293b;
    --border-color: #cbd5e1;
    --input-bg: #f8fafc;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    font-size: 14px;
}

.app-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: 350px;
    background-color: var(--sidebar-bg);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    box-shadow: 2px 0 5px rgba(0,0,0,0.05);
    z-index: 10;
}

.sidebar h2 {
    padding: 20px;
    font-size: 1.2rem;
    border-bottom: 1px solid var(--border-color);
    background-color: var(--primary);
    color: white;
}

.section {
    padding: 15px 20px;
    border-bottom: 1px solid var(--border-color);
}

.section h3 {
    font-size: 0.95rem;
    margin-bottom: 12px;
    color: var(--secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

/* Forms */
label {
    display: block;
    margin-bottom: 5px;
    font-weight: 500;
}

input[type="number"],
input[type="text"],
select {
    width: 100%;
    padding: 8px;
    margin-bottom: 10px;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    background-color: var(--input-bg);
    font-size: 14px;
}

input[type="checkbox"] {
    margin-right: 6px;
    vertical-align: middle;
}

.row {
    display: flex;
    gap: 10px;
}

.col {
    flex: 1;
}

/* Stats */
.stats-panel {
    background-color: #f8fafc;
    padding: 15px 20px;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.9rem;
}

.stat-item {
    display: flex;
    justify-content: space-between;
    margin-bottom: 4px;
}

.stat-item strong {
    color: var(--primary);
}

/* Buttons */
.actions {
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

button {
    padding: 10px 15px;
    border: none;
    border-radius: 4px;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s;
    color: white;
    font-size: 1rem;
}

button.primary { background-color: var(--primary); }
button.primary:hover { background-color: var(--primary-hover); }

button.success { background-color: var(--success); }
button.success:hover { background-color: var(--success-hover); }

button.secondary { background-color: var(--secondary); }
button.secondary:hover { background-color: var(--secondary-hover); }

button.danger { background-color: var(--danger); }
button.danger:hover { background-color: var(--danger-hover); }

.alert {
    padding: 15px;
    margin: 0 20px 20px 20px;
    border-radius: 4px;
    font-weight: 500;
}
.alert.error {
    background-color: #fee2e2;
    color: #991b1b;
    border: 1px solid #f87171;
}

/* Preview Area */
.preview-area {
    flex: 1;
    overflow: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.preview-controls {
    margin-bottom: 20px;
    background: white;
    padding: 10px 20px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    display: flex;
    align-items: center;
    gap: 10px;
}

.scale-wrapper {
    transform-origin: top center;
    transition: transform 0.2s;
}

/* PAGE CSS FOR PRINT AND PREVIEW */
.print-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
}

.page {
    background: white;
    box-shadow: 0 0 10px rgba(0,0,0,0.2);
    position: relative;
    overflow: hidden;
    /* Dimensions applied via inline JS based on A4/Letter */
}

/* THE GRID */
.saddle-grid {
    display: grid;
    /* Box sizing is critical here */
    box-sizing: border-box;
}

.saddle {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    box-sizing: border-box;
    overflow: hidden;
    background-color: white; /* Important for overlap logic if any */
}

/* Continuous Grid (0 spacing) - Trick to avoid double borders */
/* We apply borders to the top/left of the GRID container */
.grid-continuous {
    border-top-style: solid;
    border-left-style: solid;
}
/* And right/bottom to the CELLS */
.grid-continuous .saddle {
    border-right-style: solid;
    border-bottom-style: solid;
}

/* Non-continuous Grid (>0 spacing) */
/* Cells have full borders */
.grid-spaced .saddle {
    border-style: solid;
}

/* Saddle Internal Layout */
.saddle-content {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    height: 100%;
    width: 100%;
    padding: 2mm; /* Internal safe area */
    text-align: center;
}

.saddle-text-top, .saddle-text-bottom {
    font-size: 0.4em; /* Relative to main font size */
    opacity: 0.8;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 100%;
}

.saddle-code-wrapper {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
}

.saddle-code {
    line-height: 1;
    position: relative; /* For underline 6/9 */
}

/* Security 6/9 */
.protect-underline-char {
    text-decoration: underline;
    text-decoration-thickness: 10%;
    text-underline-offset: 5%;
}

.protect-underline-code::after {
    content: '';
    position: absolute;
    bottom: -0.15em;
    left: 0;
    width: 100%;
    height: 0.1em;
    background-color: currentColor;
}

/* RESPONSIVE UI */
@media (max-width: 768px) {
    .app-container {
        flex-direction: column;
    }
    .sidebar {
        width: 100%;
        height: 50vh;
        border-right: none;
        border-bottom: 2px solid var(--border-color);
    }
    .preview-area {
        height: 50vh;
        padding: 10px;
    }
    .row {
        flex-wrap: wrap;
    }
    .col {
        min-width: 45%;
    }
}

/* PRINT MEDIA QUERY */
@media print {
    /* Hide all UI elements */
    .no-print, .sidebar, .preview-controls {
        display: none !important;
    }

    body, html {
        background: transparent;
        margin: 0;
        padding: 0;
        width: 100%;
        height: 100%;
    }

    .app-container {
        height: auto;
        display: block;
        overflow: visible;
    }

    .preview-area {
        padding: 0;
        overflow: visible;
        display: block;
    }

    .print-container {
        gap: 0;
        display: block;
        transform: scale(1) !important; /* Reset visual zoom */
    }

    .page {
        box-shadow: none;
        margin: 0;
        page-break-after: always;
        /* Absolute precision */
        page-break-inside: avoid;
    }

    .page:last-child {
        page-break-after: avoid;
    }

    /* We use @page to remove browser default margins completely */
    @page {
        margin: 0;
    }

    /* Force background colors to print for some browsers, though we use borders mainly */
    * {
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }
}
