/* Font */
@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');

/* Stili Generali */
:root {
    /* Brand palette */
    --brand-purple: #9d4c9b;
    --brand-blue:   #146aab;
    /* Lightened tints for backgrounds (avoid pure colors) */
    --brand-purple-tint: #c98cc8; /* ~lighter purple */
    --brand-blue-tint:   #5aa8d3; /* ~lighter blue */
    /* UI helpers */
    --ui-bg: linear-gradient(135deg, var(--brand-purple-tint) 0%, var(--brand-blue-tint) 100%);
    --ui-accent: #ffffff;
    --ui-button: #2d7fbe; /* mid blue for buttons */
    --ui-button-hover: #4f9cd0;
}
body { margin: 0; overflow: hidden; background: var(--ui-bg); font-family: 'Press Start 2P', cursive; }

/* MODIFICATO: Aggiunta regola per anti-aliasing su tutte le immagini */
img {
    image-rendering: -moz-crisp-edges;      /* Firefox */
    image-rendering: -webkit-crisp-edges;   /* Webkit (Chrome, Safari) */
    image-rendering: pixelated;             /* Standard Moderno */
}

/* Contenitore e Gioco */
#game-frame { position: absolute; }
#game-container { position: relative; background-color: #444; overflow: hidden; }
.tile { position: absolute; width: 32px; height: 32px; box-sizing: border-box; image-rendering: -moz-crisp-edges; image-rendering: -webkit-crisp-edges; image-rendering: pixelated; }
# Ensure inner <img> used by runtime patch fills the tile properly
.tile > img { width: 100%; height: 100%; display: block; image-rendering: pixelated; }
#character { position: absolute; width: 32px; height: 32px; background-image: url('../assets/main_idle.gif'); background-size: cover; z-index: 10; image-rendering: pixelated; }
#character.walking { background-image: url('../assets/main_walk_1.png'); }
.tile-wall {}

/* Stili CRT */
.container { display: flex; flex-direction: column-reverse; position: absolute; }
#game-frame.screen { box-sizing: content-box; clip-path: url(#crtPath); width: 100%; height: 100%; position: relative; }
#game-frame > #game-container.contents { box-sizing: border-box; width: 100%; height: 100%; position: absolute; top: 0; left: 0; padding: 0; }
.scanlines:before, .scanlines:after { display: block; pointer-events: none; content: ''; position: absolute; }
.scanlines:before { top: 0; left: 0; width: 100%; height: 0.2rem; z-index: 2147483649; opacity: 0.75; }
.scanlines:after { top: 0; right: 0; bottom: 0; left: 0; z-index: 2147483648; }
#game-frame.screen.scanlines:before { background: rgba(0, 0, 0, 0.14); animation: scan-moving 12s linear infinite; }
#game-frame.screen.scanlines:after { background: linear-gradient(to bottom, transparent 50%, rgba(0, 0, 0, 0.11) 51%); background-size: 100% 0.4rem; animation: scan-crt 1s steps(15) infinite; }
.game-world-visuals { position: relative; width: 100%; height: 100%; animation: crt-power-on 0.5s forwards linear; }
body { text-shadow: 0 0 .08em #fff, -.08em 0 .08em #0ff, .08em 0 .08em #f0f; }
#game-frame.no-crt.screen.scanlines:before, #game-frame.no-crt.screen.scanlines:after { display: none; }

/* Menù */
#start-menu { 
    position: absolute; top: 0; left: 0; width: 100%; height: 100%; 
    display: flex; flex-direction: column;
    background: var(--ui-bg);
    z-index: 200; color: white; 
}
#start-menu-top {
    flex: 1; display: flex; flex-direction: column;
    justify-content: center; align-items: center; gap: 12px;
    /* Non tagliare le immagini: permetti di vedere tutto */
    overflow: visible;
}
#start-menu-bottom {
    flex: 2; display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding-bottom: 10%;
}
#logo {
    max-width: none;
    height: auto;
    image-rendering: pixelated;
    /* Scala verso il basso (origine in alto) per non uscire sopra */
    transform-origin: top center;
    /* Spazi fissi sopra/sotto (non variano con l'animazione) */
    margin-top: clamp(96px, 12vh, 220px);
    margin-bottom: clamp(56px, 8vh, 150px);
    /* Pulsazione su scala: base ~2x, +/-10% ogni 2s per fase (4s ciclo completo) */
    animation: logo-pulse 4s ease-in-out infinite;
}
#sub-logo {
    /* Dimensioni richieste e padding superiore */
    width: 70px;
    height: 70px;
    padding-top: 20px;
    object-fit: contain;
    image-rendering: pixelated;
    display: block;
    margin-left: auto; margin-right: auto;
    margin-bottom: clamp(12px, 4vh, 64px);
    filter: drop-shadow(0 2px 0 rgba(0,0,0,0.35));
}
.menu-button { 
    background-color: var(--ui-button); color: white; 
    border: 3px solid white; cursor: pointer; 
    padding: 0.8em 2.5em; font-family: inherit;
    font-size: 1em; margin-bottom: 1.5em; 
    min-width: 250px; text-align: center;
    transition: background-color 0.2s;
}
.menu-button:hover {
    background-color: var(--ui-button-hover);
}

