/* ── Design Tokens ─────────────────────────────────────────── */
:root {
    /* Colors — dark theme with purple/indigo accent */
    --bg-primary: #0a0a0f;
    --bg-secondary: #12121a;
    --bg-tertiary: #1a1a28;
    --bg-card: rgba(26, 26, 40, 0.7);
    --bg-glass: rgba(26, 26, 40, 0.45);

    --text-primary: #e8e8f0;
    --text-secondary: #9898b0;
    --text-muted: #5a5a78;

    --accent-1: #818cf8;  /* Indigo */
    --accent-2: #c084fc;  /* Purple */
    --accent-3: #f472b6;  /* Pink */
    --accent-gradient: linear-gradient(135deg, #818cf8, #c084fc);
    --accent-glow: rgba(129, 140, 248, 0.25);

    --border-subtle: rgba(255, 255, 255, 0.06);
    --border-active: rgba(129, 140, 248, 0.4);

    --point-color: rgba(129, 140, 248, 0.7);
    --point-selected: #f472b6;
    --point-hover: #fbbf24;
    --selection-fill: rgba(129, 140, 248, 0.1);
    --selection-stroke: rgba(192, 132, 252, 0.6);

    /* Sizing */
    --header-height: 56px;
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 16px;
    --radius-xl: 20px;

    /* Transitions */
    --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
    --duration-fast: 0.15s;
    --duration-normal: 0.25s;
}

/* ── Reset ─────────────────────────────────────────────────── */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
}

/* ── Header ────────────────────────────────────────────────── */
#app-header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-subtle);
    position: relative;
    z-index: 10;
}

#app-header::after {
    content: '';
    position: absolute;
    bottom: -1px;
    left: 0;
    right: 0;
    height: 1px;
    background: var(--accent-gradient);
    opacity: 0.3;
}

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

.logo {
    display: flex;
    align-items: center;
    animation: logo-pulse 3s ease-in-out infinite;
}

@keyframes logo-pulse {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.05); opacity: 0.85; }
}

#app-header h1 {
    font-size: 17px;
    font-weight: 600;
    letter-spacing: -0.02em;
    background: var(--accent-gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.badge {
    font-size: 10px;
    font-weight: 600;
    font-family: 'JetBrains Mono', monospace;
    padding: 3px 8px;
    border-radius: 20px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-subtle);
    color: var(--text-secondary);
    letter-spacing: 0.04em;
}

/* ── Model Selector ───────────────────────────────────────── */
.model-selector-wrapper,
.dataset-selector-wrapper {
    display: flex;
    align-items: center;
    gap: 8px;
}

.model-label {
    font-size: 10px;
    font-weight: 500;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

.select-container {
    position: relative;
    display: flex;
    align-items: center;
}

.select-container select {
    appearance: none;
    -webkit-appearance: none;
    font-family: 'JetBrains Mono', monospace;
    font-size: 11px;
    font-weight: 600;
    color: var(--accent-1);
    background: var(--bg-tertiary);
    border: 1px solid var(--border-subtle);
    border-radius: 20px;
    padding: 5px 28px 5px 12px;
    cursor: pointer;
    outline: none;
    transition: all var(--duration-normal) var(--ease-out);
    letter-spacing: 0.02em;
}

.select-container select:hover {
    border-color: var(--border-active);
    background: rgba(129, 140, 248, 0.08);
}

.select-container select:focus {
    border-color: var(--accent-1);
    box-shadow: 0 0 0 2px rgba(129, 140, 248, 0.15);
}

.select-container select option {
    background: var(--bg-secondary);
    color: var(--text-primary);
    padding: 8px;
}

.select-chevron {
    position: absolute;
    right: 10px;
    color: var(--text-muted);
    pointer-events: none;
    transition: transform var(--duration-fast) var(--ease-out);
}

.select-container:hover .select-chevron {
    color: var(--accent-1);
}

/* Model loading state */
.model-loading .select-container select {
    opacity: 0.5;
    pointer-events: none;
}

.model-loading .select-container::after {
    content: '';
    position: absolute;
    left: -4px;
    right: -4px;
    top: -4px;
    bottom: -4px;
    border-radius: 24px;
    border: 2px solid transparent;
    border-top-color: var(--accent-2);
    animation: spin 0.8s linear infinite;
    pointer-events: none;
}

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

.stat-pill {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 12px;
    border-radius: 20px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-subtle);
    font-size: 12px;
    transition: border-color var(--duration-normal) var(--ease-out);
}

.stat-pill:hover {
    border-color: var(--border-active);
}

