/* ==========================================
   MULTISECT ALIGNED DESIGN TOKENS
   ========================================== */
:root {
    /* Primary Brand Colors */
    --color-primary: #D32F2F;
    --color-secondary: #1B3A57;
    --color-accent: #3B9797;
    
    /* Neutrals */
    --bg-main: #F9FAFB;
    --bg-surface: #FFFFFF;
    --color-border: #E5E7EB;
    --text-main: #111827;
    --text-light: #4B5563;
    
    /* Semantic Colors */
    --easy: #10B981;
    --medium: #F59E0B;
    --hard: #EF4444;
    
    /* Shadows & Radius */
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --radius-md: 0.5rem;
    --radius-xl: 1rem;
    
    /* Legacy Hooks (Mapped to new tokens) */
    --primary-color: var(--color-primary);
    --glass-bg: var(--bg-surface);
    --glass-border: var(--color-border);
    --glass-shadow: var(--shadow-sm);
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-main);
    background-image: radial-gradient(var(--color-border) 0.5px, transparent 0.5px);
    background-size: 20px 20px;
    height: 100vh;
    color: var(--text-main);
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden; 
}

/* ==========================================
   GLOBAL APP CONTAINER
   ========================================== */
#app-container {
    width: 100%; max-width: 900px; height: 95vh;
    background: var(--bg-surface);
    border: 1px solid var(--color-border); border-radius: var(--radius-xl);
    box-shadow: var(--shadow-lg); display: flex; flex-direction: column;
    padding: 24px; margin: 10px;
    position: relative;
    z-index: 1;
}

/* ==========================================
   GLOBAL HEADER & SELECTORS
   ========================================== */
header { margin-bottom: 20px; }

.consolidated-header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 12px;
    margin-bottom: 12px;
}

.header-meta-group {
    display: flex;
    align-items: center;
    gap: 10px;
}

.header-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; }
h1 { font-family: 'Outfit', sans-serif; font-weight: 800; font-size: 1.5rem; color: var(--color-secondary); }
.topic-headline { font-family: 'Outfit', sans-serif; font-size: 1.35rem; font-weight: 700; text-align:center; margin-top: 5px; color: var(--color-primary); padding: 8px; border-radius: 8px;}


/* Difficulty Pill Logic */
.badge-container { padding: 4px 12px; border-radius: 20px; font-size: 0.75rem; font-weight: 700; text-transform: uppercase; color: white; letter-spacing: 0.5px; }
.badge-easy { background: var(--easy); }
.badge-medium { background: var(--medium); color: #fff; }
.badge-hard { background: var(--hard); }

body.hide-difficulty .badge-container { display: none !important; }

/* Global Part Selector Engine */
.part-selector { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; }
.part-tab {
    background: var(--bg-surface); border: 1px solid var(--color-border);
    padding: 8px 18px; border-radius: 50px; font-weight: 600; color: var(--text-light);
    cursor: pointer; transition: all 0.2s ease; font-family: inherit;
    font-size: 0.9rem;
}
.part-tab.active { background: var(--color-secondary); color: white; border-color: var(--color-secondary); box-shadow: var(--shadow-md); }
.part-tab:hover:not(.active) { border-color: var(--color-primary); color: var(--color-primary); }

select.glass-dropdown {
    appearance: none; background: var(--bg-surface); border: 1px solid var(--color-border);
    padding: 8px 32px 8px 16px; border-radius: 8px; font-family: inherit; font-weight: 600;
    color: var(--text-main); outline: none; cursor: pointer; transition: 0.2s ease;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23374151' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
}
select.glass-dropdown:hover { border-color: var(--color-primary); }
#topic-meta-info { font-weight: 700; color: var(--color-secondary); font-size: 0.9rem; }

/* ==========================================
   DYNAMIC MAIN CONTENT SWAPPING
   ========================================== */
main { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 15px; overflow-y: auto; overflow-x: hidden; scrollbar-width: thin; padding-right: 5px; }
main::-webkit-scrollbar { width: 6px; } main::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 10px; }

