/* ==========================================
   Cozy Packing Game Stylesheet
   ========================================== */

/* Design Tokens & Colors */
:root {
    --bg-main: #faf6f0;         /* Warm cream background */
    --bg-card: rgba(255, 255, 255, 0.9);
    --text-main: #4d4443;       /* Cozy deep warm brown */
    --text-muted: #8c7f7d;      /* Muted brown-gray */
    --color-primary: #db8b74;   /* Soft terracotta/peach */
    --color-primary-hover: #c97760;
    --color-secondary: #f0c397; /* Soft sand/gold */
    --color-secondary-hover: #e5b383;
    --color-success: #9cbda3;   /* Pastel sage green */
    --color-success-alpha: rgba(156, 189, 163, 0.3);
    --color-success-alpha-strong: rgba(156, 189, 163, 0.7);
    --color-error: #db9c9c;     /* Pastel rose/red */
    --color-error-alpha: rgba(219, 156, 156, 0.3);
    --color-error-alpha-strong: rgba(219, 156, 156, 0.7);
    
    --grid-bg: #ece5d8;         /* Empty grid cell background */
    --grid-border: #dfd5c4;
    
    /* Box cardboard colors for completion animation */
    --cardboard-light: #dfb28e;
    --cardboard-dark: #b68965;
    
    --shadow-soft: 0 10px 30px rgba(77, 68, 67, 0.05);
    --shadow-medium: 0 15px 40px rgba(77, 68, 67, 0.08);
    --shadow-strong: 0 20px 50px rgba(77, 68, 67, 0.12);
    --radius-sm: 8px;
    --radius-md: 16px;
    --radius-lg: 24px;
    
    --transition-cozy: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    --transition-bounce: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

/* Reset and Core Page Settings */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background-color: var(--bg-main);
    background-image: url('room.jpeg');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    background-attachment: fixed;
    color: var(--text-main);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow-x: hidden;
    position: relative;
    padding: 20px;
}

/* Cozy Background Decorations */
.cozy-bg-decorations {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    z-index: -1;
    pointer-events: none;
}

.decor-circle {
    position: absolute;
    border-radius: 50%;
    filter: blur(80px);
    opacity: 0.4;
}

.circle-1 {
    width: 450px;
    height: 450px;
    background-color: #f7e2ce;
    top: -100px;
    right: -100px;
}

.circle-2 {
    width: 500px;
    height: 500px;
    background-color: #e5ece6;
    bottom: -150px;
    left: -150px;
}

/* Fonts styling */
.comfortaa-font {
    font-family: 'Comfortaa', cursive, sans-serif;
    font-weight: 700;
}

/* Button UI */
.cozy-btn {
    font-family: 'Outfit', sans-serif;
    font-weight: 600;
    font-size: 15px;
    padding: 12px 24px;
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: var(--transition-cozy);
    box-shadow: 0 4px 10px rgba(0,0,0,0.02);
}

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

.btn-primary:hover {
    background-color: var(--color-primary-hover);
    transform: translateY(-2px);
    box-shadow: 0 6px 15px rgba(219, 139, 116, 0.3);
}

.btn-primary:active {
    transform: translateY(0);
}

.btn-secondary {
    background-color: white;
    color: var(--text-main);
    border: 2px solid #eadecc;
}

.btn-secondary:hover {
    background-color: #fcfbf9;
    border-color: var(--color-primary);
    transform: translateY(-2px);
}

.btn-secondary.compact {
    padding: 8px 16px;
    font-size: 14px;
    border-radius: 12px;
}

.btn-action {
    background-color: white;
    color: var(--text-main);
    border: 2px solid #eae2d5;
    padding: 10px 18px;
    font-size: 14px;
}

.btn-action:hover {
    border-color: var(--color-primary);
    transform: translateY(-2px);
}

.btn-danger-soft {
    background-color: #fcf7f7;
    color: #b86b6b;
    border: 2px solid #f2e2e2;
}

.btn-danger-soft:hover {
    background-color: #fceeee;
    border-color: var(--color-error);
    color: #a04f4f;
    transform: translateY(-2px);
}

