:root {
    --primary: #6e45e2;
    --secondary: #88d3ce;
    --accent: #ff6b6b;
    --bg: #f0f2f5;
    --panel-bg: rgba(255, 255, 255, 0.95);
    --text: #333;
    --shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    --safe-area-inset-bottom: env(safe-area-inset-bottom, 0px);
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body,
html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    height: 100dvh;
    /* Dynamic viewport height */
    overflow: hidden;
    font-family: 'Outfit', 'Inter', -apple-system, sans-serif;
    background-color: var(--bg);
    color: var(--text);
}

#app {
    display: flex;
    flex-direction: column;
    height: 100%;
    /* Height inherited from html/body */
    width: 100%;
}

/* Canvas Area */
#canvas-container {
    flex: 1;
    overflow: hidden;
    position: relative;
    /* Fixed: Added relative to anchor layer-info */
    background-image:
        linear-gradient(45deg, #eee 25%, transparent 25%),
        linear-gradient(-45deg, #eee 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, #eee 75%),
        linear-gradient(-45deg, transparent 75%, #eee 75%);
    background-size: 20px 20px;
    background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: grab;
}

#canvas-container:active {
    cursor: grabbing;
}

/* Top Bar */
#top-bar {
    position: absolute;
    top: 25px;
    /* Lowered from 15px */
    left: 15px;
    right: 15px;
    display: flex;
    gap: 15px;
    z-index: 100;
}

.circle-btn {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    border: none;
    background: white;
    box-shadow: var(--shadow);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    cursor: pointer;
    transition: transform 0.2s;
}

.circle-btn:active {
    transform: scale(0.9);
}

.rect-btn {
    padding: 0 24px;
    height: 50px;
    border-radius: 25px;
    border: none;
    background: var(--primary);
    color: white;
    font-weight: bold;
    font-size: 16px;
    box-shadow: var(--shadow);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.rect-btn.outline {
    background: white;
    color: var(--primary);
    border: 3px solid var(--primary);
    box-shadow: none;
}

.spacer {
    flex: 1;
}

/* Bottom Toolbar */
#bottom-toolbar {
    background: white;
    display: flex;
    justify-content: space-around;
    /* Extreme padding to push content above Android/iOS bars */
    padding: 10px 5px calc(65px + var(--safe-area-inset-bottom));
    box-shadow: 0 -12px 35px rgba(0, 0, 0, 0.15);
    z-index: 110;
    border-top: 2px solid #efefef;
    min-height: 120px;
}

.tool-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border: none;
    background: transparent;
    cursor: pointer;
    transition: all 0.2s;
    border-radius: 12px;
    padding: 5px 2px;
    min-width: 0;
}

.tool-btn.active {
    background: #f0ebff;
    color: var(--primary);
}

.tool-btn .icon {
    font-size: 24px;
    /* Slightly smaller to fit more buttons */
    margin-bottom: 2px;
}

.tool-btn .label {
    font-size: 10px;
    /* Smaller labels for compact screens */
    font-weight: 700;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 100%;
}

/* Layer Info Badge */
#layer-info {
    position: absolute;
    bottom: 20px;
    /* Floating inside the canvas area */
    left: 20px;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(8px);
    padding: 8px 18px;
    border-radius: 25px;
    font-size: 14px;
    font-weight: 700;
    color: var(--primary);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
    z-index: 105;
    pointer-events: none;
    border: 2px solid white;
}

/* Eraser Sizes Panel Fluid */
.floating-panel {
    position: absolute;
    bottom: 155px;
    left: 50%;
    transform: translateX(-50%);
    background: white;
    padding: 12px 25px;
    border-radius: 40px;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
    display: flex;
    align-items: center;
    gap: 12px;
    z-index: 105;
    border: 3px solid var(--primary);
    min-width: 240px;
    transition: opacity 0.3s ease, transform 0.3s ease, visibility 0.3s;
}

