:root {
    --primary: #4a90e2;
    --bg: #f4f7f6;
    --text: #333;
    --card-bg: #fff;
    --success: #2ecc71;
    --error: #e74c3c;
    --draggable: #3498db;
    --droppable: #ecf0f1;
}

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

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: var(--bg);
    color: var(--text);
    display: flex;
    justify-content: center;
    min-height: 100vh;
}

.main-container {
    width: 100%;
    max-width: 1000px;
    padding: 20px;
    display: flex;
    flex-direction: column;
}

/* Header & Stats */
header {
    text-align: center;
    margin-bottom: 20px;
}

.stats-bar {
    display: flex;
    justify-content: center;
    gap: 20px;
    margin-top: 10px;
    font-size: 1.2rem;
    font-weight: bold;
}

/* Game Board */
.game-board {
    display: flex;
    flex-direction: column;
    gap: 30px;
    flex-grow: 1;
}

h3 {
    text-align: center;
    margin-bottom: 15px;
    color: #555;
}

.box-container {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* Draggable Items (Begreper) */
.draggable {
    background-color: var(--draggable);
    color: white;
    padding: 15px;
    border-radius: 8px;
    cursor: grab;
    font-weight: bold;
    text-align: center;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    transition: transform 0.2s, opacity 0.2s;
    user-select: none;
}

.draggable:active {
    cursor: grabbing;
    transform: scale(1.05);
}

.draggable.dragging {
    opacity: 0.5;
}

/* Droppable Targets (Forklaringer) */
.droppable {
    background-color: var(--card-bg);
    border: 2px dashed #bdc3c7;
    border-radius: 8px;
    padding: 15px;
    min-height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    transition: all 0.3s;
    font-size: 0.95rem;
}

.droppable.hovered {
    background-color: #d6eaf8;
    border-color: var(--primary);
    transform: scale(1.02);
}

/* Success State */
.droppable.correct {
    background-color: var(--success);
    color: white;
    border-style: solid;
    border-color: #27ae60;
}

/* Animation for wrong answer */
.shake {
    animation: shake 0.5s;
    border-color: var(--error);
    background-color: #fadbd8;
}

@keyframes shake {
    0% {
        transform: translateX(0);
    }

    25% {
        transform: translateX(-5px);
    }

    50% {
        transform: translateX(5px);
    }

    75% {
        transform: translateX(-5px);
    }

    100% {
        transform: translateX(0);
    }
}

/* Overlay / Menus */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
}

.hidden {
    display: none;
}

.modal {
    background: white;
    padding: 40px;
    border-radius: 12px;
    text-align: center;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}

.btn-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 20px;
}

.btn {
    padding: 12px 24px;
    border: none;
    border-radius: 6px;
    font-size: 1rem;
    cursor: pointer;
    font-weight: bold;
    color: white;
    transition: opacity 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.btn.small {
    padding: 8px 16px;
    font-size: 0.9rem;
}

.btn:hover {
    opacity: 0.9;
}

.green {
    background-color: var(--success);
}

.yellow {
    background-color: #f1c40f;
    color: black;
}

.red {
    background-color: var(--error);
}

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

/* --- Tillegg for Flervalgsquiz --- */

/* Progresjonsbar */
.progress-section {
    width: 100%;
    max-width: 600px;
    margin: 0 auto 20px auto;
    text-align: center;
}

.progress-track {
    background-color: #e0e0e0;
    height: 10px;
    border-radius: 5px;
    overflow: hidden;
    margin-bottom: 5px;
}

.progress-fill {
    height: 100%;
    background-color: var(--success);
    width: 0%;
    transition: width 0.3s ease;
}

/* Quiz Layout */
.game-board-quiz {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    width: 100%;
}

.question-box {
    background-color: #fff;
    padding: 10px;
    border-radius: 10px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    width: 100%;
    text-align: center;
    border-left: 5px solid var(--primary);
}

#question-text {
    font-size: clamp(1.1rem, 4vw, 1.8rem);
    margin: 0;
    line-height: 1.4;
}

.options-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: clamp(10px, 2vw, 20px);
    width: 100%;
    max-width: 100%;
        /* Hindrer gridet i å bli bredere enn forelderen */
}