.stat-label {
    color: var(--text-muted);
    font-weight: 500;
}

.stat-value {
    color: var(--accent-1);
    font-weight: 600;
    font-family: 'JetBrains Mono', monospace;
}

#stat-selected .stat-value {
    color: var(--accent-3);
}

/* ── Main Layout ───────────────────────────────────────────── */
#app-main {
    display: flex;
    height: calc(100vh - var(--header-height));
}

/* ── Scatter Section ───────────────────────────────────────── */
#scatter-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    border-right: 1px solid var(--border-subtle);
}

.section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-subtle);
    background: var(--bg-secondary);
}

.section-header h2 {
    font-size: 13px;
    font-weight: 600;
    letter-spacing: -0.01em;
}

.section-header h2 .subtle {
    color: var(--text-muted);
    font-weight: 400;
}

/* ── Toolbar ───────────────────────────────────────────────── */
.toolbar {
    display: flex;
    align-items: center;
    gap: 2px;
}

.tool-btn {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 5px 10px;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-secondary);
    font-family: 'Inter', sans-serif;
    font-size: 11px;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--duration-fast) var(--ease-out);
    user-select: none;
}

.tool-btn:hover {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

.tool-btn.active {
    background: rgba(129, 140, 248, 0.12);
    border-color: var(--border-active);
    color: var(--accent-1);
}

.separator {
    width: 1px;
    height: 20px;
    background: var(--border-subtle);
    margin: 0 4px;
}

/* ── Projection Selector ───────────────────────────────────── */
.proj-selector {
    display: flex;
    align-items: center;
    background: var(--bg-primary);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    padding: 2px;
    gap: 2px;
}

.proj-btn {
    padding: 4px 11px;
    border: none;
    border-radius: calc(var(--radius-sm) - 2px);
    background: transparent;
    color: var(--text-muted);
    font-family: 'JetBrains Mono', monospace;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.03em;
    cursor: pointer;
    transition: all var(--duration-fast) var(--ease-out);
    white-space: nowrap;
    user-select: none;
}

.proj-btn:hover:not(.active) {
    color: var(--text-primary);
    background: var(--bg-tertiary);
}

.proj-btn.active {
    background: var(--accent-gradient);
    color: #fff;
    box-shadow: 0 2px 8px rgba(129, 140, 248, 0.35);
}

.proj-btn.active[data-proj="pca"] {
    background: linear-gradient(135deg, #34d399, #059669);
    box-shadow: 0 2px 8px rgba(52, 211, 153, 0.35);
}

.proj-btn.active[data-proj="umap"] {
    background: linear-gradient(135deg, #fb923c, #f472b6);
    box-shadow: 0 2px 8px rgba(251, 146, 60, 0.35);
}

.proj-btn.active[data-proj="pumap"] {
    background: linear-gradient(135deg, #2dd4bf, #38bdf8);
    box-shadow: 0 2px 8px rgba(45, 212, 191, 0.35);
}

/* Transitioning state during projection switch */
.proj-transitioning #scatter-canvas {
    opacity: 0.6;
    transition: opacity 0.15s;
}


/* ── Canvas Container ──────────────────────────────────────── */
#canvas-container {
    flex: 1;
    position: relative;
    overflow: hidden;
    background: var(--bg-primary);
    /* Subtle grid pattern */
    background-image:
        radial-gradient(circle at 50% 50%, rgba(129, 140, 248, 0.03) 0%, transparent 70%),
        linear-gradient(var(--border-subtle) 1px, transparent 1px),
        linear-gradient(90deg, var(--border-subtle) 1px, transparent 1px);
    background-size: 100% 100%, 40px 40px, 40px 40px;
}

#scatter-canvas,
#selection-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

#selection-canvas {
    cursor: grab;
}

.canvas-footer {
    padding: 6px 16px;
    border-top: 1px solid var(--border-subtle);
    background: var(--bg-secondary);
}

.hint {
    font-size: 11px;
    color: var(--text-muted);
}

.hint kbd {
    display: inline-block;
    padding: 1px 5px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    font-weight: 600;
    color: var(--text-secondary);
    background: var(--bg-tertiary);
    border: 1px solid var(--border-subtle);
    border-radius: 3px;
    margin: 0 1px;
    line-height: 1.4;
}

/* ── Tooltip ───────────────────────────────────────────────── */
.tooltip {
    position: absolute;
    pointer-events: none;
    z-index: 100;
    background: var(--bg-glass);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--border-active);
    border-radius: var(--radius-md);
    padding: 6px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
    transition: opacity var(--duration-fast) var(--ease-out);
}

