:root {
  --ink: #f5f7ff;
  --muted: #95a8d8;
  --night: #05091c;
  --panel: #0b1334;
  --cyan: #19e8ff;
  --pink: #ff3fa4;
  --yellow: #ffe257;
  --danger: #ff5470;
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; background: var(--night); }

body {
  min-height: 100vh;
  min-height: 100svh;
  color: var(--ink);
  font-family: "Space Grotesk", system-ui, sans-serif;
  overflow-x: hidden;
  background:
    radial-gradient(circle at 20% 0%, rgba(37, 50, 130, .34), transparent 32rem),
    linear-gradient(180deg, #080d25, #030614 75%);
}

button, kbd { font: inherit; }
button { color: inherit; }

.game-shell {
  width: min(1080px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) clamp(12px, 3vw, 42px) max(12px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 12px;
}

.topbar, .hud {
  display: flex;
  align-items: center;
}

.topbar { justify-content: space-between; }

.brand {
  color: var(--ink);
  text-decoration: none;
  font-family: "Press Start 2P", monospace;
  font-size: clamp(10px, 1.2vw, 16px);
  letter-spacing: .02em;
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand b { color: var(--pink); font-weight: inherit; }
.brand-bubble {
  display: grid;
  place-items: center;
  width: 35px;
  height: 35px;
  color: #07122b;
  background: var(--cyan);
  border-radius: 7px 7px 12px 7px;
  box-shadow: 0 0 20px rgba(25,232,255,.42), inset -4px -4px 0 rgba(0, 69, 94, .22);
  font-size: 10px;
  transform: rotate(-3deg);
}

.top-actions { display: flex; gap: 8px; }
.icon-button {
  width: 38px;
  height: 38px;
  border: 1px solid rgba(128, 161, 231, .35);
  border-radius: 9px;
  background: rgba(13, 23, 62, .82);
  cursor: pointer;
  font-weight: 700;
  transition: .18s ease;
}
.icon-button:hover, .icon-button:focus-visible { border-color: var(--cyan); color: var(--cyan); transform: translateY(-1px); }
.icon-button.muted { color: #68769a; text-decoration: line-through; }

.hud {
  width: 100%;
  padding: 10px clamp(12px, 2vw, 26px);
  background: linear-gradient(90deg, rgba(12, 21, 58, .94), rgba(16, 27, 71, .78));
  border: 1px solid rgba(93, 127, 210, .24);
  border-radius: 13px;
  box-shadow: 0 12px 35px rgba(0,0,0,.2);
}

.stat { flex: 1; display: flex; flex-direction: column; gap: 4px; }
.stat:not(:last-child) { border-right: 1px solid rgba(127, 155, 215, .18); }
.stat:not(:first-child) { padding-left: clamp(12px, 3vw, 44px); }
.stat span { color: var(--muted); font-size: clamp(8px, .7vw, 11px); font-weight: 700; letter-spacing: .17em; }
.stat strong { font-family: "Press Start 2P", monospace; font-size: clamp(13px, 1.55vw, 22px); letter-spacing: .08em; }
.lives-stat strong { color: var(--pink); letter-spacing: .18em; text-shadow: 0 0 14px rgba(255,63,164,.5); }
.time-stat strong { color: var(--cyan); }
.time-stat.urgent strong { color: var(--danger); text-shadow: 0 0 15px rgba(255,84,112,.55); animation: timer-alert .75s steps(2, end) infinite; }
.record strong { color: var(--yellow); }
@keyframes timer-alert { 50% { opacity: .55; } }

.game-frame {
  position: relative;
  width: min(100%, calc((100svh - 190px) * .8));
  aspect-ratio: 4 / 5;
  max-height: calc(100svh - 190px);
  margin: 0 auto;
  overflow: hidden;
  background: #08102d;
  border: 2px solid rgba(54, 236, 255, .35);
  border-radius: clamp(10px, 1.4vw, 20px);
  box-shadow: 0 0 0 5px rgba(21, 41, 89, .65), 0 0 48px rgba(18, 205, 255, .13), 0 24px 70px rgba(0,0,0,.45);
}

.game-frame::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(0deg, rgba(255,255,255,.018) 0 1px, transparent 1px 4px);
  mix-blend-mode: screen;
  z-index: 3;
}

canvas { width: 100%; height: 100%; display: block; touch-action: none; }

.screen {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 18px;
  background: radial-gradient(circle at 50% 45%, rgba(20, 29, 81, .45), rgba(2, 5, 18, .88));
  opacity: 0;
  visibility: hidden;
  transform: scale(1.02);
  transition: opacity .25s ease, transform .25s ease, visibility .25s;
}

.screen-visible { opacity: 1; visibility: visible; transform: scale(1); }
.screen-card { text-align: center; width: min(630px, 95%); }
.intro-card {
  width: min(700px, 96%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(16px, 2.4vh, 28px);
}
.intro-main { min-width: 0; }
.intro-imesa-logo {
  display: block;
  width: min(270px, 58vw);
  height: auto;
  margin: 0 auto 9px;
  filter: drop-shadow(0 0 12px rgba(255,255,255,.18));
}
.eyebrow { margin: 0 0 clamp(10px, 1.4vw, 20px); color: var(--cyan); font: clamp(7px, .72vw, 10px)/1.5 "Press Start 2P", monospace; letter-spacing: .22em; }
.intro-card h1, .compact-card h2 {
  margin: 0;
  font-family: "Press Start 2P", monospace;
  line-height: .98;
  letter-spacing: -.05em;
  text-shadow: 5px 5px 0 rgba(39, 58, 136, .7), 0 0 30px rgba(25,232,255,.12);
}
.intro-card h1 { font-size: clamp(30px, 6.4vw, 60px); }
.intro-card h1 span { color: var(--pink); }
.lead { margin: clamp(12px, 2vw, 25px) auto; color: #c5cff0; font-size: clamp(12px, 1.2vw, 18px); line-height: 1.45; }

.game-legend {
  width: min(620px, 100%);
  padding: 15px;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px 10px;
  text-align: left;
  background: linear-gradient(145deg, rgba(14, 27, 69, .88), rgba(8, 14, 43, .82));
  border: 1px solid rgba(67, 221, 255, .34);
  border-radius: 15px;
  box-shadow: inset 0 0 28px rgba(25,232,255,.06), 0 16px 35px rgba(0,0,0,.25);
}
.legend-title {
  grid-column: 1 / -1;
  margin: 0 0 10px;
  text-align: center;
  color: var(--cyan);
  font: 9px/1.2 "Press Start 2P", monospace;
  letter-spacing: .12em;
}
.legend-item {
  min-height: 47px;
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 4px;
  border-top: 1px solid rgba(124, 153, 220, .14);
}
.legend-item img { width: 49px; height: 36px; object-fit: contain; filter: drop-shadow(0 0 7px rgba(255,255,255,.24)); }
.legend-item img.alien-legend { transform: scale(1.35); filter: drop-shadow(0 0 8px rgba(68,255,154,.72)); }
.legend-item span { display: flex; flex-direction: column; gap: 4px; }
.legend-item strong { color: #eaf2ff; font: 8px/1.2 "Press Start 2P", monospace; }
.legend-item small { color: var(--muted); font: 7px/1.2 "Press Start 2P", monospace; }
.legend-item.positive small { color: #66ffc1; }
.legend-item.negative small { color: #ff718a; }
.legend-item.bonus { background: linear-gradient(90deg, rgba(255,211,67,.08), transparent); }
.legend-item.bonus img { filter: drop-shadow(0 0 9px #ffd84d); }
.legend-item.bonus small { color: var(--yellow); }

.how-to { display: flex; justify-content: center; gap: clamp(18px, 4vw, 52px); margin: 18px 0; }
.how-to > div { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: clamp(8px, .76vw, 11px); letter-spacing: .12em; }
.how-to .touch-guide { color: #73ffd0; }
.how-to .touch-guide kbd { color: #73ffd0; border-color: rgba(115, 255, 208, .55); }
.key-stack { display: flex; gap: 4px; }
kbd {
  min-width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  color: var(--cyan);
  background: #101b49;
  border: 1px solid #435d9e;
  border-bottom-width: 3px;
  border-radius: 5px;
  font-weight: 700;
}

.primary-button {
  width: min(360px, 90%);
  min-height: 54px;
  margin: 6px auto 0;
  padding: 0 11px 0 24px;
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  color: #061331;
  border: 0;
  border-radius: 9px;
  background: var(--cyan);
  box-shadow: 0 6px 0 #087d9d, 0 0 30px rgba(25,232,255,.28);
  font: clamp(9px, .85vw, 12px)/1 "Press Start 2P", monospace;
  cursor: pointer;
  transition: transform .12s ease, filter .12s ease;
}
.primary-button i { width: 38px; height: 38px; display: grid; place-items: center; border-radius: 6px; background: rgba(6,19,49,.14); font-style: normal; }
.primary-button:hover, .primary-button:focus-visible { filter: brightness(1.12); transform: translateY(-2px); }
.primary-button:active { transform: translateY(4px); box-shadow: 0 2px 0 #087d9d; }
.tiny-copy { margin: 18px 0 0; color: #6778a8; font: clamp(6px, .58vw, 8px)/1.5 "Press Start 2P", monospace; }

.compact-card {
  padding: clamp(24px, 4vw, 48px);
  background: rgba(9, 16, 47, .91);
  border: 1px solid rgba(52, 231, 255, .35);
  border-radius: 18px;
  box-shadow: 0 24px 80px rgba(0,0,0,.45);
}
.compact-card h2 { font-size: clamp(32px, 5vw, 70px); color: var(--yellow); }
.gameover-card h2 { color: var(--pink); }
.gameover-card.won { border-color: rgba(98, 255, 176, .65); box-shadow: 0 0 55px rgba(98,255,176,.18), 0 24px 80px rgba(0,0,0,.45); }
.gameover-card.won h2 { color: #62ffb0; text-shadow: 5px 5px 0 rgba(15, 104, 83, .72), 0 0 32px rgba(98,255,176,.4); }
.compact-card > p:not(.eyebrow):not(.record-message) { color: #b1bee2; }
.text-button { display: block; margin: 20px auto 0; padding: 5px; background: none; border: 0; color: #8293c0; text-decoration: underline; cursor: pointer; }
.result-grid { display: flex; justify-content: center; gap: 50px; margin: 28px 0 12px; }
.result-grid div { display: flex; flex-direction: column; gap: 9px; }
.result-grid span { color: var(--muted); font-size: 10px; letter-spacing: .15em; }
.result-grid strong { font: clamp(15px, 2vw, 25px)/1 "Press Start 2P", monospace; }
.result-grid div:last-child strong { color: var(--yellow); }
.record-message { min-height: 24px; color: var(--cyan); font-weight: 700; }

.level-banner {
  position: absolute;
  z-index: 6;
  top: 45%;
  left: 50%;
  padding: 15px 26px;
  color: #071029;
  background: var(--yellow);
  border: 4px solid white;
  box-shadow: 8px 8px 0 var(--pink), 0 0 35px rgba(255,226,87,.55);
  font: clamp(15px, 2vw, 28px)/1 "Press Start 2P", monospace;
  opacity: 0;
  transform: translate(-50%, -50%) scale(.6) rotate(-4deg);
  pointer-events: none;
}
.level-banner.show { animation: level-pop 1.35s ease both; }
@keyframes level-pop { 0% { opacity:0; transform:translate(-50%,-50%) scale(.4) rotate(-6deg); } 18%, 72% { opacity:1; transform:translate(-50%,-50%) scale(1) rotate(-2deg); } 100% { opacity:0; transform:translate(-50%,-50%) scale(1.18) rotate(0); } }

.touch-controls {
  display: none;
  position: absolute;
  z-index: 7;
  right: max(12px, env(safe-area-inset-right));
  bottom: 12px;
  gap: 9px;
}
.touch-controls button {
  width: 64px;
  height: 64px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--cyan);
  background: rgba(7, 17, 48, .8);
  border: 2px solid rgba(25,232,255,.6);
  border-radius: 16px;
  box-shadow: inset 0 0 20px rgba(25,232,255,.1);
  touch-action: none;
  user-select: none;
}
.touch-controls button:active, .touch-controls button.active { background: rgba(25,232,255,.23); transform: scale(.95); }
.touch-controls span { font-size: 23px; line-height: .8; }
.touch-controls small { font-size: 8px; font-weight: 700; }

.footer-tip { text-align: center; color: #7181ae; font: clamp(6px, .58vw, 8px)/1.4 "Press Start 2P", monospace; letter-spacing: .06em; }
.pulse-dot { display: inline-block; width: 6px; height: 6px; margin-right: 8px; border-radius: 50%; background: var(--cyan); box-shadow: 0 0 8px var(--cyan); animation: pulse 1.25s infinite; }
@keyframes pulse { 50% { opacity:.35; transform:scale(.7); } }

@media (hover: none), (pointer: coarse) {
  .touch-controls { display: flex; }
  .how-to .keyboard-guide { display: none; }
}

@media (max-width: 700px) {
  .game-shell { padding: 8px; gap: 7px; justify-content: flex-start; }
  .brand-bubble { width: 29px; height: 29px; }
  .icon-button { width: 34px; height: 34px; }
  .hud { padding: 8px; }
  .stat:not(:first-child) { padding-left: 8px; }
  .stat span { letter-spacing: .08em; }
  .stat strong { font-size: clamp(9px, 2.7vw, 13px); }
  .lives-stat strong { letter-spacing: .08em; }
  .game-frame { width: min(100%, calc((100svh - 135px) * .8)); max-height: calc(100svh - 135px); margin: 0 auto; border-radius: 10px; }
  .how-to { display: flex; margin: 4px 0; gap: 0; }
  .how-to .keyboard-guide { display: none; }
  .how-to .touch-guide { font-size: 7px; }
  .how-to kbd { min-width: 22px; height: 22px; }
  .intro-card { width: 99%; display: flex; gap: 7px; }
  .intro-main .eyebrow { display: none; }
  .intro-imesa-logo { width: min(185px, 55vw); margin-bottom: 2px; }
  .intro-card h1 { font-size: clamp(23px, 7.7vw, 30px); line-height: .9; }
  .intro-card .lead { margin: 5px auto; font-size: 8px; line-height: 1.2; }
  .primary-button { min-height: 44px; }
  .primary-button i { width: 31px; height: 31px; }
  .intro-card .primary-button { min-height: 34px; margin-top: 2px; }
  .intro-card .primary-button i { width: 26px; height: 26px; }
  .intro-card .tiny-copy { display: none; }
  .game-legend { margin-top: 5px; padding: 5px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px; border-radius: 8px; }
  .legend-title { grid-column: 1 / -1; margin: 0; font-size: 5px; }
  .legend-item { min-height: 0; padding: 1px; gap: 1px; border: 0; flex-direction: column; text-align: center; }
  .legend-item img { width: 25px; height: 20px; }
  .legend-item span { gap: 1px; }
  .legend-item strong { font-size: 4px; }
  .legend-item small { font-size: 4px; }
  .footer-tip { display: none; }
  .touch-controls { display: flex; }
  .touch-controls button { width: 52px; height: 52px; border-radius: 13px; }
  .compact-card { padding: 10px 16px; border-radius: 12px; }
  .compact-card .eyebrow { margin-bottom: 5px; }
  .compact-card h2 { font-size: clamp(23px, 8vw, 34px); }
  .compact-card > p:not(.eyebrow):not(.record-message) { margin: 7px 0; font-size: 11px; }
  .result-grid { gap: 28px; margin: 10px 0 5px; }
  .result-grid span { font-size: 8px; }
  .result-grid strong { font-size: 13px; }
  .record-message { min-height: 16px; margin: 5px 0; font-size: 10px; line-height: 1.2; }
  .compact-card .primary-button { min-height: 38px; margin-top: 3px; }
  .compact-card .text-button { margin-top: 8px; font-size: 10px; }
}

@media (max-height: 590px) and (orientation: landscape) {
  .game-shell { padding: 5px 10px; gap: 5px; }
  .topbar { position: absolute; z-index: 20; top: 12px; left: 18px; right: 18px; pointer-events: none; }
  .top-actions { pointer-events: auto; }
  .brand { opacity: .72; }
  .hud { position: absolute; top: 9px; left: 50%; z-index: 8; width: min(620px, 62vw); transform: translateX(-50%); background: rgba(6,13,39,.76); pointer-events: none; }
  .game-frame { width: auto; height: calc(100svh - 10px); max-height: none; }
  .footer-tip { display: none; }
}

/* Layout principale per totem e monitor verticali. */
@media (orientation: portrait) and (min-width: 701px) {
  .game-shell { justify-content: flex-start; padding: 18px 16px; }
  .game-frame { width: min(100%, calc((100svh - 185px) * .8)); }
  .intro-card h1 { font-size: clamp(38px, 7vw, 64px); }
  .lead { font-size: clamp(14px, 2vw, 18px); }
  .touch-controls { display: flex; }
}

@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; } }
