/* Root Variables */
:root {
    /* Light theme (default) */
    --primary-color: #007AFF;
    --surface-color: #ffffff;
    --surface-hover: #f5f5f5;
    --background-color: #f0f2f5;
    --text-primary: #1a1a1a;
    --text-secondary: #666666;
    --border-color: #e0e0e0;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.1);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
    
    /* Spacing */
    --spacing-xs: 4px;
    --spacing-sm: 8px;
    --spacing-md: 16px;
    --spacing-lg: 24px;
    --spacing-xl: 32px;
    
    /* Border radius */
    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;
}

[data-theme="dark"] {
    --primary-color: #0A84FF;
    --surface-color: #1c1c1e;
    --surface-hover: #2c2c2e;
    --background-color: #000000;
    --text-primary: #ffffff;
    --text-secondary: #8e8e93;
    --border-color: #38383A;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.3);
}

/* Reset & Base Styles */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Plus Jakarta Sans', 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background-color: var(--background-color);
    color: var(--text-primary);
    line-height: 1.5;
    padding: var(--spacing-lg);
}

h1 {
    text-align: center;
    margin-bottom: 30px;
    font-size: 24px;
    padding: 10px;
}

/* Container */
.container {
    max-width: 100%;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    height: 90vh;
}

/* Header */
.app-header {
    background-color: var(--primary-color);
    color: #ffffff;
    padding: 20px;
    text-align: center;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-md);
}

.app-header h1 {
    font-size: 24px;
    font-weight: 700;
}

/* Subtitle */
.subtitle {
    opacity: 0.9;
    margin: 5px 0 10px;
    font-size: 18px;
    font-weight: 400;
    color: var(--text-secondary);
}

/* Layout */
.app-layout {
    display: flex;
    flex: 1;
    flex-direction: row;
    gap: 15px;
    height: calc(90vh - 100px);
}

/* Icon Browser */
.icon-browser {
    width: 50%;
    height: 100%;
    background: var(--surface-color);
    border-radius: var(--radius-sm);
    padding: var(--spacing-md);
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
}

.icon-browser.collapsed {
    height: 48px;
}

.icon-browser-header {
    margin-bottom: var(--spacing-md);
}

.icon-browser-header.collapsed {
    min-height: 48px;
    background: var(--surface-color);
    border-bottom: 1px solid var(--border-color);
    padding: var(--spacing-sm);
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
    flex-shrink: 0;
}

/* Search Controls */
.search-controls {
    display: flex;
    gap: var(--spacing-sm);
    align-items: center;
    background-color: var(--surface-color);
    border-radius: var(--radius-sm);
    padding: var(--spacing-sm);
    box-shadow: var(--shadow-sm);
    width: 100%;
}

.search-container {
    flex: 1;
    position: relative;
}

#iconSearch {
    width: 100%;
    height: 42px;
    padding: 12px 16px;
    padding-left: 36px;
    border: 1px solid var(--input-border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-family: inherit;
    color: var(--text-primary);
    background-color: var(--surface-color);
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

#iconSearch:hover {
    border-color: var(--primary-color);
}

#iconSearch:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px var(--input-focus);
}

.search-container::before {
    content: "";
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    width: 16px;
    height: 16px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%234A5568'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z'%3E%3C/path%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: center;
    opacity: 0.5;
}

/* Library Select */
.library-select {
    background-color: var(--surface-color);
    color: var(--text-primary);
    border: 1px solid var(--input-border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    min-width: 120px;
    padding: 12px 16px;
    padding-right: 32px;
    font-family: inherit;
    cursor: pointer;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%234A5568'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 8px center;
    background-size: 16px;
    appearance: none;
    height: 42px;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.library-select:hover,
.library-select:focus {
    border-color: var(--primary-color);
}

.library-select:focus {
    outline: none;
    box-shadow: 0 0 0 3px var(--input-focus);
}

.library-select option {
    background-color: var(--input-background);
    color: var(--text-primary);
    padding: 8px;
}

/* Main Content */
.main-content {
    width: 60%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    padding: 20px;
}

/* Controls */
.controls {
    width: calc(50% - 20px);
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
    overflow-y: auto;
    padding: 12px;
    background: var(--surface-color);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-sm);
}

.controls > *:not(:last-child) {
    margin-bottom: var(--spacing-sm);
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 0;
}

.theme-controls,
.color-controls {
    display: flex;
    gap: 2px;
    flex-wrap: wrap;
    margin-bottom: 4px;
}

.color-picker-container {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-bottom: 4px;
}

input[type="color"] {
    width: 32px;
    height: 32px;
    padding: 2px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--surface-color);
    cursor: pointer;
}

