:root {
  --ink: #182125;
  --paper: #f7fbf5;
  --mint: #7fd7b2;
  --teal: #207d83;
  --coral: #ef626c;
  --lemon: #f5d547;
  --sky: #78b7df;
  --line: #244147;
  --shadow: rgba(24, 33, 37, 0.22);
  --shelf: #f1f6ea;
}

* {
  box-sizing: border-box;
}

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

body {
  min-height: 100vh;
  overflow-x: hidden;
  color: var(--ink);
  font-family: "Trebuchet MS", "Gill Sans", Verdana, sans-serif;
  background:
    radial-gradient(circle at 18% 12%, rgba(245, 213, 71, 0.32), transparent 28%),
    linear-gradient(135deg, #f7fbf5 0%, #dff4ec 44%, #f3ebe6 100%);
}

body.game-active {
  overflow: hidden;
}

body.menu-active {
  height: 100dvh;
  overflow: hidden;
}

button {
  border: 2px solid var(--line);
  border-radius: 8px;
  background: var(--lemon);
  color: var(--ink);
  cursor: pointer;
  font: inherit;
  font-weight: 800;
  padding: 0.75rem 1rem;
  box-shadow: 4px 4px 0 var(--line);
  transition:
    transform 160ms ease,
    box-shadow 160ms ease;
}

button:hover {
  transform: translate(-2px, -2px);
  box-shadow: 6px 6px 0 var(--line);
}

button:active {
  transform: translate(2px, 2px);
  box-shadow: 2px 2px 0 var(--line);
}

.game-shell {
  width: min(1180px, calc(100vw - 28px));
  margin: 0 auto;
  padding: 24px 0 30px;
}

.screen[hidden] {
  display: none;
}

#gameView {
  min-height: calc(100vh - 54px);
}

.menu-screen {
  min-height: calc(100dvh - 54px);
  display: grid;
  align-content: center;
  gap: 22px;
}

.menu-screen h1 {
  max-width: 880px;
}

.menu-actions {
  display: grid;
  grid-template-columns: repeat(4, minmax(130px, 1fr));
  gap: 14px;
  max-width: 780px;
}

.menu-actions button {
  min-height: 68px;
  background: #ffffff;
}

.menu-actions button:first-child,
.menu-actions button:nth-child(3) {
  background: var(--lemon);
}

.menu-stats,
.panel-screen {
  border: 3px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.68);
  box-shadow: 6px 6px 0 var(--line);
}

.menu-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 14px 24px;
  width: fit-content;
  padding: 14px 16px;
  font-weight: 900;
}

.menu-stats p {
  margin: 0;
}

.panel-screen {
  min-height: calc(100vh - 54px);
  padding: 20px;
}

.screen-head {
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 16px;
  margin-bottom: 18px;
}

.screen-head h2 {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(2rem, 5vw, 4rem);
  line-height: 0.94;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(10, minmax(76px, 1fr));
  gap: 10px;
}

.level-button {
  display: grid;
  place-items: center;
  gap: 3px;
  min-height: 68px;
  padding: 8px 6px;
  background: #ffffff;
}

.level-button strong {
  font-size: 1.1rem;
}

.level-button span {
  font-size: 0.75rem;
}

.level-button.level-done {
  background: var(--mint);
}

.volume-control {
  display: grid;
  grid-template-columns: minmax(160px, 1fr) auto;
  gap: 14px;
  align-items: center;
  max-width: 560px;
  margin-bottom: 22px;
  font-size: 1.1rem;
  font-weight: 900;
}

.volume-control input {
  grid-column: 1 / -1;
  width: 100%;
  accent-color: var(--teal);
}

.hud {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(280px, 420px);
  align-items: end;
  gap: 18px;
  margin-bottom: 20px;
}

.eyebrow {
  margin: 0 0 0.35rem;
  color: var(--teal);
  font-size: 0.8rem;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  max-width: 760px;
  margin-bottom: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(2rem, 5vw, 4.7rem);
  line-height: 0.92;
}

