:root {
  --wood-dark: #2b1a12;
  --wood: #5b3418;
  --wood-mid: #7a4a24;
  --cream: #fff3d6;
  --pink: #ff6fae;
  --pink-dark: #b0306e;
  --apple: #ff7a45;
  --gold: #ffd166;
  --font: "Press Start 2P", "Courier New", monospace;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background: var(--wood-dark);
  background-image:
    repeating-linear-gradient(90deg, rgba(255,255,255,0.025) 0 2px, transparent 2px 22px),
    linear-gradient(180deg, #3a2317, #1e120c);
  color: var(--cream);
  font-family: var(--font);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  -webkit-text-size-adjust: 100%;
}

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
}

/* ------------------------------------------------------------ Banner + HUD */
.banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 8px 16px 6px;
  background: linear-gradient(#6b4220, #4a2a14);
  border-bottom: 3px solid #1a0f09;
  box-shadow: inset 0 2px 0 rgba(255,255,255,0.15);
}
.banner h1 {
  margin: 0;
  font-size: clamp(12px, 3.2vw, 26px);
  letter-spacing: 1px;
  white-space: nowrap;
  text-align: center;
}
.t-owl, .t-mid, .t-apples {
  text-shadow: 2px 2px 0 var(--wood-dark), -1px -1px 0 var(--wood-dark), 1px -1px 0 var(--wood-dark), -1px 1px 0 var(--wood-dark);
}
.t-owl { color: var(--pink); }
.t-mid { color: var(--cream); font-size: 0.8em; }
.t-apples { color: var(--apple); }
.leaf {
  width: 18px; height: 12px;
  background: #5cbf3a;
  border: 2px solid #24591a;
  border-radius: 0 12px 0 12px;
  flex: none;
}
.leaf.right { transform: scaleX(-1); }

