:root {
  --cream: #FFF3D6;
  --gold: #FFD447;
  --tangerine: #FF8A4C;
  --coral: #E8503A;
  --leaf: #7CD65C;
  --mid: #3E9E4F;
  --deep: #1E5C46;
  --sky: #62C4E8;
  --violet: #8352C9;
  --ink: #1B2E2A;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: var(--sky);
  overscroll-behavior: none;
  touch-action: none;
}

body {
  font-family: "Silkscreen", monospace;
  color: var(--ink);
  -webkit-font-smoothing: none;
  -webkit-user-select: none;
  user-select: none;
}

#stage {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background: var(--sky);
}

#game {
  position: absolute;
  inset: 0;
  z-index: 0;
}

#game canvas {
  display: block;
  image-rendering: pixelated;
  filter: saturate(1.12) brightness(1.05) contrast(1.02);
}

#hud {
  position: absolute;
  inset: 0;
  z-index: 10;
  pointer-events: none;
}

img { image-rendering: pixelated; }

/* ---------- top left ---------- */

.hud-left {
  position: absolute;
  top: max(10px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  display: flex;
  align-items: flex-start;
  gap: clamp(6px, 1.1vw, 12px);
}

.portrait {
  width: clamp(48px, 6.4vw, 82px);
  aspect-ratio: 1.15;
  filter: drop-shadow(3px 3px 0 rgba(27, 46, 42, 0.28));
}

.portrait img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.vitals {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-top: 4px;
}

.hearts {
  display: flex;
  align-items: center;
  gap: clamp(0px, 0.35vw, 4px);
  min-height: clamp(24px, 3.4vw, 40px);
}

.hearts img {
  width: clamp(28px, 4.1vw, 52px);
  height: auto;
  margin: -4px -3px;
  filter: drop-shadow(2px 2px 0 rgba(27, 46, 42, 0.3));
}

.under-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.orb-slot img {
  display: block;
  width: clamp(24px, 3vw, 38px);
  height: auto;
  filter: drop-shadow(2px 2px 0 rgba(27, 46, 42, 0.25));
}

.level-chip {
  font-family: "Silkscreen", monospace;
  font-weight: 700;
  font-size: clamp(9px, 0.9vw, 12px);
  letter-spacing: 0.06em;
  color: var(--cream);
  background: var(--ink);
  border-radius: 3px;
  padding: 3px 6px 2px;
  box-shadow: 2px 2px 0 rgba(27, 46, 42, 0.35);
}

/* ---------- top right ---------- */

.hud-right {
  position: absolute;
  top: max(10px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 5px;
}

.shard-box {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--cream);
  border: 3px solid var(--ink);
  border-radius: 6px;
  padding: 1px 9px 0 6px;
  box-shadow: 3px 3px 0 rgba(27, 46, 42, 0.3);
}

.shard-box img {
  width: clamp(18px, 2.1vw, 28px);
  height: auto;
  margin: -3px 0;
}

#shardCount {
  font-family: "Jersey 10", "Silkscreen", monospace;
  font-size: clamp(26px, 3.1vw, 40px);
  line-height: 1;
  letter-spacing: 0.04em;
  margin-top: 3px;
}

.shard-total {
  font-family: "Jersey 10", "Silkscreen", monospace;
  font-size: clamp(16px, 1.9vw, 24px);
  color: var(--deep);
  margin-top: 9px;
}

.mini-buttons {
  display: flex;
  gap: 5px;
  pointer-events: auto;
}

.mini-btn {
  font-family: "Silkscreen", monospace;
  font-weight: 700;
  font-size: clamp(8px, 0.78vw, 11px);
  color: var(--ink);
  background: var(--cream);
  border: 2px solid var(--ink);
  border-radius: 4px;
  padding: 4px 7px;
  box-shadow: 0 3px 0 var(--deep);
  cursor: pointer;
}

.mini-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--deep);
}

.help-btn {
  width: 26px;
  padding: 3px 0 1px;
  font-size: 12px;
  line-height: 1;
}

/* ---------- quest + toast ---------- */

.quest-line {
  position: absolute;
  left: 50%;
  top: max(10px, env(safe-area-inset-top));
  bottom: auto;
  transform: translateX(-50%);
  max-width: min(48vw, 640px);
  text-align: center;
  font-family: "Silkscreen", monospace;
  font-weight: 700;
  font-size: clamp(9px, 0.92vw, 12px);
  line-height: 1.45;
  color: var(--ink);
  background: rgba(255, 243, 214, 0.95);
  border: 2px solid var(--ink);
  border-radius: 5px;
  padding: 5px 12px 4px;
  box-shadow: 0 3px 0 rgba(27, 46, 42, 0.35);
  transition: opacity 0.4s ease;
}