.timer-panel {
  border: 2px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.66);
  padding: 14px;
  box-shadow: 6px 6px 0 var(--line);
}

.timer-copy {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
  font-weight: 900;
}

.timer-copy strong {
  color: var(--coral);
}

.time-track {
  height: 18px;
  overflow: hidden;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
}

.time-fill {
  width: 0%;
  height: 100%;
  background: linear-gradient(90deg, var(--mint), var(--lemon), var(--coral));
  transition: width 160ms linear;
}

.shelf-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(150px, 1fr));
  gap: 14px;
  touch-action: none;
}

.shelf-cell {
  position: relative;
  min-height: 178px;
  border: 3px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.82), rgba(241, 246, 234, 0.92)),
    var(--shelf);
  box-shadow: 6px 6px 0 var(--shadow);
  padding: 12px;
  transition:
    transform 180ms ease,
    background 180ms ease,
    box-shadow 180ms ease;
}

.shelf-cell::before {
  content: "";
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 45px;
  height: 8px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #bdd4c9;
}

.shelf-cell.complete {
  background:
    linear-gradient(180deg, rgba(127, 215, 178, 0.42), rgba(255, 255, 255, 0.86)),
    var(--shelf);
  box-shadow: 6px 6px 0 rgba(32, 125, 131, 0.34);
}

.shelf-cell.drop-target {
  transform: translateY(-3px);
  box-shadow: 8px 8px 0 var(--line);
}

.layer-stack {
  position: relative;
  z-index: 1;
  height: 136px;
}

.items-row {
  position: absolute;
  left: 0;
  right: 0;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  align-items: end;
  gap: 8px;
}

.layer-active {
  bottom: 0;
}

.layer-preview {
  top: 0;
  z-index: 0;
  height: 66px;
  overflow: hidden;
  opacity: 0.78;
  pointer-events: none;
  transform: scale(0.92);
  transform-origin: top center;
}

.layer-preview .item {
  cursor: default;
  filter: saturate(0.8) brightness(0.96);
  font-size: clamp(1.45rem, 3.2vw, 2.35rem);
}

.has-preview .layer-active {
  bottom: 0;
}

.empty-shelf {
  position: absolute;
  inset: 28px 18px 18px;
  display: grid;
  place-items: center;
  border: 2px dashed rgba(36, 65, 71, 0.5);
  border-radius: 8px;
  color: rgba(24, 33, 37, 0.62);
  font-weight: 900;
}

.layer-count {
  position: absolute;
  left: 8px;
  bottom: 8px;
  z-index: 2;
  min-width: 36px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  padding: 3px 7px;
  font-size: 0.78rem;
  font-weight: 900;
  line-height: 1.1;
  box-shadow: 2px 2px 0 var(--line);
}

.item {
  display: grid;
  place-items: center;
  width: 100%;
  aspect-ratio: 1;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: var(--item-bg, #ffffff);
  cursor: grab;
  user-select: none;
  font-size: clamp(1.8rem, 4vw, 3rem);
  box-shadow: 3px 3px 0 var(--line);
  transition:
    transform 120ms ease,
    opacity 120ms ease,
    filter 120ms ease;
}

.item:hover {
  transform: translateY(-3px) rotate(-2deg);
}

.item.dragging {
  opacity: 0.24;
}

.complete .item {
  cursor: default;
  filter: saturate(0.92);
}

.complete .item:hover {
  transform: none;
}

.checkmark {
  position: absolute;
  right: 8px;
  bottom: 8px;
  z-index: 2;
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: var(--mint);
  color: var(--ink);
  font-size: 1.35rem;
  font-weight: 900;
  box-shadow: 3px 3px 0 var(--line);
}

.drag-ghost {
  position: fixed;
  z-index: 30;
  pointer-events: none;
  width: 74px;
  height: 74px;
  display: grid;
  place-items: center;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  font-size: 2.8rem;
  box-shadow: 8px 8px 0 rgba(24, 33, 37, 0.28);
  transform: translate(-50%, -50%) rotate(4deg);
}

.footer-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  margin-top: 18px;
}

.game-actions,
.result-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: flex-end;
}

