:root {
    /* DEFAULT THEME FALLBACK (Light Mode) */
    /* These values are used if the app is loaded directly without the OS theme system */
    --bg-primary: #f4f6f8;
    --bg-secondary: #ffffff;
    --bg-tertiary: #e5e5e5;
    /* Added fallback */
    --panel-bg: #ffffff;
    --text-primary: #333333;
    --text-secondary: #666666;
    --border-color: #e0e0e0;
    --accent: #2196f3;
    --accent-light: #1976d2;
    --clear-btn-bg: #dc3545;
    --input-bg: #ffffff;
    --desktop-bg: #e5e5e5;
    --properties-preview-bg: #f0f0f0;
    --crop-color: #9c27b0;

    /* Internal Maps */
    --bg-app: var(--bg-primary);
    --bg-header: var(--bg-secondary);
    --bg-panel: var(--panel-bg);
    --text-main: var(--text-primary);
    --text-muted: var(--text-secondary);
    --border: var(--border-color);
    --primary: var(--accent);
    --primary-hover: var(--accent-light);
    --danger: var(--clear-btn-bg);
    --label-bg: #ffffff;
    /* Always White Paper */
    --workspace-bg: var(--bg-tertiary);
    /* Changed from desktop-bg to avoid Teal */
    --preview-bg: var(--properties-preview-bg);
}

/* Dark mode override removed - purely reliant on theme variables now */

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    outline: none;
    -webkit-tap-highlight-color: transparent;
}

html,
body {
    height: 100%;
    font-family: 'Segoe UI', sans-serif;
    overflow: hidden;
    background: var(--bg-app);
    color: var(--text-main);
}

body {
    display: flex;
    flex-direction: column;
}

/* 1. Header */
.app-header {
    height: 44px;
    background: var(--bg-header);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 10px;
    z-index: 200;
    flex-shrink: 0;
    position: relative;
}

.header-left,
.header-center,
.header-right {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* Duplicate root block removed */

/* Duplicate dark mode block removed */

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    outline: none;
    -webkit-tap-highlight-color: transparent;
}

html,
body {
    height: 100%;
    font-family: 'Segoe UI', sans-serif;
    overflow: hidden;
    background: var(--bg-app);
    color: var(--text-main);
}

body {
    display: flex;
    flex-direction: column;
}

/* 1. Header */
.app-header {
    height: 44px;
    background: var(--bg-header);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 10px;
    z-index: 200;
    flex-shrink: 0;
    position: relative;
}

.header-left,
.header-center,
.header-right {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* --- LOGO STYLE (UPDATED FOR IMAGE) --- */
.app-logo {
    height: 30px;
    /* reduced to match smaller header */
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 6px;
    padding: 0;
    cursor: pointer;
    overflow: visible;
    /* allow the logo image to slightly overflow so it can appear larger */
}

/* -------------------------------------- */

/* ADD this new rule below the .app-logo rule */
.app-logo img {
    height: 100%;
    width: auto;
    object-fit: contain;
    display: block;
    margin: 0;
    padding: 0;
    transform-origin: center center;
    transform: scale(1.18);
    /* slightly larger without increasing header height */
    transition: transform 120ms ease;
}

.app-logo:hover {
    background: rgba(128, 128, 128, 0.1);
}

.group-separator {
    width: 1px;
    height: 20px;
    background: var(--border);
    margin: 0 2px;
}

/* Buttons */
button {
    cursor: pointer;
    border: 1px solid transparent;
    background: transparent;
    color: var(--text-main);
    border-radius: 4px;
    padding: 6px 10px;
    transition: 0.2s;
    white-space: nowrap;
}

button:hover {
    background: rgba(128, 128, 128, 0.1);
    border-color: var(--border);
}

button:active {
    transform: translateY(1px);
}

button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.primary-btn {
    background: var(--primary);
    color: white;
    font-weight: 500;
}

.primary-btn:hover {
    background: var(--primary-hover);
    border-color: transparent;
}

/* Header button sizing: keep icon buttons compact (same as undo/redo) */
.icon-btn.mobile-big-btn {
    width: 30px;
    height: 30px;
    padding: 4px;
}

#action-theme,
#action-orientation {
    width: 30px;
    height: 30px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
}

#action-print {
    min-width: 100px;
    padding: 6px 10px;
    height: 30px;
}

/* Ensure icon buttons don't collapse on very small screens */
@media (max-width: 420px) {
    #action-print {
        min-width: 84px;
        padding: 6px 8px;
        font-size: 0.95em;
    }

    .icon-btn.mobile-big-btn,
    #action-theme,
    #action-orientation {
        width: 28px;
        height: 28px;
    }
}

/* Fixed Icon Button Styles */
.icon-btn {
    font-size: 1.2rem;
    padding: 4px 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.02);
    color: var(--text-main);
}

.icon-btn:hover {
    background: rgba(0, 0, 0, 0.05);
}