/* Modal Overlay & Cards */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(77, 68, 67, 0.4);
    backdrop-filter: blur(8px);
    z-index: 100;
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.4s ease;
}

.modal-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.modal-card {
    background-color: var(--bg-card);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-strong);
    padding: 40px;
    max-width: 480px;
    width: 90%;
    text-align: center;
    position: relative;
    transform: scale(0.9);
    transition: transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    border: 1px solid rgba(255, 255, 255, 0.6);
}

.modal-overlay.active .modal-card {
    transform: scale(1);
}

/* Close button for modals */
.close-modal-btn {
    position: absolute;
    top: 20px;
    right: 20px;
    background: none;
    border: none;
    font-size: 28px;
    color: var(--text-muted);
    cursor: pointer;
    transition: var(--transition-cozy);
    line-height: 1;
}

.close-modal-btn:hover {
    color: var(--text-main);
    transform: rotate(90deg);
}

/* Nickname / Login Card styling */
.login-card h1 {
    font-size: 32px;
    color: var(--color-primary);
    margin-bottom: 12px;
}

.login-card .subtitle {
    color: var(--text-muted);
    font-size: 15px;
    line-height: 1.6;
    margin-bottom: 28px;
}

.cozy-icon-wrapper {
    background-color: #f7eade;
    width: 72px;
    height: 72px;
    border-radius: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 20px auto;
    color: var(--color-primary);
}

.cozy-box-icon {
    width: 36px;
    height: 36px;
}

.input-group {
    margin-bottom: 20px;
}

.input-group input {
    width: 100%;
    padding: 14px 20px;
    border: 2px solid #eae2d5;
    border-radius: var(--radius-md);
    font-family: 'Outfit', sans-serif;
    font-size: 16px;
    color: var(--text-main);
    background-color: white;
    outline: none;
    transition: var(--transition-cozy);
    text-align: center;
}

.input-group input:focus {
    border-color: var(--color-primary);
    box-shadow: 0 0 0 4px rgba(219, 139, 116, 0.15);
}

.error-msg {
    color: #c25f5f;
    font-size: 14px;
    margin-top: 10px;
    min-height: 20px;
}

/* Leaderboard Card & Table */
.leaderboard-card {
    max-width: 600px;
}

.modal-title {
    font-size: 26px;
    margin-bottom: 24px;
    color: var(--color-primary);
}

.leaderboard-table-container {
    max-height: 350px;
    overflow-y: auto;
    border-radius: var(--radius-md);
    border: 1px solid #ebdccb;
}

.leaderboard-table {
    width: 100%;
    border-collapse: collapse;
    text-align: left;
}

.leaderboard-table th {
    background-color: #f7eee2;
    padding: 14px 16px;
    font-weight: 600;
    color: var(--text-main);
    position: sticky;
    top: 0;
    font-size: 14px;
}

.leaderboard-table td {
    padding: 12px 16px;
    border-bottom: 1px solid #ebdccb;
    font-size: 15px;
    background-color: rgba(255, 255, 255, 0.5);
}

.leaderboard-table tr:last-child td {
    border-bottom: none;
}

.leaderboard-table tr.current-player-row td {
    background-color: #faebd7;
    font-weight: 600;
}

/* Leaderboard ranks visual badges */
.rank-badge {
    display: inline-flex;
    width: 26px;
    height: 26px;
    border-radius: 50%;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 13px;
}