/* Knapper for svaralternativer */
.option-btn {
    background-color: white;
    border: 2px solid #dcdcdc;
    border-radius: 8px;
    padding: clamp(10px, 3vw, 15x);
    font-size: clamp(0.9rem, 3vw, 1.1rem);
    cursor: pointer;
    transition: all 0.2s;
    font-weight: 600;
    color: var(--text);
    min-height: clamp(70px, 15vw, 100px);
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Hindre at matematikk-containere dytter ut boksene */
.option-btn mjx-container {
    max-width: 100% !important;
    overflow-x: hidden !important;
    /* Fjerner scrollbar */
    display: block !important;
}

.option-btn {
    width: 100%;
    box-sizing: border-box;
    /* Sørger for at padding ikke øker bredden */
    min-width: 0;
    /* Tillater knappen å bli smalere enn innholdet sitt */
}

.option-btn:hover:not(:disabled) {
    background-color: #f0f8ff;
    border-color: var(--primary);
    transform: translateY(-2px);
}

/* Tilstander for svar */
.option-btn.correct {
    background-color: var(--success);
    color: white;
    border-color: #27ae60;
}

.option-btn.wrong {
    background-color: var(--error);
    color: white;
    border-color: #c0392b;
    opacity: 0.8;
}

.option-btn:disabled {
    cursor: default;
    transform: none;
}

/* Feedback boks nederst */
.feedback-box {
    width: 100%;
    background-color: #fff;
    border-top: 4px solid var(--primary);
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 -4px 10px rgba(0, 0, 0, 0.05);
    margin-top: 20px;
    animation: slideUp 0.3s ease-out;
}

.feedback-box.correct-border {
    border-color: var(--success);
}

.feedback-box.wrong-border {
    border-color: var(--error);
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Mobil-tilpasning */
@media (max-width: 600px) {
    .options-grid {
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
        /* 1 kolonne på mobil */
        gap: clamp(8px, 2vw, 15px);
        width: 100%;
    }
    
    #game-title {
        font-size: clamp(1.3rem, 5vw, 1.8rem);
        margin-bottom: 10px;
    }
    
    .question-container {
        padding: clamp(15px, 4vw, 20px);
    }
    
    .question-box {
        padding: clamp(15px, 3vw, 20px);
        margin-bottom: 15px;
    }
    
    #feedback-box {
        margin-top: 15px;
        border-radius: 8px;
    }
    
    #feedback-box #feedback-text {
        padding: clamp(12px, 3vw, 15px);
        min-height: auto;
    }
    
    #feedback-box #next-btn {
        padding: clamp(10px, 2vw, 12px) clamp(15px, 3vw, 20px);
        font-size: clamp(0.9rem, 2.5vw, 1rem);
    }
    
    .progress-section {
        margin-bottom: 15px;
    }
    
    header {
        margin-bottom: 15px;
    }
}

/* --- Tillegg for Rangering (Ranking) --- */

.game-board-ranking {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    gap: 20px;
}

.ranking-workspace {
    display: flex;
    flex-wrap: wrap;
    gap: 40px;
    justify-content: center;
    width: 100%;
}

/* Kolonnen med nummererte plasser */
.slots-column {
    display: flex;
    flex-direction: column;
    gap: 10px;
    min-width: 300px;
}

.rank-slot {
    background-color: #ecf0f1;
    border: 2px dashed #bdc3c7;
    border-radius: 8px;
    padding: 10px;
    min-height: 60px;
    display: flex;
    align-items: center;
    position: relative;
    padding-left: 40px;
    /* Plass til nummeret */
}

/* Nummerering (1, 2, 3...) */
.rank-slot::before {
    content: attr(data-index);
    position: absolute;
    left: 10px;
    font-weight: bold;
    color: #7f8c8d;
    font-size: 1.2rem;
}

.rank-slot.hovered {
    background-color: #d6eaf8;
    border-color: var(--primary);
}

/* Banken med brikker */
.pool-area {
    display: flex;
    flex-direction: column;
    gap: 10px;
    min-width: 300px;
    padding: 20px;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
}

