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

:root {
    --primary-color: #6366f1;
    --primary-hover: #4f46e5;
    --secondary-color: #64748b;
    --secondary-hover: #475569;
    --background: #0f172a;
    --surface: #1e293b;
    --surface-hover: #334155;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --border-color: #334155;
    --success: #22c55e;
    --error: #ef4444;
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3), 0 2px 4px -2px rgba(0, 0, 0, 0.2);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.3), 0 4px 6px -4px rgba(0, 0, 0, 0.2);
    --radius: 12px;
    --radius-sm: 8px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: var(--background);
    color: var(--text-primary);
    min-height: 100vh;
    line-height: 1.6;
}

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

/* Header */
.header {
    text-align: center;
    padding: 2rem 1rem;
    background: linear-gradient(135deg, var(--surface) 0%, var(--background) 100%);
    border-bottom: 1px solid var(--border-color);
}

.logo {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    margin-bottom: 0.5rem;
}

.logo-img {
    width: 48px;
    height: 48px;
    border-radius: var(--radius-sm);
}

.header h1 {
    font-size: 1.75rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--primary-color), #a855f7);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.tagline {
    color: var(--text-secondary);
    font-size: 1rem;
}

/* Main Content */
.main-content {
    flex: 1;
    display: grid;
    grid-template-columns: 1fr 400px;
    gap: 2rem;
    padding: 2rem;
    max-width: 1400px;
    margin: 0 auto;
    width: 100%;
}

/* Video Section */
.video-section {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.video-container {
    position: relative;
    background: var(--surface);
    border-radius: var(--radius);
    overflow: hidden;
    aspect-ratio: 16 / 9;
    box-shadow: var(--shadow-lg);
}

#webcam {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

#overlay-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.video-placeholder {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    color: var(--text-secondary);
    background: var(--surface);
}

.video-placeholder svg {
    opacity: 0.5;
}

.video-placeholder.hidden {
    display: none;
}

/* Extraction Area */
.extraction-area {
    position: absolute;
    border: 2px dashed var(--primary-color);
    background: rgba(99, 102, 241, 0.1);
    cursor: move;
    display: none;
    z-index: 10;
}

.extraction-area.active {
    display: block;
}

.resize-handle {
    position: absolute;
    width: 12px;
    height: 12px;
    background: var(--primary-color);
    border-radius: 50%;
    z-index: 11;
}

.resize-handle.top-left {
    top: -6px;
    left: -6px;
    cursor: nwse-resize;
}

.resize-handle.top-right {
    top: -6px;
    right: -6px;
    cursor: nesw-resize;
}

.resize-handle.bottom-left {
    bottom: -6px;
    left: -6px;
    cursor: nesw-resize;
}

.resize-handle.bottom-right {
    bottom: -6px;
    right: -6px;
    cursor: nwse-resize;
}

/* Video Controls */
.video-controls {
    display: flex;
    gap: 0.75rem;
    flex-wrap: wrap;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1.25rem;
    font-size: 0.95rem;
    font-weight: 500;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.2s ease;
}

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

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

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

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

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

.btn-outline {
    background: transparent;
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.btn-outline:hover:not(:disabled) {
    background: var(--surface-hover);
}

.btn-small {
    padding: 0.5rem 0.875rem;
    font-size: 0.85rem;
}

/* Colors Section */
.colors-section {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.extracted-colors,
.color-history,
.saved-palettes {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 1.25rem;
    box-shadow: var(--shadow);
}

.extracted-colors h2,
.color-history h2,
.saved-palettes h2 {
    font-size: 1.1rem;
    font-weight: 600;
    margin-bottom: 1rem;
    color: var(--text-primary);
}

/* Color Count Control */
.color-count-control {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 1rem;
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.color-count-control input[type="range"] {
    flex: 1;
    height: 6px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--border-color);
    border-radius: 3px;
    outline: none;
}

.color-count-control input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    background: var(--primary-color);
    border-radius: 50%;
    cursor: pointer;
    transition: transform 0.2s ease;
}

.color-count-control input[type="range"]::-webkit-slider-thumb:hover {
    transform: scale(1.1);
}

#color-count-value {
    min-width: 1.5rem;
    text-align: center;
    font-weight: 600;
    color: var(--primary-color);
}