.theme-controls {
    display: flex;
    gap: 5px;
    margin: 10px 0;
    align-items: center;
}

.theme-controls label {
    margin: 0;
    font-size: 0.9rem;
    color: var(--text-secondary);
    width: fit-content;
    line-height: 32px;
}

.theme-controls + .theme-controls {
    margin-top: 4px;
}

.theme-btn {
    height: 32px;
    padding: 0 12px;
    font-size: 0.9rem;
    border: none;
    border-radius: 6px;
    background: var(--surface-color);
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.theme-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

.theme-btn.active {
    background: var(--primary-color);
    color: #ffffff;
    box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.5);
}

/* Save Button */
.save-button {
    margin-top: 12px;
    padding: 8px 16px;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-weight: 600;
    transition: opacity 0.2s ease;
}

.save-button:hover {
    opacity: 0.9;
}

/* Canvas */
.canvas-container {
    width: 100%;
    max-width: 400px;
    aspect-ratio: 1;
    margin-top: 20px;
    text-align: center;
    padding: 20px;
}

#iconCanvas {
    width: 100%;
    height: 100%;
    border: 0;
    border-radius: var(--radius-sm);
    max-width: 100%;
    height: auto;
}

/* Labels */
label {
    display: block;
    font-size: 0.9rem;
    font-weight: 400;
    color: var(--text-secondary);
    margin-bottom: 4px;
}

/* Range Inputs */
input[type="range"] {
    width: 100%;
    margin: 4px 0 12px 0;
    accent-color: var(--primary-color);
}

/* Icons Grid */
.icons-grid-container {
    flex: 1;
    overflow-y: auto;
    padding-right: var(--spacing-sm);
    margin-top: var(--spacing-md);
    height: calc(100% - 80px);
}

.icons-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(60px, 1fr));
    gap: var(--spacing-md);
    padding: var(--spacing-sm);
}

.icon-item {
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--surface-color);
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-color);
    cursor: pointer;
    position: relative;
    transition: background-color 0.2s ease, transform 0.2s ease;
    padding: var(--spacing-sm);
}

.icon-item:hover {
    background-color: var(--surface-hover);
    transform: translateY(-1px);
}

.icon-item:hover svg {
    fill: var(--primary-color);
}

.icon-item svg {
    width: 100%;
    height: 100%;
    fill: var(--icon-color);
    transition: fill 0.2s ease;
}

/* Icon Tooltip */
.icon-tooltip {
    position: absolute;
    bottom: -20px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(15, 23, 42, 0.7);
    color: #ffffff;
    padding: 2px 5px;
    border-radius: 4px;
    font-size: 10px;
    text-align: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s;
    pointer-events: none;
    z-index: 1000;
}

.icon-item:hover .icon-tooltip {
    opacity: 1;
    visibility: visible;
}

/* Buttons */
.button-container {
    margin-top: var(--spacing-md);
    padding: var(--spacing-sm);
}

#downloadBtn,
#downloadVariantsBtn {
    width: 100%;
    padding: 10px 20px;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s, transform 0.2s;
}

#downloadBtn:hover,
#downloadVariantsBtn:hover {
    transform: translateY(-1px);
}

#downloadVariantsBtn {
    background: #2563eb;
}

#downloadVariantsBtn:hover {
    background: #1d4ed8;
}

/* Custom Scrollbar */
.icons-grid-container::-webkit-scrollbar {
    width: 6px;
}

.icons-grid-container::-webkit-scrollbar-track {
    background: var(--background-color);
    border-radius: var(--radius-sm);
}

.icons-grid-container::-webkit-scrollbar-thumb {
    background: #94a3b8;
    border-radius: var(--radius-sm);
}

.icons-grid-container::-webkit-scrollbar-thumb:hover {
    background: #64748b;
}

::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: var(--background-color);
}

::-webkit-scrollbar-thumb {
    background: #94a3b8;
}