/* Selve brikkene */
.rank-item {
    background-color: var(--card-bg);
    border: 2px solid var(--primary);
    color: var(--text);
    padding: 12px;
    border-radius: 6px;
    cursor: grab;
    font-weight: 600;
    text-align: center;
    user-select: none;
    background: white;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

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

.rank-item.dragging {
    opacity: 0.5;
}

/* Feedback farger på slots */
.rank-slot.correct-slot {
    border-style: solid;
    border-color: var(--success);
    background-color: #e8f8f5;
}

.rank-slot.wrong-slot {
    border-style: solid;
    border-color: var(--error);
    background-color: #fadbd8;
}

.action-bar {
    margin-top: 20px;
}

/* Responsivitet */
@media (max-width: 700px) {
    .ranking-workspace {
        flex-direction: column;
    }

    .slots-column,
    .pool-area {
        width: 100%;
        min-width: auto;
    }
}

/* --- Tillegg for Begreps-match (Drag & Drop) --- */

.game-board-match {
    display: flex;
    justify-content: center;
    gap: 40px;
    width: 100%;
    margin-top: 20px;
    flex-wrap: wrap;
}

.column {
    display: flex;
    flex-direction: column;
    gap: 15px;
    width: 45%;
    min-width: 250px;
}

/* Brikkene */
.draggable,
.droppable {
    background-color: white;
    border: 2px solid #bdc3c7;
    border-radius: 8px;
    padding: 20px;
    text-align: center;
    font-size: 1rem;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 80px;
    font-weight: 500;
}

/* De som skal dras (Begreper) */
.draggable {
    cursor: grab;
    background-color: #f8f9fa;
    color: var(--primary);
    border-color: var(--primary);
}

.draggable:active {
    cursor: grabbing;
}

.draggable.dragging {
    opacity: 0.5;
    transform: scale(0.95);
}

/* Mål-boksene (Forklaringer) */
.droppable {
    border-style: dashed;
    background-color: #fff;
}

.droppable.hovered {
    background-color: #eaf2f8;
    border-color: var(--primary);
    transform: scale(1.02);
}

/* Tilstander */
.matched {
    background-color: var(--success);
    color: white;
    border-color: #27ae60;
    border-style: solid;
    cursor: default;
    animation: pulse 0.3s ease-in-out;
}

.wrong-match {
    background-color: var(--error);
    color: white;
    animation: shake 0.4s;
}

/* Når et begrep er brukt opp, kan vi skjule det eller gjøre det inaktivt */
.draggable.hidden {
    visibility: hidden;
    opacity: 0;
}

/* Animasjoner */
@keyframes shake {

    0%,
    100% {
        transform: translateX(0);
    }

    25% {
        transform: translateX(-5px);
    }

    75% {
        transform: translateX(5px);
    }
}

@keyframes pulse {
    0% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.05);
    }

    100% {
        transform: scale(1);
    }
}

/* Mobil */
@media (max-width: 700px) {
    .game-board-match {
        flex-direction: column;
        align-items: center;
    }

    .column {
        width: 100%;
    }
}

/* Container for hele quizen */
.quiz-container {
    display: flex;
    flex-direction: column;
    gap: 20px;
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
}

/* Hovedoppsettet */
.quiz-layout {
    display: flex;
    flex-direction: column;
    gap: 30px;
    max-width: 900px;
    margin: 40px auto;
}

/* Spørsmålsboksen øverst */
.question-container {
    background: #f4f4f4;
    border: 2px solid #333;
    padding: 40px;
    text-align: center;
}

/* 2x2 Rutenett for knappene */
.options-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    /* To kolonner */
    gap: 20px;
}

/* Styling for knappene (bruker klassen fra JS) */
.option-btn {
    padding: clamp(8px, 2vh, 15px) 5px !important;
    font-size: clamp(0.8rem, 3vw, 1.2rem) !important;
    border: 2px solid #333;
    cursor: pointer;
    background-color: #eee;
    /*white-space: nowrap;*/

    /* 4. Tillat skalering av innholdet */
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 0;
    /* Standardfarge før svar */
}

/* Bunnseksjonen */
.quiz-footer {
    display: flex;
    justify-content: space-between;
    /* Forklaring venstre, knapp høyre */
    align-items: flex-end;
    margin-top: 20px;
}