.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 16px;
  background: #1c1024;
  border-bottom: 3px solid #0e0812;
  font-size: clamp(8px, 1.6vw, 12px);
}
.hud .cell { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.hud label { color: #d8c8e8; font-size: 0.8em; }
.hud b { color: #fff; font-weight: normal; font-size: 1.25em; }
.hud .lives { display: flex; gap: 3px; min-height: 18px; align-items: center; }
.hud .lives img { width: 22px; height: auto; image-rendering: pixelated; }
.hud .apples { display: flex; align-items: center; gap: 6px; }
.hud .apples img { width: 20px; image-rendering: pixelated; }
.hud .apples.full b { color: var(--gold); animation: pulse 0.8s ease-in-out infinite alternate; }
.hud .stage label { color: var(--gold); font-size: 1em; }
.hud .stage small { color: var(--gold); font-size: 0.75em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud .buttons { flex-direction: row; gap: 6px; }

@keyframes pulse { from { transform: scale(1); } to { transform: scale(1.12); } }

.icon-btn {
  font: inherit;
  font-size: 12px;
  width: 36px; height: 36px;
  color: var(--cream);
  background: #3a2448;
  border: 2px solid #6d4a86;
  border-radius: 8px;
  cursor: pointer;
  touch-action: manipulation;
}
.icon-btn.off, .btn.mute.off { opacity: 0.45; text-decoration: line-through; }

/* ------------------------------------------------------------ Play area */
#play {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: stretch;
}
#stage {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
}
#game {
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #7ec8f2;
  border: 3px solid #1a0f09;
  border-radius: 4px;
  box-shadow: 0 6px 24px rgba(0,0,0,0.5);
}

/* ------------------------------------------------------------ Touch controls */
.pad { display: none; }
body.touch .pad {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: none;
}
.dpad {
  position: relative;
  display: flex;
  width: 150px;
  height: 84px;
  border-radius: 42px;
  background: rgba(255,243,214,0.1);
  border: 3px solid rgba(255,243,214,0.35);
  touch-action: none;
  overflow: hidden;
}
.dpad .arrow {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  color: var(--cream);
  font-family: system-ui, sans-serif;
  pointer-events: none;
}
.dpad .arrow.l { border-right: 2px solid rgba(255,243,214,0.2); }
.dpad .arrow.on { background: rgba(255,111,174,0.45); }
.abtn {
  width: 92px;
  height: 92px;
  border-radius: 50%;
  font: inherit;
  font-size: 26px;
  color: #fff;
  background: radial-gradient(circle at 35% 30%, #ff9cc8, #e0457f 60%, #a82458);
  border: 4px solid #5a1030;
  box-shadow: 0 5px 0 #5a1030;
  touch-action: none;
}
.abtn.down { transform: translateY(4px); box-shadow: 0 1px 0 #5a1030; }

/* Landscape (phones & tablets): controls sit beside the game */
@media (orientation: landscape) {
  body.touch .pad { width: clamp(150px, 20vw, 230px); }
  body.touch .left-pad { padding-left: 8px; }
  body.touch .right-pad { padding-right: 8px; }
  body.touch #stage { padding: 6px 0; }
}

/* Tablets sideways: plenty of height, so float the controls over the ground at the bottom corners */
@media (orientation: landscape) and (min-height: 600px) {
  body.touch #play { position: relative; }
  body.touch #stage { padding: 8px; }
  body.touch .pad { position: absolute; bottom: 22px; width: auto; z-index: 5; opacity: 0.8; }
  body.touch .left-pad { left: 22px; padding: 0; }
  body.touch .right-pad { right: 22px; padding: 0; }
  body.touch .dpad { background: rgba(43,26,18,0.45); }
}

/* Portrait: game on top, controls underneath */
@media (orientation: portrait) {
  body.touch #play {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto 1fr;
    grid-template-areas: "stage stage" "left right";
  }
  body.touch #stage { grid-area: stage; padding: 8px 6px; }
  body.touch .pad { align-items: flex-end; padding-bottom: max(28px, 7dvh); }
  body.touch .left-pad { grid-area: left; justify-content: flex-start; padding-left: 20px; }
  body.touch .right-pad { grid-area: right; justify-content: flex-end; padding-right: 24px; }
  body.touch .dpad { width: 170px; height: 96px; border-radius: 48px; }
  body.touch .abtn { width: 104px; height: 104px; }
}

/* Short screens (phones held sideways): slim header */
@media (max-height: 520px) {
  .banner { display: none; }
  .hud { padding: 4px 12px; }
  .hud .lives img { width: 16px; }
  .icon-btn { width: 32px; height: 32px; }
}

/* ------------------------------------------------------------ Overlays */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(12px + var(--safe-t)) calc(12px + var(--safe-r)) calc(12px + var(--safe-b)) calc(12px + var(--safe-l));
  background: rgba(20, 10, 30, 0.72);
  overflow: auto;
  touch-action: manipulation;
}
#title { background: rgba(20, 10, 30, 0.8); }

.card {
  background: var(--cream);
  color: var(--wood-dark);
  border: 4px solid var(--wood-dark);
  border-radius: 10px;
  box-shadow: 0 8px 0 rgba(0,0,0,0.35), inset 0 -6px 0 #eadbb8;
  padding: 20px 24px;
  text-align: center;
  max-width: 100%;
  max-height: 100%;
  overflow: auto;
  font-size: 11px;
  line-height: 1.7;
}
.card h2 { margin: 0 0 10px; font-size: 16px; line-height: 1.5; }
.card h2.big { font-size: clamp(18px, 4vw, 28px); color: var(--pink-dark); text-shadow: 2px 2px 0 #ffd0e6; }
.card p { margin: 8px 0; }
.card .dim { opacity: 0.7; }
.card .bonus { color: #c0551a; }
.card b { font-weight: normal; color: var(--pink-dark); }

.title-card {
  display: flex;
  gap: 20px;
  align-items: center;
  text-align: left;
  padding: 14px;
  max-width: 860px;
}
.title-card .poster {
  height: min(70dvh, 520px);
  max-width: 45%;
  object-fit: cover;
  border: 3px solid var(--wood-dark);
  border-radius: 6px;
  image-rendering: auto;
}
.title-side { flex: 1; min-width: 0; }
.title-side h2 .t-owl, .title-side h2 .t-apples { text-shadow: 2px 2px 0 var(--wood-dark); }
.howto { list-style: none; padding: 0; margin: 12px 0 0; font-size: 9px; line-height: 2.2; color: #5b3418; }
kbd {
  display: inline-block;
  font: inherit;
  padding: 1px 5px;
  margin-right: 3px;
  background: #fff;
  border: 2px solid var(--wood-dark);
  border-radius: 4px;
  box-shadow: 0 2px 0 var(--wood-dark);
}
.touch-only, .rotate-hint { display: none; }
body.touch .touch-only { display: block; }
body.touch .keys-only { display: none; }
@media (orientation: portrait) { body.touch .rotate-hint { display: block; font-size: 9px; color: #8a6a44; } }

.btn {
  display: inline-block;
  font: inherit;
  font-size: 14px;
  margin-top: 10px;
  padding: 14px 22px;
  color: #fff;
  background: linear-gradient(#ff85bd, #e0457f);
  border: 3px solid #5a1030;
  border-radius: 10px;
  box-shadow: 0 5px 0 #5a1030;
  cursor: pointer;
  touch-action: manipulation;
}
.btn:active { transform: translateY(4px); box-shadow: 0 1px 0 #5a1030; }
.btn:focus-visible, .icon-btn:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
.btn.small { font-size: 10px; padding: 10px 14px; background: linear-gradient(#b48ad0, #7d56a0); border-color: #33204a; box-shadow: 0 4px 0 #33204a; }
#btn-play { font-size: 18px; padding: 16px 30px; }

#ending { background: #000; flex-direction: column; }
#ending video { max-width: 100%; max-height: 100%; width: auto; height: 100%; object-fit: contain; }
#btn-skip {
  position: absolute;
  right: calc(16px + var(--safe-r));
  bottom: calc(16px + var(--safe-b));
}

.victory-card { display: flex; gap: 22px; align-items: center; max-width: 820px; }
.victory-text { flex: 1; }
.coloring { margin: 0; flex: none; }
.coloring img {
  display: block;
  height: min(60dvh, 420px);
  max-width: 100%;
  border: 3px solid var(--wood-dark);
  border-radius: 4px;
  transform: rotate(2deg);
  box-shadow: 4px 6px 0 rgba(0,0,0,0.2);
  background: #fff;
}
.coloring figcaption { margin-top: 10px; font-size: 9px; color: #8a6a44; }

/* Narrow screens: stack the cards */
@media (max-width: 640px) {
  .title-card, .victory-card { flex-direction: column; text-align: center; }
  .title-card .poster { max-width: 100%; height: min(38dvh, 360px); width: auto; }
  .coloring img { height: min(34dvh, 300px); }
  .hud { gap: 6px; padding: 6px 10px; }
  .hud .stage small { display: none; }
}
/* Short & wide (phone landscape): keep things side by side but smaller */
@media (max-height: 520px) and (orientation: landscape) {
  .card { padding: 10px 14px; font-size: 9px; line-height: 1.6; }
  .card h2 { font-size: 12px; }
  .title-card .poster { height: calc(100dvh - 60px); }
  .coloring img { height: calc(100dvh - 90px); }
  .howto { line-height: 1.9; margin-top: 6px; }
  .btn { padding: 10px 16px; font-size: 12px; }
  #btn-play { font-size: 14px; padding: 12px 22px; }
}
