:root {
    --bg-color: #0f0f12;
    --text-color: #e0e0e0;
    --primary: #00f3ff;
    --secondary: #ff00ff;
    --accent: #ffee00;
    --danger: #ff3333;
    --success: #33ff33;
    --font-heading: 'Orbitron', sans-serif;
    --font-body: 'Noto Sans KR', sans-serif;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
}

body {
    background: linear-gradient(-45deg, #0f0f12, #1a1a2e, #0f0f12, #16213e);
    background-size: 400% 400%;
    animation: gradientBG 15s ease infinite;
    color: var(--text-color);
    font-family: var(--font-body);
    overflow: hidden;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
}

@keyframes gradientBG {
    0% {
        background-position: 0% 50%;
    }

    50% {
        background-position: 100% 50%;
    }

    100% {
        background-position: 0% 50%;
    }
}

#app {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    max-width: 600px;
    margin: 0 auto;
    background: #1a1a20;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
    position: relative;
}

/* HEADER */
header {
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 15px;
    background: rgba(0, 0, 0, 0.3);
    border-bottom: 1px solid #333;
    position: relative;
}

.phase-timeline {
    display: flex;
    gap: 12px;
    align-items: center;
    background: rgba(255, 255, 255, 0.05);
    padding: 5px 15px;
    border-radius: 20px;
}

.phase-dot {
    font-size: 1rem;
    opacity: 0.2;
    filter: grayscale(1);
    transition: all 0.3s ease;
}

.phase-dot.active {
    opacity: 1;
    filter: grayscale(0);
    transform: scale(1.2);
    text-shadow: 0 0 10px var(--primary);
}

.global-progress-container {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 3px;
    background: rgba(255, 255, 255, 0.05);
}

.global-progress-bar {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, var(--primary), var(--secondary));
    box-shadow: 0 0 8px var(--primary);
    transition: width 1s linear;
}

.brand {
    font-family: var(--font-heading);
    color: var(--primary);
    font-weight: 900;
    font-size: 1.2rem;
    letter-spacing: 2px;
    text-shadow: 0 0 5px var(--primary);
}



/* MAIN CONTENT */
main {
    flex: 1;
    position: relative;
    overflow: hidden;
}

.view {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    /* Can be overridden */
    flex-direction: column;
    background: var(--bg-color);
    transition: opacity 0.3s ease;
    opacity: 0;
    pointer-events: none;
    z-index: 0;
}

.view.active {
    opacity: 1;
    pointer-events: auto;
    z-index: 10;
}

.center-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 10px 20px;
}

#globe-container {
    width: 200px;
    height: 200px;
    margin: 15px auto;
    position: relative;
    cursor: grab;
}

.globe-water {
    fill: rgba(0, 243, 255, 0.05);
}

.globe-land {
    fill: #2a2a35;
    stroke: #111;
    stroke-width: 0.3px;
}

.globe-highlight {
    fill: var(--accent);
    filter: drop-shadow(0 0 5px var(--accent));
}

.globe-my-country {
    fill: #00f3ff;
    animation: globeMyPulse 1.5s ease-in-out infinite alternate;
    filter: drop-shadow(0 0 8px #00f3ff);
}

@keyframes globeMyPulse {
    0% {
        fill: #00f3ff;
        opacity: 0.6;
    }

    100% {
        fill: #00f3ff;
        opacity: 1;
    }
}

/* TYPOGRAPHY */
h1 {
    font-family: var(--font-heading);
    font-size: 2.2rem;
    color: var(--text-color);
    margin-bottom: 10px;
    text-transform: uppercase;
}

.sub-text {
    color: #888;
    font-size: 0.9rem;
    letter-spacing: 1px;
}

.big-timer {
    font-family: var(--font-heading);
    font-size: 3.5rem;
    color: var(--primary);
    margin: 10px 0;
    text-shadow: 0 0 20px rgba(0, 243, 255, 0.5);
}

/* BUTTONS */
button {
    background: transparent;
    border: 1px solid var(--primary);
    color: var(--primary);
    padding: 10px 20px;
    font-family: var(--font-heading);
    font-size: 1.2rem;
    cursor: pointer;
    transition: all 0.2s;
    text-transform: uppercase;
}

button:active {
    background: var(--primary);
    color: #000;
    transform: scale(0.95);
}

/* WAIT VIEW */
#login-form {
    margin-top: 25px;
    display: flex;
    flex-direction: column;
    gap: 15px;
    width: 100%;
    max-width: 380px;
}