.footer-row p {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 900;
}

.result-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(24, 33, 37, 0.52);
}

.result-overlay[hidden] {
  display: none;
}

.result-dialog {
  width: min(420px, 100%);
  border: 3px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  padding: 26px;
  text-align: center;
  box-shadow: 10px 10px 0 var(--line);
}

.result-dialog h2 {
  margin-bottom: 0.7rem;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(2rem, 8vw, 3.4rem);
  line-height: 0.95;
}

.result-dialog p:not(.eyebrow) {
  margin-bottom: 1.2rem;
  font-weight: 700;
}

.result-actions {
  justify-content: center;
}

@media (max-width: 760px) {
  body.menu-active .game-shell {
    height: 100dvh;
    overflow: hidden;
  }

  .menu-screen {
    min-height: calc(100dvh - 12px);
    align-content: center;
    gap: 14px;
  }

  .menu-screen h1 {
    font-size: clamp(2rem, 13vw, 3.8rem);
  }

  body.game-active {
    height: 100dvh;
  }

  .game-shell {
    width: min(100vw - 10px, 560px);
    padding: 6px 0;
  }

  .hud {
    grid-template-columns: 1fr;
    align-items: stretch;
    gap: 7px;
    margin-bottom: 7px;
  }

  #gameView {
    height: calc(100dvh - 12px);
    min-height: 0;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
  }

  #gameView h1 {
    font-size: clamp(1.05rem, 5.6vw, 1.45rem);
    line-height: 1;
  }

  #gameView .eyebrow {
    margin-bottom: 0.12rem;
    font-size: 0.68rem;
  }

  #gameView .timer-panel {
    padding: 6px 8px;
    box-shadow: 3px 3px 0 var(--line);
  }

  #gameView .timer-copy {
    margin-bottom: 4px;
    font-size: 0.78rem;
  }

  #gameView .time-track {
    height: 10px;
  }

  .menu-actions {
    grid-template-columns: 1fr 1fr;
  }

  .panel-screen {
    padding: 14px;
  }

  .screen-head {
    align-items: stretch;
    flex-direction: column;
  }

  .level-grid {
    grid-template-columns: repeat(4, minmax(64px, 1fr));
  }

  .shelf-grid {
    min-height: 0;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-template-rows: repeat(4, minmax(0, 1fr));
    gap: 6px;
  }

  .shelf-cell {
    min-height: 0;
    height: 100%;
    border-width: 2px;
    padding: 5px;
    box-shadow: 2px 2px 0 var(--shadow);
  }

  .shelf-cell::before {
    display: none;
  }

  .layer-stack {
    height: calc(100% - 10px);
  }

  .layer-active {
    top: auto;
    bottom: 0;
    z-index: 2;
  }

  .layer-preview {
    top: auto;
    bottom: 24px;
    height: auto;
    overflow: visible;
    opacity: 0.72;
    transform: scale(0.96);
    transform-origin: bottom center;
    z-index: 1;
  }

  .items-row {
    gap: 2px;
  }

  .item {
    border-width: 1px;
    border-radius: 6px;
    box-shadow: 1px 1px 0 var(--line);
    font-size: clamp(1rem, 6vw, 1.6rem);
    line-height: 1;
    padding: 0;
  }

  .layer-preview .item {
    font-size: clamp(0.92rem, 5vw, 1.35rem);
  }

  .footer-row {
    align-items: stretch;
    flex-direction: column;
    gap: 5px;
    margin-top: 7px;
  }

  .footer-row p {
    font-size: 0.76rem;
    line-height: 1.1;
  }

  .game-actions {
    justify-content: stretch;
    gap: 5px;
  }

  .game-actions button,
  .result-actions button {
    flex: 1 1 auto;
  }

  #gameView .game-actions button {
    min-height: 30px;
    padding: 0.32rem 0.38rem;
    border-width: 1px;
    box-shadow: 2px 2px 0 var(--line);
    font-size: 0.76rem;
  }

  .layer-count {
    left: 4px;
    bottom: 4px;
    min-width: 24px;
    border-width: 1px;
    padding: 1px 4px;
    font-size: 0.58rem;
    box-shadow: 1px 1px 0 var(--line);
  }

  .checkmark {
    right: 4px;
    bottom: 4px;
    width: 22px;
    height: 22px;
    border-width: 1px;
    border-radius: 6px;
    font-size: 0.9rem;
    box-shadow: 1px 1px 0 var(--line);
  }
}