.icon-btn.active {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

.icon-btn svg {
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    width: 18px;
    height: 18px;
}

.icon-btn.small {
    font-size: 1rem;
    padding: 4px;
    width: 30px;
    height: 30px;
}

.danger-btn:hover {
    color: var(--danger);
    background: rgba(220, 53, 69, 0.1);
}

.compact-select {
    background: var(--bg-panel);
    color: var(--text-main);
    border: 1px solid var(--border);
    padding: 5px;
    border-radius: 4px;
    font-size: 0.9em;
}

/* Custom Dimension Inputs */
.custom-dims {
    display: flex;
    align-items: center;
    gap: 2px;
}

.dim-input {
    width: 45px;
    padding: 4px 2px;
    text-align: center;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text-main);
    font-size: 0.9em;
}

.dim-input:focus {
    border-color: var(--primary);
}

.dim-x {
    font-size: 0.8em;
    color: var(--text-muted);
}

/* Mail-merge variable pill styles */
.bc-var-group {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.bc-var-pill {
    display: inline-flex;
    align-items: center;
    padding: 4px 8px;
    border-radius: 8px;
    cursor: pointer;
    color: var(--text-main);
    font-size: 0.85em;
    border: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.03);
}

.bc-var-pill small {
    font-size: 0.7em;
    margin-left: 6px;
}

.bc-var-pill:hover {
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

.bc-var-pill-text {
    background: var(--primary);
    color: #fff;
    border-color: rgba(0, 0, 0, 0.06);
}

.bc-var-pill-qr {
    background: var(--accent);
    color: #fff;
    border-color: rgba(0, 0, 0, 0.06);
}

/* Dropdown */
.dropdown {
    position: relative;
}

.dropdown-content {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    min-width: 180px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
    z-index: 1000;
    border-radius: 4px;
    padding: 5px 0;
}

.dropdown-content.show {
    display: block;
}

.dropdown-content button {
    width: 100%;
    text-align: left;
    border-radius: 0;
    padding: 10px 15px;
    border: none;
}

.dropdown-content hr {
    border: 0;
    border-top: 1px solid var(--border);
    margin: 5px 0;
}

/* 2. Project Bar */
.project-bar {
    height: 38px;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: flex-end;
    padding: 0 10px;
    flex-shrink: 0;
    z-index: 90;
}

.project-tabs-list {
    display: flex;
    gap: 2px;
    overflow-x: auto;
    scrollbar-width: none;
    height: 100%;
    align-items: flex-end;
}

.project-tabs-list::-webkit-scrollbar {
    display: none;
}

.project-tab {
    padding: 6px 15px;
    background: var(--bg-app);
    border: 1px solid var(--border);
    border-bottom: none;
    border-radius: 6px 6px 0 0;
    font-size: 0.85em;
    cursor: pointer;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 100px;
    max-width: 200px;
    white-space: nowrap;
    height: 32px;
}

.project-tab.active {
    background: var(--workspace-bg);
    color: var(--primary);
    font-weight: 600;
    z-index: 2;
    border-top: 2px solid var(--primary);
    margin-bottom: -1px;
    padding-bottom: 7px;
}

.project-tab-close {
    font-size: 16px;
    line-height: 1;
    border-radius: 50%;
    padding: 0 4px;
    margin-left: auto;
    opacity: 0.6;
}

.project-tab-close:hover {
    background: rgba(220, 53, 69, 0.1);
    color: var(--danger);
    opacity: 1;
}

.add-project-btn {
    font-size: 1.2em;
    padding: 0 12px;
    color: var(--text-muted);
    margin-bottom: 4px;
}

/* 3. Main Layout */
.main-layout {
    flex: 1;
    display: flex;
    flex-direction: row;
    overflow: hidden;
    position: relative;
}

.workspace-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    position: relative;
}

/* --- LEFT SIDEBAR (LAYERS) --- */
.sidebar-left {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    background: var(--bg-panel);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 150;
    width: 240px;
    box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    /* Smooth width transition */
    transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Collapsed State */
.sidebar-left.collapsed {
    width: 50px;
    box-shadow: none;
    border-right: 1px solid transparent;
    background: var(--bg-panel);
}

.sidebar-left:not(.collapsed) {
    width: 200px;
    box-shadow: 2px 0 15px rgba(0, 0, 0, 0.15);
}

/* Header */
.layers-header {
    height: 45px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 10px;
    white-space: nowrap;
    background: var(--bg-panel);
}

.layers-title {
    font-weight: 600;
    font-size: 0.9em;
    text-transform: uppercase;
    transition: opacity 0.2s ease-in-out;
    opacity: 1;
}

.sidebar-left.collapsed .layers-title {
    display: none;
    opacity: 0;
}

.sidebar-left.collapsed .layers-header {
    padding: 0;
    justify-content: center;
    width: 50px;
    border: 1px solid var(--border);
    border-left: none;
    border-radius: 0 4px 4px 0;
}

/* List */
.layers-list {
    flex: 1;
    overflow-y: auto;
    padding: 5px;
    overflow-x: hidden;
    opacity: 1;
    background: var(--bg-panel);
}

.sidebar-left.collapsed .layers-list {
    padding: 5px 0;
    width: 50px;
}

/* Layer Items */
.layer-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 10px;
    border-radius: 4px;
    cursor: pointer;
    margin-bottom: 2px;
    font-size: 0.85em;
    position: relative;
    overflow: hidden;
    white-space: nowrap;
    border: 1px solid transparent;
}

