body, html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: #e0f7fa;
    font-family: 'Learninglings', 'Fredoka', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    touch-action: none;
}

#ui-container {
    position: absolute;
    top: 10px;
    left: 0;
    right: 0;
    z-index: 10;
    display: flex;
    flex-direction: column;
    align-items: center;
    pointer-events: none;
    max-height: 45vh;
}

#scroll-wrapper {
    pointer-events: auto;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.button-row {
    display: flex;
    gap: 8px;
    pointer-events: auto;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    justify-content: flex-start;
    padding: 10px 20px;
    width: 100%;
    box-sizing: border-box;
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none;  /* IE and Edge */
}

.button-row::-webkit-scrollbar {
    display: none; /* Chrome, Safari and Opera */
}

.decimal-row {
    margin-top: 8px;
}

.root-row {
    margin-top: 8px;
    border-top: 1px solid rgba(0,0,0,0.1);
    padding-top: 10px;
}

.spawn-btn {
    width: 40px;
    height: 40px;
    flex-shrink: 0;
    border: 3px solid white;
    border-radius: 10px;
    color: white;
    font-size: 20px;
    font-weight: bold;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 3px 5px rgba(0,0,0,0.18);
    transition: transform 0.08s;
    user-select: none;
}

.spawn-btn:active {
    transform: scale(0.9);
}

.btn-terrible {
    position: relative;
}

.btn-terrible::before {
    content: "😈";
    position: absolute;
    top: -8px;
    right: -8px;
    font-size: 16px;
    z-index: 5;
}

.btn-sm {
    width: 32px;
    height: 32px;
    font-size: 13px;
    border-radius: 8px;
    flex-shrink: 0;
}

.btn-rainbow {
    background: linear-gradient(180deg, #ff0000, #ff8c00, #ffff00, #00cc00, #0066ff, #4b0082, #ee82ee);
    border: 3px solid white;
}

.btn-light-rainbow {
    background: linear-gradient(90deg, #ffb3b3, #ffdbb3, #ffffb3, #b3ffb3, #b3ffff, #b3b3ff, #ffb3ff);
    border: 3px solid white;
}

.mode-row {
    display: flex;
    gap: 10px;
    pointer-events: auto;
}

.mode-btn {
    padding: 8px 15px;
    border: 2px solid #555;
    background: white;
    border-radius: 20px;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.2s;
}

.mode-btn.active {
    background: #333;
    color: white;
}

#mode-split.active {
    background: #4caf50;
    border-color: #4caf50;
}

#mode-delete.active {
    background: #f44336;
    border-color: #f44336;
}

#mode-array.active {
    background: #00acc1;
    color: white;
    border-color: #00838f;
}

#mode-square.active {
    background: #ff5722;
    color: white;
    border-color: #e64a19;
}

#mode-root.active {
    background: #00bcd4;
    color: white;
    border-color: #0097a7;
}

#mode-tan.active {
    background: #e91e63;
    color: white;
    border-color: #ad1457;
}

#mode-power.active {
    background: #673ab7;
    color: white;
    border-color: #4527a0;
}

#mode-pow-xy.active {
    background: #3f51b5;
    color: white;
    border-color: #283593;
}

#mode-fibonacci.active {
    background: #ff9800;
    color: white;
    border-color: #ef6c00;
}

#mode-multiply.active {
    background: #ffc107;
    color: #333;
    border-color: #ffa000;
}

#mode-chess.active {
    background: #9c27b0;
    color: white;
    border-color: #4a148c;
}

#mode-basketball.active {
    background: #ff8c00;
    color: white;
    border-color: #e65100;
}

#mode-soccer.active {
    background: #4caf50;
    color: white;
    border-color: #2e7d32;
}

#exponent-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    z-index: 200;
    display: flex;
    justify-content: center;
    align-items: center;
}

#exponent-overlay.hidden {
    display: none;
}

#exponent-modal {
    background: white;
    padding: 20px;
    border-radius: 15px;
    max-width: 90%;
    max-height: 80%;
    display: flex;
    flex-direction: column;
    gap: 15px;
}

#exponent-modal h3 {
    margin: 0;
    color: #3f51b5;
    text-align: center;
}

