:root {
  --page: #ffe3d1;
  --page-dot: #ffd2b8;
  --ink: #3f2a26;
  --ink-soft: #8a6a60;
  --shell: #ffb8a6;
  --shell-hi: #ffd2c5;
  --shell-lo: #e9907c;
  --bezel: #4a3328;
  --panel: #fff7ee;
  --panel-line: #f0d9c8;
  --btn: #fffaf4;
  --btn-lo: #e8cdbb;
  --accent: #ef6f86;
  --accent-lo: #c94d66;
  --fame: #f3a54c;
  --felicita: #ef6f86;
  --energia: #f2c94a;
  --pulizia: #5fc29a;
  --danger: #e2574c;
  --sheet: #fffaf4;
  --backdrop: rgba(63, 42, 38, 0.45);
  --radius: 22px;
  --ping: #fff1c7;
  --ping-ink: #3f2a26;
  --font: 'Pixelify Sans', ui-rounded, system-ui, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --page: #1d1b2e; --page-dot: #252239;
    --ink: #f6ece6; --ink-soft: #b9a9b8;
    --shell: #7b5fb8; --shell-hi: #9479cc; --shell-lo: #5a4290;
    --bezel: #141222; --panel: #2a2640; --panel-line: #3a3555;
    --btn: #34304e; --btn-lo: #1f1c33;
    --sheet: #262238; --backdrop: rgba(0, 0, 0, 0.55);
    --ping: #5a4a2a; --ping-ink: #fff1c7;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --page: #1d1b2e; --page-dot: #252239;
  --ink: #f6ece6; --ink-soft: #b9a9b8;
  --shell: #7b5fb8; --shell-hi: #9479cc; --shell-lo: #5a4290;
  --bezel: #141222; --panel: #2a2640; --panel-line: #3a3555;
  --btn: #34304e; --btn-lo: #1f1c33;
  --sheet: #262238; --backdrop: rgba(0, 0, 0, 0.55);
  --ping: #5a4a2a; --ping-ink: #fff1c7;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font);
  color: var(--ink);
  background-color: var(--page);
  background-image: radial-gradient(var(--page-dot) 2px, transparent 2.5px);
  background-size: 22px 22px;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
img.pix, canvas { image-rendering: pixelated; image-rendering: crisp-edges; }
[data-icon] { display: inline-flex; }
[data-icon] img { width: 24px; height: 24px; }
[hidden] { display: none !important; }

.app {
  max-width: 460px; margin: 0 auto;
  padding-block: max(14px, env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-bottom, 0px));
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  display: flex; flex-direction: column; gap: 12px;
}

/* intestazione */
.top { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.who { background: none; border: 0; padding: 4px 0; text-align: left; display: flex; flex-direction: column; min-width: 0; }
.who-name { font-size: 28px; font-weight: 700; line-height: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who-age { font-size: 14px; color: var(--ink-soft); margin-top: 4px; }
.coins {
  display: flex; align-items: center; gap: 6px; padding: 6px 12px 6px 8px;
  background: var(--panel); border: 3px solid var(--bezel); border-radius: 999px;
  font-size: 18px; box-shadow: 0 3px 0 var(--bezel);
}
.coins.bump { animation: bump .35s; }
@keyframes bump { 40% { transform: scale(1.15); } }

/* il "tamagotchi" */
.device {
  background: linear-gradient(160deg, var(--shell-hi), var(--shell) 45%, var(--shell-lo));
  border: 3px solid var(--bezel);
  border-radius: 34px 34px 46px 46px;
  padding: 16px 14px 14px;
  box-shadow: 0 6px 0 var(--bezel), inset 0 3px 0 rgba(255, 255, 255, .35);
  display: flex; flex-direction: column; gap: 12px;
}
.screen {
  position: relative; border: 3px solid var(--bezel); border-radius: 16px; overflow: hidden;
  background: var(--bezel); box-shadow: inset 0 0 0 3px rgba(0, 0, 0, .25);
}
#room { display: block; width: 100%; height: auto; aspect-ratio: 4 / 3; touch-action: manipulation; cursor: pointer; }

.toast {
  position: absolute; left: 50%; top: 10px; transform: translate(-50%, -8px);
  background: var(--panel); color: var(--ink); border: 3px solid var(--bezel); border-radius: 12px;
  padding: 6px 12px; font-size: 16px; white-space: nowrap; max-width: 92%; overflow: hidden; text-overflow: ellipsis;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.away {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px; padding: 16px; text-align: center; background: rgba(29, 27, 46, .72); color: #fff;
}
.away-title { margin: 0; font-size: 22px; font-weight: 700; }
.away-text { margin: 0 0 6px; font-size: 15px; opacity: .9; max-width: 30ch; }

/* barre */
.stats {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px;
  background: var(--panel); border: 3px solid var(--bezel); border-radius: 14px; padding: 10px 12px;
}
.stat { display: grid; grid-template-columns: 24px 1fr; grid-template-rows: auto auto; column-gap: 6px; align-items: center; }
.stat [data-icon] { grid-row: span 2; }
.stat .label { font-size: 14px; line-height: 1; color: var(--ink-soft); }
.bar { height: 12px; border: 2px solid var(--bezel); border-radius: 4px; background: var(--panel-line); overflow: hidden; margin-top: 3px; }
.bar i { display: block; height: 100%; width: 50%; transition: width .5s; background: var(--c); box-shadow: inset 0 -3px 0 rgba(0, 0, 0, .12); }
.stat[data-stat="fame"] { --c: var(--fame); }
.stat[data-stat="felicita"] { --c: var(--felicita); }
.stat[data-stat="energia"] { --c: var(--energia); }
.stat[data-stat="pulizia"] { --c: var(--pulizia); }
.stat.low .bar i { background: var(--danger); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: .45; } }

/* bottoni azione */
.actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.act {
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 9px 4px 7px;
  background: var(--btn); border: 3px solid var(--bezel); border-radius: 16px; font-size: 16px;
  box-shadow: 0 4px 0 var(--bezel); transition: transform .06s, box-shadow .06s;
}
.act [data-icon] img { width: 32px; height: 32px; }
.act:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--bezel); }
.act:disabled { opacity: .5; }
.act.ping { animation: ping 1.2s ease-in-out infinite; }
@keyframes ping { 50% { background: var(--ping); color: var(--ping-ink); } }
.hint { margin: -2px 0 0; text-align: center; font-size: 14px; color: var(--bezel); opacity: .7; }