.tooltip.hidden {
    opacity: 0;
}

.tooltip img {
    width: 120px;
    height: 120px;
    object-fit: cover;
    border-radius: var(--radius-sm);
    display: block;
}

.tooltip-label {
    margin-top: 4px;
    font-size: 10px;
    color: var(--text-secondary);
    text-align: center;
    font-family: 'JetBrains Mono', monospace;
}

/* ── Gallery Section ───────────────────────────────────────── */
#gallery-section {
    width: 360px;
    min-width: 300px;
    display: flex;
    flex-direction: column;
    background: var(--bg-secondary);
}

.gallery-count {
    font-size: 11px;
    color: var(--text-muted);
    font-family: 'JetBrains Mono', monospace;
}

.gallery-placeholder {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    color: var(--text-muted);
    text-align: center;
    padding: 20px;
}

.placeholder-icon {
    opacity: 0.3;
    animation: float 4s ease-in-out infinite;
}

@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-8px); }
}

.gallery-placeholder p {
    font-size: 13px;
    line-height: 1.6;
}

/* ── Gallery Grid ──────────────────────────────────────────── */
.gallery-grid {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    align-content: start;
}

.gallery-grid::-webkit-scrollbar {
    width: 6px;
}

.gallery-grid::-webkit-scrollbar-track {
    background: transparent;
}

.gallery-grid::-webkit-scrollbar-thumb {
    background: var(--bg-tertiary);
    border-radius: 3px;
}

.gallery-grid::-webkit-scrollbar-thumb:hover {
    background: rgba(129, 140, 248, 0.3);
}

.gallery-item {
    position: relative;
    aspect-ratio: 1;
    border-radius: var(--radius-sm);
    overflow: hidden;
    cursor: pointer;
    border: 1px solid var(--border-subtle);
    transition: all var(--duration-normal) var(--ease-out);
    animation: gallery-in 0.35s var(--ease-out) both;
}

.gallery-item:hover {
    border-color: var(--accent-1);
    transform: scale(1.04);
    box-shadow: 0 4px 20px rgba(129, 140, 248, 0.15);
}

.gallery-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform var(--duration-normal) var(--ease-out);
}

.gallery-item:hover img {
    transform: scale(1.1);
}

.gallery-item .item-label {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 4px 6px;
    background: linear-gradient(transparent, rgba(0,0,0,0.7));
    font-size: 9px;
    color: rgba(255,255,255,0.8);
    font-family: 'JetBrains Mono', monospace;
    opacity: 0;
    transition: opacity var(--duration-fast);
}

.gallery-item:hover .item-label {
    opacity: 1;
}

@keyframes gallery-in {
    from {
        opacity: 0;
        transform: scale(0.85);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* ── Modal ─────────────────────────────────────────────────── */
.modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity var(--duration-normal) var(--ease-out);
}

.modal.hidden {
    opacity: 0;
    pointer-events: none;
}

.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.75);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}

.modal-content {
    position: relative;
    max-width: 80vw;
    max-height: 80vh;
    border-radius: var(--radius-lg);
    overflow: hidden;
    border: 1px solid var(--border-active);
    box-shadow: 0 24px 80px rgba(0, 0, 0, 0.6);
    animation: modal-in 0.3s var(--ease-out);
}

@keyframes modal-in {
    from {
        transform: scale(0.9);
        opacity: 0;
    }
}

.modal-content img {
    display: block;
    max-width: 80vw;
    max-height: 70vh;
    object-fit: contain;
}

.modal-close {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: none;
    background: rgba(0, 0, 0, 0.5);
    color: white;
    font-size: 20px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background var(--duration-fast);
    backdrop-filter: blur(8px);
}

.modal-close:hover {
    background: rgba(244, 114, 182, 0.6);
}

.modal-info {
    padding: 10px 16px;
    background: var(--bg-tertiary);
    display: flex;
    justify-content: space-between;
    font-size: 12px;
    color: var(--text-secondary);
    font-family: 'JetBrains Mono', monospace;
}

/* ── Loading State ─────────────────────────────────────────── */
.loading-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: var(--bg-primary);
    z-index: 50;
    gap: 16px;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--bg-tertiary);
    border-top-color: var(--accent-1);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.loading-text {
    font-size: 13px;
    color: var(--text-muted);
}

/* ── Responsive ────────────────────────────────────────────── */
@media (max-width: 768px) {
    #app-main {
        flex-direction: column;
    }

    #gallery-section {
        width: 100%;
        height: 40%;
    }

    .header-right {
        display: none;
    }
}