.dark-theme {
    /* Icon Item Hover */
    .icon-item:hover {
        background-color: rgba(255, 255, 255, 0.1);
    }

    /* Library Select, Search Input, Theme Button */
    .dark-theme .library-select,
    .dark-theme .search-container input,
    .dark-theme .theme-btn {
        background: var(--surface-color);
        border-color: var(--border-color);
        color: var(--text-primary);
    }

    /* Scrollbar for Dark Theme */
    .dark-theme .icons-grid-container::-webkit-scrollbar-track {
        background: var(--surface-color);
    }

    .dark-theme .icons-grid-container::-webkit-scrollbar-thumb {
        background: #666;
    }

    .dark-theme .icons-grid-container::-webkit-scrollbar-thumb:hover {
        background: #888;
    }
}

/* Toggle Browser Button */
.toggle-browser-btn {
    display: none;
    background: none;
    border: none;
    color: var(--text-primary);
    cursor: pointer;
    padding: 8px;
    font-size: 16px;
}

.toggle-browser-btn .arrow {
    transition: transform 0.3s ease;
}

.toggle-browser-btn.collapsed .arrow {
    transform: rotate(-90deg);
}

/* Gumroad Container */
.gumroad-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 1000;
}

.gumroad-button {
    background: var(--primary-color) !important;
    color: white !important;
    border: none !important;
    border-radius: var(--radius-sm) !important;
    padding: 8px 16px !important;
    font-family: 'Inter', sans-serif !important;
    font-size: 12px !important;
    font-weight: 400 !important;
    text-decoration: none !important;
    display: inline-block !important;
    transition: transform 0.2s, background-color 0.2s !important;
    box-shadow: var(--shadow-sm) !important;
}

.gumroad-button:hover {
    transform: translateY(-1px);
    background: #1d4ed8 !important;
}

@media (max-width: 768px) {
    /* Body Padding */
    body {
        padding: var(--spacing-sm);
        padding-top: calc(48px + env(safe-area-inset-top, 20px));
        min-height: 100vh;
        min-height: -webkit-fill-available;
    }

    /* Container Adjustments */
    .container {
        height: auto;
        min-height: 100vh;
        padding: 0;
    }

    /* Layout Adjustments */
    .app-layout {
        flex-direction: column-reverse;
        height: auto;
        gap: var(--spacing-md);
    }

    /* Main Content Sticky */
    .main-content {
        width: 100%;
        position: sticky;
        top: calc(48px + env(safe-area-inset-top, 20px));
        z-index: 10;
        background: var(--background-color);
        padding: var(--spacing-sm);
        margin-top: auto;
        order: 2;
    }

    /* Canvas Container */
    .canvas-container {
        max-width: 250px;
        margin: 0 auto;
        padding: var(--spacing-sm);
        background: var(--background-color);
    }

    /* Icon Browser */
    .icon-browser {
        width: 100%;
        order: 1;
    }

    .icon-browser.collapsed .icons-grid-container,
    .icon-browser.collapsed .search-container {
        display: none;
    }

    /* Search Controls */
    .search-controls {
        flex-direction: column;
        gap: var(--spacing-sm);
        width: 100%;
        padding: 0;
        margin: 0;
    }

    /* Library Select */
    .library-select {
        width: 100%;
        min-width: 100px;
        height: 40px;
        background-color: var(--input-background);
    }

    /* Icons Grid */
    .icons-grid {
        grid-template-columns: repeat(auto-fill, minmax(45px, 1fr));
        gap: var(--spacing-sm);
        padding: 0;
        width: 100%;
        box-sizing: border-box;
    }

    .icon-item {
        padding: var(--spacing-xs);
        width: 100%;
    }

    .icon-item svg {
        max-width: 24px;
        max-height: 24px;
    }

    /* Toggle Browser Button */
    .toggle-browser-btn {
        display: block;
        position: absolute;
        right: var(--spacing-sm);
        top: var(--spacing-sm);
        z-index: 2;
    }

    /* Icons Grid Container */
    .icons-grid-container {
        padding: var(--spacing-sm);
        background: var(--surface-color);
        min-height: 0;
        width: 100%;
        box-sizing: border-box;
    }

    /* Controls */
    .controls {
        width: 100%;
        padding: var(--spacing-sm);
        background: var(--background-color);
    }

    /* Color Controls */
    .color-controls {
        flex-direction: column;
        gap: var(--spacing-sm);
        margin-bottom: 12px;
    }

    /* Theme Controls */
    .theme-controls {
        width: 100%;
        justify-content: space-between;
    }

    .theme-btn {
        flex: 1;
        padding: 8px;
        font-size: 13px;
    }

    /* Button Container */
    .button-container {
        padding: var(--spacing-sm);
    }

    /* Download Buttons */
    #downloadBtn {
        width: 100%;
    }

    /* Gumroad Container Adjustments */
    .gumroad-container {
        position: static;
        width: 100%;
        text-align: center;
        padding: var(--spacing-md) 0;
        margin-top: auto;
    }

    .gumroad-button {
        font-size: 12px !important;
        padding: 6px 12px !important;
        margin: 0 auto !important;
    }

    /* Mobile Top Bar */
    .mobile-top-bar {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: var(--spacing-md) var(--spacing-lg);
        background: var(--surface-color);
        border-radius: var(--radius-sm);
        margin-bottom: var(--spacing-md);
        box-shadow: var(--shadow-sm);
    }

    .mobile-brand {
        display: flex;
        align-items: center;
        gap: var(--spacing-sm);
    }

    .mobile-favicon {
        width: 24px;
        height: 24px;
    }

    .mobile-title {
        font-weight: 600;
        color: var(--text-primary);
    }

    .theme-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
        background: none;
        border: none;
        cursor: pointer;
        padding: var(--spacing-xs);
        color: var(--text-primary);
        border-radius: var(--radius-sm);
    }

    .theme-toggle:hover {
        background: var(--surface-hover);
    }

    .theme-toggle svg {
        stroke-width: 1.5;
        width: 20px;
        height: 20px;
    }

    .theme-toggle .light-icon {
        display: none;
    }

    .theme-toggle .dark-icon {
        display: block;
    }

    [data-theme="dark"] .theme-toggle .light-icon {
        display: block;
    }

    [data-theme="dark"] .theme-toggle .dark-icon {
        display: none;
    }

    .theme-toggle-text {
        display: none;
    }

    /* Hide Mobile Top Bar by Default */
    .mobile-top-bar {
        display: none;
    }

    /* Show Mobile Top Bar */
    @media (max-width: 768px) {
        .mobile-top-bar {
            display: flex;
        }
    }

    /* Hide Tooltip on Mobile */
    .icon-tooltip {
        display: none !important;
    }

    .icon-item:hover .icon-tooltip {
        display: none !important;
    }
}