.part-view { flex: 1; display: flex; flex-direction: column; animation: fadeIn 0.4s ease forwards; }
.hidden { display: none !important; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

.flex-col-gap { display: flex; flex-direction: column; gap: 10px; }
.text-center { text-align: center; }
.mt-4 { margin-top: 24px; }

/* ==========================================
   MODULAR COMPONENTS 
   ========================================== */

/* 1. Universal Question Cards */
.question-card {
    background: var(--bg-surface); padding: 20px; border-radius: var(--radius-xl);
    font-size: 1.15rem; font-weight: 500; box-shadow: var(--shadow-sm);
    border: 1px solid var(--color-border); border-left: 6px solid var(--color-primary); 
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    line-height: 1.5;
}
.highlight-q { border-left-color: var(--color-accent); background: #f0f9f9; }

/* Override styling specifically for Part 2 Questions */
#part2-container .question-card {
    font-size: 1rem;
    padding: 12px 20px;
    margin-bottom: 8px;
}

/* 2. Part 1 Giant Cards */
.giant-text { 
    font-size: 2rem; padding: 40px; font-weight: 700; 
    display: flex; align-items: center; justify-content: center; 
    min-height: 220px; border: 1px solid var(--color-border);
    border-top: 8px solid var(--color-primary); 
    color: var(--color-secondary);
    box-shadow: var(--shadow-md);
}
.instruction-text { text-align: center; color: var(--text-light); font-weight: 500; font-size: 0.95rem; margin-top: auto; padding: 20px 0; border-top: 1px dashed var(--color-border); }

/* 3. Part 2 Dynamic Images Engine */
#photos-container { display: flex; justify-content: center; align-items: center; gap: 16px; margin-top: 20px; width: 100%; }

.photos-layout-h { flex-direction: column; }
.photos-layout-v { flex-direction: row; }

.photo-grp-h { display: flex; flex-direction: row; justify-content: center; gap: 12px; width: 100%; }
.photo-grp-v { display: flex; flex-direction: column; justify-content: center; gap: 12px; }

.photo-card { 
    width: 100%; 
    max-width: 200px; 
    aspect-ratio: 1 / 1; 
    object-fit: cover; 
    border-radius: var(--radius-xl); 
    cursor: pointer; 
    border: 4px solid white;
    box-shadow: var(--shadow-md); 
    transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275), box-shadow 0.3s ease; 
}
.photo-card:hover { transform: scale(1.05) translateY(-5px); box-shadow: var(--shadow-lg); z-index: 10; }

