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

body {
    min-height: 100vh;
    min-height: calc(var(--vh, 1vh) * 100);
    font-family: 'Segoe UI', Tahoma, Verdana, sans-serif;
    color: #333;
    background-color: #ffffff;
    overflow: hidden;
    height: calc(var(--vh, 1vh) * 100);
    height: 100dvh;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* --- Fit game to viewport, no scrolling or overflow --- */
html, body {
    height: 100vh;
    height: 100dvh;
    width: 100vw;
    overflow: hidden;
    margin: 0;
    padding: 0;
}

#game-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: calc(var(--vh, 1vh) * 100);
    height: 100dvh;
    width: 100vw;
    max-width: 100vw;
    max-height: 100vh;
    overflow: hidden;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

header, #reset-button {
    flex-shrink: 0;
}

#game-board {
    flex: 1 1 0;
    display: flex;
    flex-direction: column;
    gap: 0;
    margin: 0;
    padding: 0;
    overflow: hidden;
}

#bridge-area-wrapper {
    flex-shrink: 0;
    background: #fff;
    overflow-x: auto;
    overflow-y: hidden;
    width: 100vw;
    max-width: 100vw;
    margin: 0;
    padding: 0;
    position: relative;
    left: 50%;
    right: 50%;
    margin-left: -50vw;
    margin-right: -50vw;
    scrollbar-width: none;
    scroll-behavior: smooth;
    z-index: 1000;
}

#article-pool-wrapper {
    flex: 1 1 0;
    overflow: hidden;
    min-height: 0;
    padding: 0.5rem;
}

#available-articles {
    width: 100%;
    box-sizing: border-box;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
    gap: 6px;
    align-items: start;
    justify-items: center;
    max-height: 100%;
    overflow: hidden;
}

.article-piece, .available-article {
    font-size: clamp(0.7rem, 2vw, 1rem);
    min-width: 44px;
    min-height: 36px;
    padding: 6px 4px;
}

@media (max-width: 768px) {
    html, body {
        height: 100vh;
        height: 100dvh;
        width: 100vw;
        overflow: hidden;
    }
    
    #game-container {
        height: 100vh;
        height: 100dvh;
        max-height: 100vh;
        max-height: 100dvh;
        padding: max(env(safe-area-inset-top), 8px) max(env(safe-area-inset-right), 4px) max(env(safe-area-inset-bottom), 8px) max(env(safe-area-inset-left), 4px);
    }
    
    header {
        padding: 8px 0;
        flex-shrink: 0;
    }
    
    header h3 {
        font-size: 1.1rem;
        margin: 0;
    }
    
    header p {
        font-size: 0.8rem;
        margin: 4px 0;
    }
    
    #game-stats {
        padding: 4px 0;
    }
    
    #article-pool-wrapper {
        padding: 0.25rem;
        min-height: 200px;
    }
    
    .article-piece, .available-article {
        font-size: 0.8rem;
        min-width: 40px;
        min-height: 32px;
        max-width: 300px;
        padding: 4px 3px;
    }
    
    #reset-button {
        font-size: 0.9rem;
        padding: 8px 12px;
        margin-top: 10px;
    }
}
@media (max-width: 480px) {
    #game-container {
        padding: max(env(safe-area-inset-top), 4px) max(env(safe-area-inset-right), 2px) max(env(safe-area-inset-bottom), 4px) max(env(safe-area-inset-left), 2px);
    }
    
    header h3 {
        font-size: 1rem;
    }
    
    header p {
        font-size: 0.7rem;
    }
    
    #article-pool-wrapper {
        min-height: 180px;
        padding: 0.15rem;
    }
    
    .article-piece, .available-article {
        font-size: 0.75rem;
        min-width: 50px;
        max-width: 120px;
        min-height: 28px;
        white-space: nowrap;
        word-break: keep-all;
    }
    .article-piece::before,
    .available-article::before {
        left: 4px;
        right: 4px;
    }
}
/* --- End of fit-to-viewport layout --- */

/* Layout containers */
header {
    text-align: start;
    margin: 10px;
    z-index: 10;
}

header h3 {
    font-size: 1.8rem;
    color: #333;
    margin-bottom: 10px;
}

/* Bridge area */
#bridge-area {
    display: flex;
    align-items: center;
    gap: 0;
    border: none;
    box-shadow: none;
    background-color: #f5f5f5;
    width: max-content;
    min-width: 100vw;
    z-index: 1000;
    position: relative;
}
#bridge-area.drag-over {
    box-shadow: 0 0 20px rgba(76, 175, 80, 0.3);
    transform: scale(1.03);
    animation: bridge-pulse 1.5s ease-in-out infinite;
}