.login-fields {
    display: flex;
    gap: 10px;
    width: 100%;
    align-items: stretch;
}

#nickname {
    background: #1e1e21;
    border: 2px solid #2d2d30;
    color: white;
    padding: 0 15px;
    height: 60px;
    font-size: 1.8rem;
    font-family: var(--font-heading);
    text-align: center;
    outline: none;
    flex: 1;
    min-width: 0;
    /* Allow shrinking in flex */
    width: 0;
    /* Let flex-grow handle the width */
    border-radius: 6px;
    box-sizing: border-box;
    display: block;
}

#nickname:focus {
    border-color: var(--primary);
}

#btn-join {
    width: 100%;
    background: var(--primary);
    color: #000;
    font-weight: 900;
    border: none;
    padding: 15px;
    font-size: 1.5rem;
    box-shadow: 0 4px 15px rgba(0, 243, 255, 0.4);
}

/* PLAY VIEW */
#view-play {
    display: flex;
    /* Overrides flex display in .view to ensure correct layout */
}

.game-header {
    padding: 5px 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.q-counter {
    font-family: var(--font-heading);
    font-size: 1rem;
    color: #aaa;
}

.game-timer {
    font-family: var(--font-heading);
    font-size: 1.8rem;
    color: white;
    text-align: center;
    margin-bottom: 5px;
    font-weight: bold;
    text-shadow: 0 0 10px rgba(255, 255, 255, 0.3);
}

.game-timer.urgent {
    color: var(--danger);
    text-shadow: 0 0 15px var(--danger);
    transform: scale(1.1);
    animation: pulse 0.5s infinite;
}

.problem-display {
    height: 30%;
    /* Fixed relative height instead of flex: 1 */
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.05);
    margin: 5px 15px;
    padding: 10px;
    border-radius: 10px;
    border: 1px solid #333;
    min-height: 120px;
}

.question {
    font-family: var(--font-body);
    /* Changed for readability */
    font-size: 4rem;
    margin-bottom: 15px;
    color: aqua;
    font-weight: 700;
    /* Match available weight */
    text-shadow: 0 0 20px rgba(0, 255, 255, 0.6);
    will-change: transform, opacity;
}

.explosion-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    pointer-events: none;
    z-index: 9999;
}

.exploding-char {
    position: absolute;
    font-family: var(--font-body);
    /* Match question font */
    font-weight: 700;
    will-change: transform, opacity;
}

.answer-box {
    width: 180px;
    height: 45px;
    background: #000;
    border: 2px solid #555;
    color: white;
    font-family: var(--font-heading);
    font-size: 1.8rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 5px;
}

.answer-box.wrong {
    animation: shake 0.3s;
    border-color: var(--danger);
    color: var(--danger);
}

/* KEYPAD */
.keypad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 6px;
    padding: 10px 15px;
    background: #111;
    flex: 1;
    /* Fill remaining space */
    min-height: 0;
    /* Allow shrinking if needed */
}

.key {
    background: #222;
    border: none;
    border-radius: 8px;
    color: white;
    font-size: 1.4rem;
    font-family: var(--font-heading);
    box-shadow: 0 3px 0 #000;
}

.key:active {
    box-shadow: 0 2px 0 #000;
    transform: translateY(2px);
    background: #333;
}

.key.action {
    background: #2a2a2a;
    color: var(--accent);
}

.key[data-val="OK"] {
    background: var(--success);
    color: black;
}