.rank-1 { background-color: #ffd700; color: #574400; }
.rank-2 { background-color: #c0c0c0; color: #464646; }
.rank-3 { background-color: #cd7f32; color: #4a2806; }
.rank-other { background-color: #ece5d8; color: var(--text-muted); }

/* Main Game Layout Container */
#game-container {
    width: 100%;
    max-width: 1080px;
    background-color: var(--bg-card);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-medium);
    padding: 30px;
    border: 1px solid rgba(255, 255, 255, 0.7);
    display: flex;
    flex-direction: column;
    gap: 24px;
    animation: fadeIn 0.6s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.hidden {
    display: none !important;
}

/* Game Header (Stats Bar) */
.game-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 2px solid #ebdccb;
    padding-bottom: 16px;
    flex-wrap: wrap;
    gap: 16px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
}

.brand h2 {
    font-size: 24px;
    color: var(--color-primary);
}

.brand .logo-icon {
    font-size: 26px;
}

.player-stats {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}

.stat-badge {
    background-color: #f7ebd9;
    border-radius: 12px;
    padding: 8px 14px;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 6px;
    border: 1px solid #ebdccb;
}

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

.stat-value {
    font-weight: 700;
    color: var(--text-main);
}

#player-name {
    color: var(--color-primary);
}

/* Tooltip implementation */
.tooltip {
    position: relative;
}

/* Game Main Content Area */
.game-main {
    display: flex;
    gap: 30px;
    align-items: flex-start;
}

/* Left side: Box and Grid */
.box-section {
    flex: 1.2;
    display: flex;
    flex-direction: column;
    align-items: center;
    background-color: #f9f5ed;
    border-radius: var(--radius-lg);
    padding: 24px;
    border: 1px solid #ebdccb;
}

.level-indicator {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

#level-title {
    font-size: 18px;
    font-weight: 700;
}

.grid-badge {
    background-color: var(--text-muted);
    color: white;
    font-weight: 700;
    font-size: 12px;
    padding: 3px 8px;
    border-radius: 8px;
    letter-spacing: 0.5px;
}

/* Box Graphic Wrapping Grid */
.box-outer-wrapper {
    position: relative;
    padding: 30px;
    background-color: var(--cardboard-light);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-medium);
    border: 8px solid var(--cardboard-dark);
    margin-bottom: 24px;
    transition: var(--transition-cozy);
    display: inline-block;
}

/* Simulated Box Flaps for opening/closing visual */
.box-flap {
    position: absolute;
    background-color: var(--cardboard-light);
    border: 2px solid var(--cardboard-dark);
    transition: transform 0.6s ease-in-out, opacity 0.3s;
    z-index: 10;
    transform-origin: center;
    pointer-events: none;
    opacity: 0; /* Hidden during active play, shown on win */
}

.box-flap-top {
    top: -20px; left: 10px; right: 10px; height: 35px;
    transform-origin: bottom;
    transform: rotateX(85deg);
}
.box-flap-bottom {
    bottom: -20px; left: 10px; right: 10px; height: 35px;
    transform-origin: top;
    transform: rotateX(-85deg);
}
.box-flap-left {
    left: -20px; top: 10px; bottom: 10px; width: 35px;
    transform-origin: right;
    transform: rotateY(-85deg);
}
.box-flap-right {
    right: -20px; top: 10px; bottom: 10px; width: 35px;
    transform-origin: left;
    transform: rotateY(85deg);
}

/* Closed state animations for flaps */
.box-outer-wrapper.completed {
    transform: scale(0.95);
    background-color: var(--cardboard-dark);
}

.box-outer-wrapper.completed .box-flap {
    opacity: 1;
    background-color: var(--cardboard-dark);
}

.box-outer-wrapper.completed .box-flap-top {
    transform: rotateX(0deg) translateY(10px);
}
.box-outer-wrapper.completed .box-flap-bottom {
    transform: rotateX(0deg) translateY(-10px);
}
.box-outer-wrapper.completed .box-flap-left {
    transform: rotateY(0deg) translateX(10px);
}
.box-outer-wrapper.completed .box-flap-right {
    transform: rotateY(0deg) translateX(-10px);
}

/* Box Grid Container */
.box-grid-container {
    display: grid;
    gap: 4px;
    background-color: #dfd6c5;
    padding: 6px;
    border-radius: var(--radius-sm);
    position: relative;
    /* Grid dimensions will be set dynamically via JS */
}

/* Empty Cell Styling */
.grid-cell {
    background-color: var(--grid-bg);
    border: 1px dashed var(--grid-border);
    border-radius: 4px;
    width: 100%;
    height: 100%;
    transition: background-color 0.2s ease;
}

/* Placing indicator / Drag highlight classes */
.grid-cell.highlight-valid {
    background-color: var(--color-success-alpha) !important;
    border-color: var(--color-success-alpha-strong) !important;
}