@keyframes bridge-pulse {
    0%, 100% {
        box-shadow: 0 0 20px rgba(76, 175, 80, 0.3);
    }
    50% {
        box-shadow: 0 0 30px rgba(76, 175, 80, 0.5);
    }
}
#bridge-area > * {
    scroll-snap-align: start;
}

/* Hide scrollbar for all browsers */
#bridge-area-wrapper::-webkit-scrollbar,
#bridge-area::-webkit-scrollbar {
    display: none;
}
#bridge-area-wrapper,
#bridge-area {
    -ms-overflow-style: none;  /* IE and Edge */
    scrollbar-width: none;     /* Firefox */
}

#lives-display {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 0;
    padding: 0;
}
#game-stats{
    margin: 0;
    padding: 0;
}

#level-progress{
    display: flex;
    margin: 10px 0;
    font-weight: 500;
    justify-content: center;
    align-items: center;
}

#connection-feedback {
    text-align: center;
    color: #fe5a5b;
    font-weight: 600;
    height: 0;
    max-height: 0;
    overflow: hidden;
    margin: 0;
    padding: 0;
    transition: opacity 0.3s ease-in-out;
    opacity: 0;
    flex-shrink: 0;
}

#connection-feedback.show {
    height: 1.5rem;
    max-height: 1.5rem;
    opacity: 1;
    margin: 0.5rem 0;
}
/* Article pool */
#article-pool-wrapper {
    flex-grow: 1;
    padding: 1.5rem;
    overflow: hidden;
    min-height: 0;
    position: relative;
}

#available-articles {
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
}

#available-articles .article-piece,
#available-articles .available-article {
    position: absolute;
    cursor: grab;
    user-select: none;
    transition: transform 0.2s ease;
    z-index: 1;
}

#available-articles .article-piece:active,
#available-articles .available-article:active {
    cursor: grabbing;
    z-index: 10;
    transform: scale(1.05);
}

/* Article pieces -  styles */
.article-piece,
.available-article {
    position: relative;
    display: inline-flex; 
    white-space: nowrap;
    align-items: center;
    justify-content: center;
    width: auto;
    min-width: 0;
    min-width: 80px;
    max-width: 200px;
    background: #D7D7D7;
    text-overflow: ellipsis;
    word-break: normal;
    word-wrap: normal; 
    padding: 8px 12px;
    font-size: 0.9rem;
    text-align: center;
    line-height: 1.2;
    font-weight: 500;
    border-radius: none;
    cursor: grab;
    transition: all 0.2s ease-in-out;
    user-select: none;
    box-sizing: border-box;
}
article-piece::before,
.available-article::before {
    content: attr(data-text);
    position: absolute;
    left: 8px;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
    z-index: 1;
}

.article-piece.incorrectly-guessed,
.available-article.incorrectly-guessed,
#available-articles .article-piece.incorrectly-guessed,
#available-articles .available-article.incorrectly-guessed {
    background: #f44336 !important;
    color: #fff !important;
    opacity: 0.8 !important;
    border: 2px solid #d32f2f !important;
    box-shadow: 0 2px 4px rgba(244, 67, 54, 0.3) !important;
}

.available-article {
    position: absolute;
    padding: 12px 16px;
    background: #e9e9e9;
    border: 1px solid #ccc;
    transform: translateZ(0);
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 200px;
    min-width: 80px;
    backface-visibility: hidden;
}
#available-articles {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: flex-start;
}

.article-piece:active {
    cursor: grabbing;
    transform: scale(1.05);
}

/* notches style */
.has-innie::before {
    content: '';
    position: absolute;
    left: -1px;
    top: 50%;
    transform: translateY(-50%);
    width: 8px;
    height: 8px;
    background-color: #fff;
    z-index: 1;
}

.has-outie::after {
    content: '';
    position: absolute;
    right: -7px;
    top: 50%;
    transform: translateY(-50%);
    width: 8px;
    height: 8px;
    background-color: inherit;
    z-index: 4;
}

/* Piece colors */
.start-piece {
    background-color: #fb923c;
}

.end-piece {
    background-color: #4ade80;
}

.bridge-piece,
.available-piece {
    background-color: #e9e9e9;
}

.bridge-piece, .end-piece {
    transition: background 0.4s, color 0.4s;
}

.start-piece::before,
.end-piece::after {
    display: none;
}

/* Bridge slot styles */
.bridge-slot {
    transform: translateZ(0);
    backface-visibility: hidden;
}

