:root {
    --bg-dark: #0f172a;
    --bg-sidebar: #1e293b;
    --bg-header: rgba(15, 23, 42, 0.8);
    --accent: #7c3aed;
    --accent-hover: #8b5cf6;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --border: rgba(255, 255, 255, 0.1);
    --danger: #ef4444;
    --canvas-bg: #ffffff; /* Transparent look usually uses white/checkered, but app is dark themes */
    --surface: #334155;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Inter', -apple-system, sans-serif;
    background-color: var(--bg-dark);
    color: var(--text-primary);
    overflow: hidden;
    height: 100vh;
}

.app-container {
    display: flex;
    flex-direction: column;
    height: 100%;
}

/* Header */
.app-header {
    height: 64px;
    background: var(--bg-header);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    z-index: 100;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
}

.logo-icon {
    width: 24px;
    height: 24px;
    background: linear-gradient(135deg, var(--accent), #3b82f6);
    border-radius: 6px;
    transform: rotate(45deg);
}

.brand h1 {
    font-size: 18px;
    font-weight: 800;
    letter-spacing: -0.5px;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.divider {
    width: 1px;
    height: 24px;
    background: var(--border);
    margin: 0 8px;
}

/* Main Content Layout */
.app-content {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.sidebar {
    width: 280px;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 32px;
    overflow-y: auto;
}

.properties-sidebar {
    border-right: none;
    border-left: 1px solid var(--border);
}

.canvas-wrapper {
    flex: 1;
    background: #111b2d;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px;
    position: relative;
    background-image: 
        radial-gradient(rgba(255,255,255,0.05) 1px, transparent 0);
    background-size: 30px 30px;
}

.canvas-container-inner {
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
    background: white; /* Fabric defaults to white */
    border-radius: 4px;
}

/* Tools & Grid */
.tool-section h3 {
    font-size: 12px;
    text-transform: uppercase;
    color: var(--text-secondary);
    margin-bottom: 16px;
    letter-spacing: 1px;
}

/* Templates */
.template-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.template-btn {
    display: flex;
    align-items: center;
    gap: 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px;
    color: var(--text-primary);
    cursor: pointer;
    text-align: left;
    transition: all 0.2s;
}

.template-btn:hover {
    background: #475569;
    border-color: var(--accent);
}

.tmpl-preview {
    width: 48px;
    height: 48px;
    background: #0f172a;
    border-radius: 8px;
    border: 1px solid var(--border);
    position: relative;
    overflow: hidden;
}

.sphere-icon::after {
    content: '';
    position: absolute; top: 10%; left: 10%; width: 80%; height: 80%;
    border: 1px solid var(--accent);
    border-radius: 50%;
    box-shadow: inset 0 0 10px rgba(124,58,237,0.3);
}

.spiro-icon::after {
    content: '✵';
    position: absolute; top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    font-size: 24px; color: var(--accent);
}

.petal-icon::after {
    content: '✿';
    position: absolute; top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    font-size: 24px; color: var(--accent);
}

.mesh-icon::after {
    content: '◬';
    position: absolute; top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    font-size: 24px; color: var(--accent);
}

.noise-icon::after {
    content: '⌇';
    position: absolute; top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    font-size: 24px; color: var(--accent);
}

.template-btn span {
    font-size: 14px;
    font-weight: 600;
}

/* Sidebar Sections */
.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
}

.active-tag {
    font-size: 10px;
    background: var(--accent);
    padding: 2px 8px;
    border-radius: 4px;
    font-weight: 700;
}

.divider-h {
    height: 1px;
    background: var(--border);
    margin: 24px 0;
}

.val-badge {
    float: right;
    font-size: 11px;
    color: var(--accent);
    font-weight: 700;
}

/* Properties */
.prop-row {
    margin-bottom: 20px;
}

.prop-row label {
    display: block;
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.color-picker-wrap {
    height: 40px;
    overflow: hidden;
    border-radius: 6px;
    border: 1px solid var(--border);
}

input[type="color"] {
    width: 150%;
    height: 150%;
    border: none;
    cursor: pointer;
    transform: translate(-10%, -10%);
}

input[type="range"] {
    width: 100%;
    accent-color: var(--accent);
}

input[type="number"] {
    width: 100%;
    background: var(--surface);
    border: 1px solid var(--border);
    color: white;
    padding: 8px;
    border-radius: 6px;
}

/* Buttons */
.icon-btn, .icon-btn-s {
    background: var(--surface);
    border: 1px solid transparent;
    color: var(--text-primary);
    padding: 10px;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.icon-btn:hover { background: #475569; }

.icon-btn-s { padding: 6px; }

.primary-btn {
    background: var(--accent);
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}

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

.secondary-btn {
    background: var(--surface);
    color: var(--text-primary);
    border: 1px solid var(--border);
    padding: 10px 20px;
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}
.secondary-btn:hover { background: #475569; border-color: var(--text-secondary); }

/* Share Dropdown */
.share-dropdown { position: relative; display: inline-block; }
.dropdown-content {
    display: none;
    position: absolute;
    right: 0;
    top: calc(100% + 8px);
    background-color: var(--bg-sidebar);
    min-width: 240px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.5);
    border: 1px solid var(--border);
    border-radius: 8px;
    z-index: 200;
    padding: 8px 0;
}
.dropdown-content.show { display: block; }
.dropdown-help {
    padding: 8px 16px;
    font-size: 11px;
    color: var(--text-secondary);
    line-height: 1.4;
}
.dropdown-divider {
    height: 1px;
    background: var(--border);
    margin: 4px 0;
}
.dropdown-item {
    padding: 10px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    color: var(--text-primary);
    background: transparent;
    border: none;
    width: 100%;
    text-align: left;
    cursor: pointer;
    font-size: 13px;
    transition: background 0.2s;
}
.dropdown-item:hover { background: var(--surface); }

.text-btn {
    background: transparent;
    color: var(--text-secondary);
    border: none;
    padding: 8px 12px;
    font-weight: 600;
    cursor: pointer;
}

.text-btn:hover { color: white; }

.danger-btn {
    background: rgba(239, 68, 68, 0.1);
    color: var(--danger);
    border: 1px solid var(--danger);
    padding: 10px;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
}

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

.full-width { width: 100%; }

.btn-group {
    display: flex;
    gap: 8px;
    margin-top: 12px;
}

/* Layer List */
.layer-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.layer-item {
    background: var(--surface);
    padding: 10px 12px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 13px;
    border: 1px solid transparent;
    cursor: pointer;
}

.layer-item.active {
    border-color: var(--accent);
    background: rgba(124, 58, 237, 0.1);
}

.layer-actions {
    display: flex;
    gap: 8px;
    color: var(--text-secondary);
}

/* Messages */
.empty-state {
    text-align: center;
    color: var(--text-secondary);
    font-size: 14px;
    margin-top: 40px;
}

.toast {
    position: fixed;
    bottom: 24px;
    right: 24px;
    background: #10b981;
    color: white;
    padding: 12px 24px;
    border-radius: 8px;
    box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1);
    transform: translateY(100px);
    transition: transform 0.3s ease;
    z-index: 1000;
}

.toast.show { transform: translateY(0); }

/* --- Mobile Optimizations (Max 768px) --- */
@media (max-width: 768px) {
    /* 1. Ensure sensible viewport height behavior */
    body {
        min-height: 100dvh;
        overflow-y: auto; /* Allow vertical scrolling on the body level */
    }
    .app-container {
        height: auto;
        min-height: 100dvh;
    }

    /* 2. Header layout */
    .app-header {
        padding: 0 12px;
    }
    
    /* Hide some elements in header to save space */
    #clear-btn,
    #save-json-btn,
    #export-svg-btn {
        display: none !important;
    }
    
    /* 3. Main layout switches to vertical */
    .app-content {
        flex-direction: column;
        overflow-y: visible; /* Scroll within body, not restricted content area */
    }

    /* Sidebars take full width */
    .sidebar {
        width: 100%;
        border-right: none;
        border-bottom: 1px solid var(--border);
        padding: 16px;
        gap: 20px;
    }
    .properties-sidebar {
        border-left: none;
    }

    /* 4. Canvas sizing and spacing */
    .canvas-wrapper {
        padding: 12px;
        min-height: 300px; /* Keep minimum visible area */
    }

    /* 5. Horizontal scrolling for templates to save vertical space */
    .template-list {
        flex-direction: row;
        overflow-x: auto;
        gap: 8px;
        padding-bottom: 8px; /* Room for scrollbar */
    }
    /* Hide scrollbar visually but keep function */
    .template-list::-webkit-scrollbar {
        height: 6px;
    }
    .template-list::-webkit-scrollbar-thumb {
        background: var(--surface);
        border-radius: 4px;
    }

    .template-btn {
        min-width: 160px;
        flex-shrink: 0;
        padding: 8px 12px;
    }

    /* 6. Enlarge tap targets for mobile usability */
    .icon-btn, .icon-btn-s {
        min-width: 44px;
        min-height: 44px;
    }
    
    .primary-btn, .secondary-btn, .danger-btn {
        min-height: 44px;
    }

    /* 7. Share Dropdown fix */
    .dropdown-content {
        width: min(280px, calc(100vw - 24px));
        right: 0;
        position: absolute;
    }
    
    /* Parameter and Layers compression */
    .prop-row {
        margin-bottom: 16px;
    }
    .layer-item {
        padding: 8px 10px;
    }
}