/* 4. Part 3 Spidergram / Collaborative Logic */
#part3-container { align-items: center; padding: 20px 0; }
.spider-node { background: white; padding: 18px; border-radius: var(--radius-md); box-shadow: var(--shadow-md); text-align: center; font-weight: 600; border: 1px solid var(--color-border); transition: all 0.3s ease; color: var(--color-secondary); }
.spider-node.central { border: 3px solid var(--color-primary); font-size: 1.35rem; font-weight: 800; background: #fff; color: var(--color-primary); box-shadow: var(--shadow-lg); }
.spider-node:hover { transform: translateY(-3px); border-color: var(--color-primary); }

.spider-board {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    grid-template-areas:
        ". top ."
        "left-top central right-top"
        "left-bot central right-bot";
    gap: 20px;
    column-gap: 30px;
    align-items: center;
    justify-items: center;
    width: 100%;
}
.spider-pos-top { grid-area: top; align-self: end; }
.spider-pos-left-top { grid-area: left-top; align-self: end; justify-self: end; }
.spider-pos-right-top { grid-area: right-top; align-self: end; justify-self: start; }
.spider-pos-left-bot { grid-area: left-bot; align-self: start; justify-self: end; }
.spider-pos-right-bot { grid-area: right-bot; align-self: start; justify-self: start; }
.spider-pos-central { grid-area: central; align-self: center; z-index: 10; max-width: 320px; }

/* 5. Part 4 List Mechanics */
.p4-item { padding: 24px; background: white; border-radius: var(--radius-xl); border: 1px solid var(--color-border); box-shadow: var(--shadow-sm); font-size: 1.25rem; font-weight: 600; display: flex; gap: 15px; align-items: center; color: var(--color-secondary); }
.p4-bullet { font-weight: 800; color: var(--color-primary); font-size: 1.5rem; line-height: 1; }

/* ==========================================
   GLOBAL FOOTERS AND TIMING MODALS
   ========================================== */
footer { display: flex; flex-direction: column; gap: 15px; margin-top: 24px; padding-top: 20px; border-top: 1px solid var(--color-border); }
.controls-row { display: flex; justify-content: center; align-items: center; gap: 15px; }

.timer-display { font-family: 'Outfit', sans-serif; font-size: 2.25rem; font-weight: 800; color: var(--color-secondary); background: #f3f4f6; padding: 4px 24px; border-radius: 12px; min-width: 140px; text-align: center; transition: all 0.3s ease;}
.timer-warning { background: var(--color-primary); color: white; animation: pulse 1s infinite alternate; }
@keyframes pulse { from { opacity: 1; } to { opacity: 0.8; } }

.btn { 
    display: inline-block;
    padding: var(--spacing-3, 0.75rem) var(--spacing-6, 2rem); 
    border-radius: var(--radius-md); 
    font-weight: 600; 
    cursor: pointer; 
    transition: all 150ms ease-in-out; 
    font-family: inherit;
    border: 2px solid transparent;
    text-align: center;
    line-height: 1.375;
}
.btn-primary { background: var(--color-primary); color: white; border-color: var(--color-primary); }
.btn-primary:hover { background: var(--color-secondary); border-color: var(--color-secondary); transform: translateY(-2px); box-shadow: var(--shadow-md); }
.btn-secondary { background: white; color: var(--color-secondary); border-color: var(--color-border); }
.btn-secondary:hover { border-color: var(--color-primary); color: var(--color-primary); transform: translateY(-2px); box-shadow: var(--shadow-md); }
.glass-btn-icon { font-size: 1.5rem; padding: 8px 16px; }

.nav-row { gap: 20px; }
.nav-btn { background: none; border: none; font-weight: 800; color: var(--text-light); cursor: pointer; transition: 0.2s ease; font-size: 1rem; text-transform: uppercase; letter-spacing: 1px; }
.nav-btn:hover { color: var(--color-primary); transform: translateX(2px); }
#btn-prev:hover { transform: translateX(-2px); }

.progress-bar-container { flex: 1; height: 10px; background: #e5e7eb; border-radius: 10px; overflow: hidden; }
#progress-bar { height: 100%; width: 0%; background: var(--color-accent); transition: width 0.4s ease; }

/* Lightbox Sub-module Overlay */
#lightbox { position: fixed; top: 0; left: 0; width: 100%; height: 100%; margin: 0; background: rgba(27, 58, 87, 0.95); z-index: 1000; display: flex; justify-content: center; align-items: center; transition: opacity 0.3s ease; }
#lightbox.hidden { opacity: 0; pointer-events: none; }
#lightbox-img { max-width: 90%; max-height: 90%; border-radius: var(--radius-xl); border: 8px solid white; box-shadow: var(--shadow-lg); transition: transform 0.3s ease; }
#lightbox.hidden #lightbox-img { transform: scale(0.9); }
#lightbox-close { position: absolute; top: 20px; right: 30px; background: none; border: none; color: white; font-size: 3.5rem; cursor: pointer; line-height: 1; transition: transform 0.2s ease; }
#lightbox-close:hover { transform: scale(1.1); }

@media (max-width: 600px) {
    /* 1. Global Viewport & Flex Enforcer */
    body {
        overflow: hidden;
        height: 100dvh;
        width: 100vw;
    }

    #app-container {
        height: 100dvh;
        margin: 0;
        border-radius: 0;
        padding: 12px;
        display: flex;
        flex-direction: column;
        overflow: hidden;
    }

    /* 2. Header Compaction */
    header {
        margin-bottom: 8px;
        flex-shrink: 0;
    }

    .consolidated-header {
        gap: 6px;
        margin-bottom: 6px;
        justify-content: center;
        flex-wrap: wrap;
    }

    /* Reorder header items: tabs first, category second, meta third, candidates last */
    .consolidated-header .part-selector { order: 1; }
    .consolidated-header #category-select { order: 2; }
    .consolidated-header .header-meta-group { order: 3; }
    .consolidated-header #candidates-select {
        order: 4;
        width: auto;
    }

    .part-tab {
        padding: 4px 10px;
        font-size: 0.75rem;
    }

    select.glass-dropdown {
        padding: 4px 24px 4px 8px;
        font-size: 0.8rem;
        border-radius: 6px;
    }

    .topic-headline {
        font-size: 0.95rem;
        padding: 6px;
        margin-top: 0;
        border-radius: 6px;
    }

    #topic-meta-info {
        font-size: 0.8rem;
    }

    .badge-container {
        padding: 2px 8px;
        font-size: 0.65rem;
    }

    /* 3. Main Content Distribution */
    main {
        flex: 1;
        gap: 8px;
        padding-right: 0;
        overflow-y: auto; /* Allow scroll only when content genuinely overflows */
        overflow-x: hidden;
        display: flex;
        flex-direction: column;
        min-height: 0;
    }

    .part-view {
        flex: 1;
        min-height: 0;
        display: flex;
        flex-direction: column;
    }

    /* 4. Component Refinement (Zero-Scroll logic) */
    .question-card {
        padding: 8px 12px;
        font-size: 0.85rem;
        margin-bottom: 4px;
        border-left-width: 4px;
    }

    #part2-container .question-card {
        padding: 6px 12px;
        font-size: 0.8rem;
        margin-bottom: 2px;
    }

    .giant-text {
        font-size: 1.25rem;
        padding: 15px;
        min-height: 100px;
        flex: 1;
    }

    /* 5. Image Scaling (Part 2) */
    #photos-container {
        flex: 1 1 auto;
        min-height: 80px;
        margin-top: 5px;
        gap: 8px;
        max-height: 42vh;
        overflow: hidden;
    }

    .photo-card {
        border-width: 2px;
        max-width: 160px;
        aspect-ratio: 1 / 1;
        object-fit: cover;
    }

    /* 6. Spidergram / Collaborative Logic */
    .spider-board {
        display: flex;
        flex-direction: column;
        gap: 8px;
        justify-content: center;
        flex: 1;
        min-height: 0;
    }
    .spider-node {
        padding: 8px 12px;
        font-size: 0.85rem;
        margin: 0;
    }
    .spider-node.central {
        font-size: 0.95rem;
        margin-bottom: 5px;
        order: -1;
        max-width: 100%;
    }
    .spider-pos-top,
    .spider-pos-left-top,
    .spider-pos-right-top,
    .spider-pos-left-bot,
    .spider-pos-right-bot {
        align-self: stretch;
        width: 100%;
        margin: 0;
    }

    /* 7. Footer & Controls Optimization */
    footer {
        margin-top: 8px;
        padding-top: 8px;
        gap: 10px;
        flex-shrink: 0;
        padding-bottom: env(safe-area-inset-bottom, 8px);
    }

    .controls-row {
        gap: 8px;
    }

    .timer-display {
        font-size: 1.4rem;
        min-width: 85px;
        padding: 2px 10px;
    }

    .btn {
        padding: 8px 14px;
        font-size: 0.85rem;
    }

    .glass-btn-icon {
        padding: 6px 10px;
        font-size: 1.1rem;
    }

    .nav-btn {
        font-size: 0.8rem;
    }

    .progress-bar-container {
        height: 6px;
    }

    .instruction-text {
        padding: 5px 0;
        font-size: 0.7rem;
        margin-top: 0;
    }
}