.toast {
  position: absolute;
  left: 50%;
  top: clamp(54px, 8vh, 92px);
  transform: translateX(-50%) translateY(-8px);
  background: var(--cream);
  border: 3px solid var(--ink);
  border-radius: 6px;
  box-shadow: 4px 4px 0 rgba(27, 46, 42, 0.35);
  color: var(--ink);
  font-family: "Silkscreen", monospace;
  font-weight: 700;
  font-size: clamp(10px, 1vw, 13px);
  line-height: 1.45;
  text-align: center;
  padding: 7px 14px;
  max-width: min(80vw, 460px);
  opacity: 0;
  transition: opacity 0.28s ease, transform 0.28s ease;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ---------- control chips ---------- */

.chips {
  position: absolute;
  left: max(12px, env(safe-area-inset-left));
  bottom: max(14px, calc(env(safe-area-inset-bottom) + 8px));
  display: flex;
  align-items: flex-end;
  gap: 5px;
  transition: opacity 0.5s ease, transform 0.5s ease;
  pointer-events: none;
}

.chips.fade {
  opacity: 0;
  transform: translateY(8px);
}

.keycap {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 30px;
  height: 32px;
  padding: 0 7px;
  font-family: "Pixelify Sans", "Silkscreen", monospace;
  font-weight: 700;
  font-size: 16px;
  color: var(--ink);
  background: var(--cream);
  border: 3px solid var(--ink);
  border-radius: 5px;
  box-shadow: 0 4px 0 var(--deep);
}

.keycap.wide {
  min-width: 66px;
  font-family: "Silkscreen", monospace;
  font-size: 9px;
  letter-spacing: 0.04em;
}

.keycap.paw {
  gap: 3px;
  font-family: "Silkscreen", monospace;
  font-size: 8px;
  letter-spacing: 0.02em;
}

.keycap.paw img {
  width: 16px;
  height: auto;
}

/* ---------- touch ---------- */

.touch {
  display: none;
  position: absolute;
  left: 0;
  right: 0;
  bottom: max(12px, calc(env(safe-area-inset-bottom) + 8px));
  justify-content: space-between;
  padding: 0 max(12px, env(safe-area-inset-left)) 0 max(12px, env(safe-area-inset-right));
  pointer-events: none;
}

body.touch-mode .touch { display: flex; }

.touch-side {
  display: flex;
  align-items: flex-end;
  gap: 10px;
}

.tbtn {
  pointer-events: auto;
  touch-action: none;
  width: 64px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--cream);
  border: 3px solid var(--ink);
  border-radius: 12px;
  box-shadow: 0 6px 0 var(--deep);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}

.tbtn:active,
.tbtn.down {
  transform: translateY(4px);
  box-shadow: 0 2px 0 var(--deep);
  background: var(--gold);
}

.tbtn-word {
  font-family: "Pixelify Sans", "Silkscreen", monospace;
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.02em;
}

.tri {
  width: 0;
  height: 0;
  border-top: 11px solid transparent;
  border-bottom: 11px solid transparent;
}

.tri.left { border-right: 17px solid var(--ink); }
.tri.right { border-left: 17px solid var(--ink); }

/* ---------- panels ---------- */

.panel-wrap {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(27, 46, 42, 0.55);
  pointer-events: auto;
}

.panel-wrap[hidden] { display: none; }

.panel {
  position: relative;
  width: min(540px, 100%);
  background: var(--cream);
  border: 4px solid var(--ink);
  border-radius: 8px;
  box-shadow: 8px 8px 0 rgba(27, 46, 42, 0.55);
  padding: clamp(16px, 3vw, 30px);
  text-align: center;
}

.panel h2 {
  margin: 0 0 12px;
  font-family: "Pixelify Sans", "Silkscreen", monospace;
  font-size: clamp(24px, 3.4vw, 38px);
  line-height: 1;
  letter-spacing: 0.02em;
}

.panel p {
  margin: 0 0 10px;
  font-family: "Silkscreen", monospace;
  font-size: clamp(10px, 1.05vw, 13px);
  line-height: 1.75;
  text-align: left;
}

.panel p b { color: var(--coral); }