.floating-panel.hide-temporary {
    opacity: 0;
    pointer-events: none;
    transform: translateX(-50%) translateY(20px);
    visibility: hidden;
}

#eraser-preview-container {
    width: 60px;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f8f9fa;
    border-radius: 10px;
    margin-right: 15px;
    border: 1px solid #eee;
}

#eraser-preview {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 50%;
    border: 1px solid #999;
}

#eraser-slider {
    flex: 1;
    -webkit-appearance: none;
    appearance: none;
    /* Standard property for compatibility */
    height: 12px;
    border-radius: 6px;
    background: #f0f0f0;
    outline: none;
}

#eraser-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 34px;
    height: 34px;
    border-radius: 17px;
    background: var(--primary);
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
    border: 3px solid white;
}

#eraser-slider::-moz-range-thumb {
    width: 34px;
    height: 34px;
    border-radius: 17px;
    background: var(--primary);
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
    border: 3px solid white;
}

/* Panels & Overlays */
.panel {
    position: absolute;
    right: 15px;
    bottom: 120px;
    width: 280px;
    background: var(--panel-bg);
    backdrop-filter: blur(10px);
    border-radius: 20px;
    box-shadow: var(--shadow);
    z-index: 150;
    display: flex;
    flex-direction: column;
    max-height: 50vh;
}

.panel-header {
    padding: 12px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #eee;
}

.panel-header h3 {
    margin: 0;
    font-size: 18px;
}

#layers-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
}

.layers-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
    padding: 15px;
    overflow-y: auto;
}

.layer-grid-item {
    aspect-ratio: 1;
    background: white;
    border-radius: 12px;
    border: 3px solid transparent;
    cursor: pointer;
    overflow: hidden;
    position: relative;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    transition: transform 0.2s, border-color 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.layer-grid-item:active {
    transform: scale(0.95);
}

.layer-grid-item.selected {
    border-color: var(--primary);
    background: #f8f6ff;
    box-shadow: 0 4px 12px rgba(110, 69, 226, 0.3);
}

.layer-thumb-img {
    width: 90%;
    height: 90%;
    object-fit: contain;
}

.panel-footer {
    padding: 12px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    border-top: 1px solid #eee;
}

.small-btn {
    padding: 8px;
    border-radius: 8px;
    border: 1px solid #ddd;
    background: white;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
}

.small-btn.delete {
    color: var(--accent);
}

/* Overlays */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 200;
    display: flex;
    align-items: flex-end;
}

.overlay-content {
    width: 100%;
    background: white;
    border-top-left-radius: 30px;
    border-top-right-radius: 30px;
    padding: 20px;
    max-height: 85vh;
    overflow-y: auto;
    animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
    from {
        transform: translateY(100%);
    }

    to {
        transform: translateY(0);
    }
}

.overlay-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

#stickers-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 15px;
}

.sticker-item {
    background: #f9f9f9;
    padding: 10px;
    border-radius: 15px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.sticker-item img {
    max-width: 100%;
    max-height: 100px;
}

.close-btn {
    width: 40px;
    height: 40px;
    border-radius: 20px;
    border: none;
    background: #eee;
    font-size: 18px;
    cursor: pointer;
}

.hidden {
    display: none !important;
}

/* Search Styles */
.search-input-wrapper {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
}

#search-query {
    flex: 1;
    padding: 12px 20px;
    border-radius: 25px;
    border: 2px solid #eee;
    font-size: 16px;
    outline: none;
}

#search-query:focus {
    border-color: var(--primary);
}

#btn-do-search {
    width: 50px;
    height: 50px;
    border-radius: 25px;
    background: var(--primary);
    color: white;
    border: none;
    cursor: pointer;
    font-size: 20px;
}

#search-results {
    min-height: 200px;
}

/* Responsive adjustments */
@media (min-width: 768px) {
    .panel {
        width: 320px;
    }

    #stickers-grid {
        grid-template-columns: repeat(4, 1fr);
    }
}