#exponent-options {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
    gap: 10px;
    overflow-y: auto;
    padding: 10px;
}

#exponent-options .exponent-btn {
    padding: 10px;
    border: 2px solid #ddd;
    border-radius: 8px;
    background: #f8f8f8;
    cursor: pointer;
    font-weight: bold;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#exponent-options .exponent-btn:hover {
    background: #e0e0e0;
}

#close-exponent {
    padding: 10px;
    background: #f44336;
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: bold;
}

.game-ui {
    position: absolute;
    top: 180px;
    right: 20px;
    z-index: 5;
    pointer-events: none;
}

.game-ui.hidden {
    display: none;
}

.score-board {
    background: rgba(255, 255, 255, 0.95);
    padding: 10px 20px;
    border-radius: 15px;
    border: 3px solid #ff8c00;
    font-size: 22px;
    font-weight: bold;
    color: #333;
    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
    display: flex;
    flex-direction: column;
    align-items: center;
    min-width: 140px;
}

.score-board.soccer-theme {
    border-color: #4caf50;
}

.game-label {
    font-size: 10px;
    letter-spacing: 1px;
    opacity: 0.6;
    margin-bottom: 2px;
}

.high-score-display {
    margin-top: 5px;
    font-size: 14px;
    color: #333;
    text-align: center;
    background: white;
    border-radius: 10px;
    padding: 2px 8px;
    border: 1px solid #ddd;
}

.timer-display {
    color: #f44336;
    font-size: 18px;
}

#chess-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    z-index: 100;
    display: flex;
    justify-content: center;
    align-items: center;
}

#chess-overlay.hidden {
    display: none;
}

#chess-modal {
    background: white;
    padding: 20px;
    border-radius: 15px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.5);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
    max-width: 90vw;
}

#chess-header {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

#chess-header h3 {
    margin: 0;
    color: #9c27b0;
}

#close-chess {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: #666;
}

#chess-board {
    display: grid;
    grid-template-columns: repeat(8, 40px);
    grid-template-rows: repeat(8, 40px);
    border: 4px solid #333;
}

@media (min-width: 600px) {
    #chess-board {
        grid-template-columns: repeat(8, 60px);
        grid-template-rows: repeat(8, 60px);
    }
}

.chess-square {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 30px;
    cursor: pointer;
    user-select: none;
}

@media (min-width: 600px) {
    .chess-square { font-size: 45px; }
}