.layer-item:hover {
    background: var(--bg-app);
    border-color: var(--border);
}

.layer-item.active {
    background: rgba(33, 150, 243, 0.1);
    color: var(--primary);
    border-left: 3px solid var(--primary);
    border-color: rgba(33, 150, 243, 0.2);
}

/* The Icon Wrapper */
.layer-info {
    display: flex;
    align-items: center;
    gap: 10px;
    flex: 1;
    overflow: hidden;
    width: 100%;
}

.layer-icon {
    width: 24px;
    min-width: 24px;
    text-align: center;
    font-size: 1.2em;
    display: flex;
    justify-content: center;
    align-items: center;
    color: var(--text-main);
}

.layer-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    opacity: 1;
    transition: opacity 0.2s;
}

/* Collapsed Item Logic */
.sidebar-left.collapsed .layer-item {
    justify-content: center;
    padding: 8px 0;
}

.sidebar-left.collapsed .layer-info {
    justify-content: center;
    width: 100%;
    gap: 0;
}

.sidebar-left.collapsed .layer-name {
    display: none !important;
    opacity: 0;
}

.sidebar-left.collapsed .layer-delete {
    display: none !important;
    opacity: 0;
}

/* Delete Button */
.layer-delete {
    width: 24px;
    height: 24px;
    border-radius: 4px;
    margin-left: auto;
    background: transparent;
    color: var(--text-muted);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2em;
    opacity: 0;
    transition: opacity 0.2s;
}

.layer-item:hover .layer-delete {
    opacity: 1;
}

.layer-delete:hover {
    background: var(--danger);
    color: white;
}

/* Right Sidebar */
.sidebar-right {
    width: 310px;
    background: var(--bg-panel);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 90;
    overflow: hidden;
}

.sidebar-header {
    padding: 0 10px;
    height: 45px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-app);
    display: flex;
    align-items: center;
    gap: 10px;
}

.sidebar-content {
    padding: 15px 0 15px 15px;
    display: flex;
    flex-direction: column;
    gap: 15px;
    min-height: 0;
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
}

/* Custom scrollbar for sidebar content */
.sidebar-content::-webkit-scrollbar {
    width: 10px;
}

.sidebar-content::-webkit-scrollbar-track {
    background: var(--bg-panel);
}

.sidebar-content::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 0;
    border-right: 2px solid var(--bg-panel);
}

.sidebar-content::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* Firefox scrollbar */
.sidebar-content {
    scrollbar-width: thin;
    scrollbar-color: var(--border) var(--bg-panel);
}

/* Content padding - NO right padding on container, scrollbar stays at edge */
#module-options-container {
    margin-right: 0;
    padding-right: 0;
}

/* Add right padding to the actual tool panels inside */
#module-options-container>div {
    padding-right: 15px;
}

.module-tabs {
    display: flex;
    gap: 5px;
    overflow-x: auto;
    scrollbar-width: none;
    align-items: center;
    height: 100%;
}

.module-tab {
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid transparent;
    border-radius: 4px;
    padding: 4px 12px;
    font-weight: 500;
    opacity: 0.6;
    cursor: pointer;
    min-width: 40px;
    height: 32px;
}

.module-tab:hover {
    background: rgba(0, 0, 0, 0.05);
    opacity: 0.8;
}

.module-tab.active {
    opacity: 1;
    color: var(--primary);
    background: rgba(33, 150, 243, 0.1);
    border-color: rgba(33, 150, 243, 0.3);
}

.module-tab svg {
    width: 20px;
    height: 20px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
}

.module-tab span {
    font-size: 0.85em;
    margin-left: 5px;
    display: none;
}

.tool-ribbon {
    display: none;
}

.workspace {
    flex: 1;
    background: var(--workspace-bg);
    overflow: hidden;
    position: relative;
    cursor: grab;
    touch-action: none;
}

.workspace:active {
    cursor: grabbing;
}

.label-container {
    position: absolute;
    background: var(--label-bg);
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
    transform-origin: 0 0;
    overflow: visible !important;
}

/* Poster Guides */
.poster-guide {
    position: absolute;
    background-color: var(--primary);
    opacity: 0.5;
    pointer-events: none;
    z-index: 500;
}

.poster-guide.vertical {
    top: 0;
    bottom: 0;
    width: 0;
    border-left: 1px dashed white;
    border-right: 1px dashed var(--primary);
}

