:root {
  color-scheme: light;
  --ink: #20232a;
  --muted: #596170;
  --panel: #fffaf2;
  --paper: #ffffff;
  --line: #d8cfc1;
  --leaf: #2f6f5e;
  --sun: #f2b84b;
  --rose: #d96570;
  --sky: #4c7aa5;
  --shadow: 0 18px 55px rgba(48, 38, 24, 0.18);
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  min-height: 100%;
}

body {
  margin: 0;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
  background: #f3eadc;
  color: var(--ink);
  overflow: hidden;
}

body.starting .game-shell {
  display: none;
}

button,
input {
  font: inherit;
}

button {
  border: 1px solid #1f3f37;
  background: var(--leaf);
  color: white;
  min-height: 42px;
  border-radius: 6px;
  padding: 0 16px;
  cursor: pointer;
  font-weight: 700;
}

button:disabled {
  cursor: default;
  opacity: 0.55;
}

.start-screen,
.end-screen {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 0;
  overflow: hidden;
  background: #f3eadc;
}

.start-screen.hidden,
.end-screen.hidden {
  display: none;
}

.start-screen img,
.end-screen img {
  grid-area: 1 / 1;
  width: 80vw;
  height: 80vh;
  height: 80dvh;
  height: calc(var(--app-height, 100vh) * 0.8);
  object-fit: contain;
  object-position: center;
  background: #163f2a;
  box-shadow: var(--shadow);
}

.play-button {
  grid-area: 1 / 1;
  position: relative;
  z-index: 1;
  min-width: min(260px, 72vw);
  min-height: 68px;
  padding: 0 42px;
  border: 3px solid #401b12;
  border-radius: 8px;
  background: linear-gradient(180deg, #ffcf62, #d9892f);
  color: #401b12;
  box-shadow:
    0 12px 0 #7e3f1e,
    0 24px 38px rgba(45, 24, 10, 0.34);
  font-size: clamp(1.6rem, 5vw, 2.6rem);
  line-height: 1;
  text-transform: uppercase;
}

.play-button:active {
  transform: translateY(7px);
  box-shadow:
    0 5px 0 #7e3f1e,
    0 16px 26px rgba(45, 24, 10, 0.28);
}

.replay-button {
  align-self: end;
  margin-bottom: clamp(28px, 7vh, 76px);
  font-size: clamp(1.05rem, 3.4vw, 1.85rem);
}

.game-shell {
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  height: var(--app-height, 100dvh);
  margin: 0;
  padding:
    max(8px, env(safe-area-inset-top))
    max(8px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom))
    max(8px, env(safe-area-inset-left));
  display: grid;
  grid-template-columns: minmax(0, 1fr) 310px;
  gap: 8px;
  min-height: 0;
  position: fixed;
  inset: 0;
  transition: grid-template-columns 180ms ease;
}

.game-shell.list-collapsed {
  grid-template-columns: minmax(0, 1fr) 58px;
}

.game-shell.list-collapsed .topbar,
.game-shell.list-collapsed .dialog-row {
  display: none;
}

.stage-panel,
.side-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 12px 34px rgba(48, 38, 24, 0.16);
}

.stage-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.topbar,
.side-header,
.dialog-row,
.phase-controls {
  padding: 16px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  border-bottom: 1px solid var(--line);
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--rose);
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(1.25rem, 2vw, 2.15rem);
  line-height: 1.05;
}

h2 {
  font-size: 1.15rem;
}

.progress-wrap {
  min-width: 130px;
  text-align: right;
  font-weight: 800;
}

.progress-track {
  width: 160px;
  height: 9px;
  margin-top: 7px;
  overflow: hidden;
  border: 1px solid #9c8e7c;
  background: #fff;
  border-radius: 999px;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--sun), var(--leaf));
  transition: width 220ms ease;
}

.scene-frame {
  position: relative;
  min-height: 0;
  overflow: hidden;
  background: #cde1e8;
  isolation: isolate;
  touch-action: none;
  cursor: grab;
  display: grid;
  place-items: center;
}

.scene-frame.zoomed {
  cursor: move;
}

.scene-frame.dragging {
  cursor: grabbing;
}

.scene-content {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 1;
  width: 100%;
  height: 100%;
  transform:
    translate(
      calc(-50% + var(--scene-pan-x, 0px)),
      calc(-50% + var(--scene-pan-y, 0px))
    )
    scale(var(--scene-zoom, 1));
  transform-origin: center center;
  will-change: transform;
}

#scene-image,
#fallback-scene {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: fill;
}

#scene-image {
  z-index: 1;
}

#fallback-scene {
  z-index: 0;
}