.chess-square.purple-light { background-color: #d1c4e9; }
.chess-square.purple-dark { background-color: #9575cd; }
.chess-square.white-sq { background-color: #f0d9b5; }
.chess-square.black-sq { background-color: #b58863; }
.chess-square.selected { background-color: #ffeb3b !important; }
.chess-square.last-move { background-color: #ffff33aa !important; }

@keyframes validGlow {
    from { box-shadow: inset 0 0 5px #4caf50; background-color: #c8e6c9; }
    to { box-shadow: inset 0 0 20px #4caf50; background-color: #a5d6a7; }
}

.chess-square.valid-move {
    animation: validGlow 0.8s infinite alternate;
    z-index: 1;
    cursor: pointer;
}

.chess-square.invalid-move {
    background-color: #ffcdd2 !important; /* Soft red for invalid */
    opacity: 0.8;
    cursor: not-allowed;
}



.chess-square.black.invalid-move {
    background-color: #e57373 !important;
}

#chess-status {
    font-weight: bold;
    color: #333;
    margin-bottom: 5px;
}

#chess-chat-container {
    width: 100%;
    max-width: 400px;
    background: #f9f9f9;
    border: 1px solid #ddd;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    padding: 10px;
    gap: 8px;
}

#chess-chat-log {
    height: 80px;
    overflow-y: auto;
    font-size: 13px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    border-bottom: 1px solid #eee;
    padding-bottom: 5px;
}

.chat-msg {
    line-height: 1.4;
}

.chat-msg.user { color: #555; font-style: italic; }
.chat-msg.bot { color: #9c27b0; font-weight: bold; }

#chess-chat-input-row {
    display: flex;
    gap: 5px;
}

#chess-chat-input {
    flex: 1;
    padding: 8px;
    border: 1px solid #ccc;
    border-radius: 4px;
    outline: none;
}

#chess-chat-send {
    padding: 8px 15px;
    background: #9c27b0;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

#chess-chat-send:disabled {
    background: #ccc;
}

@keyframes chessShake {
    0% { transform: translate(1px, 1px) rotate(0deg); }
    20% { transform: translate(-1px, -1px) rotate(1deg); }
    40% { transform: translate(-2px, 0px) rotate(-1deg); }
    60% { transform: translate(2px, 1px) rotate(0deg); }
    80% { transform: translate(1px, -1px) rotate(1deg); }
    100% { transform: translate(-1px, 1px) rotate(-1deg); }
}

.chess-piece {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
}

.chess-piece.shaking {
    animation: chessShake 0.15s infinite;
}

.chess-piece.cracked::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M20 10 L40 50 L10 80 M70 20 L50 60 L90 90 M30 30 L60 10' stroke='rgba(0,0,0,0.5)' stroke-width='3' fill='none'/%3E%3C/svg%3E");
    background-size: contain;
    pointer-events: none;
    z-index: 5;
}



/* Replaced banner with a fullscreen shutdown overlay */
#shutdown-overlay {
    position: fixed;
    inset: 0;
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    background: rgba(0,0,0,0.7); /* darker backdrop but message itself uses 30% visibility */
}

#shutdown-overlay .shutdown-message {
    text-align: center;
    pointer-events: none;
}

#shutdown-overlay .shutdown-main {
    color: rgba(255,255,255,0.3); /* 30% visible white */
    font-weight: 900;
    font-size: clamp(20px, 6vw, 56px);
    letter-spacing: 2px;
    line-height: 1;
    text-transform: uppercase;
    margin-bottom: 8px;
    font-family: 'Fredoka', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

#shutdown-overlay .shutdown-sub {
    color: rgba(255,255,255,0.3);
    font-size: clamp(12px, 2.5vw, 18px);
    opacity: 1;
    font-weight: 700;
}

#canvas-container {
    width: 100%;
    height: 100%;
}

/* Global Chat Panel */
#global-chat {
    position: fixed;
    right: 12px;
    bottom: 12px;
    width: clamp(260px, 36vw, 360px);
    max-height: 60vh;
    background: rgba(255,255,255,0.98);
    border-radius: 12px;
    box-shadow: 0 6px 20px rgba(0,0,0,0.25);
    z-index: 10010;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    pointer-events: auto;
}

#global-chat.hidden { display: none; }

#global-chat .chat-header {
    padding: 8px 12px;
    background: #333;
    color: white;
    font-weight: 700;
    display:flex;
    justify-content:space-between;
    align-items:center;
    gap:8px;
}

#global-chat .chat-log {
    padding: 8px;
    overflow-y: auto;
    flex: 1 1 auto;
    font-size: 13px;
    display:flex;
    flex-direction:column;
    gap:6px;
    background: #fff;
}

.chat-msg { padding:6px 8px; border-radius:8px; max-width:85%; }
.chat-msg.me { align-self: flex-end; background: #e0f2f1; color: #004d40; font-weight:600; }
.chat-msg.other { align-self:flex-start; background: #f1f8e9; color: #33691e; }

/* input row */
#global-chat .chat-input-row {
    display:flex;
    gap:8px;
    padding:8px;
    border-top:1px solid #eee;
    background:#fff;
}
#global-chat input[type="text"] {
    flex:1;
    padding:8px 10px;
    border-radius:8px;
    border:1px solid #ddd;
    outline:none;
}
#global-chat button.send {
    padding:8px 12px;
    border-radius:8px;
    background:#9c27b0;
    color:white;
    border:none;
    font-weight:700;
}

/* Shutdown activated state: force full blackout and disable pointer-events beneath */
body.shutdown-active #shutdown-overlay {
    background: rgba(0,0,0,1);
    pointer-events: auto;
}
body.shutdown-active #shutdown-overlay .shutdown-main,
body.shutdown-active #shutdown-overlay .shutdown-sub {
    color: rgba(255,255,255,1);
    opacity: 1;
}

#jumpscare-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1000;
    background: black;
    display: flex;
    justify-content: center;
    align-items: center;
    pointer-events: none;
}

