:root {
    --primary-dark: #2d1b36;
    --primary-purple: #5c2c70;
    --primary-orange: #eb532b;

    /* Mapped for compatibility */
    --batik-brown: #5c2c70;
    /* Mapping old var to new purple */
    --batik-gold: #eb532b;
    /* Mapping old var to new orange */

    --text-main: #1e293b;
    --text-muted: #64748b;
    --white: #ffffff;

    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1);

    --font-heading: 'Playfair Display', serif;
    --font-body: 'Outfit', sans-serif;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: var(--font-body);
    background: linear-gradient(135deg, #5c2c70 0%, #eb532b 100%);
    background-attachment: fixed;
    /* Keep gradient nice during scroll */
    color: var(--text-main);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Typography Overrides for Dark Background */
h1,
h2,
h3 {
    font-family: var(--font-heading);
    color: var(--primary-dark);
}

/* Header Text specifically needs to be white/light if it sits directly on body */
body>.app-container>header h1,
body>.app-container>header p {
    color: var(--white);
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

body>.app-container>header {
    border-bottom: 2px solid rgba(255, 255, 255, 0.2);
}

/* Layout */
.app-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
    width: 100%;
}


/* --- Global Header --- */
/* --- Global Header --- */
.site-header {
    background: linear-gradient(90deg, #5c2c70, #eb532b);
    border-bottom: 2px solid rgba(255, 255, 255, 0.1);
    padding: 0.75rem 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 1000;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
    flex-wrap: wrap;
    gap: 1rem;
}

.site-brand {
    text-decoration: none;
    display: flex;
    flex-direction: column;
}

.brand-title {
    font-family: var(--font-heading);
    font-size: 1.5rem;
    font-weight: 700;
    color: white;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
    line-height: 1.2;
}

.brand-subtitle {
    font-family: var(--font-body);
    font-size: 0.85rem;
    color: rgba(255, 255, 255, 0.9);
    letter-spacing: 0.5px;
}

.site-nav ul {
    list-style: none;
    display: flex;
    gap: 1.5rem;
    padding: 0;
    margin: 0;
}

.site-nav a {
    text-decoration: none;
    color: rgba(255, 255, 255, 0.8);
    font-weight: 600;
    font-size: 0.95rem;
    padding: 0.5rem 0;
    position: relative;
    transition: color 0.2s;
}

.site-nav a:hover {
    color: white;
    text-shadow: 0 0 5px rgba(255, 255, 255, 0.5);
}

.site-nav a.active {
    color: white;
}

.site-nav a.active::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 3px;
    background: #fef3c7;
    /* Batik Cream */
    border-radius: 2px;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
}

/* Footer (Gradient) */
.app-footer {
    background: linear-gradient(90deg, #5c2c70, #eb532b);
    color: white;
    text-align: center;
    padding: 1rem;
    font-size: 0.85rem;
    z-index: 20;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.15);
    width: 100%;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

/* Mobile Nav adjustment */
@media (max-width: 600px) {
    .site-header {
        flex-direction: column;
        align-items: flex-start;
    }

    .site-nav {
        width: 100%;
        overflow-x: auto;
    }

    .site-nav ul {
        gap: 1rem;
        padding-bottom: 4px;
        /* Space for scrollbar if any */
    }
}

/* Header Legacy Removal if exists */
header.legacy-header {
    display: none;
}


main.puzzle-layout {
    display: grid;
    grid-template-columns: 1fr 350px;
    /* Game area takes rest, sidebar 350px */
    gap: 2rem;
    align-items: start;
}

/* Game Section */
.game-section {
    background: var(--white);
    padding: 2rem;
    border-radius: 1rem;
    box-shadow: var(--shadow-lg);
    border: 1px solid rgba(0, 0, 0, 0.05);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 500px;
}

.puzzle-wrapper {
    position: relative;
    padding: 10px;
    background: #f8fafc;
    border-radius: 8px;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.05);
    width: 100%;
    max-width: 520px;
    /* Slightly larger than container max */
    margin: 0 auto;
    display: flex;
    justify-content: center;
}

.puzzle-container {
    display: grid;
    /* Grid template columns/rows set by JS */
    gap: 2px;
    /* Small gap between pieces */
    background-color: #cbd5e1;
    border: 2px solid var(--primary-purple);
    position: relative;
    width: 100%;
    /* Remove max-width here, control in wrapper */
    aspect-ratio: 1 / 1;
}

.puzzle-piece {
    background-repeat: no-repeat;
    /* background-size set via JS */
    cursor: grab;
    transition: transform 0.2s, box-shadow 0.2s, filter 0.2s;
    position: relative;
    overflow: hidden;
}

.puzzle-piece:hover {
    filter: brightness(1.1);
    z-index: 10;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}

.puzzle-piece.dragging {
    opacity: 0.8;
    cursor: grabbing;
    z-index: 20;
    transform: scale(1.05);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}

.puzzle-piece.correct {
    /* Optional hint for debugging or specific easy modes, usually kept hidden to not support cheating */
}

/* Panels */
.control-panel {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.card {
    background: var(--white);
    padding: 1.5rem;
    border-radius: 1rem;
    box-shadow: var(--shadow-md);
    border: 1px solid rgba(0, 0, 0, 0.05);
}

.control-card h2 {
    font-size: 1.25rem;
    margin-bottom: 1rem;
    border-bottom: 2px solid var(--batik-gold);
    padding-bottom: 0.5rem;
    display: inline-block;
}

.preview-area {
    width: 100%;
    aspect-ratio: 1;
    background: #f1f5f9;
    border-radius: 0.5rem;
    overflow: hidden;
    position: relative;
    margin-bottom: 1rem;
    border: 2px dashed #cbd5e1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.preview-area img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    /* or cover depending on preference, contain shows full image */
}

.preview-label {
    position: absolute;
    bottom: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.6);
    color: white;
    font-size: 0.75rem;
    padding: 2px 8px;
    border-top-left-radius: 6px;
}

.actions {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* Custom File Input */
.file-input-wrapper input {
    display: none;
}

.file-input-wrapper label {
    width: 100%;
    text-align: center;
    cursor: pointer;
}

.asset-picker {
    border: 1px dashed #cbd5e1;
    border-radius: 0.5rem;
    padding: 0.75rem;
    background: #f8fafc;
}

.asset-picker.hidden {
    display: none;
}

.asset-picker-hint {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
}

.asset-list {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.5rem;
    max-height: 220px;
    overflow-x: hidden;
    overflow-y: auto;
}

.asset-thumb {
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    border: 2px solid transparent;
    border-radius: 0.4rem;
    overflow: hidden;
    cursor: pointer;
    background: white;
    padding: 0;
    line-height: 0;
}

.asset-thumb img {
    display: block;
    width: 100%;
    max-width: 100%;
    height: 72px;
    aspect-ratio: 1 / 1;
    object-fit: cover;
    object-position: center;
}

.asset-thumb span {
    display: block;
    font-size: 0.7rem;
    line-height: 1.2;
    padding: 0.25rem;
    text-align: center;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.asset-thumb.active {
    border-color: var(--primary-purple);
}

/* Buttons */
.btn {
    padding: 0.75rem 1.5rem;
    border-radius: 0.5rem;
    border: none;
    font-family: var(--font-body);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-block;
    text-align: center;
}

.btn.primary {
    background: var(--primary-purple);
    color: white;
}

.btn.primary:hover {
    background: #4a1f5a;
    /* Darker purple */
}

.btn.secondary {
    background: var(--primary-orange);
    color: white;
}

.btn.secondary:hover {
    background: #c2411e;
    /* Darker orange */
}

.btn.outline {
    background: transparent;
    border: 2px solid var(--primary-purple);
    color: var(--primary-purple);
    display: block;
}

.btn.outline:hover {
    background: rgba(92, 44, 112, 0.1);
}

.btn.text {
    background: none;
    color: var(--text-muted);
}

.btn.text:hover {
    color: var(--text-main);
    text-decoration: underline;
}

.difficulty-control {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

select {
    padding: 0.5rem;
    border-radius: 0.5rem;
    border: 1px solid #cbd5e1;
    font-family: var(--font-body);
}

/* Stats */
.status-card {
    display: flex;
    justify-content: space-around;
    background: var(--primary-purple);
    color: white;
}

.stat-box {
    text-align: center;
}

.stat-label {
    font-size: 0.8rem;
    opacity: 0.8;
    display: block;
    margin-bottom: 0.25rem;
}

.stat-value {
    font-size: 1.5rem;
    font-weight: 700;
    font-family: monospace;
}

/* Leaderboard */
.leaderboard-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.leaderboard-list {
    list-style: none;
    max-height: 200px;
    overflow-y: auto;
}

#leaderboard-list li {
    display: flex;
    justify-content: space-between;
    padding: 0.5rem 0;
    border-bottom: 1px solid #f1f5f9;
    font-size: 0.9rem;
}

#leaderboard-list li:last-child {
    border-bottom: none;
}

.score-badge {
    background: var(--batik-gold);
    color: white;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.8rem;
    font-weight: bold;
}

.icon-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 1rem;
    opacity: 0.6;
}

.icon-btn:hover {
    opacity: 1;
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
    backdrop-filter: blur(4px);
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: white;
    padding: 2rem;
    border-radius: 1rem;
    width: 90%;
    max-width: 400px;
    text-align: center;
    box-shadow: var(--shadow-lg);
    animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
    from {
        transform: translateY(20px);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.result-summary {
    display: flex;
    justify-content: center;
    gap: 2rem;
    margin: 1.5rem 0;
    background: #f8fafc;
    padding: 1rem;
    border-radius: 0.5rem;
}

.res-item {
    display: flex;
    flex-direction: column;
}

.save-score-form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.save-score-form input {
    padding: 0.75rem;
    border: 1px solid #cbd5e1;
    border-radius: 0.5rem;
    text-align: center;
}

.full-width {
    width: 100%;
}

/* Responsive */
@media (max-width: 850px) {
    main.puzzle-layout {
        grid-template-columns: 1fr;
    }

    .game-section {
        min-height: auto;
    }
}

/* Piece Number Styling */
.piece-number {
    position: absolute;
    top: 4px;
    left: 4px;
    background-color: rgba(255, 255, 255, 0.8);
    color: var(--primary-dark);
    font-family: monospace;
    font-size: 1rem;
    font-weight: 700;
    line-height: 1;
    padding: 4px 6px;
    border-radius: 4px;
    pointer-events: none;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    z-index: 30;
    backdrop-filter: blur(2px);
    border: 1px solid rgba(0, 0, 0, 0.1);
}