.key[data-val="C"] {
    color: var(--danger);
}

/* FOOTER */
footer {
    height: auto;
    min-height: 40px;
    background: #0a0a0d;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 5px 15px;
    font-size: 0.75rem;
    color: #888;
    border-top: 1px solid #222;
    gap: 4px;
}

.footer-top {
    display: flex;
    width: 100%;
    justify-content: space-between;
    align-items: center;
}

.country-stats {
    display: flex;
    gap: 8px 12px;
    flex-wrap: wrap;
    justify-content: center;
    border-top: 1px solid rgba(255, 255, 255, 0.03);
    padding-top: 6px;
    width: 100%;
    max-height: 52px;
    /* Roughly 3 lines of small icons */
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: #333 transparent;
}

.country-stats::-webkit-scrollbar {
    width: 4px;
}

.country-stats::-webkit-scrollbar-track {
    background: transparent;
}

.country-stats::-webkit-scrollbar-thumb {
    background: #333;
    border-radius: 10px;
}

.country-item {
    display: flex;
    align-items: center;
    gap: 5px;
    color: #aaa;
    font-family: var(--font-heading);
}

.country-item.my-country {
    color: var(--accent);
    font-weight: bold;
    text-shadow: 0 0 5px rgba(255, 238, 0, 0.3);
}

.country-item img {
    height: 12px;
    border-radius: 2px;
}

/* ANIMATIONS */
@keyframes shake {
    0% {
        transform: translateX(0);
    }

    25% {
        transform: translateX(-5px);
    }

    50% {
        transform: translateX(5px);
    }

    75% {
        transform: translateX(-5px);
    }

    100% {
        transform: translateX(0);
    }
}

@keyframes pulse {
    0% {
        transform: scale(1);
        opacity: 1;
    }

    50% {
        transform: scale(1.1);
        opacity: 0.8;
    }

    100% {
        transform: scale(1);
        opacity: 1;
    }
}

.pulse {
    animation: pulse 1s infinite;
}

.hidden {
    display: none !important;
}

.custom-select {
    position: relative;
    width: 130px;
    font-family: var(--font-heading);
}

.select-selected {
    background: #1e1e21;
    border: 2px solid #2d2d30;
    padding: 0 12px;
    height: 60px;
    /* Match nickname height exactly */
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    color: white;
    font-size: 1.1rem;
    font-weight: 700;
    transition: all 0.3s;
    box-sizing: border-box;
}

.select-selected:hover {
    border-color: var(--primary);
}

.select-selected img,
.select-items img {
    height: 18px;
    border-radius: 2px;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
}

.select-items {
    position: absolute;
    background: #1a1a20;
    top: 100%;
    left: 0;
    right: 0;
    z-index: 1000;
    border: 1px solid #444;
    border-top: none;
    border-radius: 0 0 5px 5px;
    max-height: 250px;
    overflow-y: auto;
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.5);
    /* Hide scrollbar */
    scrollbar-width: none;
    /* Firefox */
    -ms-overflow-style: none;
    /* IE/Edge */
}

.select-items::-webkit-scrollbar {
    display: none;
    /* Chrome/Safari */
}

.select-items div {
    padding: 12px 15px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 15px;
    color: #ccc;
    font-size: 1rem;
    transition: background 0.2s, color 0.2s;
}

.select-items div:hover {
    background-color: rgba(255, 255, 255, 0.1);
    color: white;
}

.select-hide {
    display: none;
}

#country-select {
    display: none;
    /* Hide if still exists in DOM */
}

.ranking-list {
    list-style: none;
    padding: 0;
    width: 100%;
    max-width: 400px;
    margin: 20px auto;
}

.ranking-list li {
    background: rgba(255, 255, 255, 0.05);
    margin: 5px 0;
    padding: 10px;
    border-radius: 5px;
    font-size: 1.2rem;
    display: flex;
    justify-content: space-between;
    opacity: 0;
    /* Hidden initially for animation */
    transform: translateY(20px);
    transition: all 0.5s ease;
}

