:root {
    --header-height: 60px;
    --sub-header-height: 50px;
    --sidebar-width: 280px;
    --primary-color: #4a90e2;
    --bg-color: #f5f5f5;
    --panel-bg: #ffffff;
    --border-color: #e0e0e0;
    --text-color: #333;
    --icon-color: #555;
    --hover-color: #f0f8ff;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background-color: var(--bg-color);
}

/* --- Site Header --- */
.site-header {
    background-color: #fff;
    border-bottom: 1px solid var(--border-color);
    padding: 15px 20px;
}

.header-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    max-width: 100%;
    margin: 0 auto;
}

.logo a {
    font-size: 20px;
    font-weight: 700;
    color: #333;
}

.nav-link {
    margin-left: 20px;
    text-decoration: none;
    color: #555;
    font-size: 14px;
    font-weight: 500;
}

.nav-link:hover {
    color: var(--primary-color);
}

/* --- Editor Toolbar --- */
#editor-toolbar {
    height: var(--header-height);
    background-color: var(--panel-bg);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    padding: 0 10px;
    overflow-x: auto;
    white-space: nowrap;
    z-index: 100;
    /* Increased z-index */
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
}

.tool-btn {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-width: 70px;
    height: 100%;
    background: none;
    border: none;
    color: var(--icon-color);
    cursor: pointer;
    transition: all 0.2s;
    font-size: 12px;
    padding: 5px;
}

.tool-btn i {
    font-size: 18px;
    margin-bottom: 4px;
}

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

.tool-btn.active {
    color: var(--primary-color);
    border-bottom: 3px solid var(--primary-color);
}

/* Import Button in Toolbar */
.tool-btn.import-btn {
    background-color: var(--primary-color);
    color: white;
    border-radius: 6px;
    margin-right: 5px;
}

.tool-btn.import-btn:hover {
    background-color: #357abd;
    color: white;
}

/* Toolbar Divider */
.toolbar-divider {
    width: 1px;
    height: 40px;
    background-color: var(--border-color);
    margin: 0 10px;
}

/* --- Sub Toolbar (Options) --- */
#sub-toolbar {
    height: var(--sub-header-height);
    background-color: var(--panel-bg);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 20px;
    z-index: 9;
}

.tool-panel {
    display: none;
    /* Hidden by default */
    align-items: center;
    gap: 15px;
    width: 100%;
    justify-content: center;
}

.tool-panel.active {
    display: flex;
}

/* Controls in Sub Toolbar */
.control-group {
    display: flex;
    align-items: center;
    gap: 8px;
}

.control-group label {
    font-size: 12px;
    color: #666;
}

input[type="range"] {
    width: 100px;
}

input[type="color"] {
    width: 30px;
    height: 30px;
    border: none;
    padding: 0;
    cursor: pointer;
}

.sub-btn {
    padding: 6px 12px;
    border: 1px solid var(--border-color);
    background: #fff;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
}

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

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

/* --- Main Layout --- */
#app-container {
    flex: 1;
    display: flex;
    overflow: hidden;
    width: 100%;
    /* Ensure full width */
}

/* --- Canvas Area --- */
#workspace {
    flex: 1 1 0%;
    /* Force flex basis to 0 to properly share space */
    background-color: #333;
    /* Dark background for better contrast */
    position: relative;
    z-index: 0;
    min-width: 0;
    /* Prevent flex overflow */
    overflow: hidden;
}

.canvas-container-wrapper {
    position: absolute;
    /* Absolute positioning to prevent pushing flex container */
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

/* Fabric.js Container Override */
.canvas-container {
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
    /* Stronger shadow */
    background: white;
    /* Actual image background */
    z-index: 1 !important;
    position: relative !important;
    /* margin: auto;  Removed, handled by wrapper flex */
    transition: transform 0.1s ease-out;
    /* Smooth scaling */
}

/* --- Right Sidebar --- */
#sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    /* Prevent sidebar from shrinking */
    background-color: var(--panel-bg);
    border-left: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    padding: 15px;
    gap: 20px;
    overflow-y: auto;
    z-index: 50;
    /* Higher than canvas */
}

/* Sidebar Sections */
.sidebar-section {
    border-bottom: 1px solid #eee;
    padding-bottom: 15px;
}

.sidebar-section:last-child {
    border-bottom: none;
}

