/* Pelitila (/pelaa/): koko näytön sovellus puhelimelle ja tabletille. Isot napit, ei tekstiä lapsen polulla. */
:root {
  --paper: #fbf6ec;
  --paper-2: #f4ecdc;
  --ink: #1e2638;
  --crayon: #e8552b;
  --sun: #f2b33d;
  --leaf: #2f8f6b;
  --current: #e53935;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --f-head: 'Bricolage Grotesque', 'Figtree', sans-serif;
  --f-body: 'Figtree', 'Helvetica Neue', Arial, sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  font: 16px/1.4 var(--f-body);
  background-color: var(--paper);
  background-image: radial-gradient(rgba(30, 38, 56, .09) 1px, transparent 1.2px);
  background-size: 22px 22px;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; padding: 0; touch-action: manipulation; }
svg { display: block; }
button svg { fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Kuvavalikko ---------- */
.home {
  display: flex; flex-direction: column;
  height: 100dvh;
  padding: calc(6px + var(--safe-t)) calc(10px + var(--safe-r)) calc(6px + var(--safe-b)) calc(10px + var(--safe-l));
  gap: 8px;
}
.p-top { display: flex; align-items: center; gap: 12px; flex: none; }
.p-brand img { display: block; height: 34px; width: auto; }
.p-stars {
  display: flex; align-items: center; gap: 6px;
  padding: 4px 12px 4px 8px;
  border: 2px solid var(--ink); border-radius: 999px;
  background: #fff; box-shadow: 2px 2px 0 var(--ink);
  font: 800 18px/1 var(--f-head);
}
.p-stars svg { width: 22px; height: 22px; fill: var(--sun); stroke: var(--ink); stroke-width: 1.6; stroke-linejoin: round; }
.p-top-actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.p-icon {
  width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center;
  border: 2px solid var(--ink); background: #fff; box-shadow: 2px 2px 0 var(--ink);
}
.p-icon svg { width: 22px; height: 22px; }
.p-install {
  display: flex; align-items: center; gap: 6px;
  height: 40px; padding: 0 14px;
  border: 2px solid var(--ink); border-radius: 999px;
  background: var(--sun); box-shadow: 2px 2px 0 var(--ink);
  font-weight: 700;
}
.p-install svg { width: 20px; height: 20px; }

/* aiheet: kuvalaatat vaakarivissä */
.p-topics {
  display: flex; gap: 10px;
  flex: none;
  overflow-x: auto; overflow-y: hidden;
  padding: 4px 4px 8px;
  scrollbar-width: none;
}
.p-topics::-webkit-scrollbar { display: none; }
.p-topic {
  flex: none;
  width: 68px; height: 68px;
  border-radius: 18px;
  border: 3px solid var(--ink);
  background: #fff;
  box-shadow: 3px 3px 0 var(--ink);
  overflow: hidden;
  display: grid; place-items: center;
  transition: transform .12s;
}
.p-topic img { width: 100%; height: 100%; object-fit: contain; padding: 4px; }
.p-topic.on { background: var(--sun); transform: translateY(-2px) scale(1.06); box-shadow: 3px 5px 0 var(--ink); }
.p-topic.mine svg { width: 44px; height: 44px; fill: var(--sun); stroke: var(--ink); stroke-width: 1.4; }
.p-topic.mine.on { background: var(--crayon); }

/* kuvat: pystyssä ruudukko, vaakatasossa kaksi riviä sivuttain vieritettynä */
.p-grid {
  flex: 1; min-height: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  grid-auto-rows: max-content;
  gap: 14px;
  align-content: start;
  overflow-y: auto;
  padding: 4px 6px 16px 4px;
  -webkit-overflow-scrolling: touch;
}
.p-card {
  position: relative;
  aspect-ratio: 1240 / 1754;
  border-radius: 14px;
  border: 3px solid var(--ink);
  background: #fff;
  box-shadow: 3px 3px 0 var(--ink);
  overflow: hidden;
  transition: transform .1s;
}
.p-card:active { transform: scale(.96); }
.p-card > img, .p-card > svg { width: 100%; height: 100%; object-fit: contain; display: block; }
.p-card.started { border-color: var(--leaf); }
.p-badge {
  position: absolute; right: 6px; bottom: 6px;
  width: 34px; height: 34px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--sun); border: 2px solid var(--ink);
}
.p-badge svg { width: 22px; height: 22px; fill: #fff; stroke: var(--ink); stroke-width: 1.5; }

@media (orientation: landscape) and (max-height: 600px) {
  .p-topic { width: 56px; height: 56px; border-radius: 14px; }
  /* kaksi riviä sivuttain vieritettynä; sarakeleveys (--cw) lasketaan rivin korkeudesta (play.js) */
  .p-grid {
    grid-template-columns: none;
    grid-template-rows: repeat(2, minmax(0, 1fr));
    grid-auto-flow: column;
    grid-auto-columns: var(--cw, 120px);
    grid-auto-rows: auto;
    overflow-x: auto; overflow-y: hidden;
    align-content: stretch;
    padding-right: 12px;
  }
  .p-card { align-self: center; }
}

/* ---------- Väritys ---------- */
.paint {
  position: fixed; inset: 0;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr auto;
  background: var(--paper-2);
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
}
.p-cell { position: relative; min-width: 0; min-height: 0; display: grid; place-items: center; padding: 10px; }
.p-stage {
  position: relative;
  background: #fff;
  border-radius: 10px;
  overflow: hidden;
  touch-action: none;
  box-shadow: 0 6px 24px -10px rgba(30, 38, 56, .45);
}
.p-view { position: absolute; inset: 0; transform-origin: 0 0; }
.p-view canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.p-view canvas[data-lines] { pointer-events: none; }
.p-loading { position: absolute; inset: 0; display: none; place-items: center; background: #fff; }
.p-loading svg { width: 48px; height: 48px; fill: none; stroke: var(--crayon); stroke-width: 2.5; stroke-linecap: round; animation: spin 1s linear infinite; }
.p-stage.loading .p-loading { display: grid; }
@keyframes spin { to { transform: rotate(360deg); } }

.p-round {
  position: absolute; z-index: 5;
  top: calc(10px + var(--safe-t)); left: calc(10px + var(--safe-l));
  width: 52px; height: 52px; border-radius: 50%;
  display: grid; place-items: center;
  background: #fff; border: 3px solid var(--ink); box-shadow: 3px 3px 0 var(--ink);
}
.p-round svg { width: 28px; height: 28px; stroke-width: 3; }

.p-tools {
  display: flex; flex-direction: column; gap: 8px;
  padding: 8px 10px 10px;
  background: #fffdf8;
  border-top: 3px solid var(--ink);
}
.p-toolrow { display: flex; gap: 8px; justify-content: center; }
.p-tool {
  width: 54px; height: 54px; border-radius: 16px;
  display: grid; place-items: center;
  background: #fff; border: 3px solid var(--ink); box-shadow: 3px 3px 0 var(--ink);
  transition: transform .1s;
}
.p-tool svg { width: 30px; height: 30px; }
.p-tool[aria-pressed="true"] { background: var(--current); transform: translateY(-2px); box-shadow: 3px 5px 0 var(--ink); }
.p-tool[data-tool="eraser"][aria-pressed="true"] { background: #f8bbd0; }
.p-tool:disabled { opacity: .35; }
.p-tool:active { transform: scale(.94); }
.p-done { background: #7ed37f; }
.p-palette {
  display: grid;
  grid-template-columns: repeat(9, 40px);
  gap: 6px;
  justify-content: center;
}
.p-swatch {
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--c);
  border: 3px solid var(--ink);
  transition: transform .1s;
}
.p-swatch.on { transform: scale(1.08); box-shadow: 0 0 0 2px #fff, 0 0 0 5px var(--ink); }

@media (orientation: landscape) {
  .paint { grid-template-columns: 1fr auto; grid-template-rows: 1fr; }
  .p-tools {
    flex-direction: row; align-items: center;
    border-top: 0; border-left: 3px solid var(--ink);
    padding: 10px;
    overflow-y: auto;
  }
  .p-toolrow { flex-direction: column; }
  .p-palette { grid-template-columns: repeat(3, 40px); }
}
@media (orientation: landscape) and (max-height: 420px) {
  .p-tool { width: 48px; height: 48px; border-radius: 14px; }
  .p-tool svg { width: 26px; height: 26px; }
  .p-toolrow { gap: 6px; }
  .p-palette { gap: 5px; grid-template-columns: repeat(3, 36px); }
  .p-swatch { width: 36px; height: 36px; }
}
@media (orientation: portrait) and (max-width: 400px) {
  .p-palette { grid-template-columns: repeat(9, 34px); gap: 5px; }
  .p-swatch { width: 34px; height: 34px; }
  .p-tool { width: 50px; height: 50px; }
}

/* ---------- Palkinto ---------- */
.p-reward {
  position: fixed; inset: 0; z-index: 20;
  display: grid; place-items: center;
  background: rgba(30, 38, 56, .55);
  overflow: hidden;
}
.p-reward-card {
  position: relative;
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  padding: 18px 22px 20px;
  background: #fffdf8; border: 3px solid var(--ink); border-radius: 22px; box-shadow: 5px 5px 0 var(--ink);
  max-width: 92vw; max-height: 92dvh;
  animation: pop .45s cubic-bezier(.3, 1.6, .5, 1);
}
.p-bigstar { width: 64px; height: 64px; fill: var(--sun); stroke: var(--ink); stroke-width: 1.2; stroke-linejoin: round; animation: spinstar 1s ease-out; }
.p-reward-img { height: min(38dvh, 300px); width: auto; border: 2px solid var(--ink); border-radius: 10px; background: #fff; }
.p-reward-text { margin: 0; font: 800 30px/1 var(--f-head); }
.p-reward-actions { display: flex; gap: 10px; }
.p-btn {
  display: flex; align-items: center; gap: 8px;
  height: 52px; padding: 0 18px;
  border: 3px solid var(--ink); border-radius: 999px;
  background: #fff; box-shadow: 3px 3px 0 var(--ink);
  font-weight: 800; font-size: 17px;
}
.p-btn svg { width: 24px; height: 24px; }
.p-btn-main { background: var(--crayon); color: #fff; }
@keyframes pop { from { transform: scale(.4); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes spinstar { from { transform: rotate(-200deg) scale(.2); } to { transform: none; } }
@media (orientation: landscape) and (max-height: 500px) {
  .p-reward-card { flex-direction: row; flex-wrap: wrap; justify-content: center; max-width: 96vw; }
  .p-reward-img { height: 60dvh; }
  .p-bigstar { position: absolute; top: -26px; left: -22px; }
  .p-reward-actions { flex-direction: column; }
  .p-reward-text { width: 100%; text-align: center; order: -1; }
}
.p-confetti { position: absolute; inset: 0; pointer-events: none; }
.p-confetti i {
  position: absolute; top: -20px;
  width: 10px; height: 16px; border-radius: 3px;
  animation: fall linear forwards;
}
@keyframes fall { to { transform: translateY(110dvh) rotate(var(--r)); } }
@media (prefers-reduced-motion: reduce) {
  .p-confetti { display: none; }
  .p-reward-card, .p-bigstar { animation: none; }
}

/* ---------- iOS-ohje ---------- */
.p-hint {
  position: fixed; left: 12px; right: 12px; bottom: calc(12px + var(--safe-b)); z-index: 30;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px;
  background: #fff; border: 3px solid var(--ink); border-radius: 16px; box-shadow: 4px 4px 0 var(--ink);
}
.p-hint p { margin: 0; flex: 1; }
.p-hint .p-btn { height: 44px; }
