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

:root {
    --bg: #f5f5f7;
    --surface: #ffffff;
    --surface-hover: #f0f0f2;
    --border: #e0e0e3;
    --text: #1a1a1a;
    --text-secondary: #666666;
    --accent: #ff6b6b;
    --accent-hover: #ff5252;
    --shadow: 0 2px 8px rgba(0,0,0,0.08);
    --shadow-lg: 0 8px 32px rgba(0,0,0,0.12);
    --radius: 12px;
    --radius-sm: 8px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    overflow-x: hidden;
}

.app {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* ===== Header ===== */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
    z-index: 100;
    position: sticky;
    top: 0;
}

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

.logo-img {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    object-fit: cover;
}

.logo h1 {
    font-size: 20px;
    font-weight: 700;
    letter-spacing: -0.5px;
}

.header-actions {
    display: flex;
    gap: 10px;
}

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
}

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

.btn-primary:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
}

.btn-secondary {
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    background: var(--surface-hover);
}

/* ===== Main Layout ===== */
.main {
    display: flex;
    flex: 1;
    gap: 20px;
    padding: 20px;
    max-width: 1400px;
    margin: 0 auto;
    width: 100%;
}

/* ===== Toolbar ===== */
.toolbar {
    width: 220px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 16px;
    overflow-y: auto;
    max-height: calc(100vh - 80px);
    position: sticky;
    top: 20px;
}

.toolbar::-webkit-scrollbar {
    width: 4px;
}

.toolbar::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 4px;
}

.tool-section {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
}

.tool-section h3 {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 12px;
}

.tools {
    display: flex;
    gap: 8px;
}

.tool-btn {
    width: 44px;
    height: 44px;
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    color: var(--text-secondary);
}

.tool-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.tool-btn.active {
    border-color: var(--accent);
    background: rgba(255,107,107,0.08);
    color: var(--accent);
}

.current-color-wrapper {
    display: flex;
    align-items: center;
    gap: 12px;
}

.current-color {
    width: 48px;
    height: 48px;
    border-radius: var(--radius-sm);
    border: 3px solid var(--border);
    box-shadow: inset 0 0 0 2px white;
    transition: background-color 0.2s;
}

.color-name {
    font-size: 14px;
    font-weight: 600;
    color: var(--text);
}

.actions {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.action-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    color: var(--text);
    transition: all 0.2s;
    font-family: inherit;
}

.action-btn:hover {
    background: var(--surface-hover);
    border-color: var(--accent);
    color: var(--accent);
}

.action-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* ===== Palette ===== */
.palette-section {
    flex: 1;
    min-height: 0;
}

.palette {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 6px;
}

.palette-color {
    width: 100%;
    aspect-ratio: 1;
    border-radius: 6px;
    cursor: pointer;
    border: 2px solid transparent;
    transition: all 0.15s;
    position: relative;
}

.palette-color:hover {
    transform: scale(1.15);
    z-index: 2;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

.palette-color.active {
    border-color: var(--text);
    box-shadow: 0 0 0 2px white, 0 0 0 4px var(--accent);
    z-index: 3;
}

.palette-color.transparent {
    background: repeating-conic-gradient(#ddd 0% 25%, #fff 0% 50%) 50% / 8px 8px;
    border: 2px solid var(--border);
}

/* ===== Canvas Area ===== */
.canvas-wrapper {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
}

.canvas-container {
    position: relative;
    background: white;
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    padding: 24px;
    cursor: crosshair;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

.canvas-container canvas {
    display: block;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

#base-canvas,
#grid-canvas,
#draw-canvas,
#overlay-canvas {
    position: absolute;
    top: 24px;
    left: 24px;
}

#draw-canvas {
    z-index: 2;
}

#overlay-canvas {
    z-index: 3;
    pointer-events: none;
}

.canvas-info {
    display: flex;
    justify-content: space-between;
    width: 100%;
    max-width: 600px;
    padding: 0 12px;
}

.canvas-info span {
    font-size: 13px;
    color: var(--text-secondary);
}

/* ===== Modal ===== */
.modal {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1000;
    background: rgba(0,0,0,0.5);
    align-items: center;
    justify-content: center;
    padding: 20px;
    backdrop-filter: blur(4px);
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    max-width: 700px;
    width: 100%;
    max-height: 90vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.modal-small {
    max-width: 400px;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 24px;
    border-bottom: 1px solid var(--border);
}

.modal-header h2 {
    font-size: 18px;
    font-weight: 700;
}

.modal-close {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--text-secondary);
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: all 0.2s;
}

.modal-close:hover {
    background: var(--bg);
    color: var(--text);
}

.modal-body {
    padding: 24px;
    overflow-y: auto;
    flex: 1;
}

/* Export Tabs */
.export-tabs {
    display: flex;
    gap: 8px;
    margin-bottom: 20px;
}

.export-tab {
    padding: 10px 20px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg);
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    transition: all 0.2s;
    font-family: inherit;
    flex: 1;
    text-align: center;
}