.grid-cell.highlight-invalid {
    background-color: var(--color-error-alpha) !important;
    border-color: var(--color-error-alpha-strong) !important;
}

/* Control Buttons Panel under grid */
.control-panel {
    display: flex;
    gap: 10px;
    justify-content: center;
    width: 100%;
}

/* Right side: Inventory List */
.inventory-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-self: stretch;
}

.section-title {
    font-size: 18px;
    margin-bottom: 8px;
}

.inventory-tip {
    color: var(--text-muted);
    font-size: 13px;
    line-height: 1.5;
    margin-bottom: 20px;
}

.inventory-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 16px;
    background-color: #f9f5ed;
    border-radius: var(--radius-lg);
    padding: 20px;
    border: 1px solid #ebdccb;
    flex: 1;
    overflow-y: auto;
    max-height: 440px;
    align-content: start;
}

/* Draggable Items */
.drag-item {
    background-color: transparent;
    border: 2px dashed #eae2d5;
    border-radius: var(--radius-md);
    padding: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    cursor: grab;
    user-select: none;
    transition: var(--transition-cozy);
    position: relative;
}

.drag-item:hover {
    border-color: var(--color-primary);
    transform: translateY(-3px);
}

.drag-item:active {
    cursor: grabbing;
}

.drag-item.selected {
    border-color: var(--color-primary);
    background-color: transparent;
    box-shadow: 0 0 0 3px rgba(219, 139, 116, 0.15);
}

.drag-item.dragging {
    opacity: 0.4;
    transform: scale(0.9);
}

/* The Mini-Grid inside Inventory Cards */
.item-mini-grid {
    display: grid;
    gap: 2px;
    margin-bottom: 8px;
    pointer-events: none;
}

.mini-cell {
    background-color: transparent;
    border: 1.5px solid var(--color-secondary);
    border-radius: 3px;
    width: 14px;
    height: 14px;
}

.drag-item.selected .mini-cell {
    border-color: var(--color-primary);
}

/* Custom Item styles (allows distinct looks per level item) */
/* We define variables --item-color in JS to style them uniquely */
.item-block-visual {
    background-color: transparent;
    border: 2px dashed var(--item-color, var(--color-secondary));
    border-radius: 4px;
    transition: border-color 0.3s;
}

/* Placed item on the main grid */
.placed-item {
    position: absolute;
    z-index: 5;
    border-radius: var(--radius-sm);
    cursor: pointer;
    box-shadow: 0 4px 10px rgba(77, 68, 67, 0.08);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border: 2px solid rgba(0,0,0,0.1);
    width: 100%;
    height: 100%;
    transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    overflow: hidden;
}

.placed-item:hover {
    transform: scale(0.98);
    filter: brightness(1.05);
}

.placed-item-label {
    font-size: 11px;
    font-weight: 700;
    color: white;
    text-shadow: 0 1px 3px rgba(0,0,0,0.3);
    text-align: center;
    padding: 4px;
    word-break: break-all;
    user-select: none;
    pointer-events: none;
}

.placed-item-emoji {
    font-size: 18px;
    margin-bottom: 2px;
    user-select: none;
    pointer-events: none;
}

/* Drag Avatar (floating element under cursor) */
.drag-avatar-helper {
    position: fixed;
    pointer-events: none;
    z-index: 1000;
    opacity: 0.9;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-strong);
    display: grid;
    gap: 3px;
    border: 2px solid rgba(0,0,0,0.15);
    transform: translate(-50%, -50%); /* Center on cursor */
}

/* Screen / Element Shake (for error placement) */
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    20%, 60% { transform: translateX(-6px); }
    40%, 80% { transform: translateX(6px); }
}

.shake {
    animation: shake 0.4s ease;
}

/* Level Completion Card details */
.completion-card {
    background-color: var(--bg-card);
}

.completion-box-animation {
    position: relative;
    width: 120px;
    height: 120px;
    margin: 0 auto 20px auto;
    font-size: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    animation: pulse 1.5s infinite;
}