.poster-guide.horizontal {
    left: 0;
    right: 0;
    height: 0;
    border-top: 1px dashed white;
    border-bottom: 1px dashed var(--primary);
}

/* --- 4. Canvas Items --- */

/* Text Container */
.text-container {
    position: absolute !important;
    cursor: move !important;
    min-width: 20px !important;
    min-height: 20px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    border: 1px dashed transparent;
    z-index: 5;
}

.text-container:hover {
    cursor: move;
    border-color: rgba(33, 150, 243, 0.3);
}

.text-container.active {
    border: 2px solid var(--primary) !important;
    z-index: 1000;
}

.text-content {
    width: 100%;
    height: 100%;
    outline: none;
    text-align: center;
    font-size: 18px;
    white-space: pre-wrap;
    overflow: visible;
    color: black;
    line-height: 1.2;
    word-wrap: break-word;
    cursor: move;
}

.text-content[contenteditable="true"] {
    cursor: text !important;
}

/* Image Container */
.image-container {
    position: absolute !important;
    cursor: move !important;
    min-width: 20px !important;
    min-height: 20px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    border: 1px dashed transparent;
    z-index: 5;
}

.image-container:hover {
    border-color: rgba(33, 150, 243, 0.3);
}

.image-container.active {
    border: 2px solid var(--primary) !important;
    z-index: 1000;
}

/* Hide standard resizers when cropping - IMPORTANT: Uses !important to override JS styles */
.image-container.cropping .resizer {
    display: none !important;
}

.image-container.cropping {
    cursor: default !important;
    border: 2px solid rgba(0, 0, 0, 0.1) !important;
}


/* --- BARCODE CONTAINER --- */
.barcode-container {
    position: absolute !important;
    cursor: move !important;
    min-width: 20px !important;
    min-height: 20px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    border: 1px dashed transparent;
    z-index: 5;
}

.barcode-container:hover {
    border-color: rgba(33, 150, 243, 0.3);
}

.barcode-container.active {
    border: 2px solid var(--primary) !important;
    z-index: 1000;
}

/* Sidebar Preview Box */
.sidebar-preview-box {
    background-color: var(--preview-bg);
    border: 1px solid var(--border);
    border-radius: 4px;
    height: 120px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    padding: 10px;
}

/* Compact Sidebar Inputs */
.compact-group {
    margin-bottom: 15px;
    padding-bottom: 15px;
    border-bottom: 1px solid var(--border);
}

.compact-group:last-child {
    border-bottom: none;
}

.compact-input {
    padding: 4px;
    font-size: 0.9em;
    flex: 1;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text-main);
}

/* Transparent Button (Small gray box with X) */
.trans-btn {
    width: 32px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #e0e0e0;
    border: 1px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
    color: #666;
}

.trans-btn:hover {
    background: #d0d0d0;
    color: #333;
}

.trans-btn svg {
    width: 16px;
    height: 16px;
    stroke: currentColor;
    stroke-width: 2;
    fill: none;
}

/* --- DRAWING --- */
/* FIX: Drawing border transparent by default, dashed on hover, solid blue on active */
.drawing-container {
    position: absolute !important;
    cursor: default !important;
    border: 2px solid transparent;
    z-index: 5;
}

.drawing-container:hover {
    border: 2px dashed rgba(33, 150, 243, 0.5);
}

.drawing-container.active {
    border: 2px solid var(--primary) !important;
    z-index: 1000;
}

.drawing-canvas-el {
    width: 100%;
    height: 100%;
    display: block;
    cursor: crosshair;
}

/* Drawing Move Handle */
.draw-move-handle {
    position: absolute;
    top: -30px;
    left: 50%;
    transform: translateX(-50%);
    width: 24px;
    height: 24px;
    background: var(--primary);
    color: white;
    border-radius: 50%;
    display: none;
    align-items: center;
    justify-content: center;
    cursor: move;
    z-index: 2000;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.drawing-container.active .draw-move-handle {
    display: flex;
}

.draw-move-handle svg {
    width: 16px;
    height: 16px;
    stroke: white;
}


/* Crop Box */
.crop-box {
    position: absolute;
    border: 2px dashed var(--crop-color);
    /* Purple dashed line */
    box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.5);
    /* Dim everything outside */
    cursor: move;
    z-index: 2000;
    box-sizing: border-box;
}

/* Crop Resizers (Purple) */
.crop-resizer {
    position: absolute;
    width: 14px;
    height: 14px;
    background: var(--crop-color);
    border: 2px solid white;
    border-radius: 50%;
    z-index: 2001;
}

.crop-resizer.nw {
    top: -7px;
    left: -7px;
    cursor: nw-resize;
}

.crop-resizer.n {
    top: -7px;
    left: 50%;
    transform: translateX(-50%);
    cursor: n-resize;
}

.crop-resizer.ne {
    top: -7px;
    right: -7px;
    cursor: ne-resize;
}

.crop-resizer.e {
    right: -7px;
    top: 50%;
    transform: translateY(-50%);
    cursor: e-resize;
}