.export-tab:hover {
    border-color: var(--accent);
}

.export-tab.active {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

.export-preview {
    display: flex;
    justify-content: center;
    align-items: center;
    background: var(--bg);
    border-radius: var(--radius-sm);
    padding: 20px;
    margin-bottom: 20px;
    min-height: 300px;
    overflow: auto;
}

.export-preview canvas {
    max-width: 100%;
    max-height: 500px;
    box-shadow: var(--shadow);
}

.export-actions {
    display: flex;
    justify-content: center;
    gap: 12px;
}

/* Saves List */
.saves-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 16px;
}

.save-item {
    background: var(--bg);
    border-radius: var(--radius-sm);
    padding: 12px;
    cursor: pointer;
    transition: all 0.2s;
    border: 2px solid transparent;
}

.save-item:hover {
    border-color: var(--accent);
    transform: translateY(-2px);
}

.save-item canvas {
    width: 100%;
    aspect-ratio: 1;
    image-rendering: pixelated;
    border-radius: 6px;
    background: white;
    margin-bottom: 8px;
}

.save-item-name {
    font-size: 13px;
    font-weight: 600;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.save-item-date {
    font-size: 11px;
    color: var(--text-secondary);
    text-align: center;
    margin-top: 2px;
}

.save-item-actions {
    display: flex;
    gap: 4px;
    margin-top: 8px;
}

.save-item-actions button {
    flex: 1;
    padding: 4px 8px;
    font-size: 11px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: white;
    cursor: pointer;
    font-family: inherit;
}

.save-item-actions button:hover {
    background: var(--surface-hover);
}

.save-item-actions .btn-delete {
    color: var(--accent);
}

.save-item-actions .btn-delete:hover {
    background: rgba(255,107,107,0.1);
}

/* Save Name Input */
#save-name {
    width: 100%;
    padding: 10px 14px;
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-family: inherit;
    margin: 12px 0 20px;
}

#save-name:focus {
    outline: none;
    border-color: var(--accent);
}

/* Empty state */
.saves-empty {
    text-align: center;
    padding: 40px;
    color: var(--text-secondary);
}

/* ===== Responsive ===== */
@media (max-width: 900px) {
    .main {
        flex-direction: column;
    }

    .toolbar {
        width: 100%;
        max-height: none;
        position: static;
        flex-direction: row;
        flex-wrap: wrap;
    }

    .tool-section {
        flex: 1;
        min-width: 200px;
    }

    .palette-section {
        flex: 2;
        min-width: 300px;
    }

    .palette {
        grid-template-columns: repeat(10, 1fr);
    }

    .canvas-container {
        padding: 16px;
    }
}

@media (max-width: 600px) {
    .header {
        flex-direction: column;
        gap: 10px;
        padding: 12px 16px;
    }

    .logo h1 {
        font-size: 16px;
    }

    .main {
        padding: 12px;
    }

    .palette {
        grid-template-columns: repeat(8, 1fr);
    }
}

/* ===== Tooltip ===== */
.palette-color[title]:hover::after {
    content: attr(title);
    position: absolute;
    bottom: calc(100% + 6px);
    left: 50%;
    transform: translateX(-50%);
    background: var(--text);
    color: white;
    font-size: 11px;
    padding: 4px 8px;
    border-radius: 4px;
    white-space: nowrap;
    z-index: 10;
    pointer-events: none;
}