.scene-frame.intro-active .hotspot-layer {
  visibility: hidden;
  opacity: 0;
}

.scene-frame.intro-active #scene-image {
  visibility: visible;
  opacity: 1;
  filter: blur(14px) saturate(0.78) brightness(0.86);
  transform: scale(1.04);
}

.scene-frame.intro-active #fallback-scene {
  visibility: hidden;
}

.hotspot-layer {
  position: absolute;
  inset: 0;
  z-index: 2;
}

.hotspot-layer.locked {
  pointer-events: none;
  filter: blur(1px);
}

.hidden-object {
  position: absolute;
  width: var(--object-width, clamp(44px, 5.4vw, 62px));
  height: var(--object-height, clamp(44px, 5.4vw, 62px));
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  opacity: 0.84;
  mix-blend-mode: multiply;
  transform: translate(-50%, -50%) rotate(var(--object-rotation, 0deg)) scale(var(--object-scale, 1));
  filter: saturate(0.72) contrast(0.9) sepia(0.08) drop-shadow(0 1px 1px rgba(24, 28, 18, 0.16));
}

.hidden-object svg {
  width: 100%;
  height: 100%;
  pointer-events: none;
  transform: scale(0.78);
  transform-origin: center;
}

.object-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  transform: scale(0.88);
  transform-origin: center;
}

.hidden-object svg * {
  stroke-linejoin: round;
  stroke-opacity: 0.68;
}

.hidden-object.found {
  pointer-events: none;
  animation: collect 420ms ease forwards;
}

@keyframes collect {
  100% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(1.8) rotate(14deg);
  }
}

.intro-scene {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  padding: clamp(14px, 2.4vw, 26px);
  background: rgba(35, 28, 21, 0.14);
  backdrop-filter: blur(14px) saturate(0.78) brightness(0.92);
  transition:
    opacity 220ms ease,
    visibility 220ms ease;
}

.intro-scene::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: linear-gradient(180deg, rgba(255, 250, 242, 0.18), rgba(32, 25, 18, 0.28));
}

.intro-scene.hidden {
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
}

.intro-characters {
  min-height: 0;
  display: flex;
  justify-content: center;
  align-items: end;
  gap: clamp(18px, 7vw, 92px);
}

.intro-group {
  min-width: min(37vw, 360px);
  max-width: min(43vw, 430px);
  height: min(78%, 470px);
  display: flex;
  justify-content: center;
  align-items: end;
  opacity: 0.76;
  transform: translateY(12px) scale(0.96);
  transition:
    opacity 180ms ease,
    transform 180ms ease,
    filter 180ms ease;
}

.intro-group.active {
  opacity: 1;
  transform: translateY(0) scale(1.04);
}

.intro-character {
  height: min(78%, 470px);
  max-width: min(37vw, 330px);
  object-fit: contain;
  object-position: bottom;
  filter: drop-shadow(0 16px 18px rgba(0, 0, 0, 0.34));
}

.intro-group .intro-character {
  width: 42%;
  margin-inline: -5%;
}

.intro-group.single .intro-character {
  width: 74%;
  margin-inline: 0;
}

.intro-group.trio .intro-character {
  width: 34%;
  margin-inline: -4%;
}

.intro-group.active .intro-character {
  filter: drop-shadow(0 18px 20px rgba(0, 0, 0, 0.38));
}

.intro-bubble-wrap {
  width: min(760px, 100%);
  justify-self: center;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px 12px;
  align-items: end;
}

.intro-name {
  grid-column: 1 / -1;
  width: max-content;
  max-width: 100%;
  padding: 5px 10px;
  color: #fff;
  background: rgba(32, 35, 42, 0.92);
  border-radius: 6px;
  font-weight: 900;
}

.intro-bubble {
  min-height: 82px;
  padding: 17px 18px;
  border: 2px solid #2f2f2f;
  border-radius: 8px;
  background: rgba(255, 250, 242, 0.97);
  box-shadow: 0 14px 38px rgba(30, 28, 24, 0.28);
  line-height: 1.35;
  font-size: clamp(1rem, 1.7vw, 1.25rem);
  font-weight: 750;
}

#intro-next-button {
  min-width: 118px;
  align-self: stretch;
}

.toast {
  position: absolute;
  left: 50%;
  bottom: 18px;
  z-index: 4;
  opacity: 0;
  transform: translateX(-50%) translateY(12px);
  background: rgba(255, 250, 242, 0.96);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 14px;
  font-weight: 800;
  transition:
    opacity 180ms ease,
    transform 180ms ease;
}

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