/* Adjust Spacing for Groups of Controls */
.controls > *:not(:last-child) {
    margin-bottom: var(--spacing-sm);
}

[data-theme="dark"] {
    --background-color: #1a1f2e;
    --surface-color: #242938;
    --surface-hover: #2d3446;
    --border-color: #2d3446;
    --text-primary: #FFFFFF;
    --text-secondary: #A0AEC0;
    --text-tertiary: #718096;
    --icon-color: #FFFFFF;
    --input-background: #242938;
    --input-border: #2d3446;
}

.theme-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    cursor: pointer;
    padding: var(--spacing-xs);
    color: var(--text-primary);
    border-radius: var(--radius-sm);
}

.theme-toggle:hover {
    background: var(--surface-hover);
}

.theme-toggle svg {
    stroke-width: 1.5;
    width: 20px;
    height: 20px;
}

.theme-toggle .light-icon {
    display: none;
}

.theme-toggle .dark-icon {
    display: block;
}

[data-theme="dark"] .theme-toggle .light-icon {
    display: block;
}

[data-theme="dark"] .theme-toggle .dark-icon {
    display: none;
}

.theme-toggle-text {
    display: none;
}

@media (min-width: 768px) {
    .theme-toggle {
        padding: var(--spacing-xs) var(--spacing-md);
    }
}

.mobile-top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--spacing-md) var(--spacing-lg);
    background: var(--surface-color);
    border-radius: var(--radius-sm);
    margin-bottom: var(--spacing-md);
    box-shadow: var(--shadow-sm);
    position: sticky;
    top: 0;
    z-index: 100;
}

@media (min-width: 768px) {
    .mobile-top-bar {
        padding: var(--spacing-lg);
        border-radius: var(--radius-md);
        width: auto;
    }

    .mobile-title {
        font-size: 1.1rem;
    }

    .theme-toggle {
        padding: var(--spacing-sm);
    }

    .theme-toggle svg {
        width: 24px;
        height: 24px;
    }
}