@media (max-width: 760px) and (orientation: landscape) {
  .game-shell {
    width: min(100vw - 8px, 920px);
    padding: 4px 0;
  }

  #gameView {
    height: calc(100dvh - 8px);
  }

  .hud {
    grid-template-columns: minmax(0, 1fr) minmax(170px, 240px);
    gap: 6px;
    margin-bottom: 5px;
  }

  #gameView h1 {
    font-size: clamp(0.95rem, 3.2vw, 1.25rem);
  }

  #gameView .timer-panel {
    padding: 5px 7px;
  }

  .shelf-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-template-rows: repeat(3, minmax(0, 1fr));
    gap: 4px;
  }

  .shelf-cell {
    padding: 3px;
  }

  .layer-stack {
    height: calc(100% - 9px);
  }

  .items-row {
    left: 14%;
    right: 14%;
    gap: 2px;
  }

  .layer-preview {
    bottom: 30px;
    height: auto;
    overflow: visible;
    transform: scale(0.8);
  }

  .item {
    font-size: clamp(0.68rem, 2.55vw, 0.95rem);
  }

  .layer-preview .item {
    font-size: clamp(0.56rem, 1.95vw, 0.78rem);
  }

  .footer-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    margin-top: 5px;
  }
}

@media (min-width: 761px) and (max-height: 520px) and (orientation: landscape) {
  body.game-active {
    height: 100dvh;
    overflow: hidden;
  }

  .game-shell {
    width: min(100vw - 10px, 980px);
    padding: 5px 0;
  }

  #gameView {
    height: calc(100dvh - 10px);
    min-height: 0;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
  }

  .hud {
    gap: 8px;
    margin-bottom: 5px;
  }

  #gameView h1 {
    font-size: clamp(1rem, 2.6vw, 1.45rem);
    line-height: 1;
  }

  #gameView .timer-panel {
    padding: 6px 8px;
    box-shadow: 3px 3px 0 var(--line);
  }

  #gameView .timer-copy {
    margin-bottom: 4px;
    font-size: 0.8rem;
  }

  #gameView .time-track {
    height: 10px;
  }

  .shelf-grid {
    min-height: 0;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-template-rows: repeat(3, minmax(0, 1fr));
    gap: 5px;
  }

  .shelf-cell {
    min-height: 0;
    height: 100%;
    border-width: 2px;
    padding: 3px;
    box-shadow: 2px 2px 0 var(--shadow);
  }

  .shelf-cell::before {
    display: none;
  }

  .layer-stack {
    height: calc(100% - 10px);
  }

  .layer-active {
    top: auto;
    bottom: 0;
    z-index: 2;
  }

  .layer-preview {
    bottom: 34px;
    height: auto;
    overflow: visible;
    opacity: 0.72;
    transform: scale(0.8);
    transform-origin: bottom center;
    z-index: 1;
  }

  .items-row {
    left: 14%;
    right: 14%;
    gap: 2px;
  }

  .item {
    border-width: 1px;
    border-radius: 6px;
    box-shadow: 1px 1px 0 var(--line);
    font-size: clamp(0.68rem, 2.2vw, 0.95rem);
    line-height: 1;
    padding: 0;
  }

  .layer-preview .item {
    font-size: clamp(0.56rem, 1.7vw, 0.78rem);
  }

  .footer-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 6px;
    margin-top: 5px;
  }

  .footer-row p {
    font-size: 0.78rem;
    line-height: 1.1;
  }

  #gameView .game-actions button {
    min-height: 30px;
    padding: 0.32rem 0.45rem;
    border-width: 1px;
    box-shadow: 2px 2px 0 var(--line);
    font-size: 0.76rem;
  }
}