.open-btn {
    width: 100%;
    padding: 10px;
    border: 1px solid var(--primary-color);
    color: var(--primary-color);
    background: white;
    border-radius: 5px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.open-btn:hover {
    background: var(--hover-color);
}

.save-btn {
    width: 100%;
    padding: 12px;
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 5px;
    font-weight: 600;
    cursor: pointer;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.save-btn:hover {
    background-color: #357abd;
}

/* Zoom Controls */
.zoom-controls {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: #f9f9f9;
    padding: 8px;
    border-radius: 20px;
}

.zoom-btn {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    border: 1px solid #ddd;
    background: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.zoom-val {
    font-size: 14px;
    font-weight: 600;
}

/* History Controls */
.history-controls {
    display: flex;
    justify-content: center;
    gap: 20px;
    margin-top: 10px;
}

.icon-action-btn {
    background: none;
    border: none;
    font-size: 18px;
    cursor: pointer;
    color: #555;
    padding: 5px;
    border-radius: 4px;
}

.icon-action-btn:hover {
    background: #eee;
    color: var(--primary-color);
}

/* Layers Panel */
#layers-list {
    list-style: none;
    padding: 0;
    margin: 0;
    max-height: 200px;
    overflow-y: auto;
}

.layer-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px;
    background: #f9f9f9;
    border-bottom: 1px solid #eee;
    font-size: 13px;
    cursor: pointer;
}

.layer-item:hover {
    background: #f0f0f0;
}

.layer-item.active {
    background: #e6f2ff;
    border-left: 3px solid var(--primary-color);
}

.layer-name {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-left: 8px;
}

.layer-actions {
    display: flex;
    gap: 5px;
}

.layer-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 12px;
    color: #888;
}

.layer-btn:hover {
    color: #333;
}

/* Sticker Grid */
.sticker-grid {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 5px;
}

.sticker-btn {
    font-size: 20px;
    background: white;
    border: 1px solid #eee;
    cursor: pointer;
    border-radius: 4px;
    padding: 5px 0;
}

.sticker-btn:hover {
    background: #f0f0f0;
    transform: scale(1.1);
}

/* Hidden inputs */
.hidden-file {
    display: none;
}

/* --- Mobile Responsiveness --- */
@media (max-width: 768px) {
    body {
        height: auto;
        min-height: 100vh;
        overflow-y: auto;
    }

    #app-container {
        flex-direction: column;
        overflow: visible;
        flex: none;
    }

    /* Editor Toolbar (Top) */
    #editor-toolbar {
        overflow-x: auto;
        justify-content: flex-start;
        /* Hide scrollbar */
        scrollbar-width: none;
        /* Firefox */
        -ms-overflow-style: none;
        /* IE 10+ */
    }

    #editor-toolbar::-webkit-scrollbar {
        display: none;
        /* Chrome/Safari */
    }

    /* Sub Toolbar */
    #sub-toolbar {
        height: auto;
        padding: 10px;
        flex-wrap: wrap;
        justify-content: flex-start;
        overflow-x: auto;
        min-height: 50px;
    }

    .tool-panel.active {
        flex-wrap: wrap;
        justify-content: center;
    }

    .control-group {
        margin-bottom: 5px;
    }

    /* Canvas Area */
    #workspace {
        height: 50vh;
        /* Fixed height for canvas area */
        width: 100%;
        border-bottom: 1px solid #ddd;
        flex: none;
    }

    /* Sidebar (Bottom) */
    #sidebar {
        width: 100%;
        height: auto;
        border-left: none;
        padding: 15px;
        overflow: visible;
    }

    /* Make touch targets bigger */
    .tool-btn {
        min-width: 65px;
    }

    input[type="range"] {
        width: 80px;
    }

    /* Reduce layers height on mobile */
    #layers-list {
        max-height: 120px;
    }
}

/* --- Welcome Screen --- */
.welcome-container {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #f8f9fa;
    color: #555;
    text-align: center;
    border: 3px dashed #e0e0e0;
    margin: 20px;
    width: calc(100% - 40px);
    height: calc(100% - 40px);
    border-radius: 10px;
    box-sizing: border-box;
    /* Fix sizing issues */
}

.welcome-content {
    max-width: 400px;
}

.welcome-icon {
    font-size: 60px;
    color: var(--primary-color);
    margin-bottom: 20px;
    opacity: 0.8;
}

.welcome-content h2 {
    margin: 0 0 10px 0;
    font-size: 24px;
    color: #333;
}

.welcome-content p {
    margin: 0 0 20px 0;
    font-size: 16px;
    color: #666;
}

.welcome-sub {
    font-size: 13px !important;
    color: #999 !important;
    margin-top: 15px !important;
}

.welcome-btn {
    padding: 12px 30px;
    font-size: 18px;
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-weight: 600;
    box-shadow: 0 4px 6px rgba(50, 50, 93, 0.11), 0 1px 3px rgba(0, 0, 0, 0.08);
    transition: all 0.2s;
}

.welcome-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 7px 14px rgba(50, 50, 93, 0.1), 0 3px 6px rgba(0, 0, 0, 0.08);
    background-color: #357abd;
}