.crop-resizer.se {
    bottom: -7px;
    right: -7px;
    cursor: se-resize;
}

.crop-resizer.s {
    bottom: -7px;
    left: 50%;
    transform: translateX(-50%);
    cursor: s-resize;
}

.crop-resizer.sw {
    bottom: -7px;
    left: -7px;
    cursor: sw-resize;
}

.crop-resizer.w {
    left: -7px;
    top: 50%;
    transform: translateY(-50%);
    cursor: w-resize;
}

/* Standard Resizers (Blue) */
.resizer {
    position: absolute !important;
    width: calc(12px / var(--zoom, 1)) !important;
    height: calc(12px / var(--zoom, 1)) !important;
    background-color: var(--primary) !important;
    border: calc(2px / var(--zoom, 1)) solid white !important;
    border-radius: 50% !important;
    z-index: 1010 !important;
    display: none !important;
}

.text-container.active .resizer,
.image-container.active .resizer,
.drawing-container.active .resizer,
.barcode-container.active .resizer {
    display: block !important;
}

.resizer.nw {
    top: calc(-6px / var(--zoom, 1));
    left: calc(-6px / var(--zoom, 1));
    cursor: nw-resize;
}

.resizer.n {
    top: calc(-6px / var(--zoom, 1));
    left: 50%;
    transform: translateX(-50%);
    cursor: n-resize;
}

.resizer.ne {
    top: calc(-6px / var(--zoom, 1));
    right: calc(-6px / var(--zoom, 1));
    cursor: ne-resize;
}

.resizer.e {
    right: calc(-6px / var(--zoom, 1));
    top: 50%;
    transform: translateY(-50%);
    cursor: e-resize;
}

.resizer.se {
    bottom: calc(-6px / var(--zoom, 1));
    right: calc(-6px / var(--zoom, 1));
    cursor: se-resize;
}

.resizer.s {
    bottom: calc(-6px / var(--zoom, 1));
    left: 50%;
    transform: translateX(-50%);
    cursor: s-resize;
}

.resizer.sw {
    bottom: calc(-6px / var(--zoom, 1));
    left: calc(-6px / var(--zoom, 1));
    cursor: sw-resize;
}

.resizer.w {
    left: calc(-6px / var(--zoom, 1));
    top: 50%;
    transform: translateY(-50%);
    cursor: w-resize;
}

/* --- 5. Tool UI Properties --- */
.tool-group {
    margin-bottom: 15px;
    padding-bottom: 15px;
    border-bottom: 1px solid var(--border);
}

.tool-group:last-child {
    border: none;
}

.tool-label {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 8px;
    display: block;
}

.tool-row {
    display: flex;
    gap: 5px;
    flex-wrap: wrap;
    align-items: center;
}

.color-picker-wrapper {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    margin-bottom: 5px;
}

.color-picker-wrapper label {
    flex: 1;
    font-size: 0.9em;
}

input[type="color"] {
    border: none;
    width: 40px;
    height: 30px;
    cursor: pointer;
    background: transparent;
}

/* Custom Tool Layouts */
/* NOTE: The rotation rows have inline CSS to remove bottom border/padding for minimalist look */
.compact-row {
    display: flex;
    align-items: center;
    gap: 5px;
    width: 100%;
    padding-bottom: 10px;
    margin-bottom: 10px;
    border-bottom: 1px solid var(--border);
}

.compact-row:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
}

/* Text Tool Rows (Mobile Optimized) */
.row-add #tt-add {
    width: 100%;
    padding: 10px;
}

.row-font-colors {
    justify-content: space-between;
}

.row-font-colors #tt-font {
    flex: 2;
    padding: 8px;
}

.row-formatting {
    justify-content: space-between;
}

.btn-group {
    display: flex;
    gap: 2px;
}

.formatting-bar .separator,
.row-font-colors .separator,
.row-formatting .separator {
    width: 1px;
    height: 24px;
    background: var(--border);
    margin: 0 4px;
}

.color-wrapper {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 34px;
    height: 34px;
    background: rgba(0, 0, 0, 0.05);
    border-radius: 4px;
    border: 1px solid var(--border);
}