.scene-next-button {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 6;
  min-width: min(270px, 76vw);
  min-height: 68px;
  border: 3px solid #401b12;
  border-radius: 8px;
  background: linear-gradient(180deg, #ffcf62, #d9892f);
  color: #401b12;
  box-shadow:
    0 10px 0 #7e3f1e,
    0 22px 40px rgba(45, 24, 10, 0.34);
  font-size: clamp(1.25rem, 3.6vw, 2rem);
  text-transform: uppercase;
  transform: translate(-50%, -50%);
}

.scene-next-button.hidden {
  display: none;
}

.scene-next-button:active {
  transform: translate(-50%, calc(-50% + 6px));
  box-shadow:
    0 4px 0 #7e3f1e,
    0 14px 26px rgba(45, 24, 10, 0.28);
}

.dialog-row {
  display: grid;
  grid-template-columns: 86px minmax(0, 1fr);
  gap: 14px;
  align-items: end;
  border-top: 1px solid var(--line);
  background: var(--paper);
}

.character-portrait {
  width: 86px;
  height: 86px;
  border-radius: 8px;
  border: 2px solid #2f2f2f;
  overflow: hidden;
  background: #f8df9c;
  position: relative;
}

.character-portrait img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center top;
  transform: scale(1.85);
  transform-origin: center top;
}

.portrait-group {
  display: none;
  width: 100%;
  height: 100%;
  align-items: end;
  justify-content: center;
  overflow: hidden;
}

.character-portrait.group #character-image {
  display: none;
}

.character-portrait.group .portrait-group {
  display: flex;
}

.portrait-group img {
  width: 42%;
  height: 100%;
  object-fit: cover;
  object-position: center top;
  transform: scale(1.52);
  transform-origin: center top;
  margin-inline: -4%;
}

.portrait-fallback {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.portrait-fallback {
  display: grid;
  place-items: center;
  font-size: 2.5rem;
  font-weight: 900;
  color: #fff;
  background:
    radial-gradient(circle at 50% 34%, #ffd7b5 0 24%, transparent 25%),
    radial-gradient(circle at 42% 34%, #2b2522 0 4%, transparent 5%),
    radial-gradient(circle at 58% 34%, #2b2522 0 4%, transparent 5%),
    radial-gradient(circle at 50% 75%, var(--sky) 0 38%, transparent 39%),
    var(--sun);
}

.speech-bubble {
  min-height: 76px;
  position: relative;
  background: #fff;
  border: 2px solid #2f2f2f;
  border-radius: 8px;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  line-height: 1.35;
  font-weight: 650;
}

.speech-bubble::before {
  content: "";
  position: absolute;
  left: -13px;
  bottom: 18px;
  width: 20px;
  height: 20px;
  background: #fff;
  border-left: 2px solid #2f2f2f;
  border-bottom: 2px solid #2f2f2f;
  transform: rotate(45deg);
}

.side-panel {
  min-width: 0;
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
  min-height: 0;
}

.side-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  border-bottom: 1px solid var(--line);
}

.side-actions {
  display: flex;
  gap: 8px;
}

.icon-button {
  width: 42px;
  padding: 0;
  font-size: 1.25rem;
}

.game-shell.list-collapsed .side-panel {
  grid-template-rows: auto;
}

.game-shell.list-collapsed .side-header {
  height: 100%;
  padding: 8px;
  flex-direction: column;
  justify-content: flex-start;
}

.game-shell.list-collapsed .side-header h2,
.game-shell.list-collapsed .object-list,
.game-shell.list-collapsed .phase-controls,
.game-shell.list-collapsed #reset-button {
  display: none;
}

.object-list {
  list-style: none;
  margin: 0;
  padding: 10px 16px;
  overflow: auto;
}

.object-list li {
  display: grid;
  grid-template-columns: 34px 1fr;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  border-bottom: 1px solid rgba(120, 106, 85, 0.22);
  color: var(--muted);
  font-weight: 750;
}

.object-list .mini {
  width: 30px;
  height: 30px;
}

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

.object-list li.found {
  color: var(--leaf);
  text-decoration: line-through;
}

.phase-controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  border-top: 1px solid var(--line);
}

@media (max-width: 1024px), (hover: none) and (pointer: coarse) {
  .game-shell {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr);
    height: 100vh;
    height: 100dvh;
    height: var(--app-height, 100dvh);
    min-height: 0;
    padding:
      max(6px, env(safe-area-inset-top))
      max(6px, env(safe-area-inset-right))
      max(6px, env(safe-area-inset-bottom))
      max(6px, env(safe-area-inset-left));
  }

  .game-shell.list-collapsed {
    grid-template-columns: 1fr;
  }

  .stage-panel {
    position: relative;
    display: block;
  }

  .scene-frame {
    position: absolute;
    inset: 0;
    min-height: 0;
  }

  .topbar {
    position: absolute;
    top: 8px;
    left: 8px;
    right: 8px;
    z-index: 8;
    align-items: flex-start;
    flex-wrap: wrap;
    gap: 10px;
    border: 1px solid rgba(216, 207, 193, 0.82);
    border-radius: 8px;
    background: rgba(255, 250, 242, 0.92);
    box-shadow: 0 8px 24px rgba(48, 38, 24, 0.14);
  }

  .dialog-row {
    position: absolute;
    left: 8px;
    right: 8px;
    bottom: 8px;
    z-index: 8;
    border: 1px solid rgba(216, 207, 193, 0.86);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.94);
    box-shadow: 0 8px 24px rgba(48, 38, 24, 0.14);
  }

  .progress-track {
    width: 110px;
  }

  .side-panel {
    position: absolute;
    left: 6px;
    right: 6px;
    bottom: 6px;
    z-index: 9;
    max-height: min(45vh, 360px);
    min-height: 0;
    border-radius: 8px 8px 0 0;
    box-shadow: 0 12px 34px rgba(48, 38, 24, 0.22);
  }

  .game-shell.list-collapsed .side-panel {
    left: auto;
    width: 58px;
    height: 58px;
    border-radius: 8px;
  }

  .game-shell.list-collapsed .side-header {
    height: auto;
    flex-direction: row;
    justify-content: flex-end;
  }
}