/* MODIFICATO: Stili condivisi per i testi dell'UI */
#level-display, #menu-text {
    position: absolute;
    top: 3%;
    font-size: 12px;
    color: white;
    z-index: 20;
    text-shadow: -1px -1px 0 black, 1px -1px 0 black, -1px 1px 0 black, 1px 1px 0 black;
}
/* Posizione per il livello */
#level-display {
    left: 8%;
}
/* Posizione per il menu */
#menu-text {
    right: 8%;
    pointer-events: auto;
    cursor: pointer;
}

/* Indicatore Chiavi in basso a sinistra (centrato nella riga di tile) */
#keys-display {
    position: absolute;
    left: 8%;
    bottom: 0;            /* aggancia alla riga più bassa */
    height: 32px;         /* altezza di una tile */
    line-height: 32px;    /* centra verticalmente il testo */
    font-size: 12px;
    color: white;
    z-index: 20;
    text-shadow: -1px -1px 0 black, 1px -1px 0 black, -1px 1px 0 black, 1px 1px 0 black;
}

/* Timer in basso a destra */
#timer-display {
    position: absolute;
    right: 8%;
    bottom: 0;
    height: 32px;
    line-height: 32px;
    font-size: 12px;
    color: white;
    z-index: 20;
    text-shadow: -1px -1px 0 black, 1px -1px 0 black, -1px 1px 0 black, 1px 1px 0 black;
}

/* Icona chiave per l'indicatore */
.key-indicator-icon {
    width: 24px;
    height: 24px;
    vertical-align: middle;
    margin-left: 8px;
    image-rendering: pixelated;
}

/* Rendi tutto non selezionabile (testi e immagini) */
*, *::before, *::after {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

#menu-container { display: flex; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: rgba(0, 0, 0, 0.9); color: white; border: 2px solid white; z-index: 100; font-family: 'Press Start 2P', cursive; flex-direction: column; justify-content: center; align-items: center; padding: 1.2em 1.6em; gap: 1em; min-width: 260px; }
#menu-container button { background-color: #333; color: white; border: 2px solid #888; cursor: pointer; padding: 0.8em 1.5em; text-align: center; font-size: 0.9em; font-family: inherit; }
#menu-container button:hover { border-color: white; background-color: #444; }

/* Modal */
#interaction-modal-overlay, #dialogue-modal-overlay, #menu-modal-overlay, #intro-modal-overlay, #outro-modal-overlay { 
    position: fixed; top: 0; left: 0; width: 100%; height: 100%; 
    background-color: rgba(0, 0, 0, 0.85); 
    display: flex; justify-content: center; align-items: center; 
    z-index: 300;
    text-shadow: none;
}
#interaction-modal { 
    background-color: #222; color: white; border: 3px solid white; 
    padding: 2em; width: 80vw; height: 80vh; max-width: 700px;
    max-height: 400px; text-align: center; 
    display: flex; flex-direction: column; justify-content: center; align-items: center;
}
#interaction-modal p { line-height: 1.8; font-size: 1.2em; flex-grow: 1; display: flex; align-items: center; justify-content: center; }
#dialogue-modal { 
    display: grid; background-color: #222; color: white; border: 3px solid white; 
    width: 80vw; height: 80vh; max-width: 900px;
    grid-template-columns: 1fr 2fr; grid-template-rows: auto 1fr; 
}
#dialogue-npc-image-container { grid-column: 1 / 2; grid-row: 1 / 3; padding: 1em; border-right: 2px solid #555; display: flex; align-items: center; justify-content: center; }
#dialogue-npc-image { width: 100%; max-width: 256px; height: auto; object-fit: contain; }
#dialogue-question-container { grid-column: 2 / 3; grid-row: 1 / 2; padding: 1em; border-bottom: 2px solid #555; line-height: 1.5; font-size: 0.9em; overflow-y: auto; }
#dialogue-options-container { grid-column: 2 / 3; grid-row: 2 / 3; padding: 1em; display: flex; flex-direction: column; justify-content: space-around;}
#dialogue-options-container button { background-color: #333; color: white; border: 2px solid #888; cursor: pointer; padding: 0.8em; text-align: left; font-size: 0.8em; font-family: inherit;  line-height: 1.5em;}
#dialogue-options-container button:hover { border-color: white; background-color: #444; }
.modal-button { background-color: #333; color: white; border: 2px solid #888; cursor: pointer; padding: 0.8em 1.5em; text-align: center; font-size: 0.9em; font-family: inherit; }
.modal-button:hover { border-color: white; background-color: #444; }
#modal-text{display:block !important}

/* Intro modal styles */
#intro-modal-overlay, #outro-modal-overlay { background: var(--ui-bg); }
#intro-container, #outro-container { 
  display: flex; flex-direction: column; align-items: center; 
  justify-content: space-between; 
  gap: 16px; padding: 2em; width: 90vw; max-width: 1000px; height: 90vh;
  background: transparent; border: 0;
}
#intro-text, #outro-text { 
  font-size: 1rem; text-align: center; color: white; 
  text-shadow: -1px -1px 0 black, 1px -1px 0 black, -1px 1px 0 black, 1px 1px 0 black;
}
#intro-protagonist, #outro-protagonist { 
  height: 30vh; width: auto; image-rendering: pixelated; 
  display: block; margin: 0 auto; 
}
#intro-start-button, #intro-start-button.modal-button, #outro-back-button, #outro-back-button.modal-button { 
  background-color: var(--ui-button); color: white; border: 3px solid white; 
  padding: 1.1em 3em; font-size: 1.1em; 
}
#intro-start-button:hover, #outro-back-button:hover { background-color: var(--ui-button-hover); }