/* Reset button */
#reset-button {
    margin-top: 20px;
    cursor: pointer;
    padding: 10px 15px;
    border-radius: 5px;
    border: none;
    font-size: 1rem;
}

/* Scrollbar styling - consolidated */
#bridge-area-wrapper::-webkit-scrollbar,
#article-pool-wrapper::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

#bridge-area-wrapper::-webkit-scrollbar-track,
#article-pool-wrapper::-webkit-scrollbar-track {
    background: #f1f1f1;
}

#bridge-area-wrapper::-webkit-scrollbar-thumb,
#article-pool-wrapper::-webkit-scrollbar-thumb {
    background: #c1c1c1;
}

#bridge-area-wrapper::-webkit-scrollbar-thumb:hover,
#article-pool-wrapper::-webkit-scrollbar-thumb:hover {
    background: #a8a8a8;
}

/* Loading animation */
.loading {
    width: 40px;
    height: 40px;
    border: 4px solid #f3f3f3;
    border-top: 4px solid #3498db;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Responsive design */
@media (max-width: 768px) {
    #game-container {
        padding: 0;
    }
    
    header h3 {
        font-size: 1.5rem;
    }
    
    header p {
        font-size: 0.9rem;
    }
    #level-progress {
        font-size: 0.9rem;
    }
    
    #game-board {
        padding: 0px;
        gap: 0px;
    }
    
    #article-pool-wrapper {
        padding: 0;
    }
    
    #available-articles {
        grid-template-columns: repeat(auto-fit, minmax(100px, 3fr));
        gap: 10px;
    }
    #available-articles .article-piece,
    #available-articles .available-article {
        margin: 9px 5px;
    }
    
    .article-piece,
    .available-article {
        display: inline-flex; 
        white-space: nowrap;
        width: auto;
        min-width: 0;
        max-width: 100%;
        text-overflow: ellipsis;
        word-break: normal;
        word-wrap: normal;            
        padding: 8px 16px;
        font-size: 0.7rem;
        text-align: center;
        line-height: 1.2;
        font-weight: 500;
        border-radius: none;
        cursor: grab;
        transition: all 0.2s ease-in-out;
        user-select: none;
        font-size: 0.85rem;
        min-width: 70px;
        max-width: 180px;
    }
    
    #reset-button {
        font-size: 0.9rem;
        padding: 8px 12px;
    }
}

@media (max-width: 480px) {
    #game-container {
        padding: 0;
    }
    
    header h3 {
        font-size: 1.3rem;
    }
    header p {
        font-size: 0.8rem;
    }
    #level-progress {
        font-size: 0.8rem;
    }
    
    #game-board {
        padding: 0;
        gap: 0;
    }
    
    #article-pool-wrapper {
        padding: 0;
    }
    
    #available-articles {
        grid-template-columns: repeat(auto-fit, minmax(180px, 2fr));
        gap: 8px;
    }
    
    .article-piece,
    .available-article {
        display: inline-flex; 
        white-space: nowrap;
        width: auto;
        min-width: 0;
        max-width: 100%;
        text-overflow: ellipsis;
        word-break: normal;
        word-wrap: normal;            
        padding: 8px 16px;
        font-size: 0.7rem;
        text-align: center;
        line-height: 1.2;
        font-weight: 500;
        border-radius: none;
        cursor: grab;
        transition: all 0.2s ease-in-out;
        user-select: none;
        font-size: 0.85rem;
        min-width: 70px;
        max-width: 180px;
    }
    
    #reset-button {
        font-size: 0.8rem;
        padding: 6px 10px;
        margin-top: 10px;
    }
}

@media (max-width: 320px) {
    #available-articles {
        grid-template-columns: repeat(auto-fit, minmax(70px, 1fr));
        gap: 6px;
    }
    
    .article-piece {
        font-size: 0.65rem;
        min-width: 60px;
        max-width: 90px;
        padding: 3px 6px;
    }
}


/* Modal overlay base styles */
.article-summary-overlay {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    background: rgba(0, 0, 0, 0.7) !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    z-index: 10000 !important; /* Higher z-index */
    padding: 20px !important;
    box-sizing: border-box !important;
}

/* Modal card styles */
.modal-card {
    background: white;
    padding: 25px;
    border-radius: 4px;
    max-width: 500px;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: 0 10px 30px rgba(0,0,0,0.3);
    margin: 20px;
    position: relative;
}

.close-btn {
    background: none !important;
    border: none !important;
    font-size: 24px !important;
    cursor: pointer !important;
    color: #666 !important;
    padding: 0 !important;
    width: 30px !important;
    height: 30px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    border-radius: 50% !important;
    transition: all 0.2s ease !important;
    margin-left: 10px !important;
    font-weight: bold !important;
}