.panel-btn {
  margin-top: 12px;
  font-family: "Pixelify Sans", "Silkscreen", monospace;
  font-weight: 700;
  font-size: clamp(15px, 1.6vw, 20px);
  color: var(--cream);
  background: var(--coral);
  border: 3px solid var(--ink);
  border-radius: 6px;
  padding: 8px 18px 6px;
  box-shadow: 0 5px 0 var(--ink);
  cursor: pointer;
}

.panel-btn:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 var(--ink);
}

.win-panel {
  border-top: 12px solid var(--gold);
}

.win-kicker {
  text-align: center !important;
  color: var(--coral);
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  font-size: 10px !important;
  margin-bottom: 6px !important;
}

.win-title {
  color: var(--ink);
  text-shadow: 3px 3px 0 var(--gold);
}

.win-copy {
  text-align: center !important;
  font-size: 12px !important;
}

.win-stats {
  display: flex;
  justify-content: center;
  gap: clamp(12px, 3vw, 34px);
  margin: 16px 0 6px;
}

.win-stats > div {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.win-number {
  font-family: "Jersey 10", "Silkscreen", monospace;
  font-size: clamp(28px, 3.4vw, 40px);
  line-height: 1;
  color: var(--deep);
}

.win-label {
  font-family: "Silkscreen", monospace;
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink);
  opacity: 0.8;
}

/* ---------- loader ---------- */

.loader {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 16px;
  background: var(--sky);
  transition: opacity 0.3s ease;
  pointer-events: auto;
}

.loader.done {
  opacity: 0;
  pointer-events: none;
}

.loader-cat { display: none;
  width: clamp(96px, 14vw, 150px);
  height: auto;
  animation: cat-bob 1.1s steps(2, end) infinite;
}

.loader-title {
  font-family: "Pixelify Sans", "Silkscreen", monospace;
  font-weight: 700;
  font-size: clamp(24px, 3.6vw, 44px);
  letter-spacing: 0.04em;
  color: var(--cream);
  text-shadow: 3px 3px 0 var(--ink), 6px 6px 0 rgba(27, 46, 42, 0.25);
}

.loader-track {
  width: min(240px, 60vw);
  height: 14px;
  border: 3px solid var(--ink);
  border-radius: 3px;
  background: var(--cream);
  overflow: hidden;
}

.loader-bar {
  width: 0%;
  height: 100%;
  background: var(--gold);
  transition: width 0.15s linear;
}

.loader-note {
  font-size: 10px;
  color: var(--ink);
  letter-spacing: 0.08em;
}

@keyframes cat-bob {
  0% { transform: translateY(0); }
  100% { transform: translateY(-6px); }
}

/* ---------- paw cursor ---------- */

.paw-cursor {
  position: fixed;
  z-index: 40;
  width: 38px;
  height: auto;
  pointer-events: none;
  transform: translate(-8px, -4px);
  display: none;
}

.paw-cursor img {
  width: 100%;
  height: auto;
}

body.has-mouse.fine-pointer.playing .paw-cursor { display: block; }
body.fine-pointer.playing #game canvas { cursor: none; }
body.paw-hover .paw-cursor { display: none !important; }

/* ---------- portrait / narrow ---------- */

@media (max-width: 620px), (max-height: 480px) {
  .hud-left { gap: 6px; }
  .portrait { width: 46px; }
  .hearts img { width: 30px; margin: -4px -4px; }
  .under-row { gap: 5px; }
  .level-chip { font-size: 8px; padding: 2px 5px; }
  .quest-line {
    top: auto;
    bottom: max(96px, calc(env(safe-area-inset-bottom) + 88px));
    max-width: 90vw;
    font-size: 9.5px;
    line-height: 1.5;
  }
  .chips { display: none; }
  .toast { top: 84px; font-size: 10px; padding: 6px 10px; max-width: 86vw; }
  body.touch-mode .quest-line { bottom: max(98px, calc(env(safe-area-inset-bottom) + 90px)); }
  .panel p { font-size: 10px; line-height: 1.7; }
  .mini-btn { font-size: 8px; }
  .help-btn { width: 24px; }
}

@media (max-width: 380px) {
  .portrait { width: 40px; }
  .hearts img { width: 26px; }
  .shard-box { padding-right: 6px; }
  #shardCount { font-size: 24px; }
  .tbtn { width: 56px; height: 56px; }
}

@media (prefers-reduced-motion: reduce) {
  .loader-cat { display: none; animation: none; }
  .toast, .chips, .quest-line, .loader { transition-duration: 0.01s; }
}