/* Keyframes */
@keyframes scan-moving { 0% { transform: translate3d(0,100vh,0); } }
@keyframes scan-crt { 0% { background-position: 0 50%; } }
@keyframes crt-power-on { 0% { transform: scale(1,0.8); filter: brightness(15); opacity: 1 } 11% { transform: scale(1,1); filter: contrast(0) brightness(0); opacity: 0; } 100% { transform: scale(1,1); filter: contrast(1) brightness(1.2) saturate(1.3); opacity: 1; } }
@keyframes logo-pulse {
    0%   { transform: scale(1.80); }
    50%  { transform: scale(2.20); }
    100% { transform: scale(1.80); }
}

/* Cursore */
/* Applica il cursore base sia all'area di gioco che ai modal */
.container, 
#interaction-modal-overlay, 
#dialogue-modal-overlay {
    cursor: url('../assets/mano1.png') 0 0, auto;
}

/* Applica il cursore "ingrandito" a TUTTI gli elementi cliccabili, inclusi quelli nei modal */
#menu-text:hover,
.container button:hover,
.container [data-clickable="true"]:hover,
#interaction-modal-overlay button:hover,
#dialogue-modal-overlay button:hover,
#menu-modal-overlay button:hover {
}
/* Stile per l'immagine della chiave nel modal di dialogo */
.key-in-modal {
    width: 32px;
    height: 32px;
    vertical-align: middle; /* Allinea l'immagine al testo */
    margin-left: 10px;
    image-rendering: pixelated;
}

/* Hide native cursor globally; we render a custom one via JS */
html, body, #game-frame, #game-container, .container,
#interaction-modal-overlay, #dialogue-modal-overlay, #menu-modal-overlay, * {
    cursor: none !important;
}

/* Custom cursor element */
#custom-cursor {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 10000;
    pointer-events: none;
    image-rendering: pixelated;
}
#custom-cursor img {
    display: block;
    image-rendering: pixelated;
}
/* Minigame overlay */
#minigame-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: none;
    align-items: center;
    justify-content: center;
    background-color: rgba(0, 0, 0, 0.9);
    z-index: 1200;
    text-shadow: none;
}
#minigame-container {
    background-color: #111;
    color: white;
    border: 3px solid white;
    padding: 2em 1.6em;
    width: 90vw;
    max-width: 520px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.2em;
    text-align: center;
}
#minigame-title {
    font-size: 1.1em;
    text-shadow: -1px -1px 0 black, 1px -1px 0 black, -1px 1px 0 black, 1px 1px 0 black;
}
#minigame-countdown {
    font-size: 1.6em;
}
#minigame-status {
    font-size: 0.9em;
    min-height: 1.2em;
}
#minigame-grid {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(2, minmax(110px, 1fr));
    gap: 1em;
}
.minigame-card {
    position: relative;
    height: 120px;
    min-height: 120px;
    background-color: #222;
    border: 3px solid white;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.8em;
    transition: transform 0.1s ease;
}
.minigame-card:hover {
    transform: scale(1.05);
}
.minigame-card-number {
    pointer-events: none;
}
.minigame-card-image {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: none;
}
.minigame-card.showing .minigame-card-image {
    display: block;
}
.minigame-card.showing .minigame-card-number {
    display: none;
}
.minigame-card.correct {
    border-color: #3fd37f;
}
.minigame-card.wrong {
    border-color: #ff6b6b;
}
#minigame-feedback {
    min-height: 1.2em;
    font-size: 0.9em;
}
#minigame-feedback.success {
    color: #3fd37f;
}
#minigame-feedback.error {
    color: #ff6b6b;
}
#minigame-continue {
    margin-top: 0.5em;
    display: none;
}
#minigame-description {
    font-size: 0.85em;
    line-height: 1.5;
    color: #d0ecff;
}
#minigame-start {
    display: none;
}

