body { 
    margin: 0; 
    display: flex; 
    flex-direction: column; 
    min-height: 100vh; 
    font-family: Arial, sans-serif;
    overflow-x: hidden; /* Prevent horizontal scrolling */
    width: 100%;
}

* {
    box-sizing: border-box; /* Global box-sizing */
}

header {
    background-color: #333;
    color: #fff;
    padding: 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
}

footer {
    background-color: #f1f1f1;
    padding: 1rem;
    text-align: center;
    margin-top: auto;
    width: 100%;
}

.main-layout {
    display: flex;
    width: 100%;
}

.ad-column {
    width: 180px;
    flex-shrink: 0;
    background-color: #f8f9fa;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 20px;
    border-left: 1px solid #dee2e6;
    border-right: 1px solid #dee2e6;
}

.ad-column.left {
    order: 1;
}

main { 
    order: 2;
    flex: 1; 
    padding: 20px;
    min-width: 0; /* Allows flex item to shrink below content size */
}

.ad-column.right {
    order: 3;
}

.ad-row {
    width: 100%;
    background-color: #f8f9fa;
    display: flex;
    justify-content: center;
    padding: 20px 0;
    border-top: 1px solid #dee2e6;
    border-bottom: 1px solid #dee2e6;
}

.ad-placeholder {
    align-items: center;
    background-color: #e9ecef;
    display: flex;
    justify-content: center;
    border: 1px dashed #ced4da;
    position: relative; /* Ensure generated content is positioned correctly */
}

/* Show placeholder when empty OR when explicitly marked as empty by JS */
.ad-placeholder:empty::before,
.ad-placeholder.ad-empty::before {
    background-color: #6c757d;
    border-radius: 5px;
    color: #fff;
    content: attr(data-placeholder-label);
    font: 12px sans-serif;
    padding: 5px 10px;
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    white-space: nowrap;
}

/* Moved inline styles for grid-container and resizable-box here */
.grid-container {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    padding: 20px 50px;
    margin: 0;
}

.resizable-box {
    position: relative;
    display: flex;
    padding: 0;
    align-items: stretch;
    justify-content: stretch;
    overflow: hidden;
    /* Initial size for 3x3 grid (approx 33.33% minus gap) */
    width: calc(33.33% - 15px);
    aspect-ratio: 1; /* Force square aspect ratio */
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    font-size: clamp(0.8rem, 2vw, 1.5rem);
    color: #555;
    min-width: 100px;
    min-height: 100px;
    user-select: none; /* Prevent text selection during drag */
    overflow: hidden;
    box-sizing: border-box;
}

.box-content {
    width: 100%;
    height: 100%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* Handles */
.resizer {
    position: absolute;
    z-index: 10;
}

/* Right Handle */
.resizer.r {
    top: 0; bottom: 0; right: -5px;
    width: 10px;
    cursor: col-resize;
}

/* Bottom Handle */
.resizer.b {
    left: 0; right: 0; bottom: -5px;
    height: 10px;
    cursor: row-resize;
}

/* Bottom-Right Handle */
.resizer.br {
    right: -5px; bottom: -5px;
    width: 15px; height: 15px;
    cursor: se-resize;
}

/* Visual cue on hover/active */
.resizable-box:hover .resizer, .resizer.resizing {
    background-color: rgba(0, 123, 255, 0.1);
}
.resizer.br::after {
    content: '';
    position: absolute;
    bottom: 5px; right: 5px;
    width: 6px; height: 6px;
    border-right: 2px solid #999;
    border-bottom: 2px solid #999;
}

@media (max-width: 900px) {
    .main-layout {
        grid-template-columns: 1fr;
    }
    
    .ad-column {
        display: none;
    }
    
    .grid-container {
        padding: 20px;
    }
    
    .resizable-box {
        width: calc(50% - 15px); /* 2 columns on tablet */
    }
}

@media (max-width: 600px) {
    .resizable-box {
        width: 100%; /* 1 column on mobile */
    }
    
    .ad-row {
        display: none; /* Hide wide banner on mobile */
    }
}