/* --- 6. Mobile Media Query --- */
@media (max-width: 768px) {
    .app-header {
        padding: 0 4px;
        gap: 2px;
    }

    .header-left,
    .header-center,
    .header-right {
        gap: 2px;
    }

    /* Narrower buttons */
    button,
    .icon-btn {
        padding: 4px 6px;
        font-size: 0.85rem;
    }

    .primary-btn {
        padding: 4px 8px;
        font-size: 0.85rem;
    }

    /* BIG mobile buttons for undo/redo */
    .mobile-big-btn {
        padding: 8px 12px;
        min-width: 44px;
        /* Standard touch target size */
        background: rgba(0, 0, 0, 0.05);
    }

    /* Prevent select from pushing content off screen */
    .compact-select {
        max-width: 85px;
        font-size: 0.85rem;
        padding: 2px;
    }

    /* Save space */
    .group-separator {
        display: none;
    }

    .app-logo {
        display: flex !important;
        width: 28px;
        height: 28px;
        font-size: 0.8em;
    }

    .mobile-only {
        display: block !important;
    }

    .main-layout {
        flex-direction: column;
    }

    /* Mobile Left Sidebar */
    .sidebar-left {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        transform: none !important;
        width: 50px;
        z-index: 150;
        border-right: 1px solid var(--border);
    }

    .sidebar-left.collapsed {
        width: 50px;
    }

    .sidebar-left:not(.collapsed) {
        width: 200px;
        box-shadow: 2px 0 10px rgba(0, 0, 0, 0.2);
    }

    .sidebar-left.open {
        transform: none;
        width: auto;
    }

    .sidebar-left.open .layers-title {
        display: none;
    }

    /* Mobile Bottom Tools */
    .sidebar-right {
        width: 100%;
        height: auto;
        /* LIMIT HEIGHT: This ensures it never covers the whole screen */
        max-height: 40vh;
        display: flex;
        /* CRITICAL: Allows child to scroll properly */
        flex-direction: column;
        /* CRITICAL: Stack header and content vertical */
        border-left: none;
        border-top: 1px solid var(--border);
        order: 2;
        box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
        padding-left: 50px;
    }

    /* Crucial fix to allow vertical scrolling within the sidebar on mobile */
    .sidebar-content {
        padding: 10px 0 10px 10px;
        display: flex;
        flex-direction: column;
        gap: 5px;
        min-height: 0;
        flex: 1;
        /* Allow content to grow/shrink based on space */
        overflow-y: auto;
        /* Enable vertical scrolling */
        -webkit-overflow-scrolling: touch;
        /* Smooth scrolling for iOS */
    }

    /* NO right padding on container - scrollbar stays at edge */
    #module-options-container {
        padding-right: 0;
    }

    /* But add padding to the actual tool panels inside */
    #module-options-container>div {
        padding-right: 10px;
    }

    /* --- MOBILE TOOLBAR OPTIMIZATION --- */

    /* Increase icon button size for better touch target */
    .row-formatting .icon-btn {
        width: 40px;
        height: 38px;
        padding: 5px;
    }

    /* Remove separators in mobile formatting bar */
    .row-formatting .separator {
        display: none;
    }

    /* Compact font and color bar */
    .row-font-colors {
        padding-bottom: 5px;
        margin-bottom: 5px;
    }

    .row-font-colors .separator {
        display: none;
    }

    /* Make color wrappers larger touch targets */
    .row-font-colors .color-wrapper {
        width: 40px;
        height: 38px;
    }

    /* Ensure the rotation row maintains one line structure */
    .rotation-row {
        /* Remove the padding/margin/border usually added by compact-row to the rotation group */
        padding-bottom: 0 !important;
        margin-bottom: 0 !important;
        border-bottom: none !important;
    }


    /* --- RESIZER FIXES --- */
    .resizer {
        width: calc(20px / var(--zoom, 1)) !important;
        height: calc(20px / var(--zoom, 1)) !important;
    }

    .crop-resizer {
        width: 20px;
        height: 20px;
    }
}

/* --- 7. PRINT HIDE (FIXED) --- */
@media print {

    /* Hide drawing and UI elements */
    .draw-move-handle {
        display: none !important;
    }

    .resizer {
        display: none !important;
    }

    .crop-box {
        display: none !important;
    }

    .poster-guide {
        display: none !important;
    }

    .zoom-handles-wrapper {
        display: none !important;
    }

    .layer-delete {
        display: none !important;
    }

    /* FIX: Ensure ALL content containers are border and shadow free */
    .text-container,
    .image-container,
    .drawing-container,
    .barcode-container,
    .text-container.active,
    .image-container.active,
    .drawing-container.active,
    .barcode-container.active {
        border: none !important;
        box-shadow: none !important;
    }

    /* Hide business card UI overlays - zones, badges, banner */
    .bizcard-zone,
    .bizcard-master-badge,
    .bizcard-number,
    .bizcard-edit-banner {
        display: none !important;
    }

    /* Cut guides - keep visible for printing but make them lighter */
    .bizcard-guide {
        opacity: 0.5;
    }
}

/* ========================================
   8. BUSINESS CARD / DUPLICATION FEATURE
   ======================================== */

/* Properties Panel - Prevent Overflow */
.bizcard-properties {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
    box-sizing: border-box;
}