/* Color Palette */
.color-palette {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.color-placeholder {
    text-align: center;
    padding: 2rem;
    color: var(--text-secondary);
}

.color-swatch {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem;
    background: var(--background);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.2s ease;
}

.color-swatch:hover {
    transform: translateX(4px);
    background: var(--surface-hover);
}

.color-preview {
    width: 48px;
    height: 48px;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
    flex-shrink: 0;
}

.color-info {
    flex: 1;
    min-width: 0;
}

.color-hex {
    font-family: 'SF Mono', Monaco, 'Courier New', monospace;
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-primary);
}

.color-rgb {
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-top: 0.125rem;
}

.color-percentage {
    font-size: 0.75rem;
    color: var(--primary-color);
    font-weight: 500;
}

.copy-btn {
    padding: 0.5rem;
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s ease;
    opacity: 0;
}

.color-swatch:hover .copy-btn {
    opacity: 1;
}

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

/* History */
.history-header,
.palettes-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1rem;
}

.history-header h2,
.palettes-header h2 {
    margin-bottom: 0;
}

.history-palette {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    min-height: 48px;
}

.empty-history,
.empty-palettes {
    color: var(--text-secondary);
    font-size: 0.9rem;
    width: 100%;
    text-align: center;
    padding: 1rem;
}

.history-color {
    width: 40px;
    height: 40px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: transform 0.2s ease;
    box-shadow: var(--shadow);
    position: relative;
}

.history-color:hover {
    transform: scale(1.1);
}

.history-color::after {
    content: attr(data-hex);
    position: absolute;
    bottom: calc(100% + 8px);
    left: 50%;
    transform: translateX(-50%);
    background: var(--background);
    color: var(--text-primary);
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    font-family: monospace;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
    z-index: 100;
}

.history-color:hover::after {
    opacity: 1;
}

/* Saved Palettes */
.saved-palettes-container {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    max-height: 300px;
    overflow-y: auto;
}

.saved-palette {
    background: var(--background);
    border-radius: var(--radius-sm);
    padding: 0.75rem;
}

.palette-info {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.5rem;
}

.palette-name {
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.delete-palette-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0.25rem;
    transition: color 0.2s ease;
}

.delete-palette-btn:hover {
    color: var(--error);
}

.palette-colors {
    display: flex;
    gap: 0.25rem;
}

.palette-color {
    flex: 1;
    height: 32px;
    border-radius: 4px;
    cursor: pointer;
    transition: transform 0.2s ease;
}

.palette-color:first-child {
    border-radius: 4px 0 0 4px;
}

.palette-color:last-child {
    border-radius: 0 4px 4px 0;
}

.palette-color:hover {
    transform: scaleY(1.2);
}

/* Toast */
.toast {
    position: fixed;
    bottom: 2rem;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--surface);
    color: var(--text-primary);
    padding: 0.875rem 1.5rem;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    z-index: 1000;
    opacity: 0;
    transition: all 0.3s ease;
    font-size: 0.95rem;
}

.toast.show {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
}

.toast.success {
    border-left: 4px solid var(--success);
}

.toast.error {
    border-left: 4px solid var(--error);
}

/* Footer */
.footer {
    text-align: center;
    padding: 1.5rem;
    background: var(--surface);
    border-top: 1px solid var(--border-color);
}

.footer p {
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.footer a {
    color: var(--primary-color);
    text-decoration: none;
    font-weight: 500;
    transition: color 0.2s ease;
}

.footer a:hover {
    color: var(--primary-hover);
    text-decoration: underline;
}

/* Responsive Design */
@media (max-width: 1024px) {
    .main-content {
        grid-template-columns: 1fr;
        gap: 1.5rem;
        padding: 1.5rem;
    }

    .colors-section {
        order: 2;
    }
}

@media (max-width: 640px) {
    .header {
        padding: 1.5rem 1rem;
    }

    .logo {
        flex-direction: column;
        gap: 0.5rem;
    }

    .header h1 {
        font-size: 1.5rem;
    }

    .main-content {
        padding: 1rem;
    }

    .video-controls {
        justify-content: center;
    }

    .btn {
        padding: 0.625rem 1rem;
        font-size: 0.875rem;
    }

    .color-swatch {
        padding: 0.625rem;
    }

    .color-preview {
        width: 40px;
        height: 40px;
    }

    .copy-btn {
        opacity: 1;
    }
}

/* Scrollbar Styling */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: var(--background);
    border-radius: 4px;
}

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

::-webkit-scrollbar-thumb:hover {
    background: var(--secondary-color);
}