.ranking-list li.show {
    opacity: 1;
    transform: translateY(0);
}

.ranking-list li.top-rank {
    /* Enhanced Base for Top 3 */
    border-width: 2px;
    margin: 10px 0;
    transform-origin: center;
}

.ranking-list li.rank-1 {
    background: linear-gradient(90deg, rgba(255, 215, 0, 0.3), rgba(255, 215, 0, 0.6)) !important;
    border: 2px solid #ffd700 !important;
    color: #fff !important;
    font-size: 1.5rem !important;
    box-shadow: 0 0 20px rgba(255, 215, 0, 0.5), inset 0 0 10px rgba(255, 215, 0, 0.5);
    animation: glowGold 1.5s infinite alternate;
    margin-bottom: 15px;
    transform: scale(1.05);
}

.ranking-list li.rank-2 {
    background: linear-gradient(90deg, rgba(192, 192, 192, 0.3), rgba(192, 192, 192, 0.6)) !important;
    border: 2px solid #c0c0c0 !important;
    color: #fff !important;
    font-size: 1.3rem !important;
    box-shadow: 0 0 15px rgba(192, 192, 192, 0.4);
    margin-bottom: 10px;
}

/* Flag Icons */
.winner-flag {
    height: 120px;
    /* Big flag! */
    vertical-align: middle;
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    border: 3px solid rgba(255, 255, 255, 0.1);
}

.rank-flag {
    height: 1.1em;
    vertical-align: middle;
    margin-right: 8px;
    border-radius: 2px;
}

#winner-name {
    font-size: 3rem;
    font-weight: 900;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 15px;
    margin-bottom: 30px;
}

.winner-id {
    color: var(--text-color);
    text-shadow: 0 0 15px rgba(255, 255, 255, 0.3);
    letter-spacing: -1px;
}

.success-emoji {
    font-size: 5rem;
    display: block;
    animation: successPop 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    margin-bottom: 10px;
}

.success-time {
    font-size: 1.5rem;
    font-weight: 800;
    color: var(--accent);
    text-shadow: 0 0 10px var(--accent);
    animation: slideUpFade 0.7s ease-out 0.3s both;
    font-family: var(--font-heading);
}

@keyframes slideUpFade {
    0% {
        transform: translateY(20px);
        opacity: 0;
    }

    100% {
        transform: translateY(0);
        opacity: 1;
    }
}

@keyframes successPop {
    0% {
        transform: scale(0);
        opacity: 0;
    }

    100% {
        transform: scale(1);
        opacity: 1;
    }
}

@keyframes glowGold {
    0% {
        box-shadow: 0 0 20px rgba(255, 215, 0, 0.5);
        border-color: #ffd700;
    }

    100% {
        box-shadow: 0 0 40px rgba(255, 215, 0, 0.8), 0 0 20px white;
        border-color: #fff;
    }
}

/* Enhanced Winner Message */
.winner-message-box {
    background: linear-gradient(135deg, #fff, #f0f0f0);
    color: #333;
    padding: 20px 30px;
    border-radius: 20px;
    border: 1px solid rgba(0, 0, 0, 0.1);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
    margin-top: 15px !important;
    position: relative;
    max-width: 90%;
    animation: popIn 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    text-align: center;
}

.winner-message-box.hidden {
    display: none !important;
}

#winner-input-container {
    display: flex;
    gap: 10px;
    margin-top: 10px;
    z-index: 10;
}

#winner-msg-input {
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid var(--primary);
    color: white;
    padding: 8px 15px;
    border-radius: 20px;
    width: 250px;
    font-family: var(--font-body);
}

#btn-send-msg {
    background: transparent;
    color: var(--primary);
    border: 1px solid var(--primary);
    padding: 8px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 1.2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

#btn-send-msg:hover {
    background: rgba(0, 243, 255, 0.1);
}