@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.08); }
}

.sparkle {
    position: absolute;
    font-size: 24px;
    opacity: 0;
    animation: sparkleAnim 2s infinite;
}

.s1 { top: 10px; left: 10px; animation-delay: 0s; }
.s2 { top: 0px; right: 10px; animation-delay: 0.6s; }
.s3 { bottom: 10px; left: 30px; animation-delay: 1.2s; }

@keyframes sparkleAnim {
    0%, 100% { opacity: 0; transform: translateY(0) scale(0.7); }
    50% { opacity: 1; transform: translateY(-10px) scale(1.2); }
}

.completion-title {
    color: var(--color-success);
    font-size: 26px;
    margin-bottom: 12px;
}

.completion-text {
    color: var(--text-muted);
    font-size: 15px;
    line-height: 1.6;
    margin-bottom: 24px;
}

.completion-stats {
    background-color: #f7eee2;
    border-radius: var(--radius-md);
    padding: 16px;
    margin-bottom: 28px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.c-stat {
    display: flex;
    justify-content: space-between;
    font-size: 15px;
}

.c-stat-label {
    color: var(--text-muted);
}

.c-stat-val {
    font-weight: 700;
}

.next-level-action-btn {
    width: 100%;
}

/* Custom Scrollbars */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: #e3d9cc;
    border-radius: 10px;
}

::-webkit-scrollbar-thumb:hover {
    background: #d4c7b6;
}

/* Dialog Layout for Rules, Progress, and Support Modals */
.dialog-card {
    max-width: 650px !important;
}

.dialog-content {
    display: flex;
    gap: 20px;
    align-items: center;
    margin-top: 15px;
}

.dialog-text {
    flex: 1.2;
    font-size: 15px;
    line-height: 1.6;
    color: var(--text-main);
}

.dialog-text p {
    margin-bottom: 12px;
}

.dialog-image {
    flex: 0.8;
    display: flex;
    justify-content: center;
    align-items: center;
}

.dialog-image img {
    max-width: 100%;
    max-height: 220px;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    object-fit: contain;
}

.progress-dialog-stats {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-top: 15px;
}

.progress-dialog-stat {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #fcf8f2;
    padding: 10px 16px;
    border-radius: 8px;
    border: 1px solid #ebdccb;
}

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

.stat-val-highlight {
    font-size: 18px;
    font-weight: 700;
    color: var(--color-primary);
}

.support-action-btn {
    text-decoration: none;
    margin-top: 12px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 12px;
    font-size: 16px;
}

/* ==========================================
   Responsive Media Queries
   ========================================== */

@media (max-width: 900px) {
    #game-container {
        padding: 20px;
    }
    
    .game-main {
        flex-direction: column;
        align-items: stretch;
        gap: 24px;
    }
    
    .box-section {
        flex: none;
    }

    .inventory-section {
        flex: none;
    }
    
    .inventory-list {
        max-height: 250px;
    }
}

@media (max-width: 600px) {
    body {
        padding: 10px;
    }

    #game-container {
        padding: 15px;
        gap: 16px;
    }

    .game-header {
        flex-direction: column;
        align-items: center;
        text-align: center;
        gap: 12px;
    }

    .player-stats {
        justify-content: center;
        width: 100%;
    }

    .stat-badge {
        padding: 6px 10px;
        font-size: 13px;
    }

    .header-actions {
        width: 100%;
        display: flex;
        justify-content: center;
    }
    
    .header-actions button {
        width: 100%;
    }

    .box-outer-wrapper {
        padding: 15px;
    }

    .control-panel {
        flex-wrap: wrap;
    }

    .control-panel button {
        flex: 1 1 45%;
        font-size: 13px;
        padding: 8px 12px;
    }

    .brand {
        flex-direction: column !important;
        align-items: center;
        width: 100%;
        gap: 8px !important;
    }

    .top-left-actions {
        margin-left: 0 !important;
        justify-content: center;
        width: 100%;
    }

    .dialog-content {
        flex-direction: column-reverse;
        gap: 15px;
    }

    .dialog-image img {
        max-height: 180px;
    }
}