.foot { text-align: center; font-size: 14px; color: var(--ink-soft); padding-bottom: 14px; }
.foot a { color: inherit; }

/* versione in basso a destra (come peonies) */
.version { position: fixed; right: max(10px, env(safe-area-inset-right, 0px)); bottom: max(8px, env(safe-area-inset-bottom, 0px));
  margin: 0; font-size: 12px; color: var(--ink-soft); opacity: .75; pointer-events: none; z-index: 5; font-variant-numeric: tabular-nums; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

/* bottoni generici */
.btn {
  border: 3px solid var(--bezel); border-radius: 14px; padding: 10px 16px; background: var(--btn);
  font-size: 17px; box-shadow: 0 4px 0 var(--bezel); transition: transform .06s, box-shadow .06s;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--bezel); }
.btn.primary { background: var(--accent); color: #fff; }
.btn.big { font-size: 22px; padding: 12px 20px; width: 100%; }
.btn:disabled { opacity: .5; pointer-events: none; }
.btn.danger { background: var(--danger); color: #fff; }

/* foglio a comparsa */
.sheet-wrap { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.backdrop { position: absolute; inset: 0; background: var(--backdrop); animation: fade .2s; }
.sheet {
  position: relative; width: 100%; max-width: 480px; max-height: 82dvh; overflow: auto;
  background: var(--sheet); border: 3px solid var(--bezel); border-bottom: 0; border-radius: 24px 24px 0 0;
  padding: 14px 16px max(18px, env(safe-area-inset-bottom)); animation: up .22s ease-out;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.sheet-head h2 { margin: 0; font-size: 24px; }
.x { background: var(--btn); border: 3px solid var(--bezel); border-radius: 12px; width: 40px; height: 40px; font-size: 18px; }

.list { display: flex; flex-direction: column; gap: 10px; }
.item {
  display: grid; grid-template-columns: 40px 1fr auto; gap: 10px; align-items: center; width: 100%; text-align: left;
  background: var(--panel); border: 3px solid var(--bezel); border-radius: 14px; padding: 10px;
}
.item [data-icon] img, .item img.pix { width: 40px; height: 40px; }
.item b { display: block; font-size: 17px; }
.item small { display: block; font-size: 14px; color: var(--ink-soft); }
.item .price { display: flex; align-items: center; gap: 4px; font-size: 16px; white-space: nowrap; }
.item .price img { width: 20px; height: 20px; }
.item:disabled { opacity: .5; }
.item.on { outline: 3px solid var(--accent); outline-offset: -6px; }
.note { font-size: 15px; color: var(--ink-soft); margin: 0 0 10px; }
.section-title { font-size: 15px; color: var(--ink-soft); margin: 14px 0 6px; text-transform: uppercase; letter-spacing: .06em; }

.grid-shop { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.shop-item {
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 10px 6px;
  background: var(--panel); border: 3px solid var(--bezel); border-radius: 14px; font-size: 15px; text-align: center;
}
.shop-item img.pix { width: 48px; height: 48px; }
.shop-item .tag { font-size: 14px; padding: 2px 8px; border-radius: 999px; background: var(--btn); border: 2px solid var(--bezel); display: inline-flex; align-items: center; gap: 4px; }
.shop-item .tag img { width: 16px; height: 16px; }
.shop-item.on { background: var(--ping); color: var(--ping-ink); }
.shop-item.cant { opacity: .55; }

.kv { display: grid; grid-template-columns: 1fr auto; gap: 6px 12px; font-size: 16px; background: var(--panel); border: 3px solid var(--bezel); border-radius: 14px; padding: 12px; }
.kv span:nth-child(odd) { color: var(--ink-soft); }
.row-btns { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 12px; }
.summary { list-style: none; padding: 0; margin: 0 0 12px; display: flex; flex-direction: column; gap: 6px; font-size: 17px; }

/* minigiochi */
.game { position: fixed; inset: 0; z-index: 30; background: var(--page); display: flex; flex-direction: column; padding: max(10px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom)); }
.game-head { display: grid; grid-template-columns: 44px 1fr auto; gap: 10px; align-items: center; max-width: 480px; width: 100%; margin: 0 auto 10px; }
.game-head h2 { margin: 0; font-size: 22px; }
.game-hud { display: flex; align-items: center; gap: 8px; font-size: 22px; }
.game-hud img { width: 22px; height: 22px; }
.game-stage { position: relative; flex: 1; display: flex; align-items: center; justify-content: center; min-height: 0; }
#gameCanvas {
  height: 100%; max-height: calc(100dvh - 90px); width: auto; max-width: 100%; aspect-ratio: 3 / 4;
  border: 3px solid var(--bezel); border-radius: 16px; touch-action: none; background: #d6a676;
}
.game-big { position: absolute; font-size: 72px; font-weight: 700; color: #fff; text-shadow: 0 4px 0 var(--bezel), 3px 0 0 var(--bezel), -3px 0 0 var(--bezel), 0 -3px 0 var(--bezel); pointer-events: none; }
.game-card {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); width: min(320px, 88%);
  background: var(--sheet); border: 3px solid var(--bezel); border-radius: 20px; padding: 18px; text-align: center;
  box-shadow: 0 6px 0 var(--bezel);
}
.game-card:empty { display: none; }
.game-card h3 { margin: 0 0 8px; font-size: 26px; }
.game-card p { margin: 0 0 12px; font-size: 16px; color: var(--ink-soft); }
.game-card .score { font-size: 44px; font-weight: 700; color: var(--ink); margin: 4px 0 8px; }
.game-card .rewards { display: flex; justify-content: center; gap: 14px; font-size: 18px; margin-bottom: 14px; color: var(--ink); }
.game-card .rewards span { display: inline-flex; align-items: center; gap: 4px; }
.game-card .rewards img { width: 22px; height: 22px; }

/* adozione */
.adopt { position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center; padding: 16px; overflow: auto; background-color: var(--page); background-image: radial-gradient(var(--page-dot) 2px, transparent 2.5px); background-size: 22px 22px; }
.adopt-card { width: 100%; max-width: 400px; background: var(--sheet); border: 3px solid var(--bezel); border-radius: 26px; padding: 22px 18px; box-shadow: 0 6px 0 var(--bezel); display: flex; flex-direction: column; align-items: center; gap: 12px; }
.adopt .kicker { margin: 0; font-size: 15px; color: var(--ink-soft); }
.adopt h1 { margin: 0; font-size: 32px; text-align: center; }
#adoptCanvas { width: 192px; height: 168px; background: var(--panel); border: 3px solid var(--bezel); border-radius: 18px; }
.coats { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.coat { width: 44px; height: 44px; border-radius: 12px; border: 3px solid var(--bezel); padding: 0; position: relative; overflow: hidden; }
.coat[aria-checked="true"] { outline: 3px solid var(--accent); outline-offset: 2px; }
.coat-label { font-size: 16px; color: var(--ink-soft); margin: -4px 0 0; }
.name-row { display: grid; grid-template-columns: auto 1fr 48px; gap: 8px; align-items: center; width: 100%; font-size: 17px; }
.name-row input { font: inherit; font-size: 20px; padding: 8px 12px; border: 3px solid var(--bezel); border-radius: 12px; background: var(--panel); color: var(--ink); min-width: 0; }
.dice { height: 48px; border: 3px solid var(--bezel); border-radius: 12px; background: var(--btn); font-size: 22px; }
.fine { margin: 0; font-size: 14px; color: var(--ink-soft); text-align: center; }

@media (min-width: 700px) {
  .app { padding-block-start: 32px; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

.rename { display: flex; gap: 8px; margin-top: 12px; }
.rename input { flex: 1; min-width: 0; font: inherit; font-size: 18px; padding: 8px 12px; border: 3px solid var(--bezel); border-radius: 12px; background: var(--panel); color: var(--ink); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