.winner-message-box::before {
    /* Speech bubble tail outer */
    content: '';
    position: absolute;
    top: -24px;
    left: 50%;
    transform: translateX(-50%);
    border-width: 0 20px 20px;
    border-style: solid;
    border-color: transparent transparent rgba(0, 0, 0, 0.05);
    display: block;
    width: 0;
}

.winner-message-box::after {
    /* Speech bubble tail inner */
    content: '';
    position: absolute;
    top: -14px;
    left: 50%;
    transform: translateX(-50%);
    border-width: 0 14px 14px;
    border-style: solid;
    border-color: transparent transparent #fff;
    display: block;
    width: 0;
}

#winner-msg {
    font-size: 1.8rem;
    font-weight: 800;
    font-family: var(--font-body);
    margin: 0;
    line-height: 1.2;
    background: linear-gradient(45deg, #333, #000);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

@keyframes popIn {
    0% {
        transform: scale(0);
        opacity: 0;
    }

    80% {
        transform: scale(1.1);
        opacity: 1;
    }

    100% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Winner View Enhancements */
.winner-crown {
    font-size: 7rem;
    margin-bottom: 10px;
    filter: drop-shadow(0 0 25px rgba(255, 215, 0, 0.9));
    animation: floating 3s ease-in-out infinite;
}

@keyframes floating {

    0%,
    100% {
        transform: translateY(0) rotate(-5deg);
    }

    50% {
        transform: translateY(-20px) rotate(5deg);
    }
}

/* Tracker Updates */
.tracker-container {
    display: flex;
    flex-direction: row;
    /* Horizontal */
    align-items: center;
    justify-content: space-between;
    width: 90%;
    margin: 5px auto;
    gap: 15px;
}

.q-tracker {
    display: flex;
    flex: 1;
    /* Fill space */
    height: 12px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
    padding: 2px;
    gap: 2px;
}

.q-slot {
    flex: 1;
    /* Stretch to fill */
    width: auto;
    height: 100%;
    background: #444;
    border-radius: 1px;
    transition: all 0.3s;
}

.q-slot.filled {
    background: var(--accent);
    box-shadow: 0 0 5px var(--accent);
}

.q-slot.current {
    background: #fff;
    transform: scaleY(1.4);
    box-shadow: 0 0 8px #fff;
    z-index: 2;
}

.q-num-tracker {
    font-family: var(--font-heading);
    font-size: 1.1rem;
    color: #ccc;
    white-space: nowrap;
    min-width: 50px;
    text-align: right;
}

/* Responsive Adjustments for Small Heights */
@media (max-height: 700px) {
    header {
        height: 35px;
        padding: 0 10px;
    }

    .brand {
        font-size: 0.9rem;
    }

    .phase-timeline {
        padding: 2px 8px;
        gap: 8px;
    }

    /* Compact Play View */
    .game-header {
        padding: 2px 10px;
        height: 40px;
    }

    .game-timer {
        font-size: 1.2rem;
        margin-bottom: 0;
    }

    .problem-display {
        min-height: 80px;
        margin: 2px 15px;
    }

    .question {
        font-size: 1.3rem;
        margin-bottom: 2px;
    }

    .answer-box {
        height: 30px;
        font-size: 1.2rem;
        width: 140px;
    }

    .keypad {
        padding: 5px 10px;
    }

    .key {
        font-size: 1.2rem;
        border-radius: 4px;
    }

    footer {
        min-height: 25px;
        padding: 2px;
        font-size: 0.6rem;
    }

    .country-stats {
        max-height: 25px;
        gap: 4px 8px;
    }

    .country-stats img {
        height: 12px;
    }

    /* Compact Wait View */
    #globe-container {
        width: 120px;
        height: 120px;
        margin: 5px auto;
    }

    #login-form {
        margin-top: 10px;
        gap: 8px;
    }

    #nickname {
        padding: 10px;
        font-size: 1rem;
    }

    .select-selected {
        padding: 8px 12px;
        font-size: 1rem;
    }
}