#jumpscare-overlay.hidden {
    display: none;
}

#jumpscare-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    animation: jitter 0.1s infinite;
}

@keyframes jitter {
    0% { transform: translate(0,0) scale(1); filter: hue-rotate(0deg); }
    25% { transform: translate(5px, -5px) scale(1.1); filter: hue-rotate(90deg) invert(1); }
    50% { transform: translate(-5px, 5px) scale(1.05); filter: hue-rotate(180deg); }
    75% { transform: translate(10px, 10px) scale(1.2); filter: hue-rotate(270deg) invert(1); }
    100% { transform: translate(-10px, -10px) scale(1.1); filter: hue-rotate(360deg); }
}

.glitch-active {
    animation: screenGlitch var(--glitch-speed, 0.2s) infinite;
}

.glitch-red {
    animation: screenGlitchRed var(--glitch-speed, 0.15s) infinite !important;
}

.shake-active {
    animation: screenShake var(--glitch-speed, 0.2s) infinite !important;
}

@keyframes screenShake {
    0% { transform: translate(0, 0) rotate(0); }
    10% { transform: translate(calc(var(--shake-intensity, 1) * -2px), calc(var(--shake-intensity, 1) * 2px)) rotate(calc(var(--shake-intensity, 1) * -0.5deg)); }
    20% { transform: translate(calc(var(--shake-intensity, 1) * -5px), 0px) rotate(calc(var(--shake-intensity, 1) * 0.5deg)); }
    30% { transform: translate(calc(var(--shake-intensity, 1) * 5px), calc(var(--shake-intensity, 1) * 4px)) rotate(0deg); }
    40% { transform: translate(calc(var(--shake-intensity, 1) * 2px), calc(var(--shake-intensity, 1) * -2px)) rotate(calc(var(--shake-intensity, 1) * 0.5deg)); }
    50% { transform: translate(calc(var(--shake-intensity, 1) * -2px), calc(var(--shake-intensity, 1) * 4px)) rotate(calc(var(--shake-intensity, 1) * -0.5deg)); }
    60% { transform: translate(calc(var(--shake-intensity, 1) * -5px), calc(var(--shake-intensity, 1) * 2px)) rotate(0deg); }
    70% { transform: translate(calc(var(--shake-intensity, 1) * 5px), calc(var(--shake-intensity, 1) * 2px)) rotate(calc(var(--shake-intensity, 1) * -0.5deg)); }
    80% { transform: translate(calc(var(--shake-intensity, 1) * -2px), calc(var(--shake-intensity, 1) * -2px)) rotate(calc(var(--shake-intensity, 1) * 0.5deg)); }
    90% { transform: translate(calc(var(--shake-intensity, 1) * 2px), calc(var(--shake-intensity, 1) * 4px)) rotate(0deg); }
    100% { transform: translate(calc(var(--shake-intensity, 1) * 2px), calc(var(--shake-intensity, 1) * -2px)) rotate(calc(var(--shake-intensity, 1) * -0.5deg)); }
}

#jumpscare-image.red-tint {
    filter: sepia(1) saturate(20) hue-rotate(-50deg) brightness(0.7) contrast(3) !important;
}

@keyframes screenGlitch {
    0% { filter: none; transform: translate(0); }
    20% { filter: hue-rotate(90deg) contrast(2); transform: translate(-5px, 2px); }
    40% { filter: invert(1) brightness(2); transform: translate(5px, -2px); }
    60% { filter: sepia(1) saturate(5); transform: translate(-2px, 5px); }
    80% { filter: blur(2px) contrast(3); transform: translate(2px, -5px); }
    100% { filter: none; transform: translate(0); }
}

@keyframes screenGlitchRed {
    0% { filter: none; background-color: transparent; }
    25% { filter: drop-shadow(0 0 10px red) contrast(4); background-color: rgba(255, 0, 0, 0.4); }
    50% { filter: invert(0.2) sepia(1) saturate(10) hue-rotate(-50deg); background-color: rgba(150, 0, 0, 0.6); }
    75% { filter: brightness(0.5) contrast(5); background-color: rgba(255, 50, 50, 0.3); }
    100% { filter: none; background-color: transparent; }
}

canvas {
    display: block;
}