/* Memory overlay */
#memory-overlay {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    display: none; align-items: center; justify-content: center;
    background-color: rgba(0,0,0,0.9);
    z-index: 1240;
}
#memory-container {
    background-color: #111;
    color: white;
    border: 3px solid white;
    padding: 1.4em 1.2em;
    width: 92vw;
    max-width: 720px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    text-align: center;
}
#memory-title { font-size: 1.1em; text-shadow: -1px -1px 0 black, 1px -1px 0 black, -1px 1px 0 black, 1px 1px 0 black; }
#memory-instructions { font-size: 0.9em; color: #d0ecff; }
#memory-status { min-height: 1.2em; font-size: 0.9em; }
#memory-grid {
    display: grid;
    grid-template-columns: repeat(4, 96px);
    grid-template-rows: repeat(3, 96px);
    gap: 10px;
}
.memory-card { position: relative; width: 96px; height: 96px; border: 3px solid white; background: #222; cursor: pointer; }
.memory-card.disabled { pointer-events: none; opacity: 0.6; }
.memory-card .card-back, .memory-card .card-front { position: absolute; inset: 0; }
.memory-card .card-back {
    background-image: url('../assets/wall2.gif');
    /* scale by an integer multiple to avoid blur (32px * 3 = 96px) */
    background-size: 96px 96px;
    image-rendering: -moz-crisp-edges;
    image-rendering: -webkit-crisp-edges;
    image-rendering: pixelated;
}
.memory-card .card-front { display: none; align-items: center; justify-content: center; }
.memory-card .card-front img { width: 100%; height: 100%; object-fit: contain; image-rendering: pixelated; }
.memory-card.flipped .card-back { display: none; }
.memory-card.flipped .card-front { display: flex; }
#memory-continue { display: none; }

/* TicTacToe overlay */
#tictactoe-overlay {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    display: none; align-items: center; justify-content: center;
    background-color: rgba(0,0,0,0.9);
    z-index: 1250;
}
#tictactoe-modal.dialogue-modal-grid {
    background-color: #111;
    border: 3px solid white;
    color: white;
}
#tictactoe-modal {
    display: grid;
    width: 80vw; height: 80vh; max-width: 900px;
    grid-template-columns: 1fr 2fr;
    grid-template-rows: 1fr;
}
#ttt-npc-image-container { grid-column: 1 / 2; grid-row: 1 / 2; display: flex; align-items: center; justify-content: center; padding: 1em; border-right: 2px solid #555; }
#ttt-npc-image { width: 100%; max-width: 256px; height: auto; object-fit: contain; image-rendering: pixelated; }
#ttt-content { grid-column: 2 / 3; grid-row: 1 / 2; display: flex; flex-direction: column; gap: 12px; padding: 1em; align-items: center; }
#ttt-title { font-size: 1.05em; text-shadow: -1px -1px 0 black, 1px -1px 0 black, -1px 1px 0 black, 1px 1px 0 black; text-align: center; }
#ttt-status { min-height: 1.2em; font-size: 0.9em; text-align: center; }
#ttt-message { min-height: 1.2em; font-size: 0.95em; text-align: center; }

#ttt-board {
    display: grid;
    grid-template-columns: repeat(5, 64px);
    grid-template-rows: repeat(5, 64px);
    gap: 0;
    justify-content: center;
    align-items: center;
    margin: 16px auto 0 auto; /* sposta leggermente più in basso */
}
.ttt-cell, .ttt-wall { width: 64px; height: 64px; position: relative; image-rendering: pixelated; }
.ttt-wall { background-image: url('../assets/wall2.gif'); background-size: cover; }
.ttt-cell { background: transparent; border: 3px solid transparent; }
.ttt-cell:hover { border-color: rgba(255,255,255,0.25); }
.ttt-cell img { position: absolute; left: 0; top: 0; width: 64px; height: 64px; image-rendering: pixelated; }
.ttt-cell img.ttt-player-mark { left: 2px; top: 2px; width: 60px; height: 60px; }

/* Ensure custom cursor applies */
#tictactoe-overlay, #tictactoe-overlay button {
    cursor: none !important;
}

#ttt-continue { align-self: center; }
