:root {
  --white: #ffffff;
  --off-white: #f5f5f5;
  --light-gray: #e0e0e0;
  --medium-gray: #b0b0b0;
  --dark-gray: #404040;
  --black: #000000;
  --red: #d32f2f;
  --dark-red: #b71c1c;
  --green: color(display-p3 0 1 0.6);
  --blue: #0000b6;
  --yellow: #ffe056;
}

/* --- Global Styles --- */
* {
  box-sizing: border-box;
}

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: var(--off-white);
  margin: 0;
  padding: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom));
  gap: 8px;
  user-select: none;
  height: 100dvh;
  overflow: hidden;
}

/* --- Board Scaling --- */
#board-wrapper {
  flex: 1;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  container-type: size;
}

#board {
  /* 60% of 1x1 piece */
  --b-multiplier: 0.6;
  --aspect-w: calc(4 + (2 * var(--b-multiplier)));
  --aspect-h: calc(5 + (2 * var(--b-multiplier)));
  --ratio: calc(var(--aspect-w) / var(--aspect-h));
  aspect-ratio: var(--ratio);
  width: min(100%, calc(100cqh * var(--ratio)));
  max-width: 100%;
  max-height: 100%;
  margin: 0 auto;
  background: var(--dark-gray);
  position: relative;
  box-sizing: border-box;
  border-radius: 8px;
  box-shadow: 0 5px 15px color-mix(in srgb, var(--black) 20%, transparent);
  overflow: hidden;
  flex-shrink: 0;
}

#play-area {
  position: absolute;
  top: calc(100% * (var(--b-multiplier) / var(--aspect-h)));
  bottom: calc(100% * (var(--b-multiplier) / var(--aspect-h)));
  left: calc(100% * (var(--b-multiplier) / var(--aspect-w)));
  right: calc(100% * (var(--b-multiplier) / var(--aspect-w)));
  background: var(--medium-gray);
  touch-action: none;
}

#edit-panel {
  width: 100%;
  max-width: 500px;
  padding: 4px 0;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.action-buttons {
  display: flex;
  gap: 8px;
  width: 100%;
  justify-content: center;
}

/* --- Palette --- */
.palette-area {
  display: flex;
  gap: clamp(10px, 3vw, 20px);
  align-items: center;
  height: 100px;
}

.palette-piece {
  border: 2px solid var(--dark-gray);
  border-radius: 4px;
  cursor: grab;
  touch-action: none;
}

.palette-piece.C {
  width: 88px;
  height: 88px;
  background: var(--red);
}

.palette-piece.V {
  width: 44px;
  height: 88px;
  background: var(--green);
}

.palette-piece.H {
  width: 88px;
  height: 44px;
  background: var(--blue);
}

.palette-piece.S {
  width: 44px;
  height: 44px;
  background: var(--yellow);
}

.palette-piece.active {
  outline: 3px solid var(--dark-gray);
  outline-offset: 2px;
}

button {
  padding: clamp(10px, 1.4vh, 14px) clamp(10px, 2.8vw, 20px);
  font-weight: bold;
  border-radius: 6px;
  border: none;
  background: var(--dark-gray);
  color: var(--white);
  cursor: pointer;
  font-size: clamp(13px, 3.4vw, 16px);
  white-space: nowrap;
}

@media (hover: hover) {
  button:hover {
    background: var(--black);
  }
  .btn-red:hover {
    background: var(--dark-red);
  }
  .btn-primary:hover {
    background: var(--black);
    filter: brightness(0.9);
  }
}

button:disabled {
  background: var(--light-gray);
  cursor: not-allowed;
  color: var(--medium-gray);
}

.btn-primary {
  background: var(--dark-gray);
  color: var(--white);
}

.btn-red {
  background: var(--red);
}

#step-controls {
  display: flex;
  gap: clamp(6px, 1.8vw, 10px);
  align-items: stretch;
  width: 100%;
  justify-content: center;
}

#step-controls button {
  flex: 1;
  min-width: 0;
  min-height: 46px;
  padding: 10px 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}

#step-controls button svg {
  width: 22px;
  height: 22px;
  display: block;
}

#stepDisplay {
  font-weight: bold;
  min-width: 64px;
  text-align: center;
  font-size: clamp(14px, 3.6vw, 18px);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* --- Pieces --- */
.piece {
  position: absolute;
  box-sizing: border-box;
  border: 2px solid var(--dark-gray);
  border-radius: 6px;
  z-index: 10;
  touch-action: none;
}

.piece.C {
  background: var(--red);
}

.piece.V {
  background: var(--green);
}

.piece.H {
  background: var(--blue);
}

.piece.S {
  background: var(--yellow);
}

#play-area.animate-pieces .piece {
  transition: top var(--move-duration, 200ms) ease, left var(--move-duration, 200ms) ease;
}

.exit-marker {
  position: absolute;
  border-left: 4px dashed var(--light-gray);
  border-right: 4px dashed var(--light-gray);
  background: color-mix(in srgb, var(--light-gray) 20%, transparent);
  z-index: 5;
  touch-action: none;
}

#status {
  position: fixed;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: color-mix(in srgb, var(--white) 95%, transparent);
  padding: 8px 16px;
  border-radius: 8px;
  box-shadow: 0 4px 12px color-mix(in srgb, var(--black) 20%, transparent);
  font-weight: bold;
  font-size: clamp(13px, 1.8vw, 16px);
  z-index: 1000;
  pointer-events: none;
  text-align: center;
  max-width: calc(100vw - 24px);
}

#status:empty {
  display: none;
}

#ghost {
  position: fixed;
  pointer-events: none;
  opacity: 0.7;
  z-index: 9999;
  display: none;
  align-items: center;
  justify-content: center;
  box-shadow: 0 10px 20px color-mix(in srgb, var(--black) 50%, transparent);
}

#ghost.target-ghost {
  background: color-mix(in srgb, var(--light-gray) 50%, transparent);
  border-left: 4px dashed var(--light-gray);
  border-right: 4px dashed var(--light-gray);
}

#ghost.delete-ready {
  opacity: 0.55;
  outline: 3px solid var(--red);
  outline-offset: 1px;
}

#ghost.delete-ready::after {
  content: "✕";
  color: var(--white);
  background: var(--red);
  width: 26px;
  height: 26px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  font-weight: bold;
  box-shadow: 0 2px 6px color-mix(in srgb, var(--black) 35%, transparent);
}