.close-btn:hover {
    background-color: #f0f0f0 !important;
    color: #333 !important;
}

.close-btn:active {
    transform: scale(0.95);
}

/* Responsive modal styles */
@media (max-width: 768px) {
    .modal-card {
        padding: 20px;
        margin: 15px;
        max-width: 90vw;
        max-height: 85vh;
    }
}

@media (max-width: 480px) {
    .modal-card {
        padding: 15px;
        margin: 10px;
        max-width: 95vw;
        max-height: 90vh;
    }
}

/* --- Animation for incorrect article drop: tilt and red --- */
.bridge-piece.incorrect-animate {
    animation: shake-tilt-red 0.7s cubic-bezier(.36,.07,.19,.97) both;
    background-color: #f44336 !important;
    color: #fff !important;
    z-index: 10;
}

@keyframes shake-tilt-red {
    10% { transform: rotate(-10deg) scale(1.05); }
    20% { transform: rotate(8deg) scale(1.05); }
    30% { transform: rotate(-6deg) scale(1.05); }
    40% { transform: rotate(4deg) scale(1.05); }
    50% { transform: rotate(-2deg) scale(1.05); }
    60% { transform: rotate(1deg) scale(1.05); }
    70% { transform: rotate(0deg) scale(1.05); }
    100% { transform: rotate(0deg) scale(1); }
}

/* --- Pulsing animation for red dots (lost lives) --- */
@keyframes pulse-red {
    0%, 100% { 
        opacity: 0.8; 
        transform: scale(1); 
    }
    50% { 
        opacity: 1; 
        transform: scale(1.2); 
    }
}

.life-dot.red {
    animation: pulse-red 1s ease-in-out infinite;
}

/* --- Game Over Modal --- */
#game-over-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    opacity: 0;
    transition: opacity 0.3s ease;
    padding: 20px;
}

.game-over-card {
    background: white;
    padding: 30px;
    border-radius: 20px;
    text-align: center;
    max-width: 450px;
    width: 90%;
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
    transform: scale(0.8);
    transition: transform 0.3s ease;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.game-over-title {
    color: #dc3545;
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 15px;
}

.game-over-text {
    color: #555;
    font-size: 1rem;
    line-height: 1.5;
    margin-bottom: 25px;
}

.game-over-articles {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 20px;
    margin-bottom: 30px;
    flex-wrap: wrap;
}

.game-over-articles .article-piece {
    padding: 10px 20px;
    font-weight: 500;
    flex-shrink: 0;
}

.game-over-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
}

.modal-btn {
    border: none;
    padding: 12px 24px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 1rem;
    transition: all 0.2s ease;
    min-height: 44px;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.modal-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 10px rgba(0,0,0,0.15);
}

.modal-btn.try-again {
    border: 1px solid #8d8282;
    background-color: #f0f0f0;
    color: #000;
}

.modal-btn.show-solution {
    border: 1px solid #8d8282;
    background-color: #f0f0f0;
    color: #000;
}

.modal-btn.close {
    background-color: #dc3545;
    color: white;
}


/* --- Solution Modal --- */
#solution-overlay .article-piece {
    position: relative;
}
#solution-overlay .has-innie::before {
    content: '';
    position: absolute;
    left: -4px;
    top: 50%;
    transform: translateY(-50%);
    width: 8px;
    height: 8px;
    background-color: #fff;
    z-index: 2;
}

#solution-overlay .has-outie::after {
    content: '';
    position: absolute;
    right: -7px;
    top: 50%;
    transform: translateY(-50%);
    width: 8px;
    height: 8px;
    background-color: inherit;
    z-index: 4;
}

#solution-overlay .start-piece::before,
#solution-overlay .end-piece::after {
    display: none !important;
}

#solution-overlay .start-piece.has-outie::after,
#solution-overlay .end-piece.has-innie::before {
    display: block !important;
}

#solution-overlay div[style*="overflow-x: auto"]::-webkit-scrollbar {
    display: none;
}
#solution-overlay div[style*="overflow-x: auto"] {
    -ms-overflow-style: none;
    scrollbar-width: none;
}

@media (max-width: 768px) {
    #solution-overlay .article-piece {
        font-size: 0.8rem !important;
        min-width: 70px !important;
        max-width: 150px !important;
        padding: 6px 10px !important;
    }
}

@media (max-width: 480px) {
    #solution-overlay .article-piece {
        font-size: 0.75rem !important;
        min-width: 60px !important;
        max-width: 120px !important;
        padding: 6px 8px !important;
    }
}