.bizcard-properties .tool-group {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

/* Mode Toggle Buttons */
.bizcard-mode-toggle {
    display: flex;
    gap: 4px;
    background: var(--bg-app);
    padding: 3px;
    border-radius: 6px;
    width: 100%;
}

.bizcard-mode-toggle .mode-btn {
    flex: 1;
    padding: 6px 8px;
    border: none;
    border-radius: 4px;
    background: transparent;
    color: var(--text-muted);
    font-weight: 500;
    font-size: 0.85em;
    cursor: pointer;
    transition: all 0.2s;
}

.bizcard-mode-toggle .mode-btn:hover {
    background: rgba(0, 0, 0, 0.05);
}

.bizcard-mode-toggle .mode-btn.active {
    background: var(--primary);
    color: white;
}

/* Size and Gap Rows */
.bc-size-row {
    display: flex;
    align-items: center;
    gap: 5px;
    width: 100%;
}

.bc-size-row .dim-input {
    flex: 1;
    min-width: 0;
    width: 50px;
}

.bc-size-row span {
    color: var(--text-muted);
    font-size: 0.9em;
}

/* Margin Row */
.bc-margin-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 5px;
    width: 100%;
}

.bc-margin-item {
    display: flex;
    align-items: center;
    gap: 3px;
}

.bc-margin-item span {
    font-size: 0.75em;
    color: var(--text-muted);
    width: 14px;
    text-align: center;
}

.bc-margin-item .dim-input {
    flex: 1;
    min-width: 0;
    width: 40px;
}

/* Checkbox Style */
.bc-checkbox {
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    font-size: 0.85em;
    margin-bottom: 5px;
}

.bc-checkbox input {
    margin: 0;
}

/* Stats Box */
.bc-stats-box {
    background: var(--bg-app);
    padding: 10px;
    border-radius: 4px;
    text-align: center;
}

.bc-count {
    font-weight: bold;
    color: var(--primary);
    font-size: 1.1em;
}

.bc-layout-info {
    color: var(--text-muted);
    font-size: 0.85em;
    margin-top: 2px;
}

/* Edit Button */
.bc-edit-btn {
    width: 100%;
    padding: 10px;
    font-size: 0.95em;
}

/* Apply Section */
.bc-apply-btn {
    width: 100%;
    background: #4caf50 !important;
    padding: 10px;
}

.bc-apply-btn:hover {
    background: #43a047 !important;
}

.bc-cancel-btn {
    width: 100%;
    margin-top: 5px;
}

/* Edit Mode Banner */
.bizcard-edit-banner {
    background: linear-gradient(135deg, #4caf50, #2e7d32);
    color: white;
    padding: 8px 15px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 15px;
    font-size: 0.9em;
    z-index: 200;
    flex-shrink: 0;
    flex-wrap: wrap;
}

.bizcard-edit-banner .banner-title {
    font-weight: bold;
}

.bizcard-edit-banner .banner-buttons {
    display: flex;
    gap: 8px;
}

.bizcard-edit-banner .banner-btn {
    padding: 6px 14px;
    border: none;
    border-radius: 4px;
    font-weight: 600;
    cursor: pointer;
    font-size: 0.9em;
    transition: all 0.2s;
}

.bizcard-edit-banner .banner-apply {
    background: white;
    color: #2e7d32;
}

.bizcard-edit-banner .banner-apply:hover {
    background: #e8f5e9;
}

.bizcard-edit-banner .banner-cancel {
    background: rgba(255, 255, 255, 0.2);
    color: white;
}

.bizcard-edit-banner .banner-cancel:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* Edit Mode Canvas Indicator */
.label-container.bizcard-edit-mode {
    box-shadow: 0 0 0 4px #4caf50, 0 0 20px rgba(76, 175, 80, 0.4) !important;
}

/* Business Card Zones on Canvas */
/* Business Card Zones on Canvas */
.bizcard-zone {
    position: absolute;
    border: 2px dashed var(--primary);
    background: rgba(33, 150, 243, 0.05);
    z-index: 20;
    /* High Layer: Sits above text items */
    pointer-events: none;
    /* CRITICAL: Allows clicks to pass through to text underneath */
    transition: all 0.15s ease;
    box-sizing: border-box;
}



.bizcard-zone:hover {
    background: rgba(33, 150, 243, 0.12);
    border-color: var(--primary-hover);
}

.bizcard-zone.master {
    border-color: #4caf50;
    border-width: 3px;
    background: rgba(76, 175, 80, 0.08);
    cursor: pointer;
}

.bizcard-zone.master:hover {
    background: rgba(76, 175, 80, 0.15);
    border-color: #2e7d32;
}

.bizcard-zone.rotated {
    border-style: dotted;
}

/* Update the existing badge rule */
.bizcard-master-badge {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #4caf50;
    color: white;
    padding: 6px 12px;
    border-radius: 4px;
    font-size: 12px;
    font-weight: bold;
    white-space: nowrap;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    z-index: 25;
    pointer-events: auto;
    cursor: pointer;
    touch-action: manipulation;
    /* Adds better touch response */
}

/* Zone Number Badge */
.bizcard-number {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 18px;
    height: 18px;
    background: var(--primary);
    color: white;
    border-radius: 50%;
    font-size: 9px;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    opacity: 0.7;
}

/* Cut Guides */
.bizcard-guide {
    position: absolute;
    pointer-events: none;
    z-index: 99;
    opacity: 0.4;
}

.bizcard-guide.vertical {
    width: 0;
    top: 0;
    border-left: 1px dashed #666;
}

.bizcard-guide.horizontal {
    height: 0;
    left: 0;
    border-top: 1px dashed #666;
}

/* Cloned Items Styling */
.text-container.clone,
.image-container.clone,
.drawing-container.clone,
.barcode-container.clone {
    pointer-events: none;
    opacity: 0.9;
}

.text-container.clone .resizer,
.image-container.clone .resizer,
.drawing-container.clone .resizer,
.barcode-container.clone .resizer {
    display: none !important;
}

/* Dark Theme Adjustments */
[data-theme="dark"] .bizcard-zone {
    background: rgba(33, 150, 243, 0.08);
}

[data-theme="dark"] .bizcard-zone:hover {
    background: rgba(33, 150, 243, 0.15);
}

[data-theme="dark"] .bizcard-zone.master {
    background: rgba(76, 175, 80, 0.1);
}

[data-theme="dark"] .bizcard-guide {
    border-color: #888;
}

[data-theme="dark"] .bizcard-mode-toggle {
    background: var(--bg-panel);
}

/* ===== NEW: Real-Time Duplication System ===== */

/* Active card mode indicator on label */
.label-container.bizcard-mode-active {
    /* Subtle indicator that card mode is on */
}

/* Items that overflow outside the master cell bounds */
.bizcard-overflow {
    /* Visual indicator - subtle gradient fade */
}

/* Items completely outside master cell - lower opacity */
.bizcard-outside {
    opacity: 0.35 !important;
}

/* Elements partially outside get a dashed outline */
.bizcard-overflow::after {
    content: '';
    position: absolute;
    top: -2px;
    left: -2px;
    right: -2px;
    bottom: -2px;
    border: 2px dashed rgba(255, 152, 0, 0.6);
    border-radius: 4px;
    pointer-events: none;
    z-index: 100;
}

/* Clone zones (clipping containers for duplicates) */
.bizcard-clone-zone {
    position: absolute;
    overflow: hidden;
    pointer-events: none;
}

/* Duplicates container */
.bizcard-duplicates-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 10;
    /* Below master items but above background */
}