.explanation-box {
    background: #f4f4f4;
    border: 2px solid #333;
    padding: 15px;
    width: 65%;
    /* Tar opp mesteparten av plassen til venstre */
    min-height: 60px;
}

.next-btn {
    padding: 15px 40px;
}

/* --- Specific updates for #feedback-box layout (Side-by-Side) --- */

#feedback-box {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    padding: 0;
    /* Remove padding to allow button full height/touch edges */
    overflow: hidden;
    /* Keep border radius */
}

/* Tvinger MathJax til å oppføre seg som vanlig tekst */
mjx-container {
    display: inline-block !important;
    margin: 0 5px !important;
    vertical-align: middle !important;
    max-width: 100% !important;
    overflow-x: auto;
    overflow-y: hidden;
    font-size: clamp(1rem, 2.5vw, 1.2rem) !important;
}

#feedback-box #feedback-text {
    width: 100%;
    max-width: 100%;
    margin: 0;
    padding: clamp(15px, 3vw, 20px);
    /* Add padding back to text
    display: flex;*/
    display: block;        /* ENDRET fra flex: nå flyter tekst og matte sammen */
    align-items: center;
    /* Vertically center text */
    line-height: 1.6;
    /* Anbefalt: gir plass til høye mattetegn (som brøker) */
    box-sizing: border-box;
    overflow-wrap: break-word;
    hyphens: auto;
    word-break: break-word;
    font-size: clamp(0.95rem, 2.5vw, 1.1rem);
}

#feedback-box #next-btn {
    width: 100%;
    margin: 0;
    border-radius: 0;
    /* Remove radius to fit corner */
    height: auto;
    /* Flex stretch fills height */
    display: flex;
    /* For centering content */
    align-items: center;
    justify-content: center;
    padding: clamp(12px, 2vw, 15px) clamp(15px, 3vw, 20px);
    order: -1;
    /* Move to top of flex container */
    font-size: clamp(0.9rem, 2.5vw, 1rem);
}

/* Handle hidden state explicitly to avoid flex overriding it */
#feedback-box #next-btn.hidden {
    display: none;
}

/* --- Scoreboard Redesign --- */
.scoreboard-modal {
    width: 90%;
    max-width: 1000px;
    height: 80vh;
    display: flex;
    flex-direction: column;
    padding: 0 !important; /* Override standard modal padding */
    overflow: hidden;
    background-color: #f8f9fa;
}

.scoreboard-header {
    background-color: white;
    padding: 20px;
    border-bottom: 1px solid #e0e0e0;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.scoreboard-actions {
    display: flex;
    gap: 10px;
}

.scoreboard-content {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
}

.scoreboard-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 20px;
}

.score-card {
    background: white;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
    border: 1px solid #eee;
    display: flex;
    flex-direction: column;
    gap: 10px;
    transition: transform 0.2s;
}

.score-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.card-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 5px;
}

.score-badge {
    background-color: #eee;
    padding: 4px 10px;
    border-radius: 20px;
    font-weight: bold;
    font-size: 0.85rem;
}