@media (max-width: 1024px) and (orientation: landscape), (hover: none) and (pointer: coarse) and (orientation: landscape) {
  .game-shell {
    padding:
      max(4px, env(safe-area-inset-top))
      max(4px, env(safe-area-inset-right))
      max(4px, env(safe-area-inset-bottom))
      max(4px, env(safe-area-inset-left));
  }

  .topbar {
    top: 6px;
    left: 6px;
    right: 6px;
    padding: 8px 10px;
  }

  .topbar h1 {
    font-size: clamp(1rem, 2.5vw, 1.35rem);
  }

  .eyebrow {
    font-size: 0.7rem;
  }

  .progress-track {
    width: 120px;
  }

  .dialog-row {
    left: 6px;
    right: auto;
    bottom: 6px;
    width: min(680px, calc(100vw - 330px));
    min-width: min(520px, calc(100vw - 24px));
    grid-template-columns: 56px minmax(0, 1fr);
    padding: 8px;
  }

  .character-portrait {
    width: 56px;
    height: 56px;
  }

  .speech-bubble {
    min-height: 56px;
    padding: 8px 12px;
    font-size: 0.88rem;
  }

  .side-panel {
    top: 74px;
    right: 6px;
    bottom: 6px;
    left: auto;
    width: min(300px, 36vw);
    max-height: none;
    border-radius: 8px;
  }

  .game-shell.list-collapsed .side-panel {
    top: auto;
    right: 6px;
    bottom: 6px;
    width: 58px;
    height: 58px;
  }

  .object-list {
    padding: 8px 12px;
  }

  .object-list li {
    min-height: 38px;
    font-size: 0.86rem;
  }

  .phase-controls {
    padding: 8px;
  }
}

@media (max-width: 560px) {
  .game-shell {
    width: 100%;
    padding: 0;
  }

  .stage-panel,
  .side-panel {
    border-left: 0;
    border-right: 0;
  }

  .topbar,
  .dialog-row,
  .side-header,
  .phase-controls {
    padding: 10px;
  }

  .topbar {
    display: grid;
    grid-template-columns: 1fr auto;
    top: 8px;
    left: 8px;
    right: 8px;
  }

  .progress-wrap {
    min-width: 0;
    text-align: left;
  }

  .side-panel {
    left: 8px;
    right: 8px;
    bottom: 8px;
    border-radius: 8px 8px 0 0;
  }

  .game-shell.list-collapsed .side-panel {
    right: 8px;
    bottom: 8px;
    left: auto;
  }

  .dialog-row {
    grid-template-columns: 64px minmax(0, 1fr);
  }

  .character-portrait {
    width: 64px;
    height: 64px;
  }

  .speech-bubble {
    min-height: 66px;
    padding: 10px 12px;
    font-size: 0.92rem;
  }

  .intro-scene {
    padding: 10px;
  }

  .intro-characters {
    gap: 8px;
  }

  .intro-character {
    height: 100%;
    max-width: 45vw;
  }

  .intro-group {
    min-width: 44vw;
    max-width: 48vw;
    height: min(70%, 300px);
  }

  .intro-bubble-wrap {
    grid-template-columns: 1fr;
  }

  #intro-next-button {
    width: 100%;
  }
}
