:root
{
    --bg-color: #1e1e1e;
    --panel-color: #252526;
    --text-color: #cccccc;
    --accent-color: #007acc;
    --border-color: #3e3e42;
    --hover-color: #2d2d30;
    --menu-bg: #2d2d30;
}

*
{
    box-sizing: border-box;
}

body
{
    margin: 0;
    padding: 0;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* --- Left Sidebar --- */
.sidebar
{
    width: 340px;
    background-color: var(--panel-color);
    border-right: 1px solid var(--border-color);
    padding: 15px;
    display: flex;
    flex-direction: column;
    gap: 15px;
    flex-shrink: 0;
    overflow-y: auto;
    z-index: 10;
}

h2
{
    margin: 0;
    font-size: 1.1rem;
    color: #fff;
}

/* Tabs for Sidebar */
.sidebar-section
{
    display: none;
    flex-direction: column;
    gap: 15px;
}

    .sidebar-section.active
    {
        display: flex;
    }

.btn-group
{
    display: flex;
    gap: 10px;
    margin-bottom: 10px;
}

.btn-tool
{
    flex: 1;
    padding: 8px;
    background: #333;
    border: 1px solid var(--border-color);
    color: white;
    cursor: pointer;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
}

    .btn-tool:hover
    {
        background: #444;
    }

.params-grid
{
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.control-group
{
    display: flex;
    flex-direction: column;
    gap: 3px;
    width: 100%;
}

    .control-group.full
    {
        grid-column: span 2;
    }

label
{
    font-size: 0.7rem;
    color: #aaa;
    white-space: nowrap;
    display: flex;
    justify-content: space-between;
}

input[type="number"], input[type="text"], input[type="color"], select
{
    background: #3c3c3c;
    border: 1px solid var(--border-color);
    color: white;
    padding: 6px 8px;
    border-radius: 4px;
    font-size: 0.85rem;
    width: 100%;
}

    input[type="number"]:focus, input[type="text"]:focus
    {
        outline: none;
        border-color: var(--accent-color);
    }

input[type="color"]
{
    padding: 0;
    height: 30px;
    cursor: pointer;
}

input[type=range]
{
    -webkit-appearance: none;
    width: 100%;
    background: transparent;
}

    input[type=range]::-webkit-slider-thumb
    {
        -webkit-appearance: none;
        height: 14px;
        width: 14px;
        border-radius: 50%;
        background: var(--accent-color);
        cursor: pointer;
        margin-top: -5px;
    }

    input[type=range]::-webkit-slider-runnable-track
    {
        width: 100%;
        height: 4px;
        background: #444;
        border-radius: 2px;
    }

h3
{
    font-size: 0.85rem;
    color: #999;
    margin: 2px 0 0 0;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-bottom: 1px solid #444;
    padding-bottom: 5px;
}

/* --- Drop Zones --- */
.zones-container
{
    display: flex;
    flex-direction: column;
    gap: 10px;
    flex-grow: 1;
}

.drop-zone
{
    background: var(--hover-color);
    border: 2px dashed var(--border-color);
    border-radius: 6px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    padding: 5px;
    overflow: hidden;
}

    .drop-zone:hover
    {
        border-color: #666;
        background-color: #333;
    }

    .drop-zone.active
    {
        border-color: var(--accent-color);
        background-color: #2d2d30;
    }

    .drop-zone.has-image
    {
        border-style: solid;
        border-color: #4caf50;
        background-color: #1e2e1e;
    }

    .drop-zone.is-template
    {
        border-color: #555;
        background-color: #222;
    }

    .drop-zone p
    {
        margin: 0;
        font-size: 0.9rem;
        font-weight: 600;
        pointer-events: none;
    }

    .drop-zone .info-block
    {
        display: flex;
        flex-direction: column;
        gap: 2px;
        margin-top: 4px;
        pointer-events: none;
    }

    .drop-zone .rect-info
    {
        font-family: monospace;
        font-size: 0.75rem;
        color: #aaa;
        background: rgba(0,0,0,0.2);
        padding: 2px 4px;
        border-radius: 3px;
    }

    .drop-zone .src-info
    {
        font-size: 0.7rem;
        color: #666;
    }

.zone-large
{
    height: 100px;
}

.zone-medium
{
    height: 80px;
}

.offset-control
{
    width: 100%;
    padding: 0 10px 5px 10px;
    margin-top: -5px;
}

.offset-label
{
    display: flex;
    justify-content: space-between;
    font-size: 0.7rem;
    color: #888;
    margin-bottom: 2px;
}

.slider-row
{
    display: flex;
    gap: 8px;
}

.slider-row > div
{
    flex: 1;
    min-width: 0;
}

/* --- Right Main Area --- */
.main-area
{
    flex-grow: 1;
    background-color: #111;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 8px;
    overflow: hidden; /* prevent scrollbars; we aspect-fit via JS */
    position: relative;
}

/* Canvas Container & Overlays */
#canvas-wrapper
{
    position: relative;
    box-shadow: 0 0 30px rgba(0,0,0,0.7);
    /* Background checkerboard */
    background-image: linear-gradient(45deg, #222 25%, transparent 25%), linear-gradient(-45deg, #222 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #222 75%), linear-gradient(-45deg, transparent 75%, #222 75%);
    background-size: 20px 20px;
    background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
    max-width: 100%;
    max-height: 100%;
}

canvas
{
    display: block;
    width: 100%;
    height: 100%; /* fill wrapper, we control wrapper size */
}

/* Overlay Items (HTML) */
.overlay-item
{
    position: absolute;
    user-select: none;
    cursor: grab;
    border: 1px solid transparent;
}

    .overlay-item:hover
    {
        border: 1px dashed rgba(255,255,255,0.3);
    }

    .overlay-item.selected
    {
        border: 1px solid var(--accent-color);
        z-index: 100;
    }

.overlay-content
{
    white-space: normal;
    line-height: 1;
    transform-origin: center center;
    width: 100%;
    height: 100%;
    overflow: hidden;
    word-break: break-all;
    box-sizing: border-box;
}

/* Resize handles for text bounds */
.resize-handle
{
    position: absolute;
    background: transparent;
}

.resize-left,
.resize-right
{
    top: 15%;
    bottom: 15%;
    width: 8px;
    cursor: ew-resize;
}

.resize-left
{
    left: -4px;
}

.resize-right
{
    right: -4px;
}

.resize-top,
.resize-bottom
{
    left: 15%;
    right: 15%;
    height: 8px;
    cursor: ns-resize;
}

.resize-top
{
    top: -4px;
}

.resize-bottom
{
    bottom: -4px;
}

/* Three Dots Menu Button */
.menu-trigger
{
    position: absolute;
    top: -12px;
    right: -12px;
    width: 24px;
    height: 24px;
    background: var(--accent-color);
    border-radius: 50%;
    display: none;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(0,0,0,0.5);
    z-index: 101;
}

.overlay-item.selected .menu-trigger
{
    display: flex;
}

.menu-trigger svg
{
    width: 16px;
    height: 16px;
    fill: white;
}

/* Popup Menu */
.popup-menu
{
    position: absolute;
    top: 15px;
    right: -100px;
    width: 220px;
    background: var(--menu-bg);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 10px;
    display: none;
    flex-direction: column;
    gap: 8px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.5);
    z-index: 200;
}

    .popup-menu.show
    {
        display: flex;
    }

.popup-row
{
    display: flex;
    align-items: center;
    gap: 8px;
}

    .popup-row label
    {
        width: 50px;
    }

.icon-btn-group
{
    display: flex;
    gap: 4px;
    background: #222;
    padding: 2px;
    border-radius: 4px;
}

.icon-btn
{
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    border-radius: 3px;
}

    .icon-btn:hover
    {
        background: #444;
    }

    .icon-btn.active
    {
        background: var(--accent-color);
    }

    .icon-btn svg
    {
        width: 14px;
        height: 14px;
        fill: #ccc;
    }

    .icon-btn.active svg
    {
        fill: white;
    }

button.btn-download
{
    margin-top: auto;
    padding: 12px;
    background-color: var(--accent-color);
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-weight: bold;
    font-size: 0.95rem;
    width: 100%;
    text-align: center;
}

    button.btn-download:hover
    {
        background-color: #0063a5;
    }

button.btn-back
{
    background: transparent;
    border: 1px solid #666;
    color: #ccc;
    padding: 4px 8px;
    font-size: 0.8rem;
    cursor: pointer;
    width: 100%;
    margin-bottom: 10px;
}

.loader
{
    border: 4px solid #f3f3f3;
    border-top: 4px solid var(--accent-color);
    border-radius: 50%;
    width: 20px;
    height: 20px;
    animation: spin 1s linear infinite;
    display: none;
    margin-left: 10px;
}

@keyframes spin
{
    0%
    {
        transform: rotate(0deg);
    }

    100%
    {
        transform: rotate(360deg);
    }
}