.score-badge.high { background-color: #d1fae5; color: #065f46; }
.score-badge.med { background-color: #fef3c7; color: #92400e; }
.score-badge.low { background-color: #fee2e2; color: #991b1b; }

.card-date {
    font-size: 0.8rem;
    color: #999;
}

.card-title {
    font-size: 1.1rem;
    font-weight: bold;
    color: #333;
    margin: 0;
}

.card-meta {
    font-size: 0.9rem;
    color: #666;
    display: flex;
    justify-content: space-between;
    margin-top: auto;
    padding-top: 15px;
    border-top: 1px solid #f9f9f9;
}

/* --- Repetition Overlay --- */
#repetition-overlay .modal {
    border: 3px solid var(--primary);
    background: #fdfdfd;
}

/* --- Mobile Landscape Mode Optimization --- */
@media (max-height: 500px) and (orientation: landscape) {
    /* Prevent scrolling and maximize container */
    body {
        overflow: hidden;
        padding: 3px;
        height: 100vh;
        display: flex;
        justify-content: center;
    }
    
    .main-container {
        padding: 5px;
        gap: 3px;
        width: 100%;
        max-width: none;
        height: 100vh;
        display: flex;
        flex-direction: column;
    }
    
    /* Hide non-essential elements */
    #game-title,
    h1,
    .stats-text,
    .subtitle {
        display: none;
    }
    
    /* Minimal header */
    header {
        margin-bottom: 3px;
        padding: 2px 5px;
        flex-shrink: 0;
    }
    
    /* Ultra-slim progress bar */
    .progress-section {
        margin-bottom: 3px;
    }
    
    .progress-track {
        height: 4px;
    }
    
    /* Scale up question box but keep it minimal */
    .question-box,
    .question-container {
        padding: 8px;
        margin-bottom: 5px;
        min-height: auto;
        max-height: none;
        border-radius: 6px;
        flex-shrink: 0;
    }
    
    #question-text {
        font-size: clamp(1.2rem, 5vh, 2rem);
        line-height: 1.3;
        margin: 0;
    }
    
    /* FULL HEIGHT GRID - expand to fill all available space */
    .options-grid {
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        grid-template-rows: 1fr 1fr !important;
        gap: 15px;
        margin-bottom: 5px;
        flex-grow: 1;
        height: auto;
        max-height: none;
        width: 100%;
        max-width: none;
    }
    
    /* BUTTON EXPANSION - fill entire grid cell */
    .option-btn {
        padding: 10px;
        font-size: clamp(1rem, 6vh, 1.5rem);
        min-height: auto;
        max-height: none;
        height: 100%;
        width: 100%;
        line-height: 1.3;
        display: flex;
        align-items: center;
        justify-content: center;
        text-align: center;
    }
    
    /* Optimize feedback box */
    #feedback-box {
        position: relative;
        margin: 5px 0 0 0;
        border-radius: 6px;
        max-height: 25vh;
        overflow-y: auto;
        flex-shrink: 0;
    }
    
    #feedback-box #feedback-text {
        padding: 8px 10px;
        font-size: clamp(0.85rem, 2.5vh, 1rem);
        line-height: 1.3;
    }
    
    #feedback-box #next-btn {
        padding: 10px;
        font-size: clamp(0.9rem, 2.5vh, 1.1rem);
    }
    
    /* Minimize overlays */
    .overlay .modal {
        max-height: 90vh;
        overflow-y: auto;
        padding: 15px;
    }
    
    /* Compact main game board - fill all space */
    .game-board,
    .game-board-quiz {
        gap: 3px;
        flex-grow: 1;
        display: flex;
        flex-direction: column;
        height: auto;
        max-height: none;
    }
    
    /* Scale MathJax appropriately */
    mjx-container {
        font-size: clamp(0.9rem, 2vh, 1.2rem) !important;
    }

        /* Fjern scrollbar og tving MathJax til å krympe inni knappene */
        .option-btn mjx-container {
            overflow-x: visible !important;
            /* Fjerner scrollbar */
            max-width: 100% !important;
            display: inline-block !important;
        }
    
        /* Gjør fonten i knappene litt mindre på de minste skjermene */
        @media (max-width: 470px) {
            .option-btn {
                font-size: clamp(0.85rem, 3vw, 1rem) !important;
                padding: 12px 8px !important;
            }
        }
        
}
/* Spesialtilpasning for lave skjermer (f.eks. bærbare PC-er eller små vinduer) */
@media (max-height: 780px) {
    .main-container {
        padding: 10px;
        gap: 10px;
        justify-content: space-evenly;
    }

    .game-board {
        justify-content: space-evenly;
    }

    header {
        margin-bottom: 5px;
    }

    h1 {
        font-size: 1.2rem !important;
        /* Krymper tittelen */
    }

    .question-card {
        padding: 2vh 20px !important;
        margin-bottom: 10px !important;
    }

    #question-text {
        /* Balanced typography for better readability */
        font-size: clamp(1.1rem, 5vh, 1.8rem) !important;
    }

    .options-grid {
        gap: 20px !important;
        flex-grow: 1;
    }

    .option-btn {
        padding: 8px 12px !important;
        font-size: clamp(1rem, 3vh, 1.3rem) !important;
        min-height: 60px !important;
    }

    .stats-bar {
        font-size: 0.9rem !important;
        margin-top: 5px;
    }
}