/* Jurassic Wars — stile cartoon, mobile first (orizzontale) */
:root {
  --ink: #2a1b12;
  --wood: #6b4226;
  --wood-l: #8d5a33;
  --sand: #e7c07c;
  --amber: #ffa21f;
  --amber-l: #ffd27a;
  --leaf: #4caf50;
  --leaf-d: #2e7d32;
  --blue: #2f8cff;
  --red: #ff4a3d;
  --panel: #2b1e14;
  --panel-l: #3b2a1c;
  --text: #fff6e6;
  --muted: #d9c4a3;
  --sidebar-w: clamp(118px, 17vw, 180px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: #140d07; color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  overscroll-behavior: none;
}
button, select { font: inherit; }

/* ---------------------------------------------------------- schermate */
.screen { position: fixed; inset: 0; display: none; }
.screen.active { display: flex; }

.logo {
  margin: 0; font-weight: 900; letter-spacing: 2px; line-height: .9; text-align: center;
  font-size: clamp(38px, 9vw, 86px); color: var(--amber);
  -webkit-text-stroke: 3px var(--ink); paint-order: stroke fill;
  text-shadow: 0 5px 0 var(--ink), 0 9px 18px rgba(0,0,0,.5);
  transform: rotate(-2deg);
}
.logo span { display: block; color: var(--leaf); font-size: .82em; transform: rotate(2deg) translateX(.3em); }

/* ---------------------------------------------------------- caricamento */
.loading {
  align-items: center; justify-content: center; text-align: center; padding: 16px;
  background:
    radial-gradient(ellipse at 50% 120%, #4a7a2a 0%, transparent 60%),
    radial-gradient(ellipse at 50% -20%, #5a3a1f 0%, transparent 55%),
    linear-gradient(#24170d, #120b06);
}
.loading-inner { width: min(560px, 100%); display: flex; flex-direction: column; align-items: center; gap: 10px; }
.bar {
  width: min(420px, 90%); height: 22px; border-radius: 12px; border: 3px solid var(--ink);
  background: #1a110a; overflow: hidden; box-shadow: 0 4px 0 var(--ink);
}
.bar i {
  display: block; height: 100%; width: 0%;
  background: repeating-linear-gradient(-45deg, var(--amber) 0 12px, #ffb84a 12px 24px);
  background-size: 34px 34px; animation: barmove 0.8s linear infinite;
  transition: width .2s ease-out; border-right: 3px solid var(--ink);
}
@keyframes barmove { to { background-position: 34px 0; } }
.loading-label { margin: 2px 0 0; color: var(--amber-l); font-weight: 700; min-height: 1.3em; }
.tip { margin: 6px 0 0; color: var(--muted); font-size: 14px; max-width: 480px; min-height: 2.6em; }
.tip::before { content: "💡 "; }

.runner { height: 70px; width: 110px; }
.dino-run { width: 110px; height: 74px; animation: bob .3s ease-in-out infinite alternate; }
.dino-run .leg1 { animation: legA .3s ease-in-out infinite alternate; transform-origin: 46px 54px; }
.dino-run .leg2 { animation: legA .3s ease-in-out infinite alternate-reverse; transform-origin: 56px 54px; }
@keyframes bob { to { transform: translateY(-4px); } }
@keyframes legA { from { transform: rotate(-20deg); } to { transform: rotate(20deg); } }

.mission-title { margin: 0; font-size: clamp(28px, 6vw, 48px); font-weight: 900; color: var(--amber); -webkit-text-stroke: 2px var(--ink); paint-order: stroke fill; text-shadow: 0 4px 0 var(--ink); }
.mission-sub { margin: 0; color: var(--muted); font-weight: 600; }
.egg { position: relative; width: 60px; height: 76px; margin: 8px 0; animation: wobble 1.2s ease-in-out infinite; }
.egg-shell { position: absolute; inset: 0; border-radius: 50% 50% 46% 46% / 60% 60% 40% 40%; background: radial-gradient(circle at 35% 30%, #fffaf0, #f1dfb8 60%, #d9bf8a); border: 3px solid var(--ink); }
.egg-shell::after { content: ""; position: absolute; left: 14px; top: 20px; width: 9px; height: 7px; border-radius: 50%; background: #8bc34a; box-shadow: 20px 14px 0 #8bc34a, 6px 32px 0 #8bc34a; }
.egg-crack { position: absolute; left: 8px; right: 8px; top: 34px; height: 10px; border-top: 3px solid var(--ink); clip-path: polygon(0 0, 20% 100%, 40% 0, 60% 100%, 80% 0, 100% 100%, 100% 0); }
@keyframes wobble { 0%, 60%, 100% { transform: rotate(0); } 70% { transform: rotate(-10deg); } 80% { transform: rotate(9deg); } 90% { transform: rotate(-5deg); } }

/* ---------------------------------------------------------- menu */
.menu { align-items: center; justify-content: center; background: linear-gradient(#5aa0d8 0%, #9fd3f2 45%, #e7c07c 46%, #d4a862 100%); overflow: hidden; }
.menu-bg { position: absolute; inset: 0; pointer-events: none; }
.menu-bg canvas { width: 100%; height: 100%; display: block; }
.menu-inner { position: relative; display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 16px; }
.tagline { margin: 0 0 6px; font-weight: 800; color: var(--text); -webkit-text-stroke: 1px var(--ink); paint-order: stroke fill; text-shadow: 0 2px 0 var(--ink); font-size: clamp(14px, 2.5vw, 20px); }
.menu-buttons { display: flex; flex-direction: column; gap: 10px; align-items: stretch; width: min(380px, 92vw); }
.menu-buttons .row { display: flex; gap: 10px; }
.menu-buttons .row > * { flex: 1; }
.version { margin: 4px 0 0; font-size: 12px; color: #4a3020; font-weight: 700; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 14px; border-radius: 12px; border: 3px solid var(--ink);
  background: linear-gradient(#a8743f, var(--wood)); color: var(--text); font-weight: 800;
  box-shadow: 0 4px 0 var(--ink); cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: transform .05s;
  touch-action: manipulation;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.btn.big { font-size: 20px; padding: 12px 18px; }
.btn.small { padding: 6px 10px; font-size: 14px; border-radius: 10px; border-width: 2px; box-shadow: 0 3px 0 var(--ink); }
.btn.primary { background: linear-gradient(#ffc04d, #f08a14); color: var(--ink); }
.btn.good { background: linear-gradient(#7fd66b, #3f9a32); }
.btn.bad { background: linear-gradient(#ff7a6b, #c9372b); }
.btn.active { background: linear-gradient(#ff7a6b, #c9372b); }

.segmented { display: flex; border: 3px solid var(--ink); border-radius: 12px; overflow: hidden; box-shadow: 0 4px 0 var(--ink); }
.segmented button { flex: 1; border: 0; padding: 9px 4px; background: #f3dfb8; color: var(--ink); font-weight: 800; cursor: pointer; }
.segmented button + button { border-left: 2px solid var(--ink); }
.segmented button.on { background: var(--leaf); color: #fff; }

/* ---------------------------------------------------------- partita */
.game { flex-direction: row; background: #000; }
#view { position: relative; flex: 1; min-width: 0; height: 100%; }
#canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

#topbar {
  position: absolute; left: 0; right: 0; top: 0; display: flex; gap: 6px; align-items: center;
  padding: 6px 8px 6px calc(8px + var(--safe-l)); pointer-events: none;
}
#topbar > * { pointer-events: auto; }
#topbar .spacer { flex: 1; pointer-events: none; }
.res {
  display: flex; align-items: center; gap: 5px; padding: 4px 10px; border-radius: 10px;
  background: rgba(30, 20, 12, .82); border: 2px solid var(--ink); font-size: 14px; font-weight: 700;
}
.res b { font-variant-numeric: tabular-nums; }
.res.warn { background: rgba(160, 30, 20, .9); animation: pulse 1s infinite alternate; }
.res small { color: var(--muted); font-weight: 600; }
.gem { width: 12px; height: 14px; background: var(--amber); clip-path: polygon(50% 0, 100% 70%, 50% 100%, 0 70%); display: inline-block; }
@keyframes pulse { to { filter: brightness(1.3); } }

#toasts { position: absolute; top: 46px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 6px; align-items: center; pointer-events: none; width: max-content; max-width: 90%; }
.toast {
  padding: 6px 14px; border-radius: 10px; background: rgba(30, 20, 12, .9); border: 2px solid var(--ink);
  font-weight: 700; font-size: 14px; text-align: center; animation: toastIn .2s ease-out; transition: opacity .4s, transform .4s;
}
.toast.clickable { pointer-events: auto; cursor: pointer; text-decoration: underline dotted; }
.toast.good { background: rgba(46, 125, 50, .92); }
.toast.warn { background: rgba(190, 120, 10, .92); }
.toast.bad { background: rgba(180, 35, 25, .94); }
.toast.out { opacity: 0; transform: translateY(-8px); }
@keyframes toastIn { from { transform: scale(.8); opacity: 0; } }

#selection {
  position: absolute; left: calc(8px + var(--safe-l)); bottom: 8px; max-width: min(420px, 70%);
  background: rgba(30, 20, 12, .9); border: 2px solid var(--ink); border-radius: 12px; padding: 6px; display: flex; flex-direction: column; gap: 6px;
}
#selection[hidden] { display: none; }
.sel-head { display: flex; gap: 8px; align-items: center; }
.sel-icon { width: 46px; height: 46px; border-radius: 8px; border: 2px solid var(--ink); }
.sel-info { display: flex; flex-direction: column; gap: 3px; min-width: 120px; font-size: 14px; }
.sel-info small { color: var(--muted); font-size: 12px; }
.hp { height: 7px; background: #000; border-radius: 4px; overflow: hidden; }
.hp i { display: block; height: 100%; width: 100%; background: #5ee05a; }
.sel-group { display: flex; flex-wrap: wrap; gap: 4px; }
.sel-chip { position: relative; }
.sel-chip canvas { width: 34px; height: 34px; border-radius: 6px; border: 2px solid var(--ink); display: block; }
.sel-chip span { position: absolute; right: -2px; bottom: -4px; font-size: 11px; font-weight: 900; background: var(--ink); padding: 0 3px; border-radius: 4px; }
.sel-btns { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.sel-btns .hint { color: var(--muted); font-size: 12px; }

#place-bar {
  position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%); display: flex; gap: 8px; align-items: center;
  background: rgba(30, 20, 12, .92); border: 2px solid var(--ink); border-radius: 12px; padding: 6px 8px; font-weight: 700; font-size: 14px;
}
#place-bar[hidden] { display: none; }

/* ---------------------------------------------------------- barra laterale */
#sidebar {
  width: calc(var(--sidebar-w) + var(--safe-r)); height: 100%; display: flex; flex-direction: column; gap: 6px;
  padding: 6px calc(6px + var(--safe-r)) 6px 6px;
  background: linear-gradient(90deg, #3b2a1c, #2b1e14); border-left: 3px solid var(--ink);
}
#minimap {
  width: 100%; aspect-ratio: 1; height: auto; display: block; border: 3px solid var(--ink); border-radius: 8px;
  image-rendering: pixelated; background: #000; touch-action: none; flex-shrink: 0; max-height: 34vh; object-fit: contain;
}
.tabs { display: flex; gap: 4px; flex-shrink: 0; }
.tab {
  flex: 1; position: relative; padding: 5px 0; border: 2px solid var(--ink); border-radius: 8px; background: #5a3d26; color: #fff; font-size: 18px; cursor: pointer;
}
.tab.active { background: linear-gradient(#ffc04d, #f08a14); }
.tab-badge { position: absolute; top: -5px; right: -5px; width: 14px; height: 14px; border-radius: 50%; background: #5ee05a; border: 2px solid var(--ink); display: none; }
.tab-badge.on { display: block; animation: pulse .5s infinite alternate; }

#build-list {
  flex: 1; min-height: 0; overflow-y: auto; display: grid; grid-template-columns: 1fr 1fr; gap: 5px; align-content: start;
  scrollbar-width: thin; -webkit-overflow-scrolling: touch;
}
.item {
  position: relative; aspect-ratio: 1; padding: 0; border: 2px solid var(--ink); border-radius: 8px; overflow: hidden;
  background: #22384a; cursor: pointer; color: #fff; touch-action: manipulation;
}
.item[hidden] { display: none; }
.item .icon { width: 100%; height: 100%; display: block; }
.item .prog { position: absolute; inset: 0; background: rgba(0, 0, 0, .6); transform-origin: top; transform: scaleY(0); pointer-events: none; }
.item .name { position: absolute; left: 0; right: 0; top: 0; font-size: 10px; font-weight: 800; padding: 1px 3px; background: rgba(0,0,0,.45); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: left; }
.item .cost { position: absolute; left: 2px; bottom: 2px; font-size: 11px; font-weight: 900; color: var(--amber-l); text-shadow: 0 1px 0 #000, 0 0 3px #000; }
.item .badge { position: absolute; right: 2px; bottom: 2px; font-size: 12px; font-weight: 900; background: var(--blue); border-radius: 6px; padding: 0 4px; }
.item .badge:empty { display: none; }
.item .state { position: absolute; left: 0; right: 0; top: 40%; font-size: 11px; font-weight: 900; text-shadow: 0 1px 2px #000, 0 0 4px #000; pointer-events: none; }
.item .cancel { position: absolute; right: 1px; top: 14px; width: 20px; height: 20px; line-height: 18px; border-radius: 50%; background: #c9372b; border: 2px solid var(--ink); font-size: 10px; display: none; }
.item.working .cancel, .item.ready .cancel { display: block; }
.item.locked { filter: grayscale(1) brightness(.55); }
.item.locked .state { font-size: 9px; padding: 0 2px; }
.item.poor .cost { color: #ff6a5a; }
.item.busy { filter: brightness(.6); }
.item.ready { box-shadow: inset 0 0 0 3px #5ee05a; animation: pulse .5s infinite alternate; }
.item.ready .state { color: #7CFF6B; font-size: 13px; }

/* ---------------------------------------------------------- overlay */
.overlay { position: fixed; inset: 0; background: rgba(10, 6, 3, .72); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 12px; }
.overlay[hidden] { display: none; }
.panel {
  background: linear-gradient(#4a3424, #2b1e14); border: 3px solid var(--ink); border-radius: 18px; padding: 16px 20px;
  display: flex; flex-direction: column; gap: 10px; min-width: min(300px, 92vw); max-width: min(680px, 96vw); max-height: 94vh; overflow-y: auto;
  box-shadow: 0 8px 0 var(--ink), 0 16px 40px rgba(0,0,0,.6);
}
.panel h2 { margin: 0 0 4px; text-align: center; font-size: 28px; font-weight: 900; color: var(--amber); -webkit-text-stroke: 2px var(--ink); paint-order: stroke fill; }
.panel h3 { margin: 6px 0 2px; color: var(--amber-l); }
.panel p, .panel li { color: var(--text); font-size: 14px; line-height: 1.4; }
.panel ul { margin: 0; padding-left: 18px; }
.help .cols { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.opt { display: flex; flex-direction: column; gap: 4px; font-weight: 700; }
.opt.check { flex-direction: row; align-items: center; gap: 8px; }
.opt select { padding: 8px; border-radius: 8px; border: 2px solid var(--ink); background: #f3dfb8; color: var(--ink); font-weight: 700; }
.opt input { width: 20px; height: 20px; }
.note { color: var(--muted) !important; font-size: 12px !important; margin: 0; }
.end h2.win { color: #7CFF6B; }
.end h2.lose { color: #ff6a5a; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.stats div { background: rgba(0,0,0,.3); border-radius: 8px; padding: 6px 10px; font-size: 14px; }
.stats b { display: block; font-size: 20px; color: var(--amber-l); }

/* ---------------------------------------------------------- telefoni */
.side-panel { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 6px; }
.screen { height: 100dvh; }
.menu { overflow-y: auto; }

/* VERTICALE: mappa sopra, barra comandi sotto (come i giochi mobile) */
@media (orientation: portrait) {
  .game { flex-direction: column; }
  #view { flex: 1; min-height: 0; width: 100%; }
  #sidebar {
    width: 100%; height: auto; flex-direction: row; align-items: stretch; gap: 8px;
    border-left: 0; border-top: 3px solid var(--ink);
    padding: 8px calc(8px + var(--safe-r)) calc(8px + env(safe-area-inset-bottom, 0px)) calc(8px + var(--safe-l));
    background: linear-gradient(#3b2a1c, #2b1e14);
  }
  #minimap { width: 112px; height: 112px; max-height: none; flex: 0 0 112px; }
  .tabs .tab { padding: 3px 0; font-size: 16px; }
  #build-list {
    display: flex; overflow-x: auto; overflow-y: hidden; gap: 6px; align-items: stretch;
    scroll-snap-type: x proximity; padding-bottom: 2px;
  }
  .item { flex: 0 0 74px; width: 74px; height: 74px; aspect-ratio: auto; scroll-snap-align: start; }
  .item .name { font-size: 10px; }
  #topbar { padding-top: max(6px, env(safe-area-inset-top, 0px)); }
  #topbar .res { padding: 4px 8px; font-size: 13px; }
  #selection { left: 6px; bottom: 6px; max-width: calc(100% - 12px); }
  .side-panel { min-width: 0; }
  #toasts { top: 50px; }
  .menu-inner { padding: 24px 16px; }
}
@media (orientation: portrait) and (max-width: 420px) {
  #btn-army .lbl { display: none; }
  .res.time { display: none; }
}

/* ORIZZONTALE su schermi bassi (telefono con le barre del browser) */
@media (orientation: landscape) and (max-height: 500px) {
  :root { --sidebar-w: clamp(150px, 22vw, 200px); }
  #minimap { max-height: 30vh; width: auto; align-self: center; }
  #build-list { grid-template-columns: repeat(3, 1fr); gap: 4px; }
  .item .name { font-size: 8.5px; }
  .item .cost { font-size: 10px; }
  .tabs .tab { padding: 2px 0; font-size: 15px; }
  #topbar { padding-top: 4px; }
  #topbar .res { padding: 3px 8px; font-size: 13px; }
  #topbar .btn.small { padding: 4px 8px; font-size: 13px; }
  #selection { bottom: 4px; padding: 4px; }
  .sel-icon { width: 36px; height: 36px; }
  .menu-inner { gap: 6px; padding: 8px; }
  .menu .logo { font-size: clamp(26px, 9vh, 44px); }
  .menu .tagline { display: none; }
  .menu-buttons { gap: 6px; }
  .menu-buttons .btn { padding: 7px 12px; font-size: 15px; }
  .menu-buttons .btn.big { font-size: 17px; }
  .segmented button { padding: 6px 4px; }
  .version { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001s !important; animation-iteration-count: 1 !important; }
}