/* Clone elements within duplicate zones */
.bizcard-clone {
    pointer-events: none !important;
    opacity: 1;
}

.bizcard-clone.active {
    /* Remove active styling from clones */
    border: none !important;
    box-shadow: none !important;
}

.bizcard-clone .resizer,
.bizcard-clone .rotator-handle,
.bizcard-clone .draw-move-handle {
    display: none !important;
}

/* Ensure master items stay above clones */
.label-container.bizcard-mode-active .text-container:not(.bizcard-clone),
.label-container.bizcard-mode-active .image-container:not(.bizcard-clone),
.label-container.bizcard-mode-active .drawing-container:not(.bizcard-clone),
.label-container.bizcard-mode-active .barcode-container:not(.bizcard-clone) {
    z-index: 25;
}

/* Mobile Responsiveness */
@media (max-width: 768px) {
    .bizcard-master-badge {
        font-size: 9px;
        padding: 3px 6px;
    }

    .bizcard-number {
        width: 16px;
        height: 16px;
        font-size: 8px;
    }

    .bizcard-properties .compact-row {
        flex-wrap: wrap;
    }

    .bizcard-properties .dim-input {
        width: 50px;
    }

    /* Make preset/guides dropdowns wider on mobile */
    .bc-preset-guides-row {
        gap: 8px;
    }

    .bc-preset-guides-row .compact-select {
        min-width: 0;
        flex: 1;
        font-size: 12px;
        padding: 6px 4px;
    }
}

/* Preset and Cut Guides row layout */
.bc-preset-guides-row {
    display: flex;
    gap: 10px;
    width: 100%;
}

.bc-preset-col {
    flex: 1.2;
    display: flex;
    flex-direction: column;
    gap: 5px;
    min-width: 0;
}

.bc-guides-col {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 5px;
    min-width: 0;
}

.bc-preset-guides-row .compact-select {
    width: 100%;
    min-width: 0;
}

/* Margin header with unified +/- controls */
.bc-margin-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.bc-margin-header .tool-label {
    margin-bottom: 0;
}

.bc-margin-all-controls {
    display: flex;
    gap: 4px;
}

.bc-margin-all-controls .icon-btn.small {
    width: 26px;
    height: 26px;
    padding: 0;
    font-size: 16px;
    font-weight: bold;
    line-height: 1;
}

/* Mail merge toggle */
.bc-toggle-small {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.8em;
    cursor: pointer;
}

.bc-toggle-small input {
    margin: 0;
}

/* Mail merge panel */
#bc-merge-panel {
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid var(--border);
}

#bc-merge-var-list code:hover {
    background: var(--primary-hover) !important;
}